Home | History | Annotate | Line # | Download | only in usb
uhci.c revision 1.153
      1 /*	$NetBSD: uhci.c,v 1.153 2002/02/11 11:40:33 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.153 2002/02/11 11:40:33 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);
    418 	UWRITE2(sc, UHCI_FRNUM, 0);		/* set frame number to 0 */
    419 	UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma)); /* 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, xfer, 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, xfer, 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));
    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);
    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 	if (sc->sc_softwake) {
   1277 		sc->sc_softwake = 0;
   1278 		wakeup(&sc->sc_softwake);
   1279 	}
   1280 
   1281 	sc->sc_bus.intr_context--;
   1282 }
   1283 
   1284 /* Check for an interrupt. */
   1285 void
   1286 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
   1287 {
   1288 	uhci_soft_td_t *std, *lstd;
   1289 	u_int32_t status;
   1290 
   1291 	DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
   1292 #ifdef DIAGNOSTIC
   1293 	if (ii == NULL) {
   1294 		printf("uhci_check_intr: no ii? %p\n", ii);
   1295 		return;
   1296 	}
   1297 #endif
   1298 	if (ii->stdstart == NULL)
   1299 		return;
   1300 	lstd = ii->stdend;
   1301 #ifdef DIAGNOSTIC
   1302 	if (lstd == NULL) {
   1303 		printf("uhci_check_intr: std==0\n");
   1304 		return;
   1305 	}
   1306 #endif
   1307 	/*
   1308 	 * If the last TD is still active we need to check whether there
   1309 	 * is a an error somewhere in the middle, or whether there was a
   1310 	 * short packet (SPD and not ACTIVE).
   1311 	 */
   1312 	if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
   1313 		DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
   1314 		for (std = ii->stdstart; std != lstd; std = std->link.std) {
   1315 			status = le32toh(std->td.td_status);
   1316 			/* If there's an active TD the xfer isn't done. */
   1317 			if (status & UHCI_TD_ACTIVE)
   1318 				break;
   1319 			/* Any kind of error makes the xfer done. */
   1320 			if (status & UHCI_TD_STALLED)
   1321 				goto done;
   1322 			/* We want short packets, and it is short: it's done */
   1323 			if ((status & UHCI_TD_SPD) &&
   1324 			      UHCI_TD_GET_ACTLEN(status) <
   1325 			      UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)))
   1326 				goto done;
   1327 		}
   1328 		DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
   1329 			      ii, ii->stdstart));
   1330 		return;
   1331 	}
   1332  done:
   1333 	DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
   1334 	usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
   1335 	uhci_idone(ii);
   1336 }
   1337 
   1338 /* Called at splusb() */
   1339 void
   1340 uhci_idone(uhci_intr_info_t *ii)
   1341 {
   1342 	usbd_xfer_handle xfer = ii->xfer;
   1343 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1344 	uhci_soft_td_t *std;
   1345 	u_int32_t status = 0, nstatus;
   1346 	int actlen;
   1347 
   1348 	DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
   1349 #ifdef DIAGNOSTIC
   1350 	{
   1351 		int s = splhigh();
   1352 		if (ii->isdone) {
   1353 			splx(s);
   1354 #ifdef UHCI_DEBUG
   1355 			printf("uhci_idone: ii is done!\n   ");
   1356 			uhci_dump_ii(ii);
   1357 #else
   1358 			printf("uhci_idone: ii=%p is done!\n", ii);
   1359 #endif
   1360 			return;
   1361 		}
   1362 		ii->isdone = 1;
   1363 		splx(s);
   1364 	}
   1365 #endif
   1366 
   1367 	if (xfer->status == USBD_CANCELLED ||
   1368 	    xfer->status == USBD_TIMEOUT) {
   1369 		DPRINTF(("uhci_idone: aborted xfer=%p\n", xfer));
   1370 		return;
   1371 	}
   1372 
   1373 	if (xfer->nframes != 0) {
   1374 		/* Isoc transfer, do things differently. */
   1375 		uhci_soft_td_t **stds = upipe->u.iso.stds;
   1376 		int i, n, nframes, len;
   1377 
   1378 		DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
   1379 
   1380 		nframes = xfer->nframes;
   1381 		actlen = 0;
   1382 		n = UXFER(xfer)->curframe;
   1383 		for (i = 0; i < nframes; i++) {
   1384 			std = stds[n];
   1385 #ifdef UHCI_DEBUG
   1386 			if (uhcidebug > 5) {
   1387 				DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
   1388 				uhci_dump_td(std);
   1389 			}
   1390 #endif
   1391 			if (++n >= UHCI_VFRAMELIST_COUNT)
   1392 				n = 0;
   1393 			status = le32toh(std->td.td_status);
   1394 			len = UHCI_TD_GET_ACTLEN(status);
   1395 			xfer->frlengths[i] = len;
   1396 			actlen += len;
   1397 		}
   1398 		upipe->u.iso.inuse -= nframes;
   1399 		xfer->actlen = actlen;
   1400 		xfer->status = USBD_NORMAL_COMPLETION;
   1401 		goto end;
   1402 	}
   1403 
   1404 #ifdef UHCI_DEBUG
   1405 	DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
   1406 		      ii, xfer, upipe));
   1407 	if (uhcidebug > 10)
   1408 		uhci_dump_tds(ii->stdstart);
   1409 #endif
   1410 
   1411 	/* The transfer is done, compute actual length and status. */
   1412 	actlen = 0;
   1413 	for (std = ii->stdstart; std != NULL; std = std->link.std) {
   1414 		nstatus = le32toh(std->td.td_status);
   1415 		if (nstatus & UHCI_TD_ACTIVE)
   1416 			break;
   1417 
   1418 		status = nstatus;
   1419 		if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
   1420 			UHCI_TD_PID_SETUP)
   1421 			actlen += UHCI_TD_GET_ACTLEN(status);
   1422 	}
   1423 	/* If there are left over TDs we need to update the toggle. */
   1424 	if (std != NULL)
   1425 		upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
   1426 
   1427 	status &= UHCI_TD_ERROR;
   1428 	DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
   1429 		      actlen, status));
   1430 	xfer->actlen = actlen;
   1431 	if (status != 0) {
   1432 #ifdef UHCI_DEBUG
   1433 		char sbuf[128];
   1434 
   1435 		bitmask_snprintf((u_int32_t)status,
   1436 				 "\20\22BITSTUFF\23CRCTO\24NAK\25"
   1437 				 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
   1438 				 sbuf, sizeof(sbuf));
   1439 
   1440 		DPRINTFN((status == UHCI_TD_STALLED)*10,
   1441 			 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
   1442 			  "status 0x%s\n",
   1443 			  xfer->pipe->device->address,
   1444 			  xfer->pipe->endpoint->edesc->bEndpointAddress,
   1445 			  sbuf));
   1446 #endif
   1447 
   1448 		if (status == UHCI_TD_STALLED)
   1449 			xfer->status = USBD_STALLED;
   1450 		else
   1451 			xfer->status = USBD_IOERROR; /* more info XXX */
   1452 	} else {
   1453 		xfer->status = USBD_NORMAL_COMPLETION;
   1454 	}
   1455 
   1456  end:
   1457 	usb_transfer_complete(xfer);
   1458 	DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
   1459 }
   1460 
   1461 /*
   1462  * Called when a request does not complete.
   1463  */
   1464 void
   1465 uhci_timeout(void *addr)
   1466 {
   1467 	uhci_intr_info_t *ii = addr;
   1468 	struct uhci_xfer *uxfer = UXFER(ii->xfer);
   1469 	struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
   1470 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
   1471 
   1472 	DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
   1473 
   1474 	if (sc->sc_dying) {
   1475 		uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
   1476 		return;
   1477 	}
   1478 
   1479 	/* Execute the abort in a process context. */
   1480 	usb_init_task(&uxfer->abort_task, uhci_timeout_task, addr);
   1481 	usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
   1482 }
   1483 
   1484 void
   1485 uhci_timeout_task(void *addr)
   1486 {
   1487 	usbd_xfer_handle xfer = addr;
   1488 	int s;
   1489 
   1490 	DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
   1491 
   1492 	s = splusb();
   1493 	uhci_abort_xfer(xfer, USBD_TIMEOUT);
   1494 	splx(s);
   1495 }
   1496 
   1497 /*
   1498  * Wait here until controller claims to have an interrupt.
   1499  * Then call uhci_intr and return.  Use timeout to avoid waiting
   1500  * too long.
   1501  * Only used during boot when interrupts are not enabled yet.
   1502  */
   1503 void
   1504 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
   1505 {
   1506 	int timo = xfer->timeout;
   1507 	uhci_intr_info_t *ii;
   1508 
   1509 	DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
   1510 
   1511 	xfer->status = USBD_IN_PROGRESS;
   1512 	for (; timo >= 0; timo--) {
   1513 		usb_delay_ms(&sc->sc_bus, 1);
   1514 		DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
   1515 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
   1516 			uhci_intr1(sc);
   1517 			if (xfer->status != USBD_IN_PROGRESS)
   1518 				return;
   1519 		}
   1520 	}
   1521 
   1522 	/* Timeout */
   1523 	DPRINTF(("uhci_waitintr: timeout\n"));
   1524 	for (ii = LIST_FIRST(&sc->sc_intrhead);
   1525 	     ii != NULL && ii->xfer != xfer;
   1526 	     ii = LIST_NEXT(ii, list))
   1527 		;
   1528 #ifdef DIAGNOSTIC
   1529 	if (ii == NULL)
   1530 		panic("uhci_waitintr: lost intr_info\n");
   1531 #endif
   1532 	uhci_idone(ii);
   1533 }
   1534 
   1535 void
   1536 uhci_poll(struct usbd_bus *bus)
   1537 {
   1538 	uhci_softc_t *sc = (uhci_softc_t *)bus;
   1539 
   1540 	if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
   1541 		uhci_intr1(sc);
   1542 }
   1543 
   1544 void
   1545 uhci_reset(uhci_softc_t *sc)
   1546 {
   1547 	int n;
   1548 
   1549 	UHCICMD(sc, UHCI_CMD_HCRESET);
   1550 	/* The reset bit goes low when the controller is done. */
   1551 	for (n = 0; n < UHCI_RESET_TIMEOUT &&
   1552 		    (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
   1553 		usb_delay_ms(&sc->sc_bus, 1);
   1554 	if (n >= UHCI_RESET_TIMEOUT)
   1555 		printf("%s: controller did not reset\n",
   1556 		       USBDEVNAME(sc->sc_bus.bdev));
   1557 }
   1558 
   1559 usbd_status
   1560 uhci_run(uhci_softc_t *sc, int run)
   1561 {
   1562 	int s, n, running;
   1563 	u_int16_t cmd;
   1564 
   1565 	run = run != 0;
   1566 	s = splhardusb();
   1567 	DPRINTF(("uhci_run: setting run=%d\n", run));
   1568 	cmd = UREAD2(sc, UHCI_CMD);
   1569 	if (run)
   1570 		cmd |= UHCI_CMD_RS;
   1571 	else
   1572 		cmd &= ~UHCI_CMD_RS;
   1573 	UHCICMD(sc, cmd);
   1574 	for(n = 0; n < 10; n++) {
   1575 		running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
   1576 		/* return when we've entered the state we want */
   1577 		if (run == running) {
   1578 			splx(s);
   1579 			DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
   1580 				 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
   1581 			return (USBD_NORMAL_COMPLETION);
   1582 		}
   1583 		usb_delay_ms(&sc->sc_bus, 1);
   1584 	}
   1585 	splx(s);
   1586 	printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
   1587 	       run ? "start" : "stop");
   1588 	return (USBD_IOERROR);
   1589 }
   1590 
   1591 /*
   1592  * Memory management routines.
   1593  *  uhci_alloc_std allocates TDs
   1594  *  uhci_alloc_sqh allocates QHs
   1595  * These two routines do their own free list management,
   1596  * partly for speed, partly because allocating DMAable memory
   1597  * has page size granularaity so much memory would be wasted if
   1598  * only one TD/QH (32 bytes) was placed in each allocated chunk.
   1599  */
   1600 
   1601 uhci_soft_td_t *
   1602 uhci_alloc_std(uhci_softc_t *sc)
   1603 {
   1604 	uhci_soft_td_t *std;
   1605 	usbd_status err;
   1606 	int i, offs;
   1607 	usb_dma_t dma;
   1608 
   1609 	if (sc->sc_freetds == NULL) {
   1610 		DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
   1611 		err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
   1612 			  UHCI_TD_ALIGN, &dma);
   1613 		if (err)
   1614 			return (0);
   1615 		for(i = 0; i < UHCI_STD_CHUNK; i++) {
   1616 			offs = i * UHCI_STD_SIZE;
   1617 			std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
   1618 			std->physaddr = DMAADDR(&dma) + offs;
   1619 			std->link.std = sc->sc_freetds;
   1620 			sc->sc_freetds = std;
   1621 		}
   1622 	}
   1623 	std = sc->sc_freetds;
   1624 	sc->sc_freetds = std->link.std;
   1625 	memset(&std->td, 0, sizeof(uhci_td_t));
   1626 	return std;
   1627 }
   1628 
   1629 void
   1630 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
   1631 {
   1632 #ifdef DIAGNOSTIC
   1633 #define TD_IS_FREE 0x12345678
   1634 	if (le32toh(std->td.td_token) == TD_IS_FREE) {
   1635 		printf("uhci_free_std: freeing free TD %p\n", std);
   1636 		return;
   1637 	}
   1638 	std->td.td_token = htole32(TD_IS_FREE);
   1639 #endif
   1640 	std->link.std = sc->sc_freetds;
   1641 	sc->sc_freetds = std;
   1642 }
   1643 
   1644 uhci_soft_qh_t *
   1645 uhci_alloc_sqh(uhci_softc_t *sc)
   1646 {
   1647 	uhci_soft_qh_t *sqh;
   1648 	usbd_status err;
   1649 	int i, offs;
   1650 	usb_dma_t dma;
   1651 
   1652 	if (sc->sc_freeqhs == NULL) {
   1653 		DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
   1654 		err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
   1655 			  UHCI_QH_ALIGN, &dma);
   1656 		if (err)
   1657 			return (0);
   1658 		for(i = 0; i < UHCI_SQH_CHUNK; i++) {
   1659 			offs = i * UHCI_SQH_SIZE;
   1660 			sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
   1661 			sqh->physaddr = DMAADDR(&dma) + offs;
   1662 			sqh->hlink = sc->sc_freeqhs;
   1663 			sc->sc_freeqhs = sqh;
   1664 		}
   1665 	}
   1666 	sqh = sc->sc_freeqhs;
   1667 	sc->sc_freeqhs = sqh->hlink;
   1668 	memset(&sqh->qh, 0, sizeof(uhci_qh_t));
   1669 	return (sqh);
   1670 }
   1671 
   1672 void
   1673 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   1674 {
   1675 	sqh->hlink = sc->sc_freeqhs;
   1676 	sc->sc_freeqhs = sqh;
   1677 }
   1678 
   1679 void
   1680 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
   1681 		    uhci_soft_td_t *stdend)
   1682 {
   1683 	uhci_soft_td_t *p;
   1684 
   1685 	for (; std != stdend; std = p) {
   1686 		p = std->link.std;
   1687 		uhci_free_std(sc, std);
   1688 	}
   1689 }
   1690 
   1691 usbd_status
   1692 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
   1693 		     int rd, u_int16_t flags, usb_dma_t *dma,
   1694 		     uhci_soft_td_t **sp, uhci_soft_td_t **ep)
   1695 {
   1696 	uhci_soft_td_t *p, *lastp;
   1697 	uhci_physaddr_t lastlink;
   1698 	int i, ntd, l, tog, maxp;
   1699 	u_int32_t status;
   1700 	int addr = upipe->pipe.device->address;
   1701 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1702 
   1703 	DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
   1704 		      "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
   1705 		      upipe->pipe.device->speed, flags));
   1706 	maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
   1707 	if (maxp == 0) {
   1708 		printf("uhci_alloc_std_chain: maxp=0\n");
   1709 		return (USBD_INVAL);
   1710 	}
   1711 	ntd = (len + maxp - 1) / maxp;
   1712 	if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
   1713 		ntd++;
   1714 	DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
   1715 	if (ntd == 0) {
   1716 		*sp = *ep = 0;
   1717 		DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
   1718 		return (USBD_NORMAL_COMPLETION);
   1719 	}
   1720 	tog = upipe->nexttoggle;
   1721 	if (ntd % 2 == 0)
   1722 		tog ^= 1;
   1723 	upipe->nexttoggle = tog ^ 1;
   1724 	lastp = NULL;
   1725 	lastlink = UHCI_PTR_T;
   1726 	ntd--;
   1727 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
   1728 	if (upipe->pipe.device->speed == USB_SPEED_LOW)
   1729 		status |= UHCI_TD_LS;
   1730 	if (flags & USBD_SHORT_XFER_OK)
   1731 		status |= UHCI_TD_SPD;
   1732 	for (i = ntd; i >= 0; i--) {
   1733 		p = uhci_alloc_std(sc);
   1734 		if (p == NULL) {
   1735 			uhci_free_std_chain(sc, lastp, NULL);
   1736 			return (USBD_NOMEM);
   1737 		}
   1738 		p->link.std = lastp;
   1739 		p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
   1740 		lastp = p;
   1741 		lastlink = p->physaddr;
   1742 		p->td.td_status = htole32(status);
   1743 		if (i == ntd) {
   1744 			/* last TD */
   1745 			l = len % maxp;
   1746 			if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
   1747 				l = maxp;
   1748 			*ep = p;
   1749 		} else
   1750 			l = maxp;
   1751 		p->td.td_token =
   1752 		    htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
   1753 				 UHCI_TD_OUT(l, endpt, addr, tog));
   1754 		p->td.td_buffer = htole32(DMAADDR(dma) + i * maxp);
   1755 		tog ^= 1;
   1756 	}
   1757 	*sp = lastp;
   1758 	DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
   1759 		      upipe->nexttoggle));
   1760 	return (USBD_NORMAL_COMPLETION);
   1761 }
   1762 
   1763 void
   1764 uhci_device_clear_toggle(usbd_pipe_handle pipe)
   1765 {
   1766 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1767 	upipe->nexttoggle = 0;
   1768 }
   1769 
   1770 void
   1771 uhci_noop(usbd_pipe_handle pipe)
   1772 {
   1773 }
   1774 
   1775 usbd_status
   1776 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
   1777 {
   1778 	usbd_status err;
   1779 
   1780 	/* Insert last in queue. */
   1781 	err = usb_insert_transfer(xfer);
   1782 	if (err)
   1783 		return (err);
   1784 
   1785 	/*
   1786 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   1787 	 * so start it first.
   1788 	 */
   1789 	return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1790 }
   1791 
   1792 usbd_status
   1793 uhci_device_bulk_start(usbd_xfer_handle xfer)
   1794 {
   1795 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1796 	usbd_device_handle dev = upipe->pipe.device;
   1797 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1798 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   1799 	uhci_soft_td_t *data, *dataend;
   1800 	uhci_soft_qh_t *sqh;
   1801 	usbd_status err;
   1802 	int len, isread, endpt;
   1803 	int s;
   1804 
   1805 	DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
   1806 		     xfer, xfer->length, xfer->flags));
   1807 
   1808 	if (sc->sc_dying)
   1809 		return (USBD_IOERROR);
   1810 
   1811 #ifdef DIAGNOSTIC
   1812 	if (xfer->rqflags & URQ_REQUEST)
   1813 		panic("uhci_device_bulk_transfer: a request\n");
   1814 #endif
   1815 
   1816 	len = xfer->length;
   1817 	endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1818 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   1819 	sqh = upipe->u.bulk.sqh;
   1820 
   1821 	upipe->u.bulk.isread = isread;
   1822 	upipe->u.bulk.length = len;
   1823 
   1824 	err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
   1825 				   &xfer->dmabuf, &data, &dataend);
   1826 	if (err)
   1827 		return (err);
   1828 	dataend->td.td_status |= htole32(UHCI_TD_IOC);
   1829 
   1830 #ifdef UHCI_DEBUG
   1831 	if (uhcidebug > 8) {
   1832 		DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
   1833 		uhci_dump_tds(data);
   1834 	}
   1835 #endif
   1836 
   1837 	/* Set up interrupt info. */
   1838 	ii->xfer = xfer;
   1839 	ii->stdstart = data;
   1840 	ii->stdend = dataend;
   1841 #ifdef DIAGNOSTIC
   1842 	if (!ii->isdone) {
   1843 		printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
   1844 	}
   1845 	ii->isdone = 0;
   1846 #endif
   1847 
   1848 	sqh->elink = data;
   1849 	sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
   1850 
   1851 	s = splusb();
   1852 	uhci_add_bulk(sc, sqh);
   1853 	uhci_add_intr_info(sc, ii);
   1854 
   1855 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   1856 		usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
   1857 			    uhci_timeout, ii);
   1858 	}
   1859 	xfer->status = USBD_IN_PROGRESS;
   1860 	splx(s);
   1861 
   1862 #ifdef UHCI_DEBUG
   1863 	if (uhcidebug > 10) {
   1864 		DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
   1865 		uhci_dump_tds(data);
   1866 	}
   1867 #endif
   1868 
   1869 	if (sc->sc_bus.use_polling)
   1870 		uhci_waitintr(sc, xfer);
   1871 
   1872 	return (USBD_IN_PROGRESS);
   1873 }
   1874 
   1875 /* Abort a device bulk request. */
   1876 void
   1877 uhci_device_bulk_abort(usbd_xfer_handle xfer)
   1878 {
   1879 	DPRINTF(("uhci_device_bulk_abort:\n"));
   1880 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   1881 }
   1882 
   1883 /*
   1884  * XXX This way of aborting is neither safe, nor good.
   1885  * But it will have to do until I figure out what to do.
   1886  * I apologize for the delay().
   1887  */
   1888 void
   1889 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
   1890 {
   1891 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   1892 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1893 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
   1894 	uhci_soft_td_t *std;
   1895 	int s;
   1896 
   1897 	DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
   1898 
   1899 	if (sc->sc_dying) {
   1900 		/* If we're dying, just do the software part. */
   1901 		s = splusb();
   1902 		xfer->status = status;	/* make software ignore it */
   1903 		usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
   1904 		usb_transfer_complete(xfer);
   1905 		splx(s);
   1906 	}
   1907 
   1908 	if (xfer->device->bus->intr_context || !curproc)
   1909 		panic("ohci_abort_xfer: not in process context\n");
   1910 
   1911 	s = splusb();
   1912 
   1913 	/*
   1914 	 * Step 1: Make interrupt routine and hardware ignore xfer.
   1915 	 */
   1916 	s = splusb();
   1917 	xfer->status = status;	/* make software ignore it */
   1918 	usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
   1919 	DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
   1920 	for (std = ii->stdstart; std != NULL; std = std->link.std)
   1921 		std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
   1922 	splx(s);
   1923 
   1924 	/*
   1925 	 * Step 2: Wait until we know hardware has finished any possible
   1926 	 * use of the xfer.  Also make sure the soft interrupt routine
   1927 	 * has run.
   1928 	 */
   1929 	usb_delay_ms(upipe->pipe.device->bus, 20); /* Hardware finishes in 1ms */
   1930 	s = splusb();
   1931 	sc->sc_softwake = 1;
   1932 	usb_schedsoftintr(&sc->sc_bus);
   1933 	tsleep(&sc->sc_softwake, PZERO, "uhciab", 0);
   1934 	splx(s);
   1935 
   1936 	/*
   1937 	 * Step 3: Execute callback.
   1938 	 */
   1939 	xfer->hcpriv = ii;
   1940 
   1941 	s = splusb();
   1942 #ifdef DIAGNOSTIC
   1943 	ii->isdone = 1;
   1944 #endif
   1945 	usb_transfer_complete(xfer);
   1946 	splx(s);
   1947 }
   1948 
   1949 /* Close a device bulk pipe. */
   1950 void
   1951 uhci_device_bulk_close(usbd_pipe_handle pipe)
   1952 {
   1953 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1954 	usbd_device_handle dev = upipe->pipe.device;
   1955 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1956 
   1957 	uhci_free_sqh(sc, upipe->u.bulk.sqh);
   1958 }
   1959 
   1960 usbd_status
   1961 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
   1962 {
   1963 	usbd_status err;
   1964 
   1965 	/* Insert last in queue. */
   1966 	err = usb_insert_transfer(xfer);
   1967 	if (err)
   1968 		return (err);
   1969 
   1970 	/*
   1971 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   1972 	 * so start it first.
   1973 	 */
   1974 	return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1975 }
   1976 
   1977 usbd_status
   1978 uhci_device_ctrl_start(usbd_xfer_handle xfer)
   1979 {
   1980 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   1981 	usbd_status err;
   1982 
   1983 	if (sc->sc_dying)
   1984 		return (USBD_IOERROR);
   1985 
   1986 #ifdef DIAGNOSTIC
   1987 	if (!(xfer->rqflags & URQ_REQUEST))
   1988 		panic("uhci_device_ctrl_transfer: not a request\n");
   1989 #endif
   1990 
   1991 	err = uhci_device_request(xfer);
   1992 	if (err)
   1993 		return (err);
   1994 
   1995 	if (sc->sc_bus.use_polling)
   1996 		uhci_waitintr(sc, xfer);
   1997 	return (USBD_IN_PROGRESS);
   1998 }
   1999 
   2000 usbd_status
   2001 uhci_device_intr_transfer(usbd_xfer_handle xfer)
   2002 {
   2003 	usbd_status err;
   2004 
   2005 	/* Insert last in queue. */
   2006 	err = usb_insert_transfer(xfer);
   2007 	if (err)
   2008 		return (err);
   2009 
   2010 	/*
   2011 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   2012 	 * so start it first.
   2013 	 */
   2014 	return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2015 }
   2016 
   2017 usbd_status
   2018 uhci_device_intr_start(usbd_xfer_handle xfer)
   2019 {
   2020 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2021 	usbd_device_handle dev = upipe->pipe.device;
   2022 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2023 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2024 	uhci_soft_td_t *data, *dataend;
   2025 	uhci_soft_qh_t *sqh;
   2026 	usbd_status err;
   2027 	int i, s;
   2028 
   2029 	if (sc->sc_dying)
   2030 		return (USBD_IOERROR);
   2031 
   2032 	DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
   2033 		    xfer, xfer->length, xfer->flags));
   2034 
   2035 #ifdef DIAGNOSTIC
   2036 	if (xfer->rqflags & URQ_REQUEST)
   2037 		panic("uhci_device_intr_transfer: a request\n");
   2038 #endif
   2039 
   2040 	err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
   2041 				   &xfer->dmabuf, &data, &dataend);
   2042 	if (err)
   2043 		return (err);
   2044 	dataend->td.td_status |= htole32(UHCI_TD_IOC);
   2045 
   2046 #ifdef UHCI_DEBUG
   2047 	if (uhcidebug > 10) {
   2048 		DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
   2049 		uhci_dump_tds(data);
   2050 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   2051 	}
   2052 #endif
   2053 
   2054 	s = splusb();
   2055 	/* Set up interrupt info. */
   2056 	ii->xfer = xfer;
   2057 	ii->stdstart = data;
   2058 	ii->stdend = dataend;
   2059 #ifdef DIAGNOSTIC
   2060 	if (!ii->isdone) {
   2061 		printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
   2062 	}
   2063 	ii->isdone = 0;
   2064 #endif
   2065 
   2066 	DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
   2067 		     upipe->u.intr.qhs[0]));
   2068 	for (i = 0; i < upipe->u.intr.npoll; i++) {
   2069 		sqh = upipe->u.intr.qhs[i];
   2070 		sqh->elink = data;
   2071 		sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
   2072 	}
   2073 	uhci_add_intr_info(sc, ii);
   2074 	xfer->status = USBD_IN_PROGRESS;
   2075 	splx(s);
   2076 
   2077 #ifdef UHCI_DEBUG
   2078 	if (uhcidebug > 10) {
   2079 		DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
   2080 		uhci_dump_tds(data);
   2081 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   2082 	}
   2083 #endif
   2084 
   2085 	return (USBD_IN_PROGRESS);
   2086 }
   2087 
   2088 /* Abort a device control request. */
   2089 void
   2090 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
   2091 {
   2092 	DPRINTF(("uhci_device_ctrl_abort:\n"));
   2093 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   2094 }
   2095 
   2096 /* Close a device control pipe. */
   2097 void
   2098 uhci_device_ctrl_close(usbd_pipe_handle pipe)
   2099 {
   2100 }
   2101 
   2102 /* Abort a device interrupt request. */
   2103 void
   2104 uhci_device_intr_abort(usbd_xfer_handle xfer)
   2105 {
   2106 	DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
   2107 	if (xfer->pipe->intrxfer == xfer) {
   2108 		DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
   2109 		xfer->pipe->intrxfer = 0;
   2110 	}
   2111 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   2112 }
   2113 
   2114 /* Close a device interrupt pipe. */
   2115 void
   2116 uhci_device_intr_close(usbd_pipe_handle pipe)
   2117 {
   2118 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2119 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2120 	int i, npoll;
   2121 	int s;
   2122 
   2123 	/* Unlink descriptors from controller data structures. */
   2124 	npoll = upipe->u.intr.npoll;
   2125 	s = splusb();
   2126 	for (i = 0; i < npoll; i++)
   2127 		uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
   2128 	splx(s);
   2129 
   2130 	/*
   2131 	 * We now have to wait for any activity on the physical
   2132 	 * descriptors to stop.
   2133 	 */
   2134 	usb_delay_ms(&sc->sc_bus, 2);
   2135 
   2136 	for(i = 0; i < npoll; i++)
   2137 		uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
   2138 	free(upipe->u.intr.qhs, M_USBHC);
   2139 
   2140 	/* XXX free other resources */
   2141 }
   2142 
   2143 usbd_status
   2144 uhci_device_request(usbd_xfer_handle xfer)
   2145 {
   2146 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2147 	usb_device_request_t *req = &xfer->request;
   2148 	usbd_device_handle dev = upipe->pipe.device;
   2149 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2150 	int addr = dev->address;
   2151 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   2152 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2153 	uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
   2154 	uhci_soft_qh_t *sqh;
   2155 	int len;
   2156 	u_int32_t ls;
   2157 	usbd_status err;
   2158 	int isread;
   2159 	int s;
   2160 
   2161 	DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
   2162 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
   2163 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   2164 		    UGETW(req->wIndex), UGETW(req->wLength),
   2165 		    addr, endpt));
   2166 
   2167 	ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
   2168 	isread = req->bmRequestType & UT_READ;
   2169 	len = UGETW(req->wLength);
   2170 
   2171 	setup = upipe->u.ctl.setup;
   2172 	stat = upipe->u.ctl.stat;
   2173 	sqh = upipe->u.ctl.sqh;
   2174 
   2175 	/* Set up data transaction */
   2176 	if (len != 0) {
   2177 		upipe->nexttoggle = 1;
   2178 		err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
   2179 					   &xfer->dmabuf, &data, &dataend);
   2180 		if (err)
   2181 			return (err);
   2182 		next = data;
   2183 		dataend->link.std = stat;
   2184 		dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
   2185 	} else {
   2186 		next = stat;
   2187 	}
   2188 	upipe->u.ctl.length = len;
   2189 
   2190 	memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
   2191 
   2192 	setup->link.std = next;
   2193 	setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
   2194 	setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
   2195 		UHCI_TD_ACTIVE);
   2196 	setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
   2197 	setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma));
   2198 
   2199 	stat->link.std = NULL;
   2200 	stat->td.td_link = htole32(UHCI_PTR_T);
   2201 	stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
   2202 		UHCI_TD_ACTIVE | UHCI_TD_IOC);
   2203 	stat->td.td_token =
   2204 		htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
   2205 		                 UHCI_TD_IN (0, endpt, addr, 1));
   2206 	stat->td.td_buffer = htole32(0);
   2207 
   2208 #ifdef UHCI_DEBUG
   2209 	if (uhcidebug > 10) {
   2210 		DPRINTF(("uhci_device_request: before transfer\n"));
   2211 		uhci_dump_tds(setup);
   2212 	}
   2213 #endif
   2214 
   2215 	/* Set up interrupt info. */
   2216 	ii->xfer = xfer;
   2217 	ii->stdstart = setup;
   2218 	ii->stdend = stat;
   2219 #ifdef DIAGNOSTIC
   2220 	if (!ii->isdone) {
   2221 		printf("uhci_device_request: not done, ii=%p\n", ii);
   2222 	}
   2223 	ii->isdone = 0;
   2224 #endif
   2225 
   2226 	sqh->elink = setup;
   2227 	sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
   2228 
   2229 	s = splusb();
   2230 	if (dev->speed == USB_SPEED_LOW)
   2231 		uhci_add_ls_ctrl(sc, sqh);
   2232 	else
   2233 		uhci_add_hs_ctrl(sc, sqh);
   2234 	uhci_add_intr_info(sc, ii);
   2235 #ifdef UHCI_DEBUG
   2236 	if (uhcidebug > 12) {
   2237 		uhci_soft_td_t *std;
   2238 		uhci_soft_qh_t *xqh;
   2239 		uhci_soft_qh_t *sxqh;
   2240 		int maxqh = 0;
   2241 		uhci_physaddr_t link;
   2242 		DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
   2243 		for (std = sc->sc_vframes[0].htd, link = 0;
   2244 		     (link & UHCI_PTR_QH) == 0;
   2245 		     std = std->link.std) {
   2246 			link = le32toh(std->td.td_link);
   2247 			uhci_dump_td(std);
   2248 		}
   2249 		sxqh = (uhci_soft_qh_t *)std;
   2250 		uhci_dump_qh(sxqh);
   2251 		for (xqh = sxqh;
   2252 		     xqh != NULL;
   2253 		     xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
   2254                             xqh->hlink == xqh ? NULL : xqh->hlink)) {
   2255 			uhci_dump_qh(xqh);
   2256 		}
   2257 		DPRINTF(("Enqueued QH:\n"));
   2258 		uhci_dump_qh(sqh);
   2259 		uhci_dump_tds(sqh->elink);
   2260 	}
   2261 #endif
   2262 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   2263 		usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
   2264 			    uhci_timeout, ii);
   2265 	}
   2266 	xfer->status = USBD_IN_PROGRESS;
   2267 	splx(s);
   2268 
   2269 	return (USBD_NORMAL_COMPLETION);
   2270 }
   2271 
   2272 usbd_status
   2273 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
   2274 {
   2275 	usbd_status err;
   2276 
   2277 	DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
   2278 
   2279 	/* Put it on our queue, */
   2280 	err = usb_insert_transfer(xfer);
   2281 
   2282 	/* bail out on error, */
   2283 	if (err && err != USBD_IN_PROGRESS)
   2284 		return (err);
   2285 
   2286 	/* XXX should check inuse here */
   2287 
   2288 	/* insert into schedule, */
   2289 	uhci_device_isoc_enter(xfer);
   2290 
   2291 	/* and start if the pipe wasn't running */
   2292 	if (!err)
   2293 		uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
   2294 
   2295 	return (err);
   2296 }
   2297 
   2298 void
   2299 uhci_device_isoc_enter(usbd_xfer_handle xfer)
   2300 {
   2301 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2302 	usbd_device_handle dev = upipe->pipe.device;
   2303 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2304 	struct iso *iso = &upipe->u.iso;
   2305 	uhci_soft_td_t *std;
   2306 	u_int32_t buf, len, status;
   2307 	int s, i, next, nframes;
   2308 
   2309 	DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
   2310 		    "nframes=%d\n",
   2311 		    iso->inuse, iso->next, xfer, xfer->nframes));
   2312 
   2313 	if (sc->sc_dying)
   2314 		return;
   2315 
   2316 	if (xfer->status == USBD_IN_PROGRESS) {
   2317 		/* This request has already been entered into the frame list */
   2318 		printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
   2319 		/* XXX */
   2320 	}
   2321 
   2322 #ifdef DIAGNOSTIC
   2323 	if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
   2324 		printf("uhci_device_isoc_enter: overflow!\n");
   2325 #endif
   2326 
   2327 	next = iso->next;
   2328 	if (next == -1) {
   2329 		/* Not in use yet, schedule it a few frames ahead. */
   2330 		next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
   2331 		DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
   2332 	}
   2333 
   2334 	xfer->status = USBD_IN_PROGRESS;
   2335 	UXFER(xfer)->curframe = next;
   2336 
   2337 	buf = DMAADDR(&xfer->dmabuf);
   2338 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
   2339 				     UHCI_TD_ACTIVE |
   2340 				     UHCI_TD_IOS);
   2341 	nframes = xfer->nframes;
   2342 	s = splusb();
   2343 	for (i = 0; i < nframes; i++) {
   2344 		std = iso->stds[next];
   2345 		if (++next >= UHCI_VFRAMELIST_COUNT)
   2346 			next = 0;
   2347 		len = xfer->frlengths[i];
   2348 		std->td.td_buffer = htole32(buf);
   2349 		if (i == nframes - 1)
   2350 			status |= UHCI_TD_IOC;
   2351 		std->td.td_status = htole32(status);
   2352 		std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
   2353 		std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
   2354 #ifdef UHCI_DEBUG
   2355 		if (uhcidebug > 5) {
   2356 			DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
   2357 			uhci_dump_td(std);
   2358 		}
   2359 #endif
   2360 		buf += len;
   2361 	}
   2362 	iso->next = next;
   2363 	iso->inuse += xfer->nframes;
   2364 
   2365 	splx(s);
   2366 }
   2367 
   2368 usbd_status
   2369 uhci_device_isoc_start(usbd_xfer_handle xfer)
   2370 {
   2371 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2372 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
   2373 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2374 	uhci_soft_td_t *end;
   2375 	int s, i;
   2376 
   2377 	DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
   2378 
   2379 	if (sc->sc_dying)
   2380 		return (USBD_IOERROR);
   2381 
   2382 #ifdef DIAGNOSTIC
   2383 	if (xfer->status != USBD_IN_PROGRESS)
   2384 		printf("uhci_device_isoc_start: not in progress %p\n", xfer);
   2385 #endif
   2386 
   2387 	/* Find the last TD */
   2388 	i = UXFER(xfer)->curframe + xfer->nframes;
   2389 	if (i >= UHCI_VFRAMELIST_COUNT)
   2390 		i -= UHCI_VFRAMELIST_COUNT;
   2391 	end = upipe->u.iso.stds[i];
   2392 
   2393 #ifdef DIAGNOSTIC
   2394 	if (end == NULL) {
   2395 		printf("uhci_device_isoc_start: end == NULL\n");
   2396 		return (USBD_INVAL);
   2397 	}
   2398 #endif
   2399 
   2400 	s = splusb();
   2401 
   2402 	/* Set up interrupt info. */
   2403 	ii->xfer = xfer;
   2404 	ii->stdstart = end;
   2405 	ii->stdend = end;
   2406 #ifdef DIAGNOSTIC
   2407 	if (!ii->isdone)
   2408 		printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
   2409 	ii->isdone = 0;
   2410 #endif
   2411 	uhci_add_intr_info(sc, ii);
   2412 
   2413 	splx(s);
   2414 
   2415 	return (USBD_IN_PROGRESS);
   2416 }
   2417 
   2418 void
   2419 uhci_device_isoc_abort(usbd_xfer_handle xfer)
   2420 {
   2421 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2422 	uhci_soft_td_t **stds = upipe->u.iso.stds;
   2423 	uhci_soft_td_t *std;
   2424 	int i, n, s, nframes, maxlen, len;
   2425 
   2426 	s = splusb();
   2427 
   2428 	/* Transfer is already done. */
   2429 	if (xfer->status != USBD_NOT_STARTED &&
   2430 	    xfer->status != USBD_IN_PROGRESS) {
   2431 		splx(s);
   2432 		return;
   2433 	}
   2434 
   2435 	/* Give xfer the requested abort code. */
   2436 	xfer->status = USBD_CANCELLED;
   2437 
   2438 	/* make hardware ignore it, */
   2439 	nframes = xfer->nframes;
   2440 	n = UXFER(xfer)->curframe;
   2441 	maxlen = 0;
   2442 	for (i = 0; i < nframes; i++) {
   2443 		std = stds[n];
   2444 		std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
   2445 		len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
   2446 		if (len > maxlen)
   2447 			maxlen = len;
   2448 		if (++n >= UHCI_VFRAMELIST_COUNT)
   2449 			n = 0;
   2450 	}
   2451 
   2452 	/* and wait until we are sure the hardware has finished. */
   2453 	delay(maxlen);
   2454 
   2455 #ifdef DIAGNOSTIC
   2456 	UXFER(xfer)->iinfo.isdone = 1;
   2457 #endif
   2458 	/* Run callback and remove from interrupt list. */
   2459 	usb_transfer_complete(xfer);
   2460 
   2461 	splx(s);
   2462 }
   2463 
   2464 void
   2465 uhci_device_isoc_close(usbd_pipe_handle pipe)
   2466 {
   2467 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2468 	usbd_device_handle dev = upipe->pipe.device;
   2469 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2470 	uhci_soft_td_t *std, *vstd;
   2471 	struct iso *iso;
   2472 	int i, s;
   2473 
   2474 	/*
   2475 	 * Make sure all TDs are marked as inactive.
   2476 	 * Wait for completion.
   2477 	 * Unschedule.
   2478 	 * Deallocate.
   2479 	 */
   2480 	iso = &upipe->u.iso;
   2481 
   2482 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
   2483 		iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
   2484 	usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
   2485 
   2486 	s = splusb();
   2487 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2488 		std = iso->stds[i];
   2489 		for (vstd = sc->sc_vframes[i].htd;
   2490 		     vstd != NULL && vstd->link.std != std;
   2491 		     vstd = vstd->link.std)
   2492 			;
   2493 		if (vstd == NULL) {
   2494 			/*panic*/
   2495 			printf("uhci_device_isoc_close: %p not found\n", std);
   2496 			splx(s);
   2497 			return;
   2498 		}
   2499 		vstd->link = std->link;
   2500 		vstd->td.td_link = std->td.td_link;
   2501 		uhci_free_std(sc, std);
   2502 	}
   2503 	splx(s);
   2504 
   2505 	free(iso->stds, M_USBHC);
   2506 }
   2507 
   2508 usbd_status
   2509 uhci_setup_isoc(usbd_pipe_handle pipe)
   2510 {
   2511 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2512 	usbd_device_handle dev = upipe->pipe.device;
   2513 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2514 	int addr = upipe->pipe.device->address;
   2515 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   2516 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
   2517 	uhci_soft_td_t *std, *vstd;
   2518 	u_int32_t token;
   2519 	struct iso *iso;
   2520 	int i, s;
   2521 
   2522 	iso = &upipe->u.iso;
   2523 	iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
   2524 			   M_USBHC, M_WAITOK);
   2525 
   2526 	token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
   2527 		     UHCI_TD_OUT(0, endpt, addr, 0);
   2528 
   2529 	/* Allocate the TDs and mark as inactive; */
   2530 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2531 		std = uhci_alloc_std(sc);
   2532 		if (std == 0)
   2533 			goto bad;
   2534 		std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
   2535 		std->td.td_token = htole32(token);
   2536 		iso->stds[i] = std;
   2537 	}
   2538 
   2539 	/* Insert TDs into schedule. */
   2540 	s = splusb();
   2541 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2542 		std = iso->stds[i];
   2543 		vstd = sc->sc_vframes[i].htd;
   2544 		std->link = vstd->link;
   2545 		std->td.td_link = vstd->td.td_link;
   2546 		vstd->link.std = std;
   2547 		vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
   2548 	}
   2549 	splx(s);
   2550 
   2551 	iso->next = -1;
   2552 	iso->inuse = 0;
   2553 
   2554 	return (USBD_NORMAL_COMPLETION);
   2555 
   2556  bad:
   2557 	while (--i >= 0)
   2558 		uhci_free_std(sc, iso->stds[i]);
   2559 	free(iso->stds, M_USBHC);
   2560 	return (USBD_NOMEM);
   2561 }
   2562 
   2563 void
   2564 uhci_device_isoc_done(usbd_xfer_handle xfer)
   2565 {
   2566 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2567 
   2568 	DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
   2569 
   2570 	if (ii->xfer != xfer)
   2571 		/* Not on interrupt list, ignore it. */
   2572 		return;
   2573 
   2574 #ifdef DIAGNOSTIC
   2575 	if (xfer->busy_free != XFER_BUSY) {
   2576 		printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
   2577 		       xfer, xfer->busy_free);
   2578 		return;
   2579 	}
   2580 
   2581         if (ii->stdend == NULL) {
   2582                 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
   2583 #ifdef UHCI_DEBUG
   2584 		uhci_dump_ii(ii);
   2585 #endif
   2586 		return;
   2587 	}
   2588 #endif
   2589 
   2590 	/* Turn off the interrupt since it is active even if the TD is not. */
   2591 	ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
   2592 
   2593 	uhci_del_intr_info(ii);	/* remove from active list */
   2594 }
   2595 
   2596 void
   2597 uhci_device_intr_done(usbd_xfer_handle xfer)
   2598 {
   2599 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2600 	uhci_softc_t *sc = ii->sc;
   2601 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2602 	uhci_soft_qh_t *sqh;
   2603 	int i, npoll;
   2604 
   2605 	DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
   2606 
   2607 	npoll = upipe->u.intr.npoll;
   2608 	for(i = 0; i < npoll; i++) {
   2609 		sqh = upipe->u.intr.qhs[i];
   2610 		sqh->elink = NULL;
   2611 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   2612 	}
   2613 	uhci_free_std_chain(sc, ii->stdstart, NULL);
   2614 
   2615 	/* XXX Wasteful. */
   2616 	if (xfer->pipe->repeat) {
   2617 		uhci_soft_td_t *data, *dataend;
   2618 
   2619 		DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
   2620 
   2621 		/* This alloc cannot fail since we freed the chain above. */
   2622 		uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
   2623 				     &xfer->dmabuf, &data, &dataend);
   2624 		dataend->td.td_status |= htole32(UHCI_TD_IOC);
   2625 
   2626 #ifdef UHCI_DEBUG
   2627 		if (uhcidebug > 10) {
   2628 			DPRINTF(("uhci_device_intr_done: data(1)\n"));
   2629 			uhci_dump_tds(data);
   2630 			uhci_dump_qh(upipe->u.intr.qhs[0]);
   2631 		}
   2632 #endif
   2633 
   2634 		ii->stdstart = data;
   2635 		ii->stdend = dataend;
   2636 #ifdef DIAGNOSTIC
   2637 		if (!ii->isdone) {
   2638 			printf("uhci_device_intr_done: not done, ii=%p\n", ii);
   2639 		}
   2640 		ii->isdone = 0;
   2641 #endif
   2642 		for (i = 0; i < npoll; i++) {
   2643 			sqh = upipe->u.intr.qhs[i];
   2644 			sqh->elink = data;
   2645 			sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
   2646 		}
   2647 		xfer->status = USBD_IN_PROGRESS;
   2648 		/* The ii is already on the examined list, just leave it. */
   2649 	} else {
   2650 		DPRINTFN(5,("uhci_device_intr_done: removing\n"));
   2651 		uhci_del_intr_info(ii);
   2652 	}
   2653 }
   2654 
   2655 /* Deallocate request data structures */
   2656 void
   2657 uhci_device_ctrl_done(usbd_xfer_handle xfer)
   2658 {
   2659 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2660 	uhci_softc_t *sc = ii->sc;
   2661 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2662 
   2663 #ifdef DIAGNOSTIC
   2664 	if (!(xfer->rqflags & URQ_REQUEST))
   2665 		panic("uhci_ctrl_done: not a request\n");
   2666 #endif
   2667 
   2668 	uhci_del_intr_info(ii);	/* remove from active list */
   2669 
   2670 	if (upipe->pipe.device->speed == USB_SPEED_LOW)
   2671 		uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
   2672 	else
   2673 		uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
   2674 
   2675 	if (upipe->u.ctl.length != 0)
   2676 		uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
   2677 
   2678 	DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
   2679 }
   2680 
   2681 /* Deallocate request data structures */
   2682 void
   2683 uhci_device_bulk_done(usbd_xfer_handle xfer)
   2684 {
   2685 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2686 	uhci_softc_t *sc = ii->sc;
   2687 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2688 
   2689 	uhci_del_intr_info(ii);	/* remove from active list */
   2690 
   2691 	uhci_remove_bulk(sc, upipe->u.bulk.sqh);
   2692 
   2693 	uhci_free_std_chain(sc, ii->stdstart, NULL);
   2694 
   2695 	DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
   2696 }
   2697 
   2698 /* Add interrupt QH, called with vflock. */
   2699 void
   2700 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   2701 {
   2702 	struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
   2703 	uhci_soft_qh_t *eqh;
   2704 
   2705 	DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
   2706 
   2707 	eqh = vf->eqh;
   2708 	sqh->hlink       = eqh->hlink;
   2709 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
   2710 	eqh->hlink       = sqh;
   2711 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
   2712 	vf->eqh = sqh;
   2713 	vf->bandwidth++;
   2714 }
   2715 
   2716 /* Remove interrupt QH. */
   2717 void
   2718 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   2719 {
   2720 	struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
   2721 	uhci_soft_qh_t *pqh;
   2722 
   2723 	DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
   2724 
   2725 	/* See comment in uhci_remove_ctrl() */
   2726 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
   2727 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   2728 		delay(UHCI_QH_REMOVE_DELAY);
   2729 	}
   2730 
   2731 	pqh = uhci_find_prev_qh(vf->hqh, sqh);
   2732 	pqh->hlink       = sqh->hlink;
   2733 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
   2734 	delay(UHCI_QH_REMOVE_DELAY);
   2735 	if (vf->eqh == sqh)
   2736 		vf->eqh = pqh;
   2737 	vf->bandwidth--;
   2738 }
   2739 
   2740 usbd_status
   2741 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
   2742 {
   2743 	uhci_soft_qh_t *sqh;
   2744 	int i, npoll, s;
   2745 	u_int bestbw, bw, bestoffs, offs;
   2746 
   2747 	DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
   2748 	if (ival == 0) {
   2749 		printf("uhci_setintr: 0 interval\n");
   2750 		return (USBD_INVAL);
   2751 	}
   2752 
   2753 	if (ival > UHCI_VFRAMELIST_COUNT)
   2754 		ival = UHCI_VFRAMELIST_COUNT;
   2755 	npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
   2756 	DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
   2757 
   2758 	upipe->u.intr.npoll = npoll;
   2759 	upipe->u.intr.qhs =
   2760 		malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
   2761 
   2762 	/*
   2763 	 * Figure out which offset in the schedule that has most
   2764 	 * bandwidth left over.
   2765 	 */
   2766 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
   2767 	for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
   2768 		for (bw = i = 0; i < npoll; i++)
   2769 			bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
   2770 		if (bw < bestbw) {
   2771 			bestbw = bw;
   2772 			bestoffs = offs;
   2773 		}
   2774 	}
   2775 	DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
   2776 
   2777 	for(i = 0; i < npoll; i++) {
   2778 		upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
   2779 		sqh->elink = NULL;
   2780 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   2781 		sqh->pos = MOD(i * ival + bestoffs);
   2782 	}
   2783 #undef MOD
   2784 
   2785 	s = splusb();
   2786 	/* Enter QHs into the controller data structures. */
   2787 	for(i = 0; i < npoll; i++)
   2788 		uhci_add_intr(sc, upipe->u.intr.qhs[i]);
   2789 	splx(s);
   2790 
   2791 	DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
   2792 	return (USBD_NORMAL_COMPLETION);
   2793 }
   2794 
   2795 /* Open a new pipe. */
   2796 usbd_status
   2797 uhci_open(usbd_pipe_handle pipe)
   2798 {
   2799 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2800 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2801 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
   2802 	usbd_status err;
   2803 	int ival;
   2804 
   2805 	DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
   2806 		     pipe, pipe->device->address,
   2807 		     ed->bEndpointAddress, sc->sc_addr));
   2808 
   2809 	upipe->aborting = 0;
   2810 	upipe->nexttoggle = 0;
   2811 
   2812 	if (pipe->device->address == sc->sc_addr) {
   2813 		switch (ed->bEndpointAddress) {
   2814 		case USB_CONTROL_ENDPOINT:
   2815 			pipe->methods = &uhci_root_ctrl_methods;
   2816 			break;
   2817 		case UE_DIR_IN | UHCI_INTR_ENDPT:
   2818 			pipe->methods = &uhci_root_intr_methods;
   2819 			break;
   2820 		default:
   2821 			return (USBD_INVAL);
   2822 		}
   2823 	} else {
   2824 		switch (ed->bmAttributes & UE_XFERTYPE) {
   2825 		case UE_CONTROL:
   2826 			pipe->methods = &uhci_device_ctrl_methods;
   2827 			upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
   2828 			if (upipe->u.ctl.sqh == NULL)
   2829 				goto bad;
   2830 			upipe->u.ctl.setup = uhci_alloc_std(sc);
   2831 			if (upipe->u.ctl.setup == NULL) {
   2832 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2833 				goto bad;
   2834 			}
   2835 			upipe->u.ctl.stat = uhci_alloc_std(sc);
   2836 			if (upipe->u.ctl.stat == NULL) {
   2837 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2838 				uhci_free_std(sc, upipe->u.ctl.setup);
   2839 				goto bad;
   2840 			}
   2841 			err = usb_allocmem(&sc->sc_bus,
   2842 				  sizeof(usb_device_request_t),
   2843 				  0, &upipe->u.ctl.reqdma);
   2844 			if (err) {
   2845 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2846 				uhci_free_std(sc, upipe->u.ctl.setup);
   2847 				uhci_free_std(sc, upipe->u.ctl.stat);
   2848 				goto bad;
   2849 			}
   2850 			break;
   2851 		case UE_INTERRUPT:
   2852 			pipe->methods = &uhci_device_intr_methods;
   2853 			ival = pipe->interval;
   2854 			if (ival == USBD_DEFAULT_INTERVAL)
   2855 				ival = ed->bInterval;
   2856 			return (uhci_device_setintr(sc, upipe, ival));
   2857 		case UE_ISOCHRONOUS:
   2858 			pipe->methods = &uhci_device_isoc_methods;
   2859 			return (uhci_setup_isoc(pipe));
   2860 		case UE_BULK:
   2861 			pipe->methods = &uhci_device_bulk_methods;
   2862 			upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
   2863 			if (upipe->u.bulk.sqh == NULL)
   2864 				goto bad;
   2865 			break;
   2866 		}
   2867 	}
   2868 	return (USBD_NORMAL_COMPLETION);
   2869 
   2870  bad:
   2871 	return (USBD_NOMEM);
   2872 }
   2873 
   2874 /*
   2875  * Data structures and routines to emulate the root hub.
   2876  */
   2877 usb_device_descriptor_t uhci_devd = {
   2878 	USB_DEVICE_DESCRIPTOR_SIZE,
   2879 	UDESC_DEVICE,		/* type */
   2880 	{0x00, 0x01},		/* USB version */
   2881 	UDCLASS_HUB,		/* class */
   2882 	UDSUBCLASS_HUB,		/* subclass */
   2883 	UDPROTO_FSHUB,		/* protocol */
   2884 	64,			/* max packet */
   2885 	{0},{0},{0x00,0x01},	/* device id */
   2886 	1,2,0,			/* string indicies */
   2887 	1			/* # of configurations */
   2888 };
   2889 
   2890 usb_config_descriptor_t uhci_confd = {
   2891 	USB_CONFIG_DESCRIPTOR_SIZE,
   2892 	UDESC_CONFIG,
   2893 	{USB_CONFIG_DESCRIPTOR_SIZE +
   2894 	 USB_INTERFACE_DESCRIPTOR_SIZE +
   2895 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
   2896 	1,
   2897 	1,
   2898 	0,
   2899 	UC_SELF_POWERED,
   2900 	0			/* max power */
   2901 };
   2902 
   2903 usb_interface_descriptor_t uhci_ifcd = {
   2904 	USB_INTERFACE_DESCRIPTOR_SIZE,
   2905 	UDESC_INTERFACE,
   2906 	0,
   2907 	0,
   2908 	1,
   2909 	UICLASS_HUB,
   2910 	UISUBCLASS_HUB,
   2911 	UIPROTO_FSHUB,
   2912 	0
   2913 };
   2914 
   2915 usb_endpoint_descriptor_t uhci_endpd = {
   2916 	USB_ENDPOINT_DESCRIPTOR_SIZE,
   2917 	UDESC_ENDPOINT,
   2918 	UE_DIR_IN | UHCI_INTR_ENDPT,
   2919 	UE_INTERRUPT,
   2920 	{8},
   2921 	255
   2922 };
   2923 
   2924 usb_hub_descriptor_t uhci_hubd_piix = {
   2925 	USB_HUB_DESCRIPTOR_SIZE,
   2926 	UDESC_HUB,
   2927 	2,
   2928 	{ UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
   2929 	50,			/* power on to power good */
   2930 	0,
   2931 	{ 0x00 },		/* both ports are removable */
   2932 };
   2933 
   2934 int
   2935 uhci_str(usb_string_descriptor_t *p, int l, char *s)
   2936 {
   2937 	int i;
   2938 
   2939 	if (l == 0)
   2940 		return (0);
   2941 	p->bLength = 2 * strlen(s) + 2;
   2942 	if (l == 1)
   2943 		return (1);
   2944 	p->bDescriptorType = UDESC_STRING;
   2945 	l -= 2;
   2946 	for (i = 0; s[i] && l > 1; i++, l -= 2)
   2947 		USETW2(p->bString[i], 0, s[i]);
   2948 	return (2*i+2);
   2949 }
   2950 
   2951 /*
   2952  * Simulate a hardware hub by handling all the necessary requests.
   2953  */
   2954 usbd_status
   2955 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
   2956 {
   2957 	usbd_status err;
   2958 
   2959 	/* Insert last in queue. */
   2960 	err = usb_insert_transfer(xfer);
   2961 	if (err)
   2962 		return (err);
   2963 
   2964 	/*
   2965 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   2966 	 * so start it first.
   2967 	 */
   2968 	return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2969 }
   2970 
   2971 usbd_status
   2972 uhci_root_ctrl_start(usbd_xfer_handle xfer)
   2973 {
   2974 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   2975 	usb_device_request_t *req;
   2976 	void *buf = NULL;
   2977 	int port, x;
   2978 	int s, len, value, index, status, change, l, totlen = 0;
   2979 	usb_port_status_t ps;
   2980 	usbd_status err;
   2981 
   2982 	if (sc->sc_dying)
   2983 		return (USBD_IOERROR);
   2984 
   2985 #ifdef DIAGNOSTIC
   2986 	if (!(xfer->rqflags & URQ_REQUEST))
   2987 		panic("uhci_root_ctrl_transfer: not a request\n");
   2988 #endif
   2989 	req = &xfer->request;
   2990 
   2991 	DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
   2992 		    req->bmRequestType, req->bRequest));
   2993 
   2994 	len = UGETW(req->wLength);
   2995 	value = UGETW(req->wValue);
   2996 	index = UGETW(req->wIndex);
   2997 
   2998 	if (len != 0)
   2999 		buf = KERNADDR(&xfer->dmabuf);
   3000 
   3001 #define C(x,y) ((x) | ((y) << 8))
   3002 	switch(C(req->bRequest, req->bmRequestType)) {
   3003 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   3004 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   3005 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   3006 		/*
   3007 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
   3008 		 * for the integrated root hub.
   3009 		 */
   3010 		break;
   3011 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   3012 		if (len > 0) {
   3013 			*(u_int8_t *)buf = sc->sc_conf;
   3014 			totlen = 1;
   3015 		}
   3016 		break;
   3017 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   3018 		DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
   3019 		switch(value >> 8) {
   3020 		case UDESC_DEVICE:
   3021 			if ((value & 0xff) != 0) {
   3022 				err = USBD_IOERROR;
   3023 				goto ret;
   3024 			}
   3025 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   3026 			USETW(uhci_devd.idVendor, sc->sc_id_vendor);
   3027 			memcpy(buf, &uhci_devd, l);
   3028 			break;
   3029 		case UDESC_CONFIG:
   3030 			if ((value & 0xff) != 0) {
   3031 				err = USBD_IOERROR;
   3032 				goto ret;
   3033 			}
   3034 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   3035 			memcpy(buf, &uhci_confd, l);
   3036 			buf = (char *)buf + l;
   3037 			len -= l;
   3038 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   3039 			totlen += l;
   3040 			memcpy(buf, &uhci_ifcd, l);
   3041 			buf = (char *)buf + l;
   3042 			len -= l;
   3043 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   3044 			totlen += l;
   3045 			memcpy(buf, &uhci_endpd, l);
   3046 			break;
   3047 		case UDESC_STRING:
   3048 			if (len == 0)
   3049 				break;
   3050 			*(u_int8_t *)buf = 0;
   3051 			totlen = 1;
   3052 			switch (value & 0xff) {
   3053 			case 1: /* Vendor */
   3054 				totlen = uhci_str(buf, len, sc->sc_vendor);
   3055 				break;
   3056 			case 2: /* Product */
   3057 				totlen = uhci_str(buf, len, "UHCI root hub");
   3058 				break;
   3059 			}
   3060 			break;
   3061 		default:
   3062 			err = USBD_IOERROR;
   3063 			goto ret;
   3064 		}
   3065 		break;
   3066 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   3067 		if (len > 0) {
   3068 			*(u_int8_t *)buf = 0;
   3069 			totlen = 1;
   3070 		}
   3071 		break;
   3072 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   3073 		if (len > 1) {
   3074 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   3075 			totlen = 2;
   3076 		}
   3077 		break;
   3078 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   3079 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   3080 		if (len > 1) {
   3081 			USETW(((usb_status_t *)buf)->wStatus, 0);
   3082 			totlen = 2;
   3083 		}
   3084 		break;
   3085 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   3086 		if (value >= USB_MAX_DEVICES) {
   3087 			err = USBD_IOERROR;
   3088 			goto ret;
   3089 		}
   3090 		sc->sc_addr = value;
   3091 		break;
   3092 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   3093 		if (value != 0 && value != 1) {
   3094 			err = USBD_IOERROR;
   3095 			goto ret;
   3096 		}
   3097 		sc->sc_conf = value;
   3098 		break;
   3099 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   3100 		break;
   3101 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   3102 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   3103 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   3104 		err = USBD_IOERROR;
   3105 		goto ret;
   3106 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   3107 		break;
   3108 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   3109 		break;
   3110 	/* Hub requests */
   3111 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   3112 		break;
   3113 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   3114 		DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
   3115 			     "port=%d feature=%d\n",
   3116 			     index, value));
   3117 		if (index == 1)
   3118 			port = UHCI_PORTSC1;
   3119 		else if (index == 2)
   3120 			port = UHCI_PORTSC2;
   3121 		else {
   3122 			err = USBD_IOERROR;
   3123 			goto ret;
   3124 		}
   3125 		switch(value) {
   3126 		case UHF_PORT_ENABLE:
   3127 			x = URWMASK(UREAD2(sc, port));
   3128 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
   3129 			break;
   3130 		case UHF_PORT_SUSPEND:
   3131 			x = URWMASK(UREAD2(sc, port));
   3132 			UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
   3133 			break;
   3134 		case UHF_PORT_RESET:
   3135 			x = URWMASK(UREAD2(sc, port));
   3136 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   3137 			break;
   3138 		case UHF_C_PORT_CONNECTION:
   3139 			x = URWMASK(UREAD2(sc, port));
   3140 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
   3141 			break;
   3142 		case UHF_C_PORT_ENABLE:
   3143 			x = URWMASK(UREAD2(sc, port));
   3144 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
   3145 			break;
   3146 		case UHF_C_PORT_OVER_CURRENT:
   3147 			x = URWMASK(UREAD2(sc, port));
   3148 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
   3149 			break;
   3150 		case UHF_C_PORT_RESET:
   3151 			sc->sc_isreset = 0;
   3152 			err = USBD_NORMAL_COMPLETION;
   3153 			goto ret;
   3154 		case UHF_PORT_CONNECTION:
   3155 		case UHF_PORT_OVER_CURRENT:
   3156 		case UHF_PORT_POWER:
   3157 		case UHF_PORT_LOW_SPEED:
   3158 		case UHF_C_PORT_SUSPEND:
   3159 		default:
   3160 			err = USBD_IOERROR;
   3161 			goto ret;
   3162 		}
   3163 		break;
   3164 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
   3165 		if (index == 1)
   3166 			port = UHCI_PORTSC1;
   3167 		else if (index == 2)
   3168 			port = UHCI_PORTSC2;
   3169 		else {
   3170 			err = USBD_IOERROR;
   3171 			goto ret;
   3172 		}
   3173 		if (len > 0) {
   3174 			*(u_int8_t *)buf =
   3175 				(UREAD2(sc, port) & UHCI_PORTSC_LS) >>
   3176 				UHCI_PORTSC_LS_SHIFT;
   3177 			totlen = 1;
   3178 		}
   3179 		break;
   3180 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   3181 		if (value != 0) {
   3182 			err = USBD_IOERROR;
   3183 			goto ret;
   3184 		}
   3185 		l = min(len, USB_HUB_DESCRIPTOR_SIZE);
   3186 		totlen = l;
   3187 		memcpy(buf, &uhci_hubd_piix, l);
   3188 		break;
   3189 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   3190 		if (len != 4) {
   3191 			err = USBD_IOERROR;
   3192 			goto ret;
   3193 		}
   3194 		memset(buf, 0, len);
   3195 		totlen = len;
   3196 		break;
   3197 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   3198 		if (index == 1)
   3199 			port = UHCI_PORTSC1;
   3200 		else if (index == 2)
   3201 			port = UHCI_PORTSC2;
   3202 		else {
   3203 			err = USBD_IOERROR;
   3204 			goto ret;
   3205 		}
   3206 		if (len != 4) {
   3207 			err = USBD_IOERROR;
   3208 			goto ret;
   3209 		}
   3210 		x = UREAD2(sc, port);
   3211 		status = change = 0;
   3212 		if (x & UHCI_PORTSC_CCS)
   3213 			status |= UPS_CURRENT_CONNECT_STATUS;
   3214 		if (x & UHCI_PORTSC_CSC)
   3215 			change |= UPS_C_CONNECT_STATUS;
   3216 		if (x & UHCI_PORTSC_PE)
   3217 			status |= UPS_PORT_ENABLED;
   3218 		if (x & UHCI_PORTSC_POEDC)
   3219 			change |= UPS_C_PORT_ENABLED;
   3220 		if (x & UHCI_PORTSC_OCI)
   3221 			status |= UPS_OVERCURRENT_INDICATOR;
   3222 		if (x & UHCI_PORTSC_OCIC)
   3223 			change |= UPS_C_OVERCURRENT_INDICATOR;
   3224 		if (x & UHCI_PORTSC_SUSP)
   3225 			status |= UPS_SUSPEND;
   3226 		if (x & UHCI_PORTSC_LSDA)
   3227 			status |= UPS_LOW_SPEED;
   3228 		status |= UPS_PORT_POWER;
   3229 		if (sc->sc_isreset)
   3230 			change |= UPS_C_PORT_RESET;
   3231 		USETW(ps.wPortStatus, status);
   3232 		USETW(ps.wPortChange, change);
   3233 		l = min(len, sizeof ps);
   3234 		memcpy(buf, &ps, l);
   3235 		totlen = l;
   3236 		break;
   3237 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   3238 		err = USBD_IOERROR;
   3239 		goto ret;
   3240 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   3241 		break;
   3242 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   3243 		if (index == 1)
   3244 			port = UHCI_PORTSC1;
   3245 		else if (index == 2)
   3246 			port = UHCI_PORTSC2;
   3247 		else {
   3248 			err = USBD_IOERROR;
   3249 			goto ret;
   3250 		}
   3251 		switch(value) {
   3252 		case UHF_PORT_ENABLE:
   3253 			x = URWMASK(UREAD2(sc, port));
   3254 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
   3255 			break;
   3256 		case UHF_PORT_SUSPEND:
   3257 			x = URWMASK(UREAD2(sc, port));
   3258 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
   3259 			break;
   3260 		case UHF_PORT_RESET:
   3261 			x = URWMASK(UREAD2(sc, port));
   3262 			UWRITE2(sc, port, x | UHCI_PORTSC_PR);
   3263 			usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
   3264 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   3265 			delay(100);
   3266 			x = UREAD2(sc, port);
   3267 			UWRITE2(sc, port, x  | UHCI_PORTSC_PE);
   3268 			usb_delay_ms(&sc->sc_bus, 10); /* XXX */
   3269 			DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
   3270 				    index, UREAD2(sc, port)));
   3271 			sc->sc_isreset = 1;
   3272 			break;
   3273 		case UHF_PORT_POWER:
   3274 			/* Pretend we turned on power */
   3275 			err = USBD_NORMAL_COMPLETION;
   3276 			goto ret;
   3277 		case UHF_C_PORT_CONNECTION:
   3278 		case UHF_C_PORT_ENABLE:
   3279 		case UHF_C_PORT_OVER_CURRENT:
   3280 		case UHF_PORT_CONNECTION:
   3281 		case UHF_PORT_OVER_CURRENT:
   3282 		case UHF_PORT_LOW_SPEED:
   3283 		case UHF_C_PORT_SUSPEND:
   3284 		case UHF_C_PORT_RESET:
   3285 		default:
   3286 			err = USBD_IOERROR;
   3287 			goto ret;
   3288 		}
   3289 		break;
   3290 	default:
   3291 		err = USBD_IOERROR;
   3292 		goto ret;
   3293 	}
   3294 	xfer->actlen = totlen;
   3295 	err = USBD_NORMAL_COMPLETION;
   3296  ret:
   3297 	xfer->status = err;
   3298 	s = splusb();
   3299 	usb_transfer_complete(xfer);
   3300 	splx(s);
   3301 	return (USBD_IN_PROGRESS);
   3302 }
   3303 
   3304 /* Abort a root control request. */
   3305 void
   3306 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
   3307 {
   3308 	/* Nothing to do, all transfers are synchronous. */
   3309 }
   3310 
   3311 /* Close the root pipe. */
   3312 void
   3313 uhci_root_ctrl_close(usbd_pipe_handle pipe)
   3314 {
   3315 	DPRINTF(("uhci_root_ctrl_close\n"));
   3316 }
   3317 
   3318 /* Abort a root interrupt request. */
   3319 void
   3320 uhci_root_intr_abort(usbd_xfer_handle xfer)
   3321 {
   3322 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   3323 
   3324 	usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
   3325 	sc->sc_intr_xfer = NULL;
   3326 
   3327 	if (xfer->pipe->intrxfer == xfer) {
   3328 		DPRINTF(("uhci_root_intr_abort: remove\n"));
   3329 		xfer->pipe->intrxfer = 0;
   3330 	}
   3331 	xfer->status = USBD_CANCELLED;
   3332 #ifdef DIAGNOSTIC
   3333 	UXFER(xfer)->iinfo.isdone = 1;
   3334 #endif
   3335 	usb_transfer_complete(xfer);
   3336 }
   3337 
   3338 usbd_status
   3339 uhci_root_intr_transfer(usbd_xfer_handle xfer)
   3340 {
   3341 	usbd_status err;
   3342 
   3343 	/* Insert last in queue. */
   3344 	err = usb_insert_transfer(xfer);
   3345 	if (err)
   3346 		return (err);
   3347 
   3348 	/* Pipe isn't running (otherwise err would be USBD_INPROG),
   3349 	 * start first
   3350 	 */
   3351 	return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   3352 }
   3353 
   3354 /* Start a transfer on the root interrupt pipe */
   3355 usbd_status
   3356 uhci_root_intr_start(usbd_xfer_handle xfer)
   3357 {
   3358 	usbd_pipe_handle pipe = xfer->pipe;
   3359 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   3360 
   3361 	DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
   3362 		     xfer, xfer->length, xfer->flags));
   3363 
   3364 	if (sc->sc_dying)
   3365 		return (USBD_IOERROR);
   3366 
   3367 	sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
   3368 	usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
   3369 	sc->sc_intr_xfer = xfer;
   3370 	return (USBD_IN_PROGRESS);
   3371 }
   3372 
   3373 /* Close the root interrupt pipe. */
   3374 void
   3375 uhci_root_intr_close(usbd_pipe_handle pipe)
   3376 {
   3377 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   3378 
   3379 	usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
   3380 	sc->sc_intr_xfer = NULL;
   3381 	DPRINTF(("uhci_root_intr_close\n"));
   3382 }
   3383