Home | History | Annotate | Line # | Download | only in usb
ohci.c revision 1.3
      1 /*	$NetBSD: ohci.c,v 1.3 1998/07/23 13:41:04 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson <augustss (at) carlstedt.se>
      8  *         Carlstedt Research & Technology
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * USB Open Host Controller driver.
     41  *
     42  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
     43  * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/malloc.h>
     50 #include <sys/device.h>
     51 #include <sys/proc.h>
     52 #include <sys/queue.h>
     53 #include <sys/select.h>
     54 
     55 #include <dev/usb/usb.h>
     56 
     57 #include <dev/usb/usbdi.h>
     58 #include <dev/usb/usbdivar.h>
     59 
     60 #include <dev/usb/usb_quirks.h>
     61 
     62 #include <machine/bus.h>
     63 
     64 #include <dev/usb/ohcireg.h>
     65 #include <dev/usb/ohcivar.h>
     66 
     67 int ohcidebug = 0;
     68 
     69 struct ohci_pipe;
     70 
     71 ohci_soft_ed_t *ohci_alloc_sed __P((ohci_softc_t *));
     72 void		ohci_free_sed __P((ohci_softc_t *, ohci_soft_ed_t *));
     73 
     74 ohci_soft_td_t *ohci_alloc_std __P((ohci_softc_t *));
     75 void		ohci_free_std __P((ohci_softc_t *, ohci_soft_td_t *));
     76 
     77 usbd_status	ohci_open __P((usbd_pipe_handle));
     78 void		ohci_waitintr __P((ohci_softc_t *, usbd_request_handle));
     79 void		ohci_rhsc __P((ohci_softc_t *, usbd_request_handle));
     80 void		ohci_process_done __P((ohci_softc_t *, ohci_physaddr_t));
     81 void		ohci_ctrl_done __P((ohci_softc_t *, usbd_request_handle));
     82 void		ohci_intr_done __P((ohci_softc_t *, usbd_request_handle));
     83 void		ohci_bulk_done __P((ohci_softc_t *, usbd_request_handle));
     84 
     85 usbd_status	ohci_allocmem __P((ohci_softc_t *,size_t,size_t, ohci_dma_t*));
     86 void		ohci_freemem __P((ohci_softc_t *, ohci_dma_t *));
     87 
     88 usbd_status	ohci_device_request __P((usbd_request_handle reqh));
     89 void		ohci_add_ed __P((ohci_soft_ed_t *, ohci_soft_ed_t *));
     90 void		ohci_rem_ed __P((ohci_soft_ed_t *, ohci_soft_ed_t *));
     91 void		ohci_hash_add_td __P((ohci_softc_t *, ohci_soft_td_t *));
     92 void		ohci_hash_rem_td __P((ohci_softc_t *, ohci_soft_td_t *));
     93 ohci_soft_td_t *ohci_hash_find_td __P((ohci_softc_t *, ohci_physaddr_t));
     94 
     95 usbd_status	ohci_root_ctrl_transfer __P((usbd_request_handle));
     96 void		ohci_root_ctrl_abort __P((usbd_request_handle));
     97 void		ohci_root_ctrl_close __P((usbd_pipe_handle));
     98 
     99 usbd_status	ohci_root_intr_transfer __P((usbd_request_handle));
    100 void		ohci_root_intr_abort __P((usbd_request_handle));
    101 void		ohci_root_intr_close __P((usbd_pipe_handle));
    102 
    103 usbd_status	ohci_device_ctrl_transfer __P((usbd_request_handle));
    104 void		ohci_device_ctrl_abort __P((usbd_request_handle));
    105 void		ohci_device_ctrl_close __P((usbd_pipe_handle));
    106 
    107 usbd_status	ohci_device_bulk_transfer __P((usbd_request_handle));
    108 void		ohci_device_bulk_abort __P((usbd_request_handle));
    109 void		ohci_device_bulk_close __P((usbd_pipe_handle));
    110 
    111 usbd_status	ohci_device_intr_transfer __P((usbd_request_handle));
    112 void		ohci_device_intr_abort __P((usbd_request_handle));
    113 void		ohci_device_intr_close __P((usbd_pipe_handle));
    114 usbd_status	ohci_device_setintr __P((ohci_softc_t *sc,
    115 					 struct ohci_pipe *pipe, int ival));
    116 
    117 int		ohci_str __P((usb_string_descriptor_t *, int, char *));
    118 
    119 void		ohci_timeout __P((void *));
    120 void		ohci_rhsc_able __P((ohci_softc_t *, int));
    121 
    122 #ifdef USB_DEBUG
    123 ohci_softc_t   *thesc;
    124 void		ohci_dumpregs __P((ohci_softc_t *));
    125 void		ohci_dump_tds __P((ohci_soft_td_t *));
    126 void		ohci_dump_td __P((ohci_soft_td_t *));
    127 void		ohci_dump_ed __P((ohci_soft_ed_t *));
    128 #endif
    129 
    130 #define OWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
    131 #define OREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
    132 #define OREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
    133 
    134 /* Reverse the bits in a value 0 .. 31 */
    135 static u_int8_t revbits[OHCI_NO_INTRS] =
    136   { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
    137     0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
    138     0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
    139     0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
    140 
    141 struct ohci_pipe {
    142 	struct usbd_pipe pipe;
    143 	ohci_soft_ed_t *sed;
    144 	ohci_soft_td_t *tail;
    145 	/* Info needed for different pipe kinds. */
    146 	union {
    147 		/* Control pipe */
    148 		struct {
    149 			ohci_dma_t datadma;
    150 			ohci_dma_t reqdma;
    151 			u_int length;
    152 			ohci_soft_td_t *setup, *xfer, *stat;
    153 		} ctl;
    154 		/* Interrupt pipe */
    155 		struct {
    156 			ohci_dma_t datadma;
    157 			int nslots;
    158 			int pos;
    159 		} intr;
    160 		/* Bulk pipe */
    161 		struct {
    162 			ohci_dma_t datadma;
    163 			u_int length;
    164 		} bulk;
    165 	} u;
    166 };
    167 
    168 #define OHCI_INTR_ENDPT 1
    169 
    170 struct usbd_methods ohci_root_ctrl_methods = {
    171 	ohci_root_ctrl_transfer,
    172 	ohci_root_ctrl_abort,
    173 	ohci_root_ctrl_close,
    174 };
    175 
    176 struct usbd_methods ohci_root_intr_methods = {
    177 	ohci_root_intr_transfer,
    178 	ohci_root_intr_abort,
    179 	ohci_root_intr_close,
    180 };
    181 
    182 struct usbd_methods ohci_device_ctrl_methods = {
    183 	ohci_device_ctrl_transfer,
    184 	ohci_device_ctrl_abort,
    185 	ohci_device_ctrl_close,
    186 };
    187 
    188 struct usbd_methods ohci_device_intr_methods = {
    189 	ohci_device_intr_transfer,
    190 	ohci_device_intr_abort,
    191 	ohci_device_intr_close,
    192 };
    193 
    194 struct usbd_methods ohci_device_bulk_methods = {
    195 	ohci_device_bulk_transfer,
    196 	ohci_device_bulk_abort,
    197 	ohci_device_bulk_close,
    198 };
    199 
    200 usbd_status
    201 ohci_allocmem(sc, size, align, p)
    202 	ohci_softc_t *sc;
    203 	size_t size;
    204 	size_t align;
    205         ohci_dma_t *p;
    206 {
    207 	int error;
    208 
    209 	DPRINTFN(5, ("ohci_allocmem: size=%d align=%d\n", size, align));
    210 	p->size = size;
    211 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    212 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    213 				 &p->nsegs, BUS_DMA_NOWAIT);
    214 	if (error)
    215 		return (USBD_NOMEM);
    216 
    217 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    218 			       &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    219 	if (error)
    220 		goto free;
    221 
    222 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    223 				  0, BUS_DMA_NOWAIT, &p->map);
    224 	if (error)
    225 		goto unmap;
    226 
    227 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->kaddr,p->size, NULL,
    228 				BUS_DMA_NOWAIT);
    229 	if (error)
    230 		goto destroy;
    231 	return 0;
    232 
    233 destroy:
    234 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    235 unmap:
    236 	bus_dmamem_unmap(sc->sc_dmatag, p->kaddr, p->size);
    237 free:
    238 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    239 	return (USBD_NOMEM);
    240 }
    241 
    242 void
    243 ohci_freemem(sc, p)
    244 	ohci_softc_t *sc;
    245         ohci_dma_t *p;
    246 {
    247 	bus_dmamap_unload(sc->sc_dmatag, p->map);
    248 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    249 	bus_dmamem_unmap(sc->sc_dmatag, p->kaddr, p->size);
    250 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    251 }
    252 
    253 ohci_soft_ed_t *
    254 ohci_alloc_sed(sc)
    255 	ohci_softc_t *sc;
    256 {
    257 	ohci_soft_ed_t *sed;
    258 	usbd_status r;
    259 	int i, offs;
    260 	ohci_dma_t dma;
    261 
    262 	if (!sc->sc_freeeds) {
    263 		DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
    264 		sed = malloc(sizeof(ohci_soft_ed_t) * OHCI_ED_CHUNK,
    265 			     M_USBDEV, M_NOWAIT);
    266 		if (!sed)
    267 			return 0;
    268 		r = ohci_allocmem(sc, OHCI_ED_SIZE * OHCI_ED_CHUNK,
    269 				     OHCI_ED_ALIGN, &dma);
    270 		if (r != USBD_NORMAL_COMPLETION) {
    271 			free(sed, M_USBDEV);
    272 			return 0;
    273 		}
    274 		for(i = 0; i < OHCI_ED_CHUNK; i++, sed++) {
    275 			offs = i * OHCI_ED_SIZE;
    276 			sed->physaddr = DMAADDR(&dma) + offs;
    277 			sed->ed = (ohci_ed_t *)
    278 					((char *)KERNADDR(&dma) + offs);
    279 			sed->next = sc->sc_freeeds;
    280 			sc->sc_freeeds = sed;
    281 		}
    282 	}
    283 	sed = sc->sc_freeeds;
    284 	sc->sc_freeeds = sed->next;
    285 	memset(sed->ed, 0, OHCI_ED_SIZE);
    286 	sed->next = 0;
    287 	return sed;
    288 }
    289 
    290 void
    291 ohci_free_sed(sc, sed)
    292 	ohci_softc_t *sc;
    293 	ohci_soft_ed_t *sed;
    294 {
    295 	sed->next = sc->sc_freeeds;
    296 	sc->sc_freeeds = sed;
    297 }
    298 
    299 ohci_soft_td_t *
    300 ohci_alloc_std(sc)
    301 	ohci_softc_t *sc;
    302 {
    303 	ohci_soft_td_t *std;
    304 	usbd_status r;
    305 	int i, offs;
    306 	ohci_dma_t dma;
    307 
    308 	if (!sc->sc_freetds) {
    309 		DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
    310 		std = malloc(sizeof(ohci_soft_td_t) * OHCI_TD_CHUNK,
    311 			     M_USBDEV, M_NOWAIT);
    312 		if (!std)
    313 			return 0;
    314 		r = ohci_allocmem(sc, OHCI_TD_SIZE * OHCI_TD_CHUNK,
    315 				     OHCI_TD_ALIGN, &dma);
    316 		if (r != USBD_NORMAL_COMPLETION) {
    317 			free(std, M_USBDEV);
    318 			return 0;
    319 		}
    320 		for(i = 0; i < OHCI_TD_CHUNK; i++, std++) {
    321 			offs = i * OHCI_TD_SIZE;
    322 			std->physaddr = DMAADDR(&dma) + offs;
    323 			std->td = (ohci_td_t *)
    324 					((char *)KERNADDR(&dma) + offs);
    325 			std->nexttd = sc->sc_freetds;
    326 			sc->sc_freetds = std;
    327 		}
    328 	}
    329 	std = sc->sc_freetds;
    330 	sc->sc_freetds = std->nexttd;
    331 	memset(std->td, 0, OHCI_TD_SIZE);
    332 	std->nexttd = 0;
    333 	return (std);
    334 }
    335 
    336 void
    337 ohci_free_std(sc, std)
    338 	ohci_softc_t *sc;
    339 	ohci_soft_td_t *std;
    340 {
    341 	std->nexttd = sc->sc_freetds;
    342 	sc->sc_freetds = std;
    343 }
    344 
    345 usbd_status
    346 ohci_init(sc)
    347 	ohci_softc_t *sc;
    348 {
    349 	ohci_soft_ed_t *sed, *psed;
    350 	usbd_status r;
    351 	int rev;
    352 	int i;
    353 	u_int32_t s, ctl, ival, hcr, fm, per;
    354 
    355 	DPRINTF(("ohci_init: start\n"));
    356 	rev = OREAD4(sc, OHCI_REVISION);
    357 	printf("%s: OHCI version %d.%d%s\n", sc->sc_bus.bdev.dv_xname,
    358 	       OHCI_REV_HI(rev), OHCI_REV_LO(rev),
    359 	       OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
    360 	if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
    361 		printf("%s: unsupported OHCI revision\n",
    362 		       sc->sc_bus.bdev.dv_xname);
    363 		return (USBD_INVAL);
    364 	}
    365 
    366 	for (i = 0; i < OHCI_HASH_SIZE; i++)
    367 		LIST_INIT(&sc->sc_hash_tds[i]);
    368 
    369 	/* Allocate the HCCA area. */
    370 	r = ohci_allocmem(sc, OHCI_HCCA_SIZE, OHCI_HCCA_ALIGN,&sc->sc_hccadma);
    371 	if (r != USBD_NORMAL_COMPLETION)
    372 		return (r);
    373 	sc->sc_hcca = (struct ohci_hcca *)KERNADDR(&sc->sc_hccadma);
    374 	memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
    375 
    376 	sc->sc_eintrs = OHCI_NORMAL_INTRS;
    377 
    378 	sc->sc_ctrl_head = ohci_alloc_sed(sc);
    379 	if (!sc->sc_ctrl_head) {
    380 		r = USBD_NOMEM;
    381 		goto bad1;
    382 	}
    383 	sc->sc_ctrl_head->ed->ed_flags |= OHCI_ED_SKIP;
    384 	sc->sc_bulk_head = ohci_alloc_sed(sc);
    385 	if (!sc->sc_bulk_head) {
    386 		r = USBD_NOMEM;
    387 		goto bad2;
    388 	}
    389 	sc->sc_bulk_head->ed->ed_flags |= OHCI_ED_SKIP;
    390 
    391 	/* Allocate all the dummy EDs that make up the interrupt tree. */
    392 	for (i = 0; i < OHCI_NO_EDS; i++) {
    393 		sed = ohci_alloc_sed(sc);
    394 		if (!sed) {
    395 			while (--i >= 0)
    396 				ohci_free_sed(sc, sc->sc_eds[i]);
    397 			r = USBD_NOMEM;
    398 			goto bad3;
    399 		}
    400 		/* All ED fields are set to 0. */
    401 		sc->sc_eds[i] = sed;
    402 		sed->ed->ed_flags |= OHCI_ED_SKIP;
    403 		if (i != 0) {
    404 			psed = sc->sc_eds[(i-1) / 2];
    405 			sed->next = psed;
    406 			sed->ed->ed_nexted = psed->physaddr;
    407 		}
    408 	}
    409 	/*
    410 	 * Fill HCCA interrupt table.  The bit reversal is to get
    411 	 * the tree set up properly to spread the interrupts.
    412 	 */
    413 	for (i = 0; i < OHCI_NO_INTRS; i++)
    414 		sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
    415 			sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr;
    416 
    417 	/* Determine in what context we are running. */
    418 	ctl = OREAD4(sc, OHCI_CONTROL);
    419 	if (ctl & OHCI_IR) {
    420 		/* SMM active, request change */
    421 		DPRINTF(("ohci_init: SMM active, request owner change\n"));
    422 		s = OREAD4(sc, OHCI_COMMAND_STATUS);
    423 		OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
    424 		for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
    425 			delay(1000);
    426 			ctl = OREAD4(sc, OHCI_CONTROL);
    427 		}
    428 		if ((ctl & OHCI_IR) == 0) {
    429 			printf("%s: SMM does not respond, resetting\n",
    430 			       sc->sc_bus.bdev.dv_xname);
    431 			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
    432 			goto reset;
    433 		}
    434 	} else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
    435 		/* BIOS started controller. */
    436 		DPRINTF(("ohci_init: BIOS active\n"));
    437 		if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
    438 			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
    439 			delay(USB_RESUME_DELAY * 1000);
    440 		}
    441 	} else {
    442 		DPRINTF(("ohci_init: cold started\n"));
    443 	reset:
    444 		/* Controller was cold started. */
    445 		delay(USB_RESET_DELAY * 1000);
    446 	}
    447 
    448 	/* We now own the host controller and the bus has been reset. */
    449 	ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
    450 
    451 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
    452 	/* Nominal time for a reset is 10 us. */
    453 	for (i = 0; i < 10; i++) {
    454 		delay(10);
    455 		hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
    456 		if (!hcr)
    457 			break;
    458 	}
    459 	if (hcr) {
    460 		printf("%s: reset timeout\n", sc->sc_bus.bdev.dv_xname);
    461 		r = USBD_IOERROR;
    462 		goto bad3;
    463 	}
    464 #ifdef USB_DEBUG
    465 	thesc = sc;
    466 	if (ohcidebug > 15)
    467 		ohci_dumpregs(sc);
    468 #endif
    469 
    470 	/* The controller is now in suspend state, we have 2ms to finish. */
    471 
    472 	/* Set up HC registers. */
    473 	OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma));
    474 	OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
    475 	OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
    476 	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
    477 	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
    478 	ctl = OREAD4(sc, OHCI_CONTROL);
    479 	ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
    480 	ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
    481 		OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
    482 	/* And finally start it! */
    483 	OWRITE4(sc, OHCI_CONTROL, ctl);
    484 
    485 	/*
    486 	 * The controller is now OPERATIONAL.  Set a some final
    487 	 * registers that should be set earlier, but that the
    488 	 * controller ignores when in the SUSPEND state.
    489 	 */
    490 	fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
    491 	fm |= OHCI_FSMPS(ival) | ival;
    492 	OWRITE4(sc, OHCI_FM_INTERVAL, fm);
    493 	per = OHCI_PERIODIC(ival); /* 90% periodic */
    494 	OWRITE4(sc, OHCI_PERIODIC_START, per);
    495 
    496 	OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC);	/* Enable port power */
    497 
    498 	sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
    499 	printf("%s: %d downstream port%s\n",
    500 	       sc->sc_bus.bdev.dv_xname, sc->sc_noport,
    501 	       sc->sc_noport != 1 ? "s" : "");
    502 
    503 #ifdef USB_DEBUG
    504 	if (ohcidebug > 5)
    505 		ohci_dumpregs(sc);
    506 #endif
    507 
    508 	/* Set up the bus struct. */
    509 	sc->sc_bus.open_pipe = ohci_open;
    510 	sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
    511 
    512 	return (USBD_NORMAL_COMPLETION);
    513 
    514  bad3:
    515 	ohci_free_sed(sc, sc->sc_ctrl_head);
    516  bad2:
    517 	ohci_free_sed(sc, sc->sc_bulk_head);
    518  bad1:
    519 	ohci_freemem(sc, &sc->sc_hccadma);
    520 	return (r);
    521 }
    522 
    523 #ifdef USB_DEBUG
    524 void ohcidump(void);
    525 void ohcidump(void) { ohci_dumpregs(thesc); }
    526 
    527 void
    528 ohci_dumpregs(sc)
    529 	ohci_softc_t *sc;
    530 {
    531 	printf("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
    532 	       OREAD4(sc, OHCI_REVISION),
    533 	       OREAD4(sc, OHCI_CONTROL),
    534 	       OREAD4(sc, OHCI_COMMAND_STATUS));
    535 	printf("               intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
    536 	       OREAD4(sc, OHCI_INTERRUPT_STATUS),
    537 	       OREAD4(sc, OHCI_INTERRUPT_ENABLE),
    538 	       OREAD4(sc, OHCI_INTERRUPT_DISABLE));
    539 	printf("               hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
    540 	       OREAD4(sc, OHCI_HCCA),
    541 	       OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
    542 	       OREAD4(sc, OHCI_CONTROL_HEAD_ED));
    543 	printf("               ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
    544 	       OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
    545 	       OREAD4(sc, OHCI_BULK_HEAD_ED),
    546 	       OREAD4(sc, OHCI_BULK_CURRENT_ED));
    547 	printf("               done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
    548 	       OREAD4(sc, OHCI_DONE_HEAD),
    549 	       OREAD4(sc, OHCI_FM_INTERVAL),
    550 	       OREAD4(sc, OHCI_FM_REMAINING));
    551 	printf("               fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
    552 	       OREAD4(sc, OHCI_FM_NUMBER),
    553 	       OREAD4(sc, OHCI_PERIODIC_START),
    554 	       OREAD4(sc, OHCI_LS_THRESHOLD));
    555 	printf("               desca=0x%08x descb=0x%08x stat=0x%08x\n",
    556 	       OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
    557 	       OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
    558 	       OREAD4(sc, OHCI_RH_STATUS));
    559 	printf("               port1=0x%08x port2=0x%08x\n",
    560 	       OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
    561 	       OREAD4(sc, OHCI_RH_PORT_STATUS(2)));
    562 	printf("         HCCA: frame_number=0x%04x done_head=0x%08x\n",
    563 	       sc->sc_hcca->hcca_frame_number,
    564 	       sc->sc_hcca->hcca_done_head);
    565 }
    566 #endif
    567 
    568 int
    569 ohci_intr(p)
    570 	void *p;
    571 {
    572 	ohci_softc_t *sc = p;
    573 	u_int32_t intrs, eintrs;
    574 	ohci_physaddr_t done;
    575 
    576 	done = sc->sc_hcca->hcca_done_head;
    577 	if (done != 0) {
    578 		intrs = OHCI_WDH;
    579 		if (done & OHCI_DONE_INTRS)
    580 			intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
    581 	} else
    582 		intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
    583 	if (!intrs)
    584 		return (0);
    585 	intrs &= ~OHCI_MIE;
    586 	OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
    587 	eintrs = intrs & sc->sc_eintrs;
    588 	if (!eintrs)
    589 		return (0);
    590 
    591 	sc->sc_intrs++;
    592 	DPRINTFN(7, ("ohci_intr: sc=%p intrs=%x(%x) eintr=%x\n",
    593 		     sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
    594 		     (u_int)eintrs));
    595 
    596 	if (eintrs & OHCI_SO) {
    597 		printf("%s: scheduling overrun\n", sc->sc_bus.bdev.dv_xname);
    598 		/* XXX do what */
    599 		intrs &= ~OHCI_SO;
    600 	}
    601 	if (eintrs & OHCI_WDH) {
    602 		ohci_process_done(sc, done &~ OHCI_DONE_INTRS);
    603 		sc->sc_hcca->hcca_done_head = 0;
    604 		intrs &= ~OHCI_WDH;
    605 	}
    606 	if (eintrs & OHCI_RD) {
    607 		/* XXX process resume detect */
    608 	}
    609 	if (eintrs & OHCI_UE) {
    610 		printf("%s: unrecoverable error, controller halted\n",
    611 		       sc->sc_bus.bdev.dv_xname);
    612 		OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
    613 		/* XXX what else */
    614 	}
    615 	if (eintrs & OHCI_RHSC) {
    616 		ohci_rhsc(sc, sc->sc_intrreqh);
    617 		intrs &= ~OHCI_RHSC;
    618 
    619 		/*
    620 		 * Disable RHSC interrupt for now, because it will be
    621 		 * on until the port has been reset.
    622 		 */
    623 		ohci_rhsc_able(sc, 0);
    624 	}
    625 
    626 	/* Block unprocessed interrupts. XXX */
    627 	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, intrs);
    628 	sc->sc_eintrs &= ~intrs;
    629 
    630 	return (1);
    631 }
    632 
    633 void
    634 ohci_rhsc_able(sc, on)
    635 	ohci_softc_t *sc;
    636 	int on;
    637 {
    638 	DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
    639 	if (on) {
    640 		sc->sc_eintrs |= OHCI_RHSC;
    641 		OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
    642 	} else {
    643 		sc->sc_eintrs &= ~OHCI_RHSC;
    644 		OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
    645 	}
    646 }
    647 
    648 void
    649 ohci_process_done(sc, done)
    650 	ohci_softc_t *sc;
    651 	ohci_physaddr_t done;
    652 {
    653 	ohci_soft_td_t *std, *sdone;
    654 	usbd_request_handle reqh;
    655 	int len, cc;
    656 
    657 	DPRINTFN(10,("ohci_process_done: done=0x%08lx\n", (u_long)done));
    658 
    659 	/* Reverse the done list. */
    660 	for (sdone = 0; done; done = std->td->td_nexttd) {
    661 		std = ohci_hash_find_td(sc, done);
    662 		std->dnext = sdone;
    663 		sdone = std;
    664 	}
    665 
    666 #ifdef USB_DEBUG
    667 	if (ohcidebug > 10) {
    668 		printf("ohci_process_done: TD done:\n");
    669 		ohci_dump_tds(sdone);
    670 	}
    671 #endif
    672 
    673 	for (std = sdone; std; std = std->dnext) {
    674 		reqh = std->reqh;
    675 		DPRINTFN(10, ("ohci_process_done: std=%p reqh=%p\n",std,reqh));
    676 		cc = OHCI_TD_GET_CC(std->td->td_flags);
    677 		if (cc == OHCI_CC_NO_ERROR) {
    678 			if (std->td->td_cbp == 0)
    679 				len = std->len;
    680 			else
    681 				len = std->td->td_be - std->td->td_cbp + 1;
    682 			reqh->actlen += len;
    683 			if (reqh->hcpriv == std) {
    684 				switch (reqh->pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
    685 				case UE_CONTROL:
    686 					ohci_ctrl_done(sc, reqh);
    687 					break;
    688 				case UE_INTERRUPT:
    689 					ohci_intr_done(sc, reqh);
    690 					break;
    691 				case UE_BULK:
    692 					ohci_bulk_done(sc, reqh);
    693 					break;
    694 				case UE_ISOCHRONOUS:
    695 					printf("ohci_process_done: ISO done?\n");
    696 					break;
    697 				}
    698 				/* And finally execute callback. */
    699 				reqh->status = USBD_NORMAL_COMPLETION;
    700 				reqh->xfercb(reqh);
    701 			}
    702 		} else {
    703 			ohci_soft_td_t *p, *n;
    704 			struct ohci_pipe *opipe =
    705 				(struct ohci_pipe *)reqh->pipe;
    706 			DPRINTFN(-1,("ohci_process_done: error cc=%d\n",
    707 				     OHCI_TD_GET_CC(std->td->td_flags)));
    708 			/*
    709 			 * Endpoint is halted.  First unlink all the TDs
    710 			 * belonging to the failed transfer, and then restart
    711 			 * the endpoint.
    712 			 */
    713 			for (p = std->nexttd; p->reqh == reqh; p = n) {
    714 				n = p->nexttd;
    715 				ohci_hash_rem_td(sc, p);
    716 				ohci_free_std(sc, p);
    717 			}
    718 			opipe->sed->ed->ed_headp = p->physaddr;/* clear halt */
    719 			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
    720 
    721 			if (cc == OHCI_CC_STALL)
    722 				reqh->status = USBD_STALLED;
    723 			else
    724 				reqh->status = USBD_IOERROR;
    725 			reqh->xfercb(reqh);
    726 		}
    727 		ohci_hash_rem_td(sc, std);
    728 		ohci_free_std(sc, std);
    729 	}
    730 }
    731 
    732 void
    733 ohci_ctrl_done(sc, reqh)
    734 	ohci_softc_t *sc;
    735 	usbd_request_handle reqh;
    736 {
    737 	struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
    738 	u_int len = opipe->u.ctl.length;
    739 	ohci_dma_t *dma;
    740 
    741 	DPRINTFN(10,("ohci_ctrl_done: reqh=%p\n", reqh));
    742 
    743 	if (!reqh->isreq) {
    744 		panic("uhci_ctrl_done: not a request\n");
    745 		return;
    746 	}
    747 
    748 	if (len != 0) {
    749 		dma = &opipe->u.ctl.datadma;
    750 		if (reqh->request.bmRequestType & UT_READ)
    751 			memcpy(reqh->buffer, KERNADDR(dma), len);
    752 		ohci_freemem(sc, dma);
    753 	}
    754 }
    755 
    756 void
    757 ohci_intr_done(sc, reqh)
    758 	ohci_softc_t *sc;
    759 	usbd_request_handle reqh;
    760 {
    761 	struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
    762 	ohci_dma_t *dma;
    763 	ohci_soft_ed_t *sed = opipe->sed;
    764 	ohci_soft_td_t *xfer, *tail;
    765 
    766 
    767 	DPRINTFN(10,("ohci_intr_done: reqh=%p, actlen=%d\n",
    768 		     reqh, reqh->actlen));
    769 
    770 	dma = &opipe->u.intr.datadma;
    771 	memcpy(reqh->buffer, KERNADDR(dma), reqh->actlen);
    772 
    773 	if (reqh->pipe->intrreqh == reqh) {
    774 		xfer = opipe->tail;
    775 		tail = ohci_alloc_std(sc); /* XXX should reuse TD */
    776 		if (!tail) {
    777 			reqh->status = USBD_NOMEM;
    778 			return;
    779 		}
    780 		tail->reqh = 0;
    781 
    782 		xfer->td->td_flags = OHCI_TD_IN | OHCI_TD_NOCC |
    783 			OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY;
    784 		xfer->td->td_cbp = DMAADDR(dma);
    785 		xfer->nexttd = tail;
    786 		xfer->td->td_nexttd = tail->physaddr;
    787 		xfer->td->td_be = xfer->td->td_cbp + reqh->length - 1;
    788 		xfer->len = reqh->length;
    789 		xfer->reqh = reqh;
    790 
    791 		reqh->actlen = 0;
    792 		reqh->hcpriv = xfer;
    793 
    794 		ohci_hash_add_td(sc, xfer);
    795 		sed->ed->ed_tailp = tail->physaddr;
    796 		opipe->tail = tail;
    797 	} else {
    798 		ohci_freemem(sc, dma);
    799 	}
    800 }
    801 
    802 void
    803 ohci_bulk_done(sc, reqh)
    804 	ohci_softc_t *sc;
    805 	usbd_request_handle reqh;
    806 {
    807 	struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
    808 	ohci_dma_t *dma;
    809 
    810 
    811 	DPRINTFN(10,("ohci_bulk_done: reqh=%p, actlen=%d\n",
    812 		     reqh, reqh->actlen));
    813 
    814 	dma = &opipe->u.bulk.datadma;
    815 	if (reqh->request.bmRequestType & UT_READ)
    816 		memcpy(reqh->buffer, KERNADDR(dma), reqh->actlen);
    817 	ohci_freemem(sc, dma);
    818 }
    819 
    820 void
    821 ohci_rhsc(sc, reqh)
    822 	ohci_softc_t *sc;
    823 	usbd_request_handle reqh;
    824 {
    825 	usbd_pipe_handle pipe;
    826 	struct ohci_pipe *opipe;
    827 	u_char *p;
    828 	int i, m;
    829 	int hstatus;
    830 
    831 	hstatus = OREAD4(sc, OHCI_RH_STATUS);
    832 	DPRINTF(("ohci_rhsc: sc=%p reqh=%p hstatus=0x%08x\n",
    833 		 sc, reqh, hstatus));
    834 
    835 	if (reqh == 0) {
    836 		/* Just ignore the change. */
    837 		return;
    838 	}
    839 
    840 	pipe = reqh->pipe;
    841 	opipe = (struct ohci_pipe *)pipe;
    842 
    843 	p = KERNADDR(&opipe->u.intr.datadma);
    844 	m = min(sc->sc_noport, reqh->length * 8 - 1);
    845 	memset(p, 0, reqh->length);
    846 	for (i = 1; i <= m; i++) {
    847 		if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
    848 			p[i/8] |= 1 << (i%8);
    849 	}
    850 	DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
    851 	reqh->actlen = reqh->length;
    852 	reqh->status = USBD_NORMAL_COMPLETION;
    853 	reqh->xfercb(reqh);
    854 
    855 	if (reqh->pipe->intrreqh != reqh) {
    856 		sc->sc_intrreqh = 0;
    857 		ohci_freemem(sc, &opipe->u.intr.datadma);
    858 	}
    859 }
    860 
    861 /*
    862  * Wait here until controller claims to have an interrupt.
    863  * Then call ohci_intr and return.  Use timeout to avoid waiting
    864  * too long.
    865  */
    866 void
    867 ohci_waitintr(sc, reqh)
    868 	ohci_softc_t *sc;
    869 	usbd_request_handle reqh;
    870 {
    871 	int timo = reqh->timeout;
    872 	int usecs;
    873 	u_int32_t intrs;
    874 
    875 	reqh->status = USBD_IN_PROGRESS;
    876 	for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
    877 		delay(1000);
    878 		intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
    879 		DPRINTFN(10,("ohci_waitintr: 0x%04x\n", intrs));
    880 #ifdef USB_DEBUG
    881 		if (ohcidebug > 15)
    882 			ohci_dumpregs(sc);
    883 #endif
    884 		if (intrs) {
    885 			ohci_intr(sc);
    886 			if (reqh->status != USBD_IN_PROGRESS)
    887 				return;
    888 		}
    889 	}
    890 	DPRINTF(("ohci_waitintr: timeout\n"));
    891 	reqh->status = USBD_TIMEOUT;
    892 	reqh->xfercb(reqh);
    893 }
    894 
    895 usbd_status
    896 ohci_device_request(reqh)
    897 	usbd_request_handle reqh;
    898 {
    899 	struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
    900 	usb_device_request_t *req = &reqh->request;
    901 	usbd_device_handle dev = opipe->pipe.device;
    902 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
    903 	int addr = dev->address;
    904 	ohci_soft_td_t *setup, *xfer = 0, *stat, *next, *tail;
    905 	ohci_soft_ed_t *sed;
    906 	ohci_dma_t *dmap;
    907 	int isread;
    908 	int len;
    909 	usbd_status r;
    910 	int s;
    911 
    912 	isread = req->bmRequestType & UT_READ;
    913 	len = UGETW(req->wLength);
    914 
    915 	DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
    916 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
    917 		    UGETW(req->wIndex), len, addr,
    918 		    opipe->pipe.endpoint->edesc->bEndpointAddress));
    919 
    920 	setup = opipe->tail;
    921 	stat = ohci_alloc_std(sc);
    922 	if (!stat) {
    923 		r = USBD_NOMEM;
    924 		goto bad1;
    925 	}
    926 	tail = ohci_alloc_std(sc);
    927 	if (!tail) {
    928 		r = USBD_NOMEM;
    929 		goto bad2;
    930 	}
    931 	tail->reqh = 0;
    932 
    933 	sed = opipe->sed;
    934 	dmap = &opipe->u.ctl.datadma;
    935 	opipe->u.ctl.length = len;
    936 
    937 	/* Update device address */
    938 	sed->ed->ed_flags =
    939 		(sed->ed->ed_flags & ~OHCI_ED_ADDRMASK) |
    940 		OHCI_ED_SET_FA(addr);
    941 
    942 	/* Set up data transaction */
    943 	if (len != 0) {
    944 		xfer = ohci_alloc_std(sc);
    945 		if (!xfer) {
    946 			r = USBD_NOMEM;
    947 			goto bad3;
    948 		}
    949 		r = ohci_allocmem(sc, len, 0, dmap);
    950 		if (r != USBD_NORMAL_COMPLETION)
    951 			goto bad4;
    952 		xfer->td->td_flags =
    953 			(isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
    954 			OHCI_TD_TOGGLE_1 | OHCI_TD_NOINTR;
    955 		xfer->td->td_cbp = DMAADDR(dmap);
    956 		xfer->nexttd = stat;
    957 		xfer->td->td_nexttd = stat->physaddr;
    958 		xfer->td->td_be = xfer->td->td_cbp + len - 1;
    959 		xfer->len = len;
    960 		xfer->reqh = reqh;
    961 
    962 		next = xfer;
    963 	} else
    964 		next = stat;
    965 
    966 	memcpy(KERNADDR(&opipe->u.ctl.reqdma), req, sizeof *req);
    967 	if (!isread && len != 0)
    968 		memcpy(KERNADDR(dmap), reqh->buffer, len);
    969 
    970 	setup->td->td_flags = OHCI_TD_SETUP | OHCI_TD_NOCC |
    971 		              OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR;
    972 	setup->td->td_cbp = DMAADDR(&opipe->u.ctl.reqdma);
    973 	setup->nexttd = next;
    974 	setup->td->td_nexttd = next->physaddr;
    975 	setup->td->td_be = setup->td->td_cbp + sizeof *req - 1;
    976 	setup->len = 0;		/* XXX The number of byte we count */
    977 	setup->reqh = reqh;
    978 
    979 	stat->td->td_flags =
    980 		(isread ? OHCI_TD_OUT : OHCI_TD_IN) | OHCI_TD_NOCC |
    981 		OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1);
    982 	stat->td->td_cbp = 0;
    983 	stat->nexttd = tail;
    984 	stat->td->td_nexttd = tail->physaddr;
    985 	stat->td->td_be = 0;
    986 	stat->len = 0;
    987 	stat->reqh = reqh;
    988 
    989 	reqh->actlen = 0;
    990 	reqh->hcpriv = stat;
    991 
    992 #if USB_DEBUG
    993 	if (ohcidebug > 5) {
    994 		printf("ohci_device_request:\n");
    995 		ohci_dump_ed(sed);
    996 		ohci_dump_tds(setup);
    997 	}
    998 #endif
    999 
   1000 	/* Insert ED in schedule */
   1001 	s = splusb();
   1002 	ohci_hash_add_td(sc, setup);
   1003 	if (len != 0)
   1004 		ohci_hash_add_td(sc, xfer);
   1005 	ohci_hash_add_td(sc, stat);
   1006 	sed->ed->ed_tailp = tail->physaddr;
   1007 	opipe->tail = tail;
   1008 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
   1009 	if (reqh->timeout && !usbd_use_polling)
   1010 		timeout(ohci_timeout, reqh, MS_TO_TICKS(reqh->timeout));
   1011 	splx(s);
   1012 
   1013 #if USB_DEBUG
   1014 	if (ohcidebug > 5) {
   1015 		delay(5000);
   1016 		printf("ohci_device_request: status=%x\n",
   1017 		       OREAD4(sc, OHCI_COMMAND_STATUS));
   1018 		ohci_dump_ed(sed);
   1019 		ohci_dump_tds(setup);
   1020 	}
   1021 #endif
   1022 
   1023 	return (USBD_NORMAL_COMPLETION);
   1024 
   1025  bad4:
   1026 	ohci_free_std(sc, xfer);
   1027  bad3:
   1028 	ohci_free_std(sc, tail);
   1029  bad2:
   1030 	ohci_free_std(sc, stat);
   1031  bad1:
   1032 	return (r);
   1033 }
   1034 
   1035 /*
   1036  * Add an ED to the schedule.  Called at splusb().
   1037  */
   1038 void
   1039 ohci_add_ed(sed, head)
   1040 	ohci_soft_ed_t *sed;
   1041 	ohci_soft_ed_t *head;
   1042 {
   1043 	sed->next = head->next;
   1044 	sed->ed->ed_nexted = head->ed->ed_nexted;
   1045 	head->next = sed;
   1046 	head->ed->ed_nexted = sed->physaddr;
   1047 }
   1048 
   1049 /*
   1050  * Remove an ED from the schedule.  Called at splusb().
   1051  */
   1052 void
   1053 ohci_rem_ed(sed, head)
   1054 	ohci_soft_ed_t *sed;
   1055 	ohci_soft_ed_t *head;
   1056 {
   1057 	ohci_soft_ed_t *p;
   1058 
   1059 	/* XXX */
   1060 	for (p = head; p && p->next != sed; p = p->next)
   1061 		;
   1062 	if (!p)
   1063 		panic("ohci_rem_ed: ED not found\n");
   1064 	p->next = sed->next;
   1065 	p->ed->ed_nexted = sed->ed->ed_nexted;
   1066 }
   1067 
   1068 /*
   1069  * When a transfer is completed the TD is added to the done queue by
   1070  * the host controller.  This queue is the processed by software.
   1071  * Unfortunately the queue contains the physical address of the TD
   1072  * and we have no simple way to translate this back a kernel address.
   1073  * To make the translation possible (and fast) we use a hash table of
   1074  * TDs currently in the schedule.  The physical address is used as the
   1075  * hash value.
   1076  */
   1077 
   1078 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
   1079 /* Called at splusb() */
   1080 void
   1081 ohci_hash_add_td(sc, std)
   1082 	ohci_softc_t *sc;
   1083 	ohci_soft_td_t *std;
   1084 {
   1085 	int h = HASH(std->physaddr);
   1086 
   1087 	LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
   1088 }
   1089 
   1090 /* Called at splusb() */
   1091 void
   1092 ohci_hash_rem_td(sc, std)
   1093 	ohci_softc_t *sc;
   1094 	ohci_soft_td_t *std;
   1095 {
   1096 	LIST_REMOVE(std, hnext);
   1097 }
   1098 
   1099 ohci_soft_td_t *
   1100 ohci_hash_find_td(sc, a)
   1101 	ohci_softc_t *sc;
   1102 	ohci_physaddr_t a;
   1103 {
   1104 	int h = HASH(a);
   1105 	ohci_soft_td_t *std;
   1106 
   1107 	for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
   1108 	     std != 0;
   1109 	     std = LIST_NEXT(std, hnext))
   1110 		if (std->physaddr == a)
   1111 			return (std);
   1112 	panic("ohci_hash_find_td: addr 0x%08lx not found\n", (u_long)a);
   1113 }
   1114 
   1115 void
   1116 ohci_timeout(addr)
   1117 	void *addr;
   1118 {
   1119 #if 0
   1120 	usbd_request_handle *reqh = addr;
   1121 	int s;
   1122 
   1123 	DPRINTF(("ohci_timeout: reqh=%p\n", reqh));
   1124 	s = splusb();
   1125 	/* XXX need to inactivate TD before calling interrupt routine */
   1126 	ohci_XXX_done(reqh);
   1127 	splx(s);
   1128 #endif
   1129 }
   1130 
   1131 #ifdef USB_DEBUG
   1132 void
   1133 ohci_dump_tds(std)
   1134 	ohci_soft_td_t *std;
   1135 {
   1136 	for (; std; std = std->nexttd)
   1137 		ohci_dump_td(std);
   1138 }
   1139 
   1140 void
   1141 ohci_dump_td(std)
   1142 	ohci_soft_td_t *std;
   1143 {
   1144 	printf("TD(%p) at %08lx: %b delay=%d ec=%d cc=%d\ncbp=0x%08lx nexttd=0x%08lx be=0x%08lx\n",
   1145 	       std, (u_long)std->physaddr,
   1146 	       (u_long)std->td->td_flags,
   1147 	       "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
   1148 	       OHCI_TD_GET_DI(std->td->td_flags),
   1149 	       OHCI_TD_GET_EC(std->td->td_flags),
   1150 	       OHCI_TD_GET_CC(std->td->td_flags),
   1151 	       (u_long)std->td->td_cbp,
   1152 	       (u_long)std->td->td_nexttd, (u_long)std->td->td_be);
   1153 }
   1154 
   1155 void
   1156 ohci_dump_ed(sed)
   1157 	ohci_soft_ed_t *sed;
   1158 {
   1159 	printf("ED(%p) at %08lx: addr=%d endpt=%d maxp=%d %b\ntailp=0x%08lx headp=%b nexted=0x%08lx\n",
   1160 	       sed, (u_long)sed->physaddr,
   1161 	       OHCI_ED_GET_FA(sed->ed->ed_flags),
   1162 	       OHCI_ED_GET_EN(sed->ed->ed_flags),
   1163 	       OHCI_ED_GET_MAXP(sed->ed->ed_flags),
   1164 	       (u_long)sed->ed->ed_flags,
   1165 	       "\20\14OUT\15IN\16LOWSPEED\17SKIP\18ISO",
   1166 	       (u_long)sed->ed->ed_tailp,
   1167 	       (u_long)sed->ed->ed_headp, "\20\1HALT\2CARRY",
   1168 	       (u_long)sed->ed->ed_nexted);
   1169 }
   1170 #endif
   1171 
   1172 usbd_status
   1173 ohci_open(pipe)
   1174 	usbd_pipe_handle pipe;
   1175 {
   1176 	usbd_device_handle dev = pipe->device;
   1177 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
   1178 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
   1179 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
   1180 	u_int8_t addr = dev->address;
   1181 	ohci_soft_ed_t *sed;
   1182 	ohci_soft_td_t *std;
   1183 	usbd_status r;
   1184 	int s;
   1185 
   1186 	DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
   1187 		     pipe, addr, ed->bEndpointAddress, sc->sc_addr));
   1188 	if (addr == sc->sc_addr) {
   1189 		switch (ed->bEndpointAddress) {
   1190 		case USB_CONTROL_ENDPOINT:
   1191 			pipe->methods = &ohci_root_ctrl_methods;
   1192 			break;
   1193 		case UE_IN | OHCI_INTR_ENDPT:
   1194 			pipe->methods = &ohci_root_intr_methods;
   1195 			break;
   1196 		default:
   1197 			return (USBD_INVAL);
   1198 		}
   1199 	} else {
   1200 		sed = ohci_alloc_sed(sc);
   1201 		if (sed == 0)
   1202 			goto bad0;
   1203 	        std = ohci_alloc_std(sc);
   1204 		if (std == 0)
   1205 			goto bad1;
   1206 		opipe->sed = sed;
   1207 		opipe->tail = std;
   1208 		sed->ed->ed_flags =
   1209 			OHCI_ED_SET_FA(addr) |
   1210 			OHCI_ED_SET_EN(ed->bEndpointAddress) |
   1211 			OHCI_ED_DIR_TD |
   1212 			(dev->lowspeed ? OHCI_ED_SPEED : 0) |
   1213 			((ed->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS ?
   1214 			 OHCI_ED_FORMAT_ISO : OHCI_ED_FORMAT_GEN) |
   1215 			OHCI_ED_SET_MAXP(UGETW(pipe->endpoint->edesc->wMaxPacketSize));
   1216 		sed->ed->ed_headp = sed->ed->ed_tailp = std->physaddr;
   1217 
   1218 		switch (ed->bmAttributes & UE_XFERTYPE) {
   1219 		case UE_CONTROL:
   1220 			pipe->methods = &ohci_device_ctrl_methods;
   1221 			r = ohci_allocmem(sc, sizeof(ohci_dma_t), 0,
   1222 					  &opipe->u.ctl.reqdma);
   1223 			if (r != USBD_NORMAL_COMPLETION)
   1224 				goto bad;
   1225 			s = splusb();
   1226 			ohci_add_ed(sed, sc->sc_ctrl_head);
   1227 			splx(s);
   1228 			break;
   1229 		case UE_INTERRUPT:
   1230 			pipe->methods = &ohci_device_intr_methods;
   1231 			return (ohci_device_setintr(sc, opipe, ed->bInterval));
   1232 		case UE_ISOCHRONOUS:
   1233 			printf("ohci_open: open iso unimplemented\n");
   1234 			return (USBD_XXX);
   1235 		case UE_BULK:
   1236 			pipe->methods = &ohci_device_bulk_methods;
   1237 			s = splusb();
   1238 			ohci_add_ed(sed, sc->sc_bulk_head);
   1239 			splx(s);
   1240 			break;
   1241 		}
   1242 	}
   1243 	return (USBD_NORMAL_COMPLETION);
   1244 
   1245  bad:
   1246 	ohci_free_std(sc, std);
   1247  bad1:
   1248 	ohci_free_sed(sc, sed);
   1249  bad0:
   1250 	return (USBD_NOMEM);
   1251 
   1252 }
   1253 
   1254 /*
   1255  * Data structures and routines to emulate the root hub.
   1256  */
   1257 usb_device_descriptor_t ohci_devd = {
   1258 	USB_DEVICE_DESCRIPTOR_SIZE,
   1259 	UDESC_DEVICE,		/* type */
   1260 	{0x00, 0x01},		/* USB version */
   1261 	UCLASS_HUB,		/* class */
   1262 	USUBCLASS_HUB,		/* subclass */
   1263 	0,			/* protocol */
   1264 	64,			/* max packet */
   1265 	{0},{0},{0x00,0x01},	/* device id */
   1266 	1,2,0,			/* string indicies */
   1267 	1			/* # of configurations */
   1268 };
   1269 
   1270 usb_config_descriptor_t ohci_confd = {
   1271 	USB_CONFIG_DESCRIPTOR_SIZE,
   1272 	UDESC_CONFIG,
   1273 	{USB_CONFIG_DESCRIPTOR_SIZE +
   1274 	 USB_INTERFACE_DESCRIPTOR_SIZE +
   1275 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
   1276 	1,
   1277 	1,
   1278 	0,
   1279 	UC_SELF_POWERED,
   1280 	0			/* max power */
   1281 };
   1282 
   1283 usb_interface_descriptor_t ohci_ifcd = {
   1284 	USB_INTERFACE_DESCRIPTOR_SIZE,
   1285 	UDESC_INTERFACE,
   1286 	0,
   1287 	0,
   1288 	1,
   1289 	UCLASS_HUB,
   1290 	USUBCLASS_HUB,
   1291 	0,
   1292 	0
   1293 };
   1294 
   1295 usb_endpoint_descriptor_t ohci_endpd = {
   1296 	USB_ENDPOINT_DESCRIPTOR_SIZE,
   1297 	UDESC_ENDPOINT,
   1298 	UE_IN | OHCI_INTR_ENDPT,
   1299 	UE_INTERRUPT,
   1300 	{8, 0},			/* max packet */
   1301 	255
   1302 };
   1303 
   1304 usb_hub_descriptor_t ohci_hubd = {
   1305 	USB_HUB_DESCRIPTOR_SIZE,
   1306 	UDESC_HUB,
   1307 	0,
   1308 	{0,0},
   1309 	0,
   1310 	0,
   1311 	{0},
   1312 	{0},
   1313 };
   1314 
   1315 int
   1316 ohci_str(p, l, s)
   1317 	usb_string_descriptor_t *p;
   1318 	int l;
   1319 	char *s;
   1320 {
   1321 	int i;
   1322 
   1323 	if (l == 0)
   1324 		return (0);
   1325 	p->bLength = 2 * strlen(s) + 2;
   1326 	if (l == 1)
   1327 		return (1);
   1328 	p->bDescriptorType = UDESC_STRING;
   1329 	l -= 2;
   1330 	for (i = 0; s[i] && l > 1; i++, l -= 2)
   1331 		USETW2(p->bString[i], 0, s[i]);
   1332 	return (2*i+2);
   1333 }
   1334 
   1335 /*
   1336  * Simulate a hardware hub by handling all the necessary requests.
   1337  */
   1338 usbd_status
   1339 ohci_root_ctrl_transfer(reqh)
   1340 	usbd_request_handle reqh;
   1341 {
   1342 	ohci_softc_t *sc = (ohci_softc_t *)reqh->pipe->device->bus;
   1343 	usb_device_request_t *req;
   1344 	void *buf;
   1345 	int port, i;
   1346 	int len, value, index, l, totlen = 0;
   1347 	usb_port_status_t ps;
   1348 	usb_hub_descriptor_t hubd;
   1349 	usbd_status r;
   1350 	u_int32_t v;
   1351 
   1352 	if (!reqh->isreq)
   1353 		/* XXX panic */
   1354 		return (USBD_INVAL);
   1355 	req = &reqh->request;
   1356 	buf = reqh->buffer;
   1357 
   1358 	DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
   1359 		    req->bmRequestType, req->bRequest));
   1360 
   1361 	len = UGETW(req->wLength);
   1362 	value = UGETW(req->wValue);
   1363 	index = UGETW(req->wIndex);
   1364 #define C(x,y) ((x) | ((y) << 8))
   1365 	switch(C(req->bRequest, req->bmRequestType)) {
   1366 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   1367 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   1368 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   1369 		/*
   1370 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_STALL are no-ops
   1371 		 * for the integrated root hub.
   1372 		 */
   1373 		break;
   1374 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   1375 		if (len > 0) {
   1376 			*(u_int8_t *)buf = sc->sc_conf;
   1377 			totlen = 1;
   1378 		}
   1379 		break;
   1380 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   1381 		DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
   1382 		switch(value >> 8) {
   1383 		case UDESC_DEVICE:
   1384 			if ((value & 0xff) != 0) {
   1385 				r = USBD_IOERROR;
   1386 				goto ret;
   1387 			}
   1388 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   1389 			memcpy(buf, &ohci_devd, l);
   1390 			break;
   1391 		case UDESC_CONFIG:
   1392 			if ((value & 0xff) != 0) {
   1393 				r = USBD_IOERROR;
   1394 				goto ret;
   1395 			}
   1396 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   1397 			memcpy(buf, &ohci_confd, l);
   1398 			buf = (char *)buf + l;
   1399 			len -= l;
   1400 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   1401 			totlen += l;
   1402 			memcpy(buf, &ohci_ifcd, l);
   1403 			buf = (char *)buf + l;
   1404 			len -= l;
   1405 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   1406 			totlen += l;
   1407 			memcpy(buf, &ohci_endpd, l);
   1408 			break;
   1409 		case UDESC_STRING:
   1410 			if (len == 0)
   1411 				break;
   1412 			*(u_int8_t *)buf = 0;
   1413 			totlen = 1;
   1414 			switch (value & 0xff) {
   1415 			case 1: /* Vendor */
   1416 				totlen = ohci_str(buf, len, sc->sc_vendor);
   1417 				break;
   1418 			case 2: /* Product */
   1419 				totlen = ohci_str(buf, len, "OHCI root hub");
   1420 				break;
   1421 			}
   1422 			break;
   1423 		default:
   1424 			r = USBD_IOERROR;
   1425 			goto ret;
   1426 		}
   1427 		break;
   1428 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   1429 		if (len > 0) {
   1430 			*(u_int8_t *)buf = 0;
   1431 			totlen = 1;
   1432 		}
   1433 		break;
   1434 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   1435 		if (len > 1) {
   1436 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   1437 			totlen = 2;
   1438 		}
   1439 		break;
   1440 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   1441 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   1442 		if (len > 1) {
   1443 			USETW(((usb_status_t *)buf)->wStatus, 0);
   1444 			totlen = 2;
   1445 		}
   1446 		break;
   1447 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   1448 		if (value >= USB_MAX_DEVICES) {
   1449 			r = USBD_IOERROR;
   1450 			goto ret;
   1451 		}
   1452 		sc->sc_addr = value;
   1453 		break;
   1454 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   1455 		if (value != 0 && value != 1) {
   1456 			r = USBD_IOERROR;
   1457 			goto ret;
   1458 		}
   1459 		sc->sc_conf = value;
   1460 		break;
   1461 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   1462 		break;
   1463 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   1464 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   1465 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   1466 		r = USBD_IOERROR;
   1467 		goto ret;
   1468 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   1469 		break;
   1470 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   1471 		break;
   1472 	/* Hub requests */
   1473 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   1474 		break;
   1475 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   1476 		DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE port=%d feature=%d\n",
   1477 			     index, value));
   1478 		if (index < 1 || index > sc->sc_noport) {
   1479 			r = USBD_IOERROR;
   1480 			goto ret;
   1481 		}
   1482 		port = OHCI_RH_PORT_STATUS(index);
   1483 		switch(value) {
   1484 		case UHF_PORT_ENABLE:
   1485 			OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
   1486 			break;
   1487 		case UHF_PORT_SUSPEND:
   1488 			OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
   1489 			break;
   1490 		case UHF_PORT_POWER:
   1491 			OWRITE4(sc, port, UPS_LOW_SPEED);
   1492 			break;
   1493 		case UHF_C_PORT_CONNECTION:
   1494 			OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
   1495 			break;
   1496 		case UHF_C_PORT_ENABLE:
   1497 			OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
   1498 			break;
   1499 		case UHF_C_PORT_SUSPEND:
   1500 			OWRITE4(sc, port, UPS_C_SUSPEND << 16);
   1501 			break;
   1502 		case UHF_C_PORT_OVER_CURRENT:
   1503 			OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
   1504 			break;
   1505 		case UHF_C_PORT_RESET:
   1506 			OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
   1507 			break;
   1508 		default:
   1509 			r = USBD_IOERROR;
   1510 			goto ret;
   1511 		}
   1512 		switch(value) {
   1513 		case UHF_C_PORT_CONNECTION:
   1514 		case UHF_C_PORT_ENABLE:
   1515 		case UHF_C_PORT_SUSPEND:
   1516 		case UHF_C_PORT_OVER_CURRENT:
   1517 		case UHF_C_PORT_RESET:
   1518 			/* Enable RHSC interrupt if condition is cleared. */
   1519 			if ((OREAD4(sc, port) >> 16) == 0)
   1520 				ohci_rhsc_able(sc, 1);
   1521 			break;
   1522 		default:
   1523 			break;
   1524 		}
   1525 		break;
   1526 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   1527 		if (value != 0) {
   1528 			r = USBD_IOERROR;
   1529 			goto ret;
   1530 		}
   1531 		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
   1532 		hubd = ohci_hubd;
   1533 		hubd.bNbrPorts = sc->sc_noport;
   1534 		USETW(hubd.bHubCharacteristics,
   1535 		      (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
   1536 		       v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
   1537 		      /* XXX overcurrent */
   1538 		      );
   1539 		hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
   1540 		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
   1541 		if (sc->sc_noport < 8) {
   1542 			hubd.DeviceRemovable[0] = (u_int8_t)v;
   1543 			hubd.PortPowerCtrlMask[0] = (u_int8_t)(v >> 16);
   1544 			hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE;
   1545 		} else {
   1546 			hubd.DeviceRemovable[0] = (u_int8_t)v;
   1547 			hubd.DeviceRemovable[1] = (u_int8_t)(v>>8);
   1548 			hubd.PortPowerCtrlMask[1] = (u_int8_t)(v >> 16);
   1549 			hubd.PortPowerCtrlMask[2] = (u_int8_t)(v >> 24);
   1550 			hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + 2;
   1551 		}
   1552 		l = min(len, hubd.bDescLength);
   1553 		totlen = l;
   1554 		memcpy(buf, &hubd, l);
   1555 		break;
   1556 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   1557 		if (len != 4) {
   1558 			r = USBD_IOERROR;
   1559 			goto ret;
   1560 		}
   1561 		memset(buf, 0, len); /* ? XXX */
   1562 		totlen = len;
   1563 		break;
   1564 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   1565 		DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
   1566 			    index));
   1567 		if (index < 1 || index > sc->sc_noport) {
   1568 			r = USBD_IOERROR;
   1569 			goto ret;
   1570 		}
   1571 		if (len != 4) {
   1572 			r = USBD_IOERROR;
   1573 			goto ret;
   1574 		}
   1575 		v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
   1576 		DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
   1577 			    v));
   1578 		USETW(ps.wPortStatus, v);
   1579 		USETW(ps.wPortChange, v >> 16);
   1580 		l = min(len, sizeof ps);
   1581 		memcpy(buf, &ps, l);
   1582 		totlen = l;
   1583 		break;
   1584 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   1585 		r = USBD_IOERROR;
   1586 		goto ret;
   1587 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   1588 		break;
   1589 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   1590 		if (index < 1 || index > sc->sc_noport) {
   1591 			r = USBD_IOERROR;
   1592 			goto ret;
   1593 		}
   1594 		port = OHCI_RH_PORT_STATUS(index);
   1595 		switch(value) {
   1596 		case UHF_PORT_ENABLE:
   1597 			OWRITE4(sc, port, UPS_PORT_ENABLED);
   1598 			break;
   1599 		case UHF_PORT_SUSPEND:
   1600 			OWRITE4(sc, port, UPS_SUSPEND);
   1601 			break;
   1602 		case UHF_PORT_RESET:
   1603 			DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n", index));
   1604 			OWRITE4(sc, port, UPS_RESET);
   1605 			for (i = 0; i < 10; i++) {
   1606 				usbd_delay_ms(10);
   1607 				if ((OREAD4(sc, port) & UPS_RESET) == 0)
   1608 					break;
   1609 			}
   1610 			DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
   1611 				    index, OREAD4(sc, port)));
   1612 			break;
   1613 		case UHF_PORT_POWER:
   1614 			DPRINTFN(2,("ohci_root_ctrl_transfer: set port power %d\n", index));
   1615 			OWRITE4(sc, port, UPS_PORT_POWER);
   1616 			break;
   1617 		default:
   1618 			r = USBD_IOERROR;
   1619 			goto ret;
   1620 		}
   1621 		break;
   1622 	default:
   1623 		r = USBD_IOERROR;
   1624 		goto ret;
   1625 	}
   1626 	reqh->actlen = totlen;
   1627 	r = USBD_NORMAL_COMPLETION;
   1628  ret:
   1629 	reqh->status = r;
   1630 	reqh->xfercb(reqh);
   1631 	return (USBD_IN_PROGRESS);
   1632 }
   1633 
   1634 /* Abort a root control request. */
   1635 void
   1636 ohci_root_ctrl_abort(reqh)
   1637 	usbd_request_handle reqh;
   1638 {
   1639 	/* Nothing to do, all transfers are syncronous. */
   1640 }
   1641 
   1642 /* Close the root pipe. */
   1643 void
   1644 ohci_root_ctrl_close(pipe)
   1645 	usbd_pipe_handle pipe;
   1646 {
   1647 	DPRINTF(("ohci_root_ctrl_close\n"));
   1648 }
   1649 
   1650 usbd_status
   1651 ohci_root_intr_transfer(reqh)
   1652 	usbd_request_handle reqh;
   1653 {
   1654 	usbd_pipe_handle pipe = reqh->pipe;
   1655 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
   1656 	struct ohci_pipe *upipe = (struct ohci_pipe *)pipe;
   1657 	ohci_dma_t *dmap;
   1658 	usbd_status r;
   1659 	int len;
   1660 
   1661 	len = reqh->length;
   1662 	dmap = &upipe->u.intr.datadma;
   1663 	if (len == 0)
   1664 		return (USBD_INVAL); /* XXX should it be? */
   1665 
   1666 	r = ohci_allocmem(sc, len, 0, dmap);
   1667 	if (r != USBD_NORMAL_COMPLETION)
   1668 		return (r);
   1669 	sc->sc_intrreqh = reqh;
   1670 
   1671 	return (USBD_IN_PROGRESS);
   1672 }
   1673 
   1674 /* Abort a root interrupt request. */
   1675 void
   1676 ohci_root_intr_abort(reqh)
   1677 	usbd_request_handle reqh;
   1678 {
   1679 	/* No need to abort. */
   1680 }
   1681 
   1682 /* Close the root pipe. */
   1683 void
   1684 ohci_root_intr_close(pipe)
   1685 	usbd_pipe_handle pipe;
   1686 {
   1687 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
   1688 	sc->sc_intrreqh = 0;
   1689 
   1690 	DPRINTF(("ohci_root_intr_close\n"));
   1691 }
   1692 
   1693 /************************/
   1694 
   1695 usbd_status
   1696 ohci_device_ctrl_transfer(reqh)
   1697 	usbd_request_handle reqh;
   1698 {
   1699 	usbd_status r;
   1700 
   1701 	if (!reqh->isreq) {
   1702 		/* XXX panic */
   1703 		printf("ohci_device_ctrl_transfer: not a request\n");
   1704 		return (USBD_INVAL);
   1705 	}
   1706 
   1707 	r = ohci_device_request(reqh);
   1708 	if (r != USBD_NORMAL_COMPLETION)
   1709 		return (r);
   1710 
   1711 	if (usbd_use_polling)
   1712 		ohci_waitintr((ohci_softc_t *)reqh->pipe->device->bus, reqh);
   1713 	return (USBD_IN_PROGRESS);
   1714 }
   1715 
   1716 /* Abort a device control request. */
   1717 void
   1718 ohci_device_ctrl_abort(reqh)
   1719 	usbd_request_handle reqh;
   1720 {
   1721 	/* XXX inactivate */
   1722 	usbd_delay_ms(1);	/* make sure it is finished */
   1723 	/* XXX call done */
   1724 }
   1725 
   1726 /* Close a device control pipe. */
   1727 void
   1728 ohci_device_ctrl_close(pipe)
   1729 	usbd_pipe_handle pipe;
   1730 {
   1731 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
   1732 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
   1733 	ohci_soft_ed_t *sed = opipe->sed;
   1734 	int s;
   1735 
   1736 	s = splusb();
   1737 	sed->ed->ed_flags |= OHCI_ED_SKIP;
   1738 	if ((sed->ed->ed_tailp & OHCI_TAILMASK) != sed->ed->ed_headp)
   1739 		usbd_delay_ms(2);
   1740 	ohci_rem_ed(sed, sc->sc_ctrl_head);
   1741 	splx(s);
   1742 	ohci_free_std(sc, opipe->tail);
   1743 	ohci_free_sed(sc, opipe->sed);
   1744 	/* XXX free other resources */
   1745 }
   1746 
   1747 /************************/
   1748 
   1749 usbd_status
   1750 ohci_device_bulk_transfer(reqh)
   1751 	usbd_request_handle reqh;
   1752 {
   1753 	struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
   1754 	usbd_device_handle dev = opipe->pipe.device;
   1755 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
   1756 	int addr = dev->address;
   1757 	ohci_soft_td_t *xfer, *tail;
   1758 	ohci_soft_ed_t *sed;
   1759 	ohci_dma_t *dmap;
   1760 	usbd_status r;
   1761 	int s, len, isread;
   1762 
   1763 	if (reqh->isreq) {
   1764 		/* XXX panic */
   1765 		printf("ohci_device_bulk_transfer: a request\n");
   1766 		return (USBD_INVAL);
   1767 	}
   1768 
   1769 	len = reqh->length;
   1770 	dmap = &opipe->u.bulk.datadma;
   1771 	isread = reqh->pipe->endpoint->edesc->bEndpointAddress & UE_IN;
   1772 	sed = opipe->sed;
   1773 
   1774 	opipe->u.bulk.length = len;
   1775 
   1776 	r = ohci_allocmem(sc, len, 0, dmap);
   1777 	if (r != USBD_NORMAL_COMPLETION)
   1778 		goto ret1;
   1779 
   1780 	tail = ohci_alloc_std(sc);
   1781 	if (!tail) {
   1782 		r = USBD_NOMEM;
   1783 		goto ret2;
   1784 	}
   1785 	tail->reqh = 0;
   1786 
   1787 	/* Update device address */
   1788 	sed->ed->ed_flags =
   1789 		(sed->ed->ed_flags & ~OHCI_ED_ADDRMASK) |
   1790 		OHCI_ED_SET_FA(addr);
   1791 
   1792 	/* Set up data transaction */
   1793 	xfer = opipe->tail;
   1794 	xfer->td->td_flags =
   1795 		(isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
   1796 		OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY;
   1797 	xfer->td->td_cbp = DMAADDR(dmap);
   1798 	xfer->nexttd = tail;
   1799 	xfer->td->td_nexttd = tail->physaddr;
   1800 	xfer->td->td_be = xfer->td->td_cbp + len - 1;
   1801 	xfer->len = len;
   1802 	xfer->reqh = reqh;
   1803 
   1804 	reqh->actlen = 0;
   1805 	reqh->hcpriv = xfer;
   1806 
   1807 	if (!isread)
   1808 		memcpy(KERNADDR(dmap), reqh->buffer, len);
   1809 
   1810 	/* Insert ED in schedule */
   1811 	s = splusb();
   1812 	ohci_hash_add_td(sc, xfer);
   1813 	sed->ed->ed_tailp = tail->physaddr;
   1814 	opipe->tail = tail;
   1815 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
   1816 	if (reqh->timeout && !usbd_use_polling)
   1817 		timeout(ohci_timeout, reqh, MS_TO_TICKS(reqh->timeout));
   1818 	splx(s);
   1819 
   1820 	return (USBD_IN_PROGRESS);
   1821 
   1822  ret2:
   1823 	ohci_freemem(sc, dmap);
   1824  ret1:
   1825 	return (r);
   1826 }
   1827 
   1828 /* Abort a device bulk request. */
   1829 void
   1830 ohci_device_bulk_abort(reqh)
   1831 	usbd_request_handle reqh;
   1832 {
   1833 #if 0
   1834 	sed->ed->ed_flags |= OHCI_ED_SKIP;
   1835 	if ((sed->ed->ed_tailp & OHCI_TAILMASK) != sed->ed->ed_headp)
   1836 		usbd_delay_ms(2);
   1837 #endif
   1838 	/* XXX inactivate */
   1839 	usbd_delay_ms(1);	/* make sure it is finished */
   1840 	/* XXX call done */
   1841 }
   1842 
   1843 /* Close a device bulk pipe. */
   1844 void
   1845 ohci_device_bulk_close(pipe)
   1846 	usbd_pipe_handle pipe;
   1847 {
   1848 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
   1849 	usbd_device_handle dev = opipe->pipe.device;
   1850 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
   1851 	int s;
   1852 
   1853 	s = splusb();
   1854 	ohci_rem_ed(opipe->sed, sc->sc_bulk_head);
   1855 	splx(s);
   1856 	ohci_free_std(sc, opipe->tail);
   1857 	ohci_free_sed(sc, opipe->sed);
   1858 	/* XXX free other resources */
   1859 }
   1860 
   1861 /************************/
   1862 
   1863 usbd_status
   1864 ohci_device_intr_transfer(reqh)
   1865 	usbd_request_handle reqh;
   1866 {
   1867 	struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
   1868 	usbd_device_handle dev = opipe->pipe.device;
   1869 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
   1870 	ohci_soft_ed_t *sed = opipe->sed;
   1871 	ohci_soft_td_t *xfer, *tail;
   1872 	ohci_dma_t *dmap;
   1873 	usbd_status r;
   1874 	int len;
   1875 	int s;
   1876 
   1877 	DPRINTFN(3, ("ohci_device_intr_transfer: reqh=%p buf=%p len=%d flags=%d priv=%p\n",
   1878 		 reqh, reqh->buffer, reqh->length, reqh->flags, reqh->priv));
   1879 
   1880 	if (reqh->isreq)
   1881 		panic("ohci_device_intr_transfer: a request\n");
   1882 
   1883 	len = reqh->length;
   1884 	dmap = &opipe->u.intr.datadma;
   1885 	if (len == 0)
   1886 		return (USBD_INVAL); /* XXX should it be? */
   1887 
   1888 	xfer = opipe->tail;
   1889 	tail = ohci_alloc_std(sc);
   1890 	if (!tail) {
   1891 		r = USBD_NOMEM;
   1892 		goto ret1;
   1893 	}
   1894 	tail->reqh = 0;
   1895 
   1896 	r = ohci_allocmem(sc, len, 0, dmap);
   1897 	if (r != USBD_NORMAL_COMPLETION)
   1898 		goto ret2;
   1899 
   1900 	xfer->td->td_flags = OHCI_TD_IN | OHCI_TD_NOCC |
   1901 		OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY;
   1902 	xfer->td->td_cbp = DMAADDR(dmap);
   1903 	xfer->nexttd = tail;
   1904 	xfer->td->td_nexttd = tail->physaddr;
   1905 	xfer->td->td_be = xfer->td->td_cbp + len - 1;
   1906 	xfer->len = len;
   1907 	xfer->reqh = reqh;
   1908 
   1909 	reqh->actlen = 0;
   1910 	reqh->hcpriv = xfer;
   1911 
   1912 #if USB_DEBUG
   1913 	if (ohcidebug > 5) {
   1914 		printf("ohci_device_intr_transfer:\n");
   1915 		ohci_dump_ed(sed);
   1916 		ohci_dump_tds(xfer);
   1917 	}
   1918 #endif
   1919 
   1920 	/* Insert ED in schedule */
   1921 	s = splusb();
   1922 	ohci_hash_add_td(sc, xfer);
   1923 	sed->ed->ed_tailp = tail->physaddr;
   1924 	opipe->tail = tail;
   1925 #if 0
   1926 	if (reqh->timeout && !usbd_use_polling)
   1927 		timeout(ohci_timeout, reqh, MS_TO_TICKS(reqh->timeout));
   1928 #endif
   1929 	sed->ed->ed_flags &= ~OHCI_ED_SKIP;
   1930 	splx(s);
   1931 
   1932 #ifdef USB_DEBUG
   1933 	if (ohcidebug > 5) {
   1934 		delay(5000);
   1935 		printf("ohci_device_intr_transfer: status=%x\n",
   1936 		       OREAD4(sc, OHCI_COMMAND_STATUS));
   1937 		ohci_dump_ed(sed);
   1938 		ohci_dump_tds(xfer);
   1939 	}
   1940 #endif
   1941 
   1942 	return (USBD_IN_PROGRESS);
   1943 
   1944  ret2:
   1945 	ohci_free_std(sc, xfer);
   1946  ret1:
   1947 	return (r);
   1948 }
   1949 
   1950 /* Abort a device control request. */
   1951 void
   1952 ohci_device_intr_abort(reqh)
   1953 	usbd_request_handle reqh;
   1954 {
   1955 	struct uhci_pipe *opipe;
   1956 
   1957 	/* XXX inactivate */
   1958 	usbd_delay_ms(1);	/* make sure it is finished */
   1959 	if (reqh->pipe->intrreqh == reqh) {
   1960 		DPRINTF(("ohci_device_intr_abort: remove\n"));
   1961 		reqh->pipe->intrreqh = 0;
   1962 		opipe = (struct uhci_pipe *)reqh->pipe;
   1963 		ohci_intr_done((ohci_softc_t *)reqh->pipe->device->bus, reqh);
   1964 	}
   1965 }
   1966 
   1967 /* Close a device interrupt pipe. */
   1968 void
   1969 ohci_device_intr_close(pipe)
   1970 	usbd_pipe_handle pipe;
   1971 {
   1972 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
   1973 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
   1974 	int nslots = opipe->u.intr.nslots;
   1975 	int pos = opipe->u.intr.pos;
   1976 	int j;
   1977 	ohci_soft_ed_t *p, *sed = opipe->sed;
   1978 	int s;
   1979 
   1980 	DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
   1981 		    pipe, nslots, pos));
   1982 	s = splusb();
   1983 	sed->ed->ed_flags |= OHCI_ED_SKIP;
   1984 	if ((sed->ed->ed_tailp & OHCI_TAILMASK) != sed->ed->ed_headp)
   1985 		usbd_delay_ms(2);
   1986 
   1987 	for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
   1988 		;
   1989 	if (!p)
   1990 		panic("ohci_device_intr_close: ED not found\n");
   1991 	p->next = sed->next;
   1992 	p->ed->ed_nexted = sed->ed->ed_nexted;
   1993 	splx(s);
   1994 
   1995 	for (j = 0; j < nslots; j++)
   1996 		--sc->sc_bws[pos * nslots + j];
   1997 
   1998 	ohci_free_std(sc, opipe->tail);
   1999 	ohci_free_sed(sc, opipe->sed);
   2000 	/* XXX free other resources */
   2001 }
   2002 
   2003 usbd_status
   2004 ohci_device_setintr(sc, opipe, ival)
   2005 	ohci_softc_t *sc;
   2006 	struct ohci_pipe *opipe;
   2007 	int ival;
   2008 {
   2009 	int i, j, s, best;
   2010 	u_int npoll, slow, shigh, nslots;
   2011 	u_int bestbw, bw;
   2012 	ohci_soft_ed_t *hsed, *sed = opipe->sed;
   2013 
   2014 	DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
   2015 	if (ival == 0) {
   2016 		printf("ohci_setintr: 0 interval\n");
   2017 		return (USBD_INVAL);
   2018 	}
   2019 
   2020 	npoll = OHCI_NO_INTRS;
   2021 	while (npoll > ival)
   2022 		npoll /= 2;
   2023 	DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
   2024 
   2025 	/*
   2026 	 * We now know which level in the tree the ED must go into.
   2027 	 * Figure out which slot has most bandwidth left over.
   2028 	 * Slots to examine:
   2029 	 * npoll
   2030 	 * 1	0
   2031 	 * 2	1 2
   2032 	 * 4	3 4 5 6
   2033 	 * 8	7 8 9 10 11 12 13 14
   2034 	 * N    (N-1) .. (N-1+N-1)
   2035 	 */
   2036 	slow = npoll-1;
   2037 	shigh = slow + npoll;
   2038 	nslots = OHCI_NO_INTRS / npoll;
   2039 	for (best = i = slow, bestbw = ~0; i < shigh; i++) {
   2040 		bw = 0;
   2041 		for (j = 0; j < nslots; j++)
   2042 			bw += sc->sc_bws[i * nslots + j];
   2043 		if (bw < bestbw) {
   2044 			best = i;
   2045 			bestbw = bw;
   2046 		}
   2047 	}
   2048 	DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
   2049 		     best, slow, shigh, bestbw));
   2050 
   2051 	s = splusb();
   2052 	hsed = sc->sc_eds[best];
   2053 	sed->next = hsed->next;
   2054 	sed->ed->ed_nexted = hsed->ed->ed_nexted;
   2055 	hsed->next = sed;
   2056 	hsed->ed->ed_nexted = sed->physaddr;
   2057 	splx(s);
   2058 
   2059 	for (j = 0; j < nslots; j++)
   2060 		++sc->sc_bws[best * nslots + j];
   2061 	opipe->u.intr.nslots = nslots;
   2062 	opipe->u.intr.pos = best;
   2063 
   2064 	DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
   2065 	return (USBD_NORMAL_COMPLETION);
   2066 }
   2067 
   2068