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;
95 static int ehciiaadbug = 0;
96 static int ehcilostintrbug = 0;
98 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
99 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
100 &ehcidebug, 0, "Debug level");
101 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
102 &ehcinohighspeed, 0, "Disable High Speed USB");
103 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RW,
104 &ehciiaadbug, 0, "Enable doorbell bug workaround");
105 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RW,
106 &ehcilostintrbug, 0, "Enable lost interrupt bug workaround");
108 TUNABLE_INT("hw.usb.ehci.debug", &ehcidebug);
109 TUNABLE_INT("hw.usb.ehci.no_hs", &ehcinohighspeed);
110 TUNABLE_INT("hw.usb.ehci.iaadbug", &ehciiaadbug);
111 TUNABLE_INT("hw.usb.ehci.lostintrbug", &ehcilostintrbug);
113 static void ehci_dump_regs(ehci_softc_t *sc);
114 static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
118 #define EHCI_INTR_ENDPT 1
120 extern struct usb_bus_methods ehci_bus_methods;
121 extern struct usb_pipe_methods ehci_device_bulk_methods;
122 extern struct usb_pipe_methods ehci_device_ctrl_methods;
123 extern struct usb_pipe_methods ehci_device_intr_methods;
124 extern struct usb_pipe_methods ehci_device_isoc_fs_methods;
125 extern struct usb_pipe_methods ehci_device_isoc_hs_methods;
127 static void ehci_do_poll(struct usb_bus *);
128 static void ehci_device_done(struct usb_xfer *, usb_error_t);
129 static uint8_t ehci_check_transfer(struct usb_xfer *);
130 static void ehci_timeout(void *);
131 static void ehci_poll_timeout(void *);
133 static void ehci_root_intr(ehci_softc_t *sc);
135 struct ehci_std_temp {
137 struct usb_page_cache *pc;
143 uint16_t max_frame_size;
145 uint8_t auto_data_toggle;
146 uint8_t setup_alt_next;
148 uint8_t can_use_next;
152 ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
154 ehci_softc_t *sc = EHCI_BUS2SC(bus);
157 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
158 sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
160 cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
161 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
163 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
164 cb(bus, sc->sc_hw.intr_start_pc + i,
165 sc->sc_hw.intr_start_pg + i,
166 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
169 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
170 cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
171 sc->sc_hw.isoc_hs_start_pg + i,
172 sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
175 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
176 cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
177 sc->sc_hw.isoc_fs_start_pg + i,
178 sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
183 ehci_reset(ehci_softc_t *sc)
188 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
189 for (i = 0; i < 100; i++) {
190 usb_pause_mtx(NULL, hz / 1000);
191 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
193 if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
195 * Force USBMODE as requested. Controllers
196 * may have multiple operating modes.
198 uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
199 if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
200 usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
201 device_printf(sc->sc_bus.bdev,
202 "set host controller mode\n");
204 if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
205 usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
206 device_printf(sc->sc_bus.bdev,
207 "set big-endian mode\n");
209 EOWRITE4(sc, EHCI_USBMODE, usbmode);
214 device_printf(sc->sc_bus.bdev, "reset timeout\n");
215 return (USB_ERR_IOERROR);
219 ehci_hcreset(ehci_softc_t *sc)
224 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
225 for (i = 0; i < 100; i++) {
226 usb_pause_mtx(NULL, hz / 1000);
227 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
233 * Fall through and try reset anyway even though
234 * Table 2-9 in the EHCI spec says this will result
235 * in undefined behavior.
237 device_printf(sc->sc_bus.bdev, "stop timeout\n");
239 return ehci_reset(sc);
243 ehci_init(ehci_softc_t *sc)
245 struct usb_page_search buf_res;
258 usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
259 usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_mtx, 0);
263 sc->sc_flags |= EHCI_SCFLG_IAADBUG;
265 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
271 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
273 version = EREAD2(sc, EHCI_HCIVERSION);
274 device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
275 version >> 8, version & 0xff);
277 sparams = EREAD4(sc, EHCI_HCSPARAMS);
278 DPRINTF("sparams=0x%x\n", sparams);
280 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
281 cparams = EREAD4(sc, EHCI_HCCPARAMS);
282 DPRINTF("cparams=0x%x\n", cparams);
284 if (EHCI_HCC_64BIT(cparams)) {
285 DPRINTF("HCC uses 64-bit structures\n");
287 /* MUST clear segment register if 64 bit capable */
288 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
290 sc->sc_bus.usbrev = USB_REV_2_0;
292 /* Reset the controller */
293 DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
295 err = ehci_hcreset(sc);
297 device_printf(sc->sc_bus.bdev, "reset timeout\n");
301 * use current frame-list-size selection 0: 1024*4 bytes 1: 512*4
302 * bytes 2: 256*4 bytes 3: unknown
304 if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
305 device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
306 return (USB_ERR_IOERROR);
308 /* set up the bus struct */
309 sc->sc_bus.methods = &ehci_bus_methods;
311 sc->sc_eintrs = EHCI_NORMAL_INTRS;
313 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
316 usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
320 /* initialize page cache pointer */
322 qh->page_cache = sc->sc_hw.intr_start_pc + i;
324 /* store a pointer to queue head */
326 sc->sc_intr_p_last[i] = qh;
329 htohc32(sc, buf_res.physaddr) |
330 htohc32(sc, EHCI_LINK_QH);
333 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
335 htohc32(sc, EHCI_QH_SET_MULT(1));
338 qh->qh_qtd.qtd_next =
339 htohc32(sc, EHCI_LINK_TERMINATE);
340 qh->qh_qtd.qtd_altnext =
341 htohc32(sc, EHCI_LINK_TERMINATE);
342 qh->qh_qtd.qtd_status =
343 htohc32(sc, EHCI_QTD_HALTED);
347 * the QHs are arranged to give poll intervals that are
348 * powers of 2 times 1ms
350 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
357 y = (x ^ bit) | (bit / 2);
359 qh_x = sc->sc_intr_p_last[x];
360 qh_y = sc->sc_intr_p_last[y];
363 * the next QH has half the poll interval
365 qh_x->qh_link = qh_y->qh_self;
375 qh = sc->sc_intr_p_last[0];
377 /* the last (1ms) QH terminates */
378 qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
380 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
384 usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
386 sitd = buf_res.buffer;
388 /* initialize page cache pointer */
390 sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
392 /* store a pointer to the transfer descriptor */
394 sc->sc_isoc_fs_p_last[i] = sitd;
396 /* initialize full speed isochronous */
399 htohc32(sc, buf_res.physaddr) |
400 htohc32(sc, EHCI_LINK_SITD);
403 htohc32(sc, EHCI_LINK_TERMINATE);
406 sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
409 usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
411 itd = buf_res.buffer;
413 /* initialize page cache pointer */
415 itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
417 /* store a pointer to the transfer descriptor */
419 sc->sc_isoc_hs_p_last[i] = itd;
421 /* initialize high speed isochronous */
424 htohc32(sc, buf_res.physaddr) |
425 htohc32(sc, EHCI_LINK_ITD);
431 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
436 pframes = buf_res.buffer;
440 * pframes -> high speed isochronous ->
441 * full speed isochronous -> interrupt QH's
443 for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
444 pframes[i] = sc->sc_isoc_hs_p_last
445 [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
448 /* setup sync list pointer */
449 EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
451 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
459 /* initialize page cache pointer */
461 qh->page_cache = &sc->sc_hw.async_start_pc;
463 /* store a pointer to the queue head */
465 sc->sc_async_p_last = qh;
467 /* init dummy QH that starts the async list */
470 htohc32(sc, buf_res.physaddr) |
471 htohc32(sc, EHCI_LINK_QH);
475 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
476 qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
477 qh->qh_link = qh->qh_self;
480 /* fill the overlay qTD */
481 qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
482 qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
483 qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
485 /* flush all cache into memory */
487 usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
491 ehci_dump_sqh(sc, sc->sc_async_p_last);
495 /* setup async list pointer */
496 EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
499 /* enable interrupts */
500 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
502 /* turn on controller */
503 EOWRITE4(sc, EHCI_USBCMD,
504 EHCI_CMD_ITC_1 | /* 1 microframes interrupt delay */
505 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
510 /* Take over port ownership */
511 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
513 for (i = 0; i < 100; i++) {
514 usb_pause_mtx(NULL, hz / 1000);
515 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
521 device_printf(sc->sc_bus.bdev, "run timeout\n");
522 return (USB_ERR_IOERROR);
526 /* catch any lost interrupts */
527 ehci_do_poll(&sc->sc_bus);
533 * shut down the controller when the system is going down
536 ehci_detach(ehci_softc_t *sc)
538 USB_BUS_LOCK(&sc->sc_bus);
540 usb_callout_stop(&sc->sc_tmo_pcd);
541 usb_callout_stop(&sc->sc_tmo_poll);
543 EOWRITE4(sc, EHCI_USBINTR, 0);
544 USB_BUS_UNLOCK(&sc->sc_bus);
546 if (ehci_hcreset(sc)) {
547 DPRINTF("reset failed!\n");
550 /* XXX let stray task complete */
551 usb_pause_mtx(NULL, hz / 20);
553 usb_callout_drain(&sc->sc_tmo_pcd);
554 usb_callout_drain(&sc->sc_tmo_poll);
558 ehci_suspend(ehci_softc_t *sc)
564 USB_BUS_LOCK(&sc->sc_bus);
566 for (i = 1; i <= sc->sc_noport; i++) {
567 cmd = EOREAD4(sc, EHCI_PORTSC(i));
568 if (((cmd & EHCI_PS_PO) == 0) &&
569 ((cmd & EHCI_PS_PE) == EHCI_PS_PE)) {
570 EOWRITE4(sc, EHCI_PORTSC(i),
575 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
577 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
578 EOWRITE4(sc, EHCI_USBCMD, cmd);
580 for (i = 0; i < 100; i++) {
581 hcr = EOREAD4(sc, EHCI_USBSTS) &
582 (EHCI_STS_ASS | EHCI_STS_PSS);
587 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
591 device_printf(sc->sc_bus.bdev, "reset timeout\n");
594 EOWRITE4(sc, EHCI_USBCMD, cmd);
596 for (i = 0; i < 100; i++) {
597 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
598 if (hcr == EHCI_STS_HCH) {
601 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
604 if (hcr != EHCI_STS_HCH) {
605 device_printf(sc->sc_bus.bdev,
608 USB_BUS_UNLOCK(&sc->sc_bus);
612 ehci_resume(ehci_softc_t *sc)
614 struct usb_page_search buf_res;
619 USB_BUS_LOCK(&sc->sc_bus);
621 /* restore things in case the bios doesn't */
622 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
624 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
625 EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
627 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
628 EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
630 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
633 for (i = 1; i <= sc->sc_noport; i++) {
634 cmd = EOREAD4(sc, EHCI_PORTSC(i));
635 if (((cmd & EHCI_PS_PO) == 0) &&
636 ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
637 EOWRITE4(sc, EHCI_PORTSC(i),
644 usb_pause_mtx(&sc->sc_bus.bus_mtx,
645 USB_MS_TO_TICKS(USB_RESUME_WAIT));
647 for (i = 1; i <= sc->sc_noport; i++) {
648 cmd = EOREAD4(sc, EHCI_PORTSC(i));
649 if (((cmd & EHCI_PS_PO) == 0) &&
650 ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
651 EOWRITE4(sc, EHCI_PORTSC(i),
656 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
658 for (i = 0; i < 100; i++) {
659 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
660 if (hcr != EHCI_STS_HCH) {
663 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
665 if (hcr == EHCI_STS_HCH) {
666 device_printf(sc->sc_bus.bdev, "config timeout\n");
669 USB_BUS_UNLOCK(&sc->sc_bus);
672 USB_MS_TO_TICKS(USB_RESUME_WAIT));
674 /* catch any lost interrupts */
675 ehci_do_poll(&sc->sc_bus);
679 ehci_shutdown(ehci_softc_t *sc)
681 DPRINTF("stopping the HC\n");
683 if (ehci_hcreset(sc)) {
684 DPRINTF("reset failed!\n");
690 ehci_dump_regs(ehci_softc_t *sc)
694 i = EOREAD4(sc, EHCI_USBCMD);
695 printf("cmd=0x%08x\n", i);
697 if (i & EHCI_CMD_ITC_1)
698 printf(" EHCI_CMD_ITC_1\n");
699 if (i & EHCI_CMD_ITC_2)
700 printf(" EHCI_CMD_ITC_2\n");
701 if (i & EHCI_CMD_ITC_4)
702 printf(" EHCI_CMD_ITC_4\n");
703 if (i & EHCI_CMD_ITC_8)
704 printf(" EHCI_CMD_ITC_8\n");
705 if (i & EHCI_CMD_ITC_16)
706 printf(" EHCI_CMD_ITC_16\n");
707 if (i & EHCI_CMD_ITC_32)
708 printf(" EHCI_CMD_ITC_32\n");
709 if (i & EHCI_CMD_ITC_64)
710 printf(" EHCI_CMD_ITC_64\n");
711 if (i & EHCI_CMD_ASPME)
712 printf(" EHCI_CMD_ASPME\n");
713 if (i & EHCI_CMD_ASPMC)
714 printf(" EHCI_CMD_ASPMC\n");
715 if (i & EHCI_CMD_LHCR)
716 printf(" EHCI_CMD_LHCR\n");
717 if (i & EHCI_CMD_IAAD)
718 printf(" EHCI_CMD_IAAD\n");
719 if (i & EHCI_CMD_ASE)
720 printf(" EHCI_CMD_ASE\n");
721 if (i & EHCI_CMD_PSE)
722 printf(" EHCI_CMD_PSE\n");
723 if (i & EHCI_CMD_FLS_M)
724 printf(" EHCI_CMD_FLS_M\n");
725 if (i & EHCI_CMD_HCRESET)
726 printf(" EHCI_CMD_HCRESET\n");
728 printf(" EHCI_CMD_RS\n");
730 i = EOREAD4(sc, EHCI_USBSTS);
732 printf("sts=0x%08x\n", i);
734 if (i & EHCI_STS_ASS)
735 printf(" EHCI_STS_ASS\n");
736 if (i & EHCI_STS_PSS)
737 printf(" EHCI_STS_PSS\n");
738 if (i & EHCI_STS_REC)
739 printf(" EHCI_STS_REC\n");
740 if (i & EHCI_STS_HCH)
741 printf(" EHCI_STS_HCH\n");
742 if (i & EHCI_STS_IAA)
743 printf(" EHCI_STS_IAA\n");
744 if (i & EHCI_STS_HSE)
745 printf(" EHCI_STS_HSE\n");
746 if (i & EHCI_STS_FLR)
747 printf(" EHCI_STS_FLR\n");
748 if (i & EHCI_STS_PCD)
749 printf(" EHCI_STS_PCD\n");
750 if (i & EHCI_STS_ERRINT)
751 printf(" EHCI_STS_ERRINT\n");
752 if (i & EHCI_STS_INT)
753 printf(" EHCI_STS_INT\n");
755 printf("ien=0x%08x\n",
756 EOREAD4(sc, EHCI_USBINTR));
757 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
758 EOREAD4(sc, EHCI_FRINDEX),
759 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
760 EOREAD4(sc, EHCI_PERIODICLISTBASE),
761 EOREAD4(sc, EHCI_ASYNCLISTADDR));
762 for (i = 1; i <= sc->sc_noport; i++) {
763 printf("port %d status=0x%08x\n", i,
764 EOREAD4(sc, EHCI_PORTSC(i)));
769 ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
771 link = hc32toh(sc, link);
772 printf("0x%08x", link);
773 if (link & EHCI_LINK_TERMINATE)
778 switch (EHCI_LINK_TYPE(link)) {
798 ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
803 ehci_dump_link(sc, qtd->qtd_next, 0);
805 ehci_dump_link(sc, qtd->qtd_altnext, 0);
807 s = hc32toh(sc, qtd->qtd_status);
808 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
809 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
810 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
811 printf(" cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
812 EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
813 (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
814 (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
815 (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
816 (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
817 (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
818 (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
819 (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
820 (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
822 for (s = 0; s < 5; s++) {
823 printf(" buffer[%d]=0x%08x\n", s,
824 hc32toh(sc, qtd->qtd_buffer[s]));
826 for (s = 0; s < 5; s++) {
827 printf(" buffer_hi[%d]=0x%08x\n", s,
828 hc32toh(sc, qtd->qtd_buffer_hi[s]));
833 ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
837 usb_pc_cpu_invalidate(sqtd->page_cache);
838 printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
839 ehci_dump_qtd(sc, sqtd);
840 temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
845 ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
851 for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
852 stop = ehci_dump_sqtd(sc, sqtd);
855 printf("dump aborted, too many TDs\n");
860 ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
865 usb_pc_cpu_invalidate(qh->page_cache);
866 printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
868 ehci_dump_link(sc, qh->qh_link, 1);
870 endp = hc32toh(sc, qh->qh_endp);
871 printf(" endp=0x%08x\n", endp);
872 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
873 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
874 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
875 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
876 printf(" mpl=0x%x ctl=%d nrl=%d\n",
877 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
878 EHCI_QH_GET_NRL(endp));
879 endphub = hc32toh(sc, qh->qh_endphub);
880 printf(" endphub=0x%08x\n", endphub);
881 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
882 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
883 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
884 EHCI_QH_GET_MULT(endphub));
886 ehci_dump_link(sc, qh->qh_curqtd, 0);
888 printf("Overlay qTD:\n");
889 ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
893 ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
895 usb_pc_cpu_invalidate(sitd->page_cache);
896 printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
897 printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
898 printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
899 hc32toh(sc, sitd->sitd_portaddr),
900 (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
902 EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
903 EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
904 EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
905 EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
906 printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
907 printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
908 (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
909 EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
910 printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
911 hc32toh(sc, sitd->sitd_back),
912 hc32toh(sc, sitd->sitd_bp[0]),
913 hc32toh(sc, sitd->sitd_bp[1]),
914 hc32toh(sc, sitd->sitd_bp_hi[0]),
915 hc32toh(sc, sitd->sitd_bp_hi[1]));
919 ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
921 usb_pc_cpu_invalidate(itd->page_cache);
922 printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
923 printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
924 printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
925 (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
926 printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
927 (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
928 printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
929 (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
930 printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
931 (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
932 printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
933 (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
934 printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
935 (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
936 printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
937 (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
938 printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
939 (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
940 printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
941 printf(" addr=0x%02x; endpt=0x%01x\n",
942 EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
943 EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
944 printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
945 printf(" dir=%s; mpl=0x%02x\n",
946 (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
947 EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
948 printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
949 hc32toh(sc, itd->itd_bp[2]),
950 hc32toh(sc, itd->itd_bp[3]),
951 hc32toh(sc, itd->itd_bp[4]),
952 hc32toh(sc, itd->itd_bp[5]),
953 hc32toh(sc, itd->itd_bp[6]));
954 printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
955 " 0x%08x,0x%08x,0x%08x\n",
956 hc32toh(sc, itd->itd_bp_hi[0]),
957 hc32toh(sc, itd->itd_bp_hi[1]),
958 hc32toh(sc, itd->itd_bp_hi[2]),
959 hc32toh(sc, itd->itd_bp_hi[3]),
960 hc32toh(sc, itd->itd_bp_hi[4]),
961 hc32toh(sc, itd->itd_bp_hi[5]),
962 hc32toh(sc, itd->itd_bp_hi[6]));
966 ehci_dump_isoc(ehci_softc_t *sc)
973 pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
974 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
976 printf("%s: isochronous dump from frame 0x%03x:\n",
979 itd = sc->sc_isoc_hs_p_last[pos];
980 sitd = sc->sc_isoc_fs_p_last[pos];
982 while (itd && max && max--) {
983 ehci_dump_itd(sc, itd);
987 while (sitd && max && max--) {
988 ehci_dump_sitd(sc, sitd);
996 ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
998 /* check for early completion */
999 if (ehci_check_transfer(xfer)) {
1002 /* put transfer on interrupt queue */
1003 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1005 /* start timeout, if any */
1006 if (xfer->timeout != 0) {
1007 usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
1011 #define EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
1012 static ehci_sitd_t *
1013 _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
1015 DPRINTFN(11, "%p to %p\n", std, last);
1017 /* (sc->sc_bus.mtx) must be locked */
1019 std->next = last->next;
1020 std->sitd_next = last->sitd_next;
1024 usb_pc_cpu_flush(std->page_cache);
1027 * the last->next->prev is never followed: std->next->prev = std;
1030 last->sitd_next = std->sitd_self;
1032 usb_pc_cpu_flush(last->page_cache);
1037 #define EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
1039 _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1041 DPRINTFN(11, "%p to %p\n", std, last);
1043 /* (sc->sc_bus.mtx) must be locked */
1045 std->next = last->next;
1046 std->itd_next = last->itd_next;
1050 usb_pc_cpu_flush(std->page_cache);
1053 * the last->next->prev is never followed: std->next->prev = std;
1056 last->itd_next = std->itd_self;
1058 usb_pc_cpu_flush(last->page_cache);
1063 #define EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
1065 _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1067 DPRINTFN(11, "%p to %p\n", sqh, last);
1069 if (sqh->prev != NULL) {
1070 /* should not happen */
1071 DPRINTFN(0, "QH already linked!\n");
1074 /* (sc->sc_bus.mtx) must be locked */
1076 sqh->next = last->next;
1077 sqh->qh_link = last->qh_link;
1081 usb_pc_cpu_flush(sqh->page_cache);
1084 * the last->next->prev is never followed: sqh->next->prev = sqh;
1088 last->qh_link = sqh->qh_self;
1090 usb_pc_cpu_flush(last->page_cache);
1095 #define EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
1096 static ehci_sitd_t *
1097 _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_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->sitd_next = std->sitd_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_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
1117 _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1119 DPRINTFN(11, "%p from %p\n", std, last);
1121 /* (sc->sc_bus.mtx) must be locked */
1123 std->prev->next = std->next;
1124 std->prev->itd_next = std->itd_next;
1126 usb_pc_cpu_flush(std->prev->page_cache);
1129 std->next->prev = std->prev;
1130 usb_pc_cpu_flush(std->next->page_cache);
1132 return ((last == std) ? std->prev : last);
1135 #define EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
1137 _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1139 DPRINTFN(11, "%p from %p\n", sqh, last);
1141 /* (sc->sc_bus.mtx) must be locked */
1143 /* only remove if not removed from a queue */
1146 sqh->prev->next = sqh->next;
1147 sqh->prev->qh_link = sqh->qh_link;
1149 usb_pc_cpu_flush(sqh->prev->page_cache);
1152 sqh->next->prev = sqh->prev;
1153 usb_pc_cpu_flush(sqh->next->page_cache);
1155 last = ((last == sqh) ? sqh->prev : last);
1159 usb_pc_cpu_flush(sqh->page_cache);
1165 ehci_non_isoc_done_sub(struct usb_xfer *xfer)
1167 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1169 ehci_qtd_t *td_alt_next;
1173 td = xfer->td_transfer_cache;
1174 td_alt_next = td->alt_next;
1176 if (xfer->aframes != xfer->nframes) {
1177 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1181 usb_pc_cpu_invalidate(td->page_cache);
1182 status = hc32toh(sc, td->qtd_status);
1184 len = EHCI_QTD_GET_BYTES(status);
1187 * Verify the status length and
1188 * add the length to "frlengths[]":
1190 if (len > td->len) {
1191 /* should not happen */
1192 DPRINTF("Invalid status length, "
1193 "0x%04x/0x%04x bytes\n", len, td->len);
1194 status |= EHCI_QTD_HALTED;
1195 } else if (xfer->aframes != xfer->nframes) {
1196 xfer->frlengths[xfer->aframes] += td->len - len;
1198 /* Check for last transfer */
1199 if (((void *)td) == xfer->td_transfer_last) {
1203 /* Check for transfer error */
1204 if (status & EHCI_QTD_HALTED) {
1205 /* the transfer is finished */
1209 /* Check for short transfer */
1211 if (xfer->flags_int.short_frames_ok) {
1212 /* follow alt next */
1215 /* the transfer is finished */
1222 if (td->alt_next != td_alt_next) {
1223 /* this USB frame is complete */
1228 /* update transfer cache */
1230 xfer->td_transfer_cache = td;
1233 if (status & EHCI_QTD_STATERRS) {
1234 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
1235 "status=%s%s%s%s%s%s%s%s\n",
1236 xfer->address, xfer->endpointno, xfer->aframes,
1237 (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1238 (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
1239 (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
1240 (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
1241 (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
1242 (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
1243 (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
1244 (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
1248 return ((status & EHCI_QTD_HALTED) ?
1249 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1253 ehci_non_isoc_done(struct usb_xfer *xfer)
1255 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1258 usb_error_t err = 0;
1260 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1261 xfer, xfer->endpoint);
1264 if (ehcidebug > 10) {
1265 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1267 ehci_dump_sqtds(sc, xfer->td_transfer_first);
1271 /* extract data toggle directly from the QH's overlay area */
1273 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1275 usb_pc_cpu_invalidate(qh->page_cache);
1277 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1279 xfer->endpoint->toggle_next =
1280 (status & EHCI_QTD_TOGGLE_MASK) ? 1 : 0;
1284 xfer->td_transfer_cache = xfer->td_transfer_first;
1286 if (xfer->flags_int.control_xfr) {
1288 if (xfer->flags_int.control_hdr) {
1290 err = ehci_non_isoc_done_sub(xfer);
1294 if (xfer->td_transfer_cache == NULL) {
1298 while (xfer->aframes != xfer->nframes) {
1300 err = ehci_non_isoc_done_sub(xfer);
1303 if (xfer->td_transfer_cache == NULL) {
1308 if (xfer->flags_int.control_xfr &&
1309 !xfer->flags_int.control_act) {
1311 err = ehci_non_isoc_done_sub(xfer);
1314 ehci_device_done(xfer, err);
1317 /*------------------------------------------------------------------------*
1318 * ehci_check_transfer
1321 * 0: USB transfer is not finished
1322 * Else: USB transfer is finished
1323 *------------------------------------------------------------------------*/
1325 ehci_check_transfer(struct usb_xfer *xfer)
1327 struct usb_pipe_methods *methods = xfer->endpoint->methods;
1328 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1332 DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
1334 if (methods == &ehci_device_isoc_fs_methods) {
1337 /* isochronous full speed transfer */
1339 td = xfer->td_transfer_last;
1340 usb_pc_cpu_invalidate(td->page_cache);
1341 status = hc32toh(sc, td->sitd_status);
1343 /* also check if first is complete */
1345 td = xfer->td_transfer_first;
1346 usb_pc_cpu_invalidate(td->page_cache);
1347 status |= hc32toh(sc, td->sitd_status);
1349 if (!(status & EHCI_SITD_ACTIVE)) {
1350 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1353 } else if (methods == &ehci_device_isoc_hs_methods) {
1355 uint8_t n = (xfer->nframes & 7);
1357 /* isochronous high speed transfer */
1359 /* check last transfer */
1360 td = xfer->td_transfer_last;
1361 usb_pc_cpu_invalidate(td->page_cache);
1363 status = td->itd_status[7];
1365 status = td->itd_status[n-1];
1367 /* also check first transfer */
1368 td = xfer->td_transfer_first;
1369 usb_pc_cpu_invalidate(td->page_cache);
1370 status |= td->itd_status[0];
1372 /* if no transactions are active we continue */
1373 if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
1374 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1381 /* non-isochronous transfer */
1384 * check whether there is an error somewhere in the middle,
1385 * or whether there was a short packet (SPD and not ACTIVE)
1387 td = xfer->td_transfer_cache;
1389 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1391 usb_pc_cpu_invalidate(qh->page_cache);
1393 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1394 if (status & EHCI_QTD_ACTIVE) {
1395 /* transfer is pending */
1400 usb_pc_cpu_invalidate(td->page_cache);
1401 status = hc32toh(sc, td->qtd_status);
1404 * Check if there is an active TD which
1405 * indicates that the transfer isn't done.
1407 if (status & EHCI_QTD_ACTIVE) {
1409 if (xfer->td_transfer_cache != td) {
1410 xfer->td_transfer_cache = td;
1411 if (qh->qh_qtd.qtd_next &
1412 htohc32(sc, EHCI_LINK_TERMINATE)) {
1413 /* XXX - manually advance to next frame */
1414 qh->qh_qtd.qtd_next = td->qtd_self;
1415 usb_pc_cpu_flush(td->page_cache);
1421 * last transfer descriptor makes the transfer done
1423 if (((void *)td) == xfer->td_transfer_last) {
1427 * any kind of error makes the transfer done
1429 if (status & EHCI_QTD_HALTED) {
1433 * if there is no alternate next transfer, a short
1434 * packet also makes the transfer done
1436 if (EHCI_QTD_GET_BYTES(status)) {
1437 if (xfer->flags_int.short_frames_ok) {
1438 /* follow alt next */
1444 /* transfer is done */
1449 ehci_non_isoc_done(xfer);
1454 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1462 ehci_pcd_enable(ehci_softc_t *sc)
1464 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1466 sc->sc_eintrs |= EHCI_STS_PCD;
1467 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1469 /* acknowledge any PCD interrupt */
1470 EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
1476 ehci_interrupt_poll(ehci_softc_t *sc)
1478 struct usb_xfer *xfer;
1481 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1483 * check if transfer is transferred
1485 if (ehci_check_transfer(xfer)) {
1486 /* queue has been modified */
1493 * Some EHCI chips from VIA / ATI seem to trigger interrupts before
1494 * writing back the qTD status, or miss signalling occasionally under
1495 * heavy load. If the host machine is too fast, we can miss
1496 * transaction completion - when we scan the active list the
1497 * transaction still seems to be active. This generally exhibits
1498 * itself as a umass stall that never recovers.
1500 * We work around this behaviour by setting up this callback after any
1501 * softintr that completes with transactions still pending, giving us
1502 * another chance to check for completion after the writeback has
1506 ehci_poll_timeout(void *arg)
1508 ehci_softc_t *sc = arg;
1511 ehci_interrupt_poll(sc);
1514 /*------------------------------------------------------------------------*
1515 * ehci_interrupt - EHCI interrupt handler
1517 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1518 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1520 *------------------------------------------------------------------------*/
1522 ehci_interrupt(ehci_softc_t *sc)
1526 USB_BUS_LOCK(&sc->sc_bus);
1528 DPRINTFN(16, "real interrupt\n");
1531 if (ehcidebug > 15) {
1536 status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1538 /* the interrupt was not for us */
1541 if (!(status & sc->sc_eintrs)) {
1544 EOWRITE4(sc, EHCI_USBSTS, status); /* acknowledge */
1546 status &= sc->sc_eintrs;
1548 if (status & EHCI_STS_HSE) {
1549 printf("%s: unrecoverable error, "
1550 "controller halted\n", __FUNCTION__);
1556 if (status & EHCI_STS_PCD) {
1558 * Disable PCD interrupt for now, because it will be
1559 * on until the port has been reset.
1561 sc->sc_eintrs &= ~EHCI_STS_PCD;
1562 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1566 /* do not allow RHSC interrupts > 1 per second */
1567 usb_callout_reset(&sc->sc_tmo_pcd, hz,
1568 (void *)&ehci_pcd_enable, sc);
1570 status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
1573 /* block unprocessed interrupts */
1574 sc->sc_eintrs &= ~status;
1575 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1576 printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
1578 /* poll all the USB transfers */
1579 ehci_interrupt_poll(sc);
1581 if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
1582 usb_callout_reset(&sc->sc_tmo_poll, hz / 128,
1583 (void *)&ehci_poll_timeout, sc);
1587 USB_BUS_UNLOCK(&sc->sc_bus);
1591 * called when a request does not complete
1594 ehci_timeout(void *arg)
1596 struct usb_xfer *xfer = arg;
1598 DPRINTF("xfer=%p\n", xfer);
1600 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1602 /* transfer is transferred */
1603 ehci_device_done(xfer, USB_ERR_TIMEOUT);
1607 ehci_do_poll(struct usb_bus *bus)
1609 ehci_softc_t *sc = EHCI_BUS2SC(bus);
1611 USB_BUS_LOCK(&sc->sc_bus);
1612 ehci_interrupt_poll(sc);
1613 USB_BUS_UNLOCK(&sc->sc_bus);
1617 ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
1619 struct usb_page_search buf_res;
1621 ehci_qtd_t *td_next;
1622 ehci_qtd_t *td_alt_next;
1623 uint32_t buf_offset;
1627 uint8_t shortpkt_old;
1630 terminate = htohc32(temp->sc, EHCI_LINK_TERMINATE);
1633 shortpkt_old = temp->shortpkt;
1634 len_old = temp->len;
1640 td_next = temp->td_next;
1644 if (temp->len == 0) {
1646 if (temp->shortpkt) {
1649 /* send a Zero Length Packet, ZLP, last */
1656 average = temp->average;
1658 if (temp->len < average) {
1659 if (temp->len % temp->max_frame_size) {
1662 average = temp->len;
1666 if (td_next == NULL) {
1667 panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
1672 td_next = td->obj_next;
1674 /* check if we are pre-computing */
1678 /* update remaining length */
1680 temp->len -= average;
1684 /* fill out current TD */
1688 htohc32(temp->sc, EHCI_QTD_IOC |
1689 EHCI_QTD_SET_BYTES(average));
1693 if (temp->auto_data_toggle == 0) {
1695 /* update data toggle, ZLP case */
1698 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1702 td->qtd_buffer[0] = 0;
1703 td->qtd_buffer_hi[0] = 0;
1705 td->qtd_buffer[1] = 0;
1706 td->qtd_buffer_hi[1] = 0;
1712 if (temp->auto_data_toggle == 0) {
1714 /* update data toggle */
1716 if (((average + temp->max_frame_size - 1) /
1717 temp->max_frame_size) & 1) {
1719 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1724 /* update remaining length */
1726 temp->len -= average;
1728 /* fill out buffer pointers */
1730 usbd_get_page(temp->pc, buf_offset, &buf_res);
1732 htohc32(temp->sc, buf_res.physaddr);
1733 td->qtd_buffer_hi[0] = 0;
1737 while (average > EHCI_PAGE_SIZE) {
1738 average -= EHCI_PAGE_SIZE;
1739 buf_offset += EHCI_PAGE_SIZE;
1740 usbd_get_page(temp->pc, buf_offset, &buf_res);
1743 buf_res.physaddr & (~0xFFF));
1744 td->qtd_buffer_hi[x] = 0;
1749 * NOTE: The "average" variable is never zero after
1750 * exiting the loop above !
1752 * NOTE: We have to subtract one from the offset to
1753 * ensure that we are computing the physical address
1756 buf_offset += average;
1757 usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
1760 buf_res.physaddr & (~0xFFF));
1761 td->qtd_buffer_hi[x] = 0;
1764 if (temp->can_use_next) {
1766 /* link the current TD with the next one */
1767 td->qtd_next = td_next->qtd_self;
1771 * BUG WARNING: The EHCI HW can use the
1772 * qtd_next field instead of qtd_altnext when
1773 * a short packet is received! We work this
1774 * around in software by not queueing more
1775 * than one job/TD at a time!
1777 td->qtd_next = terminate;
1780 td->qtd_altnext = terminate;
1781 td->alt_next = td_alt_next;
1783 usb_pc_cpu_flush(td->page_cache);
1789 /* setup alt next pointer, if any */
1790 if (temp->last_frame) {
1793 /* we use this field internally */
1794 td_alt_next = td_next;
1798 temp->shortpkt = shortpkt_old;
1799 temp->len = len_old;
1803 temp->td_next = td_next;
1807 ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
1809 struct ehci_std_temp temp;
1810 struct usb_pipe_methods *methods;
1814 uint32_t qh_endphub;
1817 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1818 xfer->address, UE_GET_ADDR(xfer->endpointno),
1819 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1821 temp.average = xfer->max_hc_frame_size;
1822 temp.max_frame_size = xfer->max_frame_size;
1823 temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
1825 /* toggle the DMA set we are using */
1826 xfer->flags_int.curr_dma_set ^= 1;
1828 /* get next DMA set */
1829 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1831 xfer->td_transfer_first = td;
1832 xfer->td_transfer_cache = td;
1836 temp.qtd_status = 0;
1837 temp.last_frame = 0;
1838 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1839 temp.can_use_next = (xfer->flags_int.control_xfr ||
1840 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT));
1842 if (xfer->flags_int.control_xfr) {
1843 if (xfer->endpoint->toggle_next) {
1846 htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1848 temp.auto_data_toggle = 0;
1850 temp.auto_data_toggle = 1;
1853 if (usbd_get_speed(xfer->xroot->udev) != USB_SPEED_HIGH) {
1856 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1858 /* check if we should prepend a setup message */
1860 if (xfer->flags_int.control_xfr) {
1861 if (xfer->flags_int.control_hdr) {
1864 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1865 temp.qtd_status |= htohc32(temp.sc,
1867 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
1868 EHCI_QTD_SET_TOGGLE(0));
1870 temp.len = xfer->frlengths[0];
1871 temp.pc = xfer->frbuffers + 0;
1872 temp.shortpkt = temp.len ? 1 : 0;
1873 /* check for last frame */
1874 if (xfer->nframes == 1) {
1875 /* no STATUS stage yet, SETUP is last */
1876 if (xfer->flags_int.control_act) {
1877 temp.last_frame = 1;
1878 temp.setup_alt_next = 0;
1881 ehci_setup_standard_chain_sub(&temp);
1888 while (x != xfer->nframes) {
1890 /* DATA0 / DATA1 message */
1892 temp.len = xfer->frlengths[x];
1893 temp.pc = xfer->frbuffers + x;
1897 if (x == xfer->nframes) {
1898 if (xfer->flags_int.control_xfr) {
1899 /* no STATUS stage yet, DATA is last */
1900 if (xfer->flags_int.control_act) {
1901 temp.last_frame = 1;
1902 temp.setup_alt_next = 0;
1905 temp.last_frame = 1;
1906 temp.setup_alt_next = 0;
1909 /* keep previous data toggle and error count */
1912 htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1913 EHCI_QTD_SET_TOGGLE(1));
1915 if (temp.len == 0) {
1917 /* make sure that we send an USB packet */
1923 /* regular data transfer */
1925 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1928 /* set endpoint direction */
1931 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1932 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1933 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1934 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1935 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
1937 ehci_setup_standard_chain_sub(&temp);
1940 /* check if we should append a status stage */
1942 if (xfer->flags_int.control_xfr &&
1943 !xfer->flags_int.control_act) {
1946 * Send a DATA1 message and invert the current endpoint
1950 temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1951 EHCI_QTD_SET_TOGGLE(1));
1953 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1954 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1955 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
1956 EHCI_QTD_SET_TOGGLE(1)) :
1957 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1958 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
1959 EHCI_QTD_SET_TOGGLE(1));
1964 temp.last_frame = 1;
1965 temp.setup_alt_next = 0;
1967 ehci_setup_standard_chain_sub(&temp);
1971 /* the last TD terminates the transfer: */
1972 td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1973 td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1975 usb_pc_cpu_flush(td->page_cache);
1977 /* must have at least one frame! */
1979 xfer->td_transfer_last = td;
1982 if (ehcidebug > 8) {
1983 DPRINTF("nexttog=%d; data before transfer:\n",
1984 xfer->endpoint->toggle_next);
1985 ehci_dump_sqtds(temp.sc,
1986 xfer->td_transfer_first);
1990 methods = xfer->endpoint->methods;
1992 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1994 /* the "qh_link" field is filled when the QH is added */
1997 (EHCI_QH_SET_ADDR(xfer->address) |
1998 EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
1999 EHCI_QH_SET_MPL(xfer->max_packet_size));
2001 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
2002 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
2003 if (methods != &ehci_device_intr_methods)
2004 qh_endp |= EHCI_QH_SET_NRL(8);
2007 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
2008 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
2010 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
2013 if (methods == &ehci_device_ctrl_methods) {
2014 qh_endp |= EHCI_QH_CTL;
2016 if (methods != &ehci_device_intr_methods) {
2017 /* Only try one time per microframe! */
2018 qh_endp |= EHCI_QH_SET_NRL(1);
2022 if (temp.auto_data_toggle == 0) {
2023 /* software computes the data toggle */
2024 qh_endp |= EHCI_QH_DTC;
2027 qh->qh_endp = htohc32(temp.sc, qh_endp);
2030 (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
2031 EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
2032 EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
2033 EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
2034 EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
2036 qh->qh_endphub = htohc32(temp.sc, qh_endphub);
2039 /* fill the overlay qTD */
2041 if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
2043 qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
2045 qh->qh_qtd.qtd_status = 0;
2048 td = xfer->td_transfer_first;
2050 qh->qh_qtd.qtd_next = td->qtd_self;
2051 qh->qh_qtd.qtd_altnext =
2052 htohc32(temp.sc, EHCI_LINK_TERMINATE);
2054 usb_pc_cpu_flush(qh->page_cache);
2056 if (xfer->xroot->udev->flags.self_suspended == 0) {
2057 EHCI_APPEND_QH(qh, *qh_last);
2062 ehci_root_intr(ehci_softc_t *sc)
2067 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2069 /* clear any old interrupt data */
2070 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2073 m = (sc->sc_noport + 1);
2074 if (m > (8 * sizeof(sc->sc_hub_idata))) {
2075 m = (8 * sizeof(sc->sc_hub_idata));
2077 for (i = 1; i < m; i++) {
2078 /* pick out CHANGE bits from the status register */
2079 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
2080 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2081 DPRINTF("port %d changed\n", i);
2084 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2085 sizeof(sc->sc_hub_idata));
2089 ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2091 uint32_t nframes = xfer->nframes;
2093 uint32_t *plen = xfer->frlengths;
2095 ehci_sitd_t *td = xfer->td_transfer_first;
2096 ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
2098 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2099 xfer, xfer->endpoint);
2103 panic("%s:%d: out of TD's\n",
2104 __FUNCTION__, __LINE__);
2106 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2107 pp_last = &sc->sc_isoc_fs_p_last[0];
2110 if (ehcidebug > 15) {
2111 DPRINTF("isoc FS-TD\n");
2112 ehci_dump_sitd(sc, td);
2115 usb_pc_cpu_invalidate(td->page_cache);
2116 status = hc32toh(sc, td->sitd_status);
2118 len = EHCI_SITD_GET_LEN(status);
2120 DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2130 /* remove FS-TD from schedule */
2131 EHCI_REMOVE_FS_TD(td, *pp_last);
2138 xfer->aframes = xfer->nframes;
2142 ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2144 uint32_t nframes = xfer->nframes;
2146 uint32_t *plen = xfer->frlengths;
2149 ehci_itd_t *td = xfer->td_transfer_first;
2150 ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
2152 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2153 xfer, xfer->endpoint);
2157 panic("%s:%d: out of TD's\n",
2158 __FUNCTION__, __LINE__);
2160 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2161 pp_last = &sc->sc_isoc_hs_p_last[0];
2164 if (ehcidebug > 15) {
2165 DPRINTF("isoc HS-TD\n");
2166 ehci_dump_itd(sc, td);
2170 usb_pc_cpu_invalidate(td->page_cache);
2171 status = hc32toh(sc, td->itd_status[td_no]);
2173 len = EHCI_ITD_GET_LEN(status);
2175 DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2177 if (xfer->endpoint->usb_smask & (1 << td_no)) {
2181 * The length is valid. NOTE: The
2182 * complete length is written back
2183 * into the status field, and not the
2184 * remainder like with other transfer
2188 /* Invalid length - truncate */
2199 if ((td_no == 8) || (nframes == 0)) {
2200 /* remove HS-TD from schedule */
2201 EHCI_REMOVE_HS_TD(td, *pp_last);
2208 xfer->aframes = xfer->nframes;
2211 /* NOTE: "done" can be run two times in a row,
2212 * from close and from interrupt
2215 ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
2217 struct usb_pipe_methods *methods = xfer->endpoint->methods;
2218 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2220 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2222 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2223 xfer, xfer->endpoint, error);
2225 if ((methods == &ehci_device_bulk_methods) ||
2226 (methods == &ehci_device_ctrl_methods)) {
2228 if (ehcidebug > 8) {
2229 DPRINTF("nexttog=%d; data after transfer:\n",
2230 xfer->endpoint->toggle_next);
2232 xfer->td_transfer_first);
2236 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2237 sc->sc_async_p_last);
2239 if (methods == &ehci_device_intr_methods) {
2240 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2241 sc->sc_intr_p_last[xfer->qh_pos]);
2244 * Only finish isochronous transfers once which will update
2245 * "xfer->frlengths".
2247 if (xfer->td_transfer_first &&
2248 xfer->td_transfer_last) {
2249 if (methods == &ehci_device_isoc_fs_methods) {
2250 ehci_isoc_fs_done(sc, xfer);
2252 if (methods == &ehci_device_isoc_hs_methods) {
2253 ehci_isoc_hs_done(sc, xfer);
2255 xfer->td_transfer_first = NULL;
2256 xfer->td_transfer_last = NULL;
2258 /* dequeue transfer and start next transfer */
2259 usbd_transfer_done(xfer, error);
2262 /*------------------------------------------------------------------------*
2264 *------------------------------------------------------------------------*/
2266 ehci_device_bulk_open(struct usb_xfer *xfer)
2272 ehci_device_bulk_close(struct usb_xfer *xfer)
2274 ehci_device_done(xfer, USB_ERR_CANCELLED);
2278 ehci_device_bulk_enter(struct usb_xfer *xfer)
2284 ehci_device_bulk_start(struct usb_xfer *xfer)
2286 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 * XXX Certain nVidia chipsets choke when using the IAAD
2297 * feature too frequently.
2299 if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
2302 /* XXX Performance quirk: Some Host Controllers have a too low
2303 * interrupt rate. Issue an IAAD to stimulate the Host
2304 * Controller after queueing the BULK transfer.
2306 temp = EOREAD4(sc, EHCI_USBCMD);
2307 if (!(temp & EHCI_CMD_IAAD))
2308 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
2311 struct usb_pipe_methods ehci_device_bulk_methods =
2313 .open = ehci_device_bulk_open,
2314 .close = ehci_device_bulk_close,
2315 .enter = ehci_device_bulk_enter,
2316 .start = ehci_device_bulk_start,
2319 /*------------------------------------------------------------------------*
2320 * ehci control support
2321 *------------------------------------------------------------------------*/
2323 ehci_device_ctrl_open(struct usb_xfer *xfer)
2329 ehci_device_ctrl_close(struct usb_xfer *xfer)
2331 ehci_device_done(xfer, USB_ERR_CANCELLED);
2335 ehci_device_ctrl_enter(struct usb_xfer *xfer)
2341 ehci_device_ctrl_start(struct usb_xfer *xfer)
2343 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2345 /* setup TD's and QH */
2346 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2348 /* put transfer on interrupt queue */
2349 ehci_transfer_intr_enqueue(xfer);
2352 struct usb_pipe_methods ehci_device_ctrl_methods =
2354 .open = ehci_device_ctrl_open,
2355 .close = ehci_device_ctrl_close,
2356 .enter = ehci_device_ctrl_enter,
2357 .start = ehci_device_ctrl_start,
2360 /*------------------------------------------------------------------------*
2361 * ehci interrupt support
2362 *------------------------------------------------------------------------*/
2364 ehci_device_intr_open(struct usb_xfer *xfer)
2366 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2371 usb_hs_bandwidth_alloc(xfer);
2374 * Find the best QH position corresponding to the given interval:
2378 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
2380 if (xfer->interval >= bit) {
2384 if (sc->sc_intr_stat[x] <
2385 sc->sc_intr_stat[best]) {
2395 sc->sc_intr_stat[best]++;
2396 xfer->qh_pos = best;
2398 DPRINTFN(3, "best=%d interval=%d\n",
2399 best, xfer->interval);
2403 ehci_device_intr_close(struct usb_xfer *xfer)
2405 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2407 sc->sc_intr_stat[xfer->qh_pos]--;
2409 ehci_device_done(xfer, USB_ERR_CANCELLED);
2411 /* bandwidth must be freed after device done */
2412 usb_hs_bandwidth_free(xfer);
2416 ehci_device_intr_enter(struct usb_xfer *xfer)
2422 ehci_device_intr_start(struct usb_xfer *xfer)
2424 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2426 /* setup TD's and QH */
2427 ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
2429 /* put transfer on interrupt queue */
2430 ehci_transfer_intr_enqueue(xfer);
2433 struct usb_pipe_methods ehci_device_intr_methods =
2435 .open = ehci_device_intr_open,
2436 .close = ehci_device_intr_close,
2437 .enter = ehci_device_intr_enter,
2438 .start = ehci_device_intr_start,
2441 /*------------------------------------------------------------------------*
2442 * ehci full speed isochronous support
2443 *------------------------------------------------------------------------*/
2445 ehci_device_isoc_fs_open(struct usb_xfer *xfer)
2447 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2449 uint32_t sitd_portaddr;
2453 EHCI_SITD_SET_ADDR(xfer->address) |
2454 EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
2455 EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
2456 EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
2458 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2459 sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
2461 sitd_portaddr = htohc32(sc, sitd_portaddr);
2463 /* initialize all TD's */
2465 for (ds = 0; ds != 2; ds++) {
2467 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2469 td->sitd_portaddr = sitd_portaddr;
2472 * TODO: make some kind of automatic
2473 * SMASK/CMASK selection based on micro-frame
2476 * micro-frame usage (8 microframes per 1ms)
2478 td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
2480 usb_pc_cpu_flush(td->page_cache);
2486 ehci_device_isoc_fs_close(struct usb_xfer *xfer)
2488 ehci_device_done(xfer, USB_ERR_CANCELLED);
2492 ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
2494 struct usb_page_search buf_res;
2495 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2496 struct usb_fs_isoc_schedule *fss_start;
2497 struct usb_fs_isoc_schedule *fss_end;
2498 struct usb_fs_isoc_schedule *fss;
2500 ehci_sitd_t *td_last = NULL;
2501 ehci_sitd_t **pp_last;
2503 uint32_t buf_offset;
2517 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2518 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2520 /* get the current frame index */
2522 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2525 * check if the frame index is within the window where the frames
2528 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2529 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2531 if ((xfer->endpoint->is_synced == 0) ||
2532 (buf_offset < xfer->nframes)) {
2534 * If there is data underflow or the pipe queue is empty we
2535 * schedule the transfer a few frames ahead of the current
2536 * frame position. Else two isochronous transfers might
2539 xfer->endpoint->isoc_next = (nframes + 3) &
2540 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2541 xfer->endpoint->is_synced = 1;
2542 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2545 * compute how many milliseconds the insertion is ahead of the
2546 * current frame position:
2548 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2549 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2552 * pre-compute when the isochronous transfer will be finished:
2554 xfer->isoc_time_complete =
2555 usbd_fs_isoc_schedule_isoc_time_expand
2556 (xfer->xroot->udev, &fss_start, &fss_end, nframes) + buf_offset +
2559 /* get the real number of frames */
2561 nframes = xfer->nframes;
2565 plen = xfer->frlengths;
2567 /* toggle the DMA set we are using */
2568 xfer->flags_int.curr_dma_set ^= 1;
2570 /* get next DMA set */
2571 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2572 xfer->td_transfer_first = td;
2574 pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
2576 /* store starting position */
2578 xfer->qh_pos = xfer->endpoint->isoc_next;
2580 fss = fss_start + (xfer->qh_pos % USB_ISOC_TIME_MAX);
2584 panic("%s:%d: out of TD's\n",
2585 __FUNCTION__, __LINE__);
2587 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2588 pp_last = &sc->sc_isoc_fs_p_last[0];
2590 if (fss >= fss_end) {
2593 /* reuse sitd_portaddr and sitd_back from last transfer */
2595 if (*plen > xfer->max_frame_size) {
2599 printf("%s: frame length(%d) exceeds %d "
2600 "bytes (frame truncated)\n",
2601 __FUNCTION__, *plen,
2602 xfer->max_frame_size);
2605 *plen = xfer->max_frame_size;
2608 * We currently don't care if the ISOCHRONOUS schedule is
2611 error = usbd_fs_isoc_schedule_alloc(fss, &sa, *plen);
2614 * The FULL speed schedule is FULL! Set length
2621 * only call "usbd_get_page()" when we have a
2624 usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2625 td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
2626 buf_offset += *plen;
2628 * NOTE: We need to subtract one from the offset so
2629 * that we are on a valid page!
2631 usbd_get_page(xfer->frbuffers, buf_offset - 1,
2633 temp = buf_res.physaddr & ~0xFFF;
2639 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
2642 temp |= 1; /* T-count = 1, TP = ALL */
2647 temp |= tlen; /* T-count = [1..6] */
2648 temp |= 8; /* TP = Begin */
2660 sa = (sb - sa) & 0x3F;
2663 sb = (-(4 << sa)) & 0xFE;
2664 sa = (1 << sa) & 0x3F;
2667 sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
2668 EHCI_SITD_SET_CMASK(sb));
2670 td->sitd_bp[1] = htohc32(sc, temp);
2672 td->sitd_mask = htohc32(sc, sitd_mask);
2675 td->sitd_status = htohc32(sc,
2678 EHCI_SITD_SET_LEN(*plen));
2680 td->sitd_status = htohc32(sc,
2682 EHCI_SITD_SET_LEN(*plen));
2684 usb_pc_cpu_flush(td->page_cache);
2687 if (ehcidebug > 15) {
2688 DPRINTF("FS-TD %d\n", nframes);
2689 ehci_dump_sitd(sc, td);
2692 /* insert TD into schedule */
2693 EHCI_APPEND_FS_TD(td, *pp_last);
2702 xfer->td_transfer_last = td_last;
2704 /* update isoc_next */
2705 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
2706 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2710 ehci_device_isoc_fs_start(struct usb_xfer *xfer)
2712 /* put transfer on interrupt queue */
2713 ehci_transfer_intr_enqueue(xfer);
2716 struct usb_pipe_methods ehci_device_isoc_fs_methods =
2718 .open = ehci_device_isoc_fs_open,
2719 .close = ehci_device_isoc_fs_close,
2720 .enter = ehci_device_isoc_fs_enter,
2721 .start = ehci_device_isoc_fs_start,
2724 /*------------------------------------------------------------------------*
2725 * ehci high speed isochronous support
2726 *------------------------------------------------------------------------*/
2728 ehci_device_isoc_hs_open(struct usb_xfer *xfer)
2730 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2735 usb_hs_bandwidth_alloc(xfer);
2737 /* initialize all TD's */
2739 for (ds = 0; ds != 2; ds++) {
2741 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2743 /* set TD inactive */
2744 td->itd_status[0] = 0;
2745 td->itd_status[1] = 0;
2746 td->itd_status[2] = 0;
2747 td->itd_status[3] = 0;
2748 td->itd_status[4] = 0;
2749 td->itd_status[5] = 0;
2750 td->itd_status[6] = 0;
2751 td->itd_status[7] = 0;
2753 /* set endpoint and address */
2754 td->itd_bp[0] = htohc32(sc,
2755 EHCI_ITD_SET_ADDR(xfer->address) |
2756 EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
2759 EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
2762 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2763 temp |= EHCI_ITD_SET_DIR_IN;
2765 /* set maximum packet size */
2766 td->itd_bp[1] = htohc32(sc, temp);
2768 /* set transfer multiplier */
2769 td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
2771 usb_pc_cpu_flush(td->page_cache);
2777 ehci_device_isoc_hs_close(struct usb_xfer *xfer)
2779 ehci_device_done(xfer, USB_ERR_CANCELLED);
2781 /* bandwidth must be freed after device done */
2782 usb_hs_bandwidth_free(xfer);
2786 ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
2788 struct usb_page_search buf_res;
2789 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2791 ehci_itd_t *td_last = NULL;
2792 ehci_itd_t **pp_last;
2793 bus_size_t page_addr;
2796 uint32_t buf_offset;
2798 uint32_t itd_offset[8 + 1];
2808 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2809 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2811 /* get the current frame index */
2813 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2816 * check if the frame index is within the window where the frames
2819 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2820 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2822 if ((xfer->endpoint->is_synced == 0) ||
2823 (buf_offset < ((xfer->nframes + 7) / 8))) {
2825 * If there is data underflow or the pipe queue is empty we
2826 * schedule the transfer a few frames ahead of the current
2827 * frame position. Else two isochronous transfers might
2830 xfer->endpoint->isoc_next = (nframes + 3) &
2831 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2832 xfer->endpoint->is_synced = 1;
2833 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2836 * compute how many milliseconds the insertion is ahead of the
2837 * current frame position:
2839 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2840 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2843 * pre-compute when the isochronous transfer will be finished:
2845 xfer->isoc_time_complete =
2846 usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2847 ((xfer->nframes + 7) / 8);
2849 /* get the real number of frames */
2851 nframes = xfer->nframes;
2856 plen = xfer->frlengths;
2858 /* toggle the DMA set we are using */
2859 xfer->flags_int.curr_dma_set ^= 1;
2861 /* get next DMA set */
2862 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2863 xfer->td_transfer_first = td;
2865 pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
2867 /* store starting position */
2869 xfer->qh_pos = xfer->endpoint->isoc_next;
2873 panic("%s:%d: out of TD's\n",
2874 __FUNCTION__, __LINE__);
2876 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2877 pp_last = &sc->sc_isoc_hs_p_last[0];
2880 if (*plen > xfer->max_frame_size) {
2884 printf("%s: frame length(%d) exceeds %d bytes "
2885 "(frame truncated)\n",
2886 __FUNCTION__, *plen, xfer->max_frame_size);
2889 *plen = xfer->max_frame_size;
2892 if (xfer->endpoint->usb_smask & (1 << td_no)) {
2893 status = (EHCI_ITD_SET_LEN(*plen) |
2895 EHCI_ITD_SET_PG(0));
2896 td->itd_status[td_no] = htohc32(sc, status);
2897 itd_offset[td_no] = buf_offset;
2898 buf_offset += *plen;
2902 td->itd_status[td_no] = 0; /* not active */
2903 itd_offset[td_no] = buf_offset;
2908 if ((td_no == 8) || (nframes == 0)) {
2910 /* the rest of the transfers are not active, if any */
2911 for (x = td_no; x != 8; x++) {
2912 td->itd_status[x] = 0; /* not active */
2915 /* check if there is any data to be transferred */
2916 if (itd_offset[0] != buf_offset) {
2918 itd_offset[td_no] = buf_offset;
2920 /* get first page offset */
2921 usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
2922 /* get page address */
2923 page_addr = buf_res.physaddr & ~0xFFF;
2924 /* update page address */
2925 td->itd_bp[0] &= htohc32(sc, 0xFFF);
2926 td->itd_bp[0] |= htohc32(sc, page_addr);
2928 for (x = 0; x != td_no; x++) {
2929 /* set page number and page offset */
2930 status = (EHCI_ITD_SET_PG(page_no) |
2931 (buf_res.physaddr & 0xFFF));
2932 td->itd_status[x] |= htohc32(sc, status);
2934 /* get next page offset */
2935 if (itd_offset[x + 1] == buf_offset) {
2937 * We subtract one so that
2938 * we don't go off the last
2941 usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
2943 usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
2946 /* check if we need a new page */
2947 if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
2948 /* new page needed */
2949 page_addr = buf_res.physaddr & ~0xFFF;
2951 panic("%s: too many pages\n", __FUNCTION__);
2954 /* update page address */
2955 td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2956 td->itd_bp[page_no] |= htohc32(sc, page_addr);
2960 /* set IOC bit if we are complete */
2962 td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
2964 usb_pc_cpu_flush(td->page_cache);
2966 if (ehcidebug > 15) {
2967 DPRINTF("HS-TD %d\n", nframes);
2968 ehci_dump_itd(sc, td);
2971 /* insert TD into schedule */
2972 EHCI_APPEND_HS_TD(td, *pp_last);
2981 xfer->td_transfer_last = td_last;
2983 /* update isoc_next */
2984 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
2985 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2989 ehci_device_isoc_hs_start(struct usb_xfer *xfer)
2991 /* put transfer on interrupt queue */
2992 ehci_transfer_intr_enqueue(xfer);
2995 struct usb_pipe_methods ehci_device_isoc_hs_methods =
2997 .open = ehci_device_isoc_hs_open,
2998 .close = ehci_device_isoc_hs_close,
2999 .enter = ehci_device_isoc_hs_enter,
3000 .start = ehci_device_isoc_hs_start,
3003 /*------------------------------------------------------------------------*
3004 * ehci root control support
3005 *------------------------------------------------------------------------*
3006 * Simulate a hardware hub by handling all the necessary requests.
3007 *------------------------------------------------------------------------*/
3010 struct usb_device_descriptor ehci_devd =
3012 sizeof(struct usb_device_descriptor),
3013 UDESC_DEVICE, /* type */
3014 {0x00, 0x02}, /* USB version */
3015 UDCLASS_HUB, /* class */
3016 UDSUBCLASS_HUB, /* subclass */
3017 UDPROTO_HSHUBSTT, /* protocol */
3018 64, /* max packet */
3019 {0}, {0}, {0x00, 0x01}, /* device id */
3020 1, 2, 0, /* string indicies */
3021 1 /* # of configurations */
3025 struct usb_device_qualifier ehci_odevd =
3027 sizeof(struct usb_device_qualifier),
3028 UDESC_DEVICE_QUALIFIER, /* type */
3029 {0x00, 0x02}, /* USB version */
3030 UDCLASS_HUB, /* class */
3031 UDSUBCLASS_HUB, /* subclass */
3032 UDPROTO_FSHUB, /* protocol */
3034 0, /* # of configurations */
3038 static const struct ehci_config_desc ehci_confd = {
3040 .bLength = sizeof(struct usb_config_descriptor),
3041 .bDescriptorType = UDESC_CONFIG,
3042 .wTotalLength[0] = sizeof(ehci_confd),
3044 .bConfigurationValue = 1,
3045 .iConfiguration = 0,
3046 .bmAttributes = UC_SELF_POWERED,
3047 .bMaxPower = 0 /* max power */
3050 .bLength = sizeof(struct usb_interface_descriptor),
3051 .bDescriptorType = UDESC_INTERFACE,
3053 .bInterfaceClass = UICLASS_HUB,
3054 .bInterfaceSubClass = UISUBCLASS_HUB,
3055 .bInterfaceProtocol = UIPROTO_HSHUBSTT,
3059 .bLength = sizeof(struct usb_endpoint_descriptor),
3060 .bDescriptorType = UDESC_ENDPOINT,
3061 .bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
3062 .bmAttributes = UE_INTERRUPT,
3063 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
3069 struct usb_hub_descriptor ehci_hubd =
3071 0, /* dynamic length */
3081 ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
3086 DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
3088 port = EHCI_PORTSC(index);
3089 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3090 EOWRITE4(sc, port, v | EHCI_PS_PO);
3094 ehci_roothub_exec(struct usb_device *udev,
3095 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3097 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3098 const char *str_ptr;
3109 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3112 ptr = (const void *)&sc->sc_hub_desc;
3116 value = UGETW(req->wValue);
3117 index = UGETW(req->wIndex);
3119 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3120 "wValue=0x%04x wIndex=0x%04x\n",
3121 req->bmRequestType, req->bRequest,
3122 UGETW(req->wLength), value, index);
3124 #define C(x,y) ((x) | ((y) << 8))
3125 switch (C(req->bRequest, req->bmRequestType)) {
3126 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3127 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3128 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3130 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3131 * for the integrated root hub.
3134 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3136 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3138 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3139 switch (value >> 8) {
3141 if ((value & 0xff) != 0) {
3142 err = USB_ERR_IOERROR;
3145 len = sizeof(ehci_devd);
3146 ptr = (const void *)&ehci_devd;
3149 * We can't really operate at another speed,
3150 * but the specification says we need this
3153 case UDESC_DEVICE_QUALIFIER:
3154 if ((value & 0xff) != 0) {
3155 err = USB_ERR_IOERROR;
3158 len = sizeof(ehci_odevd);
3159 ptr = (const void *)&ehci_odevd;
3163 if ((value & 0xff) != 0) {
3164 err = USB_ERR_IOERROR;
3167 len = sizeof(ehci_confd);
3168 ptr = (const void *)&ehci_confd;
3172 switch (value & 0xff) {
3173 case 0: /* Language table */
3177 case 1: /* Vendor */
3178 str_ptr = sc->sc_vendor;
3181 case 2: /* Product */
3182 str_ptr = "EHCI root HUB";
3190 len = usb_make_str_desc(
3191 sc->sc_hub_desc.temp,
3192 sizeof(sc->sc_hub_desc.temp),
3196 err = USB_ERR_IOERROR;
3200 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3202 sc->sc_hub_desc.temp[0] = 0;
3204 case C(UR_GET_STATUS, UT_READ_DEVICE):
3206 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3208 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3209 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3211 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3213 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3214 if (value >= EHCI_MAX_DEVICES) {
3215 err = USB_ERR_IOERROR;
3218 sc->sc_addr = value;
3220 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3221 if ((value != 0) && (value != 1)) {
3222 err = USB_ERR_IOERROR;
3225 sc->sc_conf = value;
3227 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3229 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3230 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3231 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3232 err = USB_ERR_IOERROR;
3234 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3236 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3239 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3241 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3242 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3245 (index > sc->sc_noport)) {
3246 err = USB_ERR_IOERROR;
3249 port = EHCI_PORTSC(index);
3250 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3252 case UHF_PORT_ENABLE:
3253 EOWRITE4(sc, port, v & ~EHCI_PS_PE);
3255 case UHF_PORT_SUSPEND:
3256 if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
3259 * waking up a High Speed device is rather
3262 EOWRITE4(sc, port, v | EHCI_PS_FPR);
3264 /* wait 20ms for resume sequence to complete */
3265 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3267 EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
3268 EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
3270 /* 4ms settle time */
3271 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3273 case UHF_PORT_POWER:
3274 EOWRITE4(sc, port, v & ~EHCI_PS_PP);
3277 DPRINTFN(3, "clear port test "
3280 case UHF_PORT_INDICATOR:
3281 DPRINTFN(3, "clear port ind "
3283 EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
3285 case UHF_C_PORT_CONNECTION:
3286 EOWRITE4(sc, port, v | EHCI_PS_CSC);
3288 case UHF_C_PORT_ENABLE:
3289 EOWRITE4(sc, port, v | EHCI_PS_PEC);
3291 case UHF_C_PORT_SUSPEND:
3292 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3294 case UHF_C_PORT_OVER_CURRENT:
3295 EOWRITE4(sc, port, v | EHCI_PS_OCC);
3297 case UHF_C_PORT_RESET:
3301 err = USB_ERR_IOERROR;
3305 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3306 if ((value & 0xff) != 0) {
3307 err = USB_ERR_IOERROR;
3310 v = EOREAD4(sc, EHCI_HCSPARAMS);
3312 sc->sc_hub_desc.hubd = ehci_hubd;
3313 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3314 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics,
3315 (EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH) |
3316 (EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS)) ?
3318 /* XXX can't find out? */
3319 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
3320 for (l = 0; l < sc->sc_noport; l++) {
3321 /* XXX can't find out? */
3322 sc->sc_hub_desc.hubd.DeviceRemovable[l / 8] &= ~(1 << (l % 8));
3324 sc->sc_hub_desc.hubd.bDescLength =
3325 8 + ((sc->sc_noport + 7) / 8);
3326 len = sc->sc_hub_desc.hubd.bDescLength;
3328 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3330 bzero(sc->sc_hub_desc.temp, 16);
3332 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3333 DPRINTFN(9, "get port status i=%d\n",
3336 (index > sc->sc_noport)) {
3337 err = USB_ERR_IOERROR;
3340 v = EOREAD4(sc, EHCI_PORTSC(index));
3341 DPRINTFN(9, "port status=0x%04x\n", v);
3342 if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
3343 if ((v & 0xc000000) == 0x8000000)
3345 else if ((v & 0xc000000) == 0x4000000)
3353 i |= UPS_CURRENT_CONNECT_STATUS;
3355 i |= UPS_PORT_ENABLED;
3356 if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
3358 if (v & EHCI_PS_OCA)
3359 i |= UPS_OVERCURRENT_INDICATOR;
3363 i |= UPS_PORT_POWER;
3364 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3366 if (v & EHCI_PS_CSC)
3367 i |= UPS_C_CONNECT_STATUS;
3368 if (v & EHCI_PS_PEC)
3369 i |= UPS_C_PORT_ENABLED;
3370 if (v & EHCI_PS_OCC)
3371 i |= UPS_C_OVERCURRENT_INDICATOR;
3372 if (v & EHCI_PS_FPR)
3375 i |= UPS_C_PORT_RESET;
3376 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3377 len = sizeof(sc->sc_hub_desc.ps);
3379 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3380 err = USB_ERR_IOERROR;
3382 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3384 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3386 (index > sc->sc_noport)) {
3387 err = USB_ERR_IOERROR;
3390 port = EHCI_PORTSC(index);
3391 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3393 case UHF_PORT_ENABLE:
3394 EOWRITE4(sc, port, v | EHCI_PS_PE);
3396 case UHF_PORT_SUSPEND:
3397 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3399 case UHF_PORT_RESET:
3400 DPRINTFN(6, "reset port %d\n", index);
3402 if (ehcinohighspeed) {
3404 * Connect USB device to companion
3407 ehci_disown(sc, index, 1);
3411 if (EHCI_PS_IS_LOWSPEED(v) &&
3412 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3413 /* Low speed device, give up ownership. */
3414 ehci_disown(sc, index, 1);
3417 /* Start reset sequence. */
3418 v &= ~(EHCI_PS_PE | EHCI_PS_PR);
3419 EOWRITE4(sc, port, v | EHCI_PS_PR);
3421 /* Wait for reset to complete. */
3422 usb_pause_mtx(&sc->sc_bus.bus_mtx,
3423 USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
3425 /* Terminate reset sequence. */
3426 if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
3427 EOWRITE4(sc, port, v);
3429 /* Wait for HC to complete reset. */
3430 usb_pause_mtx(&sc->sc_bus.bus_mtx,
3431 USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
3433 v = EOREAD4(sc, port);
3434 DPRINTF("ehci after reset, status=0x%08x\n", v);
3435 if (v & EHCI_PS_PR) {
3436 device_printf(sc->sc_bus.bdev,
3437 "port reset timeout\n");
3438 err = USB_ERR_TIMEOUT;
3441 if (!(v & EHCI_PS_PE) &&
3442 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3443 /* Not a high speed device, give up ownership.*/
3444 ehci_disown(sc, index, 0);
3448 DPRINTF("ehci port %d reset, status = 0x%08x\n",
3452 case UHF_PORT_POWER:
3453 DPRINTFN(3, "set port power %d\n", index);
3454 EOWRITE4(sc, port, v | EHCI_PS_PP);
3458 DPRINTFN(3, "set port test %d\n", index);
3461 case UHF_PORT_INDICATOR:
3462 DPRINTFN(3, "set port ind %d\n", index);
3463 EOWRITE4(sc, port, v | EHCI_PS_PIC);
3467 err = USB_ERR_IOERROR;
3471 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3472 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3473 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3474 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3477 err = USB_ERR_IOERROR;
3487 ehci_xfer_setup(struct usb_setup_params *parm)
3489 struct usb_page_search page_info;
3490 struct usb_page_cache *pc;
3492 struct usb_xfer *xfer;
3500 sc = EHCI_BUS2SC(parm->udev->bus);
3501 xfer = parm->curr_xfer;
3509 * compute maximum number of some structures
3511 if (parm->methods == &ehci_device_ctrl_methods) {
3514 * The proof for the "nqtd" formula is illustrated like
3517 * +------------------------------------+
3521 * | | xxx | x | frm 0 |
3523 * | | xxx | xx | frm 1 |
3526 * +------------------------------------+
3528 * "xxx" means a completely full USB transfer descriptor
3530 * "x" and "xx" means a short USB packet
3532 * For the remainder of an USB transfer modulo
3533 * "max_data_length" we need two USB transfer descriptors.
3534 * One to transfer the remaining data and one to finalise
3535 * with a zero length packet in case the "force_short_xfer"
3536 * flag is set. We only need two USB transfer descriptors in
3537 * the case where the transfer length of the first one is a
3538 * factor of "max_frame_size". The rest of the needed USB
3539 * transfer descriptors is given by the buffer size divided
3540 * by the maximum data payload.
3542 parm->hc_max_packet_size = 0x400;
3543 parm->hc_max_packet_count = 1;
3544 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3545 xfer->flags_int.bdma_enable = 1;
3547 usbd_transfer_setup_sub(parm);
3550 nqtd = ((2 * xfer->nframes) + 1 /* STATUS */
3551 + (xfer->max_data_length / xfer->max_hc_frame_size));
3553 } else if (parm->methods == &ehci_device_bulk_methods) {
3555 parm->hc_max_packet_size = 0x400;
3556 parm->hc_max_packet_count = 1;
3557 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3558 xfer->flags_int.bdma_enable = 1;
3560 usbd_transfer_setup_sub(parm);
3563 nqtd = ((2 * xfer->nframes)
3564 + (xfer->max_data_length / xfer->max_hc_frame_size));
3566 } else if (parm->methods == &ehci_device_intr_methods) {
3568 if (parm->speed == USB_SPEED_HIGH) {
3569 parm->hc_max_packet_size = 0x400;
3570 parm->hc_max_packet_count = 3;
3571 } else if (parm->speed == USB_SPEED_FULL) {
3572 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
3573 parm->hc_max_packet_count = 1;
3575 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
3576 parm->hc_max_packet_count = 1;
3579 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3580 xfer->flags_int.bdma_enable = 1;
3582 usbd_transfer_setup_sub(parm);
3585 nqtd = ((2 * xfer->nframes)
3586 + (xfer->max_data_length / xfer->max_hc_frame_size));
3588 } else if (parm->methods == &ehci_device_isoc_fs_methods) {
3590 parm->hc_max_packet_size = 0x3FF;
3591 parm->hc_max_packet_count = 1;
3592 parm->hc_max_frame_size = 0x3FF;
3593 xfer->flags_int.bdma_enable = 1;
3595 usbd_transfer_setup_sub(parm);
3597 nsitd = xfer->nframes;
3599 } else if (parm->methods == &ehci_device_isoc_hs_methods) {
3601 parm->hc_max_packet_size = 0x400;
3602 parm->hc_max_packet_count = 3;
3603 parm->hc_max_frame_size = 0xC00;
3604 xfer->flags_int.bdma_enable = 1;
3606 usbd_transfer_setup_sub(parm);
3608 nitd = ((xfer->nframes + 7) / 8) <<
3609 usbd_xfer_get_fps_shift(xfer);
3613 parm->hc_max_packet_size = 0x400;
3614 parm->hc_max_packet_count = 1;
3615 parm->hc_max_frame_size = 0x400;
3617 usbd_transfer_setup_sub(parm);
3626 * Allocate queue heads and transfer descriptors
3630 if (usbd_transfer_setup_sub_malloc(
3631 parm, &pc, sizeof(ehci_itd_t),
3632 EHCI_ITD_ALIGN, nitd)) {
3633 parm->err = USB_ERR_NOMEM;
3637 for (n = 0; n != nitd; n++) {
3640 usbd_get_page(pc + n, 0, &page_info);
3642 td = page_info.buffer;
3645 td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
3646 td->obj_next = last_obj;
3647 td->page_cache = pc + n;
3651 usb_pc_cpu_flush(pc + n);
3654 if (usbd_transfer_setup_sub_malloc(
3655 parm, &pc, sizeof(ehci_sitd_t),
3656 EHCI_SITD_ALIGN, nsitd)) {
3657 parm->err = USB_ERR_NOMEM;
3661 for (n = 0; n != nsitd; n++) {
3664 usbd_get_page(pc + n, 0, &page_info);
3666 td = page_info.buffer;
3669 td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
3670 td->obj_next = last_obj;
3671 td->page_cache = pc + n;
3675 usb_pc_cpu_flush(pc + n);
3678 if (usbd_transfer_setup_sub_malloc(
3679 parm, &pc, sizeof(ehci_qtd_t),
3680 EHCI_QTD_ALIGN, nqtd)) {
3681 parm->err = USB_ERR_NOMEM;
3685 for (n = 0; n != nqtd; n++) {
3688 usbd_get_page(pc + n, 0, &page_info);
3690 qtd = page_info.buffer;
3693 qtd->qtd_self = htohc32(sc, page_info.physaddr);
3694 qtd->obj_next = last_obj;
3695 qtd->page_cache = pc + n;
3699 usb_pc_cpu_flush(pc + n);
3702 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3706 if (usbd_transfer_setup_sub_malloc(
3707 parm, &pc, sizeof(ehci_qh_t),
3708 EHCI_QH_ALIGN, nqh)) {
3709 parm->err = USB_ERR_NOMEM;
3713 for (n = 0; n != nqh; n++) {
3716 usbd_get_page(pc + n, 0, &page_info);
3718 qh = page_info.buffer;
3721 qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
3722 qh->obj_next = last_obj;
3723 qh->page_cache = pc + n;
3727 usb_pc_cpu_flush(pc + n);
3730 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3732 if (!xfer->flags_int.curr_dma_set) {
3733 xfer->flags_int.curr_dma_set = 1;
3739 ehci_xfer_unsetup(struct usb_xfer *xfer)
3745 ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3746 struct usb_endpoint *ep)
3748 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3750 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3752 edesc->bEndpointAddress, udev->flags.usb_mode,
3755 if (udev->flags.usb_mode != USB_MODE_HOST) {
3759 if (udev->device_index != sc->sc_addr) {
3761 if ((udev->speed != USB_SPEED_HIGH) &&
3762 ((udev->hs_hub_addr == 0) ||
3763 (udev->hs_port_no == 0) ||
3764 (udev->parent_hs_hub == NULL) ||
3765 (udev->parent_hs_hub->hub == NULL))) {
3766 /* We need a transaction translator */
3769 switch (edesc->bmAttributes & UE_XFERTYPE) {
3771 ep->methods = &ehci_device_ctrl_methods;
3774 ep->methods = &ehci_device_intr_methods;
3776 case UE_ISOCHRONOUS:
3777 if (udev->speed == USB_SPEED_HIGH) {
3778 ep->methods = &ehci_device_isoc_hs_methods;
3779 } else if (udev->speed == USB_SPEED_FULL) {
3780 ep->methods = &ehci_device_isoc_fs_methods;
3784 if (udev->speed != USB_SPEED_LOW) {
3785 ep->methods = &ehci_device_bulk_methods;
3798 ehci_get_dma_delay(struct usb_bus *bus, uint32_t *pus)
3801 * Wait until the hardware has finished any possible use of
3802 * the transfer descriptor(s) and QH
3804 *pus = (188); /* microseconds */
3808 ehci_device_resume(struct usb_device *udev)
3810 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3811 struct usb_xfer *xfer;
3812 struct usb_pipe_methods *methods;
3816 USB_BUS_LOCK(udev->bus);
3818 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3820 if (xfer->xroot->udev == udev) {
3822 methods = xfer->endpoint->methods;
3824 if ((methods == &ehci_device_bulk_methods) ||
3825 (methods == &ehci_device_ctrl_methods)) {
3826 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3827 sc->sc_async_p_last);
3829 if (methods == &ehci_device_intr_methods) {
3830 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3831 sc->sc_intr_p_last[xfer->qh_pos]);
3836 USB_BUS_UNLOCK(udev->bus);
3842 ehci_device_suspend(struct usb_device *udev)
3844 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3845 struct usb_xfer *xfer;
3846 struct usb_pipe_methods *methods;
3850 USB_BUS_LOCK(udev->bus);
3852 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3854 if (xfer->xroot->udev == udev) {
3856 methods = xfer->endpoint->methods;
3858 if ((methods == &ehci_device_bulk_methods) ||
3859 (methods == &ehci_device_ctrl_methods)) {
3860 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3861 sc->sc_async_p_last);
3863 if (methods == &ehci_device_intr_methods) {
3864 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3865 sc->sc_intr_p_last[xfer->qh_pos]);
3870 USB_BUS_UNLOCK(udev->bus);
3876 ehci_set_hw_power(struct usb_bus *bus)
3878 ehci_softc_t *sc = EHCI_BUS2SC(bus);
3886 flags = bus->hw_power_state;
3888 temp = EOREAD4(sc, EHCI_USBCMD);
3890 temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
3892 if (flags & (USB_HW_POWER_CONTROL |
3893 USB_HW_POWER_BULK)) {
3894 DPRINTF("Async is active\n");
3895 temp |= EHCI_CMD_ASE;
3897 if (flags & (USB_HW_POWER_INTERRUPT |
3898 USB_HW_POWER_ISOC)) {
3899 DPRINTF("Periodic is active\n");
3900 temp |= EHCI_CMD_PSE;
3902 EOWRITE4(sc, EHCI_USBCMD, temp);
3904 USB_BUS_UNLOCK(bus);
3909 struct usb_bus_methods ehci_bus_methods =
3911 .endpoint_init = ehci_ep_init,
3912 .xfer_setup = ehci_xfer_setup,
3913 .xfer_unsetup = ehci_xfer_unsetup,
3914 .get_dma_delay = ehci_get_dma_delay,
3915 .device_resume = ehci_device_resume,
3916 .device_suspend = ehci_device_suspend,
3917 .set_hw_power = ehci_set_hw_power,
3918 .roothub_exec = ehci_roothub_exec,
3919 .xfer_poll = ehci_do_poll,