2 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
3 * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 2004 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
32 * The EHCI 0.96 spec can be found at
33 * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
34 * The EHCI 1.0 spec can be found at
35 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
36 * and the USB 2.0 spec at
37 * http://www.usb.org/developers/docs/usb_20.zip
43 * 1) command failures are not recovered correctly
46 #include <sys/stdint.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/types.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/module.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
62 #include <bus/u4b/usb.h>
63 #include <bus/u4b/usbdi.h>
65 #define USB_DEBUG_VAR ehcidebug
67 #include <bus/u4b/usb_core.h>
68 #include <bus/u4b/usb_debug.h>
69 #include <bus/u4b/usb_busdma.h>
70 #include <bus/u4b/usb_process.h>
71 #include <bus/u4b/usb_transfer.h>
72 #include <bus/u4b/usb_device.h>
73 #include <bus/u4b/usb_hub.h>
74 #include <bus/u4b/usb_util.h>
76 #include <bus/u4b/usb_controller.h>
77 #include <bus/u4b/usb_bus.h>
78 #include <bus/u4b/controller/ehci.h>
79 #include <bus/u4b/controller/ehcireg.h>
81 #define EHCI_BUS2SC(bus) \
82 ((ehci_softc_t *)(((uint8_t *)(bus)) - \
83 ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
86 static int ehcidebug = 0;
87 static int ehcinohighspeed = 0;
88 static int ehciiaadbug = 0;
89 static int ehcilostintrbug = 0;
91 static SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
92 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
93 &ehcidebug, 0, "Debug level");
94 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
95 &ehcinohighspeed, 0, "Disable High Speed USB");
96 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RW,
97 &ehciiaadbug, 0, "Enable doorbell bug workaround");
98 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RW,
99 &ehcilostintrbug, 0, "Enable lost interrupt bug workaround");
101 TUNABLE_INT("hw.usb.ehci.debug", &ehcidebug);
102 TUNABLE_INT("hw.usb.ehci.no_hs", &ehcinohighspeed);
103 TUNABLE_INT("hw.usb.ehci.iaadbug", &ehciiaadbug);
104 TUNABLE_INT("hw.usb.ehci.lostintrbug", &ehcilostintrbug);
106 static void ehci_dump_regs(ehci_softc_t *sc);
107 static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
111 #define EHCI_INTR_ENDPT 1
113 static const struct usb_bus_methods ehci_bus_methods;
114 static const struct usb_pipe_methods ehci_device_bulk_methods;
115 static const struct usb_pipe_methods ehci_device_ctrl_methods;
116 static const struct usb_pipe_methods ehci_device_intr_methods;
117 static const struct usb_pipe_methods ehci_device_isoc_fs_methods;
118 static const struct usb_pipe_methods ehci_device_isoc_hs_methods;
120 static void ehci_do_poll(struct usb_bus *);
121 static void ehci_device_done(struct usb_xfer *, usb_error_t);
122 static uint8_t ehci_check_transfer(struct usb_xfer *xfer, int finish);
123 static void ehci_timeout(void *);
124 static void ehci_poll_timeout(void *);
126 static void ehci_root_intr(ehci_softc_t *sc);
128 struct ehci_std_temp {
130 struct usb_page_cache *pc;
136 uint16_t max_frame_size;
138 uint8_t auto_data_toggle;
139 uint8_t setup_alt_next;
144 ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
146 ehci_softc_t *sc = EHCI_BUS2SC(bus);
149 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
150 sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
152 cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg,
153 sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN);
155 cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
156 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
158 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
159 cb(bus, sc->sc_hw.intr_start_pc + i,
160 sc->sc_hw.intr_start_pg + i,
161 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
164 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
165 cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
166 sc->sc_hw.isoc_hs_start_pg + i,
167 sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
170 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
171 cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
172 sc->sc_hw.isoc_fs_start_pg + i,
173 sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
178 ehci_reset(ehci_softc_t *sc)
183 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
184 for (i = 0; i < 100; i++) {
185 usb_pause_mtx(NULL, hz / 128);
186 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
188 if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
190 * Force USBMODE as requested. Controllers
191 * may have multiple operating modes.
193 uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
194 if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
195 usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
196 device_printf(sc->sc_bus.bdev,
197 "set host controller mode\n");
199 if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
200 usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
201 device_printf(sc->sc_bus.bdev,
202 "set big-endian mode\n");
204 EOWRITE4(sc, EHCI_USBMODE, usbmode);
209 device_printf(sc->sc_bus.bdev, "Reset timeout\n");
210 return (USB_ERR_IOERROR);
214 ehci_hcreset(ehci_softc_t *sc)
219 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
220 for (i = 0; i < 100; i++) {
221 usb_pause_mtx(NULL, hz / 128);
222 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
228 * Fall through and try reset anyway even though
229 * Table 2-9 in the EHCI spec says this will result
230 * in undefined behavior.
232 device_printf(sc->sc_bus.bdev, "stop timeout\n");
234 return (ehci_reset(sc));
238 ehci_init_sub(struct ehci_softc *sc)
240 struct usb_page_search buf_res;
245 cparams = EREAD4(sc, EHCI_HCCPARAMS);
247 DPRINTF("cparams=0x%x\n", cparams);
249 if (EHCI_HCC_64BIT(cparams)) {
250 DPRINTF("HCC uses 64-bit structures\n");
252 /* MUST clear segment register if 64 bit capable */
253 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
256 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
257 EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
259 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
260 EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
262 /* enable interrupts */
263 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
265 /* turn on controller */
266 EOWRITE4(sc, EHCI_USBCMD,
267 EHCI_CMD_ITC_1 | /* 1 microframes interrupt delay */
268 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
269 EHCI_CMD_ASPME | EHCI_CMD_ASPMC |
274 /* Take over port ownership */
275 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
277 for (i = 0; i < 100; i++) {
278 usb_pause_mtx(NULL, hz / 128);
279 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
285 device_printf(sc->sc_bus.bdev, "Run timeout\n");
286 return (USB_ERR_IOERROR);
288 return (USB_ERR_NORMAL_COMPLETION);
292 ehci_init(ehci_softc_t *sc)
294 struct usb_page_search buf_res;
305 usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_lock, 0);
306 usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_lock, 0);
308 sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
312 sc->sc_flags |= EHCI_SCFLG_IAADBUG;
314 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
320 version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
321 device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
322 version >> 8, version & 0xff);
324 sparams = EREAD4(sc, EHCI_HCSPARAMS);
325 DPRINTF("sparams=0x%x\n", sparams);
327 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
328 sc->sc_bus.usbrev = USB_REV_2_0;
330 if (!(sc->sc_flags & EHCI_SCFLG_DONTRESET)) {
331 /* Reset the controller */
332 DPRINTF("%s: resetting\n",
333 device_get_nameunit(sc->sc_bus.bdev));
335 err = ehci_hcreset(sc);
337 device_printf(sc->sc_bus.bdev, "reset timeout\n");
343 * use current frame-list-size selection 0: 1024*4 bytes 1: 512*4
344 * bytes 2: 256*4 bytes 3: unknown
346 if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
347 device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
348 return (USB_ERR_IOERROR);
350 /* set up the bus struct */
351 sc->sc_bus.methods = &ehci_bus_methods;
353 sc->sc_eintrs = EHCI_NORMAL_INTRS;
356 struct ehci_qh_sub *qh;
358 usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res);
362 sc->sc_terminate_self = htohc32(sc, buf_res.physaddr);
364 /* init terminate TD */
366 htohc32(sc, EHCI_LINK_TERMINATE);
368 htohc32(sc, EHCI_LINK_TERMINATE);
370 htohc32(sc, EHCI_QTD_HALTED);
373 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
376 usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
380 /* initialize page cache pointer */
382 qh->page_cache = sc->sc_hw.intr_start_pc + i;
384 /* store a pointer to queue head */
386 sc->sc_intr_p_last[i] = qh;
389 htohc32(sc, buf_res.physaddr) |
390 htohc32(sc, EHCI_LINK_QH);
393 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
395 htohc32(sc, EHCI_QH_SET_MULT(1));
398 qh->qh_qtd.qtd_next =
399 htohc32(sc, EHCI_LINK_TERMINATE);
400 qh->qh_qtd.qtd_altnext =
401 htohc32(sc, EHCI_LINK_TERMINATE);
402 qh->qh_qtd.qtd_status =
403 htohc32(sc, EHCI_QTD_HALTED);
407 * the QHs are arranged to give poll intervals that are
408 * powers of 2 times 1ms
410 * e.g. 64-127, 32-63, 16-31, 8-15, 4-7, 2-3, 1-1, 0
411 * 1ms 2ms 4ms 8ms 16ms 32ms 64ms 128m
413 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
420 y = (x ^ bit) | (bit / 2);
422 qh_x = sc->sc_intr_p_last[x];
423 qh_y = sc->sc_intr_p_last[y];
426 * the next QH has half the poll interval
428 qh_x->qh_link = qh_y->qh_self;
438 qh = sc->sc_intr_p_last[0];
440 /* the last (1ms) QH terminates */
441 qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
443 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
447 usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
449 sitd = buf_res.buffer;
451 /* initialize page cache pointer */
453 sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
455 /* store a pointer to the transfer descriptor */
457 sc->sc_isoc_fs_p_last[i] = sitd;
459 /* initialize full speed isochronous */
462 htohc32(sc, buf_res.physaddr) |
463 htohc32(sc, EHCI_LINK_SITD);
466 htohc32(sc, EHCI_LINK_TERMINATE);
469 * Point to 1ms interrupt queue heads 1:1
472 sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
475 usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
477 itd = buf_res.buffer;
479 /* initialize page cache pointer */
481 itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
483 /* store a pointer to the transfer descriptor */
485 sc->sc_isoc_hs_p_last[i] = itd;
487 /* initialize high speed isochronous */
490 htohc32(sc, buf_res.physaddr) |
491 htohc32(sc, EHCI_LINK_ITD);
500 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
505 pframes = buf_res.buffer;
509 * pframes -> high speed isochronous ->
510 * full speed isochronous -> interrupt QH's
512 * Each pframe chains through 10 queue heads.
513 * HS FS IQ1ms IQ2ms IQ4ms ... IQ128ms -> term
515 for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
516 pframes[i] = sc->sc_isoc_hs_p_last
517 [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
520 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
523 * Asynchronous schedule requires a QH the points to itself initially.
524 * (it is not terminated with EHCI_LINK_TERMINATE).
532 /* initialize page cache pointer */
534 qh->page_cache = &sc->sc_hw.async_start_pc;
536 /* store a pointer to the queue head */
538 sc->sc_async_p_last = qh;
540 /* init dummy QH that starts the async list */
543 htohc32(sc, buf_res.physaddr) |
544 htohc32(sc, EHCI_LINK_QH);
548 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
549 qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
550 qh->qh_link = qh->qh_self;
553 /* fill the overlay qTD */
554 qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
555 qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
556 qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
558 /* flush all cache into memory */
560 usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
564 ehci_dump_sqh(sc, sc->sc_async_p_last);
569 err = ehci_init_sub(sc);
572 /* catch any lost interrupts */
573 ehci_do_poll(&sc->sc_bus);
579 * shut down the controller when the system is going down
582 ehci_detach(ehci_softc_t *sc)
584 USB_BUS_LOCK(&sc->sc_bus);
586 usb_callout_stop(&sc->sc_tmo_pcd);
587 usb_callout_stop(&sc->sc_tmo_poll);
589 EOWRITE4(sc, EHCI_USBINTR, 0);
590 USB_BUS_UNLOCK(&sc->sc_bus);
592 if (ehci_hcreset(sc)) {
593 DPRINTF("reset failed!\n");
596 /* XXX let stray task complete */
597 usb_pause_mtx(NULL, hz / 20);
599 usb_callout_drain(&sc->sc_tmo_pcd);
600 usb_callout_drain(&sc->sc_tmo_poll);
604 ehci_suspend(ehci_softc_t *sc)
606 DPRINTF("stopping the HC\n");
613 ehci_resume(ehci_softc_t *sc)
621 /* catch any lost interrupts */
622 ehci_do_poll(&sc->sc_bus);
627 ehci_dump_regs(ehci_softc_t *sc)
631 i = EOREAD4(sc, EHCI_USBCMD);
632 kprintf("cmd=0x%08x\n", i);
634 if (i & EHCI_CMD_ITC_1)
635 kprintf(" EHCI_CMD_ITC_1\n");
636 if (i & EHCI_CMD_ITC_2)
637 kprintf(" EHCI_CMD_ITC_2\n");
638 if (i & EHCI_CMD_ITC_4)
639 kprintf(" EHCI_CMD_ITC_4\n");
640 if (i & EHCI_CMD_ITC_8)
641 kprintf(" EHCI_CMD_ITC_8\n");
642 if (i & EHCI_CMD_ITC_16)
643 kprintf(" EHCI_CMD_ITC_16\n");
644 if (i & EHCI_CMD_ITC_32)
645 kprintf(" EHCI_CMD_ITC_32\n");
646 if (i & EHCI_CMD_ITC_64)
647 kprintf(" EHCI_CMD_ITC_64\n");
648 if (i & EHCI_CMD_ASPME)
649 kprintf(" EHCI_CMD_ASPME\n");
650 if (i & EHCI_CMD_ASPMC)
651 kprintf(" EHCI_CMD_ASPMC\n");
652 if (i & EHCI_CMD_LHCR)
653 kprintf(" EHCI_CMD_LHCR\n");
654 if (i & EHCI_CMD_IAAD)
655 kprintf(" EHCI_CMD_IAAD\n");
656 if (i & EHCI_CMD_ASE)
657 kprintf(" EHCI_CMD_ASE\n");
658 if (i & EHCI_CMD_PSE)
659 kprintf(" EHCI_CMD_PSE\n");
660 if (i & EHCI_CMD_FLS_M)
661 kprintf(" EHCI_CMD_FLS_M\n");
662 if (i & EHCI_CMD_HCRESET)
663 kprintf(" EHCI_CMD_HCRESET\n");
665 kprintf(" EHCI_CMD_RS\n");
667 i = EOREAD4(sc, EHCI_USBSTS);
669 kprintf("sts=0x%08x\n", i);
671 if (i & EHCI_STS_ASS)
672 kprintf(" EHCI_STS_ASS\n");
673 if (i & EHCI_STS_PSS)
674 kprintf(" EHCI_STS_PSS\n");
675 if (i & EHCI_STS_REC)
676 kprintf(" EHCI_STS_REC\n");
677 if (i & EHCI_STS_HCH)
678 kprintf(" EHCI_STS_HCH\n");
679 if (i & EHCI_STS_IAA)
680 kprintf(" EHCI_STS_IAA\n");
681 if (i & EHCI_STS_HSE)
682 kprintf(" EHCI_STS_HSE\n");
683 if (i & EHCI_STS_FLR)
684 kprintf(" EHCI_STS_FLR\n");
685 if (i & EHCI_STS_PCD)
686 kprintf(" EHCI_STS_PCD\n");
687 if (i & EHCI_STS_ERRINT)
688 kprintf(" EHCI_STS_ERRINT\n");
689 if (i & EHCI_STS_INT)
690 kprintf(" EHCI_STS_INT\n");
692 kprintf("ien=0x%08x\n",
693 EOREAD4(sc, EHCI_USBINTR));
694 kprintf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
695 EOREAD4(sc, EHCI_FRINDEX),
696 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
697 EOREAD4(sc, EHCI_PERIODICLISTBASE),
698 EOREAD4(sc, EHCI_ASYNCLISTADDR));
699 for (i = 1; i <= sc->sc_noport; i++) {
700 kprintf("port %d status=0x%08x\n", i,
701 EOREAD4(sc, EHCI_PORTSC(i)));
706 ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
708 link = hc32toh(sc, link);
709 kprintf("0x%08x", link);
710 if (link & EHCI_LINK_TERMINATE)
715 switch (EHCI_LINK_TYPE(link)) {
735 ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
740 ehci_dump_link(sc, qtd->qtd_next, 0);
741 kprintf(" altnext=");
742 ehci_dump_link(sc, qtd->qtd_altnext, 0);
744 s = hc32toh(sc, qtd->qtd_status);
745 kprintf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
746 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
747 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
748 kprintf(" cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
749 EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
750 (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
751 (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
752 (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
753 (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
754 (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
755 (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
756 (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
757 (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
759 for (s = 0; s < 5; s++) {
760 kprintf(" buffer[%d]=0x%08x\n", s,
761 hc32toh(sc, qtd->qtd_buffer[s]));
763 for (s = 0; s < 5; s++) {
764 kprintf(" buffer_hi[%d]=0x%08x\n", s,
765 hc32toh(sc, qtd->qtd_buffer_hi[s]));
770 ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
774 usb_pc_cpu_invalidate(sqtd->page_cache);
775 kprintf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
776 ehci_dump_qtd(sc, sqtd);
777 temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
782 ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
788 for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
789 stop = ehci_dump_sqtd(sc, sqtd);
792 kprintf("dump aborted, too many TDs\n");
797 ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
802 usb_pc_cpu_invalidate(qh->page_cache);
803 kprintf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
805 ehci_dump_link(sc, qh->qh_link, 1);
807 endp = hc32toh(sc, qh->qh_endp);
808 kprintf(" endp=0x%08x\n", endp);
809 kprintf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
810 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
811 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
812 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
813 kprintf(" mpl=0x%x ctl=%d nrl=%d\n",
814 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
815 EHCI_QH_GET_NRL(endp));
816 endphub = hc32toh(sc, qh->qh_endphub);
817 kprintf(" endphub=0x%08x\n", endphub);
818 kprintf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
819 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
820 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
821 EHCI_QH_GET_MULT(endphub));
823 ehci_dump_link(sc, qh->qh_curqtd, 0);
825 kprintf("Overlay qTD:\n");
826 ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
830 ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
832 usb_pc_cpu_invalidate(sitd->page_cache);
833 kprintf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
834 kprintf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
835 kprintf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
836 hc32toh(sc, sitd->sitd_portaddr),
837 (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
839 EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
840 EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
841 EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
842 EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
843 kprintf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
844 kprintf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
845 (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
846 EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
847 kprintf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
848 hc32toh(sc, sitd->sitd_back),
849 hc32toh(sc, sitd->sitd_bp[0]),
850 hc32toh(sc, sitd->sitd_bp[1]),
851 hc32toh(sc, sitd->sitd_bp_hi[0]),
852 hc32toh(sc, sitd->sitd_bp_hi[1]));
856 ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
858 usb_pc_cpu_invalidate(itd->page_cache);
859 kprintf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
860 kprintf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
861 kprintf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
862 (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
863 kprintf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
864 (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
865 kprintf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
866 (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
867 kprintf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
868 (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
869 kprintf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
870 (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
871 kprintf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
872 (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
873 kprintf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
874 (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
875 kprintf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
876 (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
877 kprintf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
878 kprintf(" addr=0x%02x; endpt=0x%01x\n",
879 EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
880 EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
881 kprintf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
882 kprintf(" dir=%s; mpl=0x%02x\n",
883 (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
884 EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
885 kprintf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
886 hc32toh(sc, itd->itd_bp[2]),
887 hc32toh(sc, itd->itd_bp[3]),
888 hc32toh(sc, itd->itd_bp[4]),
889 hc32toh(sc, itd->itd_bp[5]),
890 hc32toh(sc, itd->itd_bp[6]));
891 kprintf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
892 " 0x%08x,0x%08x,0x%08x\n",
893 hc32toh(sc, itd->itd_bp_hi[0]),
894 hc32toh(sc, itd->itd_bp_hi[1]),
895 hc32toh(sc, itd->itd_bp_hi[2]),
896 hc32toh(sc, itd->itd_bp_hi[3]),
897 hc32toh(sc, itd->itd_bp_hi[4]),
898 hc32toh(sc, itd->itd_bp_hi[5]),
899 hc32toh(sc, itd->itd_bp_hi[6]));
903 ehci_dump_isoc(ehci_softc_t *sc)
910 pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
911 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
913 kprintf("%s: isochronous dump from frame 0x%03x:\n",
916 itd = sc->sc_isoc_hs_p_last[pos];
917 sitd = sc->sc_isoc_fs_p_last[pos];
919 while (itd && max && max--) {
920 ehci_dump_itd(sc, itd);
924 while (sitd && max && max--) {
925 ehci_dump_sitd(sc, sitd);
933 ehci_ring_doorbell(ehci_softc_t *sc, struct usb_xfer *xfer)
938 * Request a doorbell interrupt. We can't safely disturb previously
939 * used QH's until controller has synchronized the list changes we've
942 * Deal with controller races if a doorbell has already been
943 * requested, we have to wait 2 doorbells in this case to be sure.
945 * (interlocked against races by the bus lock)
947 temp = EOREAD4(sc, EHCI_USBCMD);
948 if (sc->doorbell_again == 0) {
949 if ((temp & EHCI_CMD_IAAD) == 0)
950 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
952 xfer->doorbell_match = sc->doorbell_match + 1;
953 sc->doorbell_again = 1;
955 if ((temp & EHCI_CMD_IAAD) == 0)
956 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
958 xfer->doorbell_match = sc->doorbell_match + 2;
959 sc->doorbell_again = 2;
964 ehci_wait_doorbell(ehci_softc_t *sc, int match)
966 while ((int)(match - sc->doorbell_match) > 0)
967 lksleep(sc, &sc->sc_bus.bus_lock, 0, "ehcidoor", hz);
971 ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
973 /* check for early completion */
974 if (ehci_check_transfer(xfer, 1)) {
977 /* put transfer on interrupt queue */
978 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
980 /* start timeout, if any */
981 if (xfer->timeout != 0) {
982 usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
986 #define EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
988 _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
990 DPRINTFN(11, "%p to %p\n", std, last);
992 /* (sc->sc_bus.mtx) must be locked */
994 std->next = last->next;
995 std->sitd_next = last->sitd_next;
999 usb_pc_cpu_flush(std->page_cache);
1002 * the last->next->prev is never followed: std->next->prev = std;
1005 last->sitd_next = std->sitd_self;
1007 usb_pc_cpu_flush(last->page_cache);
1012 #define EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
1014 _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1016 DPRINTFN(11, "%p to %p\n", std, last);
1018 /* (sc->sc_bus.mtx) must be locked */
1020 std->next = last->next;
1021 std->itd_next = last->itd_next;
1025 usb_pc_cpu_flush(std->page_cache);
1028 * the last->next->prev is never followed: std->next->prev = std;
1031 last->itd_next = std->itd_self;
1033 usb_pc_cpu_flush(last->page_cache);
1038 #define EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
1040 _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1042 DPRINTFN(11, "%p to %p\n", sqh, last);
1044 if (sqh->prev != NULL) {
1045 /* should not happen */
1046 DPRINTFN(0, "QH already linked!\n");
1049 /* (sc->sc_bus.mtx) must be locked */
1051 sqh->next = last->next;
1052 sqh->qh_link = last->qh_link;
1056 usb_pc_cpu_flush(sqh->page_cache);
1059 * The last->next->prev is never followed: sqh->next->prev = sqh;
1061 * This should activate the command.
1065 last->qh_link = sqh->qh_self;
1067 usb_pc_cpu_flush(last->page_cache);
1073 ehci_stop_xfer(struct usb_xfer *xfer)
1076 if (xfer->flags_int.onhwqueue == 0)
1079 if (ehci_check_transfer(xfer, 0))
1083 while (retry && ehci_check_transfer(xfer, 0) == 0) {
1089 kprintf("[can't stop xfer]\n");
1093 #define EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
1094 static ehci_sitd_t *
1095 _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
1097 DPRINTFN(11, "%p from %p\n", std, last);
1099 /* (sc->sc_bus.mtx) must be locked */
1101 std->prev->next = std->next;
1102 std->prev->sitd_next = std->sitd_next;
1104 usb_pc_cpu_flush(std->prev->page_cache);
1107 std->next->prev = std->prev;
1108 usb_pc_cpu_flush(std->next->page_cache);
1110 return ((last == std) ? std->prev : last);
1113 #define EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
1115 _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1117 DPRINTFN(11, "%p from %p\n", std, last);
1119 /* (sc->sc_bus.mtx) must be locked */
1121 std->prev->next = std->next;
1122 std->prev->itd_next = std->itd_next;
1124 usb_pc_cpu_flush(std->prev->page_cache);
1127 std->next->prev = std->prev;
1128 usb_pc_cpu_flush(std->next->page_cache);
1130 return ((last == std) ? std->prev : last);
1133 #define EHCI_REMOVE_QH(sc,sqh,last) (last) = _ehci_remove_qh(sc,sqh,last)
1135 _ehci_remove_qh(ehci_softc_t *sc, ehci_qh_t *sqh, ehci_qh_t *last)
1137 DPRINTFN(11, "%p from %p\n", sqh, last);
1139 /* (sc->sc_bus.mtx) must be locked */
1141 /* only remove if not removed from a queue */
1143 sqh->prev->next = sqh->next;
1145 sqh->next->prev = sqh->prev;
1146 last = ((last == sqh) ? sqh->prev : last);
1149 * This is a horrible hack because basically the entire
1150 * U4B QH handling is broken. To do this correctly the
1151 * QH needs be unlinked but then not touched until the
1152 * next rollup doorbell interrupt. That is, the controller
1153 * could be racing us accessing the QH and qh_link, so
1154 * the links have to remain valid enough to not cause the
1155 * controller to blow up.
1157 * When we mess with the links it is possible for the
1158 * controller to end up on the deleted chain(s) and miss
1159 * new requests entered after the deletion. Then it thinks
1160 * it is finished and stops scanning. Thus we must issue a
1161 * doorbell to force the controller to re-scan and pick
1162 * up anything it missed if it got off-track.
1164 * I wanted to try to make this more reliable by setting
1165 * qh_link to htohc32(sc, EHCI_LINK_TERMINATE), but it
1166 * doesn't work. The controller barfs badly and isn't
1167 * able to probe all the usb devices. I'm not sure
1168 * why it doesn't work but... whatever.
1170 sqh->prev->qh_link = sqh->qh_link;
1171 usb_pc_cpu_flush(sqh->prev->page_cache);
1172 /* sqh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE); */
1174 if ((sc->sc_flags & EHCI_SCFLG_IAADBUG) == 0) {
1175 uint32_t temp = EOREAD4(sc, EHCI_USBCMD);
1176 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
1180 usb_pc_cpu_flush(sqh->page_cache);
1186 ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen)
1191 /* count number of full packets */
1192 dt = (actlen / xfer->max_packet_size) & 1;
1194 /* compute remainder */
1195 rem = actlen % xfer->max_packet_size;
1198 dt ^= 1; /* short packet at the end */
1199 else if (actlen != xlen)
1200 dt ^= 1; /* zero length packet at the end */
1202 dt ^= 1; /* zero length transfer */
1204 xfer->endpoint->toggle_next ^= dt;
1208 ehci_non_isoc_done_sub(struct usb_xfer *xfer)
1210 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1212 ehci_qtd_t *td_alt_next;
1216 td = xfer->td_transfer_cache;
1217 td_alt_next = td->alt_next;
1219 if (xfer->aframes != xfer->nframes) {
1220 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1223 usb_pc_cpu_invalidate(td->page_cache);
1224 status = hc32toh(sc, td->qtd_status);
1226 len = EHCI_QTD_GET_BYTES(status);
1229 * Verify the status length and
1230 * add the length to "frlengths[]":
1232 if (len > td->len) {
1233 /* should not happen */
1234 DPRINTF("Invalid status length, "
1235 "0x%04x/0x%04x bytes\n", len, td->len);
1236 status |= EHCI_QTD_HALTED;
1237 } else if (xfer->aframes != xfer->nframes) {
1238 xfer->frlengths[xfer->aframes] += td->len - len;
1239 /* manually update data toggle */
1240 ehci_data_toggle_update(xfer, td->len - len, td->len);
1243 /* Check for last transfer */
1244 if (((void *)td) == xfer->td_transfer_last) {
1248 /* Check for transfer error */
1249 if (status & EHCI_QTD_HALTED) {
1250 /* the transfer is finished */
1254 /* Check for short transfer */
1256 if (xfer->flags_int.short_frames_ok) {
1257 /* follow alt next */
1260 /* the transfer is finished */
1267 if (td->alt_next != td_alt_next) {
1268 /* this USB frame is complete */
1273 /* update transfer cache */
1275 xfer->td_transfer_cache = td;
1278 if (status & EHCI_QTD_STATERRS) {
1279 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
1280 "status=%s%s%s%s%s%s%s%s\n",
1281 xfer->address, xfer->endpointno, xfer->aframes,
1282 (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1283 (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
1284 (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
1285 (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
1286 (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
1287 (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
1288 (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
1289 (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
1293 return ((status & EHCI_QTD_HALTED) ?
1294 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1298 ehci_non_isoc_done(struct usb_xfer *xfer)
1300 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1303 usb_error_t err = 0;
1305 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1306 xfer, xfer->endpoint);
1309 if (ehcidebug > 10) {
1310 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1312 ehci_dump_sqtds(sc, xfer->td_transfer_first);
1316 /* extract data toggle directly from the QH's overlay area */
1318 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1320 usb_pc_cpu_invalidate(qh->page_cache);
1322 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1326 xfer->td_transfer_cache = xfer->td_transfer_first;
1328 if (xfer->flags_int.control_xfr) {
1330 if (xfer->flags_int.control_hdr) {
1332 err = ehci_non_isoc_done_sub(xfer);
1336 if (xfer->td_transfer_cache == NULL) {
1340 while (xfer->aframes != xfer->nframes) {
1342 err = ehci_non_isoc_done_sub(xfer);
1345 if (xfer->td_transfer_cache == NULL) {
1350 if (xfer->flags_int.control_xfr &&
1351 !xfer->flags_int.control_act) {
1353 err = ehci_non_isoc_done_sub(xfer);
1356 ehci_device_done(xfer, err);
1359 /*------------------------------------------------------------------------*
1360 * ehci_check_transfer
1363 * 0: USB transfer is not finished
1364 * Else: USB transfer is finished
1365 *------------------------------------------------------------------------*/
1367 ehci_check_transfer(struct usb_xfer *xfer, int finish)
1369 const struct usb_pipe_methods *methods = xfer->endpoint->methods;
1370 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1374 DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
1376 if (methods == &ehci_device_isoc_fs_methods) {
1379 /* isochronous full speed transfer */
1381 td = xfer->td_transfer_last;
1382 usb_pc_cpu_invalidate(td->page_cache);
1383 status = hc32toh(sc, td->sitd_status);
1385 /* also check if first is complete */
1387 td = xfer->td_transfer_first;
1388 usb_pc_cpu_invalidate(td->page_cache);
1389 status |= hc32toh(sc, td->sitd_status);
1391 if (!(status & EHCI_SITD_ACTIVE)) {
1392 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1395 } else if (methods == &ehci_device_isoc_hs_methods) {
1398 /* isochronous high speed transfer */
1400 /* check last transfer */
1401 td = xfer->td_transfer_last;
1402 usb_pc_cpu_invalidate(td->page_cache);
1403 status = td->itd_status[0];
1404 status |= td->itd_status[1];
1405 status |= td->itd_status[2];
1406 status |= td->itd_status[3];
1407 status |= td->itd_status[4];
1408 status |= td->itd_status[5];
1409 status |= td->itd_status[6];
1410 status |= td->itd_status[7];
1412 /* also check first transfer */
1413 td = xfer->td_transfer_first;
1414 usb_pc_cpu_invalidate(td->page_cache);
1415 status |= td->itd_status[0];
1416 status |= td->itd_status[1];
1417 status |= td->itd_status[2];
1418 status |= td->itd_status[3];
1419 status |= td->itd_status[4];
1420 status |= td->itd_status[5];
1421 status |= td->itd_status[6];
1422 status |= td->itd_status[7];
1424 /* if no transactions are active we continue */
1425 if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
1427 ehci_device_done(xfer,
1428 USB_ERR_NORMAL_COMPLETION);
1436 /* non-isochronous transfer */
1439 * check whether there is an error somewhere in the middle,
1440 * or whether there was a short packet (SPD and not ACTIVE)
1442 td = xfer->td_transfer_cache;
1444 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1446 usb_pc_cpu_invalidate(qh->page_cache);
1448 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1449 if (status & EHCI_QTD_ACTIVE) {
1450 /* transfer is pending */
1455 usb_pc_cpu_invalidate(td->page_cache);
1456 status = hc32toh(sc, td->qtd_status);
1459 * Check if there is an active TD which
1460 * indicates that the transfer isn't done.
1462 if (status & EHCI_QTD_ACTIVE) {
1464 xfer->td_transfer_cache = td;
1468 * last transfer descriptor makes the transfer done
1470 if (((void *)td) == xfer->td_transfer_last) {
1474 * any kind of error makes the transfer done
1476 if (status & EHCI_QTD_HALTED) {
1480 * if there is no alternate next transfer, a short
1481 * packet also makes the transfer done
1483 if (EHCI_QTD_GET_BYTES(status)) {
1484 if (xfer->flags_int.short_frames_ok) {
1485 /* follow alt next */
1491 /* transfer is done */
1497 ehci_non_isoc_done(xfer);
1502 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1510 ehci_pcd_enable(ehci_softc_t *sc)
1512 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
1514 sc->sc_eintrs |= EHCI_STS_PCD;
1515 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1517 /* acknowledge any PCD interrupt */
1518 EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
1524 ehci_interrupt_poll(ehci_softc_t *sc)
1526 struct usb_xfer *xfer;
1529 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1531 * check if transfer is transferred
1533 if (ehci_check_transfer(xfer, 1)) {
1534 /* queue has been modified */
1541 * Some EHCI chips from VIA / ATI seem to trigger interrupts before
1542 * writing back the qTD status, or miss signalling occasionally under
1543 * heavy load. If the host machine is too fast, we can miss
1544 * transaction completion - when we scan the active list the
1545 * transaction still seems to be active. This generally exhibits
1546 * itself as a umass stall that never recovers.
1548 * We work around this behaviour by setting up this callback after any
1549 * softintr that completes with transactions still pending, giving us
1550 * another chance to check for completion after the writeback has
1554 ehci_poll_timeout(void *arg)
1556 ehci_softc_t *sc = arg;
1559 ehci_interrupt_poll(sc);
1562 /*------------------------------------------------------------------------*
1563 * ehci_interrupt - EHCI interrupt handler
1565 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1566 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1568 *------------------------------------------------------------------------*/
1570 ehci_interrupt(ehci_softc_t *sc)
1574 USB_BUS_LOCK(&sc->sc_bus);
1576 DPRINTFN(16, "real interrupt\n");
1579 if (ehcidebug > 15) {
1584 status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1587 * XXX hack workaround bug where interrupt status says
1588 * nothing is pending but xfer completions might be present.
1591 /* the interrupt was not for us */
1594 if (!(status & sc->sc_eintrs)) {
1599 * Acknowlege interrupts except IAA
1601 EOWRITE4(sc, EHCI_USBSTS, status);
1603 status &= sc->sc_eintrs;
1605 if (status & EHCI_STS_HSE) {
1606 kprintf("%s: unrecoverable error, "
1607 "controller halted\n", __func__);
1613 if (status & EHCI_STS_PCD) {
1615 * Disable PCD interrupt for now, because it will be
1616 * on until the port has been reset.
1618 sc->sc_eintrs &= ~EHCI_STS_PCD;
1619 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1623 /* do not allow RHSC interrupts > 1 per second */
1624 usb_callout_reset(&sc->sc_tmo_pcd, hz,
1625 (void *)&ehci_pcd_enable, sc);
1629 * Handle IAA doorbells, acknowledge IAA after clearing CMD_IAAD.
1631 * The frontend may have tried to ring the doorbell after it had
1632 * already been rung, in which case it has to wait for two doorbells.
1633 * We track this with the doorbell_again counter. This counter never
1636 * NOTE: ehci_ring_doorbell() bumps doorbell_again so we need to
1637 * decrement it twice to net-out at a single decrement.
1639 if (status & EHCI_STS_IAA) {
1640 ++sc->doorbell_match;
1642 if (sc->doorbell_again && --sc->doorbell_again > 0) {
1643 --sc->doorbell_again;
1644 ehci_ring_doorbell(sc, NULL);
1649 * Block unprocessed interrupts
1651 status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT |
1652 EHCI_STS_PCD | EHCI_STS_IAA);
1654 sc->sc_eintrs &= ~status;
1655 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1656 kprintf("%s: blocking interrupts 0x%x\n", __func__, status);
1659 /* poll all the USB transfers */
1660 ehci_interrupt_poll(sc);
1662 if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
1663 usb_callout_reset(&sc->sc_tmo_poll, hz / 128,
1664 (void *)&ehci_poll_timeout, sc);
1670 USB_BUS_UNLOCK(&sc->sc_bus);
1674 * called when a request does not complete
1677 ehci_timeout(void *arg)
1679 struct usb_xfer *xfer = arg;
1681 DPRINTF("xfer=%p\n", xfer);
1683 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
1685 /* transfer is transferred */
1686 ehci_device_done(xfer, USB_ERR_TIMEOUT);
1690 ehci_do_poll(struct usb_bus *bus)
1692 ehci_softc_t *sc = EHCI_BUS2SC(bus);
1694 USB_BUS_LOCK(&sc->sc_bus);
1695 ehci_interrupt_poll(sc);
1696 USB_BUS_UNLOCK(&sc->sc_bus);
1700 ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
1702 struct usb_page_search buf_res;
1704 ehci_qtd_t *td_next;
1705 ehci_qtd_t *td_alt_next;
1706 uint32_t buf_offset;
1710 uint32_t qtd_altnext;
1711 uint8_t shortpkt_old;
1714 terminate = temp->sc->sc_terminate_self;
1715 qtd_altnext = temp->sc->sc_terminate_self;
1718 shortpkt_old = temp->shortpkt;
1719 len_old = temp->len;
1724 td = temp->td; /* starts out NULL */
1725 td_next = temp->td_next; /* starts with first td */
1729 if (temp->len == 0) {
1731 if (temp->shortpkt) {
1734 /* send a Zero Length Packet, ZLP, last */
1741 average = temp->average;
1743 if (average > temp->len) {
1744 if (temp->len % temp->max_frame_size) {
1747 average = temp->len;
1751 if (td_next == NULL) {
1752 panic("%s: out of EHCI transfer descriptors!", __func__);
1757 td_next = td->obj_next;
1759 /* check if we are pre-computing */
1763 /* update remaining length */
1765 temp->len -= average;
1769 /* fill out current TD */
1773 htohc32(temp->sc, EHCI_QTD_IOC |
1774 EHCI_QTD_SET_BYTES(average));
1778 if (temp->auto_data_toggle == 0) {
1780 /* update data toggle, ZLP case */
1783 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1787 td->qtd_buffer[0] = 0;
1788 td->qtd_buffer_hi[0] = 0;
1790 td->qtd_buffer[1] = 0;
1791 td->qtd_buffer_hi[1] = 0;
1797 if (temp->auto_data_toggle == 0) {
1799 /* update data toggle */
1801 if (((average + temp->max_frame_size - 1) /
1802 temp->max_frame_size) & 1) {
1804 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1809 /* update remaining length */
1811 temp->len -= average;
1813 /* fill out buffer pointers */
1815 usbd_get_page(temp->pc, buf_offset, &buf_res);
1817 htohc32(temp->sc, buf_res.physaddr);
1818 td->qtd_buffer_hi[0] = 0;
1822 while (average > EHCI_PAGE_SIZE) {
1823 average -= EHCI_PAGE_SIZE;
1824 buf_offset += EHCI_PAGE_SIZE;
1825 usbd_get_page(temp->pc, buf_offset, &buf_res);
1828 buf_res.physaddr & (~0xFFF));
1829 td->qtd_buffer_hi[x] = 0;
1834 * NOTE: The "average" variable is never zero after
1835 * exiting the loop above !
1837 * NOTE: We have to subtract one from the offset to
1838 * ensure that we are computing the physical address
1841 buf_offset += average;
1842 usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
1845 buf_res.physaddr & (~0xFFF));
1846 td->qtd_buffer_hi[x] = 0;
1850 /* link the current TD with the next one */
1851 td->qtd_next = td_next->qtd_self;
1853 td->qtd_altnext = qtd_altnext;
1854 td->alt_next = td_alt_next;
1856 usb_pc_cpu_flush(td->page_cache);
1862 /* setup alt next pointer, if any */
1863 if (temp->last_frame) {
1865 qtd_altnext = terminate;
1867 /* we use this field internally */
1868 td_alt_next = td_next;
1869 if (temp->setup_alt_next) {
1870 qtd_altnext = td_next->qtd_self;
1872 qtd_altnext = terminate;
1877 temp->shortpkt = shortpkt_old;
1878 temp->len = len_old;
1882 temp->td_next = td_next;
1886 ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
1888 struct ehci_std_temp temp;
1889 const struct usb_pipe_methods *methods;
1893 uint32_t qh_endphub;
1896 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1897 xfer->address, UE_GET_ADDR(xfer->endpointno),
1898 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1900 temp.average = xfer->max_hc_frame_size;
1901 temp.max_frame_size = xfer->max_frame_size;
1902 temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
1904 if (xfer->flags_int.doorbell_wait)
1905 ehci_wait_doorbell(temp.sc, xfer->doorbell_match);
1906 xfer->flags_int.doorbell_wait = 1;
1908 /* toggle the DMA set we are using */
1909 xfer->flags_int.curr_dma_set ^= 1;
1911 /* get next DMA set */
1912 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1914 xfer->td_transfer_first = td;
1915 xfer->td_transfer_cache = td;
1919 temp.qtd_status = 0;
1920 temp.last_frame = 0;
1921 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1923 if (xfer->flags_int.control_xfr) {
1924 if (xfer->endpoint->toggle_next) {
1927 htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1929 temp.auto_data_toggle = 0;
1931 temp.auto_data_toggle = 1;
1934 if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
1935 (xfer->xroot->udev->address != 0)) {
1938 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1940 /* check if we should prepend a setup message */
1942 if (xfer->flags_int.control_xfr) {
1943 if (xfer->flags_int.control_hdr) {
1945 xfer->endpoint->toggle_next = 0;
1948 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1949 temp.qtd_status |= htohc32(temp.sc,
1951 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
1952 EHCI_QTD_SET_TOGGLE(0));
1954 temp.len = xfer->frlengths[0];
1955 temp.pc = xfer->frbuffers + 0;
1956 temp.shortpkt = temp.len ? 1 : 0;
1957 /* check for last frame */
1958 if (xfer->nframes == 1) {
1959 /* no STATUS stage yet, SETUP is last */
1960 if (xfer->flags_int.control_act) {
1961 temp.last_frame = 1;
1962 temp.setup_alt_next = 0;
1965 ehci_setup_standard_chain_sub(&temp);
1972 while (x != xfer->nframes) {
1974 /* DATA0 / DATA1 message */
1976 temp.len = xfer->frlengths[x];
1977 temp.pc = xfer->frbuffers + x;
1981 if (x == xfer->nframes) {
1982 if (xfer->flags_int.control_xfr) {
1983 /* no STATUS stage yet, DATA is last */
1984 if (xfer->flags_int.control_act) {
1985 temp.last_frame = 1;
1986 temp.setup_alt_next = 0;
1989 temp.last_frame = 1;
1990 temp.setup_alt_next = 0;
1993 /* keep previous data toggle and error count */
1996 htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1997 EHCI_QTD_SET_TOGGLE(1));
1999 if (temp.len == 0) {
2001 /* make sure that we send an USB packet */
2007 /* regular data transfer */
2009 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
2012 /* set endpoint direction */
2015 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
2016 htohc32(temp.sc, EHCI_QTD_ACTIVE |
2017 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
2018 htohc32(temp.sc, EHCI_QTD_ACTIVE |
2019 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
2021 ehci_setup_standard_chain_sub(&temp);
2024 /* check if we should append a status stage */
2026 if (xfer->flags_int.control_xfr &&
2027 !xfer->flags_int.control_act) {
2030 * Send a DATA1 message and invert the current endpoint
2034 temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
2035 EHCI_QTD_SET_TOGGLE(1));
2037 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
2038 htohc32(temp.sc, EHCI_QTD_ACTIVE |
2039 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
2040 EHCI_QTD_SET_TOGGLE(1)) :
2041 htohc32(temp.sc, EHCI_QTD_ACTIVE |
2042 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
2043 EHCI_QTD_SET_TOGGLE(1));
2048 temp.last_frame = 1;
2049 temp.setup_alt_next = 0;
2051 ehci_setup_standard_chain_sub(&temp);
2055 /* the last TD terminates the transfer: */
2056 td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
2057 td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
2059 usb_pc_cpu_flush(td->page_cache);
2061 /* must have at least one frame! */
2063 xfer->td_transfer_last = td;
2066 if (ehcidebug > 8) {
2067 DPRINTF("nexttog=%d; data before transfer:\n",
2068 xfer->endpoint->toggle_next);
2069 ehci_dump_sqtds(temp.sc,
2070 xfer->td_transfer_first);
2074 methods = xfer->endpoint->methods;
2076 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2078 /* the "qh_link" field is filled when the QH is added */
2081 (EHCI_QH_SET_ADDR(xfer->address) |
2082 EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
2083 EHCI_QH_SET_MPL(xfer->max_packet_size));
2085 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
2086 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
2087 if (methods != &ehci_device_intr_methods)
2088 qh_endp |= EHCI_QH_SET_NRL(8);
2091 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
2092 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
2094 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
2097 if (methods == &ehci_device_ctrl_methods) {
2098 qh_endp |= EHCI_QH_CTL;
2100 if (methods != &ehci_device_intr_methods) {
2101 /* Only try one time per microframe! */
2102 qh_endp |= EHCI_QH_SET_NRL(1);
2106 if (temp.auto_data_toggle == 0) {
2107 /* software computes the data toggle */
2108 qh_endp |= EHCI_QH_DTC;
2111 qh->qh_endp = htohc32(temp.sc, qh_endp);
2114 (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
2115 EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
2116 EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
2117 EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
2118 EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
2120 qh->qh_endphub = htohc32(temp.sc, qh_endphub);
2123 /* fill the overlay qTD */
2125 if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
2127 qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
2129 qh->qh_qtd.qtd_status = 0;
2132 td = xfer->td_transfer_first;
2134 qh->qh_qtd.qtd_next = td->qtd_self;
2135 qh->qh_qtd.qtd_altnext =
2136 htohc32(temp.sc, EHCI_LINK_TERMINATE);
2138 usb_pc_cpu_flush(qh->page_cache);
2140 if (xfer->xroot->udev->flags.self_suspended == 0) {
2141 KKASSERT(xfer->flags_int.onhwqueue == 0);
2142 xfer->flags_int.onhwqueue = 1;
2143 EHCI_APPEND_QH(qh, *qh_last);
2145 ehci_ring_doorbell(temp.sc, NULL);
2149 ehci_root_intr(ehci_softc_t *sc)
2154 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2156 /* clear any old interrupt data */
2157 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2160 m = (sc->sc_noport + 1);
2161 if (m > (8 * sizeof(sc->sc_hub_idata))) {
2162 m = (8 * sizeof(sc->sc_hub_idata));
2164 for (i = 1; i < m; i++) {
2165 /* pick out CHANGE bits from the status register */
2166 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
2167 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2168 DPRINTF("port %d changed\n", i);
2171 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2172 sizeof(sc->sc_hub_idata));
2176 ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2178 uint32_t nframes = xfer->nframes;
2180 uint32_t *plen = xfer->frlengths;
2182 ehci_sitd_t *td = xfer->td_transfer_first;
2183 ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
2185 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2186 xfer, xfer->endpoint);
2188 while (xfer->flags_int.onhwqueue && nframes--) {
2190 panic("%s:%d: out of TD's\n",
2191 __func__, __LINE__);
2193 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2194 pp_last = &sc->sc_isoc_fs_p_last[0];
2197 if (ehcidebug > 15) {
2198 DPRINTF("isoc FS-TD\n");
2199 ehci_dump_sitd(sc, td);
2202 usb_pc_cpu_invalidate(td->page_cache);
2203 status = hc32toh(sc, td->sitd_status);
2205 len = EHCI_SITD_GET_LEN(status);
2207 DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2217 /* remove FS-TD from schedule */
2218 EHCI_REMOVE_FS_TD(td, *pp_last);
2224 xfer->flags_int.onhwqueue = 0;
2226 xfer->aframes = xfer->nframes;
2230 ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2232 uint32_t nframes = xfer->nframes;
2234 uint32_t *plen = xfer->frlengths;
2237 ehci_itd_t *td = xfer->td_transfer_first;
2238 ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
2240 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2241 xfer, xfer->endpoint);
2243 while (nframes && xfer->flags_int.onhwqueue) {
2245 panic("%s:%d: out of TD's\n",
2246 __func__, __LINE__);
2248 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2249 pp_last = &sc->sc_isoc_hs_p_last[0];
2252 if (ehcidebug > 15) {
2253 DPRINTF("isoc HS-TD\n");
2254 ehci_dump_itd(sc, td);
2258 usb_pc_cpu_invalidate(td->page_cache);
2259 status = hc32toh(sc, td->itd_status[td_no]);
2261 len = EHCI_ITD_GET_LEN(status);
2263 DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2265 if (xfer->endpoint->usb_smask & (1 << td_no)) {
2269 * The length is valid. NOTE: The
2270 * complete length is written back
2271 * into the status field, and not the
2272 * remainder like with other transfer
2276 /* Invalid length - truncate */
2287 if ((td_no == 8) || (nframes == 0)) {
2288 /* remove HS-TD from schedule */
2289 EHCI_REMOVE_HS_TD(td, *pp_last);
2296 xfer->aframes = xfer->nframes;
2297 xfer->flags_int.onhwqueue = 0;
2300 /* NOTE: "done" can be run two times in a row,
2301 * from close and from interrupt
2304 ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
2306 const struct usb_pipe_methods *methods = xfer->endpoint->methods;
2307 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2309 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2311 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2312 xfer, xfer->endpoint, error);
2315 * Don't throw away a timed-out transfer without safely removing
2316 * it from the controller's notice!
2318 if (error != USB_ERR_NORMAL_COMPLETION)
2319 ehci_stop_xfer(xfer);
2321 if (xfer->flags_int.onhwqueue) {
2322 if ((methods == &ehci_device_bulk_methods) ||
2323 (methods == &ehci_device_ctrl_methods)) {
2325 if (ehcidebug > 8) {
2326 DPRINTF("nexttog=%d; data after transfer:\n",
2327 xfer->endpoint->toggle_next);
2329 xfer->td_transfer_first);
2333 EHCI_REMOVE_QH(sc, xfer->qh_start[xfer->flags_int.curr_dma_set],
2334 sc->sc_async_p_last);
2336 if (methods == &ehci_device_intr_methods) {
2337 EHCI_REMOVE_QH(sc, xfer->qh_start[xfer->flags_int.curr_dma_set],
2338 sc->sc_intr_p_last[xfer->qh_pos]);
2340 xfer->flags_int.onhwqueue = 0;
2341 ehci_ring_doorbell(sc, xfer);
2345 * Only finish isochronous transfers once which will update
2346 * "xfer->frlengths".
2348 if (xfer->td_transfer_first &&
2349 xfer->td_transfer_last) {
2350 if (methods == &ehci_device_isoc_fs_methods) {
2351 ehci_isoc_fs_done(sc, xfer);
2353 if (methods == &ehci_device_isoc_hs_methods) {
2354 ehci_isoc_hs_done(sc, xfer);
2356 xfer->td_transfer_first = NULL;
2357 xfer->td_transfer_last = NULL;
2359 /* dequeue transfer and start next transfer */
2360 usbd_transfer_done(xfer, error);
2363 /*------------------------------------------------------------------------*
2365 *------------------------------------------------------------------------*/
2367 ehci_device_bulk_open(struct usb_xfer *xfer)
2373 ehci_device_bulk_close(struct usb_xfer *xfer)
2375 ehci_device_done(xfer, USB_ERR_CANCELLED);
2379 ehci_device_bulk_enter(struct usb_xfer *xfer)
2385 ehci_device_bulk_start(struct usb_xfer *xfer)
2387 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2392 /* setup TD's and QH */
2393 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2395 /* put transfer on interrupt queue */
2396 ehci_transfer_intr_enqueue(xfer);
2400 * XXX Certain nVidia chipsets choke when using the IAAD
2401 * feature too frequently.
2403 * XXX This code has been moved to another place which handles
2404 * more cases. EHCI appears to need the doorbell to be hit
2405 * in the other queueing cases or it can wind up not
2406 * processing the queue.
2408 if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
2411 /* XXX Performance quirk: Some Host Controllers have a too low
2412 * interrupt rate. Issue an IAAD to stimulate the Host
2413 * Controller after queueing the BULK transfer.
2415 temp = EOREAD4(sc, EHCI_USBCMD);
2416 if (!(temp & EHCI_CMD_IAAD))
2417 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
2421 static const struct usb_pipe_methods ehci_device_bulk_methods =
2423 .open = ehci_device_bulk_open,
2424 .close = ehci_device_bulk_close,
2425 .enter = ehci_device_bulk_enter,
2426 .start = ehci_device_bulk_start,
2429 /*------------------------------------------------------------------------*
2430 * ehci control support
2431 *------------------------------------------------------------------------*/
2433 ehci_device_ctrl_open(struct usb_xfer *xfer)
2439 ehci_device_ctrl_close(struct usb_xfer *xfer)
2441 ehci_device_done(xfer, USB_ERR_CANCELLED);
2445 ehci_device_ctrl_enter(struct usb_xfer *xfer)
2451 ehci_device_ctrl_start(struct usb_xfer *xfer)
2453 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2455 /* setup TD's and QH */
2456 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2458 /* put transfer on interrupt queue */
2459 ehci_transfer_intr_enqueue(xfer);
2462 static const struct usb_pipe_methods ehci_device_ctrl_methods =
2464 .open = ehci_device_ctrl_open,
2465 .close = ehci_device_ctrl_close,
2466 .enter = ehci_device_ctrl_enter,
2467 .start = ehci_device_ctrl_start,
2470 /*------------------------------------------------------------------------*
2471 * ehci interrupt support
2472 *------------------------------------------------------------------------*/
2474 ehci_device_intr_open(struct usb_xfer *xfer)
2476 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2481 usb_hs_bandwidth_alloc(xfer);
2484 * Find the best QH position corresponding to the given interval:
2488 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
2490 if (xfer->interval >= bit) {
2494 if (sc->sc_intr_stat[x] <
2495 sc->sc_intr_stat[best]) {
2505 sc->sc_intr_stat[best]++;
2506 xfer->qh_pos = best;
2508 DPRINTFN(3, "best=%d interval=%d\n",
2509 best, xfer->interval);
2513 ehci_device_intr_close(struct usb_xfer *xfer)
2515 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2517 sc->sc_intr_stat[xfer->qh_pos]--;
2519 ehci_device_done(xfer, USB_ERR_CANCELLED);
2521 /* bandwidth must be freed after device done */
2522 usb_hs_bandwidth_free(xfer);
2526 ehci_device_intr_enter(struct usb_xfer *xfer)
2532 ehci_device_intr_start(struct usb_xfer *xfer)
2534 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2536 /* setup TD's and QH */
2537 ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
2539 /* put transfer on interrupt queue */
2540 ehci_transfer_intr_enqueue(xfer);
2543 static const struct usb_pipe_methods ehci_device_intr_methods =
2545 .open = ehci_device_intr_open,
2546 .close = ehci_device_intr_close,
2547 .enter = ehci_device_intr_enter,
2548 .start = ehci_device_intr_start,
2551 /*------------------------------------------------------------------------*
2552 * ehci full speed isochronous support
2553 *------------------------------------------------------------------------*/
2555 ehci_device_isoc_fs_open(struct usb_xfer *xfer)
2557 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2559 uint32_t sitd_portaddr;
2563 EHCI_SITD_SET_ADDR(xfer->address) |
2564 EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
2565 EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
2566 EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
2568 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2569 sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
2571 sitd_portaddr = htohc32(sc, sitd_portaddr);
2573 /* initialize all TD's */
2575 for (ds = 0; ds != 2; ds++) {
2577 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2579 td->sitd_portaddr = sitd_portaddr;
2582 * TODO: make some kind of automatic
2583 * SMASK/CMASK selection based on micro-frame
2586 * micro-frame usage (8 microframes per 1ms)
2588 td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
2590 usb_pc_cpu_flush(td->page_cache);
2596 ehci_device_isoc_fs_close(struct usb_xfer *xfer)
2598 ehci_device_done(xfer, USB_ERR_CANCELLED);
2602 ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
2604 struct usb_page_search buf_res;
2605 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2607 ehci_sitd_t *td_last = NULL;
2608 ehci_sitd_t **pp_last;
2610 uint32_t buf_offset;
2623 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2624 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2626 /* get the current frame index */
2628 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2631 * check if the frame index is within the window where the frames
2634 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2635 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2637 if ((xfer->endpoint->is_synced == 0) ||
2638 (buf_offset < xfer->nframes)) {
2640 * If there is data underflow or the pipe queue is empty we
2641 * schedule the transfer a few frames ahead of the current
2642 * frame position. Else two isochronous transfers might
2645 xfer->endpoint->isoc_next = (nframes + 3) &
2646 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2647 xfer->endpoint->is_synced = 1;
2648 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2651 * compute how many milliseconds the insertion is ahead of the
2652 * current frame position:
2654 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2655 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2658 * pre-compute when the isochronous transfer will be finished:
2660 xfer->isoc_time_complete =
2661 usb_isoc_time_expand(&sc->sc_bus, nframes) +
2662 buf_offset + xfer->nframes;
2664 /* get the real number of frames */
2666 nframes = xfer->nframes;
2670 plen = xfer->frlengths;
2672 /* toggle the DMA set we are using */
2673 xfer->flags_int.curr_dma_set ^= 1;
2675 /* get next DMA set */
2676 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2677 xfer->td_transfer_first = td;
2679 pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
2681 /* store starting position */
2683 xfer->qh_pos = xfer->endpoint->isoc_next;
2687 panic("%s:%d: out of TD's\n",
2688 __func__, __LINE__);
2690 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2691 pp_last = &sc->sc_isoc_fs_p_last[0];
2693 /* reuse sitd_portaddr and sitd_back from last transfer */
2695 if (*plen > xfer->max_frame_size) {
2699 kprintf("%s: frame length(%d) exceeds %d "
2700 "bytes (frame truncated)\n",
2702 xfer->max_frame_size);
2705 *plen = xfer->max_frame_size;
2708 /* allocate a slot */
2710 sa = usbd_fs_isoc_schedule_alloc_slot(xfer,
2711 xfer->isoc_time_complete - nframes - 1);
2715 * Schedule is FULL, set length to zero:
2719 sa = USB_FS_ISOC_UFRAME_MAX - 1;
2723 * only call "usbd_get_page()" when we have a
2726 usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2727 td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
2728 buf_offset += *plen;
2730 * NOTE: We need to subtract one from the offset so
2731 * that we are on a valid page!
2733 usbd_get_page(xfer->frbuffers, buf_offset - 1,
2735 temp = buf_res.physaddr & ~0xFFF;
2741 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
2744 temp |= 1; /* T-count = 1, TP = ALL */
2749 temp |= tlen; /* T-count = [1..6] */
2750 temp |= 8; /* TP = Begin */
2762 sa = (sb - sa) & 0x3F;
2765 sb = (-(4 << sa)) & 0xFE;
2766 sa = (1 << sa) & 0x3F;
2769 sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
2770 EHCI_SITD_SET_CMASK(sb));
2772 td->sitd_bp[1] = htohc32(sc, temp);
2774 td->sitd_mask = htohc32(sc, sitd_mask);
2777 td->sitd_status = htohc32(sc,
2780 EHCI_SITD_SET_LEN(*plen));
2782 td->sitd_status = htohc32(sc,
2784 EHCI_SITD_SET_LEN(*plen));
2786 usb_pc_cpu_flush(td->page_cache);
2789 if (ehcidebug > 15) {
2790 DPRINTF("FS-TD %d\n", nframes);
2791 ehci_dump_sitd(sc, td);
2794 /* insert TD into schedule */
2795 xfer->flags_int.onhwqueue = 1;
2796 EHCI_APPEND_FS_TD(td, *pp_last);
2804 xfer->td_transfer_last = td_last;
2806 /* update isoc_next */
2807 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
2808 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2811 * We don't allow cancelling of the SPLIT transaction USB FULL
2812 * speed transfer, because it disturbs the bandwidth
2813 * computation algorithm.
2815 xfer->flags_int.can_cancel_immed = 0;
2816 ehci_ring_doorbell(sc, NULL);
2820 ehci_device_isoc_fs_start(struct usb_xfer *xfer)
2823 * We don't allow cancelling of the SPLIT transaction USB FULL
2824 * speed transfer, because it disturbs the bandwidth
2825 * computation algorithm.
2827 xfer->flags_int.can_cancel_immed = 0;
2829 /* set a default timeout */
2830 if (xfer->timeout == 0)
2831 xfer->timeout = 1000; /* ms */
2833 /* put transfer on interrupt queue */
2834 ehci_transfer_intr_enqueue(xfer);
2837 static const struct usb_pipe_methods ehci_device_isoc_fs_methods =
2839 .open = ehci_device_isoc_fs_open,
2840 .close = ehci_device_isoc_fs_close,
2841 .enter = ehci_device_isoc_fs_enter,
2842 .start = ehci_device_isoc_fs_start,
2845 /*------------------------------------------------------------------------*
2846 * ehci high speed isochronous support
2847 *------------------------------------------------------------------------*/
2849 ehci_device_isoc_hs_open(struct usb_xfer *xfer)
2851 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2856 usb_hs_bandwidth_alloc(xfer);
2858 /* initialize all TD's */
2860 for (ds = 0; ds != 2; ds++) {
2862 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2864 /* set TD inactive */
2865 td->itd_status[0] = 0;
2866 td->itd_status[1] = 0;
2867 td->itd_status[2] = 0;
2868 td->itd_status[3] = 0;
2869 td->itd_status[4] = 0;
2870 td->itd_status[5] = 0;
2871 td->itd_status[6] = 0;
2872 td->itd_status[7] = 0;
2874 /* set endpoint and address */
2875 td->itd_bp[0] = htohc32(sc,
2876 EHCI_ITD_SET_ADDR(xfer->address) |
2877 EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
2880 EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
2883 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2884 temp |= EHCI_ITD_SET_DIR_IN;
2886 /* set maximum packet size */
2887 td->itd_bp[1] = htohc32(sc, temp);
2889 /* set transfer multiplier */
2890 td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
2892 usb_pc_cpu_flush(td->page_cache);
2898 ehci_device_isoc_hs_close(struct usb_xfer *xfer)
2900 ehci_device_done(xfer, USB_ERR_CANCELLED);
2902 /* bandwidth must be freed after device done */
2903 usb_hs_bandwidth_free(xfer);
2907 ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
2909 struct usb_page_search buf_res;
2910 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2912 ehci_itd_t *td_last = NULL;
2913 ehci_itd_t **pp_last;
2914 bus_size_t page_addr;
2917 uint32_t buf_offset;
2919 uint32_t itd_offset[8 + 1];
2923 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
2930 DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n",
2931 xfer, xfer->endpoint->isoc_next, xfer->nframes, (int)shift);
2933 /* get the current frame index */
2935 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2938 * check if the frame index is within the window where the frames
2941 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2942 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2944 if ((xfer->endpoint->is_synced == 0) ||
2945 (buf_offset < (((xfer->nframes << shift) + 7) / 8))) {
2947 * If there is data underflow or the pipe queue is empty we
2948 * schedule the transfer a few frames ahead of the current
2949 * frame position. Else two isochronous transfers might
2952 xfer->endpoint->isoc_next = (nframes + 3) &
2953 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2954 xfer->endpoint->is_synced = 1;
2955 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2958 * compute how many milliseconds the insertion is ahead of the
2959 * current frame position:
2961 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2962 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2965 * pre-compute when the isochronous transfer will be finished:
2967 xfer->isoc_time_complete =
2968 usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2969 (((xfer->nframes << shift) + 7) / 8);
2971 /* get the real number of frames */
2973 nframes = xfer->nframes;
2978 plen = xfer->frlengths;
2980 /* toggle the DMA set we are using */
2981 xfer->flags_int.curr_dma_set ^= 1;
2983 /* get next DMA set */
2984 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2985 xfer->td_transfer_first = td;
2987 pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
2989 /* store starting position */
2991 xfer->qh_pos = xfer->endpoint->isoc_next;
2995 panic("%s:%d: out of TD's\n",
2996 __func__, __LINE__);
2998 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2999 pp_last = &sc->sc_isoc_hs_p_last[0];
3002 if (*plen > xfer->max_frame_size) {
3006 kprintf("%s: frame length(%d) exceeds %d bytes "
3007 "(frame truncated)\n",
3008 __func__, *plen, xfer->max_frame_size);
3011 *plen = xfer->max_frame_size;
3014 if (xfer->endpoint->usb_smask & (1 << td_no)) {
3015 status = (EHCI_ITD_SET_LEN(*plen) |
3017 EHCI_ITD_SET_PG(0));
3018 td->itd_status[td_no] = htohc32(sc, status);
3019 itd_offset[td_no] = buf_offset;
3020 buf_offset += *plen;
3024 td->itd_status[td_no] = 0; /* not active */
3025 itd_offset[td_no] = buf_offset;
3030 if ((td_no == 8) || (nframes == 0)) {
3032 /* the rest of the transfers are not active, if any */
3033 for (x = td_no; x != 8; x++) {
3034 td->itd_status[x] = 0; /* not active */
3037 /* check if there is any data to be transferred */
3038 if (itd_offset[0] != buf_offset) {
3040 itd_offset[td_no] = buf_offset;
3042 /* get first page offset */
3043 usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
3044 /* get page address */
3045 page_addr = buf_res.physaddr & ~0xFFF;
3046 /* update page address */
3047 td->itd_bp[0] &= htohc32(sc, 0xFFF);
3048 td->itd_bp[0] |= htohc32(sc, page_addr);
3050 for (x = 0; x != td_no; x++) {
3051 /* set page number and page offset */
3052 status = (EHCI_ITD_SET_PG(page_no) |
3053 (buf_res.physaddr & 0xFFF));
3054 td->itd_status[x] |= htohc32(sc, status);
3056 /* get next page offset */
3057 if (itd_offset[x + 1] == buf_offset) {
3059 * We subtract one so that
3060 * we don't go off the last
3063 usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
3065 usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
3068 /* check if we need a new page */
3069 if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
3070 /* new page needed */
3071 page_addr = buf_res.physaddr & ~0xFFF;
3073 panic("%s: too many pages\n", __func__);
3076 /* update page address */
3077 td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
3078 td->itd_bp[page_no] |= htohc32(sc, page_addr);
3082 /* set IOC bit if we are complete */
3084 td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
3086 usb_pc_cpu_flush(td->page_cache);
3088 if (ehcidebug > 15) {
3089 DPRINTF("HS-TD %d\n", nframes);
3090 ehci_dump_itd(sc, td);
3093 /* insert TD into schedule */
3094 xfer->flags_int.onhwqueue = 1;
3095 EHCI_APPEND_HS_TD(td, *pp_last);
3104 xfer->td_transfer_last = td_last;
3106 /* update isoc_next */
3107 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
3108 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
3109 ehci_ring_doorbell(sc, NULL);
3113 ehci_device_isoc_hs_start(struct usb_xfer *xfer)
3115 /* put transfer on interrupt queue */
3116 ehci_transfer_intr_enqueue(xfer);
3119 static const struct usb_pipe_methods ehci_device_isoc_hs_methods =
3121 .open = ehci_device_isoc_hs_open,
3122 .close = ehci_device_isoc_hs_close,
3123 .enter = ehci_device_isoc_hs_enter,
3124 .start = ehci_device_isoc_hs_start,
3127 /*------------------------------------------------------------------------*
3128 * ehci root control support
3129 *------------------------------------------------------------------------*
3130 * Simulate a hardware hub by handling all the necessary requests.
3131 *------------------------------------------------------------------------*/
3134 struct usb_device_descriptor ehci_devd =
3136 sizeof(struct usb_device_descriptor),
3137 UDESC_DEVICE, /* type */
3138 {0x00, 0x02}, /* USB version */
3139 UDCLASS_HUB, /* class */
3140 UDSUBCLASS_HUB, /* subclass */
3141 UDPROTO_HSHUBSTT, /* protocol */
3142 64, /* max packet */
3143 {0}, {0}, {0x00, 0x01}, /* device id */
3144 1, 2, 0, /* string indicies */
3145 1 /* # of configurations */
3149 struct usb_device_qualifier ehci_odevd =
3151 sizeof(struct usb_device_qualifier),
3152 UDESC_DEVICE_QUALIFIER, /* type */
3153 {0x00, 0x02}, /* USB version */
3154 UDCLASS_HUB, /* class */
3155 UDSUBCLASS_HUB, /* subclass */
3156 UDPROTO_FSHUB, /* protocol */
3158 0, /* # of configurations */
3162 static const struct ehci_config_desc ehci_confd = {
3164 .bLength = sizeof(struct usb_config_descriptor),
3165 .bDescriptorType = UDESC_CONFIG,
3166 .wTotalLength[0] = sizeof(ehci_confd),
3168 .bConfigurationValue = 1,
3169 .iConfiguration = 0,
3170 .bmAttributes = UC_SELF_POWERED,
3171 .bMaxPower = 0 /* max power */
3174 .bLength = sizeof(struct usb_interface_descriptor),
3175 .bDescriptorType = UDESC_INTERFACE,
3177 .bInterfaceClass = UICLASS_HUB,
3178 .bInterfaceSubClass = UISUBCLASS_HUB,
3179 .bInterfaceProtocol = 0,
3182 .bLength = sizeof(struct usb_endpoint_descriptor),
3183 .bDescriptorType = UDESC_ENDPOINT,
3184 .bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
3185 .bmAttributes = UE_INTERRUPT,
3186 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
3192 struct usb_hub_descriptor ehci_hubd =
3194 .bDescLength = 0, /* dynamic length */
3195 .bDescriptorType = UDESC_HUB,
3199 ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
3204 DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
3206 port = EHCI_PORTSC(index);
3207 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3208 EOWRITE4(sc, port, v | EHCI_PS_PO);
3212 ehci_roothub_exec(struct usb_device *udev,
3213 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3215 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3216 const char *str_ptr;
3226 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
3229 ptr = (const void *)&sc->sc_hub_desc;
3233 value = UGETW(req->wValue);
3234 index = UGETW(req->wIndex);
3236 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3237 "wValue=0x%04x wIndex=0x%04x\n",
3238 req->bmRequestType, req->bRequest,
3239 UGETW(req->wLength), value, index);
3241 #define C(x,y) ((x) | ((y) << 8))
3242 switch (C(req->bRequest, req->bmRequestType)) {
3243 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3244 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3245 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3247 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3248 * for the integrated root hub.
3251 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3253 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3255 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3256 switch (value >> 8) {
3258 if ((value & 0xff) != 0) {
3259 err = USB_ERR_IOERROR;
3262 len = sizeof(ehci_devd);
3263 ptr = (const void *)&ehci_devd;
3266 * We can't really operate at another speed,
3267 * but the specification says we need this
3270 case UDESC_DEVICE_QUALIFIER:
3271 if ((value & 0xff) != 0) {
3272 err = USB_ERR_IOERROR;
3275 len = sizeof(ehci_odevd);
3276 ptr = (const void *)&ehci_odevd;
3280 if ((value & 0xff) != 0) {
3281 err = USB_ERR_IOERROR;
3284 len = sizeof(ehci_confd);
3285 ptr = (const void *)&ehci_confd;
3289 switch (value & 0xff) {
3290 case 0: /* Language table */
3294 case 1: /* Vendor */
3295 str_ptr = sc->sc_vendor;
3298 case 2: /* Product */
3299 str_ptr = "EHCI root HUB";
3307 len = usb_make_str_desc(
3308 sc->sc_hub_desc.temp,
3309 sizeof(sc->sc_hub_desc.temp),
3313 err = USB_ERR_IOERROR;
3317 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3319 sc->sc_hub_desc.temp[0] = 0;
3321 case C(UR_GET_STATUS, UT_READ_DEVICE):
3323 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3325 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3326 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3328 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3330 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3331 if (value >= EHCI_MAX_DEVICES) {
3332 err = USB_ERR_IOERROR;
3335 sc->sc_addr = value;
3337 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3338 if ((value != 0) && (value != 1)) {
3339 err = USB_ERR_IOERROR;
3342 sc->sc_conf = value;
3344 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3346 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3347 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3348 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3349 err = USB_ERR_IOERROR;
3351 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3353 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3356 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3358 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3359 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3362 (index > sc->sc_noport)) {
3363 err = USB_ERR_IOERROR;
3366 port = EHCI_PORTSC(index);
3367 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3369 case UHF_PORT_ENABLE:
3370 EOWRITE4(sc, port, v & ~EHCI_PS_PE);
3372 case UHF_PORT_SUSPEND:
3373 if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
3376 * waking up a High Speed device is rather
3379 EOWRITE4(sc, port, v | EHCI_PS_FPR);
3381 /* wait 20ms for resume sequence to complete */
3382 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 50);
3384 EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
3385 EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
3387 /* 4ms settle time */
3388 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 250);
3390 case UHF_PORT_POWER:
3391 EOWRITE4(sc, port, v & ~EHCI_PS_PP);
3394 DPRINTFN(3, "clear port test "
3397 case UHF_PORT_INDICATOR:
3398 DPRINTFN(3, "clear port ind "
3400 EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
3402 case UHF_C_PORT_CONNECTION:
3403 EOWRITE4(sc, port, v | EHCI_PS_CSC);
3405 case UHF_C_PORT_ENABLE:
3406 EOWRITE4(sc, port, v | EHCI_PS_PEC);
3408 case UHF_C_PORT_SUSPEND:
3409 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3411 case UHF_C_PORT_OVER_CURRENT:
3412 EOWRITE4(sc, port, v | EHCI_PS_OCC);
3414 case UHF_C_PORT_RESET:
3418 err = USB_ERR_IOERROR;
3422 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3423 if ((value & 0xff) != 0) {
3424 err = USB_ERR_IOERROR;
3427 v = EREAD4(sc, EHCI_HCSPARAMS);
3429 sc->sc_hub_desc.hubd = ehci_hubd;
3430 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3432 if (EHCI_HCS_PPC(v))
3433 i = UHD_PWR_INDIVIDUAL;
3435 i = UHD_PWR_NO_SWITCH;
3437 if (EHCI_HCS_P_INDICATOR(v))
3440 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3441 /* XXX can't find out? */
3442 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
3443 /* XXX don't know if ports are removable or not */
3444 sc->sc_hub_desc.hubd.bDescLength =
3445 8 + ((sc->sc_noport + 7) / 8);
3446 len = sc->sc_hub_desc.hubd.bDescLength;
3448 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3450 memset(sc->sc_hub_desc.temp, 0, 16);
3452 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3453 DPRINTFN(9, "get port status i=%d\n",
3456 (index > sc->sc_noport)) {
3457 err = USB_ERR_IOERROR;
3460 v = EOREAD4(sc, EHCI_PORTSC(index));
3461 DPRINTFN(9, "port status=0x%04x\n", v);
3462 if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
3463 if ((v & 0xc000000) == 0x8000000)
3465 else if ((v & 0xc000000) == 0x4000000)
3473 i |= UPS_CURRENT_CONNECT_STATUS;
3475 i |= UPS_PORT_ENABLED;
3476 if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
3478 if (v & EHCI_PS_OCA)
3479 i |= UPS_OVERCURRENT_INDICATOR;
3483 i |= UPS_PORT_POWER;
3484 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3486 if (v & EHCI_PS_CSC)
3487 i |= UPS_C_CONNECT_STATUS;
3488 if (v & EHCI_PS_PEC)
3489 i |= UPS_C_PORT_ENABLED;
3490 if (v & EHCI_PS_OCC)
3491 i |= UPS_C_OVERCURRENT_INDICATOR;
3492 if (v & EHCI_PS_FPR)
3495 i |= UPS_C_PORT_RESET;
3496 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3497 len = sizeof(sc->sc_hub_desc.ps);
3499 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3500 err = USB_ERR_IOERROR;
3502 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3504 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3506 (index > sc->sc_noport)) {
3507 err = USB_ERR_IOERROR;
3510 port = EHCI_PORTSC(index);
3511 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3513 case UHF_PORT_ENABLE:
3514 EOWRITE4(sc, port, v | EHCI_PS_PE);
3516 case UHF_PORT_SUSPEND:
3517 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3519 case UHF_PORT_RESET:
3520 DPRINTFN(6, "reset port %d\n", index);
3522 if (ehcinohighspeed) {
3524 * Connect USB device to companion
3527 ehci_disown(sc, index, 1);
3531 if (EHCI_PS_IS_LOWSPEED(v) &&
3532 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3533 /* Low speed device, give up ownership. */
3534 ehci_disown(sc, index, 1);
3537 /* Start reset sequence. */
3538 v &= ~(EHCI_PS_PE | EHCI_PS_PR);
3539 EOWRITE4(sc, port, v | EHCI_PS_PR);
3541 /* Wait for reset to complete. */
3542 usb_pause_mtx(&sc->sc_bus.bus_lock,
3543 USB_MS_TO_TICKS(usb_port_root_reset_delay));
3545 /* Terminate reset sequence. */
3546 if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
3547 EOWRITE4(sc, port, v);
3549 /* Wait for HC to complete reset. */
3550 usb_pause_mtx(&sc->sc_bus.bus_lock,
3551 USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
3553 v = EOREAD4(sc, port);
3554 DPRINTF("ehci after reset, status=0x%08x\n", v);
3555 if (v & EHCI_PS_PR) {
3556 device_printf(sc->sc_bus.bdev,
3557 "port reset timeout\n");
3558 err = USB_ERR_TIMEOUT;
3561 if (!(v & EHCI_PS_PE) &&
3562 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3563 /* Not a high speed device, give up ownership.*/
3564 ehci_disown(sc, index, 0);
3568 DPRINTF("ehci port %d reset, status = 0x%08x\n",
3572 case UHF_PORT_POWER:
3573 DPRINTFN(3, "set port power %d\n", index);
3574 EOWRITE4(sc, port, v | EHCI_PS_PP);
3578 DPRINTFN(3, "set port test %d\n", index);
3581 case UHF_PORT_INDICATOR:
3582 DPRINTFN(3, "set port ind %d\n", index);
3583 EOWRITE4(sc, port, v | EHCI_PS_PIC);
3587 err = USB_ERR_IOERROR;
3591 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3592 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3593 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3594 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3597 err = USB_ERR_IOERROR;
3607 ehci_xfer_setup(struct usb_setup_params *parm)
3609 struct usb_page_search page_info;
3610 struct usb_page_cache *pc;
3612 struct usb_xfer *xfer;
3620 sc = EHCI_BUS2SC(parm->udev->bus);
3621 xfer = parm->curr_xfer;
3629 * compute maximum number of some structures
3631 if (parm->methods == &ehci_device_ctrl_methods) {
3634 * The proof for the "nqtd" formula is illustrated like
3637 * +------------------------------------+
3641 * | | xxx | x | frm 0 |
3643 * | | xxx | xx | frm 1 |
3646 * +------------------------------------+
3648 * "xxx" means a completely full USB transfer descriptor
3650 * "x" and "xx" means a short USB packet
3652 * For the remainder of an USB transfer modulo
3653 * "max_data_length" we need two USB transfer descriptors.
3654 * One to transfer the remaining data and one to finalise
3655 * with a zero length packet in case the "force_short_xfer"
3656 * flag is set. We only need two USB transfer descriptors in
3657 * the case where the transfer length of the first one is a
3658 * factor of "max_frame_size". The rest of the needed USB
3659 * transfer descriptors is given by the buffer size divided
3660 * by the maximum data payload.
3662 parm->hc_max_packet_size = 0x400;
3663 parm->hc_max_packet_count = 1;
3664 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3665 xfer->flags_int.bdma_enable = 1;
3667 usbd_transfer_setup_sub(parm);
3670 nqtd = ((2 * xfer->nframes) + 1 /* STATUS */
3671 + (xfer->max_data_length / xfer->max_hc_frame_size));
3672 } else if (parm->methods == &ehci_device_bulk_methods) {
3674 parm->hc_max_packet_size = 0x400;
3675 parm->hc_max_packet_count = 1;
3676 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3677 xfer->flags_int.bdma_enable = 1;
3679 usbd_transfer_setup_sub(parm);
3682 nqtd = ((2 * xfer->nframes)
3683 + (xfer->max_data_length / xfer->max_hc_frame_size));
3685 } else if (parm->methods == &ehci_device_intr_methods) {
3687 if (parm->speed == USB_SPEED_HIGH) {
3688 parm->hc_max_packet_size = 0x400;
3689 parm->hc_max_packet_count = 3;
3690 } else if (parm->speed == USB_SPEED_FULL) {
3691 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
3692 parm->hc_max_packet_count = 1;
3694 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
3695 parm->hc_max_packet_count = 1;
3698 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3699 xfer->flags_int.bdma_enable = 1;
3701 usbd_transfer_setup_sub(parm);
3704 nqtd = ((2 * xfer->nframes)
3705 + (xfer->max_data_length / xfer->max_hc_frame_size));
3707 } else if (parm->methods == &ehci_device_isoc_fs_methods) {
3709 parm->hc_max_packet_size = 0x3FF;
3710 parm->hc_max_packet_count = 1;
3711 parm->hc_max_frame_size = 0x3FF;
3712 xfer->flags_int.bdma_enable = 1;
3714 usbd_transfer_setup_sub(parm);
3716 nsitd = xfer->nframes;
3718 } else if (parm->methods == &ehci_device_isoc_hs_methods) {
3720 parm->hc_max_packet_size = 0x400;
3721 parm->hc_max_packet_count = 3;
3722 parm->hc_max_frame_size = 0xC00;
3723 xfer->flags_int.bdma_enable = 1;
3725 usbd_transfer_setup_sub(parm);
3727 nitd = ((xfer->nframes + 7) / 8) <<
3728 usbd_xfer_get_fps_shift(xfer);
3732 parm->hc_max_packet_size = 0x400;
3733 parm->hc_max_packet_count = 1;
3734 parm->hc_max_frame_size = 0x400;
3736 usbd_transfer_setup_sub(parm);
3745 * Allocate queue heads and transfer descriptors
3749 if (usbd_transfer_setup_sub_malloc(
3750 parm, &pc, sizeof(ehci_itd_t),
3751 EHCI_ITD_ALIGN, nitd)) {
3752 parm->err = USB_ERR_NOMEM;
3756 for (n = 0; n != nitd; n++) {
3759 usbd_get_page(pc + n, 0, &page_info);
3761 td = page_info.buffer;
3764 td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
3765 td->obj_next = last_obj;
3766 td->page_cache = pc + n;
3770 usb_pc_cpu_flush(pc + n);
3773 if (usbd_transfer_setup_sub_malloc(
3774 parm, &pc, sizeof(ehci_sitd_t),
3775 EHCI_SITD_ALIGN, nsitd)) {
3776 parm->err = USB_ERR_NOMEM;
3780 for (n = 0; n != nsitd; n++) {
3783 usbd_get_page(pc + n, 0, &page_info);
3785 td = page_info.buffer;
3788 td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
3789 td->obj_next = last_obj;
3790 td->page_cache = pc + n;
3794 usb_pc_cpu_flush(pc + n);
3797 if (usbd_transfer_setup_sub_malloc(
3798 parm, &pc, sizeof(ehci_qtd_t),
3799 EHCI_QTD_ALIGN, nqtd)) {
3800 parm->err = USB_ERR_NOMEM;
3804 for (n = 0; n != nqtd; n++) {
3807 usbd_get_page(pc + n, 0, &page_info);
3809 qtd = page_info.buffer;
3812 qtd->qtd_self = htohc32(sc, page_info.physaddr);
3813 qtd->obj_next = last_obj;
3814 qtd->page_cache = pc + n;
3818 usb_pc_cpu_flush(pc + n);
3821 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3825 if (usbd_transfer_setup_sub_malloc(
3826 parm, &pc, sizeof(ehci_qh_t),
3827 EHCI_QH_ALIGN, nqh)) {
3828 parm->err = USB_ERR_NOMEM;
3832 for (n = 0; n != nqh; n++) {
3835 usbd_get_page(pc + n, 0, &page_info);
3837 qh = page_info.buffer;
3840 qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
3841 qh->obj_next = last_obj;
3842 qh->page_cache = pc + n;
3846 usb_pc_cpu_flush(pc + n);
3849 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3851 if (!xfer->flags_int.curr_dma_set) {
3852 xfer->flags_int.curr_dma_set = 1;
3858 ehci_xfer_unsetup(struct usb_xfer *xfer)
3864 ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3865 struct usb_endpoint *ep)
3867 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3869 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3871 edesc->bEndpointAddress, udev->flags.usb_mode,
3874 if (udev->device_index != sc->sc_addr) {
3876 if ((udev->speed != USB_SPEED_HIGH) &&
3877 ((udev->hs_hub_addr == 0) ||
3878 (udev->hs_port_no == 0) ||
3879 (udev->parent_hs_hub == NULL) ||
3880 (udev->parent_hs_hub->hub == NULL))) {
3881 /* We need a transaction translator */
3884 switch (edesc->bmAttributes & UE_XFERTYPE) {
3886 ep->methods = &ehci_device_ctrl_methods;
3889 ep->methods = &ehci_device_intr_methods;
3891 case UE_ISOCHRONOUS:
3892 if (udev->speed == USB_SPEED_HIGH) {
3893 ep->methods = &ehci_device_isoc_hs_methods;
3894 } else if (udev->speed == USB_SPEED_FULL) {
3895 ep->methods = &ehci_device_isoc_fs_methods;
3899 ep->methods = &ehci_device_bulk_methods;
3911 ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3914 * Wait until the hardware has finished any possible use of
3915 * the transfer descriptor(s) and QH
3917 *pus = (188); /* microseconds */
3921 ehci_device_resume(struct usb_device *udev)
3923 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3924 struct usb_xfer *xfer;
3925 const struct usb_pipe_methods *methods;
3929 USB_BUS_LOCK(udev->bus);
3931 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3933 if (xfer->xroot->udev == udev) {
3935 methods = xfer->endpoint->methods;
3936 KKASSERT(xfer->flags_int.onhwqueue == 0);
3937 if ((methods == &ehci_device_bulk_methods) ||
3938 (methods == &ehci_device_ctrl_methods)) {
3939 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3940 sc->sc_async_p_last);
3942 if (methods == &ehci_device_intr_methods) {
3943 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3944 sc->sc_intr_p_last[xfer->qh_pos]);
3946 xfer->flags_int.onhwqueue = 1;
3949 ehci_ring_doorbell(sc, NULL);
3951 USB_BUS_UNLOCK(udev->bus);
3957 ehci_device_suspend(struct usb_device *udev)
3959 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3960 struct usb_xfer *xfer;
3961 const struct usb_pipe_methods *methods;
3965 USB_BUS_LOCK(udev->bus);
3967 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3969 if (xfer->xroot->udev == udev) {
3970 methods = xfer->endpoint->methods;
3972 if ((methods == &ehci_device_bulk_methods) ||
3973 (methods == &ehci_device_ctrl_methods)) {
3974 EHCI_REMOVE_QH(sc, xfer->qh_start[xfer->flags_int.curr_dma_set],
3975 sc->sc_async_p_last);
3977 if (methods == &ehci_device_intr_methods) {
3978 EHCI_REMOVE_QH(sc, xfer->qh_start[xfer->flags_int.curr_dma_set],
3979 sc->sc_intr_p_last[xfer->qh_pos]);
3981 xfer->flags_int.onhwqueue = 0;
3984 ehci_ring_doorbell(sc, NULL);
3986 USB_BUS_UNLOCK(udev->bus);
3990 ehci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3992 struct ehci_softc *sc = EHCI_BUS2SC(bus);
3995 case USB_HW_POWER_SUSPEND:
3996 case USB_HW_POWER_SHUTDOWN:
3999 case USB_HW_POWER_RESUME:
4008 ehci_set_hw_power(struct usb_bus *bus)
4010 ehci_softc_t *sc = EHCI_BUS2SC(bus);
4018 flags = bus->hw_power_state;
4020 temp = EOREAD4(sc, EHCI_USBCMD);
4022 temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
4024 if (flags & (USB_HW_POWER_CONTROL |
4025 USB_HW_POWER_BULK)) {
4026 DPRINTF("Async is active\n");
4027 temp |= EHCI_CMD_ASE;
4029 if (flags & (USB_HW_POWER_INTERRUPT |
4030 USB_HW_POWER_ISOC)) {
4031 DPRINTF("Periodic is active\n");
4032 temp |= EHCI_CMD_PSE;
4034 EOWRITE4(sc, EHCI_USBCMD, temp);
4036 USB_BUS_UNLOCK(bus);
4041 static const struct usb_bus_methods ehci_bus_methods =
4043 .endpoint_init = ehci_ep_init,
4044 .xfer_setup = ehci_xfer_setup,
4045 .xfer_unsetup = ehci_xfer_unsetup,
4046 .get_dma_delay = ehci_get_dma_delay,
4047 .device_resume = ehci_device_resume,
4048 .device_suspend = ehci_device_suspend,
4049 .set_hw_power = ehci_set_hw_power,
4050 .set_hw_power_sleep = ehci_set_hw_power_sleep,
4051 .roothub_exec = ehci_roothub_exec,
4052 .xfer_poll = ehci_do_poll,