1 /* $NetBSD: ehci.c,v 1.91 2005/02/27 00:27:51 perry Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) and by Charles M. Hannum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
40 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
42 * The EHCI 1.0 spec can be found at
43 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
44 * and the USB 2.0 spec at
45 * http://www.usb.org/developers/docs/usb_20.zip
51 * 1) The EHCI driver lacks support for isochronous transfers, so
52 * devices using them don't work.
54 * 2) Interrupt transfer scheduling does not manage the time available
55 * in each frame, so it is possible for the transfers to overrun
56 * the end of the frame.
58 * 3) Command failures are not recovered correctly.
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/malloc.h>
67 #include <sys/kernel.h>
68 #include <sys/endian.h>
69 #include <sys/module.h>
72 #include <sys/lockmgr.h>
73 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
74 #include <machine/cpu.h>
77 #include <sys/queue.h>
78 #include <sys/sysctl.h>
80 #include <machine/bus.h>
81 #include <machine/endian.h>
83 #include <dev/usb/usb.h>
84 #include <dev/usb/usbdi.h>
85 #include <dev/usb/usbdivar.h>
86 #include <dev/usb/usb_mem.h>
87 #include <dev/usb/usb_quirks.h>
89 #include <dev/usb/ehcireg.h>
90 #include <dev/usb/ehcivar.h>
92 #define delay(d) DELAY(d)
95 #define EHCI_DEBUG USB_DEBUG
96 #define DPRINTF(x) do { if (ehcidebug) printf x; } while (0)
97 #define DPRINTFN(n,x) do { if (ehcidebug>(n)) printf x; } while (0)
99 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
100 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
101 &ehcidebug, 0, "ehci debug level");
102 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
105 #define DPRINTFN(n,x)
109 struct usbd_pipe pipe;
113 ehci_soft_qtd_t *qtd;
114 /* ehci_soft_itd_t *itd; */
121 /*ehci_soft_qtd_t *setup, *data, *stat;*/
139 static usbd_status ehci_open(usbd_pipe_handle);
140 static void ehci_poll(struct usbd_bus *);
141 static void ehci_softintr(void *);
142 static int ehci_intr1(ehci_softc_t *);
143 static void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
144 static void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
145 static void ehci_check_qh_intr(ehci_softc_t *, struct ehci_xfer *);
146 static void ehci_check_itd_intr(ehci_softc_t *, struct ehci_xfer *);
147 static void ehci_idone(struct ehci_xfer *);
148 static void ehci_timeout(void *);
149 static void ehci_timeout_task(void *);
150 static void ehci_intrlist_timeout(void *);
152 static usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
153 static void ehci_freem(struct usbd_bus *, usb_dma_t *);
155 static usbd_xfer_handle ehci_allocx(struct usbd_bus *);
156 static void ehci_freex(struct usbd_bus *, usbd_xfer_handle);
158 static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle);
159 static usbd_status ehci_root_ctrl_start(usbd_xfer_handle);
160 static void ehci_root_ctrl_abort(usbd_xfer_handle);
161 static void ehci_root_ctrl_close(usbd_pipe_handle);
162 static void ehci_root_ctrl_done(usbd_xfer_handle);
164 static usbd_status ehci_root_intr_transfer(usbd_xfer_handle);
165 static usbd_status ehci_root_intr_start(usbd_xfer_handle);
166 static void ehci_root_intr_abort(usbd_xfer_handle);
167 static void ehci_root_intr_close(usbd_pipe_handle);
168 static void ehci_root_intr_done(usbd_xfer_handle);
170 static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle);
171 static usbd_status ehci_device_ctrl_start(usbd_xfer_handle);
172 static void ehci_device_ctrl_abort(usbd_xfer_handle);
173 static void ehci_device_ctrl_close(usbd_pipe_handle);
174 static void ehci_device_ctrl_done(usbd_xfer_handle);
176 static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle);
177 static usbd_status ehci_device_bulk_start(usbd_xfer_handle);
178 static void ehci_device_bulk_abort(usbd_xfer_handle);
179 static void ehci_device_bulk_close(usbd_pipe_handle);
180 static void ehci_device_bulk_done(usbd_xfer_handle);
182 static usbd_status ehci_device_intr_transfer(usbd_xfer_handle);
183 static usbd_status ehci_device_intr_start(usbd_xfer_handle);
184 static void ehci_device_intr_abort(usbd_xfer_handle);
185 static void ehci_device_intr_close(usbd_pipe_handle);
186 static void ehci_device_intr_done(usbd_xfer_handle);
188 static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle);
189 static usbd_status ehci_device_isoc_start(usbd_xfer_handle);
190 static void ehci_device_isoc_abort(usbd_xfer_handle);
191 static void ehci_device_isoc_close(usbd_pipe_handle);
192 static void ehci_device_isoc_done(usbd_xfer_handle);
194 static void ehci_device_clear_toggle(usbd_pipe_handle pipe);
195 static void ehci_noop(usbd_pipe_handle pipe);
197 static int ehci_str(usb_string_descriptor_t *, int, char *);
198 static void ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
199 static void ehci_disown(ehci_softc_t *, int, int);
201 static ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *);
202 static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
204 static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
205 static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
206 static usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *,
207 ehci_softc_t *, int, int, usbd_xfer_handle,
208 ehci_soft_qtd_t *, ehci_soft_qtd_t *,
209 ehci_soft_qtd_t **, ehci_soft_qtd_t **);
210 static void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qh_t *,
211 ehci_soft_qtd_t *, ehci_soft_qtd_t *);
213 static ehci_soft_itd_t *ehci_alloc_itd(ehci_softc_t *);
214 static void ehci_free_itd(ehci_softc_t *, ehci_soft_itd_t *);
215 static void ehci_rem_free_itd_chain(ehci_softc_t *,
217 static void ehci_abort_isoc_xfer(usbd_xfer_handle, usbd_status);
219 static usbd_status ehci_device_request(usbd_xfer_handle xfer);
221 static usbd_status ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
224 static void ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *);
225 static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
227 static void ehci_activate_qh(ehci_soft_qh_t *, ehci_soft_qtd_t *);
228 static void ehci_sync_hc(ehci_softc_t *);
230 static void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
231 static void ehci_abort_xfer(usbd_xfer_handle, usbd_status);
234 static void ehci_dump_regs(ehci_softc_t *);
235 void ehci_dump(void);
236 static ehci_softc_t *theehci;
237 static void ehci_dump_link(ehci_link_t, int);
238 static void ehci_dump_sqtds(ehci_soft_qtd_t *);
239 static void ehci_dump_sqtd(ehci_soft_qtd_t *);
240 static void ehci_dump_qtd(ehci_qtd_t *);
241 static void ehci_dump_sqh(ehci_soft_qh_t *);
243 static void ehci_dump_sitd(struct ehci_soft_itd *);
244 static void ehci_dump_itd(struct ehci_soft_itd *);
247 static void ehci_dump_exfer(struct ehci_xfer *);
251 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
253 #define EHCI_INTR_ENDPT 1
255 #define ehci_add_intr_list(sc, ex) \
256 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext);
257 #define ehci_del_intr_list(ex) \
259 LIST_REMOVE((ex), inext); \
260 (ex)->inext.le_prev = NULL; \
262 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL)
264 static struct usbd_bus_methods ehci_bus_methods = {
274 static struct usbd_pipe_methods ehci_root_ctrl_methods = {
275 ehci_root_ctrl_transfer,
276 ehci_root_ctrl_start,
277 ehci_root_ctrl_abort,
278 ehci_root_ctrl_close,
283 static struct usbd_pipe_methods ehci_root_intr_methods = {
284 ehci_root_intr_transfer,
285 ehci_root_intr_start,
286 ehci_root_intr_abort,
287 ehci_root_intr_close,
292 static struct usbd_pipe_methods ehci_device_ctrl_methods = {
293 ehci_device_ctrl_transfer,
294 ehci_device_ctrl_start,
295 ehci_device_ctrl_abort,
296 ehci_device_ctrl_close,
298 ehci_device_ctrl_done,
301 static struct usbd_pipe_methods ehci_device_intr_methods = {
302 ehci_device_intr_transfer,
303 ehci_device_intr_start,
304 ehci_device_intr_abort,
305 ehci_device_intr_close,
306 ehci_device_clear_toggle,
307 ehci_device_intr_done,
310 static struct usbd_pipe_methods ehci_device_bulk_methods = {
311 ehci_device_bulk_transfer,
312 ehci_device_bulk_start,
313 ehci_device_bulk_abort,
314 ehci_device_bulk_close,
315 ehci_device_clear_toggle,
316 ehci_device_bulk_done,
319 static struct usbd_pipe_methods ehci_device_isoc_methods = {
320 ehci_device_isoc_transfer,
321 ehci_device_isoc_start,
322 ehci_device_isoc_abort,
323 ehci_device_isoc_close,
325 ehci_device_isoc_done,
329 ehci_hcreset(ehci_softc_t *sc)
334 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
335 for (i = 0; i < 100; i++) {
336 usb_delay_ms(&sc->sc_bus, 1);
337 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
343 * Fall through and try reset anyway even though
344 * Table 2-9 in the EHCI spec says this will result
345 * in undefined behavior.
347 printf("%s: stop timeout\n",
348 device_get_nameunit(sc->sc_bus.bdev));
350 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
351 for (i = 0; i < 100; i++) {
352 usb_delay_ms(&sc->sc_bus, 1);
353 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
355 return (USBD_NORMAL_COMPLETION);
357 printf("%s: reset timeout\n", device_get_nameunit(sc->sc_bus.bdev));
358 return (USBD_IOERROR);
362 ehci_init(ehci_softc_t *sc)
364 u_int32_t version, sparams, cparams, hcr;
371 DPRINTF(("ehci_init: start\n"));
376 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
378 version = EREAD2(sc, EHCI_HCIVERSION);
379 printf("%s: EHCI version %x.%x\n", device_get_nameunit(sc->sc_bus.bdev),
380 version >> 8, version & 0xff);
382 sparams = EREAD4(sc, EHCI_HCSPARAMS);
383 DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
384 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
385 ncomp = EHCI_HCS_N_CC(sparams);
386 if (ncomp != sc->sc_ncomp) {
387 printf("%s: wrong number of companions (%d != %d)\n",
388 device_get_nameunit(sc->sc_bus.bdev),
389 ncomp, sc->sc_ncomp);
390 if (ncomp < sc->sc_ncomp)
391 sc->sc_ncomp = ncomp;
393 if (sc->sc_ncomp > 0) {
394 printf("%s: companion controller%s, %d port%s each:",
395 device_get_nameunit(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
396 EHCI_HCS_N_PCC(sparams),
397 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
398 for (i = 0; i < sc->sc_ncomp; i++)
399 printf(" %s", device_get_nameunit(sc->sc_comps[i]->bdev));
402 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
403 cparams = EREAD4(sc, EHCI_HCCPARAMS);
404 DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
406 if (EHCI_HCC_64BIT(cparams)) {
407 /* MUST clear segment register if 64 bit capable. */
408 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
411 sc->sc_bus.usbrev = USBREV_2_0;
413 /* Reset the controller */
414 DPRINTF(("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev)));
415 err = ehci_hcreset(sc);
416 if (err != USBD_NORMAL_COMPLETION)
419 /* frame list size at default, read back what we got and use that */
420 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
421 case 0: sc->sc_flsize = 1024; break;
422 case 1: sc->sc_flsize = 512; break;
423 case 2: sc->sc_flsize = 256; break;
424 case 3: return (USBD_IOERROR);
426 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t),
427 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
430 DPRINTF(("%s: flsize=%d\n", device_get_nameunit(sc->sc_bus.bdev),sc->sc_flsize));
431 sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
433 for (i = 0; i < sc->sc_flsize; i++) {
434 sc->sc_flist[i] = EHCI_NULL;
437 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
439 sc->sc_softitds = malloc(sc->sc_flsize * sizeof(ehci_soft_itd_t *),
440 M_USB, M_NOWAIT | M_ZERO);
441 if (sc->sc_softitds == NULL)
443 LIST_INIT(&sc->sc_freeitds);
445 /* Set up the bus struct. */
446 sc->sc_bus.methods = &ehci_bus_methods;
447 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
449 #if defined(__NetBSD__) || defined(__OpenBSD__)
450 sc->sc_powerhook = powerhook_establish(ehci_power, sc);
451 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
454 sc->sc_eintrs = EHCI_NORMAL_INTRS;
457 * Allocate the interrupt dummy QHs. These are arranged to give
458 * poll intervals that are powers of 2 times 1ms.
460 for (i = 0; i < EHCI_INTRQHS; i++) {
461 sqh = ehci_alloc_sqh(sc);
466 sc->sc_islots[i].sqh = sqh;
469 for (i = 0; i < EHCI_INTRQHS; i++) {
470 if (i == EHCI_IQHIDX(lev + 1, 0))
472 sqh = sc->sc_islots[i].sqh;
474 /* The last (1ms) QH terminates. */
475 sqh->qh.qh_link = EHCI_NULL;
478 /* Otherwise the next QH has half the poll interval */
480 sc->sc_islots[EHCI_IQHIDX(lev - 1, i + 1)].sqh;
481 sqh->qh.qh_link = htole32(sqh->next->physaddr |
484 sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
485 sqh->qh.qh_endphub = htole32(EHCI_QH_SET_MULT(1));
486 sqh->qh.qh_curqtd = EHCI_NULL;
487 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
488 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
489 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
491 /* Point the frame list at the last level (128ms). */
492 for (i = 0; i < sc->sc_flsize; i++) {
493 sc->sc_flist[i] = htole32(EHCI_LINK_QH |
494 sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1,
498 /* Allocate dummy QH that starts the async list. */
499 sqh = ehci_alloc_sqh(sc);
506 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
508 htole32(sqh->physaddr | EHCI_LINK_QH);
509 sqh->qh.qh_curqtd = EHCI_NULL;
510 sqh->prev = sqh; /*It's a circular list.. */
512 /* Fill the overlay qTD */
513 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
514 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
515 sqh->qh.qh_qtd.qtd_status = htole32(0);
522 /* Point to async list */
523 sc->sc_async_head = sqh;
524 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
526 callout_init(&sc->sc_tmo_intrlist, 0);
528 lockinit(&sc->sc_doorbell_lock, PZERO, "ehcidb", 0, 0);
530 /* Enable interrupts */
531 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
533 /* Turn on controller */
534 EOWRITE4(sc, EHCI_USBCMD,
535 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */
536 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
541 /* Take over port ownership */
542 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
544 for (i = 0; i < 100; i++) {
545 usb_delay_ms(&sc->sc_bus, 1);
546 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
551 printf("%s: run timeout\n", device_get_nameunit(sc->sc_bus.bdev));
552 return (USBD_IOERROR);
555 return (USBD_NORMAL_COMPLETION);
559 ehci_free_sqh(sc, sc->sc_async_head);
562 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
569 ehci_softc_t *sc = v;
571 if (sc == NULL || sc->sc_dying)
574 /* If we get an interrupt while polling, then just ignore it. */
575 if (sc->sc_bus.use_polling) {
576 u_int32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
579 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
581 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
586 return (ehci_intr1(sc));
590 ehci_intr1(ehci_softc_t *sc)
592 u_int32_t intrs, eintrs;
594 DPRINTFN(20,("ehci_intr1: enter\n"));
596 /* In case the interrupt occurs before initialization has completed. */
599 printf("ehci_intr1: sc == NULL\n");
604 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
608 eintrs = intrs & sc->sc_eintrs;
609 DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
610 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
615 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
616 sc->sc_bus.intr_context++;
617 sc->sc_bus.no_intrs++;
618 if (eintrs & EHCI_STS_IAA) {
619 DPRINTF(("ehci_intr1: door bell\n"));
620 wakeup(&sc->sc_async_head);
621 eintrs &= ~EHCI_STS_IAA;
623 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
624 DPRINTFN(5,("ehci_intr1: %s %s\n",
625 eintrs & EHCI_STS_INT ? "INT" : "",
626 eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
627 usb_schedsoftintr(&sc->sc_bus);
628 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
630 if (eintrs & EHCI_STS_HSE) {
631 printf("%s: unrecoverable error, controller halted\n",
632 device_get_nameunit(sc->sc_bus.bdev));
635 if (eintrs & EHCI_STS_PCD) {
636 ehci_pcd(sc, sc->sc_intrxfer);
637 eintrs &= ~EHCI_STS_PCD;
640 sc->sc_bus.intr_context--;
643 /* Block unprocessed interrupts. */
644 sc->sc_eintrs &= ~eintrs;
645 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
646 printf("%s: blocking intrs 0x%x\n",
647 device_get_nameunit(sc->sc_bus.bdev), eintrs);
654 * XXX write back xfer data for architectures with a write-back
655 * data cache; this is a hack because usb is mis-architected
656 * in blindly mixing bus_dma w/ PIO.
659 hacksync(usbd_xfer_handle xfer)
662 struct usb_dma_mapping *dmap;
664 if (xfer->length == 0)
666 tag = xfer->pipe->device->bus->buffer_dmatag;
667 dmap = &xfer->dmamap;
668 bus_dmamap_sync(tag, dmap->map, BUS_DMASYNC_PREWRITE);
672 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
674 usbd_pipe_handle pipe;
679 /* Just ignore the change. */
686 m = min(sc->sc_noport, xfer->length * 8 - 1);
687 memset(p, 0, xfer->length);
688 for (i = 1; i <= m; i++) {
689 /* Pick out CHANGE bits from the status reg. */
690 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
691 p[i/8] |= 1 << (i%8);
693 DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
694 xfer->actlen = xfer->length;
695 xfer->status = USBD_NORMAL_COMPLETION;
697 hacksync(xfer); /* XXX to compensate for usb_transfer_complete */
698 usb_transfer_complete(xfer);
702 ehci_softintr(void *v)
704 ehci_softc_t *sc = v;
705 struct ehci_xfer *ex, *nextex;
707 DPRINTFN(10,("%s: ehci_softintr (%d)\n", device_get_nameunit(sc->sc_bus.bdev),
708 sc->sc_bus.intr_context));
710 sc->sc_bus.intr_context++;
713 * The only explanation I can think of for why EHCI is as brain dead
714 * as UHCI interrupt-wise is that Intel was involved in both.
715 * An interrupt just tells us that something is done, we have no
716 * clue what, so we need to scan through all active transfers. :-(
718 for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
719 nextex = LIST_NEXT(ex, inext);
720 ehci_check_intr(sc, ex);
723 /* Schedule a callout to catch any dropped transactions. */
724 if ((sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) &&
725 !LIST_EMPTY(&sc->sc_intrhead))
726 callout_reset(&sc->sc_tmo_intrlist, hz / 5,
727 ehci_intrlist_timeout, sc);
729 #ifdef USB_USE_SOFTINTR
730 if (sc->sc_softwake) {
732 wakeup(&sc->sc_softwake);
734 #endif /* USB_USE_SOFTINTR */
736 sc->sc_bus.intr_context--;
739 /* Check for an interrupt. */
741 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
745 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
747 attr = ex->xfer.pipe->endpoint->edesc->bmAttributes;
748 if (UE_GET_XFERTYPE(attr) == UE_ISOCHRONOUS)
749 ehci_check_itd_intr(sc, ex);
751 ehci_check_qh_intr(sc, ex);
755 ehci_check_qh_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
757 ehci_soft_qtd_t *sqtd, *lsqtd;
760 if (ex->sqtdstart == NULL) {
761 printf("ehci_check_qh_intr: not valid sqtd\n");
767 printf("ehci_check_qh_intr: lsqtd==0\n");
772 * If the last TD is still active we need to check whether there
773 * is a an error somewhere in the middle, or whether there was a
774 * short packet (SPD and not ACTIVE).
776 if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
777 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
778 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
779 status = le32toh(sqtd->qtd.qtd_status);
780 /* If there's an active QTD the xfer isn't done. */
781 if (status & EHCI_QTD_ACTIVE)
783 /* Any kind of error makes the xfer done. */
784 if (status & EHCI_QTD_HALTED)
786 /* We want short packets, and it is short: it's done */
787 if (EHCI_QTD_GET_BYTES(status) != 0)
790 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
795 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
796 callout_stop(&ex->xfer.timeout_handle);
797 usb_rem_task(ex->xfer.pipe->device, &ex->abort_task);
802 ehci_check_itd_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
804 ehci_soft_itd_t *itd;
807 if (ex->itdstart == NULL) {
808 printf("ehci_check_itd_intr: not valid itd\n");
815 printf("ehci_check_itd_intr: itdend == 0\n");
821 * Step 1, check no active transfers in last itd, meaning we're finished
823 for (i = 0; i < 8; i++) {
824 if (le32toh(itd->itd.itd_ctl[i]) & EHCI_ITD_ACTIVE)
829 goto done; /* All 8 descriptors inactive, it's done */
833 * Step 2, check for errors in status bits, throughout chain...
836 DPRINTFN(12, ("ehci_check_itd_intr: active ex=%p\n", ex));
838 for (itd = ex->itdstart; itd != ex->itdend; itd = itd->xfer_next) {
839 for (i = 0; i < 8; i++) {
840 if (le32toh(itd->itd.itd_ctl[i]) & (EHCI_ITD_BUF_ERR |
841 EHCI_ITD_BABBLE | EHCI_ITD_ERROR))
844 if (i != 8) { /* Error in one of the itds */
847 } /* itd search loop */
849 DPRINTFN(12, ("ehci_check_itd_intr: ex %p itd %p still active\n", ex,
853 DPRINTFN(12, ("ehci_check_itd_intr: ex=%p done\n", ex));
854 callout_stop(&ex->xfer.timeout_handle);
855 usb_rem_task(ex->xfer.pipe->device, &ex->abort_task);
860 ehci_idone(struct ehci_xfer *ex)
862 usbd_xfer_handle xfer = &ex->xfer;
863 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
864 ehci_soft_qtd_t *sqtd, *lsqtd;
865 u_int32_t status = 0, nstatus = 0;
866 ehci_physaddr_t nextphys, altnextphys;
869 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
876 printf("ehci_idone: ex is done!\n ");
879 printf("ehci_idone: ex=%p is done!\n", ex);
888 if (xfer->status == USBD_CANCELLED ||
889 xfer->status == USBD_TIMEOUT) {
890 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
895 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
897 ehci_dump_sqtds(ex->sqtdstart);
901 * Make sure that the QH overlay qTD does not reference any
902 * of the qTDs we are about to free. This is probably only
903 * necessary if the transfer is marked as HALTED.
905 nextphys = EHCI_LINK_ADDR(le32toh(epipe->sqh->qh.qh_qtd.qtd_next));
907 EHCI_LINK_ADDR(le32toh(epipe->sqh->qh.qh_qtd.qtd_altnext));
908 for (sqtd = ex->sqtdstart; sqtd != ex->sqtdend->nextqtd;
909 sqtd = sqtd->nextqtd) {
910 if (sqtd->physaddr == nextphys) {
911 epipe->sqh->qh.qh_qtd.qtd_next =
912 htole32(ex->sqtdend->nextqtd->physaddr);
913 DPRINTFN(4, ("ehci_idone: updated overlay next ptr\n"));
916 if (sqtd->physaddr == altnextphys) {
918 ("ehci_idone: updated overlay altnext ptr\n"));
919 epipe->sqh->qh.qh_qtd.qtd_altnext =
920 htole32(ex->sqtdend->nextqtd->physaddr);
924 /* The transfer is done, compute actual length and status. */
925 if (UE_GET_XFERTYPE(xfer->pipe->endpoint->edesc->bmAttributes)
928 struct ehci_soft_itd *itd;
929 int i, nframes, len, uframes;
934 switch (xfer->pipe->endpoint->edesc->bInterval) {
936 panic("ehci: isoc xfer suddenly has 0 bInterval, "
952 for (itd = ex->itdstart; itd != NULL; itd = itd->xfer_next) {
953 for (i = 0; i < 8; i += uframes) {
954 /* XXX - driver didn't fill in the frame full
955 * of uframes. This leads to scheduling
956 * inefficiencies, but working around
957 * this doubles complexity of tracking
960 if (nframes >= xfer->nframes)
963 status = le32toh(itd->itd.itd_ctl[i]);
964 len = EHCI_ITD_GET_LEN(status);
965 xfer->frlengths[nframes++] = len;
968 if (nframes >= xfer->nframes)
971 xfer->actlen = actlen;
972 xfer->status = USBD_NORMAL_COMPLETION;
977 /* Continue processing xfers using queue heads */
981 for (sqtd = ex->sqtdstart; sqtd != lsqtd->nextqtd;
982 sqtd =sqtd->nextqtd) {
983 nstatus = le32toh(sqtd->qtd.qtd_status);
984 if (nstatus & EHCI_QTD_ACTIVE)
988 /* halt is ok if descriptor is last, and complete */
989 if (sqtd == lsqtd && EHCI_QTD_GET_BYTES(status) == 0)
990 status &= ~EHCI_QTD_HALTED;
991 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
992 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
995 cerr = EHCI_QTD_GET_CERR(status);
996 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, cerr=%d, "
997 "status=0x%x\n", xfer->length, actlen, cerr, status));
998 xfer->actlen = actlen;
999 if ((status & EHCI_QTD_HALTED) != 0) {
1003 bitmask_snprintf((u_int32_t)status,
1004 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1005 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
1008 ("ehci_idone: error, addr=%d, endpt=0x%02x, "
1010 xfer->pipe->device->address,
1011 xfer->pipe->endpoint->edesc->bEndpointAddress,
1013 if (ehcidebug > 2) {
1014 ehci_dump_sqh(epipe->sqh);
1015 ehci_dump_sqtds(ex->sqtdstart);
1018 if ((status & EHCI_QTD_BABBLE) == 0 && cerr > 0)
1019 xfer->status = USBD_STALLED;
1021 xfer->status = USBD_IOERROR; /* more info XXX */
1023 xfer->status = USBD_NORMAL_COMPLETION;
1026 /* XXX transfer_complete memcpys out transfer data (for in endpoints)
1027 * during this call, before methods->done is called: dma sync required
1030 usb_transfer_complete(xfer);
1031 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
1035 * Wait here until controller claims to have an interrupt.
1036 * Then call ehci_intr and return. Use timeout to avoid waiting
1040 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
1042 int timo = xfer->timeout;
1046 xfer->status = USBD_IN_PROGRESS;
1047 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1048 usb_delay_ms(&sc->sc_bus, 1);
1051 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
1053 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
1060 if (xfer->status != USBD_IN_PROGRESS)
1066 DPRINTF(("ehci_waitintr: timeout\n"));
1067 xfer->status = USBD_TIMEOUT;
1068 usb_transfer_complete(xfer);
1069 /* XXX should free TD */
1073 ehci_poll(struct usbd_bus *bus)
1075 ehci_softc_t *sc = (ehci_softc_t *)bus;
1079 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1081 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
1086 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
1091 ehci_detach(struct ehci_softc *sc, int flags)
1097 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1098 (void) ehci_hcreset(sc);
1099 callout_stop(&sc->sc_tmo_intrlist);
1101 #if defined(__NetBSD__) || defined(__OpenBSD__)
1102 if (sc->sc_powerhook != NULL)
1103 powerhook_disestablish(sc->sc_powerhook);
1104 if (sc->sc_shutdownhook != NULL)
1105 shutdownhook_disestablish(sc->sc_shutdownhook);
1107 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
1109 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
1110 /* XXX free other data structures XXX */
1116 * Handle suspend/resume.
1118 * We need to switch to polling mode here, because this routine is
1119 * called from an interrupt context. This is all right since we
1120 * are almost suspended anyway.
1123 ehci_power(int why, void *v)
1125 ehci_softc_t *sc = v;
1130 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
1139 sc->sc_bus.use_polling++;
1141 for (i = 1; i <= sc->sc_noport; i++) {
1142 cmd = EOREAD4(sc, EHCI_PORTSC(i));
1143 if ((cmd & EHCI_PS_PO) == 0 &&
1144 (cmd & EHCI_PS_PE) == EHCI_PS_PE)
1145 EOWRITE4(sc, EHCI_PORTSC(i),
1146 cmd | EHCI_PS_SUSP);
1149 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
1151 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
1152 EOWRITE4(sc, EHCI_USBCMD, cmd);
1154 for (i = 0; i < 100; i++) {
1155 hcr = EOREAD4(sc, EHCI_USBSTS) &
1156 (EHCI_STS_ASS | EHCI_STS_PSS);
1160 usb_delay_ms(&sc->sc_bus, 1);
1163 printf("%s: reset timeout\n",
1164 device_get_nameunit(sc->sc_bus.bdev));
1167 cmd &= ~EHCI_CMD_RS;
1168 EOWRITE4(sc, EHCI_USBCMD, cmd);
1170 for (i = 0; i < 100; i++) {
1171 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1172 if (hcr == EHCI_STS_HCH)
1175 usb_delay_ms(&sc->sc_bus, 1);
1177 if (hcr != EHCI_STS_HCH) {
1178 printf("%s: config timeout\n",
1179 device_get_nameunit(sc->sc_bus.bdev));
1182 sc->sc_bus.use_polling--;
1186 sc->sc_bus.use_polling++;
1188 /* restore things in case the bios sucks */
1189 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
1190 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
1191 EOWRITE4(sc, EHCI_ASYNCLISTADDR,
1192 sc->sc_async_head->physaddr | EHCI_LINK_QH);
1193 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1196 for (i = 1; i <= sc->sc_noport; i++) {
1197 cmd = EOREAD4(sc, EHCI_PORTSC(i));
1198 if ((cmd & EHCI_PS_PO) == 0 &&
1199 (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP) {
1200 EOWRITE4(sc, EHCI_PORTSC(i),
1207 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1209 for (i = 1; i <= sc->sc_noport; i++) {
1210 cmd = EOREAD4(sc, EHCI_PORTSC(i));
1211 if ((cmd & EHCI_PS_PO) == 0 &&
1212 (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)
1213 EOWRITE4(sc, EHCI_PORTSC(i),
1214 cmd & ~EHCI_PS_FPR);
1218 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
1220 for (i = 0; i < 100; i++) {
1221 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1222 if (hcr != EHCI_STS_HCH)
1225 usb_delay_ms(&sc->sc_bus, 1);
1227 if (hcr == EHCI_STS_HCH) {
1228 printf("%s: config timeout\n",
1229 device_get_nameunit(sc->sc_bus.bdev));
1232 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1234 sc->sc_bus.use_polling--;
1236 case PWR_SOFTSUSPEND:
1237 case PWR_SOFTSTANDBY:
1238 case PWR_SOFTRESUME:
1244 DPRINTF(("ehci_power: sc=%p\n", sc));
1251 * Shut down the controller when the system is going down.
1254 ehci_shutdown(void *v)
1256 ehci_softc_t *sc = v;
1258 DPRINTF(("ehci_shutdown: stopping the HC\n"));
1259 (void) ehci_hcreset(sc);
1263 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
1267 err = usb_allocmem(bus, size, 0, dma);
1270 printf("ehci_allocm: usb_allocmem()=%d\n", err);
1276 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
1278 usb_freemem(bus, dma);
1282 ehci_allocx(struct usbd_bus *bus)
1284 struct ehci_softc *sc = (struct ehci_softc *)bus;
1285 usbd_xfer_handle xfer;
1287 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
1289 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
1291 if (xfer->busy_free != XFER_FREE) {
1292 printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer,
1297 xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_NOWAIT);
1300 memset(xfer, 0, sizeof(struct ehci_xfer));
1301 usb_init_task(&EXFER(xfer)->abort_task, ehci_timeout_task,
1303 EXFER(xfer)->ehci_xfer_flags = 0;
1305 EXFER(xfer)->isdone = 1;
1306 xfer->busy_free = XFER_BUSY;
1313 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1315 struct ehci_softc *sc = (struct ehci_softc *)bus;
1318 if (xfer->busy_free != XFER_BUSY) {
1319 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
1323 xfer->busy_free = XFER_FREE;
1324 if (!EXFER(xfer)->isdone) {
1325 printf("ehci_freex: !isdone\n");
1329 STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
1333 ehci_device_clear_toggle(usbd_pipe_handle pipe)
1335 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1337 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
1338 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
1341 usbd_dump_pipe(pipe);
1343 KASSERT((epipe->sqh->qh.qh_qtd.qtd_status &
1344 htole32(EHCI_QTD_ACTIVE)) == 0,
1345 ("ehci_device_clear_toggle: queue active"));
1346 epipe->sqh->qh.qh_qtd.qtd_status &= htole32(~EHCI_QTD_TOGGLE_MASK);
1350 ehci_noop(usbd_pipe_handle pipe)
1356 ehci_dump_regs(ehci_softc_t *sc)
1359 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1360 EOREAD4(sc, EHCI_USBCMD),
1361 EOREAD4(sc, EHCI_USBSTS),
1362 EOREAD4(sc, EHCI_USBINTR));
1363 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1364 EOREAD4(sc, EHCI_FRINDEX),
1365 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1366 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1367 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1368 for (i = 1; i <= sc->sc_noport; i++)
1369 printf("port %d status=0x%08x\n", i,
1370 EOREAD4(sc, EHCI_PORTSC(i)));
1374 * Unused function - this is meant to be called from a kernel
1380 ehci_dump_regs(theehci);
1384 ehci_dump_link(ehci_link_t link, int type)
1386 link = le32toh(link);
1387 printf("0x%08x", link);
1388 if (link & EHCI_LINK_TERMINATE)
1393 switch (EHCI_LINK_TYPE(link)) {
1394 case EHCI_LINK_ITD: printf("ITD"); break;
1395 case EHCI_LINK_QH: printf("QH"); break;
1396 case EHCI_LINK_SITD: printf("SITD"); break;
1397 case EHCI_LINK_FSTN: printf("FSTN"); break;
1405 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1411 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1412 ehci_dump_sqtd(sqtd);
1413 stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE);
1416 printf("dump aborted, too many TDs\n");
1420 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1422 printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr);
1423 ehci_dump_qtd(&sqtd->qtd);
1427 ehci_dump_qtd(ehci_qtd_t *qtd)
1432 printf(" next="); ehci_dump_link(qtd->qtd_next, 0);
1433 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
1435 s = le32toh(qtd->qtd_status);
1436 bitmask_snprintf(EHCI_QTD_GET_STATUS(s),
1437 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1438 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
1439 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
1440 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
1441 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
1442 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
1443 EHCI_QTD_GET_PID(s), sbuf);
1444 for (s = 0; s < 5; s++)
1445 printf(" buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
1449 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1451 ehci_qh_t *qh = &sqh->qh;
1452 u_int32_t endp, endphub;
1454 printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr);
1455 printf(" sqtd=%p inactivesqtd=%p\n", sqh->sqtd, sqh->inactivesqtd);
1456 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
1457 endp = le32toh(qh->qh_endp);
1458 printf(" endp=0x%08x\n", endp);
1459 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
1460 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1461 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
1462 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
1463 printf(" mpl=0x%x ctl=%d nrl=%d\n",
1464 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
1465 EHCI_QH_GET_NRL(endp));
1466 endphub = le32toh(qh->qh_endphub);
1467 printf(" endphub=0x%08x\n", endphub);
1468 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
1469 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
1470 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1471 EHCI_QH_GET_MULT(endphub));
1472 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
1473 printf("Overlay qTD:\n");
1474 ehci_dump_qtd(&qh->qh_qtd);
1479 ehci_dump_itd(struct ehci_soft_itd *itd)
1481 ehci_isoc_trans_t t;
1482 ehci_isoc_bufr_ptr_t b, b2, b3;
1485 printf("ITD: next phys=%X\n", itd->itd.itd_next);
1487 for (i = 0; i < 8;i++) {
1488 t = le32toh(itd->itd.itd_ctl[i]);
1489 printf("ITDctl %d: stat=%X len=%X ioc=%X pg=%X offs=%X\n", i,
1490 EHCI_ITD_GET_STATUS(t), EHCI_ITD_GET_LEN(t),
1491 EHCI_ITD_GET_IOC(t), EHCI_ITD_GET_PG(t),
1492 EHCI_ITD_GET_OFFS(t));
1494 printf("ITDbufr: ");
1495 for (i = 0; i < 7; i++)
1496 printf("%X,", EHCI_ITD_GET_BPTR(le32toh(itd->itd.itd_bufr[i])));
1498 b = le32toh(itd->itd.itd_bufr[0]);
1499 b2 = le32toh(itd->itd.itd_bufr[1]);
1500 b3 = le32toh(itd->itd.itd_bufr[2]);
1501 printf("\nep=%X daddr=%X dir=%d maxpkt=%X multi=%X\n",
1502 EHCI_ITD_GET_EP(b), EHCI_ITD_GET_DADDR(b), EHCI_ITD_GET_DIR(b2),
1503 EHCI_ITD_GET_MAXPKT(b2), EHCI_ITD_GET_MULTI(b3));
1507 ehci_dump_sitd(struct ehci_soft_itd *itd)
1509 printf("SITD %p next=%p prev=%p xfernext=%p physaddr=%X slot=%d\n",
1510 itd, itd->u.frame_list.next, itd->u.frame_list.prev,
1511 itd->xfer_next, itd->physaddr, itd->slot);
1517 ehci_dump_exfer(struct ehci_xfer *ex)
1519 printf("ehci_dump_exfer: ex=%p sqtdstart=%p end=%p itdstart=%p "
1520 "end=%p isdone=%d\n", ex, ex->sqtdstart, ex->sqtdend, ex->itdstart,
1521 ex->itdend, ex->isdone);
1527 ehci_open(usbd_pipe_handle pipe)
1529 usbd_device_handle dev = pipe->device;
1530 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
1531 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1532 u_int8_t addr = dev->address;
1533 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1534 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1535 ehci_soft_qh_t *sqh;
1538 int ival, speed, naks;
1539 int hshubaddr, hshubport;
1541 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1542 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1544 if (dev->myhsport) {
1545 hshubaddr = dev->myhsport->parent->address;
1546 hshubport = dev->myhsport->portno;
1553 return (USBD_IOERROR);
1555 if (addr == sc->sc_addr) {
1556 switch (ed->bEndpointAddress) {
1557 case USB_CONTROL_ENDPOINT:
1558 pipe->methods = &ehci_root_ctrl_methods;
1560 case UE_DIR_IN | EHCI_INTR_ENDPT:
1561 pipe->methods = &ehci_root_intr_methods;
1564 DPRINTF(("ehci_open: bad bEndpointAddress 0x%02x\n",
1565 ed->bEndpointAddress));
1566 return (USBD_INVAL);
1568 return (USBD_NORMAL_COMPLETION);
1571 /* XXX All this stuff is only valid for async. */
1572 switch (dev->speed) {
1573 case USB_SPEED_LOW: speed = EHCI_QH_SPEED_LOW; break;
1574 case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
1575 case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
1576 default: panic("ehci_open: bad device speed %d", dev->speed);
1578 if (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_ISOCHRONOUS) {
1579 printf("%s: *** Error: opening low/full speed isoc device on"
1580 "ehci, this does not work yet. Feel free to implement\n",
1581 device_get_nameunit(sc->sc_bus.bdev));
1582 DPRINTFN(1,("ehci_open: hshubaddr=%d hshubport=%d\n",
1583 hshubaddr, hshubport));
1588 /* Allocate sqh for everything, save isoc xfers */
1589 if (xfertype != UE_ISOCHRONOUS) {
1590 sqh = ehci_alloc_sqh(sc);
1593 /* qh_link filled when the QH is added */
1594 sqh->qh.qh_endp = htole32(
1595 EHCI_QH_SET_ADDR(addr) |
1596 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
1597 EHCI_QH_SET_EPS(speed) |
1598 (xfertype == UE_CONTROL ? EHCI_QH_DTC : 0) |
1599 EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
1600 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
1602 EHCI_QH_SET_NRL(naks)
1604 sqh->qh.qh_endphub = htole32(
1605 EHCI_QH_SET_MULT(1) |
1606 EHCI_QH_SET_HUBA(hshubaddr) |
1607 EHCI_QH_SET_PORT(hshubport) |
1608 EHCI_QH_SET_CMASK(0x1c) |
1609 EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x01 : 0)
1611 sqh->qh.qh_curqtd = EHCI_NULL;
1612 /* The overlay qTD was already set up by ehci_alloc_sqh(). */
1613 sqh->qh.qh_qtd.qtd_status =
1614 htole32(EHCI_QTD_SET_TOGGLE(pipe->endpoint->savedtoggle));
1622 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
1623 0, &epipe->u.ctl.reqdma);
1626 printf("ehci_open: usb_allocmem()=%d\n", err);
1630 pipe->methods = &ehci_device_ctrl_methods;
1632 ehci_add_qh(sqh, sc->sc_async_head);
1636 pipe->methods = &ehci_device_bulk_methods;
1638 ehci_add_qh(sqh, sc->sc_async_head);
1642 pipe->methods = &ehci_device_intr_methods;
1643 ival = pipe->interval;
1644 if (ival == USBD_DEFAULT_INTERVAL)
1645 ival = ed->bInterval;
1646 return (ehci_device_setintr(sc, sqh, ival));
1647 case UE_ISOCHRONOUS:
1648 pipe->methods = &ehci_device_isoc_methods;
1649 if (ed->bInterval == 0 || ed->bInterval > 16) {
1650 printf("ehci: opening pipe with invalid bInterval\n");
1654 if (UGETW(ed->wMaxPacketSize) == 0) {
1655 printf("ehci: zero length endpoint open request\n");
1659 epipe->u.isoc.next_frame = 0;
1660 epipe->u.isoc.cur_xfers = 0;
1663 DPRINTF(("ehci: bad xfer type %d\n", xfertype));
1664 return (USBD_INVAL);
1666 return (USBD_NORMAL_COMPLETION);
1670 ehci_free_sqh(sc, sqh);
1673 return (USBD_NOMEM);
1677 * Add an ED to the schedule. Called at splusb().
1678 * If in the async schedule, it will always have a next.
1679 * If in the intr schedule it may not.
1682 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1686 sqh->next = head->next;
1688 sqh->qh.qh_link = head->qh.qh_link;
1691 sqh->next->prev = sqh;
1692 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
1695 if (ehcidebug > 5) {
1696 printf("ehci_add_qh:\n");
1703 * Remove an ED from the schedule. Called at splusb().
1704 * Will always have a 'next' if it's in the async list as it's circular.
1707 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1711 sqh->prev->qh.qh_link = sqh->qh.qh_link;
1712 sqh->prev->next = sqh->next;
1714 sqh->next->prev = sqh->prev;
1718 /* Restart a QH following the addition of a qTD. */
1720 ehci_activate_qh(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
1722 KASSERT((sqtd->qtd.qtd_status & htole32(EHCI_QTD_ACTIVE)) == 0,
1723 ("ehci_activate_qh: already active"));
1726 * When a QH is idle, the overlay qTD should be marked as not
1727 * halted and not active. This causes the host controller to
1728 * retrieve the real qTD on each pass (rather than just examinig
1729 * the overlay), so it will notice when we activate the qTD.
1731 if (sqtd == sqh->sqtd) {
1732 /* Check that the hardware is in the state we expect. */
1733 if (EHCI_LINK_ADDR(le32toh(sqh->qh.qh_qtd.qtd_next)) !=
1736 printf("ehci_activate_qh: unexpected next ptr\n");
1738 ehci_dump_sqtds(sqh->sqtd);
1740 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
1741 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
1743 /* Ensure the flags are correct. */
1744 sqh->qh.qh_qtd.qtd_status &= htole32(EHCI_QTD_PINGSTATE |
1745 EHCI_QTD_TOGGLE_MASK);
1748 /* Now activate the qTD. */
1749 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_ACTIVE);
1753 * Ensure that the HC has released all references to the QH. We do this
1754 * by asking for a Async Advance Doorbell interrupt and then we wait for
1756 * To make this easier we first obtain exclusive use of the doorbell.
1759 ehci_sync_hc(ehci_softc_t *sc)
1764 DPRINTFN(2,("ehci_sync_hc: dying\n"));
1767 DPRINTFN(2,("ehci_sync_hc: enter\n"));
1769 lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE, NULL);
1771 /* ask for doorbell */
1772 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
1773 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1774 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1775 error = tsleep(&sc->sc_async_head, PZERO, "ehcidi", hz); /* bell wait */
1776 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1777 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1779 /* release doorbell */
1780 lockmgr(&sc->sc_doorbell_lock, LK_RELEASE, NULL);
1783 printf("ehci_sync_hc: tsleep() = %d\n", error);
1785 DPRINTFN(2,("ehci_sync_hc: exit\n"));
1790 ehci_rem_free_itd_chain(ehci_softc_t *sc, struct ehci_xfer *exfer)
1792 struct ehci_soft_itd *itd, *prev;
1796 if (exfer->itdstart == NULL || exfer->itdend == NULL)
1797 panic("ehci isoc xfer being freed, but with no itd chain\n");
1799 for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) {
1800 prev = itd->u.frame_list.prev;
1801 /* Unlink itd from hardware chain, or frame array */
1802 if (prev == NULL) { /* We're at the table head */
1803 sc->sc_softitds[itd->slot] = itd->u.frame_list.next;
1804 sc->sc_flist[itd->slot] = itd->itd.itd_next;
1806 if (itd->u.frame_list.next != NULL)
1807 itd->u.frame_list.next->u.frame_list.prev =
1810 /* XXX this part is untested... */
1811 prev->itd.itd_next = itd->itd.itd_next;
1812 prev->u.frame_list.next = itd->u.frame_list.next;
1813 if (itd->u.frame_list.next != NULL)
1814 itd->u.frame_list.next->u.frame_list.prev =
1820 for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) {
1822 ehci_free_itd(sc, prev);
1826 ehci_free_itd(sc, prev);
1827 exfer->itdstart = NULL;
1828 exfer->itdend = NULL;
1834 * Data structures and routines to emulate the root hub.
1836 static usb_device_descriptor_t ehci_devd = {
1837 USB_DEVICE_DESCRIPTOR_SIZE,
1838 UDESC_DEVICE, /* type */
1839 {0x00, 0x02}, /* USB version */
1840 UDCLASS_HUB, /* class */
1841 UDSUBCLASS_HUB, /* subclass */
1842 UDPROTO_HSHUBSTT, /* protocol */
1843 64, /* max packet */
1844 {0},{0},{0x00,0x01}, /* device id */
1845 1,2,0, /* string indicies */
1846 1 /* # of configurations */
1849 static usb_device_qualifier_t ehci_odevd = {
1850 USB_DEVICE_DESCRIPTOR_SIZE,
1851 UDESC_DEVICE_QUALIFIER, /* type */
1852 {0x00, 0x02}, /* USB version */
1853 UDCLASS_HUB, /* class */
1854 UDSUBCLASS_HUB, /* subclass */
1855 UDPROTO_FSHUB, /* protocol */
1856 64, /* max packet */
1857 1, /* # of configurations */
1861 static usb_config_descriptor_t ehci_confd = {
1862 USB_CONFIG_DESCRIPTOR_SIZE,
1864 {USB_CONFIG_DESCRIPTOR_SIZE +
1865 USB_INTERFACE_DESCRIPTOR_SIZE +
1866 USB_ENDPOINT_DESCRIPTOR_SIZE},
1874 static usb_interface_descriptor_t ehci_ifcd = {
1875 USB_INTERFACE_DESCRIPTOR_SIZE,
1886 static usb_endpoint_descriptor_t ehci_endpd = {
1887 USB_ENDPOINT_DESCRIPTOR_SIZE,
1889 UE_DIR_IN | EHCI_INTR_ENDPT,
1891 {8, 0}, /* max packet */
1895 static usb_hub_descriptor_t ehci_hubd = {
1896 USB_HUB_DESCRIPTOR_SIZE,
1906 ehci_str(usb_string_descriptor_t *p, int l, char *s)
1912 p->bLength = 2 * strlen(s) + 2;
1915 p->bDescriptorType = UDESC_STRING;
1917 for (i = 0; s[i] && l > 1; i++, l -= 2)
1918 USETW2(p->bString[i], 0, s[i]);
1923 * Simulate a hardware hub by handling all the necessary requests.
1926 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
1930 /* Insert last in queue. */
1931 err = usb_insert_transfer(xfer);
1935 /* Pipe isn't running, start first */
1936 return (ehci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
1940 ehci_root_ctrl_start(usbd_xfer_handle xfer)
1942 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
1943 usb_device_request_t *req;
1946 int s, len, value, index, l, totlen = 0;
1947 usb_port_status_t ps;
1948 usb_hub_descriptor_t hubd;
1953 return (USBD_IOERROR);
1956 if (!(xfer->rqflags & URQ_REQUEST))
1958 return (USBD_INVAL);
1960 req = &xfer->request;
1962 DPRINTFN(4,("ehci_root_ctrl_start: type=0x%02x request=%02x\n",
1963 req->bmRequestType, req->bRequest));
1965 len = UGETW(req->wLength);
1966 value = UGETW(req->wValue);
1967 index = UGETW(req->wIndex);
1972 #define C(x,y) ((x) | ((y) << 8))
1973 switch(C(req->bRequest, req->bmRequestType)) {
1974 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1975 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1976 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1978 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1979 * for the integrated root hub.
1982 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1984 *(u_int8_t *)buf = sc->sc_conf;
1988 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1989 DPRINTFN(8,("ehci_root_ctrl_start: wValue=0x%04x\n", value));
1990 switch(value >> 8) {
1992 if ((value & 0xff) != 0) {
1996 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1997 USETW(ehci_devd.idVendor, sc->sc_id_vendor);
1998 memcpy(buf, &ehci_devd, l);
2001 * We can't really operate at another speed, but the spec says
2002 * we need this descriptor.
2004 case UDESC_DEVICE_QUALIFIER:
2005 if ((value & 0xff) != 0) {
2009 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2010 memcpy(buf, &ehci_odevd, l);
2013 * We can't really operate at another speed, but the spec says
2014 * we need this descriptor.
2016 case UDESC_OTHER_SPEED_CONFIGURATION:
2018 if ((value & 0xff) != 0) {
2022 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2023 memcpy(buf, &ehci_confd, l);
2024 ((usb_config_descriptor_t *)buf)->bDescriptorType =
2026 buf = (char *)buf + l;
2028 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2030 memcpy(buf, &ehci_ifcd, l);
2031 buf = (char *)buf + l;
2033 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2035 memcpy(buf, &ehci_endpd, l);
2040 *(u_int8_t *)buf = 0;
2042 switch (value & 0xff) {
2043 case 0: /* Language table */
2044 totlen = ehci_str(buf, len, "\001");
2046 case 1: /* Vendor */
2047 totlen = ehci_str(buf, len, sc->sc_vendor);
2049 case 2: /* Product */
2050 totlen = ehci_str(buf, len, "EHCI root hub");
2059 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2061 *(u_int8_t *)buf = 0;
2065 case C(UR_GET_STATUS, UT_READ_DEVICE):
2067 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2071 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2072 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2074 USETW(((usb_status_t *)buf)->wStatus, 0);
2078 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2079 if (value >= USB_MAX_DEVICES) {
2083 sc->sc_addr = value;
2085 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2086 if (value != 0 && value != 1) {
2090 sc->sc_conf = value;
2092 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2094 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2095 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2096 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2099 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2101 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2104 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2106 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2107 DPRINTFN(8, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
2108 "port=%d feature=%d\n",
2110 if (index < 1 || index > sc->sc_noport) {
2114 port = EHCI_PORTSC(index);
2115 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
2117 case UHF_PORT_ENABLE:
2118 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
2120 case UHF_PORT_SUSPEND:
2121 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
2123 case UHF_PORT_POWER:
2124 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
2127 DPRINTFN(2,("ehci_root_ctrl_start: clear port test "
2130 case UHF_PORT_INDICATOR:
2131 DPRINTFN(2,("ehci_root_ctrl_start: clear port ind "
2133 EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
2135 case UHF_C_PORT_CONNECTION:
2136 EOWRITE4(sc, port, v | EHCI_PS_CSC);
2138 case UHF_C_PORT_ENABLE:
2139 EOWRITE4(sc, port, v | EHCI_PS_PEC);
2141 case UHF_C_PORT_SUSPEND:
2144 case UHF_C_PORT_OVER_CURRENT:
2145 EOWRITE4(sc, port, v | EHCI_PS_OCC);
2147 case UHF_C_PORT_RESET:
2155 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2156 if ((value & 0xff) != 0) {
2161 hubd.bNbrPorts = sc->sc_noport;
2162 v = EOREAD4(sc, EHCI_HCSPARAMS);
2163 USETW(hubd.wHubCharacteristics,
2164 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
2165 EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
2166 ? UHD_PORT_IND : 0);
2167 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
2168 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2169 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
2170 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2171 l = min(len, hubd.bDescLength);
2173 memcpy(buf, &hubd, l);
2175 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2180 memset(buf, 0, len); /* ? XXX */
2183 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2184 DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n",
2186 if (index < 1 || index > sc->sc_noport) {
2194 v = EOREAD4(sc, EHCI_PORTSC(index));
2195 DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n",
2198 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
2199 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
2200 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
2201 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
2202 if (v & EHCI_PS_PR) i |= UPS_RESET;
2203 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
2204 USETW(ps.wPortStatus, i);
2206 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
2207 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
2208 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
2209 if (sc->sc_isreset) i |= UPS_C_PORT_RESET;
2210 USETW(ps.wPortChange, i);
2211 l = min(len, sizeof ps);
2212 memcpy(buf, &ps, l);
2215 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2218 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2220 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2221 if (index < 1 || index > sc->sc_noport) {
2225 port = EHCI_PORTSC(index);
2226 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
2228 case UHF_PORT_ENABLE:
2229 EOWRITE4(sc, port, v | EHCI_PS_PE);
2231 case UHF_PORT_SUSPEND:
2232 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
2234 case UHF_PORT_RESET:
2235 DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n",
2237 if (EHCI_PS_IS_LOWSPEED(v)) {
2238 /* Low speed device, give up ownership. */
2239 ehci_disown(sc, index, 1);
2242 /* Start reset sequence. */
2243 v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
2244 EOWRITE4(sc, port, v | EHCI_PS_PR);
2245 /* Wait for reset to complete. */
2246 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2251 /* Terminate reset sequence. */
2252 EOWRITE4(sc, port, v);
2253 /* Wait for HC to complete reset. */
2254 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE);
2259 v = EOREAD4(sc, port);
2260 DPRINTF(("ehci after reset, status=0x%08x\n", v));
2261 if (v & EHCI_PS_PR) {
2262 printf("%s: port reset timeout\n",
2263 device_get_nameunit(sc->sc_bus.bdev));
2264 return (USBD_TIMEOUT);
2266 if (!(v & EHCI_PS_PE)) {
2267 /* Not a high speed device, give up ownership.*/
2268 ehci_disown(sc, index, 0);
2272 DPRINTF(("ehci port %d reset, status = 0x%08x\n",
2275 case UHF_PORT_POWER:
2276 DPRINTFN(2,("ehci_root_ctrl_start: set port power "
2278 EOWRITE4(sc, port, v | EHCI_PS_PP);
2281 DPRINTFN(2,("ehci_root_ctrl_start: set port test "
2284 case UHF_PORT_INDICATOR:
2285 DPRINTFN(2,("ehci_root_ctrl_start: set port ind "
2287 EOWRITE4(sc, port, v | EHCI_PS_PIC);
2294 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2295 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2296 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2297 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2303 xfer->actlen = totlen;
2304 err = USBD_NORMAL_COMPLETION;
2308 hacksync(xfer); /* XXX to compensate for usb_transfer_complete */
2309 usb_transfer_complete(xfer);
2311 return (USBD_IN_PROGRESS);
2315 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
2320 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
2322 if (sc->sc_npcomp != 0) {
2323 int i = (index-1) / sc->sc_npcomp;
2324 if (i >= sc->sc_ncomp)
2325 printf("%s: strange port\n",
2326 device_get_nameunit(sc->sc_bus.bdev));
2328 printf("%s: handing over %s speed device on "
2330 device_get_nameunit(sc->sc_bus.bdev),
2331 lowspeed ? "low" : "full",
2332 index, device_get_nameunit(sc->sc_comps[i]->bdev));
2334 printf("%s: npcomp == 0\n", device_get_nameunit(sc->sc_bus.bdev));
2337 port = EHCI_PORTSC(index);
2338 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
2339 EOWRITE4(sc, port, v | EHCI_PS_PO);
2342 /* Abort a root control request. */
2344 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
2346 /* Nothing to do, all transfers are synchronous. */
2349 /* Close the root pipe. */
2351 ehci_root_ctrl_close(usbd_pipe_handle pipe)
2353 DPRINTF(("ehci_root_ctrl_close\n"));
2354 /* Nothing to do. */
2358 ehci_root_intr_done(usbd_xfer_handle xfer)
2363 ehci_root_intr_transfer(usbd_xfer_handle xfer)
2367 /* Insert last in queue. */
2368 err = usb_insert_transfer(xfer);
2372 /* Pipe isn't running, start first */
2373 return (ehci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
2377 ehci_root_intr_start(usbd_xfer_handle xfer)
2379 usbd_pipe_handle pipe = xfer->pipe;
2380 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2383 return (USBD_IOERROR);
2385 sc->sc_intrxfer = xfer;
2387 return (USBD_IN_PROGRESS);
2390 /* Abort a root interrupt request. */
2392 ehci_root_intr_abort(usbd_xfer_handle xfer)
2396 if (xfer->pipe->intrxfer == xfer) {
2397 DPRINTF(("ehci_root_intr_abort: remove\n"));
2398 xfer->pipe->intrxfer = NULL;
2400 xfer->status = USBD_CANCELLED;
2402 usb_transfer_complete(xfer);
2406 /* Close the root pipe. */
2408 ehci_root_intr_close(usbd_pipe_handle pipe)
2410 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2412 DPRINTF(("ehci_root_intr_close\n"));
2414 sc->sc_intrxfer = NULL;
2418 ehci_root_ctrl_done(usbd_xfer_handle xfer)
2422 /************************/
2425 ehci_alloc_sqh(ehci_softc_t *sc)
2427 ehci_soft_qh_t *sqh;
2428 ehci_soft_qtd_t *sqtd;
2433 if (sc->sc_freeqhs == NULL) {
2434 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
2435 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
2436 EHCI_PAGE_SIZE, &dma);
2439 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2443 for(i = 0; i < EHCI_SQH_CHUNK; i++) {
2444 offs = i * EHCI_SQH_SIZE;
2445 sqh = KERNADDR(&dma, offs);
2446 sqh->physaddr = DMAADDR(&dma, offs);
2447 sqh->next = sc->sc_freeqhs;
2448 sc->sc_freeqhs = sqh;
2451 /* Allocate the initial inactive sqtd. */
2452 sqtd = ehci_alloc_sqtd(sc);
2455 sqtd->qtd.qtd_status = htole32(0);
2456 sqtd->qtd.qtd_next = EHCI_NULL;
2457 sqtd->qtd.qtd_altnext = EHCI_NULL;
2459 sqh = sc->sc_freeqhs;
2460 sc->sc_freeqhs = sqh->next;
2462 /* The overlay QTD should begin zeroed. */
2463 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
2464 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
2465 sqh->qh.qh_qtd.qtd_status = 0;
2466 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
2467 sqh->qh.qh_qtd.qtd_buffer[i] = 0;
2468 sqh->qh.qh_qtd.qtd_buffer_hi[i] = 0;
2473 sqh->inactivesqtd = sqtd;
2478 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2480 ehci_free_sqtd(sc, sqh->inactivesqtd);
2481 sqh->next = sc->sc_freeqhs;
2482 sc->sc_freeqhs = sqh;
2486 ehci_alloc_sqtd(ehci_softc_t *sc)
2488 ehci_soft_qtd_t *sqtd;
2494 if (sc->sc_freeqtds == NULL) {
2495 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
2496 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
2497 EHCI_PAGE_SIZE, &dma);
2500 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
2505 for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
2506 offs = i * EHCI_SQTD_SIZE;
2507 sqtd = KERNADDR(&dma, offs);
2508 sqtd->physaddr = DMAADDR(&dma, offs);
2509 sqtd->nextqtd = sc->sc_freeqtds;
2510 sc->sc_freeqtds = sqtd;
2516 sqtd = sc->sc_freeqtds;
2517 sc->sc_freeqtds = sqtd->nextqtd;
2518 sqtd->qtd.qtd_next = EHCI_NULL;
2519 sqtd->qtd.qtd_altnext = EHCI_NULL;
2520 sqtd->qtd.qtd_status = 0;
2521 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
2522 sqtd->qtd.qtd_buffer[i] = 0;
2523 sqtd->qtd.qtd_buffer_hi[i] = 0;
2525 sqtd->nextqtd = NULL;
2533 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2538 sqtd->nextqtd = sc->sc_freeqtds;
2539 sc->sc_freeqtds = sqtd;
2544 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
2545 int alen, int rd, usbd_xfer_handle xfer, ehci_soft_qtd_t *start,
2546 ehci_soft_qtd_t *newinactive, ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
2548 ehci_soft_qtd_t *next, *cur;
2549 ehci_physaddr_t dataphys, nextphys;
2550 u_int32_t qtdstatus;
2551 int adj, len, curlen, mps, offset, pagelen, seg, segoff;
2552 int i, iscontrol, forceshort;
2553 struct usb_dma_mapping *dma = &xfer->dmamap;
2555 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
2559 iscontrol = (epipe->pipe.endpoint->edesc->bmAttributes & UE_XFERTYPE) ==
2561 qtdstatus = EHCI_QTD_ACTIVE |
2562 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2563 EHCI_QTD_SET_CERR(3)
2565 /* BYTES set below */
2567 mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
2568 forceshort = ((xfer->flags & USBD_FORCE_SHORT_XFER) || len == 0) &&
2571 * The control transfer data stage always starts with a toggle of 1.
2572 * For other transfers we let the hardware track the toggle state.
2575 qtdstatus |= EHCI_QTD_SET_TOGGLE(1);
2577 if (start != NULL) {
2579 * If we are given a starting qTD, assume it is linked into
2580 * an active QH so be careful not to mark it active.
2584 qtdstatus &= ~EHCI_QTD_ACTIVE;
2586 cur = ehci_alloc_sqtd(sc);
2596 /* The EHCI hardware can handle at most 5 pages. */
2597 for (i = 0; i < EHCI_QTD_NBUFFERS && curlen < len; i++) {
2598 KASSERT(seg < dma->nsegs,
2599 ("ehci_alloc_sqtd_chain: overrun"));
2600 dataphys = dma->segs[seg].ds_addr + segoff;
2601 pagelen = dma->segs[seg].ds_len - segoff;
2602 if (pagelen > len - curlen)
2603 pagelen = len - curlen;
2604 if (pagelen > EHCI_PAGE_SIZE -
2605 EHCI_PAGE_OFFSET(dataphys))
2606 pagelen = EHCI_PAGE_SIZE -
2607 EHCI_PAGE_OFFSET(dataphys);
2609 if (segoff >= dma->segs[seg].ds_len) {
2610 KASSERT(segoff == dma->segs[seg].ds_len,
2611 ("ehci_alloc_sqtd_chain: overlap"));
2616 cur->qtd.qtd_buffer[i] = htole32(dataphys);
2617 cur->qtd.qtd_buffer_hi[i] = 0;
2621 * Must stop if there is any gap before or after
2622 * the page boundary.
2624 if (EHCI_PAGE_OFFSET(dataphys + pagelen) != 0)
2626 if (seg < dma->nsegs && EHCI_PAGE_OFFSET(segoff +
2627 dma->segs[seg].ds_addr) != 0)
2630 /* Adjust down to a multiple of mps if not at the end. */
2631 if (curlen < len && curlen % mps != 0) {
2635 ("ehci_alloc_sqtd_chain: need to copy"));
2639 segoff += dma->segs[seg].ds_len;
2641 KASSERT(seg >= 0 && segoff >= 0,
2642 ("ehci_alloc_sqtd_chain: adjust to mps"));
2647 if (len != 0 || forceshort) {
2648 next = ehci_alloc_sqtd(sc);
2651 nextphys = htole32(next->physaddr);
2654 nextphys = EHCI_NULL;
2657 cur->nextqtd = next;
2658 cur->qtd.qtd_next = nextphys;
2659 /* Make sure to stop after a short transfer. */
2660 cur->qtd.qtd_altnext = htole32(newinactive->physaddr);
2661 cur->qtd.qtd_status =
2662 htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
2665 DPRINTFN(10,("ehci_alloc_sqtd_chain: curlen=%d\n", curlen));
2668 * adjust the toggle based on the number of packets
2671 if ((((curlen + mps - 1) / mps) & 1) || curlen == 0)
2672 qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
2674 qtdstatus |= EHCI_QTD_ACTIVE;
2680 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
2684 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
2687 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
2690 return (USBD_NORMAL_COMPLETION);
2693 /* XXX free chain */
2694 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
2695 return (USBD_NOMEM);
2698 /* Free the chain starting at sqtd and end at the qTD before sqtdend */
2700 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qh_t *sqh,
2701 ehci_soft_qtd_t *sqtd, ehci_soft_qtd_t *sqtdend)
2703 ehci_soft_qtd_t *p, **prevp;
2706 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
2709 /* First unlink the chain from the QH's software qTD list. */
2711 for (p = sqh->sqtd; p != NULL; p = p->nextqtd) {
2716 prevp = &p->nextqtd;
2718 KASSERT(p != NULL, ("ehci_free_sqtd_chain: chain not found"));
2719 for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
2721 ehci_free_sqtd(sc, sqtd);
2726 ehci_alloc_itd(ehci_softc_t *sc)
2728 struct ehci_soft_itd *itd, *freeitd;
2730 int i, s, offs, frindex, previndex;
2735 /* Find an itd that wasn't freed this frame or last frame. This can
2736 * discard itds that were freed before frindex wrapped around
2737 * XXX - can this lead to thrashing? Could fix by enabling wrap-around
2738 * interrupt and fiddling with list when that happens */
2739 frindex = (EOREAD4(sc, EHCI_FRINDEX) + 1) >> 3;
2740 previndex = (frindex != 0) ? frindex - 1 : sc->sc_flsize;
2743 LIST_FOREACH(itd, &sc->sc_freeitds, u.free_list) {
2746 if (itd->slot != frindex && itd->slot != previndex) {
2752 if (freeitd == NULL) {
2753 DPRINTFN(2, ("ehci_alloc_itd allocating chunk\n"));
2754 err = usb_allocmem(&sc->sc_bus, EHCI_ITD_SIZE * EHCI_ITD_CHUNK,
2755 EHCI_PAGE_SIZE, &dma);
2758 DPRINTF(("ehci_alloc_itd, alloc returned %d\n", err));
2762 for (i = 0; i < EHCI_ITD_CHUNK; i++) {
2763 offs = i * EHCI_ITD_SIZE;
2764 itd = KERNADDR(&dma, offs);
2765 itd->physaddr = DMAADDR(&dma, offs);
2768 LIST_INSERT_HEAD(&sc->sc_freeitds, itd, u.free_list);
2770 freeitd = LIST_FIRST(&sc->sc_freeitds);
2774 LIST_REMOVE(itd, u.free_list);
2775 memset(&itd->itd, 0, sizeof(ehci_itd_t));
2776 itd->u.frame_list.next = NULL;
2777 itd->u.frame_list.prev = NULL;
2778 itd->xfer_next = NULL;
2786 ehci_free_itd(ehci_softc_t *sc, ehci_soft_itd_t *itd)
2791 LIST_INSERT_AFTER(LIST_FIRST(&sc->sc_freeitds), itd, u.free_list);
2798 * Close a reqular pipe.
2799 * Assumes that there are no pending transactions.
2802 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
2804 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
2805 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2806 ehci_soft_qh_t *sqh = epipe->sqh;
2810 ehci_rem_qh(sc, sqh, head);
2812 pipe->endpoint->savedtoggle =
2813 EHCI_QTD_GET_TOGGLE(le32toh(sqh->qh.qh_qtd.qtd_status));
2814 ehci_free_sqh(sc, epipe->sqh);
2818 * Abort a device request.
2819 * If this routine is called at splusb() it guarantees that the request
2820 * will be removed from the hardware scheduling and that the callback
2821 * for it will be called with USBD_CANCELLED status.
2822 * It's impossible to guarantee that the requested transfer will not
2823 * have happened since the hardware runs concurrently.
2824 * If the transaction has already happened we rely on the ordinary
2825 * interrupt processing to process it.
2828 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2830 #define exfer EXFER(xfer)
2831 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2832 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2833 ehci_soft_qh_t *sqh = epipe->sqh;
2834 ehci_soft_qtd_t *sqtd, *snext;
2835 ehci_physaddr_t cur, us, next;
2838 /* int count = 0; */
2839 ehci_soft_qh_t *psqh;
2841 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
2844 /* If we're dying, just do the software part. */
2846 xfer->status = status; /* make software ignore it */
2847 callout_stop(&xfer->timeout_handle);
2848 usb_rem_task(epipe->pipe.device, &exfer->abort_task);
2849 usb_transfer_complete(xfer);
2854 if (xfer->device->bus->intr_context)
2855 panic("ehci_abort_xfer: not in process context");
2858 * If an abort is already in progress then just wait for it to
2859 * complete and return.
2861 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING) {
2862 DPRINTFN(2, ("ehci_abort_xfer: already aborting\n"));
2863 /* No need to wait if we're aborting from a timeout. */
2864 if (status == USBD_TIMEOUT)
2866 /* Override the status which might be USBD_TIMEOUT. */
2867 xfer->status = status;
2868 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n"));
2869 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTWAIT;
2870 while (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING)
2871 tsleep(&exfer->ehci_xfer_flags, PZERO, "ehciaw", 0);
2876 * Step 1: Make interrupt routine and timeouts ignore xfer.
2879 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTING;
2880 xfer->status = status; /* make software ignore it */
2881 callout_stop(&xfer->timeout_handle);
2882 usb_rem_task(epipe->pipe.device, &exfer->abort_task);
2886 * Step 2: Wait until we know hardware has finished any possible
2887 * use of the xfer. We do this by removing the entire
2888 * queue from the async schedule and waiting for the doorbell.
2889 * Nothing else should be touching the queue now.
2892 ehci_rem_qh(sc, sqh, psqh);
2895 * Step 3: make sure the soft interrupt routine
2896 * has run. This should remove any completed items off the queue.
2897 * The hardware has no reference to completed items (TDs).
2898 * It's safe to remove them at any time.
2901 #ifdef USB_USE_SOFTINTR
2902 sc->sc_softwake = 1;
2903 #endif /* USB_USE_SOFTINTR */
2904 usb_schedsoftintr(&sc->sc_bus);
2905 #ifdef USB_USE_SOFTINTR
2906 tsleep(&sc->sc_softwake, PZERO, "ehciab", 0);
2907 #endif /* USB_USE_SOFTINTR */
2910 * Step 4: Remove any vestiges of the xfer from the hardware.
2911 * The complication here is that the hardware may have executed
2912 * into or even beyond the xfer we're trying to abort.
2913 * So as we're scanning the TDs of this xfer we check if
2914 * the hardware points to any of them.
2916 * first we need to see if there are any transfers
2917 * on this queue before the xfer we are aborting.. we need
2918 * to update any pointers that point to us to point past
2919 * the aborting xfer. (If there is something past us).
2920 * Hardware and software.
2922 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
2925 /* If they initially point here. */
2926 us = exfer->sqtdstart->physaddr;
2928 /* We will change them to point here */
2929 snext = exfer->sqtdend->nextqtd;
2930 next = htole32(snext->physaddr);
2933 * Now loop through any qTDs before us and keep track of the pointer
2934 * that points to us for the end.
2937 while (sqtd && sqtd != exfer->sqtdstart) {
2938 hit |= (cur == sqtd->physaddr);
2939 if (EHCI_LINK_ADDR(le32toh(sqtd->qtd.qtd_next)) == us)
2940 sqtd->qtd.qtd_next = next;
2941 if (EHCI_LINK_ADDR(le32toh(sqtd->qtd.qtd_altnext)) == us)
2942 sqtd->qtd.qtd_altnext = next;
2943 sqtd = sqtd->nextqtd;
2947 * If we already saw the active one then we are pretty much done.
2948 * We've done all the relinking we need to do.
2953 * Now reinitialise the QH to point to the next qTD
2954 * (if there is one). We only need to do this if
2955 * it was previously pointing to us.
2957 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2958 if (cur == sqtd->physaddr) {
2961 if (sqtd == exfer->sqtdend)
2964 sqtd = sqtd->nextqtd;
2966 * Only need to alter the QH if it was pointing at a qTD
2967 * that we are removing.
2970 sqh->qh.qh_qtd.qtd_next = htole32(snext->physaddr);
2971 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
2972 sqh->qh.qh_qtd.qtd_status &=
2973 htole32(EHCI_QTD_TOGGLE_MASK);
2974 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
2975 sqh->qh.qh_qtd.qtd_buffer[i] = 0;
2976 sqh->qh.qh_qtd.qtd_buffer_hi[i] = 0;
2980 ehci_add_qh(sqh, psqh);
2982 * Step 5: Execute callback.
2987 /* Do the wakeup first to avoid touching the xfer after the callback. */
2988 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTING;
2989 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTWAIT) {
2990 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTWAIT;
2991 wakeup(&exfer->ehci_xfer_flags);
2993 usb_transfer_complete(xfer);
2995 /* printf("%s: %d TDs aborted\n", __func__, count); */
3001 ehci_timeout(void *addr)
3003 struct ehci_xfer *exfer = addr;
3004 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
3005 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
3007 DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
3010 usbd_dump_pipe(exfer->xfer.pipe);
3014 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
3018 /* Execute the abort in a process context. */
3019 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task,
3024 ehci_abort_isoc_xfer(usbd_xfer_handle xfer, usbd_status status)
3026 ehci_isoc_trans_t trans_status;
3027 struct ehci_pipe *epipe;
3028 struct ehci_xfer *exfer;
3030 struct ehci_soft_itd *itd;
3033 epipe = (struct ehci_pipe *) xfer->pipe;
3034 exfer = EXFER(xfer);
3035 sc = (ehci_softc_t *)epipe->pipe.device->bus;
3037 DPRINTF(("ehci_abort_isoc_xfer: xfer %p pipe %p\n", xfer, epipe));
3041 xfer->status = status;
3042 callout_stop(&xfer->timeout_handle);
3043 usb_rem_task(epipe->pipe.device, &exfer->abort_task);
3044 usb_transfer_complete(xfer);
3049 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING) {
3050 DPRINTFN(2, ("ehci_abort_isoc_xfer: already aborting\n"));
3053 if (status == USBD_TIMEOUT)
3054 printf("ehci_abort_xfer: TIMEOUT while aborting\n");
3057 xfer->status = status;
3058 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n"));
3059 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTWAIT;
3060 while (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING)
3061 tsleep(&exfer->ehci_xfer_flags, PZERO, "ehciaw", 0);
3064 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTING;
3066 xfer->status = status;
3067 callout_stop(&xfer->timeout_handle);
3068 usb_rem_task(epipe->pipe.device, &exfer->abort_task);
3071 for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) {
3073 for (i = 0; i < 8; i++) {
3074 trans_status = le32toh(itd->itd.itd_ctl[i]);
3075 trans_status &= ~EHCI_ITD_ACTIVE;
3076 itd->itd.itd_ctl[i] = htole32(trans_status);
3083 #ifdef USB_USE_SOFTINTR
3084 sc->sc_softwake = 1;
3085 #endif /* USB_USE_SOFTINTR */
3086 usb_schedsoftintr(&sc->sc_bus);
3087 #ifdef USB_USE_SOFTINTR
3088 tsleep(&sc->sc_softwake, PZERO, "ehciab", 0);
3089 #endif /* USB_USE_SOFTINTR */
3095 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTING;
3096 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTWAIT) {
3097 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTWAIT;
3098 wakeup(&exfer->ehci_xfer_flags);
3100 usb_transfer_complete(xfer);
3104 ehci_timeout_task(void *addr)
3106 usbd_xfer_handle xfer = addr;
3109 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
3112 ehci_abort_xfer(xfer, USBD_TIMEOUT);
3117 * Some EHCI chips from VIA / ATI seem to trigger interrupts before writing
3118 * back the qTD status, or miss signalling occasionally under heavy load.
3119 * If the host machine is too fast, we can miss transaction completion - when
3120 * we scan the active list the transaction still seems to be active. This
3121 * generally exhibits itself as a umass stall that never recovers.
3123 * We work around this behaviour by setting up this callback after any softintr
3124 * that completes with transactions still pending, giving us another chance to
3125 * check for completion after the writeback has taken place.
3128 ehci_intrlist_timeout(void *arg)
3130 ehci_softc_t *sc = arg;
3133 DPRINTFN(3, ("ehci_intrlist_timeout\n"));
3134 usb_schedsoftintr(&sc->sc_bus);
3139 /************************/
3142 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
3146 /* Insert last in queue. */
3147 err = usb_insert_transfer(xfer);
3151 /* Pipe isn't running, start first */
3152 return (ehci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
3156 ehci_device_ctrl_start(usbd_xfer_handle xfer)
3158 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3162 return (USBD_IOERROR);
3165 if (!(xfer->rqflags & URQ_REQUEST)) {
3167 printf("ehci_device_ctrl_transfer: not a request\n");
3168 return (USBD_INVAL);
3172 err = ehci_device_request(xfer);
3176 if (sc->sc_bus.use_polling)
3177 ehci_waitintr(sc, xfer);
3178 return (USBD_IN_PROGRESS);
3182 ehci_device_ctrl_done(usbd_xfer_handle xfer)
3184 struct ehci_xfer *ex = EXFER(xfer);
3185 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3186 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3188 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
3191 if (!(xfer->rqflags & URQ_REQUEST)) {
3192 panic("ehci_ctrl_done: not a request");
3196 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3197 ehci_del_intr_list(ex); /* remove from active list */
3198 ehci_free_sqtd_chain(sc, epipe->sqh, ex->sqtdstart,
3199 ex->sqtdend->nextqtd);
3202 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
3205 /* Abort a device control request. */
3207 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
3209 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
3210 ehci_abort_xfer(xfer, USBD_CANCELLED);
3213 /* Close a device control pipe. */
3215 ehci_device_ctrl_close(usbd_pipe_handle pipe)
3217 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3218 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
3220 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
3221 ehci_close_pipe(pipe, sc->sc_async_head);
3225 ehci_device_request(usbd_xfer_handle xfer)
3227 #define exfer EXFER(xfer)
3228 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3229 usb_device_request_t *req = &xfer->request;
3230 usbd_device_handle dev = epipe->pipe.device;
3231 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
3232 ehci_soft_qtd_t *newinactive, *setup, *stat, *next;
3233 ehci_soft_qh_t *sqh;
3239 isread = req->bmRequestType & UT_READ;
3240 len = UGETW(req->wLength);
3242 DPRINTFN(3,("ehci_device_request: type=0x%02x, request=0x%02x, "
3243 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
3244 req->bmRequestType, req->bRequest, UGETW(req->wValue),
3245 UGETW(req->wIndex), len, dev->address,
3246 epipe->pipe.endpoint->edesc->bEndpointAddress));
3248 newinactive = ehci_alloc_sqtd(sc);
3249 if (newinactive == NULL) {
3253 newinactive->qtd.qtd_status = htole32(0);
3254 newinactive->qtd.qtd_next = EHCI_NULL;
3255 newinactive->qtd.qtd_altnext = EHCI_NULL;
3256 stat = ehci_alloc_sqtd(sc);
3263 setup = sqh->inactivesqtd;
3264 sqh->inactivesqtd = newinactive;
3265 epipe->u.ctl.length = len;
3267 /* Set up data transaction */
3269 ehci_soft_qtd_t *end;
3271 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3272 NULL, newinactive, &next, &end);
3275 end->qtd.qtd_status &= htole32(~EHCI_QTD_IOC);
3276 end->nextqtd = stat;
3277 end->qtd.qtd_next = htole32(stat->physaddr);
3278 end->qtd.qtd_altnext = htole32(newinactive->physaddr);
3283 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
3285 /* Clear toggle, and do not activate until complete */
3286 setup->qtd.qtd_status = htole32(
3287 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
3288 EHCI_QTD_SET_CERR(3) |
3289 EHCI_QTD_SET_TOGGLE(0) |
3290 EHCI_QTD_SET_BYTES(sizeof *req)
3292 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
3293 setup->qtd.qtd_buffer_hi[0] = 0;
3294 setup->nextqtd = next;
3295 setup->qtd.qtd_next = htole32(next->physaddr);
3296 setup->qtd.qtd_altnext = htole32(newinactive->physaddr);
3298 setup->len = sizeof *req;
3300 stat->qtd.qtd_status = htole32(
3302 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
3303 EHCI_QTD_SET_CERR(3) |
3304 EHCI_QTD_SET_TOGGLE(1) |
3307 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
3308 stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */
3309 stat->nextqtd = newinactive;
3310 stat->qtd.qtd_next = htole32(newinactive->physaddr);
3311 stat->qtd.qtd_altnext = htole32(newinactive->physaddr);
3316 if (ehcidebug > 5) {
3317 DPRINTF(("ehci_device_request:\n"));
3319 ehci_dump_sqtds(setup);
3323 exfer->sqtdstart = setup;
3324 exfer->sqtdend = stat;
3326 if (!exfer->isdone) {
3327 printf("ehci_device_request: not done, exfer=%p\n", exfer);
3332 /* Activate the new qTD in the QH list. */
3334 ehci_activate_qh(sqh, setup);
3335 if (xfer->timeout && !sc->sc_bus.use_polling) {
3336 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3337 ehci_timeout, xfer);
3339 ehci_add_intr_list(sc, exfer);
3340 xfer->status = USBD_IN_PROGRESS;
3344 if (ehcidebug > 10) {
3345 DPRINTF(("ehci_device_request: status=%x\n",
3346 EOREAD4(sc, EHCI_USBSTS)));
3349 ehci_dump_sqh(sc->sc_async_head);
3351 ehci_dump_sqtds(setup);
3355 return (USBD_NORMAL_COMPLETION);
3358 sqh->inactivesqtd = setup;
3359 ehci_free_sqtd(sc, stat);
3361 ehci_free_sqtd(sc, newinactive);
3363 DPRINTFN(-1,("ehci_device_request: no memory\n"));
3365 usb_transfer_complete(xfer);
3370 /************************/
3373 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
3377 /* Insert last in queue. */
3378 err = usb_insert_transfer(xfer);
3382 /* Pipe isn't running, start first */
3383 return (ehci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
3387 ehci_device_bulk_start(usbd_xfer_handle xfer)
3389 #define exfer EXFER(xfer)
3390 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3391 usbd_device_handle dev = epipe->pipe.device;
3392 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
3393 ehci_soft_qtd_t *data, *dataend, *newinactive;
3394 ehci_soft_qh_t *sqh;
3396 int len, isread, endpt;
3399 DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n",
3400 xfer, xfer->length, xfer->flags));
3403 return (USBD_IOERROR);
3406 if (xfer->rqflags & URQ_REQUEST)
3407 panic("ehci_device_bulk_start: a request");
3411 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3412 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3415 epipe->u.bulk.length = len;
3417 newinactive = ehci_alloc_sqtd(sc);
3418 if (newinactive == NULL) {
3419 DPRINTFN(-1,("ehci_device_bulk_start: no sqtd memory\n"));
3422 usb_transfer_complete(xfer);
3425 newinactive->qtd.qtd_status = htole32(0);
3426 newinactive->qtd.qtd_next = EHCI_NULL;
3427 newinactive->qtd.qtd_altnext = EHCI_NULL;
3428 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3429 sqh->inactivesqtd, newinactive, &data, &dataend);
3431 DPRINTFN(-1,("ehci_device_bulk_start: no memory\n"));
3432 ehci_free_sqtd(sc, newinactive);
3434 usb_transfer_complete(xfer);
3437 dataend->nextqtd = newinactive;
3438 dataend->qtd.qtd_next = htole32(newinactive->physaddr);
3439 dataend->qtd.qtd_altnext = htole32(newinactive->physaddr);
3440 sqh->inactivesqtd = newinactive;
3443 if (ehcidebug > 5) {
3444 DPRINTF(("ehci_device_bulk_start: data(1)\n"));
3446 ehci_dump_sqtds(data);
3450 /* Set up interrupt info. */
3451 exfer->sqtdstart = data;
3452 exfer->sqtdend = dataend;
3454 if (!exfer->isdone) {
3455 printf("ehci_device_bulk_start: not done, ex=%p\n", exfer);
3461 ehci_activate_qh(sqh, data);
3462 if (xfer->timeout && !sc->sc_bus.use_polling) {
3463 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3464 ehci_timeout, xfer);
3466 ehci_add_intr_list(sc, exfer);
3467 xfer->status = USBD_IN_PROGRESS;
3471 if (ehcidebug > 10) {
3472 DPRINTF(("ehci_device_bulk_start: data(2)\n"));
3474 DPRINTF(("ehci_device_bulk_start: data(3)\n"));
3477 printf("async_head:\n");
3478 ehci_dump_sqh(sc->sc_async_head);
3482 ehci_dump_sqtds(data);
3486 if (sc->sc_bus.use_polling)
3487 ehci_waitintr(sc, xfer);
3489 return (USBD_IN_PROGRESS);
3494 ehci_device_bulk_abort(usbd_xfer_handle xfer)
3496 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
3497 ehci_abort_xfer(xfer, USBD_CANCELLED);
3501 * Close a device bulk pipe.
3504 ehci_device_bulk_close(usbd_pipe_handle pipe)
3506 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3508 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
3509 ehci_close_pipe(pipe, sc->sc_async_head);
3513 ehci_device_bulk_done(usbd_xfer_handle xfer)
3515 struct ehci_xfer *ex = EXFER(xfer);
3516 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3517 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3519 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
3520 xfer, xfer->actlen));
3522 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3523 ehci_del_intr_list(ex); /* remove from active list */
3524 ehci_free_sqtd_chain(sc, epipe->sqh, ex->sqtdstart,
3525 ex->sqtdend->nextqtd);
3528 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
3531 /************************/
3534 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
3536 struct ehci_soft_islot *isp;
3539 /* Find a poll rate that is large enough. */
3540 for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
3541 if (EHCI_ILEV_IVAL(lev) <= ival)
3544 /* Pick an interrupt slot at the right level. */
3545 /* XXX could do better than picking at random. */
3546 islot = EHCI_IQHIDX(lev, arc4random());
3549 isp = &sc->sc_islots[islot];
3550 ehci_add_qh(sqh, isp->sqh);
3552 return (USBD_NORMAL_COMPLETION);
3556 ehci_device_intr_transfer(usbd_xfer_handle xfer)
3560 /* Insert last in queue. */
3561 err = usb_insert_transfer(xfer);
3566 * Pipe isn't running (otherwise err would be USBD_INPROG),
3567 * so start it first.
3569 return (ehci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3573 ehci_device_intr_start(usbd_xfer_handle xfer)
3575 #define exfer EXFER(xfer)
3576 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3577 usbd_device_handle dev = xfer->pipe->device;
3578 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
3579 ehci_soft_qtd_t *data, *dataend, *newinactive;
3580 ehci_soft_qh_t *sqh;
3582 int len, isread, endpt;
3585 DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%d flags=%d\n",
3586 xfer, xfer->length, xfer->flags));
3589 return (USBD_IOERROR);
3592 if (xfer->rqflags & URQ_REQUEST)
3593 panic("ehci_device_intr_start: a request");
3597 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3598 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3601 epipe->u.intr.length = len;
3603 newinactive = ehci_alloc_sqtd(sc);
3604 if (newinactive == NULL) {
3605 DPRINTFN(-1,("ehci_device_intr_start: no sqtd memory\n"));
3608 usb_transfer_complete(xfer);
3611 newinactive->qtd.qtd_status = htole32(0);
3612 newinactive->qtd.qtd_next = EHCI_NULL;
3613 newinactive->qtd.qtd_altnext = EHCI_NULL;
3614 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3615 sqh->inactivesqtd, newinactive, &data, &dataend);
3617 DPRINTFN(-1, ("ehci_device_intr_start: no memory\n"));
3619 usb_transfer_complete(xfer);
3622 dataend->nextqtd = newinactive;
3623 dataend->qtd.qtd_next = htole32(newinactive->physaddr);
3624 dataend->qtd.qtd_altnext = htole32(newinactive->physaddr);
3625 sqh->inactivesqtd = newinactive;
3628 if (ehcidebug > 5) {
3629 DPRINTF(("ehci_device_intr_start: data(1)\n"));
3631 ehci_dump_sqtds(data);
3635 /* Set up interrupt info. */
3636 exfer->sqtdstart = data;
3637 exfer->sqtdend = dataend;
3639 if (!exfer->isdone) {
3640 printf("ehci_device_intr_start: not done, ex=%p\n", exfer);
3646 ehci_activate_qh(sqh, data);
3647 if (xfer->timeout && !sc->sc_bus.use_polling) {
3648 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3649 ehci_timeout, xfer);
3651 ehci_add_intr_list(sc, exfer);
3652 xfer->status = USBD_IN_PROGRESS;
3656 if (ehcidebug > 10) {
3657 DPRINTF(("ehci_device_intr_start: data(2)\n"));
3659 DPRINTF(("ehci_device_intr_start: data(3)\n"));
3663 ehci_dump_sqtds(data);
3667 if (sc->sc_bus.use_polling)
3668 ehci_waitintr(sc, xfer);
3670 return (USBD_IN_PROGRESS);
3675 ehci_device_intr_abort(usbd_xfer_handle xfer)
3677 DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer));
3678 if (xfer->pipe->intrxfer == xfer) {
3679 DPRINTFN(1, ("ehci_device_intr_abort: remove\n"));
3680 xfer->pipe->intrxfer = NULL;
3683 * XXX - abort_xfer uses ehci_sync_hc, which syncs via the advance
3684 * async doorbell. That's dependant on the async list, wheras
3685 * intr xfers are periodic, should not use this?
3687 ehci_abort_xfer(xfer, USBD_CANCELLED);
3691 ehci_device_intr_close(usbd_pipe_handle pipe)
3693 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3694 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
3695 struct ehci_soft_islot *isp;
3697 isp = &sc->sc_islots[epipe->sqh->islot];
3698 ehci_close_pipe(pipe, isp->sqh);
3702 ehci_device_intr_done(usbd_xfer_handle xfer)
3704 #define exfer EXFER(xfer)
3705 struct ehci_xfer *ex = EXFER(xfer);
3706 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3707 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3708 ehci_soft_qtd_t *data, *dataend, *newinactive;
3709 ehci_soft_qh_t *sqh;
3711 int len, isread, endpt, s;
3713 DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n",
3714 xfer, xfer->actlen));
3717 if (xfer->pipe->repeat) {
3718 ehci_free_sqtd_chain(sc, sqh, ex->sqtdstart,
3719 ex->sqtdend->nextqtd);
3721 len = epipe->u.intr.length;
3723 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3724 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3726 newinactive = ehci_alloc_sqtd(sc);
3727 if (newinactive == NULL) {
3729 ("ehci_device_intr_done: no sqtd memory\n"));
3734 newinactive->qtd.qtd_status = htole32(0);
3735 newinactive->qtd.qtd_next = EHCI_NULL;
3736 newinactive->qtd.qtd_altnext = EHCI_NULL;
3737 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3738 sqh->inactivesqtd, newinactive, &data, &dataend);
3740 DPRINTFN(-1, ("ehci_device_intr_done: no memory\n"));
3744 dataend->nextqtd = newinactive;
3745 dataend->qtd.qtd_next = htole32(newinactive->physaddr);
3746 dataend->qtd.qtd_altnext = htole32(newinactive->physaddr);
3747 sqh->inactivesqtd = newinactive;
3749 /* Set up interrupt info. */
3750 exfer->sqtdstart = data;
3751 exfer->sqtdend = dataend;
3753 if (!exfer->isdone) {
3754 printf("ehci_device_intr_done: not done, ex=%p\n",
3761 ehci_activate_qh(sqh, data);
3762 if (xfer->timeout && !sc->sc_bus.use_polling) {
3763 callout_reset(&xfer->timeout_handle,
3764 MS_TO_TICKS(xfer->timeout), ehci_timeout, xfer);
3768 xfer->status = USBD_IN_PROGRESS;
3769 } else if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3770 ehci_del_intr_list(ex); /* remove from active list */
3771 ehci_free_sqtd_chain(sc, sqh, ex->sqtdstart,
3772 ex->sqtdend->nextqtd);
3777 /************************/
3780 ehci_device_isoc_transfer(usbd_xfer_handle xfer)
3784 err = usb_insert_transfer(xfer);
3785 if (err && err != USBD_IN_PROGRESS)
3788 return (ehci_device_isoc_start(xfer));
3792 ehci_device_isoc_start(usbd_xfer_handle xfer)
3794 struct ehci_pipe *epipe;
3795 usbd_device_handle dev;
3797 struct ehci_xfer *exfer;
3798 ehci_soft_itd_t *itd, *prev, *start, *stop;
3800 int i, j, k, frames, uframes, ufrperframe;
3801 int s, trans_count, offs, total_length;
3809 exfer = (struct ehci_xfer *) xfer;
3810 sc = (ehci_softc_t *)xfer->pipe->device->bus;
3811 dev = xfer->pipe->device;
3812 epipe = (struct ehci_pipe *)xfer->pipe;
3815 * To allow continuous transfers, above we start all transfers
3816 * immediately. However, we're still going to get usbd_start_next call
3817 * this when another xfer completes. So, check if this is already
3818 * in progress or not
3821 if (exfer->itdstart != NULL)
3822 return (USBD_IN_PROGRESS);
3824 DPRINTFN(2, ("ehci_device_isoc_start: xfer %p len %d flags %d\n",
3825 xfer, xfer->length, xfer->flags));
3828 return (USBD_IOERROR);
3831 * To avoid complication, don't allow a request right now that'll span
3832 * the entire frame table. To within 4 frames, to allow some leeway
3833 * on either side of where the hc currently is.
3835 if ((1 << (epipe->pipe.endpoint->edesc->bInterval)) *
3836 xfer->nframes >= (sc->sc_flsize - 4) * 8) {
3837 printf("ehci: isoc descriptor requested that spans the entire"
3838 " frametable, too many frames\n");
3839 return (USBD_INVAL);
3843 if (xfer->rqflags & URQ_REQUEST)
3844 panic("ehci_device_isoc_start: request\n");
3847 printf("ehci_device_isoc_start: not done, ex = %p\n", exfer);
3852 * Step 1: Allocate and initialize itds, how many do we need?
3853 * One per transfer if interval >= 8 microframes, fewer if we use
3854 * multiple microframes per frame.
3857 i = epipe->pipe.endpoint->edesc->bInterval;
3858 if (i > 16 || i == 0) {
3859 /* Spec page 271 says intervals > 16 are invalid */
3860 DPRINTF(("ehci_device_isoc_start: bInvertal %d invalid\n", i));
3861 return (USBD_INVAL);
3878 frames = (xfer->nframes + (ufrperframe - 1)) / ufrperframe;
3879 uframes = 8 / ufrperframe;
3882 DPRINTF(("ehci_device_isoc_start: frames == 0\n"));
3883 return (USBD_INVAL);
3886 dma_buf = xfer->buffer;
3889 for (i = 0; i < frames; i++) {
3891 itd = ehci_alloc_itd(sc);
3894 prev->itd.itd_next =
3895 htole32(itd->physaddr | EHCI_LINK_ITD);
3896 prev->xfer_next = itd;
3902 * Step 1.5, initialize uframes
3904 for (j = 0; j < 8; j += uframes) {
3905 /* Calculate which page in the list this starts in */
3906 int addr = DMAADDR(dma_buf, froffs);
3907 addr = EHCI_PAGE_OFFSET(addr);
3908 addr += (offs - froffs);
3909 addr = EHCI_PAGE(addr);
3910 addr /= EHCI_PAGE_SIZE;
3912 /* This gets the initial offset into the first page,
3913 * looks how far further along the current uframe
3914 * offset is. Works out how many pages that is.
3917 itd->itd.itd_ctl[j] = htole32 ( EHCI_ITD_ACTIVE |
3918 EHCI_ITD_SET_LEN(xfer->frlengths[trans_count]) |
3919 EHCI_ITD_SET_PG(addr) |
3920 EHCI_ITD_SET_OFFS(EHCI_PAGE_OFFSET(DMAADDR(dma_buf,
3923 total_length += xfer->frlengths[trans_count];
3924 offs += xfer->frlengths[trans_count];
3927 if (trans_count >= xfer->nframes) { /*Set IOC*/
3928 itd->itd.itd_ctl[j] |= htole32(EHCI_ITD_IOC);
3932 /* Step 1.75, set buffer pointers. To simplify matters, all
3933 * pointers are filled out for the next 7 hardware pages in
3934 * the dma block, so no need to worry what pages to cover
3938 for (j=0; j < 7; j++) {
3940 * Don't try to lookup a page that's past the end
3943 int page_offs = EHCI_PAGE(froffs +
3944 (EHCI_PAGE_SIZE * j));
3945 if (page_offs >= dma_buf->block->size)
3948 int page = DMAADDR(dma_buf, page_offs);
3949 page = EHCI_PAGE(page);
3950 itd->itd.itd_bufr[j] =
3951 htole32(EHCI_ITD_SET_BPTR(page) | EHCI_LINK_ITD);
3955 * Other special values
3958 k = epipe->pipe.endpoint->edesc->bEndpointAddress;
3959 itd->itd.itd_bufr[0] |= htole32(
3960 EHCI_ITD_SET_EP(UE_GET_ADDR(k)) |
3961 EHCI_ITD_SET_DADDR(epipe->pipe.device->address));
3963 k = (UE_GET_DIR(epipe->pipe.endpoint->edesc->bEndpointAddress))
3966 UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize));
3967 itd->itd.itd_bufr[1] |= htole32(EHCI_ITD_SET_DIR(k) |
3968 EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j)));
3970 /* FIXME: handle invalid trans */
3971 itd->itd.itd_bufr[2] |=
3972 htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j)+1));
3974 } /* End of frame */
3977 stop->xfer_next = NULL;
3978 exfer->isoc_len = total_length;
3981 * Part 2: Transfer descriptors have now been set up, now they must
3982 * be scheduled into the period frame list. Erk. Not wanting to
3983 * complicate matters, transfer is denied if the transfer spans
3984 * more than the period frame list.
3989 /* Start inserting frames */
3990 if (epipe->u.isoc.cur_xfers > 0) {
3991 frindex = epipe->u.isoc.next_frame;
3993 frindex = EOREAD4(sc, EHCI_FRINDEX);
3994 frindex = frindex >> 3; /* Erase microframe index */
3998 if (frindex >= sc->sc_flsize)
3999 frindex &= (sc->sc_flsize - 1);
4001 /* Whats the frame interval? */
4002 i = (1 << epipe->pipe.endpoint->edesc->bInterval);
4009 for (j = 0; j < frames; j++) {
4011 panic("ehci: unexpectedly ran out of isoc itds,"
4014 itd->itd.itd_next = sc->sc_flist[frindex];
4015 if (itd->itd.itd_next == 0)
4016 /* FIXME: frindex table gets initialized to NULL
4018 itd->itd.itd_next = htole32(EHCI_NULL);
4020 sc->sc_flist[frindex] = htole32(EHCI_LINK_ITD | itd->physaddr);
4022 itd->u.frame_list.next = sc->sc_softitds[frindex];
4023 sc->sc_softitds[frindex] = itd;
4024 if (itd->u.frame_list.next != NULL)
4025 itd->u.frame_list.next->u.frame_list.prev = itd;
4026 itd->slot = frindex;
4027 itd->u.frame_list.prev = NULL;
4030 if (frindex >= sc->sc_flsize)
4031 frindex -= sc->sc_flsize;
4033 itd = itd->xfer_next;
4036 epipe->u.isoc.cur_xfers++;
4037 epipe->u.isoc.next_frame = frindex;
4039 exfer->itdstart = start;
4040 exfer->itdend = stop;
4041 exfer->sqtdstart = NULL;
4042 exfer->sqtdstart = NULL;
4044 ehci_add_intr_list(sc, exfer);
4045 xfer->status = USBD_IN_PROGRESS;
4049 if (sc->sc_bus.use_polling) {
4050 printf("Starting ehci isoc xfer with polling. Bad idea?\n");
4051 ehci_waitintr(sc, xfer);
4054 return (USBD_IN_PROGRESS);
4058 ehci_device_isoc_abort(usbd_xfer_handle xfer)
4060 DPRINTFN(1, ("ehci_device_isoc_abort: xfer = %p\n", xfer));
4061 ehci_abort_isoc_xfer(xfer, USBD_CANCELLED);
4065 ehci_device_isoc_close(usbd_pipe_handle pipe)
4067 printf("ehci_device_isoc_close: nothing in the pipe to free?\n");
4071 ehci_device_isoc_done(usbd_xfer_handle xfer)
4073 struct ehci_xfer *exfer;
4075 struct ehci_pipe *epipe;
4078 exfer = EXFER(xfer);
4079 sc = (ehci_softc_t *)xfer->pipe->device->bus;
4080 epipe = (struct ehci_pipe *) xfer->pipe;
4083 epipe->u.isoc.cur_xfers--;
4084 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(exfer)) {
4085 ehci_del_intr_list(exfer);
4086 ehci_rem_free_itd_chain(sc, exfer);