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/module.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
71 #define USB_DEBUG_VAR ehcidebug
73 #include <dev/usb/usb_core.h>
74 #include <dev/usb/usb_debug.h>
75 #include <dev/usb/usb_busdma.h>
76 #include <dev/usb/usb_process.h>
77 #include <dev/usb/usb_transfer.h>
78 #include <dev/usb/usb_device.h>
79 #include <dev/usb/usb_hub.h>
80 #include <dev/usb/usb_util.h>
82 #include <dev/usb/usb_controller.h>
83 #include <dev/usb/usb_bus.h>
84 #include <dev/usb/controller/ehci.h>
85 #include <dev/usb/controller/ehcireg.h>
87 #define EHCI_BUS2SC(bus) \
88 ((ehci_softc_t *)(((uint8_t *)(bus)) - \
89 ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
92 static int ehcidebug = 0;
93 static int ehcinohighspeed = 0;
94 static int ehciiaadbug = 0;
95 static int ehcilostintrbug = 0;
97 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
98 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
99 &ehcidebug, 0, "Debug level");
100 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
101 &ehcinohighspeed, 0, "Disable High Speed USB");
102 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RW,
103 &ehciiaadbug, 0, "Enable doorbell bug workaround");
104 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RW,
105 &ehcilostintrbug, 0, "Enable lost interrupt bug workaround");
107 TUNABLE_INT("hw.usb.ehci.debug", &ehcidebug);
108 TUNABLE_INT("hw.usb.ehci.no_hs", &ehcinohighspeed);
109 TUNABLE_INT("hw.usb.ehci.iaadbug", &ehciiaadbug);
110 TUNABLE_INT("hw.usb.ehci.lostintrbug", &ehcilostintrbug);
112 static void ehci_dump_regs(ehci_softc_t *sc);
113 static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
117 #define EHCI_INTR_ENDPT 1
119 extern struct usb_bus_methods ehci_bus_methods;
120 extern struct usb_pipe_methods ehci_device_bulk_methods;
121 extern struct usb_pipe_methods ehci_device_ctrl_methods;
122 extern struct usb_pipe_methods ehci_device_intr_methods;
123 extern struct usb_pipe_methods ehci_device_isoc_fs_methods;
124 extern struct usb_pipe_methods ehci_device_isoc_hs_methods;
126 static void ehci_do_poll(struct usb_bus *);
127 static void ehci_device_done(struct usb_xfer *, usb_error_t);
128 static uint8_t ehci_check_transfer(struct usb_xfer *);
129 static void ehci_timeout(void *);
130 static void ehci_poll_timeout(void *);
132 static void ehci_root_intr(ehci_softc_t *sc);
134 struct ehci_std_temp {
136 struct usb_page_cache *pc;
142 uint16_t max_frame_size;
144 uint8_t auto_data_toggle;
145 uint8_t setup_alt_next;
150 ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
152 ehci_softc_t *sc = EHCI_BUS2SC(bus);
155 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
156 sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
158 cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg,
159 sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN);
161 cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
162 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
164 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
165 cb(bus, sc->sc_hw.intr_start_pc + i,
166 sc->sc_hw.intr_start_pg + i,
167 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
170 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
171 cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
172 sc->sc_hw.isoc_hs_start_pg + i,
173 sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
176 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
177 cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
178 sc->sc_hw.isoc_fs_start_pg + i,
179 sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
184 ehci_reset(ehci_softc_t *sc)
189 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
190 for (i = 0; i < 100; i++) {
191 usb_pause_mtx(NULL, hz / 128);
192 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
194 if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
196 * Force USBMODE as requested. Controllers
197 * may have multiple operating modes.
199 uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
200 if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
201 usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
202 device_printf(sc->sc_bus.bdev,
203 "set host controller mode\n");
205 if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
206 usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
207 device_printf(sc->sc_bus.bdev,
208 "set big-endian mode\n");
210 EOWRITE4(sc, EHCI_USBMODE, usbmode);
215 device_printf(sc->sc_bus.bdev, "Reset timeout\n");
216 return (USB_ERR_IOERROR);
220 ehci_hcreset(ehci_softc_t *sc)
225 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
226 for (i = 0; i < 100; i++) {
227 usb_pause_mtx(NULL, hz / 128);
228 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
234 * Fall through and try reset anyway even though
235 * Table 2-9 in the EHCI spec says this will result
236 * in undefined behavior.
238 device_printf(sc->sc_bus.bdev, "stop timeout\n");
240 return (ehci_reset(sc));
244 ehci_init_sub(struct ehci_softc *sc)
246 struct usb_page_search buf_res;
251 cparams = EREAD4(sc, EHCI_HCCPARAMS);
253 DPRINTF("cparams=0x%x\n", cparams);
255 if (EHCI_HCC_64BIT(cparams)) {
256 DPRINTF("HCC uses 64-bit structures\n");
258 /* MUST clear segment register if 64 bit capable */
259 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
262 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
263 EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
265 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
266 EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
268 /* enable interrupts */
269 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
271 /* turn on controller */
272 EOWRITE4(sc, EHCI_USBCMD,
273 EHCI_CMD_ITC_1 | /* 1 microframes interrupt delay */
274 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
279 /* Take over port ownership */
280 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
282 for (i = 0; i < 100; i++) {
283 usb_pause_mtx(NULL, hz / 128);
284 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
290 device_printf(sc->sc_bus.bdev, "Run timeout\n");
291 return (USB_ERR_IOERROR);
293 return (USB_ERR_NORMAL_COMPLETION);
297 ehci_init(ehci_softc_t *sc)
299 struct usb_page_search buf_res;
310 usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
311 usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_mtx, 0);
313 sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
317 sc->sc_flags |= EHCI_SCFLG_IAADBUG;
319 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
325 version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
326 device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
327 version >> 8, version & 0xff);
329 sparams = EREAD4(sc, EHCI_HCSPARAMS);
330 DPRINTF("sparams=0x%x\n", sparams);
332 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
333 sc->sc_bus.usbrev = USB_REV_2_0;
335 /* Reset the controller */
336 DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
338 err = ehci_hcreset(sc);
340 device_printf(sc->sc_bus.bdev, "reset timeout\n");
344 * use current frame-list-size selection 0: 1024*4 bytes 1: 512*4
345 * bytes 2: 256*4 bytes 3: unknown
347 if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
348 device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
349 return (USB_ERR_IOERROR);
351 /* set up the bus struct */
352 sc->sc_bus.methods = &ehci_bus_methods;
354 sc->sc_eintrs = EHCI_NORMAL_INTRS;
357 struct ehci_qh_sub *qh;
359 usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res);
363 sc->sc_terminate_self = htohc32(sc, buf_res.physaddr);
365 /* init terminate TD */
367 htohc32(sc, EHCI_LINK_TERMINATE);
369 htohc32(sc, EHCI_LINK_TERMINATE);
371 htohc32(sc, EHCI_QTD_HALTED);
374 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
377 usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
381 /* initialize page cache pointer */
383 qh->page_cache = sc->sc_hw.intr_start_pc + i;
385 /* store a pointer to queue head */
387 sc->sc_intr_p_last[i] = qh;
390 htohc32(sc, buf_res.physaddr) |
391 htohc32(sc, EHCI_LINK_QH);
394 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
396 htohc32(sc, EHCI_QH_SET_MULT(1));
399 qh->qh_qtd.qtd_next =
400 htohc32(sc, EHCI_LINK_TERMINATE);
401 qh->qh_qtd.qtd_altnext =
402 htohc32(sc, EHCI_LINK_TERMINATE);
403 qh->qh_qtd.qtd_status =
404 htohc32(sc, EHCI_QTD_HALTED);
408 * the QHs are arranged to give poll intervals that are
409 * powers of 2 times 1ms
411 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
418 y = (x ^ bit) | (bit / 2);
420 qh_x = sc->sc_intr_p_last[x];
421 qh_y = sc->sc_intr_p_last[y];
424 * the next QH has half the poll interval
426 qh_x->qh_link = qh_y->qh_self;
436 qh = sc->sc_intr_p_last[0];
438 /* the last (1ms) QH terminates */
439 qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
441 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
445 usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
447 sitd = buf_res.buffer;
449 /* initialize page cache pointer */
451 sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
453 /* store a pointer to the transfer descriptor */
455 sc->sc_isoc_fs_p_last[i] = sitd;
457 /* initialize full speed isochronous */
460 htohc32(sc, buf_res.physaddr) |
461 htohc32(sc, EHCI_LINK_SITD);
464 htohc32(sc, EHCI_LINK_TERMINATE);
467 sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
470 usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
472 itd = buf_res.buffer;
474 /* initialize page cache pointer */
476 itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
478 /* store a pointer to the transfer descriptor */
480 sc->sc_isoc_hs_p_last[i] = itd;
482 /* initialize high speed isochronous */
485 htohc32(sc, buf_res.physaddr) |
486 htohc32(sc, EHCI_LINK_ITD);
492 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
497 pframes = buf_res.buffer;
501 * pframes -> high speed isochronous ->
502 * full speed isochronous -> interrupt QH's
504 for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
505 pframes[i] = sc->sc_isoc_hs_p_last
506 [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
509 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
517 /* initialize page cache pointer */
519 qh->page_cache = &sc->sc_hw.async_start_pc;
521 /* store a pointer to the queue head */
523 sc->sc_async_p_last = qh;
525 /* init dummy QH that starts the async list */
528 htohc32(sc, buf_res.physaddr) |
529 htohc32(sc, EHCI_LINK_QH);
533 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
534 qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
535 qh->qh_link = qh->qh_self;
538 /* fill the overlay qTD */
539 qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
540 qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
541 qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
543 /* flush all cache into memory */
545 usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
549 ehci_dump_sqh(sc, sc->sc_async_p_last);
554 err = ehci_init_sub(sc);
557 /* catch any lost interrupts */
558 ehci_do_poll(&sc->sc_bus);
564 * shut down the controller when the system is going down
567 ehci_detach(ehci_softc_t *sc)
569 USB_BUS_LOCK(&sc->sc_bus);
571 usb_callout_stop(&sc->sc_tmo_pcd);
572 usb_callout_stop(&sc->sc_tmo_poll);
574 EOWRITE4(sc, EHCI_USBINTR, 0);
575 USB_BUS_UNLOCK(&sc->sc_bus);
577 if (ehci_hcreset(sc)) {
578 DPRINTF("reset failed!\n");
581 /* XXX let stray task complete */
582 usb_pause_mtx(NULL, hz / 20);
584 usb_callout_drain(&sc->sc_tmo_pcd);
585 usb_callout_drain(&sc->sc_tmo_poll);
589 ehci_suspend(ehci_softc_t *sc)
591 DPRINTF("stopping the HC\n");
598 ehci_resume(ehci_softc_t *sc)
606 /* catch any lost interrupts */
607 ehci_do_poll(&sc->sc_bus);
612 ehci_dump_regs(ehci_softc_t *sc)
616 i = EOREAD4(sc, EHCI_USBCMD);
617 printf("cmd=0x%08x\n", i);
619 if (i & EHCI_CMD_ITC_1)
620 printf(" EHCI_CMD_ITC_1\n");
621 if (i & EHCI_CMD_ITC_2)
622 printf(" EHCI_CMD_ITC_2\n");
623 if (i & EHCI_CMD_ITC_4)
624 printf(" EHCI_CMD_ITC_4\n");
625 if (i & EHCI_CMD_ITC_8)
626 printf(" EHCI_CMD_ITC_8\n");
627 if (i & EHCI_CMD_ITC_16)
628 printf(" EHCI_CMD_ITC_16\n");
629 if (i & EHCI_CMD_ITC_32)
630 printf(" EHCI_CMD_ITC_32\n");
631 if (i & EHCI_CMD_ITC_64)
632 printf(" EHCI_CMD_ITC_64\n");
633 if (i & EHCI_CMD_ASPME)
634 printf(" EHCI_CMD_ASPME\n");
635 if (i & EHCI_CMD_ASPMC)
636 printf(" EHCI_CMD_ASPMC\n");
637 if (i & EHCI_CMD_LHCR)
638 printf(" EHCI_CMD_LHCR\n");
639 if (i & EHCI_CMD_IAAD)
640 printf(" EHCI_CMD_IAAD\n");
641 if (i & EHCI_CMD_ASE)
642 printf(" EHCI_CMD_ASE\n");
643 if (i & EHCI_CMD_PSE)
644 printf(" EHCI_CMD_PSE\n");
645 if (i & EHCI_CMD_FLS_M)
646 printf(" EHCI_CMD_FLS_M\n");
647 if (i & EHCI_CMD_HCRESET)
648 printf(" EHCI_CMD_HCRESET\n");
650 printf(" EHCI_CMD_RS\n");
652 i = EOREAD4(sc, EHCI_USBSTS);
654 printf("sts=0x%08x\n", i);
656 if (i & EHCI_STS_ASS)
657 printf(" EHCI_STS_ASS\n");
658 if (i & EHCI_STS_PSS)
659 printf(" EHCI_STS_PSS\n");
660 if (i & EHCI_STS_REC)
661 printf(" EHCI_STS_REC\n");
662 if (i & EHCI_STS_HCH)
663 printf(" EHCI_STS_HCH\n");
664 if (i & EHCI_STS_IAA)
665 printf(" EHCI_STS_IAA\n");
666 if (i & EHCI_STS_HSE)
667 printf(" EHCI_STS_HSE\n");
668 if (i & EHCI_STS_FLR)
669 printf(" EHCI_STS_FLR\n");
670 if (i & EHCI_STS_PCD)
671 printf(" EHCI_STS_PCD\n");
672 if (i & EHCI_STS_ERRINT)
673 printf(" EHCI_STS_ERRINT\n");
674 if (i & EHCI_STS_INT)
675 printf(" EHCI_STS_INT\n");
677 printf("ien=0x%08x\n",
678 EOREAD4(sc, EHCI_USBINTR));
679 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
680 EOREAD4(sc, EHCI_FRINDEX),
681 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
682 EOREAD4(sc, EHCI_PERIODICLISTBASE),
683 EOREAD4(sc, EHCI_ASYNCLISTADDR));
684 for (i = 1; i <= sc->sc_noport; i++) {
685 printf("port %d status=0x%08x\n", i,
686 EOREAD4(sc, EHCI_PORTSC(i)));
691 ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
693 link = hc32toh(sc, link);
694 printf("0x%08x", link);
695 if (link & EHCI_LINK_TERMINATE)
700 switch (EHCI_LINK_TYPE(link)) {
720 ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
725 ehci_dump_link(sc, qtd->qtd_next, 0);
727 ehci_dump_link(sc, qtd->qtd_altnext, 0);
729 s = hc32toh(sc, qtd->qtd_status);
730 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
731 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
732 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
733 printf(" cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
734 EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
735 (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
736 (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
737 (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
738 (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
739 (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
740 (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
741 (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
742 (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
744 for (s = 0; s < 5; s++) {
745 printf(" buffer[%d]=0x%08x\n", s,
746 hc32toh(sc, qtd->qtd_buffer[s]));
748 for (s = 0; s < 5; s++) {
749 printf(" buffer_hi[%d]=0x%08x\n", s,
750 hc32toh(sc, qtd->qtd_buffer_hi[s]));
755 ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
759 usb_pc_cpu_invalidate(sqtd->page_cache);
760 printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
761 ehci_dump_qtd(sc, sqtd);
762 temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
767 ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
773 for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
774 stop = ehci_dump_sqtd(sc, sqtd);
777 printf("dump aborted, too many TDs\n");
782 ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
787 usb_pc_cpu_invalidate(qh->page_cache);
788 printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
790 ehci_dump_link(sc, qh->qh_link, 1);
792 endp = hc32toh(sc, qh->qh_endp);
793 printf(" endp=0x%08x\n", endp);
794 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
795 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
796 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
797 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
798 printf(" mpl=0x%x ctl=%d nrl=%d\n",
799 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
800 EHCI_QH_GET_NRL(endp));
801 endphub = hc32toh(sc, qh->qh_endphub);
802 printf(" endphub=0x%08x\n", endphub);
803 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
804 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
805 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
806 EHCI_QH_GET_MULT(endphub));
808 ehci_dump_link(sc, qh->qh_curqtd, 0);
810 printf("Overlay qTD:\n");
811 ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
815 ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
817 usb_pc_cpu_invalidate(sitd->page_cache);
818 printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
819 printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
820 printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
821 hc32toh(sc, sitd->sitd_portaddr),
822 (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
824 EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
825 EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
826 EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
827 EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
828 printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
829 printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
830 (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
831 EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
832 printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
833 hc32toh(sc, sitd->sitd_back),
834 hc32toh(sc, sitd->sitd_bp[0]),
835 hc32toh(sc, sitd->sitd_bp[1]),
836 hc32toh(sc, sitd->sitd_bp_hi[0]),
837 hc32toh(sc, sitd->sitd_bp_hi[1]));
841 ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
843 usb_pc_cpu_invalidate(itd->page_cache);
844 printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
845 printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
846 printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
847 (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
848 printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
849 (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
850 printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
851 (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
852 printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
853 (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
854 printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
855 (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
856 printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
857 (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
858 printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
859 (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
860 printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
861 (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
862 printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
863 printf(" addr=0x%02x; endpt=0x%01x\n",
864 EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
865 EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
866 printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
867 printf(" dir=%s; mpl=0x%02x\n",
868 (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
869 EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
870 printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
871 hc32toh(sc, itd->itd_bp[2]),
872 hc32toh(sc, itd->itd_bp[3]),
873 hc32toh(sc, itd->itd_bp[4]),
874 hc32toh(sc, itd->itd_bp[5]),
875 hc32toh(sc, itd->itd_bp[6]));
876 printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
877 " 0x%08x,0x%08x,0x%08x\n",
878 hc32toh(sc, itd->itd_bp_hi[0]),
879 hc32toh(sc, itd->itd_bp_hi[1]),
880 hc32toh(sc, itd->itd_bp_hi[2]),
881 hc32toh(sc, itd->itd_bp_hi[3]),
882 hc32toh(sc, itd->itd_bp_hi[4]),
883 hc32toh(sc, itd->itd_bp_hi[5]),
884 hc32toh(sc, itd->itd_bp_hi[6]));
888 ehci_dump_isoc(ehci_softc_t *sc)
895 pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
896 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
898 printf("%s: isochronous dump from frame 0x%03x:\n",
901 itd = sc->sc_isoc_hs_p_last[pos];
902 sitd = sc->sc_isoc_fs_p_last[pos];
904 while (itd && max && max--) {
905 ehci_dump_itd(sc, itd);
909 while (sitd && max && max--) {
910 ehci_dump_sitd(sc, sitd);
918 ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
920 /* check for early completion */
921 if (ehci_check_transfer(xfer)) {
924 /* put transfer on interrupt queue */
925 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
927 /* start timeout, if any */
928 if (xfer->timeout != 0) {
929 usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
933 #define EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
935 _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
937 DPRINTFN(11, "%p to %p\n", std, last);
939 /* (sc->sc_bus.mtx) must be locked */
941 std->next = last->next;
942 std->sitd_next = last->sitd_next;
946 usb_pc_cpu_flush(std->page_cache);
949 * the last->next->prev is never followed: std->next->prev = std;
952 last->sitd_next = std->sitd_self;
954 usb_pc_cpu_flush(last->page_cache);
959 #define EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
961 _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
963 DPRINTFN(11, "%p to %p\n", std, last);
965 /* (sc->sc_bus.mtx) must be locked */
967 std->next = last->next;
968 std->itd_next = last->itd_next;
972 usb_pc_cpu_flush(std->page_cache);
975 * the last->next->prev is never followed: std->next->prev = std;
978 last->itd_next = std->itd_self;
980 usb_pc_cpu_flush(last->page_cache);
985 #define EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
987 _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
989 DPRINTFN(11, "%p to %p\n", sqh, last);
991 if (sqh->prev != NULL) {
992 /* should not happen */
993 DPRINTFN(0, "QH already linked!\n");
996 /* (sc->sc_bus.mtx) must be locked */
998 sqh->next = last->next;
999 sqh->qh_link = last->qh_link;
1003 usb_pc_cpu_flush(sqh->page_cache);
1006 * the last->next->prev is never followed: sqh->next->prev = sqh;
1010 last->qh_link = sqh->qh_self;
1012 usb_pc_cpu_flush(last->page_cache);
1017 #define EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
1018 static ehci_sitd_t *
1019 _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
1021 DPRINTFN(11, "%p from %p\n", std, last);
1023 /* (sc->sc_bus.mtx) must be locked */
1025 std->prev->next = std->next;
1026 std->prev->sitd_next = std->sitd_next;
1028 usb_pc_cpu_flush(std->prev->page_cache);
1031 std->next->prev = std->prev;
1032 usb_pc_cpu_flush(std->next->page_cache);
1034 return ((last == std) ? std->prev : last);
1037 #define EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
1039 _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1041 DPRINTFN(11, "%p from %p\n", std, last);
1043 /* (sc->sc_bus.mtx) must be locked */
1045 std->prev->next = std->next;
1046 std->prev->itd_next = std->itd_next;
1048 usb_pc_cpu_flush(std->prev->page_cache);
1051 std->next->prev = std->prev;
1052 usb_pc_cpu_flush(std->next->page_cache);
1054 return ((last == std) ? std->prev : last);
1057 #define EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
1059 _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1061 DPRINTFN(11, "%p from %p\n", sqh, last);
1063 /* (sc->sc_bus.mtx) must be locked */
1065 /* only remove if not removed from a queue */
1068 sqh->prev->next = sqh->next;
1069 sqh->prev->qh_link = sqh->qh_link;
1071 usb_pc_cpu_flush(sqh->prev->page_cache);
1074 sqh->next->prev = sqh->prev;
1075 usb_pc_cpu_flush(sqh->next->page_cache);
1077 last = ((last == sqh) ? sqh->prev : last);
1081 usb_pc_cpu_flush(sqh->page_cache);
1087 ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen)
1092 /* count number of full packets */
1093 dt = (actlen / xfer->max_packet_size) & 1;
1095 /* compute remainder */
1096 rem = actlen % xfer->max_packet_size;
1099 dt ^= 1; /* short packet at the end */
1100 else if (actlen != xlen)
1101 dt ^= 1; /* zero length packet at the end */
1103 dt ^= 1; /* zero length transfer */
1105 xfer->endpoint->toggle_next ^= dt;
1109 ehci_non_isoc_done_sub(struct usb_xfer *xfer)
1111 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1113 ehci_qtd_t *td_alt_next;
1117 td = xfer->td_transfer_cache;
1118 td_alt_next = td->alt_next;
1120 if (xfer->aframes != xfer->nframes) {
1121 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1125 usb_pc_cpu_invalidate(td->page_cache);
1126 status = hc32toh(sc, td->qtd_status);
1128 len = EHCI_QTD_GET_BYTES(status);
1131 * Verify the status length and
1132 * add the length to "frlengths[]":
1134 if (len > td->len) {
1135 /* should not happen */
1136 DPRINTF("Invalid status length, "
1137 "0x%04x/0x%04x bytes\n", len, td->len);
1138 status |= EHCI_QTD_HALTED;
1139 } else if (xfer->aframes != xfer->nframes) {
1140 xfer->frlengths[xfer->aframes] += td->len - len;
1141 /* manually update data toggle */
1142 ehci_data_toggle_update(xfer, td->len - len, td->len);
1145 /* Check for last transfer */
1146 if (((void *)td) == xfer->td_transfer_last) {
1150 /* Check for transfer error */
1151 if (status & EHCI_QTD_HALTED) {
1152 /* the transfer is finished */
1156 /* Check for short transfer */
1158 if (xfer->flags_int.short_frames_ok) {
1159 /* follow alt next */
1162 /* the transfer is finished */
1169 if (td->alt_next != td_alt_next) {
1170 /* this USB frame is complete */
1175 /* update transfer cache */
1177 xfer->td_transfer_cache = td;
1180 if (status & EHCI_QTD_STATERRS) {
1181 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
1182 "status=%s%s%s%s%s%s%s%s\n",
1183 xfer->address, xfer->endpointno, xfer->aframes,
1184 (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1185 (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
1186 (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
1187 (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
1188 (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
1189 (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
1190 (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
1191 (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
1195 return ((status & EHCI_QTD_HALTED) ?
1196 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1200 ehci_non_isoc_done(struct usb_xfer *xfer)
1202 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1205 usb_error_t err = 0;
1207 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1208 xfer, xfer->endpoint);
1211 if (ehcidebug > 10) {
1212 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1214 ehci_dump_sqtds(sc, xfer->td_transfer_first);
1218 /* extract data toggle directly from the QH's overlay area */
1220 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1222 usb_pc_cpu_invalidate(qh->page_cache);
1224 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1228 xfer->td_transfer_cache = xfer->td_transfer_first;
1230 if (xfer->flags_int.control_xfr) {
1232 if (xfer->flags_int.control_hdr) {
1234 err = ehci_non_isoc_done_sub(xfer);
1238 if (xfer->td_transfer_cache == NULL) {
1242 while (xfer->aframes != xfer->nframes) {
1244 err = ehci_non_isoc_done_sub(xfer);
1247 if (xfer->td_transfer_cache == NULL) {
1252 if (xfer->flags_int.control_xfr &&
1253 !xfer->flags_int.control_act) {
1255 err = ehci_non_isoc_done_sub(xfer);
1258 ehci_device_done(xfer, err);
1261 /*------------------------------------------------------------------------*
1262 * ehci_check_transfer
1265 * 0: USB transfer is not finished
1266 * Else: USB transfer is finished
1267 *------------------------------------------------------------------------*/
1269 ehci_check_transfer(struct usb_xfer *xfer)
1271 struct usb_pipe_methods *methods = xfer->endpoint->methods;
1272 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1276 DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
1278 if (methods == &ehci_device_isoc_fs_methods) {
1281 /* isochronous full speed transfer */
1283 td = xfer->td_transfer_last;
1284 usb_pc_cpu_invalidate(td->page_cache);
1285 status = hc32toh(sc, td->sitd_status);
1287 /* also check if first is complete */
1289 td = xfer->td_transfer_first;
1290 usb_pc_cpu_invalidate(td->page_cache);
1291 status |= hc32toh(sc, td->sitd_status);
1293 if (!(status & EHCI_SITD_ACTIVE)) {
1294 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1297 } else if (methods == &ehci_device_isoc_hs_methods) {
1300 /* isochronous high speed transfer */
1302 /* check last transfer */
1303 td = xfer->td_transfer_last;
1304 usb_pc_cpu_invalidate(td->page_cache);
1305 status = td->itd_status[0];
1306 status |= td->itd_status[1];
1307 status |= td->itd_status[2];
1308 status |= td->itd_status[3];
1309 status |= td->itd_status[4];
1310 status |= td->itd_status[5];
1311 status |= td->itd_status[6];
1312 status |= td->itd_status[7];
1314 /* also check first transfer */
1315 td = xfer->td_transfer_first;
1316 usb_pc_cpu_invalidate(td->page_cache);
1317 status |= td->itd_status[0];
1318 status |= td->itd_status[1];
1319 status |= td->itd_status[2];
1320 status |= td->itd_status[3];
1321 status |= td->itd_status[4];
1322 status |= td->itd_status[5];
1323 status |= td->itd_status[6];
1324 status |= td->itd_status[7];
1326 /* if no transactions are active we continue */
1327 if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
1328 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1335 /* non-isochronous transfer */
1338 * check whether there is an error somewhere in the middle,
1339 * or whether there was a short packet (SPD and not ACTIVE)
1341 td = xfer->td_transfer_cache;
1343 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1345 usb_pc_cpu_invalidate(qh->page_cache);
1347 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1348 if (status & EHCI_QTD_ACTIVE) {
1349 /* transfer is pending */
1354 usb_pc_cpu_invalidate(td->page_cache);
1355 status = hc32toh(sc, td->qtd_status);
1358 * Check if there is an active TD which
1359 * indicates that the transfer isn't done.
1361 if (status & EHCI_QTD_ACTIVE) {
1363 xfer->td_transfer_cache = td;
1367 * last transfer descriptor makes the transfer done
1369 if (((void *)td) == xfer->td_transfer_last) {
1373 * any kind of error makes the transfer done
1375 if (status & EHCI_QTD_HALTED) {
1379 * if there is no alternate next transfer, a short
1380 * packet also makes the transfer done
1382 if (EHCI_QTD_GET_BYTES(status)) {
1383 if (xfer->flags_int.short_frames_ok) {
1384 /* follow alt next */
1390 /* transfer is done */
1395 ehci_non_isoc_done(xfer);
1400 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1408 ehci_pcd_enable(ehci_softc_t *sc)
1410 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1412 sc->sc_eintrs |= EHCI_STS_PCD;
1413 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1415 /* acknowledge any PCD interrupt */
1416 EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
1422 ehci_interrupt_poll(ehci_softc_t *sc)
1424 struct usb_xfer *xfer;
1427 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1429 * check if transfer is transferred
1431 if (ehci_check_transfer(xfer)) {
1432 /* queue has been modified */
1439 * Some EHCI chips from VIA / ATI seem to trigger interrupts before
1440 * writing back the qTD status, or miss signalling occasionally under
1441 * heavy load. If the host machine is too fast, we can miss
1442 * transaction completion - when we scan the active list the
1443 * transaction still seems to be active. This generally exhibits
1444 * itself as a umass stall that never recovers.
1446 * We work around this behaviour by setting up this callback after any
1447 * softintr that completes with transactions still pending, giving us
1448 * another chance to check for completion after the writeback has
1452 ehci_poll_timeout(void *arg)
1454 ehci_softc_t *sc = arg;
1457 ehci_interrupt_poll(sc);
1460 /*------------------------------------------------------------------------*
1461 * ehci_interrupt - EHCI interrupt handler
1463 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1464 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1466 *------------------------------------------------------------------------*/
1468 ehci_interrupt(ehci_softc_t *sc)
1472 USB_BUS_LOCK(&sc->sc_bus);
1474 DPRINTFN(16, "real interrupt\n");
1477 if (ehcidebug > 15) {
1482 status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1484 /* the interrupt was not for us */
1487 if (!(status & sc->sc_eintrs)) {
1490 EOWRITE4(sc, EHCI_USBSTS, status); /* acknowledge */
1492 status &= sc->sc_eintrs;
1494 if (status & EHCI_STS_HSE) {
1495 printf("%s: unrecoverable error, "
1496 "controller halted\n", __FUNCTION__);
1502 if (status & EHCI_STS_PCD) {
1504 * Disable PCD interrupt for now, because it will be
1505 * on until the port has been reset.
1507 sc->sc_eintrs &= ~EHCI_STS_PCD;
1508 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1512 /* do not allow RHSC interrupts > 1 per second */
1513 usb_callout_reset(&sc->sc_tmo_pcd, hz,
1514 (void *)&ehci_pcd_enable, sc);
1516 status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
1519 /* block unprocessed interrupts */
1520 sc->sc_eintrs &= ~status;
1521 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1522 printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
1524 /* poll all the USB transfers */
1525 ehci_interrupt_poll(sc);
1527 if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
1528 usb_callout_reset(&sc->sc_tmo_poll, hz / 128,
1529 (void *)&ehci_poll_timeout, sc);
1533 USB_BUS_UNLOCK(&sc->sc_bus);
1537 * called when a request does not complete
1540 ehci_timeout(void *arg)
1542 struct usb_xfer *xfer = arg;
1544 DPRINTF("xfer=%p\n", xfer);
1546 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1548 /* transfer is transferred */
1549 ehci_device_done(xfer, USB_ERR_TIMEOUT);
1553 ehci_do_poll(struct usb_bus *bus)
1555 ehci_softc_t *sc = EHCI_BUS2SC(bus);
1557 USB_BUS_LOCK(&sc->sc_bus);
1558 ehci_interrupt_poll(sc);
1559 USB_BUS_UNLOCK(&sc->sc_bus);
1563 ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
1565 struct usb_page_search buf_res;
1567 ehci_qtd_t *td_next;
1568 ehci_qtd_t *td_alt_next;
1569 uint32_t buf_offset;
1573 uint32_t qtd_altnext;
1574 uint8_t shortpkt_old;
1577 terminate = temp->sc->sc_terminate_self;
1578 qtd_altnext = temp->sc->sc_terminate_self;
1581 shortpkt_old = temp->shortpkt;
1582 len_old = temp->len;
1588 td_next = temp->td_next;
1592 if (temp->len == 0) {
1594 if (temp->shortpkt) {
1597 /* send a Zero Length Packet, ZLP, last */
1604 average = temp->average;
1606 if (temp->len < average) {
1607 if (temp->len % temp->max_frame_size) {
1610 average = temp->len;
1614 if (td_next == NULL) {
1615 panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
1620 td_next = td->obj_next;
1622 /* check if we are pre-computing */
1626 /* update remaining length */
1628 temp->len -= average;
1632 /* fill out current TD */
1636 htohc32(temp->sc, EHCI_QTD_IOC |
1637 EHCI_QTD_SET_BYTES(average));
1641 if (temp->auto_data_toggle == 0) {
1643 /* update data toggle, ZLP case */
1646 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1650 td->qtd_buffer[0] = 0;
1651 td->qtd_buffer_hi[0] = 0;
1653 td->qtd_buffer[1] = 0;
1654 td->qtd_buffer_hi[1] = 0;
1660 if (temp->auto_data_toggle == 0) {
1662 /* update data toggle */
1664 if (((average + temp->max_frame_size - 1) /
1665 temp->max_frame_size) & 1) {
1667 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1672 /* update remaining length */
1674 temp->len -= average;
1676 /* fill out buffer pointers */
1678 usbd_get_page(temp->pc, buf_offset, &buf_res);
1680 htohc32(temp->sc, buf_res.physaddr);
1681 td->qtd_buffer_hi[0] = 0;
1685 while (average > EHCI_PAGE_SIZE) {
1686 average -= EHCI_PAGE_SIZE;
1687 buf_offset += EHCI_PAGE_SIZE;
1688 usbd_get_page(temp->pc, buf_offset, &buf_res);
1691 buf_res.physaddr & (~0xFFF));
1692 td->qtd_buffer_hi[x] = 0;
1697 * NOTE: The "average" variable is never zero after
1698 * exiting the loop above !
1700 * NOTE: We have to subtract one from the offset to
1701 * ensure that we are computing the physical address
1704 buf_offset += average;
1705 usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
1708 buf_res.physaddr & (~0xFFF));
1709 td->qtd_buffer_hi[x] = 0;
1713 /* link the current TD with the next one */
1714 td->qtd_next = td_next->qtd_self;
1716 td->qtd_altnext = qtd_altnext;
1717 td->alt_next = td_alt_next;
1719 usb_pc_cpu_flush(td->page_cache);
1725 /* setup alt next pointer, if any */
1726 if (temp->last_frame) {
1728 qtd_altnext = terminate;
1730 /* we use this field internally */
1731 td_alt_next = td_next;
1732 if (temp->setup_alt_next) {
1733 qtd_altnext = td_next->qtd_self;
1735 qtd_altnext = terminate;
1740 temp->shortpkt = shortpkt_old;
1741 temp->len = len_old;
1745 temp->td_next = td_next;
1749 ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
1751 struct ehci_std_temp temp;
1752 struct usb_pipe_methods *methods;
1756 uint32_t qh_endphub;
1759 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1760 xfer->address, UE_GET_ADDR(xfer->endpointno),
1761 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1763 temp.average = xfer->max_hc_frame_size;
1764 temp.max_frame_size = xfer->max_frame_size;
1765 temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
1767 /* toggle the DMA set we are using */
1768 xfer->flags_int.curr_dma_set ^= 1;
1770 /* get next DMA set */
1771 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1773 xfer->td_transfer_first = td;
1774 xfer->td_transfer_cache = td;
1778 temp.qtd_status = 0;
1779 temp.last_frame = 0;
1780 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1782 if (xfer->flags_int.control_xfr) {
1783 if (xfer->endpoint->toggle_next) {
1786 htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1788 temp.auto_data_toggle = 0;
1790 temp.auto_data_toggle = 1;
1793 if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
1794 (xfer->xroot->udev->address != 0)) {
1797 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1799 /* check if we should prepend a setup message */
1801 if (xfer->flags_int.control_xfr) {
1802 if (xfer->flags_int.control_hdr) {
1804 xfer->endpoint->toggle_next = 0;
1807 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1808 temp.qtd_status |= htohc32(temp.sc,
1810 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
1811 EHCI_QTD_SET_TOGGLE(0));
1813 temp.len = xfer->frlengths[0];
1814 temp.pc = xfer->frbuffers + 0;
1815 temp.shortpkt = temp.len ? 1 : 0;
1816 /* check for last frame */
1817 if (xfer->nframes == 1) {
1818 /* no STATUS stage yet, SETUP is last */
1819 if (xfer->flags_int.control_act) {
1820 temp.last_frame = 1;
1821 temp.setup_alt_next = 0;
1824 ehci_setup_standard_chain_sub(&temp);
1831 while (x != xfer->nframes) {
1833 /* DATA0 / DATA1 message */
1835 temp.len = xfer->frlengths[x];
1836 temp.pc = xfer->frbuffers + x;
1840 if (x == xfer->nframes) {
1841 if (xfer->flags_int.control_xfr) {
1842 /* no STATUS stage yet, DATA is last */
1843 if (xfer->flags_int.control_act) {
1844 temp.last_frame = 1;
1845 temp.setup_alt_next = 0;
1848 temp.last_frame = 1;
1849 temp.setup_alt_next = 0;
1852 /* keep previous data toggle and error count */
1855 htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1856 EHCI_QTD_SET_TOGGLE(1));
1858 if (temp.len == 0) {
1860 /* make sure that we send an USB packet */
1866 /* regular data transfer */
1868 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1871 /* set endpoint direction */
1874 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1875 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1876 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1877 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1878 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
1880 ehci_setup_standard_chain_sub(&temp);
1883 /* check if we should append a status stage */
1885 if (xfer->flags_int.control_xfr &&
1886 !xfer->flags_int.control_act) {
1889 * Send a DATA1 message and invert the current endpoint
1893 temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1894 EHCI_QTD_SET_TOGGLE(1));
1896 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1897 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1898 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
1899 EHCI_QTD_SET_TOGGLE(1)) :
1900 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1901 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
1902 EHCI_QTD_SET_TOGGLE(1));
1907 temp.last_frame = 1;
1908 temp.setup_alt_next = 0;
1910 ehci_setup_standard_chain_sub(&temp);
1914 /* the last TD terminates the transfer: */
1915 td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1916 td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1918 usb_pc_cpu_flush(td->page_cache);
1920 /* must have at least one frame! */
1922 xfer->td_transfer_last = td;
1925 if (ehcidebug > 8) {
1926 DPRINTF("nexttog=%d; data before transfer:\n",
1927 xfer->endpoint->toggle_next);
1928 ehci_dump_sqtds(temp.sc,
1929 xfer->td_transfer_first);
1933 methods = xfer->endpoint->methods;
1935 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1937 /* the "qh_link" field is filled when the QH is added */
1940 (EHCI_QH_SET_ADDR(xfer->address) |
1941 EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
1942 EHCI_QH_SET_MPL(xfer->max_packet_size));
1944 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
1945 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
1946 if (methods != &ehci_device_intr_methods)
1947 qh_endp |= EHCI_QH_SET_NRL(8);
1950 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
1951 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
1953 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
1956 if (methods == &ehci_device_ctrl_methods) {
1957 qh_endp |= EHCI_QH_CTL;
1959 if (methods != &ehci_device_intr_methods) {
1960 /* Only try one time per microframe! */
1961 qh_endp |= EHCI_QH_SET_NRL(1);
1965 if (temp.auto_data_toggle == 0) {
1966 /* software computes the data toggle */
1967 qh_endp |= EHCI_QH_DTC;
1970 qh->qh_endp = htohc32(temp.sc, qh_endp);
1973 (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
1974 EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
1975 EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
1976 EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
1977 EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
1979 qh->qh_endphub = htohc32(temp.sc, qh_endphub);
1982 /* fill the overlay qTD */
1984 if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
1986 qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1988 qh->qh_qtd.qtd_status = 0;
1991 td = xfer->td_transfer_first;
1993 qh->qh_qtd.qtd_next = td->qtd_self;
1994 qh->qh_qtd.qtd_altnext =
1995 htohc32(temp.sc, EHCI_LINK_TERMINATE);
1997 usb_pc_cpu_flush(qh->page_cache);
1999 if (xfer->xroot->udev->flags.self_suspended == 0) {
2000 EHCI_APPEND_QH(qh, *qh_last);
2005 ehci_root_intr(ehci_softc_t *sc)
2010 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2012 /* clear any old interrupt data */
2013 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2016 m = (sc->sc_noport + 1);
2017 if (m > (8 * sizeof(sc->sc_hub_idata))) {
2018 m = (8 * sizeof(sc->sc_hub_idata));
2020 for (i = 1; i < m; i++) {
2021 /* pick out CHANGE bits from the status register */
2022 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
2023 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2024 DPRINTF("port %d changed\n", i);
2027 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2028 sizeof(sc->sc_hub_idata));
2032 ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2034 uint32_t nframes = xfer->nframes;
2036 uint32_t *plen = xfer->frlengths;
2038 ehci_sitd_t *td = xfer->td_transfer_first;
2039 ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
2041 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2042 xfer, xfer->endpoint);
2046 panic("%s:%d: out of TD's\n",
2047 __FUNCTION__, __LINE__);
2049 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2050 pp_last = &sc->sc_isoc_fs_p_last[0];
2053 if (ehcidebug > 15) {
2054 DPRINTF("isoc FS-TD\n");
2055 ehci_dump_sitd(sc, td);
2058 usb_pc_cpu_invalidate(td->page_cache);
2059 status = hc32toh(sc, td->sitd_status);
2061 len = EHCI_SITD_GET_LEN(status);
2063 DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2073 /* remove FS-TD from schedule */
2074 EHCI_REMOVE_FS_TD(td, *pp_last);
2081 xfer->aframes = xfer->nframes;
2085 ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2087 uint32_t nframes = xfer->nframes;
2089 uint32_t *plen = xfer->frlengths;
2092 ehci_itd_t *td = xfer->td_transfer_first;
2093 ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
2095 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2096 xfer, xfer->endpoint);
2100 panic("%s:%d: out of TD's\n",
2101 __FUNCTION__, __LINE__);
2103 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2104 pp_last = &sc->sc_isoc_hs_p_last[0];
2107 if (ehcidebug > 15) {
2108 DPRINTF("isoc HS-TD\n");
2109 ehci_dump_itd(sc, td);
2113 usb_pc_cpu_invalidate(td->page_cache);
2114 status = hc32toh(sc, td->itd_status[td_no]);
2116 len = EHCI_ITD_GET_LEN(status);
2118 DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2120 if (xfer->endpoint->usb_smask & (1 << td_no)) {
2124 * The length is valid. NOTE: The
2125 * complete length is written back
2126 * into the status field, and not the
2127 * remainder like with other transfer
2131 /* Invalid length - truncate */
2142 if ((td_no == 8) || (nframes == 0)) {
2143 /* remove HS-TD from schedule */
2144 EHCI_REMOVE_HS_TD(td, *pp_last);
2151 xfer->aframes = xfer->nframes;
2154 /* NOTE: "done" can be run two times in a row,
2155 * from close and from interrupt
2158 ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
2160 struct usb_pipe_methods *methods = xfer->endpoint->methods;
2161 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2163 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2165 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2166 xfer, xfer->endpoint, error);
2168 if ((methods == &ehci_device_bulk_methods) ||
2169 (methods == &ehci_device_ctrl_methods)) {
2171 if (ehcidebug > 8) {
2172 DPRINTF("nexttog=%d; data after transfer:\n",
2173 xfer->endpoint->toggle_next);
2175 xfer->td_transfer_first);
2179 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2180 sc->sc_async_p_last);
2182 if (methods == &ehci_device_intr_methods) {
2183 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2184 sc->sc_intr_p_last[xfer->qh_pos]);
2187 * Only finish isochronous transfers once which will update
2188 * "xfer->frlengths".
2190 if (xfer->td_transfer_first &&
2191 xfer->td_transfer_last) {
2192 if (methods == &ehci_device_isoc_fs_methods) {
2193 ehci_isoc_fs_done(sc, xfer);
2195 if (methods == &ehci_device_isoc_hs_methods) {
2196 ehci_isoc_hs_done(sc, xfer);
2198 xfer->td_transfer_first = NULL;
2199 xfer->td_transfer_last = NULL;
2201 /* dequeue transfer and start next transfer */
2202 usbd_transfer_done(xfer, error);
2205 /*------------------------------------------------------------------------*
2207 *------------------------------------------------------------------------*/
2209 ehci_device_bulk_open(struct usb_xfer *xfer)
2215 ehci_device_bulk_close(struct usb_xfer *xfer)
2217 ehci_device_done(xfer, USB_ERR_CANCELLED);
2221 ehci_device_bulk_enter(struct usb_xfer *xfer)
2227 ehci_device_bulk_start(struct usb_xfer *xfer)
2229 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2232 /* setup TD's and QH */
2233 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2235 /* put transfer on interrupt queue */
2236 ehci_transfer_intr_enqueue(xfer);
2239 * XXX Certain nVidia chipsets choke when using the IAAD
2240 * feature too frequently.
2242 if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
2245 /* XXX Performance quirk: Some Host Controllers have a too low
2246 * interrupt rate. Issue an IAAD to stimulate the Host
2247 * Controller after queueing the BULK transfer.
2249 temp = EOREAD4(sc, EHCI_USBCMD);
2250 if (!(temp & EHCI_CMD_IAAD))
2251 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
2254 struct usb_pipe_methods ehci_device_bulk_methods =
2256 .open = ehci_device_bulk_open,
2257 .close = ehci_device_bulk_close,
2258 .enter = ehci_device_bulk_enter,
2259 .start = ehci_device_bulk_start,
2262 /*------------------------------------------------------------------------*
2263 * ehci control support
2264 *------------------------------------------------------------------------*/
2266 ehci_device_ctrl_open(struct usb_xfer *xfer)
2272 ehci_device_ctrl_close(struct usb_xfer *xfer)
2274 ehci_device_done(xfer, USB_ERR_CANCELLED);
2278 ehci_device_ctrl_enter(struct usb_xfer *xfer)
2284 ehci_device_ctrl_start(struct usb_xfer *xfer)
2286 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2288 /* setup TD's and QH */
2289 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2291 /* put transfer on interrupt queue */
2292 ehci_transfer_intr_enqueue(xfer);
2295 struct usb_pipe_methods ehci_device_ctrl_methods =
2297 .open = ehci_device_ctrl_open,
2298 .close = ehci_device_ctrl_close,
2299 .enter = ehci_device_ctrl_enter,
2300 .start = ehci_device_ctrl_start,
2303 /*------------------------------------------------------------------------*
2304 * ehci interrupt support
2305 *------------------------------------------------------------------------*/
2307 ehci_device_intr_open(struct usb_xfer *xfer)
2309 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2314 usb_hs_bandwidth_alloc(xfer);
2317 * Find the best QH position corresponding to the given interval:
2321 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
2323 if (xfer->interval >= bit) {
2327 if (sc->sc_intr_stat[x] <
2328 sc->sc_intr_stat[best]) {
2338 sc->sc_intr_stat[best]++;
2339 xfer->qh_pos = best;
2341 DPRINTFN(3, "best=%d interval=%d\n",
2342 best, xfer->interval);
2346 ehci_device_intr_close(struct usb_xfer *xfer)
2348 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2350 sc->sc_intr_stat[xfer->qh_pos]--;
2352 ehci_device_done(xfer, USB_ERR_CANCELLED);
2354 /* bandwidth must be freed after device done */
2355 usb_hs_bandwidth_free(xfer);
2359 ehci_device_intr_enter(struct usb_xfer *xfer)
2365 ehci_device_intr_start(struct usb_xfer *xfer)
2367 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2369 /* setup TD's and QH */
2370 ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
2372 /* put transfer on interrupt queue */
2373 ehci_transfer_intr_enqueue(xfer);
2376 struct usb_pipe_methods ehci_device_intr_methods =
2378 .open = ehci_device_intr_open,
2379 .close = ehci_device_intr_close,
2380 .enter = ehci_device_intr_enter,
2381 .start = ehci_device_intr_start,
2384 /*------------------------------------------------------------------------*
2385 * ehci full speed isochronous support
2386 *------------------------------------------------------------------------*/
2388 ehci_device_isoc_fs_open(struct usb_xfer *xfer)
2390 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2392 uint32_t sitd_portaddr;
2396 EHCI_SITD_SET_ADDR(xfer->address) |
2397 EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
2398 EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
2399 EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
2401 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
2402 sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
2404 sitd_portaddr = htohc32(sc, sitd_portaddr);
2406 /* initialize all TD's */
2408 for (ds = 0; ds != 2; ds++) {
2410 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2412 td->sitd_portaddr = sitd_portaddr;
2415 * TODO: make some kind of automatic
2416 * SMASK/CMASK selection based on micro-frame
2419 * micro-frame usage (8 microframes per 1ms)
2421 td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
2423 usb_pc_cpu_flush(td->page_cache);
2429 ehci_device_isoc_fs_close(struct usb_xfer *xfer)
2431 ehci_device_done(xfer, USB_ERR_CANCELLED);
2435 ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
2437 struct usb_page_search buf_res;
2438 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2440 ehci_sitd_t *td_last = NULL;
2441 ehci_sitd_t **pp_last;
2443 uint32_t buf_offset;
2456 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2457 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2459 /* get the current frame index */
2461 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2464 * check if the frame index is within the window where the frames
2467 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2468 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2470 if ((xfer->endpoint->is_synced == 0) ||
2471 (buf_offset < xfer->nframes)) {
2473 * If there is data underflow or the pipe queue is empty we
2474 * schedule the transfer a few frames ahead of the current
2475 * frame position. Else two isochronous transfers might
2478 xfer->endpoint->isoc_next = (nframes + 3) &
2479 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2480 xfer->endpoint->is_synced = 1;
2481 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2484 * compute how many milliseconds the insertion is ahead of the
2485 * current frame position:
2487 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2488 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2491 * pre-compute when the isochronous transfer will be finished:
2493 xfer->isoc_time_complete =
2494 usb_isoc_time_expand(&sc->sc_bus, nframes) +
2495 buf_offset + xfer->nframes;
2497 /* get the real number of frames */
2499 nframes = xfer->nframes;
2503 plen = xfer->frlengths;
2505 /* toggle the DMA set we are using */
2506 xfer->flags_int.curr_dma_set ^= 1;
2508 /* get next DMA set */
2509 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2510 xfer->td_transfer_first = td;
2512 pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
2514 /* store starting position */
2516 xfer->qh_pos = xfer->endpoint->isoc_next;
2520 panic("%s:%d: out of TD's\n",
2521 __FUNCTION__, __LINE__);
2523 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT])
2524 pp_last = &sc->sc_isoc_fs_p_last[0];
2526 /* reuse sitd_portaddr and sitd_back from last transfer */
2528 if (*plen > xfer->max_frame_size) {
2532 printf("%s: frame length(%d) exceeds %d "
2533 "bytes (frame truncated)\n",
2534 __FUNCTION__, *plen,
2535 xfer->max_frame_size);
2538 *plen = xfer->max_frame_size;
2541 /* allocate a slot */
2543 sa = usbd_fs_isoc_schedule_alloc_slot(xfer,
2544 xfer->isoc_time_complete - nframes - 1);
2548 * Schedule is FULL, set length to zero:
2552 sa = USB_FS_ISOC_UFRAME_MAX - 1;
2556 * only call "usbd_get_page()" when we have a
2559 usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2560 td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
2561 buf_offset += *plen;
2563 * NOTE: We need to subtract one from the offset so
2564 * that we are on a valid page!
2566 usbd_get_page(xfer->frbuffers, buf_offset - 1,
2568 temp = buf_res.physaddr & ~0xFFF;
2574 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
2577 temp |= 1; /* T-count = 1, TP = ALL */
2582 temp |= tlen; /* T-count = [1..6] */
2583 temp |= 8; /* TP = Begin */
2595 sa = (sb - sa) & 0x3F;
2598 sb = (-(4 << sa)) & 0xFE;
2599 sa = (1 << sa) & 0x3F;
2602 sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
2603 EHCI_SITD_SET_CMASK(sb));
2605 td->sitd_bp[1] = htohc32(sc, temp);
2607 td->sitd_mask = htohc32(sc, sitd_mask);
2610 td->sitd_status = htohc32(sc,
2613 EHCI_SITD_SET_LEN(*plen));
2615 td->sitd_status = htohc32(sc,
2617 EHCI_SITD_SET_LEN(*plen));
2619 usb_pc_cpu_flush(td->page_cache);
2622 if (ehcidebug > 15) {
2623 DPRINTF("FS-TD %d\n", nframes);
2624 ehci_dump_sitd(sc, td);
2627 /* insert TD into schedule */
2628 EHCI_APPEND_FS_TD(td, *pp_last);
2636 xfer->td_transfer_last = td_last;
2638 /* update isoc_next */
2639 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
2640 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2643 * We don't allow cancelling of the SPLIT transaction USB FULL
2644 * speed transfer, because it disturbs the bandwidth
2645 * computation algorithm.
2647 xfer->flags_int.can_cancel_immed = 0;
2651 ehci_device_isoc_fs_start(struct usb_xfer *xfer)
2654 * We don't allow cancelling of the SPLIT transaction USB FULL
2655 * speed transfer, because it disturbs the bandwidth
2656 * computation algorithm.
2658 xfer->flags_int.can_cancel_immed = 0;
2660 /* set a default timeout */
2661 if (xfer->timeout == 0)
2662 xfer->timeout = 500; /* ms */
2664 /* put transfer on interrupt queue */
2665 ehci_transfer_intr_enqueue(xfer);
2668 struct usb_pipe_methods ehci_device_isoc_fs_methods =
2670 .open = ehci_device_isoc_fs_open,
2671 .close = ehci_device_isoc_fs_close,
2672 .enter = ehci_device_isoc_fs_enter,
2673 .start = ehci_device_isoc_fs_start,
2676 /*------------------------------------------------------------------------*
2677 * ehci high speed isochronous support
2678 *------------------------------------------------------------------------*/
2680 ehci_device_isoc_hs_open(struct usb_xfer *xfer)
2682 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2687 usb_hs_bandwidth_alloc(xfer);
2689 /* initialize all TD's */
2691 for (ds = 0; ds != 2; ds++) {
2693 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2695 /* set TD inactive */
2696 td->itd_status[0] = 0;
2697 td->itd_status[1] = 0;
2698 td->itd_status[2] = 0;
2699 td->itd_status[3] = 0;
2700 td->itd_status[4] = 0;
2701 td->itd_status[5] = 0;
2702 td->itd_status[6] = 0;
2703 td->itd_status[7] = 0;
2705 /* set endpoint and address */
2706 td->itd_bp[0] = htohc32(sc,
2707 EHCI_ITD_SET_ADDR(xfer->address) |
2708 EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
2711 EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
2714 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2715 temp |= EHCI_ITD_SET_DIR_IN;
2717 /* set maximum packet size */
2718 td->itd_bp[1] = htohc32(sc, temp);
2720 /* set transfer multiplier */
2721 td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
2723 usb_pc_cpu_flush(td->page_cache);
2729 ehci_device_isoc_hs_close(struct usb_xfer *xfer)
2731 ehci_device_done(xfer, USB_ERR_CANCELLED);
2733 /* bandwidth must be freed after device done */
2734 usb_hs_bandwidth_free(xfer);
2738 ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
2740 struct usb_page_search buf_res;
2741 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2743 ehci_itd_t *td_last = NULL;
2744 ehci_itd_t **pp_last;
2745 bus_size_t page_addr;
2748 uint32_t buf_offset;
2750 uint32_t itd_offset[8 + 1];
2754 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
2761 DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n",
2762 xfer, xfer->endpoint->isoc_next, xfer->nframes, (int)shift);
2764 /* get the current frame index */
2766 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2769 * check if the frame index is within the window where the frames
2772 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2773 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2775 if ((xfer->endpoint->is_synced == 0) ||
2776 (buf_offset < (((xfer->nframes << shift) + 7) / 8))) {
2778 * If there is data underflow or the pipe queue is empty we
2779 * schedule the transfer a few frames ahead of the current
2780 * frame position. Else two isochronous transfers might
2783 xfer->endpoint->isoc_next = (nframes + 3) &
2784 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2785 xfer->endpoint->is_synced = 1;
2786 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2789 * compute how many milliseconds the insertion is ahead of the
2790 * current frame position:
2792 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2793 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2796 * pre-compute when the isochronous transfer will be finished:
2798 xfer->isoc_time_complete =
2799 usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2800 (((xfer->nframes << shift) + 7) / 8);
2802 /* get the real number of frames */
2804 nframes = xfer->nframes;
2809 plen = xfer->frlengths;
2811 /* toggle the DMA set we are using */
2812 xfer->flags_int.curr_dma_set ^= 1;
2814 /* get next DMA set */
2815 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2816 xfer->td_transfer_first = td;
2818 pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
2820 /* store starting position */
2822 xfer->qh_pos = xfer->endpoint->isoc_next;
2826 panic("%s:%d: out of TD's\n",
2827 __FUNCTION__, __LINE__);
2829 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2830 pp_last = &sc->sc_isoc_hs_p_last[0];
2833 if (*plen > xfer->max_frame_size) {
2837 printf("%s: frame length(%d) exceeds %d bytes "
2838 "(frame truncated)\n",
2839 __FUNCTION__, *plen, xfer->max_frame_size);
2842 *plen = xfer->max_frame_size;
2845 if (xfer->endpoint->usb_smask & (1 << td_no)) {
2846 status = (EHCI_ITD_SET_LEN(*plen) |
2848 EHCI_ITD_SET_PG(0));
2849 td->itd_status[td_no] = htohc32(sc, status);
2850 itd_offset[td_no] = buf_offset;
2851 buf_offset += *plen;
2855 td->itd_status[td_no] = 0; /* not active */
2856 itd_offset[td_no] = buf_offset;
2861 if ((td_no == 8) || (nframes == 0)) {
2863 /* the rest of the transfers are not active, if any */
2864 for (x = td_no; x != 8; x++) {
2865 td->itd_status[x] = 0; /* not active */
2868 /* check if there is any data to be transferred */
2869 if (itd_offset[0] != buf_offset) {
2871 itd_offset[td_no] = buf_offset;
2873 /* get first page offset */
2874 usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
2875 /* get page address */
2876 page_addr = buf_res.physaddr & ~0xFFF;
2877 /* update page address */
2878 td->itd_bp[0] &= htohc32(sc, 0xFFF);
2879 td->itd_bp[0] |= htohc32(sc, page_addr);
2881 for (x = 0; x != td_no; x++) {
2882 /* set page number and page offset */
2883 status = (EHCI_ITD_SET_PG(page_no) |
2884 (buf_res.physaddr & 0xFFF));
2885 td->itd_status[x] |= htohc32(sc, status);
2887 /* get next page offset */
2888 if (itd_offset[x + 1] == buf_offset) {
2890 * We subtract one so that
2891 * we don't go off the last
2894 usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
2896 usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
2899 /* check if we need a new page */
2900 if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
2901 /* new page needed */
2902 page_addr = buf_res.physaddr & ~0xFFF;
2904 panic("%s: too many pages\n", __FUNCTION__);
2907 /* update page address */
2908 td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2909 td->itd_bp[page_no] |= htohc32(sc, page_addr);
2913 /* set IOC bit if we are complete */
2915 td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
2917 usb_pc_cpu_flush(td->page_cache);
2919 if (ehcidebug > 15) {
2920 DPRINTF("HS-TD %d\n", nframes);
2921 ehci_dump_itd(sc, td);
2924 /* insert TD into schedule */
2925 EHCI_APPEND_HS_TD(td, *pp_last);
2934 xfer->td_transfer_last = td_last;
2936 /* update isoc_next */
2937 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
2938 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2942 ehci_device_isoc_hs_start(struct usb_xfer *xfer)
2944 /* put transfer on interrupt queue */
2945 ehci_transfer_intr_enqueue(xfer);
2948 struct usb_pipe_methods ehci_device_isoc_hs_methods =
2950 .open = ehci_device_isoc_hs_open,
2951 .close = ehci_device_isoc_hs_close,
2952 .enter = ehci_device_isoc_hs_enter,
2953 .start = ehci_device_isoc_hs_start,
2956 /*------------------------------------------------------------------------*
2957 * ehci root control support
2958 *------------------------------------------------------------------------*
2959 * Simulate a hardware hub by handling all the necessary requests.
2960 *------------------------------------------------------------------------*/
2963 struct usb_device_descriptor ehci_devd =
2965 sizeof(struct usb_device_descriptor),
2966 UDESC_DEVICE, /* type */
2967 {0x00, 0x02}, /* USB version */
2968 UDCLASS_HUB, /* class */
2969 UDSUBCLASS_HUB, /* subclass */
2970 UDPROTO_HSHUBSTT, /* protocol */
2971 64, /* max packet */
2972 {0}, {0}, {0x00, 0x01}, /* device id */
2973 1, 2, 0, /* string indicies */
2974 1 /* # of configurations */
2978 struct usb_device_qualifier ehci_odevd =
2980 sizeof(struct usb_device_qualifier),
2981 UDESC_DEVICE_QUALIFIER, /* type */
2982 {0x00, 0x02}, /* USB version */
2983 UDCLASS_HUB, /* class */
2984 UDSUBCLASS_HUB, /* subclass */
2985 UDPROTO_FSHUB, /* protocol */
2987 0, /* # of configurations */
2991 static const struct ehci_config_desc ehci_confd = {
2993 .bLength = sizeof(struct usb_config_descriptor),
2994 .bDescriptorType = UDESC_CONFIG,
2995 .wTotalLength[0] = sizeof(ehci_confd),
2997 .bConfigurationValue = 1,
2998 .iConfiguration = 0,
2999 .bmAttributes = UC_SELF_POWERED,
3000 .bMaxPower = 0 /* max power */
3003 .bLength = sizeof(struct usb_interface_descriptor),
3004 .bDescriptorType = UDESC_INTERFACE,
3006 .bInterfaceClass = UICLASS_HUB,
3007 .bInterfaceSubClass = UISUBCLASS_HUB,
3008 .bInterfaceProtocol = 0,
3011 .bLength = sizeof(struct usb_endpoint_descriptor),
3012 .bDescriptorType = UDESC_ENDPOINT,
3013 .bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
3014 .bmAttributes = UE_INTERRUPT,
3015 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
3021 struct usb_hub_descriptor ehci_hubd =
3023 .bDescLength = 0, /* dynamic length */
3024 .bDescriptorType = UDESC_HUB,
3028 ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
3033 DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
3035 port = EHCI_PORTSC(index);
3036 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3037 EOWRITE4(sc, port, v | EHCI_PS_PO);
3041 ehci_roothub_exec(struct usb_device *udev,
3042 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3044 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3045 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 = EREAD4(sc, EHCI_HCSPARAMS);
3258 sc->sc_hub_desc.hubd = ehci_hubd;
3259 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3261 if (EHCI_HCS_PPC(v))
3262 i = UHD_PWR_INDIVIDUAL;
3264 i = UHD_PWR_NO_SWITCH;
3266 if (EHCI_HCS_P_INDICATOR(v))
3269 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3270 /* XXX can't find out? */
3271 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
3272 /* XXX don't know if ports are removable or not */
3273 sc->sc_hub_desc.hubd.bDescLength =
3274 8 + ((sc->sc_noport + 7) / 8);
3275 len = sc->sc_hub_desc.hubd.bDescLength;
3277 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3279 memset(sc->sc_hub_desc.temp, 0, 16);
3281 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3282 DPRINTFN(9, "get port status i=%d\n",
3285 (index > sc->sc_noport)) {
3286 err = USB_ERR_IOERROR;
3289 v = EOREAD4(sc, EHCI_PORTSC(index));
3290 DPRINTFN(9, "port status=0x%04x\n", v);
3291 if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
3292 if ((v & 0xc000000) == 0x8000000)
3294 else if ((v & 0xc000000) == 0x4000000)
3302 i |= UPS_CURRENT_CONNECT_STATUS;
3304 i |= UPS_PORT_ENABLED;
3305 if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
3307 if (v & EHCI_PS_OCA)
3308 i |= UPS_OVERCURRENT_INDICATOR;
3312 i |= UPS_PORT_POWER;
3313 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3315 if (v & EHCI_PS_CSC)
3316 i |= UPS_C_CONNECT_STATUS;
3317 if (v & EHCI_PS_PEC)
3318 i |= UPS_C_PORT_ENABLED;
3319 if (v & EHCI_PS_OCC)
3320 i |= UPS_C_OVERCURRENT_INDICATOR;
3321 if (v & EHCI_PS_FPR)
3324 i |= UPS_C_PORT_RESET;
3325 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3326 len = sizeof(sc->sc_hub_desc.ps);
3328 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3329 err = USB_ERR_IOERROR;
3331 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3333 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3335 (index > sc->sc_noport)) {
3336 err = USB_ERR_IOERROR;
3339 port = EHCI_PORTSC(index);
3340 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3342 case UHF_PORT_ENABLE:
3343 EOWRITE4(sc, port, v | EHCI_PS_PE);
3345 case UHF_PORT_SUSPEND:
3346 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3348 case UHF_PORT_RESET:
3349 DPRINTFN(6, "reset port %d\n", index);
3351 if (ehcinohighspeed) {
3353 * Connect USB device to companion
3356 ehci_disown(sc, index, 1);
3360 if (EHCI_PS_IS_LOWSPEED(v) &&
3361 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3362 /* Low speed device, give up ownership. */
3363 ehci_disown(sc, index, 1);
3366 /* Start reset sequence. */
3367 v &= ~(EHCI_PS_PE | EHCI_PS_PR);
3368 EOWRITE4(sc, port, v | EHCI_PS_PR);
3370 /* Wait for reset to complete. */
3371 usb_pause_mtx(&sc->sc_bus.bus_mtx,
3372 USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
3374 /* Terminate reset sequence. */
3375 if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
3376 EOWRITE4(sc, port, v);
3378 /* Wait for HC to complete reset. */
3379 usb_pause_mtx(&sc->sc_bus.bus_mtx,
3380 USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
3382 v = EOREAD4(sc, port);
3383 DPRINTF("ehci after reset, status=0x%08x\n", v);
3384 if (v & EHCI_PS_PR) {
3385 device_printf(sc->sc_bus.bdev,
3386 "port reset timeout\n");
3387 err = USB_ERR_TIMEOUT;
3390 if (!(v & EHCI_PS_PE) &&
3391 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3392 /* Not a high speed device, give up ownership.*/
3393 ehci_disown(sc, index, 0);
3397 DPRINTF("ehci port %d reset, status = 0x%08x\n",
3401 case UHF_PORT_POWER:
3402 DPRINTFN(3, "set port power %d\n", index);
3403 EOWRITE4(sc, port, v | EHCI_PS_PP);
3407 DPRINTFN(3, "set port test %d\n", index);
3410 case UHF_PORT_INDICATOR:
3411 DPRINTFN(3, "set port ind %d\n", index);
3412 EOWRITE4(sc, port, v | EHCI_PS_PIC);
3416 err = USB_ERR_IOERROR;
3420 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3421 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3422 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3423 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3426 err = USB_ERR_IOERROR;
3436 ehci_xfer_setup(struct usb_setup_params *parm)
3438 struct usb_page_search page_info;
3439 struct usb_page_cache *pc;
3441 struct usb_xfer *xfer;
3449 sc = EHCI_BUS2SC(parm->udev->bus);
3450 xfer = parm->curr_xfer;
3458 * compute maximum number of some structures
3460 if (parm->methods == &ehci_device_ctrl_methods) {
3463 * The proof for the "nqtd" formula is illustrated like
3466 * +------------------------------------+
3470 * | | xxx | x | frm 0 |
3472 * | | xxx | xx | frm 1 |
3475 * +------------------------------------+
3477 * "xxx" means a completely full USB transfer descriptor
3479 * "x" and "xx" means a short USB packet
3481 * For the remainder of an USB transfer modulo
3482 * "max_data_length" we need two USB transfer descriptors.
3483 * One to transfer the remaining data and one to finalise
3484 * with a zero length packet in case the "force_short_xfer"
3485 * flag is set. We only need two USB transfer descriptors in
3486 * the case where the transfer length of the first one is a
3487 * factor of "max_frame_size". The rest of the needed USB
3488 * transfer descriptors is given by the buffer size divided
3489 * by the maximum data payload.
3491 parm->hc_max_packet_size = 0x400;
3492 parm->hc_max_packet_count = 1;
3493 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3494 xfer->flags_int.bdma_enable = 1;
3496 usbd_transfer_setup_sub(parm);
3499 nqtd = ((2 * xfer->nframes) + 1 /* STATUS */
3500 + (xfer->max_data_length / xfer->max_hc_frame_size));
3502 } else if (parm->methods == &ehci_device_bulk_methods) {
3504 parm->hc_max_packet_size = 0x400;
3505 parm->hc_max_packet_count = 1;
3506 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3507 xfer->flags_int.bdma_enable = 1;
3509 usbd_transfer_setup_sub(parm);
3512 nqtd = ((2 * xfer->nframes)
3513 + (xfer->max_data_length / xfer->max_hc_frame_size));
3515 } else if (parm->methods == &ehci_device_intr_methods) {
3517 if (parm->speed == USB_SPEED_HIGH) {
3518 parm->hc_max_packet_size = 0x400;
3519 parm->hc_max_packet_count = 3;
3520 } else if (parm->speed == USB_SPEED_FULL) {
3521 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
3522 parm->hc_max_packet_count = 1;
3524 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
3525 parm->hc_max_packet_count = 1;
3528 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3529 xfer->flags_int.bdma_enable = 1;
3531 usbd_transfer_setup_sub(parm);
3534 nqtd = ((2 * xfer->nframes)
3535 + (xfer->max_data_length / xfer->max_hc_frame_size));
3537 } else if (parm->methods == &ehci_device_isoc_fs_methods) {
3539 parm->hc_max_packet_size = 0x3FF;
3540 parm->hc_max_packet_count = 1;
3541 parm->hc_max_frame_size = 0x3FF;
3542 xfer->flags_int.bdma_enable = 1;
3544 usbd_transfer_setup_sub(parm);
3546 nsitd = xfer->nframes;
3548 } else if (parm->methods == &ehci_device_isoc_hs_methods) {
3550 parm->hc_max_packet_size = 0x400;
3551 parm->hc_max_packet_count = 3;
3552 parm->hc_max_frame_size = 0xC00;
3553 xfer->flags_int.bdma_enable = 1;
3555 usbd_transfer_setup_sub(parm);
3557 nitd = ((xfer->nframes + 7) / 8) <<
3558 usbd_xfer_get_fps_shift(xfer);
3562 parm->hc_max_packet_size = 0x400;
3563 parm->hc_max_packet_count = 1;
3564 parm->hc_max_frame_size = 0x400;
3566 usbd_transfer_setup_sub(parm);
3575 * Allocate queue heads and transfer descriptors
3579 if (usbd_transfer_setup_sub_malloc(
3580 parm, &pc, sizeof(ehci_itd_t),
3581 EHCI_ITD_ALIGN, nitd)) {
3582 parm->err = USB_ERR_NOMEM;
3586 for (n = 0; n != nitd; n++) {
3589 usbd_get_page(pc + n, 0, &page_info);
3591 td = page_info.buffer;
3594 td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
3595 td->obj_next = last_obj;
3596 td->page_cache = pc + n;
3600 usb_pc_cpu_flush(pc + n);
3603 if (usbd_transfer_setup_sub_malloc(
3604 parm, &pc, sizeof(ehci_sitd_t),
3605 EHCI_SITD_ALIGN, nsitd)) {
3606 parm->err = USB_ERR_NOMEM;
3610 for (n = 0; n != nsitd; n++) {
3613 usbd_get_page(pc + n, 0, &page_info);
3615 td = page_info.buffer;
3618 td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
3619 td->obj_next = last_obj;
3620 td->page_cache = pc + n;
3624 usb_pc_cpu_flush(pc + n);
3627 if (usbd_transfer_setup_sub_malloc(
3628 parm, &pc, sizeof(ehci_qtd_t),
3629 EHCI_QTD_ALIGN, nqtd)) {
3630 parm->err = USB_ERR_NOMEM;
3634 for (n = 0; n != nqtd; n++) {
3637 usbd_get_page(pc + n, 0, &page_info);
3639 qtd = page_info.buffer;
3642 qtd->qtd_self = htohc32(sc, page_info.physaddr);
3643 qtd->obj_next = last_obj;
3644 qtd->page_cache = pc + n;
3648 usb_pc_cpu_flush(pc + n);
3651 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3655 if (usbd_transfer_setup_sub_malloc(
3656 parm, &pc, sizeof(ehci_qh_t),
3657 EHCI_QH_ALIGN, nqh)) {
3658 parm->err = USB_ERR_NOMEM;
3662 for (n = 0; n != nqh; n++) {
3665 usbd_get_page(pc + n, 0, &page_info);
3667 qh = page_info.buffer;
3670 qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
3671 qh->obj_next = last_obj;
3672 qh->page_cache = pc + n;
3676 usb_pc_cpu_flush(pc + n);
3679 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3681 if (!xfer->flags_int.curr_dma_set) {
3682 xfer->flags_int.curr_dma_set = 1;
3688 ehci_xfer_unsetup(struct usb_xfer *xfer)
3694 ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3695 struct usb_endpoint *ep)
3697 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3699 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3701 edesc->bEndpointAddress, udev->flags.usb_mode,
3704 if (udev->flags.usb_mode != USB_MODE_HOST) {
3708 if (udev->device_index != sc->sc_addr) {
3710 if ((udev->speed != USB_SPEED_HIGH) &&
3711 ((udev->hs_hub_addr == 0) ||
3712 (udev->hs_port_no == 0) ||
3713 (udev->parent_hs_hub == NULL) ||
3714 (udev->parent_hs_hub->hub == NULL))) {
3715 /* We need a transaction translator */
3718 switch (edesc->bmAttributes & UE_XFERTYPE) {
3720 ep->methods = &ehci_device_ctrl_methods;
3723 ep->methods = &ehci_device_intr_methods;
3725 case UE_ISOCHRONOUS:
3726 if (udev->speed == USB_SPEED_HIGH) {
3727 ep->methods = &ehci_device_isoc_hs_methods;
3728 } else if (udev->speed == USB_SPEED_FULL) {
3729 ep->methods = &ehci_device_isoc_fs_methods;
3733 ep->methods = &ehci_device_bulk_methods;
3745 ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3748 * Wait until the hardware has finished any possible use of
3749 * the transfer descriptor(s) and QH
3751 *pus = (188); /* microseconds */
3755 ehci_device_resume(struct usb_device *udev)
3757 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3758 struct usb_xfer *xfer;
3759 struct usb_pipe_methods *methods;
3763 USB_BUS_LOCK(udev->bus);
3765 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3767 if (xfer->xroot->udev == udev) {
3769 methods = xfer->endpoint->methods;
3771 if ((methods == &ehci_device_bulk_methods) ||
3772 (methods == &ehci_device_ctrl_methods)) {
3773 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3774 sc->sc_async_p_last);
3776 if (methods == &ehci_device_intr_methods) {
3777 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3778 sc->sc_intr_p_last[xfer->qh_pos]);
3783 USB_BUS_UNLOCK(udev->bus);
3789 ehci_device_suspend(struct usb_device *udev)
3791 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3792 struct usb_xfer *xfer;
3793 struct usb_pipe_methods *methods;
3797 USB_BUS_LOCK(udev->bus);
3799 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3801 if (xfer->xroot->udev == udev) {
3803 methods = xfer->endpoint->methods;
3805 if ((methods == &ehci_device_bulk_methods) ||
3806 (methods == &ehci_device_ctrl_methods)) {
3807 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3808 sc->sc_async_p_last);
3810 if (methods == &ehci_device_intr_methods) {
3811 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3812 sc->sc_intr_p_last[xfer->qh_pos]);
3817 USB_BUS_UNLOCK(udev->bus);
3821 ehci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3823 struct ehci_softc *sc = EHCI_BUS2SC(bus);
3826 case USB_HW_POWER_SUSPEND:
3827 case USB_HW_POWER_SHUTDOWN:
3830 case USB_HW_POWER_RESUME:
3839 ehci_set_hw_power(struct usb_bus *bus)
3841 ehci_softc_t *sc = EHCI_BUS2SC(bus);
3849 flags = bus->hw_power_state;
3851 temp = EOREAD4(sc, EHCI_USBCMD);
3853 temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
3855 if (flags & (USB_HW_POWER_CONTROL |
3856 USB_HW_POWER_BULK)) {
3857 DPRINTF("Async is active\n");
3858 temp |= EHCI_CMD_ASE;
3860 if (flags & (USB_HW_POWER_INTERRUPT |
3861 USB_HW_POWER_ISOC)) {
3862 DPRINTF("Periodic is active\n");
3863 temp |= EHCI_CMD_PSE;
3865 EOWRITE4(sc, EHCI_USBCMD, temp);
3867 USB_BUS_UNLOCK(bus);
3872 struct usb_bus_methods ehci_bus_methods =
3874 .endpoint_init = ehci_ep_init,
3875 .xfer_setup = ehci_xfer_setup,
3876 .xfer_unsetup = ehci_xfer_unsetup,
3877 .get_dma_delay = ehci_get_dma_delay,
3878 .device_resume = ehci_device_resume,
3879 .device_suspend = ehci_device_suspend,
3880 .set_hw_power = ehci_set_hw_power,
3881 .set_hw_power_sleep = ehci_set_hw_power_sleep,
3882 .roothub_exec = ehci_roothub_exec,
3883 .xfer_poll = ehci_do_poll,