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/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/stdint.h>
50 #include <sys/stddef.h>
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/linker_set.h>
58 #include <sys/module.h>
60 #include <sys/mutex.h>
61 #include <sys/condvar.h>
62 #include <sys/sysctl.h>
64 #include <sys/unistd.h>
65 #include <sys/callout.h>
66 #include <sys/malloc.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
72 #define USB_DEBUG_VAR ehcidebug
74 #include <dev/usb/usb_core.h>
75 #include <dev/usb/usb_debug.h>
76 #include <dev/usb/usb_busdma.h>
77 #include <dev/usb/usb_process.h>
78 #include <dev/usb/usb_transfer.h>
79 #include <dev/usb/usb_device.h>
80 #include <dev/usb/usb_hub.h>
81 #include <dev/usb/usb_util.h>
83 #include <dev/usb/usb_controller.h>
84 #include <dev/usb/usb_bus.h>
85 #include <dev/usb/controller/ehci.h>
86 #include <dev/usb/controller/ehcireg.h>
88 #define EHCI_BUS2SC(bus) \
89 ((ehci_softc_t *)(((uint8_t *)(bus)) - \
90 ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
93 static int ehcidebug = 0;
94 static int ehcinohighspeed = 0;
96 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
97 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
98 &ehcidebug, 0, "Debug level");
99 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
100 &ehcinohighspeed, 0, "Disable High Speed USB");
102 static void ehci_dump_regs(ehci_softc_t *sc);
103 static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
107 #define EHCI_INTR_ENDPT 1
109 extern struct usb_bus_methods ehci_bus_methods;
110 extern struct usb_pipe_methods ehci_device_bulk_methods;
111 extern struct usb_pipe_methods ehci_device_ctrl_methods;
112 extern struct usb_pipe_methods ehci_device_intr_methods;
113 extern struct usb_pipe_methods ehci_device_isoc_fs_methods;
114 extern struct usb_pipe_methods ehci_device_isoc_hs_methods;
116 static void ehci_do_poll(struct usb_bus *bus);
117 static void ehci_device_done(struct usb_xfer *xfer, usb_error_t error);
118 static uint8_t ehci_check_transfer(struct usb_xfer *xfer);
119 static void ehci_timeout(void *arg);
120 static void ehci_root_intr(ehci_softc_t *sc);
122 struct ehci_std_temp {
124 struct usb_page_cache *pc;
130 uint16_t max_frame_size;
132 uint8_t auto_data_toggle;
133 uint8_t setup_alt_next;
135 uint8_t can_use_next;
139 ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
141 ehci_softc_t *sc = EHCI_BUS2SC(bus);
144 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
145 sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
147 cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
148 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
150 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
151 cb(bus, sc->sc_hw.intr_start_pc + i,
152 sc->sc_hw.intr_start_pg + i,
153 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
156 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
157 cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
158 sc->sc_hw.isoc_hs_start_pg + i,
159 sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
162 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
163 cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
164 sc->sc_hw.isoc_fs_start_pg + i,
165 sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
170 ehci_reset(ehci_softc_t *sc)
175 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
176 for (i = 0; i < 100; i++) {
177 usb_pause_mtx(NULL, hz / 1000);
178 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
180 if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
182 * Force USBMODE as requested. Controllers
183 * may have multiple operating modes.
185 uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
186 if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
187 usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
188 device_printf(sc->sc_bus.bdev,
189 "set host controller mode\n");
191 if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
192 usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
193 device_printf(sc->sc_bus.bdev,
194 "set big-endian mode\n");
196 EOWRITE4(sc, EHCI_USBMODE, usbmode);
201 device_printf(sc->sc_bus.bdev, "reset timeout\n");
202 return (USB_ERR_IOERROR);
206 ehci_hcreset(ehci_softc_t *sc)
211 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
212 for (i = 0; i < 100; i++) {
213 usb_pause_mtx(NULL, hz / 1000);
214 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
220 * Fall through and try reset anyway even though
221 * Table 2-9 in the EHCI spec says this will result
222 * in undefined behavior.
224 device_printf(sc->sc_bus.bdev, "stop timeout\n");
226 return ehci_reset(sc);
230 ehci_init(ehci_softc_t *sc)
232 struct usb_page_search buf_res;
245 usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
253 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
255 version = EREAD2(sc, EHCI_HCIVERSION);
256 device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
257 version >> 8, version & 0xff);
259 sparams = EREAD4(sc, EHCI_HCSPARAMS);
260 DPRINTF("sparams=0x%x\n", sparams);
262 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
263 cparams = EREAD4(sc, EHCI_HCCPARAMS);
264 DPRINTF("cparams=0x%x\n", cparams);
266 if (EHCI_HCC_64BIT(cparams)) {
267 DPRINTF("HCC uses 64-bit structures\n");
269 /* MUST clear segment register if 64 bit capable */
270 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
272 sc->sc_bus.usbrev = USB_REV_2_0;
274 /* Reset the controller */
275 DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
277 err = ehci_hcreset(sc);
279 device_printf(sc->sc_bus.bdev, "reset timeout\n");
283 * use current frame-list-size selection 0: 1024*4 bytes 1: 512*4
284 * bytes 2: 256*4 bytes 3: unknown
286 if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
287 device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
288 return (USB_ERR_IOERROR);
290 /* set up the bus struct */
291 sc->sc_bus.methods = &ehci_bus_methods;
293 sc->sc_eintrs = EHCI_NORMAL_INTRS;
295 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
298 usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
302 /* initialize page cache pointer */
304 qh->page_cache = sc->sc_hw.intr_start_pc + i;
306 /* store a pointer to queue head */
308 sc->sc_intr_p_last[i] = qh;
311 htohc32(sc, buf_res.physaddr) |
312 htohc32(sc, EHCI_LINK_QH);
315 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
317 htohc32(sc, EHCI_QH_SET_MULT(1));
320 qh->qh_qtd.qtd_next =
321 htohc32(sc, EHCI_LINK_TERMINATE);
322 qh->qh_qtd.qtd_altnext =
323 htohc32(sc, EHCI_LINK_TERMINATE);
324 qh->qh_qtd.qtd_status =
325 htohc32(sc, EHCI_QTD_HALTED);
329 * the QHs are arranged to give poll intervals that are
330 * powers of 2 times 1ms
332 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
339 y = (x ^ bit) | (bit / 2);
341 qh_x = sc->sc_intr_p_last[x];
342 qh_y = sc->sc_intr_p_last[y];
345 * the next QH has half the poll interval
347 qh_x->qh_link = qh_y->qh_self;
357 qh = sc->sc_intr_p_last[0];
359 /* the last (1ms) QH terminates */
360 qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
362 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
366 usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
368 sitd = buf_res.buffer;
370 /* initialize page cache pointer */
372 sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
374 /* store a pointer to the transfer descriptor */
376 sc->sc_isoc_fs_p_last[i] = sitd;
378 /* initialize full speed isochronous */
381 htohc32(sc, buf_res.physaddr) |
382 htohc32(sc, EHCI_LINK_SITD);
385 htohc32(sc, EHCI_LINK_TERMINATE);
388 sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
391 usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
393 itd = buf_res.buffer;
395 /* initialize page cache pointer */
397 itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
399 /* store a pointer to the transfer descriptor */
401 sc->sc_isoc_hs_p_last[i] = itd;
403 /* initialize high speed isochronous */
406 htohc32(sc, buf_res.physaddr) |
407 htohc32(sc, EHCI_LINK_ITD);
413 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
418 pframes = buf_res.buffer;
422 * pframes -> high speed isochronous ->
423 * full speed isochronous -> interrupt QH's
425 for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
426 pframes[i] = sc->sc_isoc_hs_p_last
427 [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
430 /* setup sync list pointer */
431 EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
433 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
441 /* initialize page cache pointer */
443 qh->page_cache = &sc->sc_hw.async_start_pc;
445 /* store a pointer to the queue head */
447 sc->sc_async_p_last = qh;
449 /* init dummy QH that starts the async list */
452 htohc32(sc, buf_res.physaddr) |
453 htohc32(sc, EHCI_LINK_QH);
457 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
458 qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
459 qh->qh_link = qh->qh_self;
462 /* fill the overlay qTD */
463 qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
464 qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
465 qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
467 /* flush all cache into memory */
469 usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
473 ehci_dump_sqh(sc, sc->sc_async_p_last);
477 /* setup async list pointer */
478 EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
481 /* enable interrupts */
482 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
484 /* turn on controller */
485 EOWRITE4(sc, EHCI_USBCMD,
486 EHCI_CMD_ITC_1 | /* 1 microframes interrupt delay */
487 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
492 /* Take over port ownership */
493 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
495 for (i = 0; i < 100; i++) {
496 usb_pause_mtx(NULL, hz / 1000);
497 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
503 device_printf(sc->sc_bus.bdev, "run timeout\n");
504 return (USB_ERR_IOERROR);
508 /* catch any lost interrupts */
509 ehci_do_poll(&sc->sc_bus);
515 * shut down the controller when the system is going down
518 ehci_detach(ehci_softc_t *sc)
520 USB_BUS_LOCK(&sc->sc_bus);
522 usb_callout_stop(&sc->sc_tmo_pcd);
524 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
525 USB_BUS_UNLOCK(&sc->sc_bus);
527 if (ehci_hcreset(sc)) {
528 DPRINTF("reset failed!\n");
531 /* XXX let stray task complete */
532 usb_pause_mtx(NULL, hz / 20);
534 usb_callout_drain(&sc->sc_tmo_pcd);
538 ehci_suspend(ehci_softc_t *sc)
544 USB_BUS_LOCK(&sc->sc_bus);
546 for (i = 1; i <= sc->sc_noport; i++) {
547 cmd = EOREAD4(sc, EHCI_PORTSC(i));
548 if (((cmd & EHCI_PS_PO) == 0) &&
549 ((cmd & EHCI_PS_PE) == EHCI_PS_PE)) {
550 EOWRITE4(sc, EHCI_PORTSC(i),
555 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
557 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
558 EOWRITE4(sc, EHCI_USBCMD, cmd);
560 for (i = 0; i < 100; i++) {
561 hcr = EOREAD4(sc, EHCI_USBSTS) &
562 (EHCI_STS_ASS | EHCI_STS_PSS);
567 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
571 device_printf(sc->sc_bus.bdev, "reset timeout\n");
574 EOWRITE4(sc, EHCI_USBCMD, cmd);
576 for (i = 0; i < 100; i++) {
577 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
578 if (hcr == EHCI_STS_HCH) {
581 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
584 if (hcr != EHCI_STS_HCH) {
585 device_printf(sc->sc_bus.bdev,
588 USB_BUS_UNLOCK(&sc->sc_bus);
592 ehci_resume(ehci_softc_t *sc)
594 struct usb_page_search buf_res;
599 USB_BUS_LOCK(&sc->sc_bus);
601 /* restore things in case the bios doesn't */
602 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
604 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
605 EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
607 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
608 EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
610 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
613 for (i = 1; i <= sc->sc_noport; i++) {
614 cmd = EOREAD4(sc, EHCI_PORTSC(i));
615 if (((cmd & EHCI_PS_PO) == 0) &&
616 ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
617 EOWRITE4(sc, EHCI_PORTSC(i),
624 usb_pause_mtx(&sc->sc_bus.bus_mtx,
625 USB_MS_TO_TICKS(USB_RESUME_WAIT));
627 for (i = 1; i <= sc->sc_noport; i++) {
628 cmd = EOREAD4(sc, EHCI_PORTSC(i));
629 if (((cmd & EHCI_PS_PO) == 0) &&
630 ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
631 EOWRITE4(sc, EHCI_PORTSC(i),
636 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
638 for (i = 0; i < 100; i++) {
639 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
640 if (hcr != EHCI_STS_HCH) {
643 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
645 if (hcr == EHCI_STS_HCH) {
646 device_printf(sc->sc_bus.bdev, "config timeout\n");
649 USB_BUS_UNLOCK(&sc->sc_bus);
652 USB_MS_TO_TICKS(USB_RESUME_WAIT));
654 /* catch any lost interrupts */
655 ehci_do_poll(&sc->sc_bus);
659 ehci_shutdown(ehci_softc_t *sc)
661 DPRINTF("stopping the HC\n");
663 if (ehci_hcreset(sc)) {
664 DPRINTF("reset failed!\n");
670 ehci_dump_regs(ehci_softc_t *sc)
674 i = EOREAD4(sc, EHCI_USBCMD);
675 printf("cmd=0x%08x\n", i);
677 if (i & EHCI_CMD_ITC_1)
678 printf(" EHCI_CMD_ITC_1\n");
679 if (i & EHCI_CMD_ITC_2)
680 printf(" EHCI_CMD_ITC_2\n");
681 if (i & EHCI_CMD_ITC_4)
682 printf(" EHCI_CMD_ITC_4\n");
683 if (i & EHCI_CMD_ITC_8)
684 printf(" EHCI_CMD_ITC_8\n");
685 if (i & EHCI_CMD_ITC_16)
686 printf(" EHCI_CMD_ITC_16\n");
687 if (i & EHCI_CMD_ITC_32)
688 printf(" EHCI_CMD_ITC_32\n");
689 if (i & EHCI_CMD_ITC_64)
690 printf(" EHCI_CMD_ITC_64\n");
691 if (i & EHCI_CMD_ASPME)
692 printf(" EHCI_CMD_ASPME\n");
693 if (i & EHCI_CMD_ASPMC)
694 printf(" EHCI_CMD_ASPMC\n");
695 if (i & EHCI_CMD_LHCR)
696 printf(" EHCI_CMD_LHCR\n");
697 if (i & EHCI_CMD_IAAD)
698 printf(" EHCI_CMD_IAAD\n");
699 if (i & EHCI_CMD_ASE)
700 printf(" EHCI_CMD_ASE\n");
701 if (i & EHCI_CMD_PSE)
702 printf(" EHCI_CMD_PSE\n");
703 if (i & EHCI_CMD_FLS_M)
704 printf(" EHCI_CMD_FLS_M\n");
705 if (i & EHCI_CMD_HCRESET)
706 printf(" EHCI_CMD_HCRESET\n");
708 printf(" EHCI_CMD_RS\n");
710 i = EOREAD4(sc, EHCI_USBSTS);
712 printf("sts=0x%08x\n", i);
714 if (i & EHCI_STS_ASS)
715 printf(" EHCI_STS_ASS\n");
716 if (i & EHCI_STS_PSS)
717 printf(" EHCI_STS_PSS\n");
718 if (i & EHCI_STS_REC)
719 printf(" EHCI_STS_REC\n");
720 if (i & EHCI_STS_HCH)
721 printf(" EHCI_STS_HCH\n");
722 if (i & EHCI_STS_IAA)
723 printf(" EHCI_STS_IAA\n");
724 if (i & EHCI_STS_HSE)
725 printf(" EHCI_STS_HSE\n");
726 if (i & EHCI_STS_FLR)
727 printf(" EHCI_STS_FLR\n");
728 if (i & EHCI_STS_PCD)
729 printf(" EHCI_STS_PCD\n");
730 if (i & EHCI_STS_ERRINT)
731 printf(" EHCI_STS_ERRINT\n");
732 if (i & EHCI_STS_INT)
733 printf(" EHCI_STS_INT\n");
735 printf("ien=0x%08x\n",
736 EOREAD4(sc, EHCI_USBINTR));
737 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
738 EOREAD4(sc, EHCI_FRINDEX),
739 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
740 EOREAD4(sc, EHCI_PERIODICLISTBASE),
741 EOREAD4(sc, EHCI_ASYNCLISTADDR));
742 for (i = 1; i <= sc->sc_noport; i++) {
743 printf("port %d status=0x%08x\n", i,
744 EOREAD4(sc, EHCI_PORTSC(i)));
749 ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
751 link = hc32toh(sc, link);
752 printf("0x%08x", link);
753 if (link & EHCI_LINK_TERMINATE)
758 switch (EHCI_LINK_TYPE(link)) {
778 ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
783 ehci_dump_link(sc, qtd->qtd_next, 0);
785 ehci_dump_link(sc, qtd->qtd_altnext, 0);
787 s = hc32toh(sc, qtd->qtd_status);
788 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
789 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
790 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
791 printf(" cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
792 EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
793 (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
794 (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
795 (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
796 (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
797 (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
798 (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
799 (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
800 (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
802 for (s = 0; s < 5; s++) {
803 printf(" buffer[%d]=0x%08x\n", s,
804 hc32toh(sc, qtd->qtd_buffer[s]));
806 for (s = 0; s < 5; s++) {
807 printf(" buffer_hi[%d]=0x%08x\n", s,
808 hc32toh(sc, qtd->qtd_buffer_hi[s]));
813 ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
817 usb_pc_cpu_invalidate(sqtd->page_cache);
818 printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
819 ehci_dump_qtd(sc, sqtd);
820 temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
825 ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
831 for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
832 stop = ehci_dump_sqtd(sc, sqtd);
835 printf("dump aborted, too many TDs\n");
840 ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
845 usb_pc_cpu_invalidate(qh->page_cache);
846 printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
848 ehci_dump_link(sc, qh->qh_link, 1);
850 endp = hc32toh(sc, qh->qh_endp);
851 printf(" endp=0x%08x\n", endp);
852 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
853 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
854 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
855 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
856 printf(" mpl=0x%x ctl=%d nrl=%d\n",
857 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
858 EHCI_QH_GET_NRL(endp));
859 endphub = hc32toh(sc, qh->qh_endphub);
860 printf(" endphub=0x%08x\n", endphub);
861 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
862 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
863 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
864 EHCI_QH_GET_MULT(endphub));
866 ehci_dump_link(sc, qh->qh_curqtd, 0);
868 printf("Overlay qTD:\n");
869 ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
873 ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
875 usb_pc_cpu_invalidate(sitd->page_cache);
876 printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
877 printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
878 printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
879 hc32toh(sc, sitd->sitd_portaddr),
880 (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
882 EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
883 EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
884 EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
885 EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
886 printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
887 printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
888 (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
889 EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
890 printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
891 hc32toh(sc, sitd->sitd_back),
892 hc32toh(sc, sitd->sitd_bp[0]),
893 hc32toh(sc, sitd->sitd_bp[1]),
894 hc32toh(sc, sitd->sitd_bp_hi[0]),
895 hc32toh(sc, sitd->sitd_bp_hi[1]));
899 ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
901 usb_pc_cpu_invalidate(itd->page_cache);
902 printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
903 printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
904 printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
905 (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
906 printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
907 (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
908 printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
909 (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
910 printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
911 (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
912 printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
913 (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
914 printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
915 (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
916 printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
917 (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
918 printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
919 (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
920 printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
921 printf(" addr=0x%02x; endpt=0x%01x\n",
922 EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
923 EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
924 printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
925 printf(" dir=%s; mpl=0x%02x\n",
926 (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
927 EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
928 printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
929 hc32toh(sc, itd->itd_bp[2]),
930 hc32toh(sc, itd->itd_bp[3]),
931 hc32toh(sc, itd->itd_bp[4]),
932 hc32toh(sc, itd->itd_bp[5]),
933 hc32toh(sc, itd->itd_bp[6]));
934 printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
935 " 0x%08x,0x%08x,0x%08x\n",
936 hc32toh(sc, itd->itd_bp_hi[0]),
937 hc32toh(sc, itd->itd_bp_hi[1]),
938 hc32toh(sc, itd->itd_bp_hi[2]),
939 hc32toh(sc, itd->itd_bp_hi[3]),
940 hc32toh(sc, itd->itd_bp_hi[4]),
941 hc32toh(sc, itd->itd_bp_hi[5]),
942 hc32toh(sc, itd->itd_bp_hi[6]));
946 ehci_dump_isoc(ehci_softc_t *sc)
953 pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
954 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
956 printf("%s: isochronous dump from frame 0x%03x:\n",
959 itd = sc->sc_isoc_hs_p_last[pos];
960 sitd = sc->sc_isoc_fs_p_last[pos];
962 while (itd && max && max--) {
963 ehci_dump_itd(sc, itd);
967 while (sitd && max && max--) {
968 ehci_dump_sitd(sc, sitd);
976 ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
978 /* check for early completion */
979 if (ehci_check_transfer(xfer)) {
982 /* put transfer on interrupt queue */
983 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
985 /* start timeout, if any */
986 if (xfer->timeout != 0) {
987 usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
991 #define EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
993 _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
995 DPRINTFN(11, "%p to %p\n", std, last);
997 /* (sc->sc_bus.mtx) must be locked */
999 std->next = last->next;
1000 std->sitd_next = last->sitd_next;
1004 usb_pc_cpu_flush(std->page_cache);
1007 * the last->next->prev is never followed: std->next->prev = std;
1010 last->sitd_next = std->sitd_self;
1012 usb_pc_cpu_flush(last->page_cache);
1017 #define EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
1019 _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1021 DPRINTFN(11, "%p to %p\n", std, last);
1023 /* (sc->sc_bus.mtx) must be locked */
1025 std->next = last->next;
1026 std->itd_next = last->itd_next;
1030 usb_pc_cpu_flush(std->page_cache);
1033 * the last->next->prev is never followed: std->next->prev = std;
1036 last->itd_next = std->itd_self;
1038 usb_pc_cpu_flush(last->page_cache);
1043 #define EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
1045 _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1047 DPRINTFN(11, "%p to %p\n", sqh, last);
1049 if (sqh->prev != NULL) {
1050 /* should not happen */
1051 DPRINTFN(0, "QH already linked!\n");
1054 /* (sc->sc_bus.mtx) must be locked */
1056 sqh->next = last->next;
1057 sqh->qh_link = last->qh_link;
1061 usb_pc_cpu_flush(sqh->page_cache);
1064 * the last->next->prev is never followed: sqh->next->prev = sqh;
1068 last->qh_link = sqh->qh_self;
1070 usb_pc_cpu_flush(last->page_cache);
1075 #define EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
1076 static ehci_sitd_t *
1077 _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
1079 DPRINTFN(11, "%p from %p\n", std, last);
1081 /* (sc->sc_bus.mtx) must be locked */
1083 std->prev->next = std->next;
1084 std->prev->sitd_next = std->sitd_next;
1086 usb_pc_cpu_flush(std->prev->page_cache);
1089 std->next->prev = std->prev;
1090 usb_pc_cpu_flush(std->next->page_cache);
1092 return ((last == std) ? std->prev : last);
1095 #define EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
1097 _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1099 DPRINTFN(11, "%p from %p\n", std, last);
1101 /* (sc->sc_bus.mtx) must be locked */
1103 std->prev->next = std->next;
1104 std->prev->itd_next = std->itd_next;
1106 usb_pc_cpu_flush(std->prev->page_cache);
1109 std->next->prev = std->prev;
1110 usb_pc_cpu_flush(std->next->page_cache);
1112 return ((last == std) ? std->prev : last);
1115 #define EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
1117 _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1119 DPRINTFN(11, "%p from %p\n", sqh, last);
1121 /* (sc->sc_bus.mtx) must be locked */
1123 /* only remove if not removed from a queue */
1126 sqh->prev->next = sqh->next;
1127 sqh->prev->qh_link = sqh->qh_link;
1129 usb_pc_cpu_flush(sqh->prev->page_cache);
1132 sqh->next->prev = sqh->prev;
1133 usb_pc_cpu_flush(sqh->next->page_cache);
1135 last = ((last == sqh) ? sqh->prev : last);
1139 usb_pc_cpu_flush(sqh->page_cache);
1145 ehci_non_isoc_done_sub(struct usb_xfer *xfer)
1147 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1149 ehci_qtd_t *td_alt_next;
1153 td = xfer->td_transfer_cache;
1154 td_alt_next = td->alt_next;
1156 if (xfer->aframes != xfer->nframes) {
1157 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1161 usb_pc_cpu_invalidate(td->page_cache);
1162 status = hc32toh(sc, td->qtd_status);
1164 len = EHCI_QTD_GET_BYTES(status);
1167 * Verify the status length and
1168 * add the length to "frlengths[]":
1170 if (len > td->len) {
1171 /* should not happen */
1172 DPRINTF("Invalid status length, "
1173 "0x%04x/0x%04x bytes\n", len, td->len);
1174 status |= EHCI_QTD_HALTED;
1175 } else if (xfer->aframes != xfer->nframes) {
1176 xfer->frlengths[xfer->aframes] += td->len - len;
1178 /* Check for last transfer */
1179 if (((void *)td) == xfer->td_transfer_last) {
1183 /* Check for transfer error */
1184 if (status & EHCI_QTD_HALTED) {
1185 /* the transfer is finished */
1189 /* Check for short transfer */
1191 if (xfer->flags_int.short_frames_ok) {
1192 /* follow alt next */
1195 /* the transfer is finished */
1202 if (td->alt_next != td_alt_next) {
1203 /* this USB frame is complete */
1208 /* update transfer cache */
1210 xfer->td_transfer_cache = td;
1213 if (status & EHCI_QTD_STATERRS) {
1214 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
1215 "status=%s%s%s%s%s%s%s%s\n",
1216 xfer->address, xfer->endpointno, xfer->aframes,
1217 (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1218 (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
1219 (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
1220 (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
1221 (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
1222 (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
1223 (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
1224 (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
1228 return ((status & EHCI_QTD_HALTED) ?
1229 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1233 ehci_non_isoc_done(struct usb_xfer *xfer)
1235 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1238 usb_error_t err = 0;
1240 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1241 xfer, xfer->endpoint);
1244 if (ehcidebug > 10) {
1245 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1247 ehci_dump_sqtds(sc, xfer->td_transfer_first);
1251 /* extract data toggle directly from the QH's overlay area */
1253 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1255 usb_pc_cpu_invalidate(qh->page_cache);
1257 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1259 xfer->endpoint->toggle_next =
1260 (status & EHCI_QTD_TOGGLE_MASK) ? 1 : 0;
1264 xfer->td_transfer_cache = xfer->td_transfer_first;
1266 if (xfer->flags_int.control_xfr) {
1268 if (xfer->flags_int.control_hdr) {
1270 err = ehci_non_isoc_done_sub(xfer);
1274 if (xfer->td_transfer_cache == NULL) {
1278 while (xfer->aframes != xfer->nframes) {
1280 err = ehci_non_isoc_done_sub(xfer);
1283 if (xfer->td_transfer_cache == NULL) {
1288 if (xfer->flags_int.control_xfr &&
1289 !xfer->flags_int.control_act) {
1291 err = ehci_non_isoc_done_sub(xfer);
1294 ehci_device_done(xfer, err);
1297 /*------------------------------------------------------------------------*
1298 * ehci_check_transfer
1301 * 0: USB transfer is not finished
1302 * Else: USB transfer is finished
1303 *------------------------------------------------------------------------*/
1305 ehci_check_transfer(struct usb_xfer *xfer)
1307 struct usb_pipe_methods *methods = xfer->endpoint->methods;
1308 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1312 DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
1314 if (methods == &ehci_device_isoc_fs_methods) {
1317 /* isochronous full speed transfer */
1319 td = xfer->td_transfer_last;
1320 usb_pc_cpu_invalidate(td->page_cache);
1321 status = hc32toh(sc, td->sitd_status);
1323 /* also check if first is complete */
1325 td = xfer->td_transfer_first;
1326 usb_pc_cpu_invalidate(td->page_cache);
1327 status |= hc32toh(sc, td->sitd_status);
1329 if (!(status & EHCI_SITD_ACTIVE)) {
1330 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1333 } else if (methods == &ehci_device_isoc_hs_methods) {
1336 /* isochronous high speed transfer */
1338 td = xfer->td_transfer_last;
1339 usb_pc_cpu_invalidate(td->page_cache);
1341 td->itd_status[0] | td->itd_status[1] |
1342 td->itd_status[2] | td->itd_status[3] |
1343 td->itd_status[4] | td->itd_status[5] |
1344 td->itd_status[6] | td->itd_status[7];
1346 /* also check first transfer */
1347 td = xfer->td_transfer_first;
1348 usb_pc_cpu_invalidate(td->page_cache);
1350 td->itd_status[0] | td->itd_status[1] |
1351 td->itd_status[2] | td->itd_status[3] |
1352 td->itd_status[4] | td->itd_status[5] |
1353 td->itd_status[6] | td->itd_status[7];
1355 /* if no transactions are active we continue */
1356 if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
1357 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1364 /* non-isochronous transfer */
1367 * check whether there is an error somewhere in the middle,
1368 * or whether there was a short packet (SPD and not ACTIVE)
1370 td = xfer->td_transfer_cache;
1372 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1374 usb_pc_cpu_invalidate(qh->page_cache);
1376 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1377 if (status & EHCI_QTD_ACTIVE) {
1378 /* transfer is pending */
1383 usb_pc_cpu_invalidate(td->page_cache);
1384 status = hc32toh(sc, td->qtd_status);
1387 * Check if there is an active TD which
1388 * indicates that the transfer isn't done.
1390 if (status & EHCI_QTD_ACTIVE) {
1392 if (xfer->td_transfer_cache != td) {
1393 xfer->td_transfer_cache = td;
1394 if (qh->qh_qtd.qtd_next &
1395 htohc32(sc, EHCI_LINK_TERMINATE)) {
1396 /* XXX - manually advance to next frame */
1397 qh->qh_qtd.qtd_next = td->qtd_self;
1398 usb_pc_cpu_flush(td->page_cache);
1404 * last transfer descriptor makes the transfer done
1406 if (((void *)td) == xfer->td_transfer_last) {
1410 * any kind of error makes the transfer done
1412 if (status & EHCI_QTD_HALTED) {
1416 * if there is no alternate next transfer, a short
1417 * packet also makes the transfer done
1419 if (EHCI_QTD_GET_BYTES(status)) {
1420 if (xfer->flags_int.short_frames_ok) {
1421 /* follow alt next */
1427 /* transfer is done */
1432 ehci_non_isoc_done(xfer);
1437 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1445 ehci_pcd_enable(ehci_softc_t *sc)
1447 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1449 sc->sc_eintrs |= EHCI_STS_PCD;
1450 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1452 /* acknowledge any PCD interrupt */
1453 EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
1459 ehci_interrupt_poll(ehci_softc_t *sc)
1461 struct usb_xfer *xfer;
1464 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1466 * check if transfer is transferred
1468 if (ehci_check_transfer(xfer)) {
1469 /* queue has been modified */
1475 /*------------------------------------------------------------------------*
1476 * ehci_interrupt - EHCI interrupt handler
1478 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1479 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1481 *------------------------------------------------------------------------*/
1483 ehci_interrupt(ehci_softc_t *sc)
1487 USB_BUS_LOCK(&sc->sc_bus);
1489 DPRINTFN(16, "real interrupt\n");
1492 if (ehcidebug > 15) {
1497 status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1499 /* the interrupt was not for us */
1502 if (!(status & sc->sc_eintrs)) {
1505 EOWRITE4(sc, EHCI_USBSTS, status); /* acknowledge */
1507 status &= sc->sc_eintrs;
1509 if (status & EHCI_STS_HSE) {
1510 printf("%s: unrecoverable error, "
1511 "controller halted\n", __FUNCTION__);
1517 if (status & EHCI_STS_PCD) {
1519 * Disable PCD interrupt for now, because it will be
1520 * on until the port has been reset.
1522 sc->sc_eintrs &= ~EHCI_STS_PCD;
1523 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1527 /* do not allow RHSC interrupts > 1 per second */
1528 usb_callout_reset(&sc->sc_tmo_pcd, hz,
1529 (void *)&ehci_pcd_enable, sc);
1531 status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
1534 /* block unprocessed interrupts */
1535 sc->sc_eintrs &= ~status;
1536 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1537 printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
1539 /* poll all the USB transfers */
1540 ehci_interrupt_poll(sc);
1543 USB_BUS_UNLOCK(&sc->sc_bus);
1547 * called when a request does not complete
1550 ehci_timeout(void *arg)
1552 struct usb_xfer *xfer = arg;
1554 DPRINTF("xfer=%p\n", xfer);
1556 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1558 /* transfer is transferred */
1559 ehci_device_done(xfer, USB_ERR_TIMEOUT);
1563 ehci_do_poll(struct usb_bus *bus)
1565 ehci_softc_t *sc = EHCI_BUS2SC(bus);
1567 USB_BUS_LOCK(&sc->sc_bus);
1568 ehci_interrupt_poll(sc);
1569 USB_BUS_UNLOCK(&sc->sc_bus);
1573 ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
1575 struct usb_page_search buf_res;
1577 ehci_qtd_t *td_next;
1578 ehci_qtd_t *td_alt_next;
1579 uint32_t buf_offset;
1583 uint8_t shortpkt_old;
1586 terminate = htohc32(temp->sc, EHCI_LINK_TERMINATE);
1589 shortpkt_old = temp->shortpkt;
1590 len_old = temp->len;
1596 td_next = temp->td_next;
1600 if (temp->len == 0) {
1602 if (temp->shortpkt) {
1605 /* send a Zero Length Packet, ZLP, last */
1612 average = temp->average;
1614 if (temp->len < average) {
1615 if (temp->len % temp->max_frame_size) {
1618 average = temp->len;
1622 if (td_next == NULL) {
1623 panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
1628 td_next = td->obj_next;
1630 /* check if we are pre-computing */
1634 /* update remaining length */
1636 temp->len -= average;
1640 /* fill out current TD */
1644 htohc32(temp->sc, EHCI_QTD_IOC |
1645 EHCI_QTD_SET_BYTES(average));
1649 if (temp->auto_data_toggle == 0) {
1651 /* update data toggle, ZLP case */
1654 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1658 td->qtd_buffer[0] = 0;
1659 td->qtd_buffer_hi[0] = 0;
1661 td->qtd_buffer[1] = 0;
1662 td->qtd_buffer_hi[1] = 0;
1668 if (temp->auto_data_toggle == 0) {
1670 /* update data toggle */
1672 if (((average + temp->max_frame_size - 1) /
1673 temp->max_frame_size) & 1) {
1675 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1680 /* update remaining length */
1682 temp->len -= average;
1684 /* fill out buffer pointers */
1686 usbd_get_page(temp->pc, buf_offset, &buf_res);
1688 htohc32(temp->sc, buf_res.physaddr);
1689 td->qtd_buffer_hi[0] = 0;
1693 while (average > EHCI_PAGE_SIZE) {
1694 average -= EHCI_PAGE_SIZE;
1695 buf_offset += EHCI_PAGE_SIZE;
1696 usbd_get_page(temp->pc, buf_offset, &buf_res);
1699 buf_res.physaddr & (~0xFFF));
1700 td->qtd_buffer_hi[x] = 0;
1705 * NOTE: The "average" variable is never zero after
1706 * exiting the loop above !
1708 * NOTE: We have to subtract one from the offset to
1709 * ensure that we are computing the physical address
1712 buf_offset += average;
1713 usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
1716 buf_res.physaddr & (~0xFFF));
1717 td->qtd_buffer_hi[x] = 0;
1720 if (temp->can_use_next) {
1722 /* link the current TD with the next one */
1723 td->qtd_next = td_next->qtd_self;
1727 * BUG WARNING: The EHCI HW can use the
1728 * qtd_next field instead of qtd_altnext when
1729 * a short packet is received! We work this
1730 * around in software by not queueing more
1731 * than one job/TD at a time!
1733 td->qtd_next = terminate;
1736 td->qtd_altnext = terminate;
1737 td->alt_next = td_alt_next;
1739 usb_pc_cpu_flush(td->page_cache);
1745 /* setup alt next pointer, if any */
1746 if (temp->last_frame) {
1749 /* we use this field internally */
1750 td_alt_next = td_next;
1754 temp->shortpkt = shortpkt_old;
1755 temp->len = len_old;
1759 temp->td_next = td_next;
1763 ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
1765 struct ehci_std_temp temp;
1766 struct usb_pipe_methods *methods;
1770 uint32_t qh_endphub;
1773 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1774 xfer->address, UE_GET_ADDR(xfer->endpointno),
1775 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1777 temp.average = xfer->max_hc_frame_size;
1778 temp.max_frame_size = xfer->max_frame_size;
1779 temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
1781 /* toggle the DMA set we are using */
1782 xfer->flags_int.curr_dma_set ^= 1;
1784 /* get next DMA set */
1785 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1787 xfer->td_transfer_first = td;
1788 xfer->td_transfer_cache = td;
1792 temp.qtd_status = 0;
1793 temp.last_frame = 0;
1794 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1795 temp.can_use_next = (xfer->flags_int.control_xfr ||
1796 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT));
1798 if (xfer->flags_int.control_xfr) {
1799 if (xfer->endpoint->toggle_next) {
1802 htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1804 temp.auto_data_toggle = 0;
1806 temp.auto_data_toggle = 1;
1809 if (usbd_get_speed(xfer->xroot->udev) != USB_SPEED_HIGH) {
1812 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1814 /* check if we should prepend a setup message */
1816 if (xfer->flags_int.control_xfr) {
1817 if (xfer->flags_int.control_hdr) {
1820 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1821 temp.qtd_status |= htohc32(temp.sc,
1823 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
1824 EHCI_QTD_SET_TOGGLE(0));
1826 temp.len = xfer->frlengths[0];
1827 temp.pc = xfer->frbuffers + 0;
1828 temp.shortpkt = temp.len ? 1 : 0;
1829 /* check for last frame */
1830 if (xfer->nframes == 1) {
1831 /* no STATUS stage yet, SETUP is last */
1832 if (xfer->flags_int.control_act) {
1833 temp.last_frame = 1;
1834 temp.setup_alt_next = 0;
1837 ehci_setup_standard_chain_sub(&temp);
1844 while (x != xfer->nframes) {
1846 /* DATA0 / DATA1 message */
1848 temp.len = xfer->frlengths[x];
1849 temp.pc = xfer->frbuffers + x;
1853 if (x == xfer->nframes) {
1854 if (xfer->flags_int.control_xfr) {
1855 /* no STATUS stage yet, DATA is last */
1856 if (xfer->flags_int.control_act) {
1857 temp.last_frame = 1;
1858 temp.setup_alt_next = 0;
1861 temp.last_frame = 1;
1862 temp.setup_alt_next = 0;
1865 /* keep previous data toggle and error count */
1868 htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1869 EHCI_QTD_SET_TOGGLE(1));
1871 if (temp.len == 0) {
1873 /* make sure that we send an USB packet */
1879 /* regular data transfer */
1881 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1884 /* set endpoint direction */
1887 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1888 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1889 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1890 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1891 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
1893 ehci_setup_standard_chain_sub(&temp);
1896 /* check if we should append a status stage */
1898 if (xfer->flags_int.control_xfr &&
1899 !xfer->flags_int.control_act) {
1902 * Send a DATA1 message and invert the current endpoint
1906 temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1907 EHCI_QTD_SET_TOGGLE(1));
1909 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1910 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1911 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
1912 EHCI_QTD_SET_TOGGLE(1)) :
1913 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1914 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
1915 EHCI_QTD_SET_TOGGLE(1));
1920 temp.last_frame = 1;
1921 temp.setup_alt_next = 0;
1923 ehci_setup_standard_chain_sub(&temp);
1927 /* the last TD terminates the transfer: */
1928 td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1929 td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1931 usb_pc_cpu_flush(td->page_cache);
1933 /* must have at least one frame! */
1935 xfer->td_transfer_last = td;
1938 if (ehcidebug > 8) {
1939 DPRINTF("nexttog=%d; data before transfer:\n",
1940 xfer->endpoint->toggle_next);
1941 ehci_dump_sqtds(temp.sc,
1942 xfer->td_transfer_first);
1946 methods = xfer->endpoint->methods;
1948 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1950 /* the "qh_link" field is filled when the QH is added */
1953 (EHCI_QH_SET_ADDR(xfer->address) |
1954 EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
1955 EHCI_QH_SET_MPL(xfer->max_packet_size));
1957 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
1958 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
1959 if (methods != &ehci_device_intr_methods)
1960 qh_endp |= EHCI_QH_SET_NRL(8);
1963 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
1964 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
1966 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
1969 if (methods == &ehci_device_ctrl_methods) {
1970 qh_endp |= EHCI_QH_CTL;
1972 if (methods != &ehci_device_intr_methods) {
1973 /* Only try one time per microframe! */
1974 qh_endp |= EHCI_QH_SET_NRL(1);
1978 if (temp.auto_data_toggle == 0) {
1979 /* software computes the data toggle */
1980 qh_endp |= EHCI_QH_DTC;
1983 qh->qh_endp = htohc32(temp.sc, qh_endp);
1986 (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
1987 EHCI_QH_SET_CMASK(xfer->usb_cmask) |
1988 EHCI_QH_SET_SMASK(xfer->usb_smask) |
1989 EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
1990 EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
1992 qh->qh_endphub = htohc32(temp.sc, qh_endphub);
1995 /* fill the overlay qTD */
1997 if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
1999 qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
2001 qh->qh_qtd.qtd_status = 0;
2004 td = xfer->td_transfer_first;
2006 qh->qh_qtd.qtd_next = td->qtd_self;
2007 qh->qh_qtd.qtd_altnext =
2008 htohc32(temp.sc, EHCI_LINK_TERMINATE);
2010 usb_pc_cpu_flush(qh->page_cache);
2012 if (xfer->xroot->udev->flags.self_suspended == 0) {
2013 EHCI_APPEND_QH(qh, *qh_last);
2018 ehci_root_intr(ehci_softc_t *sc)
2023 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2025 /* clear any old interrupt data */
2026 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2029 m = (sc->sc_noport + 1);
2030 if (m > (8 * sizeof(sc->sc_hub_idata))) {
2031 m = (8 * sizeof(sc->sc_hub_idata));
2033 for (i = 1; i < m; i++) {
2034 /* pick out CHANGE bits from the status register */
2035 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
2036 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2037 DPRINTF("port %d changed\n", i);
2040 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2041 sizeof(sc->sc_hub_idata));
2045 ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2047 uint32_t nframes = xfer->nframes;
2049 uint32_t *plen = xfer->frlengths;
2051 ehci_sitd_t *td = xfer->td_transfer_first;
2052 ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
2054 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2055 xfer, xfer->endpoint);
2059 panic("%s:%d: out of TD's\n",
2060 __FUNCTION__, __LINE__);
2062 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2063 pp_last = &sc->sc_isoc_fs_p_last[0];
2066 if (ehcidebug > 15) {
2067 DPRINTF("isoc FS-TD\n");
2068 ehci_dump_sitd(sc, td);
2071 usb_pc_cpu_invalidate(td->page_cache);
2072 status = hc32toh(sc, td->sitd_status);
2074 len = EHCI_SITD_GET_LEN(status);
2076 DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2086 /* remove FS-TD from schedule */
2087 EHCI_REMOVE_FS_TD(td, *pp_last);
2094 xfer->aframes = xfer->nframes;
2098 ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2100 uint32_t nframes = xfer->nframes;
2102 uint32_t *plen = xfer->frlengths;
2105 ehci_itd_t *td = xfer->td_transfer_first;
2106 ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
2108 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2109 xfer, xfer->endpoint);
2113 panic("%s:%d: out of TD's\n",
2114 __FUNCTION__, __LINE__);
2116 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2117 pp_last = &sc->sc_isoc_hs_p_last[0];
2120 if (ehcidebug > 15) {
2121 DPRINTF("isoc HS-TD\n");
2122 ehci_dump_itd(sc, td);
2126 usb_pc_cpu_invalidate(td->page_cache);
2127 status = hc32toh(sc, td->itd_status[td_no]);
2129 len = EHCI_ITD_GET_LEN(status);
2131 DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2135 * The length is valid. NOTE: The complete
2136 * length is written back into the status
2137 * field, and not the remainder like with
2138 * other transfer descriptor types.
2141 /* Invalid length - truncate */
2150 if ((td_no == 8) || (nframes == 0)) {
2151 /* remove HS-TD from schedule */
2152 EHCI_REMOVE_HS_TD(td, *pp_last);
2159 xfer->aframes = xfer->nframes;
2162 /* NOTE: "done" can be run two times in a row,
2163 * from close and from interrupt
2166 ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
2168 struct usb_pipe_methods *methods = xfer->endpoint->methods;
2169 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2171 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2173 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2174 xfer, xfer->endpoint, error);
2176 if ((methods == &ehci_device_bulk_methods) ||
2177 (methods == &ehci_device_ctrl_methods)) {
2179 if (ehcidebug > 8) {
2180 DPRINTF("nexttog=%d; data after transfer:\n",
2181 xfer->endpoint->toggle_next);
2183 xfer->td_transfer_first);
2187 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2188 sc->sc_async_p_last);
2190 if (methods == &ehci_device_intr_methods) {
2191 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2192 sc->sc_intr_p_last[xfer->qh_pos]);
2195 * Only finish isochronous transfers once which will update
2196 * "xfer->frlengths".
2198 if (xfer->td_transfer_first &&
2199 xfer->td_transfer_last) {
2200 if (methods == &ehci_device_isoc_fs_methods) {
2201 ehci_isoc_fs_done(sc, xfer);
2203 if (methods == &ehci_device_isoc_hs_methods) {
2204 ehci_isoc_hs_done(sc, xfer);
2206 xfer->td_transfer_first = NULL;
2207 xfer->td_transfer_last = NULL;
2209 /* dequeue transfer and start next transfer */
2210 usbd_transfer_done(xfer, error);
2213 /*------------------------------------------------------------------------*
2215 *------------------------------------------------------------------------*/
2217 ehci_device_bulk_open(struct usb_xfer *xfer)
2223 ehci_device_bulk_close(struct usb_xfer *xfer)
2225 ehci_device_done(xfer, USB_ERR_CANCELLED);
2229 ehci_device_bulk_enter(struct usb_xfer *xfer)
2235 ehci_device_bulk_start(struct usb_xfer *xfer)
2237 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2240 /* setup TD's and QH */
2241 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2243 /* put transfer on interrupt queue */
2244 ehci_transfer_intr_enqueue(xfer);
2246 /* XXX Performance quirk: Some Host Controllers have a too low
2247 * interrupt rate. Issue an IAAD to stimulate the Host
2248 * Controller after queueing the BULK transfer.
2250 temp = EOREAD4(sc, EHCI_USBCMD);
2251 if (!(temp & EHCI_CMD_IAAD))
2252 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
2255 struct usb_pipe_methods ehci_device_bulk_methods =
2257 .open = ehci_device_bulk_open,
2258 .close = ehci_device_bulk_close,
2259 .enter = ehci_device_bulk_enter,
2260 .start = ehci_device_bulk_start,
2263 /*------------------------------------------------------------------------*
2264 * ehci control support
2265 *------------------------------------------------------------------------*/
2267 ehci_device_ctrl_open(struct usb_xfer *xfer)
2273 ehci_device_ctrl_close(struct usb_xfer *xfer)
2275 ehci_device_done(xfer, USB_ERR_CANCELLED);
2279 ehci_device_ctrl_enter(struct usb_xfer *xfer)
2285 ehci_device_ctrl_start(struct usb_xfer *xfer)
2287 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2289 /* setup TD's and QH */
2290 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2292 /* put transfer on interrupt queue */
2293 ehci_transfer_intr_enqueue(xfer);
2296 struct usb_pipe_methods ehci_device_ctrl_methods =
2298 .open = ehci_device_ctrl_open,
2299 .close = ehci_device_ctrl_close,
2300 .enter = ehci_device_ctrl_enter,
2301 .start = ehci_device_ctrl_start,
2304 /*------------------------------------------------------------------------*
2305 * ehci interrupt support
2306 *------------------------------------------------------------------------*/
2308 ehci_device_intr_open(struct usb_xfer *xfer)
2310 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2316 /* Allocate a microframe slot first: */
2318 slot = usb_intr_schedule_adjust
2319 (xfer->xroot->udev, xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX);
2321 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
2322 xfer->usb_uframe = slot;
2323 xfer->usb_smask = (1 << slot) & 0xFF;
2324 xfer->usb_cmask = 0;
2326 xfer->usb_uframe = slot;
2327 xfer->usb_smask = (1 << slot) & 0x3F;
2328 xfer->usb_cmask = (-(4 << slot)) & 0xFE;
2332 * Find the best QH position corresponding to the given interval:
2336 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
2338 if (xfer->interval >= bit) {
2342 if (sc->sc_intr_stat[x] <
2343 sc->sc_intr_stat[best]) {
2353 sc->sc_intr_stat[best]++;
2354 xfer->qh_pos = best;
2356 DPRINTFN(3, "best=%d interval=%d\n",
2357 best, xfer->interval);
2361 ehci_device_intr_close(struct usb_xfer *xfer)
2363 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2366 slot = usb_intr_schedule_adjust
2367 (xfer->xroot->udev, -(xfer->max_frame_size), xfer->usb_uframe);
2369 sc->sc_intr_stat[xfer->qh_pos]--;
2371 ehci_device_done(xfer, USB_ERR_CANCELLED);
2375 ehci_device_intr_enter(struct usb_xfer *xfer)
2381 ehci_device_intr_start(struct usb_xfer *xfer)
2383 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2385 /* setup TD's and QH */
2386 ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
2388 /* put transfer on interrupt queue */
2389 ehci_transfer_intr_enqueue(xfer);
2392 struct usb_pipe_methods ehci_device_intr_methods =
2394 .open = ehci_device_intr_open,
2395 .close = ehci_device_intr_close,
2396 .enter = ehci_device_intr_enter,
2397 .start = ehci_device_intr_start,
2400 /*------------------------------------------------------------------------*
2401 * ehci full speed isochronous support
2402 *------------------------------------------------------------------------*/
2404 ehci_device_isoc_fs_open(struct usb_xfer *xfer)
2406 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2408 uint32_t sitd_portaddr;
2412 EHCI_SITD_SET_ADDR(xfer->address) |
2413 EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
2414 EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
2415 EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
2417 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2418 sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
2420 sitd_portaddr = htohc32(sc, sitd_portaddr);
2422 /* initialize all TD's */
2424 for (ds = 0; ds != 2; ds++) {
2426 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2428 td->sitd_portaddr = sitd_portaddr;
2431 * TODO: make some kind of automatic
2432 * SMASK/CMASK selection based on micro-frame
2435 * micro-frame usage (8 microframes per 1ms)
2437 td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
2439 usb_pc_cpu_flush(td->page_cache);
2445 ehci_device_isoc_fs_close(struct usb_xfer *xfer)
2447 ehci_device_done(xfer, USB_ERR_CANCELLED);
2451 ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
2453 struct usb_page_search buf_res;
2454 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2455 struct usb_fs_isoc_schedule *fss_start;
2456 struct usb_fs_isoc_schedule *fss_end;
2457 struct usb_fs_isoc_schedule *fss;
2459 ehci_sitd_t *td_last = NULL;
2460 ehci_sitd_t **pp_last;
2462 uint32_t buf_offset;
2476 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2477 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2479 /* get the current frame index */
2481 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2484 * check if the frame index is within the window where the frames
2487 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2488 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2490 if ((xfer->endpoint->is_synced == 0) ||
2491 (buf_offset < xfer->nframes)) {
2493 * If there is data underflow or the pipe queue is empty we
2494 * schedule the transfer a few frames ahead of the current
2495 * frame position. Else two isochronous transfers might
2498 xfer->endpoint->isoc_next = (nframes + 3) &
2499 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2500 xfer->endpoint->is_synced = 1;
2501 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2504 * compute how many milliseconds the insertion is ahead of the
2505 * current frame position:
2507 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2508 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2511 * pre-compute when the isochronous transfer will be finished:
2513 xfer->isoc_time_complete =
2514 usbd_fs_isoc_schedule_isoc_time_expand
2515 (xfer->xroot->udev, &fss_start, &fss_end, nframes) + buf_offset +
2518 /* get the real number of frames */
2520 nframes = xfer->nframes;
2524 plen = xfer->frlengths;
2526 /* toggle the DMA set we are using */
2527 xfer->flags_int.curr_dma_set ^= 1;
2529 /* get next DMA set */
2530 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2531 xfer->td_transfer_first = td;
2533 pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
2535 /* store starting position */
2537 xfer->qh_pos = xfer->endpoint->isoc_next;
2539 fss = fss_start + (xfer->qh_pos % USB_ISOC_TIME_MAX);
2543 panic("%s:%d: out of TD's\n",
2544 __FUNCTION__, __LINE__);
2546 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2547 pp_last = &sc->sc_isoc_fs_p_last[0];
2549 if (fss >= fss_end) {
2552 /* reuse sitd_portaddr and sitd_back from last transfer */
2554 if (*plen > xfer->max_frame_size) {
2558 printf("%s: frame length(%d) exceeds %d "
2559 "bytes (frame truncated)\n",
2560 __FUNCTION__, *plen,
2561 xfer->max_frame_size);
2564 *plen = xfer->max_frame_size;
2567 * We currently don't care if the ISOCHRONOUS schedule is
2570 error = usbd_fs_isoc_schedule_alloc(fss, &sa, *plen);
2573 * The FULL speed schedule is FULL! Set length
2580 * only call "usbd_get_page()" when we have a
2583 usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2584 td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
2585 buf_offset += *plen;
2587 * NOTE: We need to subtract one from the offset so
2588 * that we are on a valid page!
2590 usbd_get_page(xfer->frbuffers, buf_offset - 1,
2592 temp = buf_res.physaddr & ~0xFFF;
2598 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
2601 temp |= 1; /* T-count = 1, TP = ALL */
2606 temp |= tlen; /* T-count = [1..6] */
2607 temp |= 8; /* TP = Begin */
2619 sa = (sb - sa) & 0x3F;
2622 sb = (-(4 << sa)) & 0xFE;
2623 sa = (1 << sa) & 0x3F;
2626 sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
2627 EHCI_SITD_SET_CMASK(sb));
2629 td->sitd_bp[1] = htohc32(sc, temp);
2631 td->sitd_mask = htohc32(sc, sitd_mask);
2634 td->sitd_status = htohc32(sc,
2637 EHCI_SITD_SET_LEN(*plen));
2639 td->sitd_status = htohc32(sc,
2641 EHCI_SITD_SET_LEN(*plen));
2643 usb_pc_cpu_flush(td->page_cache);
2646 if (ehcidebug > 15) {
2647 DPRINTF("FS-TD %d\n", nframes);
2648 ehci_dump_sitd(sc, td);
2651 /* insert TD into schedule */
2652 EHCI_APPEND_FS_TD(td, *pp_last);
2661 xfer->td_transfer_last = td_last;
2663 /* update isoc_next */
2664 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
2665 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2669 ehci_device_isoc_fs_start(struct usb_xfer *xfer)
2671 /* put transfer on interrupt queue */
2672 ehci_transfer_intr_enqueue(xfer);
2675 struct usb_pipe_methods ehci_device_isoc_fs_methods =
2677 .open = ehci_device_isoc_fs_open,
2678 .close = ehci_device_isoc_fs_close,
2679 .enter = ehci_device_isoc_fs_enter,
2680 .start = ehci_device_isoc_fs_start,
2683 /*------------------------------------------------------------------------*
2684 * ehci high speed isochronous support
2685 *------------------------------------------------------------------------*/
2687 ehci_device_isoc_hs_open(struct usb_xfer *xfer)
2689 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2694 /* initialize all TD's */
2696 for (ds = 0; ds != 2; ds++) {
2698 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2700 /* set TD inactive */
2701 td->itd_status[0] = 0;
2702 td->itd_status[1] = 0;
2703 td->itd_status[2] = 0;
2704 td->itd_status[3] = 0;
2705 td->itd_status[4] = 0;
2706 td->itd_status[5] = 0;
2707 td->itd_status[6] = 0;
2708 td->itd_status[7] = 0;
2710 /* set endpoint and address */
2711 td->itd_bp[0] = htohc32(sc,
2712 EHCI_ITD_SET_ADDR(xfer->address) |
2713 EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
2716 EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
2719 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2720 temp |= EHCI_ITD_SET_DIR_IN;
2722 /* set maximum packet size */
2723 td->itd_bp[1] = htohc32(sc, temp);
2725 /* set transfer multiplier */
2726 td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
2728 usb_pc_cpu_flush(td->page_cache);
2734 ehci_device_isoc_hs_close(struct usb_xfer *xfer)
2736 ehci_device_done(xfer, USB_ERR_CANCELLED);
2740 ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
2742 struct usb_page_search buf_res;
2743 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2745 ehci_itd_t *td_last = NULL;
2746 ehci_itd_t **pp_last;
2747 bus_size_t page_addr;
2750 uint32_t buf_offset;
2752 uint32_t itd_offset[8 + 1];
2762 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2763 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2765 /* get the current frame index */
2767 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2770 * check if the frame index is within the window where the frames
2773 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2774 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2776 if ((xfer->endpoint->is_synced == 0) ||
2777 (buf_offset < ((xfer->nframes + 7) / 8))) {
2779 * If there is data underflow or the pipe queue is empty we
2780 * schedule the transfer a few frames ahead of the current
2781 * frame position. Else two isochronous transfers might
2784 xfer->endpoint->isoc_next = (nframes + 3) &
2785 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2786 xfer->endpoint->is_synced = 1;
2787 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2790 * compute how many milliseconds the insertion is ahead of the
2791 * current frame position:
2793 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2794 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2797 * pre-compute when the isochronous transfer will be finished:
2799 xfer->isoc_time_complete =
2800 usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2801 ((xfer->nframes + 7) / 8);
2803 /* get the real number of frames */
2805 nframes = xfer->nframes;
2810 plen = xfer->frlengths;
2812 /* toggle the DMA set we are using */
2813 xfer->flags_int.curr_dma_set ^= 1;
2815 /* get next DMA set */
2816 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2817 xfer->td_transfer_first = td;
2819 pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
2821 /* store starting position */
2823 xfer->qh_pos = xfer->endpoint->isoc_next;
2827 panic("%s:%d: out of TD's\n",
2828 __FUNCTION__, __LINE__);
2830 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2831 pp_last = &sc->sc_isoc_hs_p_last[0];
2834 if (*plen > xfer->max_frame_size) {
2838 printf("%s: frame length(%d) exceeds %d bytes "
2839 "(frame truncated)\n",
2840 __FUNCTION__, *plen, xfer->max_frame_size);
2843 *plen = xfer->max_frame_size;
2845 status = (EHCI_ITD_SET_LEN(*plen) |
2847 EHCI_ITD_SET_PG(0));
2848 td->itd_status[td_no] = htohc32(sc, status);
2849 itd_offset[td_no] = buf_offset;
2850 buf_offset += *plen;
2854 if ((td_no == 8) || (nframes == 0)) {
2856 /* the rest of the transfers are not active, if any */
2857 for (x = td_no; x != 8; x++) {
2858 td->itd_status[x] = 0; /* not active */
2861 /* check if there is any data to be transferred */
2862 if (itd_offset[0] != buf_offset) {
2864 itd_offset[td_no] = buf_offset;
2866 /* get first page offset */
2867 usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
2868 /* get page address */
2869 page_addr = buf_res.physaddr & ~0xFFF;
2870 /* update page address */
2871 td->itd_bp[0] &= htohc32(sc, 0xFFF);
2872 td->itd_bp[0] |= htohc32(sc, page_addr);
2874 for (x = 0; x != td_no; x++) {
2875 /* set page number and page offset */
2876 status = (EHCI_ITD_SET_PG(page_no) |
2877 (buf_res.physaddr & 0xFFF));
2878 td->itd_status[x] |= htohc32(sc, status);
2880 /* get next page offset */
2881 if (itd_offset[x + 1] == buf_offset) {
2883 * We subtract one so that
2884 * we don't go off the last
2887 usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
2889 usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
2892 /* check if we need a new page */
2893 if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
2894 /* new page needed */
2895 page_addr = buf_res.physaddr & ~0xFFF;
2897 panic("%s: too many pages\n", __FUNCTION__);
2900 /* update page address */
2901 td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2902 td->itd_bp[page_no] |= htohc32(sc, page_addr);
2906 /* set IOC bit if we are complete */
2908 td->itd_status[7] |= htohc32(sc, EHCI_ITD_IOC);
2910 usb_pc_cpu_flush(td->page_cache);
2912 if (ehcidebug > 15) {
2913 DPRINTF("HS-TD %d\n", nframes);
2914 ehci_dump_itd(sc, td);
2917 /* insert TD into schedule */
2918 EHCI_APPEND_HS_TD(td, *pp_last);
2927 xfer->td_transfer_last = td_last;
2929 /* update isoc_next */
2930 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
2931 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2935 ehci_device_isoc_hs_start(struct usb_xfer *xfer)
2937 /* put transfer on interrupt queue */
2938 ehci_transfer_intr_enqueue(xfer);
2941 struct usb_pipe_methods ehci_device_isoc_hs_methods =
2943 .open = ehci_device_isoc_hs_open,
2944 .close = ehci_device_isoc_hs_close,
2945 .enter = ehci_device_isoc_hs_enter,
2946 .start = ehci_device_isoc_hs_start,
2949 /*------------------------------------------------------------------------*
2950 * ehci root control support
2951 *------------------------------------------------------------------------*
2952 * Simulate a hardware hub by handling all the necessary requests.
2953 *------------------------------------------------------------------------*/
2956 struct usb_device_descriptor ehci_devd =
2958 sizeof(struct usb_device_descriptor),
2959 UDESC_DEVICE, /* type */
2960 {0x00, 0x02}, /* USB version */
2961 UDCLASS_HUB, /* class */
2962 UDSUBCLASS_HUB, /* subclass */
2963 UDPROTO_HSHUBSTT, /* protocol */
2964 64, /* max packet */
2965 {0}, {0}, {0x00, 0x01}, /* device id */
2966 1, 2, 0, /* string indicies */
2967 1 /* # of configurations */
2971 struct usb_device_qualifier ehci_odevd =
2973 sizeof(struct usb_device_qualifier),
2974 UDESC_DEVICE_QUALIFIER, /* type */
2975 {0x00, 0x02}, /* USB version */
2976 UDCLASS_HUB, /* class */
2977 UDSUBCLASS_HUB, /* subclass */
2978 UDPROTO_FSHUB, /* protocol */
2980 0, /* # of configurations */
2984 static const struct ehci_config_desc ehci_confd = {
2986 .bLength = sizeof(struct usb_config_descriptor),
2987 .bDescriptorType = UDESC_CONFIG,
2988 .wTotalLength[0] = sizeof(ehci_confd),
2990 .bConfigurationValue = 1,
2991 .iConfiguration = 0,
2992 .bmAttributes = UC_SELF_POWERED,
2993 .bMaxPower = 0 /* max power */
2996 .bLength = sizeof(struct usb_interface_descriptor),
2997 .bDescriptorType = UDESC_INTERFACE,
2999 .bInterfaceClass = UICLASS_HUB,
3000 .bInterfaceSubClass = UISUBCLASS_HUB,
3001 .bInterfaceProtocol = UIPROTO_HSHUBSTT,
3005 .bLength = sizeof(struct usb_endpoint_descriptor),
3006 .bDescriptorType = UDESC_ENDPOINT,
3007 .bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
3008 .bmAttributes = UE_INTERRUPT,
3009 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
3015 struct usb_hub_descriptor ehci_hubd =
3017 0, /* dynamic length */
3027 ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
3032 DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
3034 port = EHCI_PORTSC(index);
3035 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3036 EOWRITE4(sc, port, v | EHCI_PS_PO);
3040 ehci_roothub_exec(struct usb_device *udev,
3041 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3043 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3044 const char *str_ptr;
3055 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3058 ptr = (const void *)&sc->sc_hub_desc;
3062 value = UGETW(req->wValue);
3063 index = UGETW(req->wIndex);
3065 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3066 "wValue=0x%04x wIndex=0x%04x\n",
3067 req->bmRequestType, req->bRequest,
3068 UGETW(req->wLength), value, index);
3070 #define C(x,y) ((x) | ((y) << 8))
3071 switch (C(req->bRequest, req->bmRequestType)) {
3072 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3073 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3074 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3076 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3077 * for the integrated root hub.
3080 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3082 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3084 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3085 switch (value >> 8) {
3087 if ((value & 0xff) != 0) {
3088 err = USB_ERR_IOERROR;
3091 len = sizeof(ehci_devd);
3092 ptr = (const void *)&ehci_devd;
3095 * We can't really operate at another speed,
3096 * but the specification says we need this
3099 case UDESC_DEVICE_QUALIFIER:
3100 if ((value & 0xff) != 0) {
3101 err = USB_ERR_IOERROR;
3104 len = sizeof(ehci_odevd);
3105 ptr = (const void *)&ehci_odevd;
3109 if ((value & 0xff) != 0) {
3110 err = USB_ERR_IOERROR;
3113 len = sizeof(ehci_confd);
3114 ptr = (const void *)&ehci_confd;
3118 switch (value & 0xff) {
3119 case 0: /* Language table */
3123 case 1: /* Vendor */
3124 str_ptr = sc->sc_vendor;
3127 case 2: /* Product */
3128 str_ptr = "EHCI root HUB";
3136 len = usb_make_str_desc(
3137 sc->sc_hub_desc.temp,
3138 sizeof(sc->sc_hub_desc.temp),
3142 err = USB_ERR_IOERROR;
3146 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3148 sc->sc_hub_desc.temp[0] = 0;
3150 case C(UR_GET_STATUS, UT_READ_DEVICE):
3152 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3154 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3155 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3157 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3159 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3160 if (value >= EHCI_MAX_DEVICES) {
3161 err = USB_ERR_IOERROR;
3164 sc->sc_addr = value;
3166 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3167 if ((value != 0) && (value != 1)) {
3168 err = USB_ERR_IOERROR;
3171 sc->sc_conf = value;
3173 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3175 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3176 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3177 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3178 err = USB_ERR_IOERROR;
3180 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3182 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3185 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3187 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3188 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3191 (index > sc->sc_noport)) {
3192 err = USB_ERR_IOERROR;
3195 port = EHCI_PORTSC(index);
3196 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3198 case UHF_PORT_ENABLE:
3199 EOWRITE4(sc, port, v & ~EHCI_PS_PE);
3201 case UHF_PORT_SUSPEND:
3202 if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
3205 * waking up a High Speed device is rather
3208 EOWRITE4(sc, port, v | EHCI_PS_FPR);
3210 /* wait 20ms for resume sequence to complete */
3211 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3213 EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
3214 EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
3216 /* 4ms settle time */
3217 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3219 case UHF_PORT_POWER:
3220 EOWRITE4(sc, port, v & ~EHCI_PS_PP);
3223 DPRINTFN(3, "clear port test "
3226 case UHF_PORT_INDICATOR:
3227 DPRINTFN(3, "clear port ind "
3229 EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
3231 case UHF_C_PORT_CONNECTION:
3232 EOWRITE4(sc, port, v | EHCI_PS_CSC);
3234 case UHF_C_PORT_ENABLE:
3235 EOWRITE4(sc, port, v | EHCI_PS_PEC);
3237 case UHF_C_PORT_SUSPEND:
3238 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3240 case UHF_C_PORT_OVER_CURRENT:
3241 EOWRITE4(sc, port, v | EHCI_PS_OCC);
3243 case UHF_C_PORT_RESET:
3247 err = USB_ERR_IOERROR;
3251 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3252 if ((value & 0xff) != 0) {
3253 err = USB_ERR_IOERROR;
3256 v = EOREAD4(sc, EHCI_HCSPARAMS);
3258 sc->sc_hub_desc.hubd = ehci_hubd;
3259 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3260 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics,
3261 (EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH) |
3262 (EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS)) ?
3264 /* XXX can't find out? */
3265 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
3266 for (l = 0; l < sc->sc_noport; l++) {
3267 /* XXX can't find out? */
3268 sc->sc_hub_desc.hubd.DeviceRemovable[l / 8] &= ~(1 << (l % 8));
3270 sc->sc_hub_desc.hubd.bDescLength =
3271 8 + ((sc->sc_noport + 7) / 8);
3272 len = sc->sc_hub_desc.hubd.bDescLength;
3274 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3276 bzero(sc->sc_hub_desc.temp, 16);
3278 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3279 DPRINTFN(9, "get port status i=%d\n",
3282 (index > sc->sc_noport)) {
3283 err = USB_ERR_IOERROR;
3286 v = EOREAD4(sc, EHCI_PORTSC(index));
3287 DPRINTFN(9, "port status=0x%04x\n", v);
3288 if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
3289 if ((v & 0xc000000) == 0x8000000)
3291 else if ((v & 0xc000000) == 0x4000000)
3299 i |= UPS_CURRENT_CONNECT_STATUS;
3301 i |= UPS_PORT_ENABLED;
3302 if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
3304 if (v & EHCI_PS_OCA)
3305 i |= UPS_OVERCURRENT_INDICATOR;
3309 i |= UPS_PORT_POWER;
3310 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3312 if (v & EHCI_PS_CSC)
3313 i |= UPS_C_CONNECT_STATUS;
3314 if (v & EHCI_PS_PEC)
3315 i |= UPS_C_PORT_ENABLED;
3316 if (v & EHCI_PS_OCC)
3317 i |= UPS_C_OVERCURRENT_INDICATOR;
3318 if (v & EHCI_PS_FPR)
3321 i |= UPS_C_PORT_RESET;
3322 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3323 len = sizeof(sc->sc_hub_desc.ps);
3325 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3326 err = USB_ERR_IOERROR;
3328 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3330 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3332 (index > sc->sc_noport)) {
3333 err = USB_ERR_IOERROR;
3336 port = EHCI_PORTSC(index);
3337 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3339 case UHF_PORT_ENABLE:
3340 EOWRITE4(sc, port, v | EHCI_PS_PE);
3342 case UHF_PORT_SUSPEND:
3343 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3345 case UHF_PORT_RESET:
3346 DPRINTFN(6, "reset port %d\n", index);
3348 if (ehcinohighspeed) {
3350 * Connect USB device to companion
3353 ehci_disown(sc, index, 1);
3357 if (EHCI_PS_IS_LOWSPEED(v) &&
3358 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3359 /* Low speed device, give up ownership. */
3360 ehci_disown(sc, index, 1);
3363 /* Start reset sequence. */
3364 v &= ~(EHCI_PS_PE | EHCI_PS_PR);
3365 EOWRITE4(sc, port, v | EHCI_PS_PR);
3367 /* Wait for reset to complete. */
3368 usb_pause_mtx(&sc->sc_bus.bus_mtx,
3369 USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
3371 /* Terminate reset sequence. */
3372 if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
3373 EOWRITE4(sc, port, v);
3375 /* Wait for HC to complete reset. */
3376 usb_pause_mtx(&sc->sc_bus.bus_mtx,
3377 USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
3379 v = EOREAD4(sc, port);
3380 DPRINTF("ehci after reset, status=0x%08x\n", v);
3381 if (v & EHCI_PS_PR) {
3382 device_printf(sc->sc_bus.bdev,
3383 "port reset timeout\n");
3384 err = USB_ERR_TIMEOUT;
3387 if (!(v & EHCI_PS_PE) &&
3388 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3389 /* Not a high speed device, give up ownership.*/
3390 ehci_disown(sc, index, 0);
3394 DPRINTF("ehci port %d reset, status = 0x%08x\n",
3398 case UHF_PORT_POWER:
3399 DPRINTFN(3, "set port power %d\n", index);
3400 EOWRITE4(sc, port, v | EHCI_PS_PP);
3404 DPRINTFN(3, "set port test %d\n", index);
3407 case UHF_PORT_INDICATOR:
3408 DPRINTFN(3, "set port ind %d\n", index);
3409 EOWRITE4(sc, port, v | EHCI_PS_PIC);
3413 err = USB_ERR_IOERROR;
3417 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3418 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3419 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3420 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3423 err = USB_ERR_IOERROR;
3433 ehci_xfer_setup(struct usb_setup_params *parm)
3435 struct usb_page_search page_info;
3436 struct usb_page_cache *pc;
3438 struct usb_xfer *xfer;
3446 sc = EHCI_BUS2SC(parm->udev->bus);
3447 xfer = parm->curr_xfer;
3455 * compute maximum number of some structures
3457 if (parm->methods == &ehci_device_ctrl_methods) {
3460 * The proof for the "nqtd" formula is illustrated like
3463 * +------------------------------------+
3467 * | | xxx | x | frm 0 |
3469 * | | xxx | xx | frm 1 |
3472 * +------------------------------------+
3474 * "xxx" means a completely full USB transfer descriptor
3476 * "x" and "xx" means a short USB packet
3478 * For the remainder of an USB transfer modulo
3479 * "max_data_length" we need two USB transfer descriptors.
3480 * One to transfer the remaining data and one to finalise
3481 * with a zero length packet in case the "force_short_xfer"
3482 * flag is set. We only need two USB transfer descriptors in
3483 * the case where the transfer length of the first one is a
3484 * factor of "max_frame_size". The rest of the needed USB
3485 * transfer descriptors is given by the buffer size divided
3486 * by the maximum data payload.
3488 parm->hc_max_packet_size = 0x400;
3489 parm->hc_max_packet_count = 1;
3490 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3491 xfer->flags_int.bdma_enable = 1;
3493 usbd_transfer_setup_sub(parm);
3496 nqtd = ((2 * xfer->nframes) + 1 /* STATUS */
3497 + (xfer->max_data_length / xfer->max_hc_frame_size));
3499 } else if (parm->methods == &ehci_device_bulk_methods) {
3501 parm->hc_max_packet_size = 0x400;
3502 parm->hc_max_packet_count = 1;
3503 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3504 xfer->flags_int.bdma_enable = 1;
3506 usbd_transfer_setup_sub(parm);
3509 nqtd = ((2 * xfer->nframes)
3510 + (xfer->max_data_length / xfer->max_hc_frame_size));
3512 } else if (parm->methods == &ehci_device_intr_methods) {
3514 if (parm->speed == USB_SPEED_HIGH) {
3515 parm->hc_max_packet_size = 0x400;
3516 parm->hc_max_packet_count = 3;
3517 } else if (parm->speed == USB_SPEED_FULL) {
3518 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
3519 parm->hc_max_packet_count = 1;
3521 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
3522 parm->hc_max_packet_count = 1;
3525 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3526 xfer->flags_int.bdma_enable = 1;
3528 usbd_transfer_setup_sub(parm);
3531 nqtd = ((2 * xfer->nframes)
3532 + (xfer->max_data_length / xfer->max_hc_frame_size));
3534 } else if (parm->methods == &ehci_device_isoc_fs_methods) {
3536 parm->hc_max_packet_size = 0x3FF;
3537 parm->hc_max_packet_count = 1;
3538 parm->hc_max_frame_size = 0x3FF;
3539 xfer->flags_int.bdma_enable = 1;
3541 usbd_transfer_setup_sub(parm);
3543 nsitd = xfer->nframes;
3545 } else if (parm->methods == &ehci_device_isoc_hs_methods) {
3547 parm->hc_max_packet_size = 0x400;
3548 parm->hc_max_packet_count = 3;
3549 parm->hc_max_frame_size = 0xC00;
3550 xfer->flags_int.bdma_enable = 1;
3552 usbd_transfer_setup_sub(parm);
3554 nitd = (xfer->nframes + 7) / 8;
3558 parm->hc_max_packet_size = 0x400;
3559 parm->hc_max_packet_count = 1;
3560 parm->hc_max_frame_size = 0x400;
3562 usbd_transfer_setup_sub(parm);
3571 * Allocate queue heads and transfer descriptors
3575 if (usbd_transfer_setup_sub_malloc(
3576 parm, &pc, sizeof(ehci_itd_t),
3577 EHCI_ITD_ALIGN, nitd)) {
3578 parm->err = USB_ERR_NOMEM;
3582 for (n = 0; n != nitd; n++) {
3585 usbd_get_page(pc + n, 0, &page_info);
3587 td = page_info.buffer;
3590 td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
3591 td->obj_next = last_obj;
3592 td->page_cache = pc + n;
3596 usb_pc_cpu_flush(pc + n);
3599 if (usbd_transfer_setup_sub_malloc(
3600 parm, &pc, sizeof(ehci_sitd_t),
3601 EHCI_SITD_ALIGN, nsitd)) {
3602 parm->err = USB_ERR_NOMEM;
3606 for (n = 0; n != nsitd; n++) {
3609 usbd_get_page(pc + n, 0, &page_info);
3611 td = page_info.buffer;
3614 td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
3615 td->obj_next = last_obj;
3616 td->page_cache = pc + n;
3620 usb_pc_cpu_flush(pc + n);
3623 if (usbd_transfer_setup_sub_malloc(
3624 parm, &pc, sizeof(ehci_qtd_t),
3625 EHCI_QTD_ALIGN, nqtd)) {
3626 parm->err = USB_ERR_NOMEM;
3630 for (n = 0; n != nqtd; n++) {
3633 usbd_get_page(pc + n, 0, &page_info);
3635 qtd = page_info.buffer;
3638 qtd->qtd_self = htohc32(sc, page_info.physaddr);
3639 qtd->obj_next = last_obj;
3640 qtd->page_cache = pc + n;
3644 usb_pc_cpu_flush(pc + n);
3647 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3651 if (usbd_transfer_setup_sub_malloc(
3652 parm, &pc, sizeof(ehci_qh_t),
3653 EHCI_QH_ALIGN, nqh)) {
3654 parm->err = USB_ERR_NOMEM;
3658 for (n = 0; n != nqh; n++) {
3661 usbd_get_page(pc + n, 0, &page_info);
3663 qh = page_info.buffer;
3666 qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
3667 qh->obj_next = last_obj;
3668 qh->page_cache = pc + n;
3672 usb_pc_cpu_flush(pc + n);
3675 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3677 if (!xfer->flags_int.curr_dma_set) {
3678 xfer->flags_int.curr_dma_set = 1;
3684 ehci_xfer_unsetup(struct usb_xfer *xfer)
3690 ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3691 struct usb_endpoint *ep)
3693 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3695 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3697 edesc->bEndpointAddress, udev->flags.usb_mode,
3700 if (udev->flags.usb_mode != USB_MODE_HOST) {
3704 if (udev->device_index != sc->sc_addr) {
3706 if ((udev->speed != USB_SPEED_HIGH) &&
3707 ((udev->hs_hub_addr == 0) ||
3708 (udev->hs_port_no == 0) ||
3709 (udev->parent_hs_hub == NULL) ||
3710 (udev->parent_hs_hub->hub == NULL))) {
3711 /* We need a transaction translator */
3714 switch (edesc->bmAttributes & UE_XFERTYPE) {
3716 ep->methods = &ehci_device_ctrl_methods;
3719 ep->methods = &ehci_device_intr_methods;
3721 case UE_ISOCHRONOUS:
3722 if (udev->speed == USB_SPEED_HIGH) {
3723 ep->methods = &ehci_device_isoc_hs_methods;
3724 } else if (udev->speed == USB_SPEED_FULL) {
3725 ep->methods = &ehci_device_isoc_fs_methods;
3729 if (udev->speed != USB_SPEED_LOW) {
3730 ep->methods = &ehci_device_bulk_methods;
3743 ehci_get_dma_delay(struct usb_bus *bus, uint32_t *pus)
3746 * Wait until the hardware has finished any possible use of
3747 * the transfer descriptor(s) and QH
3749 *pus = (188); /* microseconds */
3753 ehci_device_resume(struct usb_device *udev)
3755 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3756 struct usb_xfer *xfer;
3757 struct usb_pipe_methods *methods;
3761 USB_BUS_LOCK(udev->bus);
3763 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3765 if (xfer->xroot->udev == udev) {
3767 methods = xfer->endpoint->methods;
3769 if ((methods == &ehci_device_bulk_methods) ||
3770 (methods == &ehci_device_ctrl_methods)) {
3771 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3772 sc->sc_async_p_last);
3774 if (methods == &ehci_device_intr_methods) {
3775 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3776 sc->sc_intr_p_last[xfer->qh_pos]);
3781 USB_BUS_UNLOCK(udev->bus);
3787 ehci_device_suspend(struct usb_device *udev)
3789 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3790 struct usb_xfer *xfer;
3791 struct usb_pipe_methods *methods;
3795 USB_BUS_LOCK(udev->bus);
3797 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3799 if (xfer->xroot->udev == udev) {
3801 methods = xfer->endpoint->methods;
3803 if ((methods == &ehci_device_bulk_methods) ||
3804 (methods == &ehci_device_ctrl_methods)) {
3805 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3806 sc->sc_async_p_last);
3808 if (methods == &ehci_device_intr_methods) {
3809 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3810 sc->sc_intr_p_last[xfer->qh_pos]);
3815 USB_BUS_UNLOCK(udev->bus);
3821 ehci_set_hw_power(struct usb_bus *bus)
3823 ehci_softc_t *sc = EHCI_BUS2SC(bus);
3831 flags = bus->hw_power_state;
3833 temp = EOREAD4(sc, EHCI_USBCMD);
3835 temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
3837 if (flags & (USB_HW_POWER_CONTROL |
3838 USB_HW_POWER_BULK)) {
3839 DPRINTF("Async is active\n");
3840 temp |= EHCI_CMD_ASE;
3842 if (flags & (USB_HW_POWER_INTERRUPT |
3843 USB_HW_POWER_ISOC)) {
3844 DPRINTF("Periodic is active\n");
3845 temp |= EHCI_CMD_PSE;
3847 EOWRITE4(sc, EHCI_USBCMD, temp);
3849 USB_BUS_UNLOCK(bus);
3854 struct usb_bus_methods ehci_bus_methods =
3856 .endpoint_init = ehci_ep_init,
3857 .xfer_setup = ehci_xfer_setup,
3858 .xfer_unsetup = ehci_xfer_unsetup,
3859 .get_dma_delay = ehci_get_dma_delay,
3860 .device_resume = ehci_device_resume,
3861 .device_suspend = ehci_device_suspend,
3862 .set_hw_power = ehci_set_hw_power,
3863 .roothub_exec = ehci_roothub_exec,
3864 .xfer_poll = ehci_do_poll,