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>
71 #include <sys/lockmgr.h>
72 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
73 #include <machine/cpu.h>
76 #include <sys/queue.h>
77 #include <sys/sysctl.h>
79 #include <machine/bus.h>
80 #include <machine/endian.h>
82 #include <dev/usb/usb.h>
83 #include <dev/usb/usbdi.h>
84 #include <dev/usb/usbdivar.h>
85 #include <dev/usb/usb_mem.h>
86 #include <dev/usb/usb_quirks.h>
88 #include <dev/usb/ehcireg.h>
89 #include <dev/usb/ehcivar.h>
91 #define delay(d) DELAY(d)
94 #define EHCI_DEBUG USB_DEBUG
95 #define DPRINTF(x) do { if (ehcidebug) printf x; } while (0)
96 #define DPRINTFN(n,x) do { if (ehcidebug>(n)) printf x; } while (0)
98 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
99 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
100 &ehcidebug, 0, "ehci debug level");
101 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
104 #define DPRINTFN(n,x)
108 struct usbd_pipe pipe;
112 ehci_soft_qtd_t *qtd;
113 /* ehci_soft_itd_t *itd; */
120 /*ehci_soft_qtd_t *setup, *data, *stat;*/
135 static usbd_status ehci_open(usbd_pipe_handle);
136 static void ehci_poll(struct usbd_bus *);
137 static void ehci_softintr(void *);
138 static int ehci_intr1(ehci_softc_t *);
139 static void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
140 static void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
141 static void ehci_idone(struct ehci_xfer *);
142 static void ehci_timeout(void *);
143 static void ehci_timeout_task(void *);
144 static void ehci_intrlist_timeout(void *);
146 static usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
147 static void ehci_freem(struct usbd_bus *, usb_dma_t *);
149 static usbd_xfer_handle ehci_allocx(struct usbd_bus *);
150 static void ehci_freex(struct usbd_bus *, usbd_xfer_handle);
152 static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle);
153 static usbd_status ehci_root_ctrl_start(usbd_xfer_handle);
154 static void ehci_root_ctrl_abort(usbd_xfer_handle);
155 static void ehci_root_ctrl_close(usbd_pipe_handle);
156 static void ehci_root_ctrl_done(usbd_xfer_handle);
158 static usbd_status ehci_root_intr_transfer(usbd_xfer_handle);
159 static usbd_status ehci_root_intr_start(usbd_xfer_handle);
160 static void ehci_root_intr_abort(usbd_xfer_handle);
161 static void ehci_root_intr_close(usbd_pipe_handle);
162 static void ehci_root_intr_done(usbd_xfer_handle);
164 static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle);
165 static usbd_status ehci_device_ctrl_start(usbd_xfer_handle);
166 static void ehci_device_ctrl_abort(usbd_xfer_handle);
167 static void ehci_device_ctrl_close(usbd_pipe_handle);
168 static void ehci_device_ctrl_done(usbd_xfer_handle);
170 static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle);
171 static usbd_status ehci_device_bulk_start(usbd_xfer_handle);
172 static void ehci_device_bulk_abort(usbd_xfer_handle);
173 static void ehci_device_bulk_close(usbd_pipe_handle);
174 static void ehci_device_bulk_done(usbd_xfer_handle);
176 static usbd_status ehci_device_intr_transfer(usbd_xfer_handle);
177 static usbd_status ehci_device_intr_start(usbd_xfer_handle);
178 static void ehci_device_intr_abort(usbd_xfer_handle);
179 static void ehci_device_intr_close(usbd_pipe_handle);
180 static void ehci_device_intr_done(usbd_xfer_handle);
182 static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle);
183 static usbd_status ehci_device_isoc_start(usbd_xfer_handle);
184 static void ehci_device_isoc_abort(usbd_xfer_handle);
185 static void ehci_device_isoc_close(usbd_pipe_handle);
186 static void ehci_device_isoc_done(usbd_xfer_handle);
188 static void ehci_device_clear_toggle(usbd_pipe_handle pipe);
189 static void ehci_noop(usbd_pipe_handle pipe);
191 static int ehci_str(usb_string_descriptor_t *, int, char *);
192 static void ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
193 static void ehci_pcd_able(ehci_softc_t *, int);
194 static void ehci_pcd_enable(void *);
195 static void ehci_disown(ehci_softc_t *, int, int);
197 static ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *);
198 static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
200 static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
201 static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
202 static usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *,
203 ehci_softc_t *, int, int, usbd_xfer_handle,
204 ehci_soft_qtd_t *, ehci_soft_qtd_t *,
205 ehci_soft_qtd_t **, ehci_soft_qtd_t **);
206 static void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qh_t *,
207 ehci_soft_qtd_t *, ehci_soft_qtd_t *);
209 static usbd_status ehci_device_request(usbd_xfer_handle xfer);
211 static usbd_status ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
214 static void ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *);
215 static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
217 static void ehci_activate_qh(ehci_soft_qh_t *, ehci_soft_qtd_t *);
218 static void ehci_sync_hc(ehci_softc_t *);
220 static void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
221 static void ehci_abort_xfer(usbd_xfer_handle, usbd_status);
224 static void ehci_dump_regs(ehci_softc_t *);
225 void ehci_dump(void);
226 static ehci_softc_t *theehci;
227 static void ehci_dump_link(ehci_link_t, int);
228 static void ehci_dump_sqtds(ehci_soft_qtd_t *);
229 static void ehci_dump_sqtd(ehci_soft_qtd_t *);
230 static void ehci_dump_qtd(ehci_qtd_t *);
231 static void ehci_dump_sqh(ehci_soft_qh_t *);
233 static void ehci_dump_exfer(struct ehci_xfer *);
237 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
239 #define EHCI_INTR_ENDPT 1
241 #define ehci_add_intr_list(sc, ex) \
242 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext);
243 #define ehci_del_intr_list(ex) \
245 LIST_REMOVE((ex), inext); \
246 (ex)->inext.le_prev = NULL; \
248 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL)
250 static struct usbd_bus_methods ehci_bus_methods = {
260 static struct usbd_pipe_methods ehci_root_ctrl_methods = {
261 ehci_root_ctrl_transfer,
262 ehci_root_ctrl_start,
263 ehci_root_ctrl_abort,
264 ehci_root_ctrl_close,
269 static struct usbd_pipe_methods ehci_root_intr_methods = {
270 ehci_root_intr_transfer,
271 ehci_root_intr_start,
272 ehci_root_intr_abort,
273 ehci_root_intr_close,
278 static struct usbd_pipe_methods ehci_device_ctrl_methods = {
279 ehci_device_ctrl_transfer,
280 ehci_device_ctrl_start,
281 ehci_device_ctrl_abort,
282 ehci_device_ctrl_close,
284 ehci_device_ctrl_done,
287 static struct usbd_pipe_methods ehci_device_intr_methods = {
288 ehci_device_intr_transfer,
289 ehci_device_intr_start,
290 ehci_device_intr_abort,
291 ehci_device_intr_close,
292 ehci_device_clear_toggle,
293 ehci_device_intr_done,
296 static struct usbd_pipe_methods ehci_device_bulk_methods = {
297 ehci_device_bulk_transfer,
298 ehci_device_bulk_start,
299 ehci_device_bulk_abort,
300 ehci_device_bulk_close,
301 ehci_device_clear_toggle,
302 ehci_device_bulk_done,
305 static struct usbd_pipe_methods ehci_device_isoc_methods = {
306 ehci_device_isoc_transfer,
307 ehci_device_isoc_start,
308 ehci_device_isoc_abort,
309 ehci_device_isoc_close,
311 ehci_device_isoc_done,
315 ehci_init(ehci_softc_t *sc)
317 u_int32_t version, sparams, cparams, hcr;
324 DPRINTF(("ehci_init: start\n"));
329 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
331 version = EREAD2(sc, EHCI_HCIVERSION);
332 printf("%s: EHCI version %x.%x\n", device_get_nameunit(sc->sc_bus.bdev),
333 version >> 8, version & 0xff);
335 sparams = EREAD4(sc, EHCI_HCSPARAMS);
336 DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
337 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
338 ncomp = EHCI_HCS_N_CC(sparams);
339 if (ncomp != sc->sc_ncomp) {
340 printf("%s: wrong number of companions (%d != %d)\n",
341 device_get_nameunit(sc->sc_bus.bdev),
342 ncomp, sc->sc_ncomp);
343 if (ncomp < sc->sc_ncomp)
344 sc->sc_ncomp = ncomp;
346 if (sc->sc_ncomp > 0) {
347 printf("%s: companion controller%s, %d port%s each:",
348 device_get_nameunit(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
349 EHCI_HCS_N_PCC(sparams),
350 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
351 for (i = 0; i < sc->sc_ncomp; i++)
352 printf(" %s", device_get_nameunit(sc->sc_comps[i]->bdev));
355 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
356 cparams = EREAD4(sc, EHCI_HCCPARAMS);
357 DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
359 if (EHCI_HCC_64BIT(cparams)) {
360 /* MUST clear segment register if 64 bit capable. */
361 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
364 sc->sc_bus.usbrev = USBREV_2_0;
366 /* Reset the controller */
367 DPRINTF(("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev)));
368 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
369 usb_delay_ms(&sc->sc_bus, 1);
370 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
371 for (i = 0; i < 100; i++) {
372 usb_delay_ms(&sc->sc_bus, 1);
373 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
378 printf("%s: reset timeout\n",
379 device_get_nameunit(sc->sc_bus.bdev));
380 return (USBD_IOERROR);
383 /* frame list size at default, read back what we got and use that */
384 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
385 case 0: sc->sc_flsize = 1024; break;
386 case 1: sc->sc_flsize = 512; break;
387 case 2: sc->sc_flsize = 256; break;
388 case 3: return (USBD_IOERROR);
390 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t),
391 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
394 DPRINTF(("%s: flsize=%d\n", device_get_nameunit(sc->sc_bus.bdev),sc->sc_flsize));
395 sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
396 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
398 /* Set up the bus struct. */
399 sc->sc_bus.methods = &ehci_bus_methods;
400 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
402 #if defined(__NetBSD__) || defined(__OpenBSD__)
403 sc->sc_powerhook = powerhook_establish(ehci_power, sc);
404 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
407 sc->sc_eintrs = EHCI_NORMAL_INTRS;
410 * Allocate the interrupt dummy QHs. These are arranged to give
411 * poll intervals that are powers of 2 times 1ms.
413 for (i = 0; i < EHCI_INTRQHS; i++) {
414 sqh = ehci_alloc_sqh(sc);
419 sc->sc_islots[i].sqh = sqh;
422 for (i = 0; i < EHCI_INTRQHS; i++) {
423 if (i == EHCI_IQHIDX(lev + 1, 0))
425 sqh = sc->sc_islots[i].sqh;
427 /* The last (1ms) QH terminates. */
428 sqh->qh.qh_link = EHCI_NULL;
431 /* Otherwise the next QH has half the poll interval */
433 sc->sc_islots[EHCI_IQHIDX(lev - 1, i + 1)].sqh;
434 sqh->qh.qh_link = htole32(sqh->next->physaddr |
437 sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
438 sqh->qh.qh_endphub = htole32(EHCI_QH_SET_MULT(1));
439 sqh->qh.qh_curqtd = EHCI_NULL;
440 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
441 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
442 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
444 /* Point the frame list at the last level (128ms). */
445 for (i = 0; i < sc->sc_flsize; i++) {
446 sc->sc_flist[i] = htole32(EHCI_LINK_QH |
447 sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1,
451 /* Allocate dummy QH that starts the async list. */
452 sqh = ehci_alloc_sqh(sc);
459 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
461 htole32(sqh->physaddr | EHCI_LINK_QH);
462 sqh->qh.qh_curqtd = EHCI_NULL;
463 sqh->prev = sqh; /*It's a circular list.. */
465 /* Fill the overlay qTD */
466 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
467 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
468 sqh->qh.qh_qtd.qtd_status = htole32(0);
475 /* Point to async list */
476 sc->sc_async_head = sqh;
477 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
479 callout_init(&sc->sc_tmo_pcd, 0);
480 callout_init(&sc->sc_tmo_intrlist, 0);
482 lockinit(&sc->sc_doorbell_lock, PZERO, "ehcidb", 0, 0);
484 /* Enable interrupts */
485 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
487 /* Turn on controller */
488 EOWRITE4(sc, EHCI_USBCMD,
489 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */
490 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
495 /* Take over port ownership */
496 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
498 for (i = 0; i < 100; i++) {
499 usb_delay_ms(&sc->sc_bus, 1);
500 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
505 printf("%s: run timeout\n", device_get_nameunit(sc->sc_bus.bdev));
506 return (USBD_IOERROR);
509 return (USBD_NORMAL_COMPLETION);
513 ehci_free_sqh(sc, sc->sc_async_head);
516 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
523 ehci_softc_t *sc = v;
525 if (sc == NULL || sc->sc_dying)
528 /* If we get an interrupt while polling, then just ignore it. */
529 if (sc->sc_bus.use_polling) {
530 u_int32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
533 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
535 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
540 return (ehci_intr1(sc));
544 ehci_intr1(ehci_softc_t *sc)
546 u_int32_t intrs, eintrs;
548 DPRINTFN(20,("ehci_intr1: enter\n"));
550 /* In case the interrupt occurs before initialization has completed. */
553 printf("ehci_intr1: sc == NULL\n");
558 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
562 eintrs = intrs & sc->sc_eintrs;
563 DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
564 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
569 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
570 sc->sc_bus.intr_context++;
571 sc->sc_bus.no_intrs++;
572 if (eintrs & EHCI_STS_IAA) {
573 DPRINTF(("ehci_intr1: door bell\n"));
574 wakeup(&sc->sc_async_head);
575 eintrs &= ~EHCI_STS_IAA;
577 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
578 DPRINTFN(5,("ehci_intr1: %s %s\n",
579 eintrs & EHCI_STS_INT ? "INT" : "",
580 eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
581 usb_schedsoftintr(&sc->sc_bus);
582 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
584 if (eintrs & EHCI_STS_HSE) {
585 printf("%s: unrecoverable error, controller halted\n",
586 device_get_nameunit(sc->sc_bus.bdev));
589 if (eintrs & EHCI_STS_PCD) {
590 ehci_pcd(sc, sc->sc_intrxfer);
592 * Disable PCD interrupt for now, because it will be
593 * on until the port has been reset.
595 ehci_pcd_able(sc, 0);
596 /* Do not allow RHSC interrupts > 1 per second */
597 callout_reset(&sc->sc_tmo_pcd, hz, ehci_pcd_enable, sc);
598 eintrs &= ~EHCI_STS_PCD;
601 sc->sc_bus.intr_context--;
604 /* Block unprocessed interrupts. */
605 sc->sc_eintrs &= ~eintrs;
606 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
607 printf("%s: blocking intrs 0x%x\n",
608 device_get_nameunit(sc->sc_bus.bdev), eintrs);
615 ehci_pcd_able(ehci_softc_t *sc, int on)
617 DPRINTFN(4, ("ehci_pcd_able: on=%d\n", on));
619 sc->sc_eintrs |= EHCI_STS_PCD;
621 sc->sc_eintrs &= ~EHCI_STS_PCD;
622 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
626 ehci_pcd_enable(void *v_sc)
628 ehci_softc_t *sc = v_sc;
630 ehci_pcd_able(sc, 1);
634 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
636 usbd_pipe_handle pipe;
641 /* Just ignore the change. */
648 m = min(sc->sc_noport, xfer->length * 8 - 1);
649 memset(p, 0, xfer->length);
650 for (i = 1; i <= m; i++) {
651 /* Pick out CHANGE bits from the status reg. */
652 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
653 p[i/8] |= 1 << (i%8);
655 DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
656 xfer->actlen = xfer->length;
657 xfer->status = USBD_NORMAL_COMPLETION;
659 usb_transfer_complete(xfer);
663 ehci_softintr(void *v)
665 ehci_softc_t *sc = v;
666 struct ehci_xfer *ex, *nextex;
668 DPRINTFN(10,("%s: ehci_softintr (%d)\n", device_get_nameunit(sc->sc_bus.bdev),
669 sc->sc_bus.intr_context));
671 sc->sc_bus.intr_context++;
674 * The only explanation I can think of for why EHCI is as brain dead
675 * as UHCI interrupt-wise is that Intel was involved in both.
676 * An interrupt just tells us that something is done, we have no
677 * clue what, so we need to scan through all active transfers. :-(
679 for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
680 nextex = LIST_NEXT(ex, inext);
681 ehci_check_intr(sc, ex);
684 /* Schedule a callout to catch any dropped transactions. */
685 if ((sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) &&
686 !LIST_EMPTY(&sc->sc_intrhead))
687 callout_reset(&sc->sc_tmo_intrlist, hz / 5,
688 ehci_intrlist_timeout, sc);
690 #ifdef USB_USE_SOFTINTR
691 if (sc->sc_softwake) {
693 wakeup(&sc->sc_softwake);
695 #endif /* USB_USE_SOFTINTR */
697 sc->sc_bus.intr_context--;
700 /* Check for an interrupt. */
702 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
704 ehci_soft_qtd_t *sqtd, *lsqtd;
707 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
709 if (ex->sqtdstart == NULL) {
710 printf("ehci_check_intr: sqtdstart=NULL\n");
716 printf("ehci_check_intr: lsqtd==0\n");
721 * If the last TD is still active we need to check whether there
722 * is a an error somewhere in the middle, or whether there was a
723 * short packet (SPD and not ACTIVE).
725 if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
726 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
727 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
728 status = le32toh(sqtd->qtd.qtd_status);
729 /* If there's an active QTD the xfer isn't done. */
730 if (status & EHCI_QTD_ACTIVE)
732 /* Any kind of error makes the xfer done. */
733 if (status & EHCI_QTD_HALTED)
735 /* We want short packets, and it is short: it's done */
736 if (EHCI_QTD_GET_BYTES(status) != 0)
739 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
744 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
745 callout_stop(&ex->xfer.timeout_handle);
746 usb_rem_task(ex->xfer.pipe->device, &ex->abort_task);
751 ehci_idone(struct ehci_xfer *ex)
753 usbd_xfer_handle xfer = &ex->xfer;
754 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
755 ehci_soft_qtd_t *sqtd, *lsqtd;
756 u_int32_t status = 0, nstatus = 0;
757 ehci_physaddr_t nextphys, altnextphys;
760 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
767 printf("ehci_idone: ex is done!\n ");
770 printf("ehci_idone: ex=%p is done!\n", ex);
779 if (xfer->status == USBD_CANCELLED ||
780 xfer->status == USBD_TIMEOUT) {
781 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
786 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
788 ehci_dump_sqtds(ex->sqtdstart);
792 * Make sure that the QH overlay qTD does not reference any
793 * of the qTDs we are about to free. This is probably only
794 * necessary if the transfer is marked as HALTED.
796 nextphys = EHCI_LINK_ADDR(le32toh(epipe->sqh->qh.qh_qtd.qtd_next));
798 EHCI_LINK_ADDR(le32toh(epipe->sqh->qh.qh_qtd.qtd_altnext));
799 for (sqtd = ex->sqtdstart; sqtd != ex->sqtdend->nextqtd;
800 sqtd = sqtd->nextqtd) {
801 if (sqtd->physaddr == nextphys) {
802 epipe->sqh->qh.qh_qtd.qtd_next =
803 htole32(ex->sqtdend->nextqtd->physaddr);
804 DPRINTFN(4, ("ehci_idone: updated overlay next ptr\n"));
807 if (sqtd->physaddr == altnextphys) {
809 ("ehci_idone: updated overlay altnext ptr\n"));
810 epipe->sqh->qh.qh_qtd.qtd_altnext =
811 htole32(ex->sqtdend->nextqtd->physaddr);
815 /* The transfer is done, compute actual length and status. */
818 for (sqtd = ex->sqtdstart; sqtd != lsqtd->nextqtd; sqtd=sqtd->nextqtd) {
819 nstatus = le32toh(sqtd->qtd.qtd_status);
820 if (nstatus & EHCI_QTD_ACTIVE)
824 /* halt is ok if descriptor is last, and complete */
825 if (sqtd == lsqtd && EHCI_QTD_GET_BYTES(status) == 0)
826 status &= ~EHCI_QTD_HALTED;
827 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
828 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
831 cerr = EHCI_QTD_GET_CERR(status);
832 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, cerr=%d, "
833 "status=0x%x\n", xfer->length, actlen, cerr, status));
834 xfer->actlen = actlen;
835 if ((status & EHCI_QTD_HALTED) != 0) {
839 bitmask_snprintf((u_int32_t)status,
840 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR"
841 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
844 ("ehci_idone: error, addr=%d, endpt=0x%02x, "
846 xfer->pipe->device->address,
847 xfer->pipe->endpoint->edesc->bEndpointAddress,
850 ehci_dump_sqh(epipe->sqh);
851 ehci_dump_sqtds(ex->sqtdstart);
854 if ((status & EHCI_QTD_BABBLE) == 0 && cerr > 0)
855 xfer->status = USBD_STALLED;
857 xfer->status = USBD_IOERROR; /* more info XXX */
859 xfer->status = USBD_NORMAL_COMPLETION;
862 usb_transfer_complete(xfer);
863 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
867 * Wait here until controller claims to have an interrupt.
868 * Then call ehci_intr and return. Use timeout to avoid waiting
872 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
874 int timo = xfer->timeout;
878 xfer->status = USBD_IN_PROGRESS;
879 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
880 usb_delay_ms(&sc->sc_bus, 1);
883 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
885 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
892 if (xfer->status != USBD_IN_PROGRESS)
898 DPRINTF(("ehci_waitintr: timeout\n"));
899 xfer->status = USBD_TIMEOUT;
900 usb_transfer_complete(xfer);
901 /* XXX should free TD */
905 ehci_poll(struct usbd_bus *bus)
907 ehci_softc_t *sc = (ehci_softc_t *)bus;
911 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
913 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
918 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
923 ehci_detach(struct ehci_softc *sc, int flags)
929 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
930 EOWRITE4(sc, EHCI_USBCMD, 0);
931 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
932 callout_stop(&sc->sc_tmo_intrlist);
933 callout_stop(&sc->sc_tmo_pcd);
935 #if defined(__NetBSD__) || defined(__OpenBSD__)
936 if (sc->sc_powerhook != NULL)
937 powerhook_disestablish(sc->sc_powerhook);
938 if (sc->sc_shutdownhook != NULL)
939 shutdownhook_disestablish(sc->sc_shutdownhook);
941 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
943 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
944 /* XXX free other data structures XXX */
950 * Handle suspend/resume.
952 * We need to switch to polling mode here, because this routine is
953 * called from an interrupt context. This is all right since we
954 * are almost suspended anyway.
957 ehci_power(int why, void *v)
959 ehci_softc_t *sc = v;
964 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
973 sc->sc_bus.use_polling++;
975 for (i = 1; i <= sc->sc_noport; i++) {
976 cmd = EOREAD4(sc, EHCI_PORTSC(i));
977 if ((cmd & EHCI_PS_PO) == 0 &&
978 (cmd & EHCI_PS_PE) == EHCI_PS_PE)
979 EOWRITE4(sc, EHCI_PORTSC(i),
983 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
985 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
986 EOWRITE4(sc, EHCI_USBCMD, cmd);
988 for (i = 0; i < 100; i++) {
989 hcr = EOREAD4(sc, EHCI_USBSTS) &
990 (EHCI_STS_ASS | EHCI_STS_PSS);
994 usb_delay_ms(&sc->sc_bus, 1);
997 printf("%s: reset timeout\n",
998 device_get_nameunit(sc->sc_bus.bdev));
1001 cmd &= ~EHCI_CMD_RS;
1002 EOWRITE4(sc, EHCI_USBCMD, cmd);
1004 for (i = 0; i < 100; i++) {
1005 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1006 if (hcr == EHCI_STS_HCH)
1009 usb_delay_ms(&sc->sc_bus, 1);
1011 if (hcr != EHCI_STS_HCH) {
1012 printf("%s: config timeout\n",
1013 device_get_nameunit(sc->sc_bus.bdev));
1016 sc->sc_bus.use_polling--;
1020 sc->sc_bus.use_polling++;
1022 /* restore things in case the bios sucks */
1023 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
1024 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
1025 EOWRITE4(sc, EHCI_ASYNCLISTADDR,
1026 sc->sc_async_head->physaddr | EHCI_LINK_QH);
1027 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1030 for (i = 1; i <= sc->sc_noport; i++) {
1031 cmd = EOREAD4(sc, EHCI_PORTSC(i));
1032 if ((cmd & EHCI_PS_PO) == 0 &&
1033 (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP) {
1034 EOWRITE4(sc, EHCI_PORTSC(i),
1041 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1043 for (i = 1; i <= sc->sc_noport; i++) {
1044 cmd = EOREAD4(sc, EHCI_PORTSC(i));
1045 if ((cmd & EHCI_PS_PO) == 0 &&
1046 (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)
1047 EOWRITE4(sc, EHCI_PORTSC(i),
1048 cmd & ~EHCI_PS_FPR);
1052 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
1054 for (i = 0; i < 100; i++) {
1055 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1056 if (hcr != EHCI_STS_HCH)
1059 usb_delay_ms(&sc->sc_bus, 1);
1061 if (hcr == EHCI_STS_HCH) {
1062 printf("%s: config timeout\n",
1063 device_get_nameunit(sc->sc_bus.bdev));
1066 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1068 sc->sc_bus.use_polling--;
1070 case PWR_SOFTSUSPEND:
1071 case PWR_SOFTSTANDBY:
1072 case PWR_SOFTRESUME:
1078 DPRINTF(("ehci_power: sc=%p\n", sc));
1085 * Shut down the controller when the system is going down.
1088 ehci_shutdown(void *v)
1090 ehci_softc_t *sc = v;
1092 DPRINTF(("ehci_shutdown: stopping the HC\n"));
1093 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
1094 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1098 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
1102 err = usb_allocmem(bus, size, 0, dma);
1105 printf("ehci_allocm: usb_allocmem()=%d\n", err);
1111 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
1113 usb_freemem(bus, dma);
1117 ehci_allocx(struct usbd_bus *bus)
1119 struct ehci_softc *sc = (struct ehci_softc *)bus;
1120 usbd_xfer_handle xfer;
1122 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
1124 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
1126 if (xfer->busy_free != XFER_FREE) {
1127 printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer,
1132 xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_NOWAIT);
1135 memset(xfer, 0, sizeof(struct ehci_xfer));
1136 usb_init_task(&EXFER(xfer)->abort_task, ehci_timeout_task,
1138 EXFER(xfer)->ehci_xfer_flags = 0;
1140 EXFER(xfer)->isdone = 1;
1141 xfer->busy_free = XFER_BUSY;
1148 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1150 struct ehci_softc *sc = (struct ehci_softc *)bus;
1153 if (xfer->busy_free != XFER_BUSY) {
1154 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
1158 xfer->busy_free = XFER_FREE;
1159 if (!EXFER(xfer)->isdone) {
1160 printf("ehci_freex: !isdone\n");
1164 STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
1168 ehci_device_clear_toggle(usbd_pipe_handle pipe)
1170 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1172 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
1173 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
1176 usbd_dump_pipe(pipe);
1178 KASSERT((epipe->sqh->qh.qh_qtd.qtd_status &
1179 htole32(EHCI_QTD_ACTIVE)) == 0,
1180 ("ehci_device_clear_toggle: queue active"));
1181 epipe->sqh->qh.qh_qtd.qtd_status &= htole32(~EHCI_QTD_TOGGLE_MASK);
1185 ehci_noop(usbd_pipe_handle pipe)
1191 ehci_dump_regs(ehci_softc_t *sc)
1194 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1195 EOREAD4(sc, EHCI_USBCMD),
1196 EOREAD4(sc, EHCI_USBSTS),
1197 EOREAD4(sc, EHCI_USBINTR));
1198 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1199 EOREAD4(sc, EHCI_FRINDEX),
1200 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1201 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1202 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1203 for (i = 1; i <= sc->sc_noport; i++)
1204 printf("port %d status=0x%08x\n", i,
1205 EOREAD4(sc, EHCI_PORTSC(i)));
1209 * Unused function - this is meant to be called from a kernel
1215 ehci_dump_regs(theehci);
1219 ehci_dump_link(ehci_link_t link, int type)
1221 link = le32toh(link);
1222 printf("0x%08x", link);
1223 if (link & EHCI_LINK_TERMINATE)
1228 switch (EHCI_LINK_TYPE(link)) {
1229 case EHCI_LINK_ITD: printf("ITD"); break;
1230 case EHCI_LINK_QH: printf("QH"); break;
1231 case EHCI_LINK_SITD: printf("SITD"); break;
1232 case EHCI_LINK_FSTN: printf("FSTN"); break;
1240 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1246 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1247 ehci_dump_sqtd(sqtd);
1248 stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE);
1251 printf("dump aborted, too many TDs\n");
1255 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1257 printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr);
1258 ehci_dump_qtd(&sqtd->qtd);
1262 ehci_dump_qtd(ehci_qtd_t *qtd)
1267 printf(" next="); ehci_dump_link(qtd->qtd_next, 0);
1268 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
1270 s = le32toh(qtd->qtd_status);
1271 bitmask_snprintf(EHCI_QTD_GET_STATUS(s),
1272 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1273 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
1274 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
1275 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
1276 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
1277 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
1278 EHCI_QTD_GET_PID(s), sbuf);
1279 for (s = 0; s < 5; s++)
1280 printf(" buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
1284 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1286 ehci_qh_t *qh = &sqh->qh;
1287 u_int32_t endp, endphub;
1289 printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr);
1290 printf(" sqtd=%p inactivesqtd=%p\n", sqh->sqtd, sqh->inactivesqtd);
1291 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
1292 endp = le32toh(qh->qh_endp);
1293 printf(" endp=0x%08x\n", endp);
1294 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
1295 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1296 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
1297 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
1298 printf(" mpl=0x%x ctl=%d nrl=%d\n",
1299 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
1300 EHCI_QH_GET_NRL(endp));
1301 endphub = le32toh(qh->qh_endphub);
1302 printf(" endphub=0x%08x\n", endphub);
1303 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
1304 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
1305 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1306 EHCI_QH_GET_MULT(endphub));
1307 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
1308 printf("Overlay qTD:\n");
1309 ehci_dump_qtd(&qh->qh_qtd);
1314 ehci_dump_exfer(struct ehci_xfer *ex)
1316 printf("ehci_dump_exfer: ex=%p\n", ex);
1322 ehci_open(usbd_pipe_handle pipe)
1324 usbd_device_handle dev = pipe->device;
1325 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
1326 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1327 u_int8_t addr = dev->address;
1328 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1329 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1330 ehci_soft_qh_t *sqh;
1333 int ival, speed, naks;
1334 int hshubaddr, hshubport;
1336 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1337 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1339 if (dev->myhsport) {
1340 hshubaddr = dev->myhsport->parent->address;
1341 hshubport = dev->myhsport->portno;
1348 return (USBD_IOERROR);
1350 if (addr == sc->sc_addr) {
1351 switch (ed->bEndpointAddress) {
1352 case USB_CONTROL_ENDPOINT:
1353 pipe->methods = &ehci_root_ctrl_methods;
1355 case UE_DIR_IN | EHCI_INTR_ENDPT:
1356 pipe->methods = &ehci_root_intr_methods;
1359 return (USBD_INVAL);
1361 return (USBD_NORMAL_COMPLETION);
1364 /* XXX All this stuff is only valid for async. */
1365 switch (dev->speed) {
1366 case USB_SPEED_LOW: speed = EHCI_QH_SPEED_LOW; break;
1367 case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
1368 case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
1369 default: panic("ehci_open: bad device speed %d", dev->speed);
1371 if (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_ISOCHRONOUS) {
1372 printf("%s: *** WARNING: opening low/full speed device, this "
1373 "does not work yet.\n",
1374 device_get_nameunit(sc->sc_bus.bdev));
1375 DPRINTFN(1,("ehci_open: hshubaddr=%d hshubport=%d\n",
1376 hshubaddr, hshubport));
1381 sqh = ehci_alloc_sqh(sc);
1384 /* qh_link filled when the QH is added */
1385 sqh->qh.qh_endp = htole32(
1386 EHCI_QH_SET_ADDR(addr) |
1387 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
1388 EHCI_QH_SET_EPS(speed) |
1389 (xfertype == UE_CONTROL ? EHCI_QH_DTC : 0) |
1390 EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
1391 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
1393 EHCI_QH_SET_NRL(naks)
1395 sqh->qh.qh_endphub = htole32(
1396 EHCI_QH_SET_MULT(1) |
1397 EHCI_QH_SET_HUBA(hshubaddr) |
1398 EHCI_QH_SET_PORT(hshubport) |
1399 EHCI_QH_SET_CMASK(0x1c) |
1400 EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x01 : 0)
1402 sqh->qh.qh_curqtd = EHCI_NULL;
1403 /* The overlay qTD was already set up by ehci_alloc_sqh(). */
1404 sqh->qh.qh_qtd.qtd_status =
1405 htole32(EHCI_QTD_SET_TOGGLE(pipe->endpoint->savedtoggle));
1411 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
1412 0, &epipe->u.ctl.reqdma);
1415 printf("ehci_open: usb_allocmem()=%d\n", err);
1419 pipe->methods = &ehci_device_ctrl_methods;
1421 ehci_add_qh(sqh, sc->sc_async_head);
1425 pipe->methods = &ehci_device_bulk_methods;
1427 ehci_add_qh(sqh, sc->sc_async_head);
1431 pipe->methods = &ehci_device_intr_methods;
1432 ival = pipe->interval;
1433 if (ival == USBD_DEFAULT_INTERVAL)
1434 ival = ed->bInterval;
1435 return (ehci_device_setintr(sc, sqh, ival));
1436 case UE_ISOCHRONOUS:
1437 pipe->methods = &ehci_device_isoc_methods;
1438 return (USBD_INVAL);
1440 return (USBD_INVAL);
1442 return (USBD_NORMAL_COMPLETION);
1445 ehci_free_sqh(sc, sqh);
1447 return (USBD_NOMEM);
1451 * Add an ED to the schedule. Called at splusb().
1452 * If in the async schedule, it will always have a next.
1453 * If in the intr schedule it may not.
1456 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1460 sqh->next = head->next;
1462 sqh->qh.qh_link = head->qh.qh_link;
1465 sqh->next->prev = sqh;
1466 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
1469 if (ehcidebug > 5) {
1470 printf("ehci_add_qh:\n");
1477 * Remove an ED from the schedule. Called at splusb().
1478 * Will always have a 'next' if it's in the async list as it's circular.
1481 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1485 sqh->prev->qh.qh_link = sqh->qh.qh_link;
1486 sqh->prev->next = sqh->next;
1488 sqh->next->prev = sqh->prev;
1492 /* Restart a QH following the addition of a qTD. */
1494 ehci_activate_qh(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
1496 KASSERT((sqtd->qtd.qtd_status & htole32(EHCI_QTD_ACTIVE)) == 0,
1497 ("ehci_activate_qh: already active"));
1500 * When a QH is idle, the overlay qTD should be marked as not
1501 * halted and not active. This causes the host controller to
1502 * retrieve the real qTD on each pass (rather than just examinig
1503 * the overlay), so it will notice when we activate the qTD.
1505 if (sqtd == sqh->sqtd) {
1506 /* Check that the hardware is in the state we expect. */
1507 if (EHCI_LINK_ADDR(le32toh(sqh->qh.qh_qtd.qtd_next)) !=
1510 printf("ehci_activate_qh: unexpected next ptr\n");
1512 ehci_dump_sqtds(sqh->sqtd);
1514 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
1515 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
1517 /* Ensure the flags are correct. */
1518 sqh->qh.qh_qtd.qtd_status &= htole32(EHCI_QTD_PINGSTATE |
1519 EHCI_QTD_TOGGLE_MASK);
1522 /* Now activate the qTD. */
1523 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_ACTIVE);
1527 * Ensure that the HC has released all references to the QH. We do this
1528 * by asking for a Async Advance Doorbell interrupt and then we wait for
1530 * To make this easier we first obtain exclusive use of the doorbell.
1533 ehci_sync_hc(ehci_softc_t *sc)
1538 DPRINTFN(2,("ehci_sync_hc: dying\n"));
1541 DPRINTFN(2,("ehci_sync_hc: enter\n"));
1543 lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE, NULL, NULL);
1545 /* ask for doorbell */
1546 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
1547 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1548 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1549 error = tsleep(&sc->sc_async_head, PZERO, "ehcidi", hz); /* bell wait */
1550 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1551 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1553 /* release doorbell */
1554 lockmgr(&sc->sc_doorbell_lock, LK_RELEASE, NULL, NULL);
1557 printf("ehci_sync_hc: tsleep() = %d\n", error);
1559 DPRINTFN(2,("ehci_sync_hc: exit\n"));
1565 * Data structures and routines to emulate the root hub.
1567 static usb_device_descriptor_t ehci_devd = {
1568 USB_DEVICE_DESCRIPTOR_SIZE,
1569 UDESC_DEVICE, /* type */
1570 {0x00, 0x02}, /* USB version */
1571 UDCLASS_HUB, /* class */
1572 UDSUBCLASS_HUB, /* subclass */
1573 UDPROTO_HSHUBSTT, /* protocol */
1574 64, /* max packet */
1575 {0},{0},{0x00,0x01}, /* device id */
1576 1,2,0, /* string indicies */
1577 1 /* # of configurations */
1580 static usb_device_qualifier_t ehci_odevd = {
1581 USB_DEVICE_DESCRIPTOR_SIZE,
1582 UDESC_DEVICE_QUALIFIER, /* type */
1583 {0x00, 0x02}, /* USB version */
1584 UDCLASS_HUB, /* class */
1585 UDSUBCLASS_HUB, /* subclass */
1586 UDPROTO_FSHUB, /* protocol */
1587 64, /* max packet */
1588 1, /* # of configurations */
1592 static usb_config_descriptor_t ehci_confd = {
1593 USB_CONFIG_DESCRIPTOR_SIZE,
1595 {USB_CONFIG_DESCRIPTOR_SIZE +
1596 USB_INTERFACE_DESCRIPTOR_SIZE +
1597 USB_ENDPOINT_DESCRIPTOR_SIZE},
1605 static usb_interface_descriptor_t ehci_ifcd = {
1606 USB_INTERFACE_DESCRIPTOR_SIZE,
1617 static usb_endpoint_descriptor_t ehci_endpd = {
1618 USB_ENDPOINT_DESCRIPTOR_SIZE,
1620 UE_DIR_IN | EHCI_INTR_ENDPT,
1622 {8, 0}, /* max packet */
1626 static usb_hub_descriptor_t ehci_hubd = {
1627 USB_HUB_DESCRIPTOR_SIZE,
1637 ehci_str(usb_string_descriptor_t *p, int l, char *s)
1643 p->bLength = 2 * strlen(s) + 2;
1646 p->bDescriptorType = UDESC_STRING;
1648 for (i = 0; s[i] && l > 1; i++, l -= 2)
1649 USETW2(p->bString[i], 0, s[i]);
1654 * Simulate a hardware hub by handling all the necessary requests.
1657 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
1661 /* Insert last in queue. */
1662 err = usb_insert_transfer(xfer);
1666 /* Pipe isn't running, start first */
1667 return (ehci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
1671 ehci_root_ctrl_start(usbd_xfer_handle xfer)
1673 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
1674 usb_device_request_t *req;
1677 int s, len, value, index, l, totlen = 0;
1678 usb_port_status_t ps;
1679 usb_hub_descriptor_t hubd;
1684 return (USBD_IOERROR);
1687 if (!(xfer->rqflags & URQ_REQUEST))
1689 return (USBD_INVAL);
1691 req = &xfer->request;
1693 DPRINTFN(4,("ehci_root_ctrl_start: type=0x%02x request=%02x\n",
1694 req->bmRequestType, req->bRequest));
1696 len = UGETW(req->wLength);
1697 value = UGETW(req->wValue);
1698 index = UGETW(req->wIndex);
1703 #define C(x,y) ((x) | ((y) << 8))
1704 switch(C(req->bRequest, req->bmRequestType)) {
1705 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1706 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1707 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1709 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1710 * for the integrated root hub.
1713 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1715 *(u_int8_t *)buf = sc->sc_conf;
1719 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1720 DPRINTFN(8,("ehci_root_ctrl_start: wValue=0x%04x\n", value));
1721 switch(value >> 8) {
1723 if ((value & 0xff) != 0) {
1727 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1728 USETW(ehci_devd.idVendor, sc->sc_id_vendor);
1729 memcpy(buf, &ehci_devd, l);
1732 * We can't really operate at another speed, but the spec says
1733 * we need this descriptor.
1735 case UDESC_DEVICE_QUALIFIER:
1736 if ((value & 0xff) != 0) {
1740 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1741 memcpy(buf, &ehci_odevd, l);
1744 * We can't really operate at another speed, but the spec says
1745 * we need this descriptor.
1747 case UDESC_OTHER_SPEED_CONFIGURATION:
1749 if ((value & 0xff) != 0) {
1753 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
1754 memcpy(buf, &ehci_confd, l);
1755 ((usb_config_descriptor_t *)buf)->bDescriptorType =
1757 buf = (char *)buf + l;
1759 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
1761 memcpy(buf, &ehci_ifcd, l);
1762 buf = (char *)buf + l;
1764 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
1766 memcpy(buf, &ehci_endpd, l);
1771 *(u_int8_t *)buf = 0;
1773 switch (value & 0xff) {
1774 case 0: /* Language table */
1775 totlen = ehci_str(buf, len, "\001");
1777 case 1: /* Vendor */
1778 totlen = ehci_str(buf, len, sc->sc_vendor);
1780 case 2: /* Product */
1781 totlen = ehci_str(buf, len, "EHCI root hub");
1790 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
1792 *(u_int8_t *)buf = 0;
1796 case C(UR_GET_STATUS, UT_READ_DEVICE):
1798 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
1802 case C(UR_GET_STATUS, UT_READ_INTERFACE):
1803 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
1805 USETW(((usb_status_t *)buf)->wStatus, 0);
1809 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
1810 if (value >= USB_MAX_DEVICES) {
1814 sc->sc_addr = value;
1816 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
1817 if (value != 0 && value != 1) {
1821 sc->sc_conf = value;
1823 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
1825 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
1826 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
1827 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
1830 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
1832 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
1835 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
1837 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
1838 DPRINTFN(8, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
1839 "port=%d feature=%d\n",
1841 if (index < 1 || index > sc->sc_noport) {
1845 port = EHCI_PORTSC(index);
1846 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1848 case UHF_PORT_ENABLE:
1849 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
1851 case UHF_PORT_SUSPEND:
1852 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
1854 case UHF_PORT_POWER:
1855 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
1858 DPRINTFN(2,("ehci_root_ctrl_start: clear port test "
1861 case UHF_PORT_INDICATOR:
1862 DPRINTFN(2,("ehci_root_ctrl_start: clear port ind "
1864 EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
1866 case UHF_C_PORT_CONNECTION:
1867 EOWRITE4(sc, port, v | EHCI_PS_CSC);
1869 case UHF_C_PORT_ENABLE:
1870 EOWRITE4(sc, port, v | EHCI_PS_PEC);
1872 case UHF_C_PORT_SUSPEND:
1875 case UHF_C_PORT_OVER_CURRENT:
1876 EOWRITE4(sc, port, v | EHCI_PS_OCC);
1878 case UHF_C_PORT_RESET:
1887 case UHF_C_PORT_CONNECTION:
1888 case UHF_C_PORT_ENABLE:
1889 case UHF_C_PORT_SUSPEND:
1890 case UHF_C_PORT_OVER_CURRENT:
1891 case UHF_C_PORT_RESET:
1892 /* Enable RHSC interrupt if condition is cleared. */
1893 if ((OREAD4(sc, port) >> 16) == 0)
1894 ehci_pcd_able(sc, 1);
1901 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
1902 if ((value & 0xff) != 0) {
1907 hubd.bNbrPorts = sc->sc_noport;
1908 v = EOREAD4(sc, EHCI_HCSPARAMS);
1909 USETW(hubd.wHubCharacteristics,
1910 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
1911 EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
1912 ? UHD_PORT_IND : 0);
1913 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
1914 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
1915 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
1916 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
1917 l = min(len, hubd.bDescLength);
1919 memcpy(buf, &hubd, l);
1921 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
1926 memset(buf, 0, len); /* ? XXX */
1929 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
1930 DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n",
1932 if (index < 1 || index > sc->sc_noport) {
1940 v = EOREAD4(sc, EHCI_PORTSC(index));
1941 DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n",
1944 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
1945 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
1946 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
1947 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
1948 if (v & EHCI_PS_PR) i |= UPS_RESET;
1949 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
1950 USETW(ps.wPortStatus, i);
1952 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
1953 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
1954 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
1955 if (sc->sc_isreset) i |= UPS_C_PORT_RESET;
1956 USETW(ps.wPortChange, i);
1957 l = min(len, sizeof ps);
1958 memcpy(buf, &ps, l);
1961 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
1964 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
1966 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
1967 if (index < 1 || index > sc->sc_noport) {
1971 port = EHCI_PORTSC(index);
1972 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1974 case UHF_PORT_ENABLE:
1975 EOWRITE4(sc, port, v | EHCI_PS_PE);
1977 case UHF_PORT_SUSPEND:
1978 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
1980 case UHF_PORT_RESET:
1981 DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n",
1983 if (EHCI_PS_IS_LOWSPEED(v)) {
1984 /* Low speed device, give up ownership. */
1985 ehci_disown(sc, index, 1);
1988 /* Start reset sequence. */
1989 v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
1990 EOWRITE4(sc, port, v | EHCI_PS_PR);
1991 /* Wait for reset to complete. */
1992 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
1997 /* Terminate reset sequence. */
1998 EOWRITE4(sc, port, v);
1999 /* Wait for HC to complete reset. */
2000 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE);
2005 v = EOREAD4(sc, port);
2006 DPRINTF(("ehci after reset, status=0x%08x\n", v));
2007 if (v & EHCI_PS_PR) {
2008 printf("%s: port reset timeout\n",
2009 device_get_nameunit(sc->sc_bus.bdev));
2010 return (USBD_TIMEOUT);
2012 if (!(v & EHCI_PS_PE)) {
2013 /* Not a high speed device, give up ownership.*/
2014 ehci_disown(sc, index, 0);
2018 DPRINTF(("ehci port %d reset, status = 0x%08x\n",
2021 case UHF_PORT_POWER:
2022 DPRINTFN(2,("ehci_root_ctrl_start: set port power "
2024 EOWRITE4(sc, port, v | EHCI_PS_PP);
2027 DPRINTFN(2,("ehci_root_ctrl_start: set port test "
2030 case UHF_PORT_INDICATOR:
2031 DPRINTFN(2,("ehci_root_ctrl_start: set port ind "
2033 EOWRITE4(sc, port, v | EHCI_PS_PIC);
2040 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2041 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2042 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2043 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2049 xfer->actlen = totlen;
2050 err = USBD_NORMAL_COMPLETION;
2054 usb_transfer_complete(xfer);
2056 return (USBD_IN_PROGRESS);
2060 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
2065 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
2067 if (sc->sc_npcomp != 0) {
2068 int i = (index-1) / sc->sc_npcomp;
2069 if (i >= sc->sc_ncomp)
2070 printf("%s: strange port\n",
2071 device_get_nameunit(sc->sc_bus.bdev));
2073 printf("%s: handing over %s speed device on "
2075 device_get_nameunit(sc->sc_bus.bdev),
2076 lowspeed ? "low" : "full",
2077 index, device_get_nameunit(sc->sc_comps[i]->bdev));
2079 printf("%s: npcomp == 0\n", device_get_nameunit(sc->sc_bus.bdev));
2082 port = EHCI_PORTSC(index);
2083 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
2084 EOWRITE4(sc, port, v | EHCI_PS_PO);
2087 /* Abort a root control request. */
2089 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
2091 /* Nothing to do, all transfers are synchronous. */
2094 /* Close the root pipe. */
2096 ehci_root_ctrl_close(usbd_pipe_handle pipe)
2098 DPRINTF(("ehci_root_ctrl_close\n"));
2099 /* Nothing to do. */
2103 ehci_root_intr_done(usbd_xfer_handle xfer)
2108 ehci_root_intr_transfer(usbd_xfer_handle xfer)
2112 /* Insert last in queue. */
2113 err = usb_insert_transfer(xfer);
2117 /* Pipe isn't running, start first */
2118 return (ehci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
2122 ehci_root_intr_start(usbd_xfer_handle xfer)
2124 usbd_pipe_handle pipe = xfer->pipe;
2125 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2128 return (USBD_IOERROR);
2130 sc->sc_intrxfer = xfer;
2132 return (USBD_IN_PROGRESS);
2135 /* Abort a root interrupt request. */
2137 ehci_root_intr_abort(usbd_xfer_handle xfer)
2141 if (xfer->pipe->intrxfer == xfer) {
2142 DPRINTF(("ehci_root_intr_abort: remove\n"));
2143 xfer->pipe->intrxfer = NULL;
2145 xfer->status = USBD_CANCELLED;
2147 usb_transfer_complete(xfer);
2151 /* Close the root pipe. */
2153 ehci_root_intr_close(usbd_pipe_handle pipe)
2155 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2157 DPRINTF(("ehci_root_intr_close\n"));
2159 sc->sc_intrxfer = NULL;
2163 ehci_root_ctrl_done(usbd_xfer_handle xfer)
2167 /************************/
2170 ehci_alloc_sqh(ehci_softc_t *sc)
2172 ehci_soft_qh_t *sqh;
2173 ehci_soft_qtd_t *sqtd;
2178 if (sc->sc_freeqhs == NULL) {
2179 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
2180 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
2181 EHCI_PAGE_SIZE, &dma);
2184 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2188 for(i = 0; i < EHCI_SQH_CHUNK; i++) {
2189 offs = i * EHCI_SQH_SIZE;
2190 sqh = KERNADDR(&dma, offs);
2191 sqh->physaddr = DMAADDR(&dma, offs);
2192 sqh->next = sc->sc_freeqhs;
2193 sc->sc_freeqhs = sqh;
2196 /* Allocate the initial inactive sqtd. */
2197 sqtd = ehci_alloc_sqtd(sc);
2200 sqtd->qtd.qtd_status = htole32(0);
2201 sqtd->qtd.qtd_next = EHCI_NULL;
2202 sqtd->qtd.qtd_altnext = EHCI_NULL;
2204 sqh = sc->sc_freeqhs;
2205 sc->sc_freeqhs = sqh->next;
2207 /* The overlay QTD should begin zeroed. */
2208 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
2209 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
2210 sqh->qh.qh_qtd.qtd_status = 0;
2211 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
2212 sqh->qh.qh_qtd.qtd_buffer[i] = 0;
2213 sqh->qh.qh_qtd.qtd_buffer_hi[i] = 0;
2218 sqh->inactivesqtd = sqtd;
2223 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2225 ehci_free_sqtd(sc, sqh->inactivesqtd);
2226 sqh->next = sc->sc_freeqhs;
2227 sc->sc_freeqhs = sqh;
2231 ehci_alloc_sqtd(ehci_softc_t *sc)
2233 ehci_soft_qtd_t *sqtd;
2239 if (sc->sc_freeqtds == NULL) {
2240 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
2241 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
2242 EHCI_PAGE_SIZE, &dma);
2245 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
2250 for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
2251 offs = i * EHCI_SQTD_SIZE;
2252 sqtd = KERNADDR(&dma, offs);
2253 sqtd->physaddr = DMAADDR(&dma, offs);
2254 sqtd->nextqtd = sc->sc_freeqtds;
2255 sc->sc_freeqtds = sqtd;
2261 sqtd = sc->sc_freeqtds;
2262 sc->sc_freeqtds = sqtd->nextqtd;
2263 sqtd->qtd.qtd_next = EHCI_NULL;
2264 sqtd->qtd.qtd_altnext = EHCI_NULL;
2265 sqtd->qtd.qtd_status = 0;
2266 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
2267 sqtd->qtd.qtd_buffer[i] = 0;
2268 sqtd->qtd.qtd_buffer_hi[i] = 0;
2270 sqtd->nextqtd = NULL;
2278 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2283 sqtd->nextqtd = sc->sc_freeqtds;
2284 sc->sc_freeqtds = sqtd;
2289 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
2290 int alen, int rd, usbd_xfer_handle xfer, ehci_soft_qtd_t *start,
2291 ehci_soft_qtd_t *newinactive, ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
2293 ehci_soft_qtd_t *next, *cur;
2294 ehci_physaddr_t dataphys, nextphys;
2295 u_int32_t qtdstatus;
2296 int adj, len, curlen, mps, offset, pagelen, seg, segoff;
2297 int i, iscontrol, forceshort;
2298 struct usb_dma_mapping *dma = &xfer->dmamap;
2300 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
2304 iscontrol = (epipe->pipe.endpoint->edesc->bmAttributes & UE_XFERTYPE) ==
2306 qtdstatus = EHCI_QTD_ACTIVE |
2307 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2308 EHCI_QTD_SET_CERR(3)
2310 /* BYTES set below */
2312 mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
2313 forceshort = ((xfer->flags & USBD_FORCE_SHORT_XFER) || len == 0) &&
2316 * The control transfer data stage always starts with a toggle of 1.
2317 * For other transfers we let the hardware track the toggle state.
2320 qtdstatus |= EHCI_QTD_SET_TOGGLE(1);
2322 if (start != NULL) {
2324 * If we are given a starting qTD, assume it is linked into
2325 * an active QH so be careful not to mark it active.
2329 qtdstatus &= ~EHCI_QTD_ACTIVE;
2331 cur = ehci_alloc_sqtd(sc);
2341 /* The EHCI hardware can handle at most 5 pages. */
2342 for (i = 0; i < EHCI_QTD_NBUFFERS && curlen < len; i++) {
2343 KASSERT(seg < dma->nsegs,
2344 ("ehci_alloc_sqtd_chain: overrun"));
2345 dataphys = dma->segs[seg].ds_addr + segoff;
2346 pagelen = dma->segs[seg].ds_len - segoff;
2347 if (pagelen > len - curlen)
2348 pagelen = len - curlen;
2349 if (pagelen > EHCI_PAGE_SIZE -
2350 EHCI_PAGE_OFFSET(dataphys))
2351 pagelen = EHCI_PAGE_SIZE -
2352 EHCI_PAGE_OFFSET(dataphys);
2354 if (segoff >= dma->segs[seg].ds_len) {
2355 KASSERT(segoff == dma->segs[seg].ds_len,
2356 ("ehci_alloc_sqtd_chain: overlap"));
2361 cur->qtd.qtd_buffer[i] = htole32(dataphys);
2362 cur->qtd.qtd_buffer_hi[i] = 0;
2366 * Must stop if there is any gap before or after
2367 * the page boundary.
2369 if (EHCI_PAGE_OFFSET(dataphys + pagelen) != 0)
2371 if (seg < dma->nsegs && EHCI_PAGE_OFFSET(segoff +
2372 dma->segs[seg].ds_addr) != 0)
2375 /* Adjust down to a multiple of mps if not at the end. */
2376 if (curlen < len && curlen % mps != 0) {
2380 ("ehci_alloc_sqtd_chain: need to copy"));
2384 segoff += dma->segs[seg].ds_len;
2386 KASSERT(seg >= 0 && segoff >= 0,
2387 ("ehci_alloc_sqtd_chain: adjust to mps"));
2392 if (len != 0 || forceshort) {
2393 next = ehci_alloc_sqtd(sc);
2396 nextphys = htole32(next->physaddr);
2399 nextphys = EHCI_NULL;
2402 cur->nextqtd = next;
2403 cur->qtd.qtd_next = nextphys;
2404 /* Make sure to stop after a short transfer. */
2405 cur->qtd.qtd_altnext = htole32(newinactive->physaddr);
2406 cur->qtd.qtd_status =
2407 htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
2410 DPRINTFN(10,("ehci_alloc_sqtd_chain: curlen=%d\n", curlen));
2413 * adjust the toggle based on the number of packets
2416 if ((((curlen + mps - 1) / mps) & 1) || curlen == 0)
2417 qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
2419 qtdstatus |= EHCI_QTD_ACTIVE;
2425 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
2429 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
2432 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
2435 return (USBD_NORMAL_COMPLETION);
2438 /* XXX free chain */
2439 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
2440 return (USBD_NOMEM);
2443 /* Free the chain starting at sqtd and end at the qTD before sqtdend */
2445 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qh_t *sqh,
2446 ehci_soft_qtd_t *sqtd, ehci_soft_qtd_t *sqtdend)
2448 ehci_soft_qtd_t *p, **prevp;
2451 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
2454 /* First unlink the chain from the QH's software qTD list. */
2456 for (p = sqh->sqtd; p != NULL; p = p->nextqtd) {
2461 prevp = &p->nextqtd;
2463 KASSERT(p != NULL, ("ehci_free_sqtd_chain: chain not found"));
2464 for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
2466 ehci_free_sqtd(sc, sqtd);
2473 * Close a reqular pipe.
2474 * Assumes that there are no pending transactions.
2477 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
2479 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
2480 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2481 ehci_soft_qh_t *sqh = epipe->sqh;
2485 ehci_rem_qh(sc, sqh, head);
2487 pipe->endpoint->savedtoggle =
2488 EHCI_QTD_GET_TOGGLE(le32toh(sqh->qh.qh_qtd.qtd_status));
2489 ehci_free_sqh(sc, epipe->sqh);
2493 * Abort a device request.
2494 * If this routine is called at splusb() it guarantees that the request
2495 * will be removed from the hardware scheduling and that the callback
2496 * for it will be called with USBD_CANCELLED status.
2497 * It's impossible to guarantee that the requested transfer will not
2498 * have happened since the hardware runs concurrently.
2499 * If the transaction has already happened we rely on the ordinary
2500 * interrupt processing to process it.
2503 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2505 #define exfer EXFER(xfer)
2506 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2507 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2508 ehci_soft_qh_t *sqh = epipe->sqh;
2509 ehci_soft_qtd_t *sqtd, *snext;
2510 ehci_physaddr_t cur, us, next;
2513 /* int count = 0; */
2514 ehci_soft_qh_t *psqh;
2516 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
2519 /* If we're dying, just do the software part. */
2521 xfer->status = status; /* make software ignore it */
2522 callout_stop(&xfer->timeout_handle);
2523 usb_rem_task(epipe->pipe.device, &exfer->abort_task);
2524 usb_transfer_complete(xfer);
2529 if (xfer->device->bus->intr_context || !curproc)
2530 panic("ehci_abort_xfer: not in process context");
2533 * If an abort is already in progress then just wait for it to
2534 * complete and return.
2536 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING) {
2537 DPRINTFN(2, ("ehci_abort_xfer: already aborting\n"));
2538 /* No need to wait if we're aborting from a timeout. */
2539 if (status == USBD_TIMEOUT)
2541 /* Override the status which might be USBD_TIMEOUT. */
2542 xfer->status = status;
2543 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n"));
2544 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTWAIT;
2545 while (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING)
2546 tsleep(&exfer->ehci_xfer_flags, PZERO, "ehciaw", 0);
2551 * Step 1: Make interrupt routine and timeouts ignore xfer.
2554 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTING;
2555 xfer->status = status; /* make software ignore it */
2556 callout_stop(&xfer->timeout_handle);
2557 usb_rem_task(epipe->pipe.device, &exfer->abort_task);
2561 * Step 2: Wait until we know hardware has finished any possible
2562 * use of the xfer. We do this by removing the entire
2563 * queue from the async schedule and waiting for the doorbell.
2564 * Nothing else should be touching the queue now.
2567 ehci_rem_qh(sc, sqh, psqh);
2570 * Step 3: make sure the soft interrupt routine
2571 * has run. This should remove any completed items off the queue.
2572 * The hardware has no reference to completed items (TDs).
2573 * It's safe to remove them at any time.
2576 #ifdef USB_USE_SOFTINTR
2577 sc->sc_softwake = 1;
2578 #endif /* USB_USE_SOFTINTR */
2579 usb_schedsoftintr(&sc->sc_bus);
2580 #ifdef USB_USE_SOFTINTR
2581 tsleep(&sc->sc_softwake, PZERO, "ehciab", 0);
2582 #endif /* USB_USE_SOFTINTR */
2585 * Step 4: Remove any vestiges of the xfer from the hardware.
2586 * The complication here is that the hardware may have executed
2587 * into or even beyond the xfer we're trying to abort.
2588 * So as we're scanning the TDs of this xfer we check if
2589 * the hardware points to any of them.
2591 * first we need to see if there are any transfers
2592 * on this queue before the xfer we are aborting.. we need
2593 * to update any pointers that point to us to point past
2594 * the aborting xfer. (If there is something past us).
2595 * Hardware and software.
2597 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
2600 /* If they initially point here. */
2601 us = exfer->sqtdstart->physaddr;
2603 /* We will change them to point here */
2604 snext = exfer->sqtdend->nextqtd;
2605 next = htole32(snext->physaddr);
2608 * Now loop through any qTDs before us and keep track of the pointer
2609 * that points to us for the end.
2612 while (sqtd && sqtd != exfer->sqtdstart) {
2613 hit |= (cur == sqtd->physaddr);
2614 if (EHCI_LINK_ADDR(le32toh(sqtd->qtd.qtd_next)) == us)
2615 sqtd->qtd.qtd_next = next;
2616 if (EHCI_LINK_ADDR(le32toh(sqtd->qtd.qtd_altnext)) == us)
2617 sqtd->qtd.qtd_altnext = next;
2618 sqtd = sqtd->nextqtd;
2622 * If we already saw the active one then we are pretty much done.
2623 * We've done all the relinking we need to do.
2628 * Now reinitialise the QH to point to the next qTD
2629 * (if there is one). We only need to do this if
2630 * it was previously pointing to us.
2632 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2633 if (cur == sqtd->physaddr) {
2636 if (sqtd == exfer->sqtdend)
2639 sqtd = sqtd->nextqtd;
2641 * Only need to alter the QH if it was pointing at a qTD
2642 * that we are removing.
2645 sqh->qh.qh_qtd.qtd_next = htole32(snext->physaddr);
2646 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
2647 sqh->qh.qh_qtd.qtd_status &=
2648 htole32(EHCI_QTD_TOGGLE_MASK);
2649 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
2650 sqh->qh.qh_qtd.qtd_buffer[i] = 0;
2651 sqh->qh.qh_qtd.qtd_buffer_hi[i] = 0;
2655 ehci_add_qh(sqh, psqh);
2657 * Step 5: Execute callback.
2662 /* Do the wakeup first to avoid touching the xfer after the callback. */
2663 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTING;
2664 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTWAIT) {
2665 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTWAIT;
2666 wakeup(&exfer->ehci_xfer_flags);
2668 usb_transfer_complete(xfer);
2670 /* printf("%s: %d TDs aborted\n", __func__, count); */
2676 ehci_timeout(void *addr)
2678 struct ehci_xfer *exfer = addr;
2679 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
2680 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2682 DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
2685 usbd_dump_pipe(exfer->xfer.pipe);
2689 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
2693 /* Execute the abort in a process context. */
2694 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task,
2699 ehci_timeout_task(void *addr)
2701 usbd_xfer_handle xfer = addr;
2704 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
2707 ehci_abort_xfer(xfer, USBD_TIMEOUT);
2712 * Some EHCI chips from VIA / ATI seem to trigger interrupts before writing
2713 * back the qTD status, or miss signalling occasionally under heavy load.
2714 * If the host machine is too fast, we can miss transaction completion - when
2715 * we scan the active list the transaction still seems to be active. This
2716 * generally exhibits itself as a umass stall that never recovers.
2718 * We work around this behaviour by setting up this callback after any softintr
2719 * that completes with transactions still pending, giving us another chance to
2720 * check for completion after the writeback has taken place.
2723 ehci_intrlist_timeout(void *arg)
2725 ehci_softc_t *sc = arg;
2728 DPRINTFN(3, ("ehci_intrlist_timeout\n"));
2729 usb_schedsoftintr(&sc->sc_bus);
2734 /************************/
2737 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
2741 /* Insert last in queue. */
2742 err = usb_insert_transfer(xfer);
2746 /* Pipe isn't running, start first */
2747 return (ehci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2751 ehci_device_ctrl_start(usbd_xfer_handle xfer)
2753 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2757 return (USBD_IOERROR);
2760 if (!(xfer->rqflags & URQ_REQUEST)) {
2762 printf("ehci_device_ctrl_transfer: not a request\n");
2763 return (USBD_INVAL);
2767 err = ehci_device_request(xfer);
2771 if (sc->sc_bus.use_polling)
2772 ehci_waitintr(sc, xfer);
2773 return (USBD_IN_PROGRESS);
2777 ehci_device_ctrl_done(usbd_xfer_handle xfer)
2779 struct ehci_xfer *ex = EXFER(xfer);
2780 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2781 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2783 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
2786 if (!(xfer->rqflags & URQ_REQUEST)) {
2787 panic("ehci_ctrl_done: not a request");
2791 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2792 ehci_del_intr_list(ex); /* remove from active list */
2793 ehci_free_sqtd_chain(sc, epipe->sqh, ex->sqtdstart,
2794 ex->sqtdend->nextqtd);
2797 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
2800 /* Abort a device control request. */
2802 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
2804 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
2805 ehci_abort_xfer(xfer, USBD_CANCELLED);
2808 /* Close a device control pipe. */
2810 ehci_device_ctrl_close(usbd_pipe_handle pipe)
2812 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2813 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
2815 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
2816 ehci_close_pipe(pipe, sc->sc_async_head);
2820 ehci_device_request(usbd_xfer_handle xfer)
2822 #define exfer EXFER(xfer)
2823 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2824 usb_device_request_t *req = &xfer->request;
2825 usbd_device_handle dev = epipe->pipe.device;
2826 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2827 ehci_soft_qtd_t *newinactive, *setup, *stat, *next;
2828 ehci_soft_qh_t *sqh;
2834 isread = req->bmRequestType & UT_READ;
2835 len = UGETW(req->wLength);
2837 DPRINTFN(3,("ehci_device_request: type=0x%02x, request=0x%02x, "
2838 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2839 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2840 UGETW(req->wIndex), len, dev->address,
2841 epipe->pipe.endpoint->edesc->bEndpointAddress));
2843 newinactive = ehci_alloc_sqtd(sc);
2844 if (newinactive == NULL) {
2848 newinactive->qtd.qtd_status = htole32(0);
2849 newinactive->qtd.qtd_next = EHCI_NULL;
2850 newinactive->qtd.qtd_altnext = EHCI_NULL;
2851 stat = ehci_alloc_sqtd(sc);
2858 setup = sqh->inactivesqtd;
2859 sqh->inactivesqtd = newinactive;
2860 epipe->u.ctl.length = len;
2862 /* Set up data transaction */
2864 ehci_soft_qtd_t *end;
2866 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
2867 NULL, newinactive, &next, &end);
2870 end->qtd.qtd_status &= htole32(~EHCI_QTD_IOC);
2871 end->nextqtd = stat;
2872 end->qtd.qtd_next = htole32(stat->physaddr);
2873 end->qtd.qtd_altnext = htole32(newinactive->physaddr);
2878 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
2880 /* Clear toggle, and do not activate until complete */
2881 setup->qtd.qtd_status = htole32(
2882 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
2883 EHCI_QTD_SET_CERR(3) |
2884 EHCI_QTD_SET_TOGGLE(0) |
2885 EHCI_QTD_SET_BYTES(sizeof *req)
2887 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
2888 setup->qtd.qtd_buffer_hi[0] = 0;
2889 setup->nextqtd = next;
2890 setup->qtd.qtd_next = htole32(next->physaddr);
2891 setup->qtd.qtd_altnext = htole32(newinactive->physaddr);
2893 setup->len = sizeof *req;
2895 stat->qtd.qtd_status = htole32(
2897 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
2898 EHCI_QTD_SET_CERR(3) |
2899 EHCI_QTD_SET_TOGGLE(1) |
2902 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
2903 stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */
2904 stat->nextqtd = newinactive;
2905 stat->qtd.qtd_next = htole32(newinactive->physaddr);
2906 stat->qtd.qtd_altnext = htole32(newinactive->physaddr);
2911 if (ehcidebug > 5) {
2912 DPRINTF(("ehci_device_request:\n"));
2914 ehci_dump_sqtds(setup);
2918 exfer->sqtdstart = setup;
2919 exfer->sqtdend = stat;
2921 if (!exfer->isdone) {
2922 printf("ehci_device_request: not done, exfer=%p\n", exfer);
2927 /* Activate the new qTD in the QH list. */
2929 ehci_activate_qh(sqh, setup);
2930 if (xfer->timeout && !sc->sc_bus.use_polling) {
2931 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2932 ehci_timeout, xfer);
2934 ehci_add_intr_list(sc, exfer);
2935 xfer->status = USBD_IN_PROGRESS;
2939 if (ehcidebug > 10) {
2940 DPRINTF(("ehci_device_request: status=%x\n",
2941 EOREAD4(sc, EHCI_USBSTS)));
2944 ehci_dump_sqh(sc->sc_async_head);
2946 ehci_dump_sqtds(setup);
2950 return (USBD_NORMAL_COMPLETION);
2953 sqh->inactivesqtd = setup;
2954 ehci_free_sqtd(sc, stat);
2956 ehci_free_sqtd(sc, newinactive);
2958 DPRINTFN(-1,("ehci_device_request: no memory\n"));
2960 usb_transfer_complete(xfer);
2965 /************************/
2968 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
2972 /* Insert last in queue. */
2973 err = usb_insert_transfer(xfer);
2977 /* Pipe isn't running, start first */
2978 return (ehci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
2982 ehci_device_bulk_start(usbd_xfer_handle xfer)
2984 #define exfer EXFER(xfer)
2985 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2986 usbd_device_handle dev = epipe->pipe.device;
2987 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2988 ehci_soft_qtd_t *data, *dataend, *newinactive;
2989 ehci_soft_qh_t *sqh;
2991 int len, isread, endpt;
2994 DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n",
2995 xfer, xfer->length, xfer->flags));
2998 return (USBD_IOERROR);
3001 if (xfer->rqflags & URQ_REQUEST)
3002 panic("ehci_device_bulk_start: a request");
3006 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3007 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3010 epipe->u.bulk.length = len;
3012 newinactive = ehci_alloc_sqtd(sc);
3013 if (newinactive == NULL) {
3014 DPRINTFN(-1,("ehci_device_bulk_start: no sqtd memory\n"));
3017 usb_transfer_complete(xfer);
3020 newinactive->qtd.qtd_status = htole32(0);
3021 newinactive->qtd.qtd_next = EHCI_NULL;
3022 newinactive->qtd.qtd_altnext = EHCI_NULL;
3023 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3024 sqh->inactivesqtd, newinactive, &data, &dataend);
3026 DPRINTFN(-1,("ehci_device_bulk_start: no memory\n"));
3027 ehci_free_sqtd(sc, newinactive);
3029 usb_transfer_complete(xfer);
3032 dataend->nextqtd = newinactive;
3033 dataend->qtd.qtd_next = htole32(newinactive->physaddr);
3034 dataend->qtd.qtd_altnext = htole32(newinactive->physaddr);
3035 sqh->inactivesqtd = newinactive;
3038 if (ehcidebug > 5) {
3039 DPRINTF(("ehci_device_bulk_start: data(1)\n"));
3041 ehci_dump_sqtds(data);
3045 /* Set up interrupt info. */
3046 exfer->sqtdstart = data;
3047 exfer->sqtdend = dataend;
3049 if (!exfer->isdone) {
3050 printf("ehci_device_bulk_start: not done, ex=%p\n", exfer);
3056 ehci_activate_qh(sqh, data);
3057 if (xfer->timeout && !sc->sc_bus.use_polling) {
3058 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3059 ehci_timeout, xfer);
3061 ehci_add_intr_list(sc, exfer);
3062 xfer->status = USBD_IN_PROGRESS;
3066 if (ehcidebug > 10) {
3067 DPRINTF(("ehci_device_bulk_start: data(2)\n"));
3069 DPRINTF(("ehci_device_bulk_start: data(3)\n"));
3072 printf("async_head:\n");
3073 ehci_dump_sqh(sc->sc_async_head);
3077 ehci_dump_sqtds(data);
3081 if (sc->sc_bus.use_polling)
3082 ehci_waitintr(sc, xfer);
3084 return (USBD_IN_PROGRESS);
3089 ehci_device_bulk_abort(usbd_xfer_handle xfer)
3091 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
3092 ehci_abort_xfer(xfer, USBD_CANCELLED);
3096 * Close a device bulk pipe.
3099 ehci_device_bulk_close(usbd_pipe_handle pipe)
3101 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3103 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
3104 ehci_close_pipe(pipe, sc->sc_async_head);
3108 ehci_device_bulk_done(usbd_xfer_handle xfer)
3110 struct ehci_xfer *ex = EXFER(xfer);
3111 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3112 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3114 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
3115 xfer, xfer->actlen));
3117 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3118 ehci_del_intr_list(ex); /* remove from active list */
3119 ehci_free_sqtd_chain(sc, epipe->sqh, ex->sqtdstart,
3120 ex->sqtdend->nextqtd);
3123 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
3126 /************************/
3129 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
3131 struct ehci_soft_islot *isp;
3134 /* Find a poll rate that is large enough. */
3135 for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
3136 if (EHCI_ILEV_IVAL(lev) <= ival)
3139 /* Pick an interrupt slot at the right level. */
3140 /* XXX could do better than picking at random. */
3141 islot = EHCI_IQHIDX(lev, arc4random());
3144 isp = &sc->sc_islots[islot];
3145 ehci_add_qh(sqh, isp->sqh);
3147 return (USBD_NORMAL_COMPLETION);
3151 ehci_device_intr_transfer(usbd_xfer_handle xfer)
3155 /* Insert last in queue. */
3156 err = usb_insert_transfer(xfer);
3161 * Pipe isn't running (otherwise err would be USBD_INPROG),
3162 * so start it first.
3164 return (ehci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3168 ehci_device_intr_start(usbd_xfer_handle xfer)
3170 #define exfer EXFER(xfer)
3171 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3172 usbd_device_handle dev = xfer->pipe->device;
3173 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
3174 ehci_soft_qtd_t *data, *dataend, *newinactive;
3175 ehci_soft_qh_t *sqh;
3177 int len, isread, endpt;
3180 DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%d flags=%d\n",
3181 xfer, xfer->length, xfer->flags));
3184 return (USBD_IOERROR);
3187 if (xfer->rqflags & URQ_REQUEST)
3188 panic("ehci_device_intr_start: a request");
3192 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3193 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3196 epipe->u.intr.length = len;
3198 newinactive = ehci_alloc_sqtd(sc);
3199 if (newinactive == NULL) {
3200 DPRINTFN(-1,("ehci_device_intr_start: no sqtd memory\n"));
3203 usb_transfer_complete(xfer);
3206 newinactive->qtd.qtd_status = htole32(0);
3207 newinactive->qtd.qtd_next = EHCI_NULL;
3208 newinactive->qtd.qtd_altnext = EHCI_NULL;
3209 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3210 sqh->inactivesqtd, newinactive, &data, &dataend);
3212 DPRINTFN(-1, ("ehci_device_intr_start: no memory\n"));
3214 usb_transfer_complete(xfer);
3217 dataend->nextqtd = newinactive;
3218 dataend->qtd.qtd_next = htole32(newinactive->physaddr);
3219 dataend->qtd.qtd_altnext = htole32(newinactive->physaddr);
3220 sqh->inactivesqtd = newinactive;
3223 if (ehcidebug > 5) {
3224 DPRINTF(("ehci_device_intr_start: data(1)\n"));
3226 ehci_dump_sqtds(data);
3230 /* Set up interrupt info. */
3231 exfer->sqtdstart = data;
3232 exfer->sqtdend = dataend;
3234 if (!exfer->isdone) {
3235 printf("ehci_device_intr_start: not done, ex=%p\n", exfer);
3241 ehci_activate_qh(sqh, data);
3242 if (xfer->timeout && !sc->sc_bus.use_polling) {
3243 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3244 ehci_timeout, xfer);
3246 ehci_add_intr_list(sc, exfer);
3247 xfer->status = USBD_IN_PROGRESS;
3251 if (ehcidebug > 10) {
3252 DPRINTF(("ehci_device_intr_start: data(2)\n"));
3254 DPRINTF(("ehci_device_intr_start: data(3)\n"));
3258 ehci_dump_sqtds(data);
3262 if (sc->sc_bus.use_polling)
3263 ehci_waitintr(sc, xfer);
3265 return (USBD_IN_PROGRESS);
3270 ehci_device_intr_abort(usbd_xfer_handle xfer)
3272 DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer));
3273 if (xfer->pipe->intrxfer == xfer) {
3274 DPRINTFN(1, ("ehci_device_intr_abort: remove\n"));
3275 xfer->pipe->intrxfer = NULL;
3277 ehci_abort_xfer(xfer, USBD_CANCELLED);
3281 ehci_device_intr_close(usbd_pipe_handle pipe)
3283 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3284 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
3285 struct ehci_soft_islot *isp;
3287 isp = &sc->sc_islots[epipe->sqh->islot];
3288 ehci_close_pipe(pipe, isp->sqh);
3292 ehci_device_intr_done(usbd_xfer_handle xfer)
3294 #define exfer EXFER(xfer)
3295 struct ehci_xfer *ex = EXFER(xfer);
3296 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3297 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3298 ehci_soft_qtd_t *data, *dataend, *newinactive;
3299 ehci_soft_qh_t *sqh;
3301 int len, isread, endpt, s;
3303 DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n",
3304 xfer, xfer->actlen));
3307 if (xfer->pipe->repeat) {
3308 ehci_free_sqtd_chain(sc, sqh, ex->sqtdstart,
3309 ex->sqtdend->nextqtd);
3311 len = epipe->u.intr.length;
3313 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3314 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3316 newinactive = ehci_alloc_sqtd(sc);
3317 if (newinactive == NULL) {
3319 ("ehci_device_intr_done: no sqtd memory\n"));
3324 newinactive->qtd.qtd_status = htole32(0);
3325 newinactive->qtd.qtd_next = EHCI_NULL;
3326 newinactive->qtd.qtd_altnext = EHCI_NULL;
3327 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3328 sqh->inactivesqtd, newinactive, &data, &dataend);
3330 DPRINTFN(-1, ("ehci_device_intr_done: no memory\n"));
3334 dataend->nextqtd = newinactive;
3335 dataend->qtd.qtd_next = htole32(newinactive->physaddr);
3336 dataend->qtd.qtd_altnext = htole32(newinactive->physaddr);
3337 sqh->inactivesqtd = newinactive;
3339 /* Set up interrupt info. */
3340 exfer->sqtdstart = data;
3341 exfer->sqtdend = dataend;
3343 if (!exfer->isdone) {
3344 printf("ehci_device_intr_done: not done, ex=%p\n",
3351 ehci_activate_qh(sqh, data);
3352 if (xfer->timeout && !sc->sc_bus.use_polling) {
3353 callout_reset(&xfer->timeout_handle,
3354 MS_TO_TICKS(xfer->timeout), ehci_timeout, xfer);
3358 xfer->status = USBD_IN_PROGRESS;
3359 } else if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3360 ehci_del_intr_list(ex); /* remove from active list */
3361 ehci_free_sqtd_chain(sc, sqh, ex->sqtdstart,
3362 ex->sqtdend->nextqtd);
3367 /************************/
3369 static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
3370 static usbd_status ehci_device_isoc_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
3371 static void ehci_device_isoc_abort(usbd_xfer_handle xfer) { }
3372 static void ehci_device_isoc_close(usbd_pipe_handle pipe) { }
3373 static void ehci_device_isoc_done(usbd_xfer_handle xfer) { }