Sync ehci.c with FreeBSD
[dragonfly.git] / sys / bus / u4b / controller / ehci.c
blobdbbd5f842800370f15ece58ce80700007f65e645
1 /*-
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
9 * are met:
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
26 * SUCH DAMAGE.
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
42 * TODO:
43 * 1) command failures are not recovered correctly
46 #include <sys/stdint.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/types.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/bus.h>
53 #include <sys/module.h>
54 #include <sys/lock.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
60 #include <sys/priv.h>
62 #include <bus/u4b/usb.h>
63 #include <bus/u4b/usbdi.h>
65 #define USB_DEBUG_VAR ehcidebug
67 #include <bus/u4b/usb_core.h>
68 #include <bus/u4b/usb_debug.h>
69 #include <bus/u4b/usb_busdma.h>
70 #include <bus/u4b/usb_process.h>
71 #include <bus/u4b/usb_transfer.h>
72 #include <bus/u4b/usb_device.h>
73 #include <bus/u4b/usb_hub.h>
74 #include <bus/u4b/usb_util.h>
76 #include <bus/u4b/usb_controller.h>
77 #include <bus/u4b/usb_bus.h>
78 #include <bus/u4b/controller/ehci.h>
79 #include <bus/u4b/controller/ehcireg.h>
81 #define EHCI_BUS2SC(bus) \
82 ((ehci_softc_t *)(((uint8_t *)(bus)) - \
83 ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
85 #ifdef USB_DEBUG
86 static int ehcidebug = 0;
87 static int ehcinohighspeed = 0;
88 static int ehciiaadbug = 0;
89 static int ehcilostintrbug = 0;
91 static SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
92 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
93 &ehcidebug, 0, "Debug level");
94 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
95 &ehcinohighspeed, 0, "Disable High Speed USB");
96 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RW,
97 &ehciiaadbug, 0, "Enable doorbell bug workaround");
98 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RW,
99 &ehcilostintrbug, 0, "Enable lost interrupt bug workaround");
101 TUNABLE_INT("hw.usb.ehci.debug", &ehcidebug);
102 TUNABLE_INT("hw.usb.ehci.no_hs", &ehcinohighspeed);
103 TUNABLE_INT("hw.usb.ehci.iaadbug", &ehciiaadbug);
104 TUNABLE_INT("hw.usb.ehci.lostintrbug", &ehcilostintrbug);
106 static void ehci_dump_regs(ehci_softc_t *sc);
107 static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
109 #endif
111 #define EHCI_INTR_ENDPT 1
113 static const struct usb_bus_methods ehci_bus_methods;
114 static const struct usb_pipe_methods ehci_device_bulk_methods;
115 static const struct usb_pipe_methods ehci_device_ctrl_methods;
116 static const struct usb_pipe_methods ehci_device_intr_methods;
117 static const struct usb_pipe_methods ehci_device_isoc_fs_methods;
118 static const struct usb_pipe_methods ehci_device_isoc_hs_methods;
120 static void ehci_do_poll(struct usb_bus *);
121 static void ehci_device_done(struct usb_xfer *, usb_error_t);
122 static uint8_t ehci_check_transfer(struct usb_xfer *);
123 static void ehci_timeout(void *);
124 static void ehci_poll_timeout(void *);
126 static void ehci_root_intr(ehci_softc_t *sc);
128 struct ehci_std_temp {
129 ehci_softc_t *sc;
130 struct usb_page_cache *pc;
131 ehci_qtd_t *td;
132 ehci_qtd_t *td_next;
133 uint32_t average;
134 uint32_t qtd_status;
135 uint32_t len;
136 uint16_t max_frame_size;
137 uint8_t shortpkt;
138 uint8_t auto_data_toggle;
139 uint8_t setup_alt_next;
140 uint8_t last_frame;
143 void
144 ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
146 ehci_softc_t *sc = EHCI_BUS2SC(bus);
147 uint32_t i;
149 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
150 sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
152 cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg,
153 sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN);
155 cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
156 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
158 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
159 cb(bus, sc->sc_hw.intr_start_pc + i,
160 sc->sc_hw.intr_start_pg + i,
161 sizeof(ehci_qh_t), EHCI_QH_ALIGN);
164 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
165 cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
166 sc->sc_hw.isoc_hs_start_pg + i,
167 sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
170 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
171 cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
172 sc->sc_hw.isoc_fs_start_pg + i,
173 sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
177 usb_error_t
178 ehci_reset(ehci_softc_t *sc)
180 uint32_t hcr;
181 int i;
183 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
184 for (i = 0; i < 100; i++) {
185 usb_pause_mtx(NULL, hz / 128);
186 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
187 if (!hcr) {
188 if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
190 * Force USBMODE as requested. Controllers
191 * may have multiple operating modes.
193 uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
194 if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
195 usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
196 device_printf(sc->sc_bus.bdev,
197 "set host controller mode\n");
199 if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
200 usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
201 device_printf(sc->sc_bus.bdev,
202 "set big-endian mode\n");
204 EOWRITE4(sc, EHCI_USBMODE, usbmode);
206 return (0);
209 device_printf(sc->sc_bus.bdev, "Reset timeout\n");
210 return (USB_ERR_IOERROR);
213 static usb_error_t
214 ehci_hcreset(ehci_softc_t *sc)
216 uint32_t hcr;
217 int i;
219 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
220 for (i = 0; i < 100; i++) {
221 usb_pause_mtx(NULL, hz / 128);
222 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
223 if (hcr)
224 break;
226 if (!hcr)
228 * Fall through and try reset anyway even though
229 * Table 2-9 in the EHCI spec says this will result
230 * in undefined behavior.
232 device_printf(sc->sc_bus.bdev, "stop timeout\n");
234 return (ehci_reset(sc));
237 static int
238 ehci_init_sub(struct ehci_softc *sc)
240 struct usb_page_search buf_res;
241 uint32_t cparams;
242 uint32_t hcr;
243 uint8_t i;
245 cparams = EREAD4(sc, EHCI_HCCPARAMS);
247 DPRINTF("cparams=0x%x\n", cparams);
249 if (EHCI_HCC_64BIT(cparams)) {
250 DPRINTF("HCC uses 64-bit structures\n");
252 /* MUST clear segment register if 64 bit capable */
253 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
256 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
257 EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
259 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
260 EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
262 /* enable interrupts */
263 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
265 /* turn on controller */
266 EOWRITE4(sc, EHCI_USBCMD,
267 EHCI_CMD_ITC_1 | /* 1 microframes interrupt delay */
268 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
269 EHCI_CMD_ASE |
270 EHCI_CMD_PSE |
271 EHCI_CMD_RS);
273 /* Take over port ownership */
274 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
276 for (i = 0; i < 100; i++) {
277 usb_pause_mtx(NULL, hz / 128);
278 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
279 if (!hcr) {
280 break;
283 if (hcr) {
284 device_printf(sc->sc_bus.bdev, "Run timeout\n");
285 return (USB_ERR_IOERROR);
287 return (USB_ERR_NORMAL_COMPLETION);
290 usb_error_t
291 ehci_init(ehci_softc_t *sc)
293 struct usb_page_search buf_res;
294 uint32_t version;
295 uint32_t sparams;
296 uint16_t i;
297 uint16_t x;
298 uint16_t y;
299 uint16_t bit;
300 usb_error_t err = 0;
302 DPRINTF("start\n");
304 usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_lock, 0);
305 usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_lock, 0);
307 sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
309 #ifdef USB_DEBUG
310 if (ehciiaadbug)
311 sc->sc_flags |= EHCI_SCFLG_IAADBUG;
312 if (ehcilostintrbug)
313 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
314 if (ehcidebug > 2) {
315 ehci_dump_regs(sc);
317 #endif
319 version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
320 device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
321 version >> 8, version & 0xff);
323 sparams = EREAD4(sc, EHCI_HCSPARAMS);
324 DPRINTF("sparams=0x%x\n", sparams);
326 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
327 sc->sc_bus.usbrev = USB_REV_2_0;
329 if (!(sc->sc_flags & EHCI_SCFLG_DONTRESET)) {
330 /* Reset the controller */
331 DPRINTF("%s: resetting\n",
332 device_get_nameunit(sc->sc_bus.bdev));
334 err = ehci_hcreset(sc);
335 if (err) {
336 device_printf(sc->sc_bus.bdev, "reset timeout\n");
337 return (err);
342 * use current frame-list-size selection 0: 1024*4 bytes 1: 512*4
343 * bytes 2: 256*4 bytes 3: unknown
345 if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
346 device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
347 return (USB_ERR_IOERROR);
349 /* set up the bus struct */
350 sc->sc_bus.methods = &ehci_bus_methods;
352 sc->sc_eintrs = EHCI_NORMAL_INTRS;
354 if (1) {
355 struct ehci_qh_sub *qh;
357 usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res);
359 qh = buf_res.buffer;
361 sc->sc_terminate_self = htohc32(sc, buf_res.physaddr);
363 /* init terminate TD */
364 qh->qtd_next =
365 htohc32(sc, EHCI_LINK_TERMINATE);
366 qh->qtd_altnext =
367 htohc32(sc, EHCI_LINK_TERMINATE);
368 qh->qtd_status =
369 htohc32(sc, EHCI_QTD_HALTED);
372 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
373 ehci_qh_t *qh;
375 usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
377 qh = buf_res.buffer;
379 /* initialize page cache pointer */
381 qh->page_cache = sc->sc_hw.intr_start_pc + i;
383 /* store a pointer to queue head */
385 sc->sc_intr_p_last[i] = qh;
387 qh->qh_self =
388 htohc32(sc, buf_res.physaddr) |
389 htohc32(sc, EHCI_LINK_QH);
391 qh->qh_endp =
392 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
393 qh->qh_endphub =
394 htohc32(sc, EHCI_QH_SET_MULT(1));
395 qh->qh_curqtd = 0;
397 qh->qh_qtd.qtd_next =
398 htohc32(sc, EHCI_LINK_TERMINATE);
399 qh->qh_qtd.qtd_altnext =
400 htohc32(sc, EHCI_LINK_TERMINATE);
401 qh->qh_qtd.qtd_status =
402 htohc32(sc, EHCI_QTD_HALTED);
406 * the QHs are arranged to give poll intervals that are
407 * powers of 2 times 1ms
409 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
410 while (bit) {
411 x = bit;
412 while (x & bit) {
413 ehci_qh_t *qh_x;
414 ehci_qh_t *qh_y;
416 y = (x ^ bit) | (bit / 2);
418 qh_x = sc->sc_intr_p_last[x];
419 qh_y = sc->sc_intr_p_last[y];
422 * the next QH has half the poll interval
424 qh_x->qh_link = qh_y->qh_self;
426 x++;
428 bit >>= 1;
431 if (1) {
432 ehci_qh_t *qh;
434 qh = sc->sc_intr_p_last[0];
436 /* the last (1ms) QH terminates */
437 qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
439 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
440 ehci_sitd_t *sitd;
441 ehci_itd_t *itd;
443 usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
445 sitd = buf_res.buffer;
447 /* initialize page cache pointer */
449 sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
451 /* store a pointer to the transfer descriptor */
453 sc->sc_isoc_fs_p_last[i] = sitd;
455 /* initialize full speed isochronous */
457 sitd->sitd_self =
458 htohc32(sc, buf_res.physaddr) |
459 htohc32(sc, EHCI_LINK_SITD);
461 sitd->sitd_back =
462 htohc32(sc, EHCI_LINK_TERMINATE);
464 sitd->sitd_next =
465 sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
468 usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
470 itd = buf_res.buffer;
472 /* initialize page cache pointer */
474 itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
476 /* store a pointer to the transfer descriptor */
478 sc->sc_isoc_hs_p_last[i] = itd;
480 /* initialize high speed isochronous */
482 itd->itd_self =
483 htohc32(sc, buf_res.physaddr) |
484 htohc32(sc, EHCI_LINK_ITD);
486 itd->itd_next =
487 sitd->sitd_self;
490 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
492 if (1) {
493 uint32_t *pframes;
495 pframes = buf_res.buffer;
498 * execution order:
499 * pframes -> high speed isochronous ->
500 * full speed isochronous -> interrupt QH's
502 for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
503 pframes[i] = sc->sc_isoc_hs_p_last
504 [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
507 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
509 if (1) {
511 ehci_qh_t *qh;
513 qh = buf_res.buffer;
515 /* initialize page cache pointer */
517 qh->page_cache = &sc->sc_hw.async_start_pc;
519 /* store a pointer to the queue head */
521 sc->sc_async_p_last = qh;
523 /* init dummy QH that starts the async list */
525 qh->qh_self =
526 htohc32(sc, buf_res.physaddr) |
527 htohc32(sc, EHCI_LINK_QH);
529 /* fill the QH */
530 qh->qh_endp =
531 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
532 qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
533 qh->qh_link = qh->qh_self;
534 qh->qh_curqtd = 0;
536 /* fill the overlay qTD */
537 qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
538 qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
539 qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
541 /* flush all cache into memory */
543 usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
545 #ifdef USB_DEBUG
546 if (ehcidebug) {
547 ehci_dump_sqh(sc, sc->sc_async_p_last);
549 #endif
551 /* finial setup */
552 err = ehci_init_sub(sc);
554 if (!err) {
555 /* catch any lost interrupts */
556 ehci_do_poll(&sc->sc_bus);
558 return (err);
562 * shut down the controller when the system is going down
564 void
565 ehci_detach(ehci_softc_t *sc)
567 USB_BUS_LOCK(&sc->sc_bus);
569 usb_callout_stop(&sc->sc_tmo_pcd);
570 usb_callout_stop(&sc->sc_tmo_poll);
572 EOWRITE4(sc, EHCI_USBINTR, 0);
573 USB_BUS_UNLOCK(&sc->sc_bus);
575 if (ehci_hcreset(sc)) {
576 DPRINTF("reset failed!\n");
579 /* XXX let stray task complete */
580 usb_pause_mtx(NULL, hz / 20);
582 usb_callout_drain(&sc->sc_tmo_pcd);
583 usb_callout_drain(&sc->sc_tmo_poll);
586 static void
587 ehci_suspend(ehci_softc_t *sc)
589 DPRINTF("stopping the HC\n");
591 /* reset HC */
592 ehci_hcreset(sc);
595 static void
596 ehci_resume(ehci_softc_t *sc)
598 /* reset HC */
599 ehci_hcreset(sc);
601 /* setup HC */
602 ehci_init_sub(sc);
604 /* catch any lost interrupts */
605 ehci_do_poll(&sc->sc_bus);
608 #ifdef USB_DEBUG
609 static void
610 ehci_dump_regs(ehci_softc_t *sc)
612 uint32_t i;
614 i = EOREAD4(sc, EHCI_USBCMD);
615 kprintf("cmd=0x%08x\n", i);
617 if (i & EHCI_CMD_ITC_1)
618 kprintf(" EHCI_CMD_ITC_1\n");
619 if (i & EHCI_CMD_ITC_2)
620 kprintf(" EHCI_CMD_ITC_2\n");
621 if (i & EHCI_CMD_ITC_4)
622 kprintf(" EHCI_CMD_ITC_4\n");
623 if (i & EHCI_CMD_ITC_8)
624 kprintf(" EHCI_CMD_ITC_8\n");
625 if (i & EHCI_CMD_ITC_16)
626 kprintf(" EHCI_CMD_ITC_16\n");
627 if (i & EHCI_CMD_ITC_32)
628 kprintf(" EHCI_CMD_ITC_32\n");
629 if (i & EHCI_CMD_ITC_64)
630 kprintf(" EHCI_CMD_ITC_64\n");
631 if (i & EHCI_CMD_ASPME)
632 kprintf(" EHCI_CMD_ASPME\n");
633 if (i & EHCI_CMD_ASPMC)
634 kprintf(" EHCI_CMD_ASPMC\n");
635 if (i & EHCI_CMD_LHCR)
636 kprintf(" EHCI_CMD_LHCR\n");
637 if (i & EHCI_CMD_IAAD)
638 kprintf(" EHCI_CMD_IAAD\n");
639 if (i & EHCI_CMD_ASE)
640 kprintf(" EHCI_CMD_ASE\n");
641 if (i & EHCI_CMD_PSE)
642 kprintf(" EHCI_CMD_PSE\n");
643 if (i & EHCI_CMD_FLS_M)
644 kprintf(" EHCI_CMD_FLS_M\n");
645 if (i & EHCI_CMD_HCRESET)
646 kprintf(" EHCI_CMD_HCRESET\n");
647 if (i & EHCI_CMD_RS)
648 kprintf(" EHCI_CMD_RS\n");
650 i = EOREAD4(sc, EHCI_USBSTS);
652 kprintf("sts=0x%08x\n", i);
654 if (i & EHCI_STS_ASS)
655 kprintf(" EHCI_STS_ASS\n");
656 if (i & EHCI_STS_PSS)
657 kprintf(" EHCI_STS_PSS\n");
658 if (i & EHCI_STS_REC)
659 kprintf(" EHCI_STS_REC\n");
660 if (i & EHCI_STS_HCH)
661 kprintf(" EHCI_STS_HCH\n");
662 if (i & EHCI_STS_IAA)
663 kprintf(" EHCI_STS_IAA\n");
664 if (i & EHCI_STS_HSE)
665 kprintf(" EHCI_STS_HSE\n");
666 if (i & EHCI_STS_FLR)
667 kprintf(" EHCI_STS_FLR\n");
668 if (i & EHCI_STS_PCD)
669 kprintf(" EHCI_STS_PCD\n");
670 if (i & EHCI_STS_ERRINT)
671 kprintf(" EHCI_STS_ERRINT\n");
672 if (i & EHCI_STS_INT)
673 kprintf(" EHCI_STS_INT\n");
675 kprintf("ien=0x%08x\n",
676 EOREAD4(sc, EHCI_USBINTR));
677 kprintf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
678 EOREAD4(sc, EHCI_FRINDEX),
679 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
680 EOREAD4(sc, EHCI_PERIODICLISTBASE),
681 EOREAD4(sc, EHCI_ASYNCLISTADDR));
682 for (i = 1; i <= sc->sc_noport; i++) {
683 kprintf("port %d status=0x%08x\n", i,
684 EOREAD4(sc, EHCI_PORTSC(i)));
688 static void
689 ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
691 link = hc32toh(sc, link);
692 kprintf("0x%08x", link);
693 if (link & EHCI_LINK_TERMINATE)
694 kprintf("<T>");
695 else {
696 kprintf("<");
697 if (type) {
698 switch (EHCI_LINK_TYPE(link)) {
699 case EHCI_LINK_ITD:
700 kprintf("ITD");
701 break;
702 case EHCI_LINK_QH:
703 kprintf("QH");
704 break;
705 case EHCI_LINK_SITD:
706 kprintf("SITD");
707 break;
708 case EHCI_LINK_FSTN:
709 kprintf("FSTN");
710 break;
713 kprintf(">");
717 static void
718 ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
720 uint32_t s;
722 kprintf(" next=");
723 ehci_dump_link(sc, qtd->qtd_next, 0);
724 kprintf(" altnext=");
725 ehci_dump_link(sc, qtd->qtd_altnext, 0);
726 kprintf("\n");
727 s = hc32toh(sc, qtd->qtd_status);
728 kprintf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
729 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
730 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
731 kprintf(" cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
732 EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
733 (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
734 (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
735 (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
736 (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
737 (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
738 (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
739 (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
740 (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
742 for (s = 0; s < 5; s++) {
743 kprintf(" buffer[%d]=0x%08x\n", s,
744 hc32toh(sc, qtd->qtd_buffer[s]));
746 for (s = 0; s < 5; s++) {
747 kprintf(" buffer_hi[%d]=0x%08x\n", s,
748 hc32toh(sc, qtd->qtd_buffer_hi[s]));
752 static uint8_t
753 ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
755 uint8_t temp;
757 usb_pc_cpu_invalidate(sqtd->page_cache);
758 kprintf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
759 ehci_dump_qtd(sc, sqtd);
760 temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
761 return (temp);
764 static void
765 ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
767 uint16_t i;
768 uint8_t stop;
770 stop = 0;
771 for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
772 stop = ehci_dump_sqtd(sc, sqtd);
774 if (sqtd) {
775 kprintf("dump aborted, too many TDs\n");
779 static void
780 ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
782 uint32_t endp;
783 uint32_t endphub;
785 usb_pc_cpu_invalidate(qh->page_cache);
786 kprintf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
787 kprintf(" link=");
788 ehci_dump_link(sc, qh->qh_link, 1);
789 kprintf("\n");
790 endp = hc32toh(sc, qh->qh_endp);
791 kprintf(" endp=0x%08x\n", endp);
792 kprintf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
793 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
794 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
795 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
796 kprintf(" mpl=0x%x ctl=%d nrl=%d\n",
797 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
798 EHCI_QH_GET_NRL(endp));
799 endphub = hc32toh(sc, qh->qh_endphub);
800 kprintf(" endphub=0x%08x\n", endphub);
801 kprintf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
802 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
803 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
804 EHCI_QH_GET_MULT(endphub));
805 kprintf(" curqtd=");
806 ehci_dump_link(sc, qh->qh_curqtd, 0);
807 kprintf("\n");
808 kprintf("Overlay qTD:\n");
809 ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
812 static void
813 ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
815 usb_pc_cpu_invalidate(sitd->page_cache);
816 kprintf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
817 kprintf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
818 kprintf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
819 hc32toh(sc, sitd->sitd_portaddr),
820 (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
821 ? "in" : "out",
822 EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
823 EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
824 EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
825 EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
826 kprintf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
827 kprintf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
828 (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
829 EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
830 kprintf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
831 hc32toh(sc, sitd->sitd_back),
832 hc32toh(sc, sitd->sitd_bp[0]),
833 hc32toh(sc, sitd->sitd_bp[1]),
834 hc32toh(sc, sitd->sitd_bp_hi[0]),
835 hc32toh(sc, sitd->sitd_bp_hi[1]));
838 static void
839 ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
841 usb_pc_cpu_invalidate(itd->page_cache);
842 kprintf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
843 kprintf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
844 kprintf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
845 (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
846 kprintf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
847 (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
848 kprintf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
849 (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
850 kprintf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
851 (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
852 kprintf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
853 (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
854 kprintf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
855 (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
856 kprintf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
857 (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
858 kprintf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
859 (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
860 kprintf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
861 kprintf(" addr=0x%02x; endpt=0x%01x\n",
862 EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
863 EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
864 kprintf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
865 kprintf(" dir=%s; mpl=0x%02x\n",
866 (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
867 EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
868 kprintf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
869 hc32toh(sc, itd->itd_bp[2]),
870 hc32toh(sc, itd->itd_bp[3]),
871 hc32toh(sc, itd->itd_bp[4]),
872 hc32toh(sc, itd->itd_bp[5]),
873 hc32toh(sc, itd->itd_bp[6]));
874 kprintf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
875 " 0x%08x,0x%08x,0x%08x\n",
876 hc32toh(sc, itd->itd_bp_hi[0]),
877 hc32toh(sc, itd->itd_bp_hi[1]),
878 hc32toh(sc, itd->itd_bp_hi[2]),
879 hc32toh(sc, itd->itd_bp_hi[3]),
880 hc32toh(sc, itd->itd_bp_hi[4]),
881 hc32toh(sc, itd->itd_bp_hi[5]),
882 hc32toh(sc, itd->itd_bp_hi[6]));
885 static void
886 ehci_dump_isoc(ehci_softc_t *sc)
888 ehci_itd_t *itd;
889 ehci_sitd_t *sitd;
890 uint16_t max = 1000;
891 uint16_t pos;
893 pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
894 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
896 kprintf("%s: isochronous dump from frame 0x%03x:\n",
897 __func__, pos);
899 itd = sc->sc_isoc_hs_p_last[pos];
900 sitd = sc->sc_isoc_fs_p_last[pos];
902 while (itd && max && max--) {
903 ehci_dump_itd(sc, itd);
904 itd = itd->prev;
907 while (sitd && max && max--) {
908 ehci_dump_sitd(sc, sitd);
909 sitd = sitd->prev;
913 #endif
915 static void
916 ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
918 /* check for early completion */
919 if (ehci_check_transfer(xfer)) {
920 return;
922 /* put transfer on interrupt queue */
923 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
925 /* start timeout, if any */
926 if (xfer->timeout != 0) {
927 usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
931 #define EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
932 static ehci_sitd_t *
933 _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
935 DPRINTFN(11, "%p to %p\n", std, last);
937 /* (sc->sc_bus.mtx) must be locked */
939 std->next = last->next;
940 std->sitd_next = last->sitd_next;
942 std->prev = last;
944 usb_pc_cpu_flush(std->page_cache);
947 * the last->next->prev is never followed: std->next->prev = std;
949 last->next = std;
950 last->sitd_next = std->sitd_self;
952 usb_pc_cpu_flush(last->page_cache);
954 return (std);
957 #define EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
958 static ehci_itd_t *
959 _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
961 DPRINTFN(11, "%p to %p\n", std, last);
963 /* (sc->sc_bus.mtx) must be locked */
965 std->next = last->next;
966 std->itd_next = last->itd_next;
968 std->prev = last;
970 usb_pc_cpu_flush(std->page_cache);
973 * the last->next->prev is never followed: std->next->prev = std;
975 last->next = std;
976 last->itd_next = std->itd_self;
978 usb_pc_cpu_flush(last->page_cache);
980 return (std);
983 #define EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
984 static ehci_qh_t *
985 _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
987 DPRINTFN(11, "%p to %p\n", sqh, last);
989 if (sqh->prev != NULL) {
990 /* should not happen */
991 DPRINTFN(0, "QH already linked!\n");
992 return (last);
994 /* (sc->sc_bus.mtx) must be locked */
996 sqh->next = last->next;
997 sqh->qh_link = last->qh_link;
999 sqh->prev = last;
1001 usb_pc_cpu_flush(sqh->page_cache);
1004 * the last->next->prev is never followed: sqh->next->prev = sqh;
1007 last->next = sqh;
1008 last->qh_link = sqh->qh_self;
1010 usb_pc_cpu_flush(last->page_cache);
1012 return (sqh);
1015 #define EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
1016 static ehci_sitd_t *
1017 _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
1019 DPRINTFN(11, "%p from %p\n", std, last);
1021 /* (sc->sc_bus.mtx) must be locked */
1023 std->prev->next = std->next;
1024 std->prev->sitd_next = std->sitd_next;
1026 usb_pc_cpu_flush(std->prev->page_cache);
1028 if (std->next) {
1029 std->next->prev = std->prev;
1030 usb_pc_cpu_flush(std->next->page_cache);
1032 return ((last == std) ? std->prev : last);
1035 #define EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
1036 static ehci_itd_t *
1037 _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1039 DPRINTFN(11, "%p from %p\n", std, last);
1041 /* (sc->sc_bus.mtx) must be locked */
1043 std->prev->next = std->next;
1044 std->prev->itd_next = std->itd_next;
1046 usb_pc_cpu_flush(std->prev->page_cache);
1048 if (std->next) {
1049 std->next->prev = std->prev;
1050 usb_pc_cpu_flush(std->next->page_cache);
1052 return ((last == std) ? std->prev : last);
1055 #define EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
1056 static ehci_qh_t *
1057 _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1059 DPRINTFN(11, "%p from %p\n", sqh, last);
1061 /* (sc->sc_bus.mtx) must be locked */
1063 /* only remove if not removed from a queue */
1064 if (sqh->prev) {
1066 sqh->prev->next = sqh->next;
1067 sqh->prev->qh_link = sqh->qh_link;
1069 usb_pc_cpu_flush(sqh->prev->page_cache);
1071 if (sqh->next) {
1072 sqh->next->prev = sqh->prev;
1073 usb_pc_cpu_flush(sqh->next->page_cache);
1075 last = ((last == sqh) ? sqh->prev : last);
1077 sqh->prev = 0;
1079 usb_pc_cpu_flush(sqh->page_cache);
1081 return (last);
1084 static void
1085 ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen)
1087 uint16_t rem;
1088 uint8_t dt;
1090 /* count number of full packets */
1091 dt = (actlen / xfer->max_packet_size) & 1;
1093 /* compute remainder */
1094 rem = actlen % xfer->max_packet_size;
1096 if (rem > 0)
1097 dt ^= 1; /* short packet at the end */
1098 else if (actlen != xlen)
1099 dt ^= 1; /* zero length packet at the end */
1100 else if (xlen == 0)
1101 dt ^= 1; /* zero length transfer */
1103 xfer->endpoint->toggle_next ^= dt;
1106 static usb_error_t
1107 ehci_non_isoc_done_sub(struct usb_xfer *xfer)
1109 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1110 ehci_qtd_t *td;
1111 ehci_qtd_t *td_alt_next;
1112 uint32_t status;
1113 uint16_t len;
1115 td = xfer->td_transfer_cache;
1116 td_alt_next = td->alt_next;
1118 if (xfer->aframes != xfer->nframes) {
1119 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1121 while (1) {
1123 usb_pc_cpu_invalidate(td->page_cache);
1124 status = hc32toh(sc, td->qtd_status);
1126 len = EHCI_QTD_GET_BYTES(status);
1129 * Verify the status length and
1130 * add the length to "frlengths[]":
1132 if (len > td->len) {
1133 /* should not happen */
1134 DPRINTF("Invalid status length, "
1135 "0x%04x/0x%04x bytes\n", len, td->len);
1136 status |= EHCI_QTD_HALTED;
1137 } else if (xfer->aframes != xfer->nframes) {
1138 xfer->frlengths[xfer->aframes] += td->len - len;
1139 /* manually update data toggle */
1140 ehci_data_toggle_update(xfer, td->len - len, td->len);
1143 /* Check for last transfer */
1144 if (((void *)td) == xfer->td_transfer_last) {
1145 td = NULL;
1146 break;
1148 /* Check for transfer error */
1149 if (status & EHCI_QTD_HALTED) {
1150 /* the transfer is finished */
1151 td = NULL;
1152 break;
1154 /* Check for short transfer */
1155 if (len > 0) {
1156 if (xfer->flags_int.short_frames_ok) {
1157 /* follow alt next */
1158 td = td->alt_next;
1159 } else {
1160 /* the transfer is finished */
1161 td = NULL;
1163 break;
1165 td = td->obj_next;
1167 if (td->alt_next != td_alt_next) {
1168 /* this USB frame is complete */
1169 break;
1173 /* update transfer cache */
1175 xfer->td_transfer_cache = td;
1177 #ifdef USB_DEBUG
1178 if (status & EHCI_QTD_STATERRS) {
1179 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
1180 "status=%s%s%s%s%s%s%s%s\n",
1181 xfer->address, xfer->endpointno, xfer->aframes,
1182 (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1183 (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
1184 (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
1185 (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
1186 (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
1187 (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
1188 (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
1189 (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
1191 #endif
1193 return ((status & EHCI_QTD_HALTED) ?
1194 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1197 static void
1198 ehci_non_isoc_done(struct usb_xfer *xfer)
1200 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1201 ehci_qh_t *qh;
1202 uint32_t status;
1203 usb_error_t err = 0;
1205 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1206 xfer, xfer->endpoint);
1208 #ifdef USB_DEBUG
1209 if (ehcidebug > 10) {
1210 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1212 ehci_dump_sqtds(sc, xfer->td_transfer_first);
1214 #endif
1216 /* extract data toggle directly from the QH's overlay area */
1218 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1220 usb_pc_cpu_invalidate(qh->page_cache);
1222 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1224 /* reset scanner */
1226 xfer->td_transfer_cache = xfer->td_transfer_first;
1228 if (xfer->flags_int.control_xfr) {
1230 if (xfer->flags_int.control_hdr) {
1232 err = ehci_non_isoc_done_sub(xfer);
1234 xfer->aframes = 1;
1236 if (xfer->td_transfer_cache == NULL) {
1237 goto done;
1240 while (xfer->aframes != xfer->nframes) {
1242 err = ehci_non_isoc_done_sub(xfer);
1243 xfer->aframes++;
1245 if (xfer->td_transfer_cache == NULL) {
1246 goto done;
1250 if (xfer->flags_int.control_xfr &&
1251 !xfer->flags_int.control_act) {
1253 err = ehci_non_isoc_done_sub(xfer);
1255 done:
1256 ehci_device_done(xfer, err);
1259 /*------------------------------------------------------------------------*
1260 * ehci_check_transfer
1262 * Return values:
1263 * 0: USB transfer is not finished
1264 * Else: USB transfer is finished
1265 *------------------------------------------------------------------------*/
1266 static uint8_t
1267 ehci_check_transfer(struct usb_xfer *xfer)
1269 const struct usb_pipe_methods *methods = xfer->endpoint->methods;
1270 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1272 uint32_t status;
1274 DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
1276 if (methods == &ehci_device_isoc_fs_methods) {
1277 ehci_sitd_t *td;
1279 /* isochronous full speed transfer */
1281 td = xfer->td_transfer_last;
1282 usb_pc_cpu_invalidate(td->page_cache);
1283 status = hc32toh(sc, td->sitd_status);
1285 /* also check if first is complete */
1287 td = xfer->td_transfer_first;
1288 usb_pc_cpu_invalidate(td->page_cache);
1289 status |= hc32toh(sc, td->sitd_status);
1291 if (!(status & EHCI_SITD_ACTIVE)) {
1292 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1293 goto transferred;
1295 } else if (methods == &ehci_device_isoc_hs_methods) {
1296 ehci_itd_t *td;
1298 /* isochronous high speed transfer */
1300 /* check last transfer */
1301 td = xfer->td_transfer_last;
1302 usb_pc_cpu_invalidate(td->page_cache);
1303 status = td->itd_status[0];
1304 status |= td->itd_status[1];
1305 status |= td->itd_status[2];
1306 status |= td->itd_status[3];
1307 status |= td->itd_status[4];
1308 status |= td->itd_status[5];
1309 status |= td->itd_status[6];
1310 status |= td->itd_status[7];
1312 /* also check first transfer */
1313 td = xfer->td_transfer_first;
1314 usb_pc_cpu_invalidate(td->page_cache);
1315 status |= td->itd_status[0];
1316 status |= td->itd_status[1];
1317 status |= td->itd_status[2];
1318 status |= td->itd_status[3];
1319 status |= td->itd_status[4];
1320 status |= td->itd_status[5];
1321 status |= td->itd_status[6];
1322 status |= td->itd_status[7];
1324 /* if no transactions are active we continue */
1325 if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
1326 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1327 goto transferred;
1329 } else {
1330 ehci_qtd_t *td;
1331 ehci_qh_t *qh;
1333 /* non-isochronous transfer */
1336 * check whether there is an error somewhere in the middle,
1337 * or whether there was a short packet (SPD and not ACTIVE)
1339 td = xfer->td_transfer_cache;
1341 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1343 usb_pc_cpu_invalidate(qh->page_cache);
1345 status = hc32toh(sc, qh->qh_qtd.qtd_status);
1346 if (status & EHCI_QTD_ACTIVE) {
1347 /* transfer is pending */
1348 goto done;
1351 while (1) {
1352 usb_pc_cpu_invalidate(td->page_cache);
1353 status = hc32toh(sc, td->qtd_status);
1356 * Check if there is an active TD which
1357 * indicates that the transfer isn't done.
1359 if (status & EHCI_QTD_ACTIVE) {
1360 /* update cache */
1361 xfer->td_transfer_cache = td;
1362 goto done;
1365 * last transfer descriptor makes the transfer done
1367 if (((void *)td) == xfer->td_transfer_last) {
1368 break;
1371 * any kind of error makes the transfer done
1373 if (status & EHCI_QTD_HALTED) {
1374 break;
1377 * if there is no alternate next transfer, a short
1378 * packet also makes the transfer done
1380 if (EHCI_QTD_GET_BYTES(status)) {
1381 if (xfer->flags_int.short_frames_ok) {
1382 /* follow alt next */
1383 if (td->alt_next) {
1384 td = td->alt_next;
1385 continue;
1388 /* transfer is done */
1389 break;
1391 td = td->obj_next;
1393 ehci_non_isoc_done(xfer);
1394 goto transferred;
1397 done:
1398 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1399 return (0);
1401 transferred:
1402 return (1);
1405 static void
1406 ehci_pcd_enable(ehci_softc_t *sc)
1408 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
1410 sc->sc_eintrs |= EHCI_STS_PCD;
1411 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1413 /* acknowledge any PCD interrupt */
1414 EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
1416 ehci_root_intr(sc);
1419 static void
1420 ehci_interrupt_poll(ehci_softc_t *sc)
1422 struct usb_xfer *xfer;
1424 repeat:
1425 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1427 * check if transfer is transferred
1429 if (ehci_check_transfer(xfer)) {
1430 /* queue has been modified */
1431 goto repeat;
1437 * Some EHCI chips from VIA / ATI seem to trigger interrupts before
1438 * writing back the qTD status, or miss signalling occasionally under
1439 * heavy load. If the host machine is too fast, we can miss
1440 * transaction completion - when we scan the active list the
1441 * transaction still seems to be active. This generally exhibits
1442 * itself as a umass stall that never recovers.
1444 * We work around this behaviour by setting up this callback after any
1445 * softintr that completes with transactions still pending, giving us
1446 * another chance to check for completion after the writeback has
1447 * taken place.
1449 static void
1450 ehci_poll_timeout(void *arg)
1452 ehci_softc_t *sc = arg;
1454 DPRINTFN(3, "\n");
1455 ehci_interrupt_poll(sc);
1458 /*------------------------------------------------------------------------*
1459 * ehci_interrupt - EHCI interrupt handler
1461 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1462 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1463 * is present !
1464 *------------------------------------------------------------------------*/
1465 void
1466 ehci_interrupt(ehci_softc_t *sc)
1468 uint32_t status;
1470 USB_BUS_LOCK(&sc->sc_bus);
1472 DPRINTFN(16, "real interrupt\n");
1474 #ifdef USB_DEBUG
1475 if (ehcidebug > 15) {
1476 ehci_dump_regs(sc);
1478 #endif
1480 status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1481 if (status == 0) {
1482 /* the interrupt was not for us */
1483 goto done;
1485 if (!(status & sc->sc_eintrs)) {
1486 goto done;
1488 EOWRITE4(sc, EHCI_USBSTS, status); /* acknowledge */
1490 status &= sc->sc_eintrs;
1492 if (status & EHCI_STS_HSE) {
1493 kprintf("%s: unrecoverable error, "
1494 "controller halted\n", __func__);
1495 #ifdef USB_DEBUG
1496 ehci_dump_regs(sc);
1497 ehci_dump_isoc(sc);
1498 #endif
1500 if (status & EHCI_STS_PCD) {
1502 * Disable PCD interrupt for now, because it will be
1503 * on until the port has been reset.
1505 sc->sc_eintrs &= ~EHCI_STS_PCD;
1506 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1508 ehci_root_intr(sc);
1510 /* do not allow RHSC interrupts > 1 per second */
1511 usb_callout_reset(&sc->sc_tmo_pcd, hz,
1512 (void *)&ehci_pcd_enable, sc);
1514 status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
1516 if (status != 0) {
1517 /* block unprocessed interrupts */
1518 sc->sc_eintrs &= ~status;
1519 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1520 kprintf("%s: blocking interrupts 0x%x\n", __func__, status);
1522 /* poll all the USB transfers */
1523 ehci_interrupt_poll(sc);
1525 if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
1526 usb_callout_reset(&sc->sc_tmo_poll, hz / 128,
1527 (void *)&ehci_poll_timeout, sc);
1530 done:
1531 USB_BUS_UNLOCK(&sc->sc_bus);
1535 * called when a request does not complete
1537 static void
1538 ehci_timeout(void *arg)
1540 struct usb_xfer *xfer = arg;
1542 DPRINTF("xfer=%p\n", xfer);
1544 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
1546 /* transfer is transferred */
1547 ehci_device_done(xfer, USB_ERR_TIMEOUT);
1550 static void
1551 ehci_do_poll(struct usb_bus *bus)
1553 ehci_softc_t *sc = EHCI_BUS2SC(bus);
1555 USB_BUS_LOCK(&sc->sc_bus);
1556 ehci_interrupt_poll(sc);
1557 USB_BUS_UNLOCK(&sc->sc_bus);
1560 static void
1561 ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
1563 struct usb_page_search buf_res;
1564 ehci_qtd_t *td;
1565 ehci_qtd_t *td_next;
1566 ehci_qtd_t *td_alt_next;
1567 uint32_t buf_offset;
1568 uint32_t average;
1569 uint32_t len_old;
1570 uint32_t terminate;
1571 uint32_t qtd_altnext;
1572 uint8_t shortpkt_old;
1573 uint8_t precompute;
1575 terminate = temp->sc->sc_terminate_self;
1576 qtd_altnext = temp->sc->sc_terminate_self;
1577 td_alt_next = NULL;
1578 buf_offset = 0;
1579 shortpkt_old = temp->shortpkt;
1580 len_old = temp->len;
1581 precompute = 1;
1583 restart:
1585 td = temp->td;
1586 td_next = temp->td_next;
1588 while (1) {
1590 if (temp->len == 0) {
1592 if (temp->shortpkt) {
1593 break;
1595 /* send a Zero Length Packet, ZLP, last */
1597 temp->shortpkt = 1;
1598 average = 0;
1600 } else {
1602 average = temp->average;
1604 if (temp->len < average) {
1605 if (temp->len % temp->max_frame_size) {
1606 temp->shortpkt = 1;
1608 average = temp->len;
1612 if (td_next == NULL) {
1613 panic("%s: out of EHCI transfer descriptors!", __func__);
1615 /* get next TD */
1617 td = td_next;
1618 td_next = td->obj_next;
1620 /* check if we are pre-computing */
1622 if (precompute) {
1624 /* update remaining length */
1626 temp->len -= average;
1628 continue;
1630 /* fill out current TD */
1632 td->qtd_status =
1633 temp->qtd_status |
1634 htohc32(temp->sc, EHCI_QTD_IOC |
1635 EHCI_QTD_SET_BYTES(average));
1637 if (average == 0) {
1639 if (temp->auto_data_toggle == 0) {
1641 /* update data toggle, ZLP case */
1643 temp->qtd_status ^=
1644 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1646 td->len = 0;
1648 td->qtd_buffer[0] = 0;
1649 td->qtd_buffer_hi[0] = 0;
1651 td->qtd_buffer[1] = 0;
1652 td->qtd_buffer_hi[1] = 0;
1654 } else {
1656 uint8_t x;
1658 if (temp->auto_data_toggle == 0) {
1660 /* update data toggle */
1662 if (((average + temp->max_frame_size - 1) /
1663 temp->max_frame_size) & 1) {
1664 temp->qtd_status ^=
1665 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1668 td->len = average;
1670 /* update remaining length */
1672 temp->len -= average;
1674 /* fill out buffer pointers */
1676 usbd_get_page(temp->pc, buf_offset, &buf_res);
1677 td->qtd_buffer[0] =
1678 htohc32(temp->sc, buf_res.physaddr);
1679 td->qtd_buffer_hi[0] = 0;
1681 x = 1;
1683 while (average > EHCI_PAGE_SIZE) {
1684 average -= EHCI_PAGE_SIZE;
1685 buf_offset += EHCI_PAGE_SIZE;
1686 usbd_get_page(temp->pc, buf_offset, &buf_res);
1687 td->qtd_buffer[x] =
1688 htohc32(temp->sc,
1689 buf_res.physaddr & (~0xFFF));
1690 td->qtd_buffer_hi[x] = 0;
1691 x++;
1695 * NOTE: The "average" variable is never zero after
1696 * exiting the loop above !
1698 * NOTE: We have to subtract one from the offset to
1699 * ensure that we are computing the physical address
1700 * of a valid page !
1702 buf_offset += average;
1703 usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
1704 td->qtd_buffer[x] =
1705 htohc32(temp->sc,
1706 buf_res.physaddr & (~0xFFF));
1707 td->qtd_buffer_hi[x] = 0;
1710 if (td_next) {
1711 /* link the current TD with the next one */
1712 td->qtd_next = td_next->qtd_self;
1714 td->qtd_altnext = qtd_altnext;
1715 td->alt_next = td_alt_next;
1717 usb_pc_cpu_flush(td->page_cache);
1720 if (precompute) {
1721 precompute = 0;
1723 /* setup alt next pointer, if any */
1724 if (temp->last_frame) {
1725 td_alt_next = NULL;
1726 qtd_altnext = terminate;
1727 } else {
1728 /* we use this field internally */
1729 td_alt_next = td_next;
1730 if (temp->setup_alt_next) {
1731 qtd_altnext = td_next->qtd_self;
1732 } else {
1733 qtd_altnext = terminate;
1737 /* restore */
1738 temp->shortpkt = shortpkt_old;
1739 temp->len = len_old;
1740 goto restart;
1742 temp->td = td;
1743 temp->td_next = td_next;
1746 static void
1747 ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
1749 struct ehci_std_temp temp;
1750 const struct usb_pipe_methods *methods;
1751 ehci_qh_t *qh;
1752 ehci_qtd_t *td;
1753 uint32_t qh_endp;
1754 uint32_t qh_endphub;
1755 uint32_t x;
1757 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1758 xfer->address, UE_GET_ADDR(xfer->endpointno),
1759 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1761 temp.average = xfer->max_hc_frame_size;
1762 temp.max_frame_size = xfer->max_frame_size;
1763 temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
1765 /* toggle the DMA set we are using */
1766 xfer->flags_int.curr_dma_set ^= 1;
1768 /* get next DMA set */
1769 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1771 xfer->td_transfer_first = td;
1772 xfer->td_transfer_cache = td;
1774 temp.td = NULL;
1775 temp.td_next = td;
1776 temp.qtd_status = 0;
1777 temp.last_frame = 0;
1778 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1780 if (xfer->flags_int.control_xfr) {
1781 if (xfer->endpoint->toggle_next) {
1782 /* DATA1 is next */
1783 temp.qtd_status |=
1784 htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1786 temp.auto_data_toggle = 0;
1787 } else {
1788 temp.auto_data_toggle = 1;
1791 if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
1792 (xfer->xroot->udev->address != 0)) {
1793 /* max 3 retries */
1794 temp.qtd_status |=
1795 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1797 /* check if we should prepend a setup message */
1799 if (xfer->flags_int.control_xfr) {
1800 if (xfer->flags_int.control_hdr) {
1802 xfer->endpoint->toggle_next = 0;
1804 temp.qtd_status &=
1805 htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1806 temp.qtd_status |= htohc32(temp.sc,
1807 EHCI_QTD_ACTIVE |
1808 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
1809 EHCI_QTD_SET_TOGGLE(0));
1811 temp.len = xfer->frlengths[0];
1812 temp.pc = xfer->frbuffers + 0;
1813 temp.shortpkt = temp.len ? 1 : 0;
1814 /* check for last frame */
1815 if (xfer->nframes == 1) {
1816 /* no STATUS stage yet, SETUP is last */
1817 if (xfer->flags_int.control_act) {
1818 temp.last_frame = 1;
1819 temp.setup_alt_next = 0;
1822 ehci_setup_standard_chain_sub(&temp);
1824 x = 1;
1825 } else {
1826 x = 0;
1829 while (x != xfer->nframes) {
1831 /* DATA0 / DATA1 message */
1833 temp.len = xfer->frlengths[x];
1834 temp.pc = xfer->frbuffers + x;
1836 x++;
1838 if (x == xfer->nframes) {
1839 if (xfer->flags_int.control_xfr) {
1840 /* no STATUS stage yet, DATA is last */
1841 if (xfer->flags_int.control_act) {
1842 temp.last_frame = 1;
1843 temp.setup_alt_next = 0;
1845 } else {
1846 temp.last_frame = 1;
1847 temp.setup_alt_next = 0;
1850 /* keep previous data toggle and error count */
1852 temp.qtd_status &=
1853 htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1854 EHCI_QTD_SET_TOGGLE(1));
1856 if (temp.len == 0) {
1858 /* make sure that we send an USB packet */
1860 temp.shortpkt = 0;
1862 } else {
1864 /* regular data transfer */
1866 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1869 /* set endpoint direction */
1871 temp.qtd_status |=
1872 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1873 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1874 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1875 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1876 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
1878 ehci_setup_standard_chain_sub(&temp);
1881 /* check if we should append a status stage */
1883 if (xfer->flags_int.control_xfr &&
1884 !xfer->flags_int.control_act) {
1887 * Send a DATA1 message and invert the current endpoint
1888 * direction.
1891 temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1892 EHCI_QTD_SET_TOGGLE(1));
1893 temp.qtd_status |=
1894 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1895 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1896 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
1897 EHCI_QTD_SET_TOGGLE(1)) :
1898 htohc32(temp.sc, EHCI_QTD_ACTIVE |
1899 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
1900 EHCI_QTD_SET_TOGGLE(1));
1902 temp.len = 0;
1903 temp.pc = NULL;
1904 temp.shortpkt = 0;
1905 temp.last_frame = 1;
1906 temp.setup_alt_next = 0;
1908 ehci_setup_standard_chain_sub(&temp);
1910 td = temp.td;
1912 /* the last TD terminates the transfer: */
1913 td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1914 td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1916 usb_pc_cpu_flush(td->page_cache);
1918 /* must have at least one frame! */
1920 xfer->td_transfer_last = td;
1922 #ifdef USB_DEBUG
1923 if (ehcidebug > 8) {
1924 DPRINTF("nexttog=%d; data before transfer:\n",
1925 xfer->endpoint->toggle_next);
1926 ehci_dump_sqtds(temp.sc,
1927 xfer->td_transfer_first);
1929 #endif
1931 methods = xfer->endpoint->methods;
1933 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1935 /* the "qh_link" field is filled when the QH is added */
1937 qh_endp =
1938 (EHCI_QH_SET_ADDR(xfer->address) |
1939 EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
1940 EHCI_QH_SET_MPL(xfer->max_packet_size));
1942 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
1943 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
1944 if (methods != &ehci_device_intr_methods)
1945 qh_endp |= EHCI_QH_SET_NRL(8);
1946 } else {
1948 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
1949 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
1950 } else {
1951 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
1954 if (methods == &ehci_device_ctrl_methods) {
1955 qh_endp |= EHCI_QH_CTL;
1957 if (methods != &ehci_device_intr_methods) {
1958 /* Only try one time per microframe! */
1959 qh_endp |= EHCI_QH_SET_NRL(1);
1963 if (temp.auto_data_toggle == 0) {
1964 /* software computes the data toggle */
1965 qh_endp |= EHCI_QH_DTC;
1968 qh->qh_endp = htohc32(temp.sc, qh_endp);
1970 qh_endphub =
1971 (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
1972 EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
1973 EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
1974 EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
1975 EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
1977 qh->qh_endphub = htohc32(temp.sc, qh_endphub);
1978 qh->qh_curqtd = 0;
1980 /* fill the overlay qTD */
1982 if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
1983 /* DATA1 is next */
1984 qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1985 } else {
1986 qh->qh_qtd.qtd_status = 0;
1989 td = xfer->td_transfer_first;
1991 qh->qh_qtd.qtd_next = td->qtd_self;
1992 qh->qh_qtd.qtd_altnext =
1993 htohc32(temp.sc, EHCI_LINK_TERMINATE);
1995 usb_pc_cpu_flush(qh->page_cache);
1997 if (xfer->xroot->udev->flags.self_suspended == 0) {
1998 EHCI_APPEND_QH(qh, *qh_last);
2002 static void
2003 ehci_root_intr(ehci_softc_t *sc)
2005 uint16_t i;
2006 uint16_t m;
2008 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2010 /* clear any old interrupt data */
2011 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2013 /* set bits */
2014 m = (sc->sc_noport + 1);
2015 if (m > (8 * sizeof(sc->sc_hub_idata))) {
2016 m = (8 * sizeof(sc->sc_hub_idata));
2018 for (i = 1; i < m; i++) {
2019 /* pick out CHANGE bits from the status register */
2020 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
2021 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2022 DPRINTF("port %d changed\n", i);
2025 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2026 sizeof(sc->sc_hub_idata));
2029 static void
2030 ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2032 uint32_t nframes = xfer->nframes;
2033 uint32_t status;
2034 uint32_t *plen = xfer->frlengths;
2035 uint16_t len = 0;
2036 ehci_sitd_t *td = xfer->td_transfer_first;
2037 ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
2039 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2040 xfer, xfer->endpoint);
2042 while (nframes--) {
2043 if (td == NULL) {
2044 panic("%s:%d: out of TD's\n",
2045 __func__, __LINE__);
2047 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2048 pp_last = &sc->sc_isoc_fs_p_last[0];
2050 #ifdef USB_DEBUG
2051 if (ehcidebug > 15) {
2052 DPRINTF("isoc FS-TD\n");
2053 ehci_dump_sitd(sc, td);
2055 #endif
2056 usb_pc_cpu_invalidate(td->page_cache);
2057 status = hc32toh(sc, td->sitd_status);
2059 len = EHCI_SITD_GET_LEN(status);
2061 DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2063 if (*plen >= len) {
2064 len = *plen - len;
2065 } else {
2066 len = 0;
2069 *plen = len;
2071 /* remove FS-TD from schedule */
2072 EHCI_REMOVE_FS_TD(td, *pp_last);
2074 pp_last++;
2075 plen++;
2076 td = td->obj_next;
2079 xfer->aframes = xfer->nframes;
2082 static void
2083 ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
2085 uint32_t nframes = xfer->nframes;
2086 uint32_t status;
2087 uint32_t *plen = xfer->frlengths;
2088 uint16_t len = 0;
2089 uint8_t td_no = 0;
2090 ehci_itd_t *td = xfer->td_transfer_first;
2091 ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
2093 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2094 xfer, xfer->endpoint);
2096 while (nframes) {
2097 if (td == NULL) {
2098 panic("%s:%d: out of TD's\n",
2099 __func__, __LINE__);
2101 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2102 pp_last = &sc->sc_isoc_hs_p_last[0];
2104 #ifdef USB_DEBUG
2105 if (ehcidebug > 15) {
2106 DPRINTF("isoc HS-TD\n");
2107 ehci_dump_itd(sc, td);
2109 #endif
2111 usb_pc_cpu_invalidate(td->page_cache);
2112 status = hc32toh(sc, td->itd_status[td_no]);
2114 len = EHCI_ITD_GET_LEN(status);
2116 DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2118 if (xfer->endpoint->usb_smask & (1 << td_no)) {
2120 if (*plen >= len) {
2122 * The length is valid. NOTE: The
2123 * complete length is written back
2124 * into the status field, and not the
2125 * remainder like with other transfer
2126 * descriptor types.
2128 } else {
2129 /* Invalid length - truncate */
2130 len = 0;
2133 *plen = len;
2134 plen++;
2135 nframes--;
2138 td_no++;
2140 if ((td_no == 8) || (nframes == 0)) {
2141 /* remove HS-TD from schedule */
2142 EHCI_REMOVE_HS_TD(td, *pp_last);
2143 pp_last++;
2145 td_no = 0;
2146 td = td->obj_next;
2149 xfer->aframes = xfer->nframes;
2152 /* NOTE: "done" can be run two times in a row,
2153 * from close and from interrupt
2155 static void
2156 ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
2158 const struct usb_pipe_methods *methods = xfer->endpoint->methods;
2159 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2161 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2163 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2164 xfer, xfer->endpoint, error);
2166 if ((methods == &ehci_device_bulk_methods) ||
2167 (methods == &ehci_device_ctrl_methods)) {
2168 #ifdef USB_DEBUG
2169 if (ehcidebug > 8) {
2170 DPRINTF("nexttog=%d; data after transfer:\n",
2171 xfer->endpoint->toggle_next);
2172 ehci_dump_sqtds(sc,
2173 xfer->td_transfer_first);
2175 #endif
2177 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2178 sc->sc_async_p_last);
2180 if (methods == &ehci_device_intr_methods) {
2181 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2182 sc->sc_intr_p_last[xfer->qh_pos]);
2185 * Only finish isochronous transfers once which will update
2186 * "xfer->frlengths".
2188 if (xfer->td_transfer_first &&
2189 xfer->td_transfer_last) {
2190 if (methods == &ehci_device_isoc_fs_methods) {
2191 ehci_isoc_fs_done(sc, xfer);
2193 if (methods == &ehci_device_isoc_hs_methods) {
2194 ehci_isoc_hs_done(sc, xfer);
2196 xfer->td_transfer_first = NULL;
2197 xfer->td_transfer_last = NULL;
2199 /* dequeue transfer and start next transfer */
2200 usbd_transfer_done(xfer, error);
2203 /*------------------------------------------------------------------------*
2204 * ehci bulk support
2205 *------------------------------------------------------------------------*/
2206 static void
2207 ehci_device_bulk_open(struct usb_xfer *xfer)
2209 return;
2212 static void
2213 ehci_device_bulk_close(struct usb_xfer *xfer)
2215 ehci_device_done(xfer, USB_ERR_CANCELLED);
2218 static void
2219 ehci_device_bulk_enter(struct usb_xfer *xfer)
2221 return;
2224 static void
2225 ehci_device_bulk_start(struct usb_xfer *xfer)
2227 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2228 uint32_t temp;
2230 /* setup TD's and QH */
2231 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2233 /* put transfer on interrupt queue */
2234 ehci_transfer_intr_enqueue(xfer);
2237 * XXX Certain nVidia chipsets choke when using the IAAD
2238 * feature too frequently.
2240 if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
2241 return;
2243 /* XXX Performance quirk: Some Host Controllers have a too low
2244 * interrupt rate. Issue an IAAD to stimulate the Host
2245 * Controller after queueing the BULK transfer.
2247 temp = EOREAD4(sc, EHCI_USBCMD);
2248 if (!(temp & EHCI_CMD_IAAD))
2249 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
2252 static const struct usb_pipe_methods ehci_device_bulk_methods =
2254 .open = ehci_device_bulk_open,
2255 .close = ehci_device_bulk_close,
2256 .enter = ehci_device_bulk_enter,
2257 .start = ehci_device_bulk_start,
2260 /*------------------------------------------------------------------------*
2261 * ehci control support
2262 *------------------------------------------------------------------------*/
2263 static void
2264 ehci_device_ctrl_open(struct usb_xfer *xfer)
2266 return;
2269 static void
2270 ehci_device_ctrl_close(struct usb_xfer *xfer)
2272 ehci_device_done(xfer, USB_ERR_CANCELLED);
2275 static void
2276 ehci_device_ctrl_enter(struct usb_xfer *xfer)
2278 return;
2281 static void
2282 ehci_device_ctrl_start(struct usb_xfer *xfer)
2284 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2286 /* setup TD's and QH */
2287 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2289 /* put transfer on interrupt queue */
2290 ehci_transfer_intr_enqueue(xfer);
2293 static const struct usb_pipe_methods ehci_device_ctrl_methods =
2295 .open = ehci_device_ctrl_open,
2296 .close = ehci_device_ctrl_close,
2297 .enter = ehci_device_ctrl_enter,
2298 .start = ehci_device_ctrl_start,
2301 /*------------------------------------------------------------------------*
2302 * ehci interrupt support
2303 *------------------------------------------------------------------------*/
2304 static void
2305 ehci_device_intr_open(struct usb_xfer *xfer)
2307 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2308 uint16_t best;
2309 uint16_t bit;
2310 uint16_t x;
2312 usb_hs_bandwidth_alloc(xfer);
2315 * Find the best QH position corresponding to the given interval:
2318 best = 0;
2319 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
2320 while (bit) {
2321 if (xfer->interval >= bit) {
2322 x = bit;
2323 best = bit;
2324 while (x & bit) {
2325 if (sc->sc_intr_stat[x] <
2326 sc->sc_intr_stat[best]) {
2327 best = x;
2329 x++;
2331 break;
2333 bit >>= 1;
2336 sc->sc_intr_stat[best]++;
2337 xfer->qh_pos = best;
2339 DPRINTFN(3, "best=%d interval=%d\n",
2340 best, xfer->interval);
2343 static void
2344 ehci_device_intr_close(struct usb_xfer *xfer)
2346 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2348 sc->sc_intr_stat[xfer->qh_pos]--;
2350 ehci_device_done(xfer, USB_ERR_CANCELLED);
2352 /* bandwidth must be freed after device done */
2353 usb_hs_bandwidth_free(xfer);
2356 static void
2357 ehci_device_intr_enter(struct usb_xfer *xfer)
2359 return;
2362 static void
2363 ehci_device_intr_start(struct usb_xfer *xfer)
2365 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2367 /* setup TD's and QH */
2368 ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
2370 /* put transfer on interrupt queue */
2371 ehci_transfer_intr_enqueue(xfer);
2374 static const struct usb_pipe_methods ehci_device_intr_methods =
2376 .open = ehci_device_intr_open,
2377 .close = ehci_device_intr_close,
2378 .enter = ehci_device_intr_enter,
2379 .start = ehci_device_intr_start,
2382 /*------------------------------------------------------------------------*
2383 * ehci full speed isochronous support
2384 *------------------------------------------------------------------------*/
2385 static void
2386 ehci_device_isoc_fs_open(struct usb_xfer *xfer)
2388 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2389 ehci_sitd_t *td;
2390 uint32_t sitd_portaddr;
2391 uint8_t ds;
2393 sitd_portaddr =
2394 EHCI_SITD_SET_ADDR(xfer->address) |
2395 EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
2396 EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
2397 EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
2399 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2400 sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
2402 sitd_portaddr = htohc32(sc, sitd_portaddr);
2404 /* initialize all TD's */
2406 for (ds = 0; ds != 2; ds++) {
2408 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2410 td->sitd_portaddr = sitd_portaddr;
2413 * TODO: make some kind of automatic
2414 * SMASK/CMASK selection based on micro-frame
2415 * usage
2417 * micro-frame usage (8 microframes per 1ms)
2419 td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
2421 usb_pc_cpu_flush(td->page_cache);
2426 static void
2427 ehci_device_isoc_fs_close(struct usb_xfer *xfer)
2429 ehci_device_done(xfer, USB_ERR_CANCELLED);
2432 static void
2433 ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
2435 struct usb_page_search buf_res;
2436 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2437 ehci_sitd_t *td;
2438 ehci_sitd_t *td_last = NULL;
2439 ehci_sitd_t **pp_last;
2440 uint32_t *plen;
2441 uint32_t buf_offset;
2442 uint32_t nframes;
2443 uint32_t temp;
2444 uint32_t sitd_mask;
2445 uint16_t tlen;
2446 uint8_t sa;
2447 uint8_t sb;
2449 #ifdef USB_DEBUG
2450 uint8_t once = 1;
2452 #endif
2454 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2455 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2457 /* get the current frame index */
2459 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2462 * check if the frame index is within the window where the frames
2463 * will be inserted
2465 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2466 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2468 if ((xfer->endpoint->is_synced == 0) ||
2469 (buf_offset < xfer->nframes)) {
2471 * If there is data underflow or the pipe queue is empty we
2472 * schedule the transfer a few frames ahead of the current
2473 * frame position. Else two isochronous transfers might
2474 * overlap.
2476 xfer->endpoint->isoc_next = (nframes + 3) &
2477 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2478 xfer->endpoint->is_synced = 1;
2479 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2482 * compute how many milliseconds the insertion is ahead of the
2483 * current frame position:
2485 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2486 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2489 * pre-compute when the isochronous transfer will be finished:
2491 xfer->isoc_time_complete =
2492 usb_isoc_time_expand(&sc->sc_bus, nframes) +
2493 buf_offset + xfer->nframes;
2495 /* get the real number of frames */
2497 nframes = xfer->nframes;
2499 buf_offset = 0;
2501 plen = xfer->frlengths;
2503 /* toggle the DMA set we are using */
2504 xfer->flags_int.curr_dma_set ^= 1;
2506 /* get next DMA set */
2507 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2508 xfer->td_transfer_first = td;
2510 pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
2512 /* store starting position */
2514 xfer->qh_pos = xfer->endpoint->isoc_next;
2516 while (nframes--) {
2517 if (td == NULL) {
2518 panic("%s:%d: out of TD's\n",
2519 __func__, __LINE__);
2521 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2522 pp_last = &sc->sc_isoc_fs_p_last[0];
2524 /* reuse sitd_portaddr and sitd_back from last transfer */
2526 if (*plen > xfer->max_frame_size) {
2527 #ifdef USB_DEBUG
2528 if (once) {
2529 once = 0;
2530 kprintf("%s: frame length(%d) exceeds %d "
2531 "bytes (frame truncated)\n",
2532 __func__, *plen,
2533 xfer->max_frame_size);
2535 #endif
2536 *plen = xfer->max_frame_size;
2539 /* allocate a slot */
2541 sa = usbd_fs_isoc_schedule_alloc_slot(xfer,
2542 xfer->isoc_time_complete - nframes - 1);
2544 if(sa == 255) {
2546 * Schedule is FULL, set length to zero:
2549 *plen = 0;
2550 sa = USB_FS_ISOC_UFRAME_MAX - 1;
2552 if (*plen) {
2554 * only call "usbd_get_page()" when we have a
2555 * non-zero length
2557 usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2558 td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
2559 buf_offset += *plen;
2561 * NOTE: We need to subtract one from the offset so
2562 * that we are on a valid page!
2564 usbd_get_page(xfer->frbuffers, buf_offset - 1,
2565 &buf_res);
2566 temp = buf_res.physaddr & ~0xFFF;
2567 } else {
2568 td->sitd_bp[0] = 0;
2569 temp = 0;
2572 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
2573 tlen = *plen;
2574 if (tlen <= 188) {
2575 temp |= 1; /* T-count = 1, TP = ALL */
2576 tlen = 1;
2577 } else {
2578 tlen += 187;
2579 tlen /= 188;
2580 temp |= tlen; /* T-count = [1..6] */
2581 temp |= 8; /* TP = Begin */
2584 tlen += sa;
2586 if (tlen >= 8) {
2587 sb = 0;
2588 } else {
2589 sb = (1 << tlen);
2592 sa = (1 << sa);
2593 sa = (sb - sa) & 0x3F;
2594 sb = 0;
2595 } else {
2596 sb = (-(4 << sa)) & 0xFE;
2597 sa = (1 << sa) & 0x3F;
2600 sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
2601 EHCI_SITD_SET_CMASK(sb));
2603 td->sitd_bp[1] = htohc32(sc, temp);
2605 td->sitd_mask = htohc32(sc, sitd_mask);
2607 if (nframes == 0) {
2608 td->sitd_status = htohc32(sc,
2609 EHCI_SITD_IOC |
2610 EHCI_SITD_ACTIVE |
2611 EHCI_SITD_SET_LEN(*plen));
2612 } else {
2613 td->sitd_status = htohc32(sc,
2614 EHCI_SITD_ACTIVE |
2615 EHCI_SITD_SET_LEN(*plen));
2617 usb_pc_cpu_flush(td->page_cache);
2619 #ifdef USB_DEBUG
2620 if (ehcidebug > 15) {
2621 DPRINTF("FS-TD %d\n", nframes);
2622 ehci_dump_sitd(sc, td);
2624 #endif
2625 /* insert TD into schedule */
2626 EHCI_APPEND_FS_TD(td, *pp_last);
2627 pp_last++;
2629 plen++;
2630 td_last = td;
2631 td = td->obj_next;
2634 xfer->td_transfer_last = td_last;
2636 /* update isoc_next */
2637 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
2638 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2641 * We don't allow cancelling of the SPLIT transaction USB FULL
2642 * speed transfer, because it disturbs the bandwidth
2643 * computation algorithm.
2645 xfer->flags_int.can_cancel_immed = 0;
2648 static void
2649 ehci_device_isoc_fs_start(struct usb_xfer *xfer)
2652 * We don't allow cancelling of the SPLIT transaction USB FULL
2653 * speed transfer, because it disturbs the bandwidth
2654 * computation algorithm.
2656 xfer->flags_int.can_cancel_immed = 0;
2658 /* set a default timeout */
2659 if (xfer->timeout == 0)
2660 xfer->timeout = 500; /* ms */
2662 /* put transfer on interrupt queue */
2663 ehci_transfer_intr_enqueue(xfer);
2666 static const struct usb_pipe_methods ehci_device_isoc_fs_methods =
2668 .open = ehci_device_isoc_fs_open,
2669 .close = ehci_device_isoc_fs_close,
2670 .enter = ehci_device_isoc_fs_enter,
2671 .start = ehci_device_isoc_fs_start,
2674 /*------------------------------------------------------------------------*
2675 * ehci high speed isochronous support
2676 *------------------------------------------------------------------------*/
2677 static void
2678 ehci_device_isoc_hs_open(struct usb_xfer *xfer)
2680 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2681 ehci_itd_t *td;
2682 uint32_t temp;
2683 uint8_t ds;
2685 usb_hs_bandwidth_alloc(xfer);
2687 /* initialize all TD's */
2689 for (ds = 0; ds != 2; ds++) {
2691 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2693 /* set TD inactive */
2694 td->itd_status[0] = 0;
2695 td->itd_status[1] = 0;
2696 td->itd_status[2] = 0;
2697 td->itd_status[3] = 0;
2698 td->itd_status[4] = 0;
2699 td->itd_status[5] = 0;
2700 td->itd_status[6] = 0;
2701 td->itd_status[7] = 0;
2703 /* set endpoint and address */
2704 td->itd_bp[0] = htohc32(sc,
2705 EHCI_ITD_SET_ADDR(xfer->address) |
2706 EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
2708 temp =
2709 EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
2711 /* set direction */
2712 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
2713 temp |= EHCI_ITD_SET_DIR_IN;
2715 /* set maximum packet size */
2716 td->itd_bp[1] = htohc32(sc, temp);
2718 /* set transfer multiplier */
2719 td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
2721 usb_pc_cpu_flush(td->page_cache);
2726 static void
2727 ehci_device_isoc_hs_close(struct usb_xfer *xfer)
2729 ehci_device_done(xfer, USB_ERR_CANCELLED);
2731 /* bandwidth must be freed after device done */
2732 usb_hs_bandwidth_free(xfer);
2735 static void
2736 ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
2738 struct usb_page_search buf_res;
2739 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2740 ehci_itd_t *td;
2741 ehci_itd_t *td_last = NULL;
2742 ehci_itd_t **pp_last;
2743 bus_size_t page_addr;
2744 uint32_t *plen;
2745 uint32_t status;
2746 uint32_t buf_offset;
2747 uint32_t nframes;
2748 uint32_t itd_offset[8 + 1];
2749 uint8_t x;
2750 uint8_t td_no;
2751 uint8_t page_no;
2752 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
2754 #ifdef USB_DEBUG
2755 uint8_t once = 1;
2757 #endif
2759 DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n",
2760 xfer, xfer->endpoint->isoc_next, xfer->nframes, (int)shift);
2762 /* get the current frame index */
2764 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2767 * check if the frame index is within the window where the frames
2768 * will be inserted
2770 buf_offset = (nframes - xfer->endpoint->isoc_next) &
2771 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2773 if ((xfer->endpoint->is_synced == 0) ||
2774 (buf_offset < (((xfer->nframes << shift) + 7) / 8))) {
2776 * If there is data underflow or the pipe queue is empty we
2777 * schedule the transfer a few frames ahead of the current
2778 * frame position. Else two isochronous transfers might
2779 * overlap.
2781 xfer->endpoint->isoc_next = (nframes + 3) &
2782 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2783 xfer->endpoint->is_synced = 1;
2784 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2787 * compute how many milliseconds the insertion is ahead of the
2788 * current frame position:
2790 buf_offset = (xfer->endpoint->isoc_next - nframes) &
2791 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2794 * pre-compute when the isochronous transfer will be finished:
2796 xfer->isoc_time_complete =
2797 usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2798 (((xfer->nframes << shift) + 7) / 8);
2800 /* get the real number of frames */
2802 nframes = xfer->nframes;
2804 buf_offset = 0;
2805 td_no = 0;
2807 plen = xfer->frlengths;
2809 /* toggle the DMA set we are using */
2810 xfer->flags_int.curr_dma_set ^= 1;
2812 /* get next DMA set */
2813 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2814 xfer->td_transfer_first = td;
2816 pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
2818 /* store starting position */
2820 xfer->qh_pos = xfer->endpoint->isoc_next;
2822 while (nframes) {
2823 if (td == NULL) {
2824 panic("%s:%d: out of TD's\n",
2825 __func__, __LINE__);
2827 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2828 pp_last = &sc->sc_isoc_hs_p_last[0];
2830 /* range check */
2831 if (*plen > xfer->max_frame_size) {
2832 #ifdef USB_DEBUG
2833 if (once) {
2834 once = 0;
2835 kprintf("%s: frame length(%d) exceeds %d bytes "
2836 "(frame truncated)\n",
2837 __func__, *plen, xfer->max_frame_size);
2839 #endif
2840 *plen = xfer->max_frame_size;
2843 if (xfer->endpoint->usb_smask & (1 << td_no)) {
2844 status = (EHCI_ITD_SET_LEN(*plen) |
2845 EHCI_ITD_ACTIVE |
2846 EHCI_ITD_SET_PG(0));
2847 td->itd_status[td_no] = htohc32(sc, status);
2848 itd_offset[td_no] = buf_offset;
2849 buf_offset += *plen;
2850 plen++;
2851 nframes --;
2852 } else {
2853 td->itd_status[td_no] = 0; /* not active */
2854 itd_offset[td_no] = buf_offset;
2857 td_no++;
2859 if ((td_no == 8) || (nframes == 0)) {
2861 /* the rest of the transfers are not active, if any */
2862 for (x = td_no; x != 8; x++) {
2863 td->itd_status[x] = 0; /* not active */
2866 /* check if there is any data to be transferred */
2867 if (itd_offset[0] != buf_offset) {
2868 page_no = 0;
2869 itd_offset[td_no] = buf_offset;
2871 /* get first page offset */
2872 usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
2873 /* get page address */
2874 page_addr = buf_res.physaddr & ~0xFFF;
2875 /* update page address */
2876 td->itd_bp[0] &= htohc32(sc, 0xFFF);
2877 td->itd_bp[0] |= htohc32(sc, page_addr);
2879 for (x = 0; x != td_no; x++) {
2880 /* set page number and page offset */
2881 status = (EHCI_ITD_SET_PG(page_no) |
2882 (buf_res.physaddr & 0xFFF));
2883 td->itd_status[x] |= htohc32(sc, status);
2885 /* get next page offset */
2886 if (itd_offset[x + 1] == buf_offset) {
2888 * We subtract one so that
2889 * we don't go off the last
2890 * page!
2892 usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
2893 } else {
2894 usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
2897 /* check if we need a new page */
2898 if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
2899 /* new page needed */
2900 page_addr = buf_res.physaddr & ~0xFFF;
2901 if (page_no == 6) {
2902 panic("%s: too many pages\n", __func__);
2904 page_no++;
2905 /* update page address */
2906 td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2907 td->itd_bp[page_no] |= htohc32(sc, page_addr);
2911 /* set IOC bit if we are complete */
2912 if (nframes == 0) {
2913 td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
2915 usb_pc_cpu_flush(td->page_cache);
2916 #ifdef USB_DEBUG
2917 if (ehcidebug > 15) {
2918 DPRINTF("HS-TD %d\n", nframes);
2919 ehci_dump_itd(sc, td);
2921 #endif
2922 /* insert TD into schedule */
2923 EHCI_APPEND_HS_TD(td, *pp_last);
2924 pp_last++;
2926 td_no = 0;
2927 td_last = td;
2928 td = td->obj_next;
2932 xfer->td_transfer_last = td_last;
2934 /* update isoc_next */
2935 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
2936 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2939 static void
2940 ehci_device_isoc_hs_start(struct usb_xfer *xfer)
2942 /* put transfer on interrupt queue */
2943 ehci_transfer_intr_enqueue(xfer);
2946 static const struct usb_pipe_methods ehci_device_isoc_hs_methods =
2948 .open = ehci_device_isoc_hs_open,
2949 .close = ehci_device_isoc_hs_close,
2950 .enter = ehci_device_isoc_hs_enter,
2951 .start = ehci_device_isoc_hs_start,
2954 /*------------------------------------------------------------------------*
2955 * ehci root control support
2956 *------------------------------------------------------------------------*
2957 * Simulate a hardware hub by handling all the necessary requests.
2958 *------------------------------------------------------------------------*/
2960 static const
2961 struct usb_device_descriptor ehci_devd =
2963 sizeof(struct usb_device_descriptor),
2964 UDESC_DEVICE, /* type */
2965 {0x00, 0x02}, /* USB version */
2966 UDCLASS_HUB, /* class */
2967 UDSUBCLASS_HUB, /* subclass */
2968 UDPROTO_HSHUBSTT, /* protocol */
2969 64, /* max packet */
2970 {0}, {0}, {0x00, 0x01}, /* device id */
2971 1, 2, 0, /* string indicies */
2972 1 /* # of configurations */
2975 static const
2976 struct usb_device_qualifier ehci_odevd =
2978 sizeof(struct usb_device_qualifier),
2979 UDESC_DEVICE_QUALIFIER, /* type */
2980 {0x00, 0x02}, /* USB version */
2981 UDCLASS_HUB, /* class */
2982 UDSUBCLASS_HUB, /* subclass */
2983 UDPROTO_FSHUB, /* protocol */
2984 0, /* max packet */
2985 0, /* # of configurations */
2989 static const struct ehci_config_desc ehci_confd = {
2990 .confd = {
2991 .bLength = sizeof(struct usb_config_descriptor),
2992 .bDescriptorType = UDESC_CONFIG,
2993 .wTotalLength[0] = sizeof(ehci_confd),
2994 .bNumInterface = 1,
2995 .bConfigurationValue = 1,
2996 .iConfiguration = 0,
2997 .bmAttributes = UC_SELF_POWERED,
2998 .bMaxPower = 0 /* max power */
3000 .ifcd = {
3001 .bLength = sizeof(struct usb_interface_descriptor),
3002 .bDescriptorType = UDESC_INTERFACE,
3003 .bNumEndpoints = 1,
3004 .bInterfaceClass = UICLASS_HUB,
3005 .bInterfaceSubClass = UISUBCLASS_HUB,
3006 .bInterfaceProtocol = 0,
3008 .endpd = {
3009 .bLength = sizeof(struct usb_endpoint_descriptor),
3010 .bDescriptorType = UDESC_ENDPOINT,
3011 .bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
3012 .bmAttributes = UE_INTERRUPT,
3013 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
3014 .bInterval = 255,
3018 static const
3019 struct usb_hub_descriptor ehci_hubd =
3021 .bDescLength = 0, /* dynamic length */
3022 .bDescriptorType = UDESC_HUB,
3025 static void
3026 ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
3028 uint32_t port;
3029 uint32_t v;
3031 DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
3033 port = EHCI_PORTSC(index);
3034 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3035 EOWRITE4(sc, port, v | EHCI_PS_PO);
3038 static usb_error_t
3039 ehci_roothub_exec(struct usb_device *udev,
3040 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3042 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3043 const char *str_ptr;
3044 const void *ptr;
3045 uint32_t port;
3046 uint32_t v;
3047 uint16_t len;
3048 uint16_t i;
3049 uint16_t value;
3050 uint16_t index;
3051 usb_error_t err;
3053 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
3055 /* buffer reset */
3056 ptr = (const void *)&sc->sc_hub_desc;
3057 len = 0;
3058 err = 0;
3060 value = UGETW(req->wValue);
3061 index = UGETW(req->wIndex);
3063 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3064 "wValue=0x%04x wIndex=0x%04x\n",
3065 req->bmRequestType, req->bRequest,
3066 UGETW(req->wLength), value, index);
3068 #define C(x,y) ((x) | ((y) << 8))
3069 switch (C(req->bRequest, req->bmRequestType)) {
3070 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3071 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3072 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3074 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3075 * for the integrated root hub.
3077 break;
3078 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3079 len = 1;
3080 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3081 break;
3082 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3083 switch (value >> 8) {
3084 case UDESC_DEVICE:
3085 if ((value & 0xff) != 0) {
3086 err = USB_ERR_IOERROR;
3087 goto done;
3089 len = sizeof(ehci_devd);
3090 ptr = (const void *)&ehci_devd;
3091 break;
3093 * We can't really operate at another speed,
3094 * but the specification says we need this
3095 * descriptor:
3097 case UDESC_DEVICE_QUALIFIER:
3098 if ((value & 0xff) != 0) {
3099 err = USB_ERR_IOERROR;
3100 goto done;
3102 len = sizeof(ehci_odevd);
3103 ptr = (const void *)&ehci_odevd;
3104 break;
3106 case UDESC_CONFIG:
3107 if ((value & 0xff) != 0) {
3108 err = USB_ERR_IOERROR;
3109 goto done;
3111 len = sizeof(ehci_confd);
3112 ptr = (const void *)&ehci_confd;
3113 break;
3115 case UDESC_STRING:
3116 switch (value & 0xff) {
3117 case 0: /* Language table */
3118 str_ptr = "\001";
3119 break;
3121 case 1: /* Vendor */
3122 str_ptr = sc->sc_vendor;
3123 break;
3125 case 2: /* Product */
3126 str_ptr = "EHCI root HUB";
3127 break;
3129 default:
3130 str_ptr = "";
3131 break;
3134 len = usb_make_str_desc(
3135 sc->sc_hub_desc.temp,
3136 sizeof(sc->sc_hub_desc.temp),
3137 str_ptr);
3138 break;
3139 default:
3140 err = USB_ERR_IOERROR;
3141 goto done;
3143 break;
3144 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3145 len = 1;
3146 sc->sc_hub_desc.temp[0] = 0;
3147 break;
3148 case C(UR_GET_STATUS, UT_READ_DEVICE):
3149 len = 2;
3150 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3151 break;
3152 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3153 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3154 len = 2;
3155 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3156 break;
3157 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3158 if (value >= EHCI_MAX_DEVICES) {
3159 err = USB_ERR_IOERROR;
3160 goto done;
3162 sc->sc_addr = value;
3163 break;
3164 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3165 if ((value != 0) && (value != 1)) {
3166 err = USB_ERR_IOERROR;
3167 goto done;
3169 sc->sc_conf = value;
3170 break;
3171 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3172 break;
3173 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3174 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3175 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3176 err = USB_ERR_IOERROR;
3177 goto done;
3178 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3179 break;
3180 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3181 break;
3182 /* Hub requests */
3183 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3184 break;
3185 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3186 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3188 if ((index < 1) ||
3189 (index > sc->sc_noport)) {
3190 err = USB_ERR_IOERROR;
3191 goto done;
3193 port = EHCI_PORTSC(index);
3194 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3195 switch (value) {
3196 case UHF_PORT_ENABLE:
3197 EOWRITE4(sc, port, v & ~EHCI_PS_PE);
3198 break;
3199 case UHF_PORT_SUSPEND:
3200 if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
3203 * waking up a High Speed device is rather
3204 * complicated if
3206 EOWRITE4(sc, port, v | EHCI_PS_FPR);
3208 /* wait 20ms for resume sequence to complete */
3209 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 50);
3211 EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
3212 EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
3214 /* 4ms settle time */
3215 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 250);
3216 break;
3217 case UHF_PORT_POWER:
3218 EOWRITE4(sc, port, v & ~EHCI_PS_PP);
3219 break;
3220 case UHF_PORT_TEST:
3221 DPRINTFN(3, "clear port test "
3222 "%d\n", index);
3223 break;
3224 case UHF_PORT_INDICATOR:
3225 DPRINTFN(3, "clear port ind "
3226 "%d\n", index);
3227 EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
3228 break;
3229 case UHF_C_PORT_CONNECTION:
3230 EOWRITE4(sc, port, v | EHCI_PS_CSC);
3231 break;
3232 case UHF_C_PORT_ENABLE:
3233 EOWRITE4(sc, port, v | EHCI_PS_PEC);
3234 break;
3235 case UHF_C_PORT_SUSPEND:
3236 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3237 break;
3238 case UHF_C_PORT_OVER_CURRENT:
3239 EOWRITE4(sc, port, v | EHCI_PS_OCC);
3240 break;
3241 case UHF_C_PORT_RESET:
3242 sc->sc_isreset = 0;
3243 break;
3244 default:
3245 err = USB_ERR_IOERROR;
3246 goto done;
3248 break;
3249 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3250 if ((value & 0xff) != 0) {
3251 err = USB_ERR_IOERROR;
3252 goto done;
3254 v = EREAD4(sc, EHCI_HCSPARAMS);
3256 sc->sc_hub_desc.hubd = ehci_hubd;
3257 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3259 if (EHCI_HCS_PPC(v))
3260 i = UHD_PWR_INDIVIDUAL;
3261 else
3262 i = UHD_PWR_NO_SWITCH;
3264 if (EHCI_HCS_P_INDICATOR(v))
3265 i |= UHD_PORT_IND;
3267 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3268 /* XXX can't find out? */
3269 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
3270 /* XXX don't know if ports are removable or not */
3271 sc->sc_hub_desc.hubd.bDescLength =
3272 8 + ((sc->sc_noport + 7) / 8);
3273 len = sc->sc_hub_desc.hubd.bDescLength;
3274 break;
3275 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3276 len = 16;
3277 memset(sc->sc_hub_desc.temp, 0, 16);
3278 break;
3279 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3280 DPRINTFN(9, "get port status i=%d\n",
3281 index);
3282 if ((index < 1) ||
3283 (index > sc->sc_noport)) {
3284 err = USB_ERR_IOERROR;
3285 goto done;
3287 v = EOREAD4(sc, EHCI_PORTSC(index));
3288 DPRINTFN(9, "port status=0x%04x\n", v);
3289 if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
3290 if ((v & 0xc000000) == 0x8000000)
3291 i = UPS_HIGH_SPEED;
3292 else if ((v & 0xc000000) == 0x4000000)
3293 i = UPS_LOW_SPEED;
3294 else
3295 i = 0;
3296 } else {
3297 i = UPS_HIGH_SPEED;
3299 if (v & EHCI_PS_CS)
3300 i |= UPS_CURRENT_CONNECT_STATUS;
3301 if (v & EHCI_PS_PE)
3302 i |= UPS_PORT_ENABLED;
3303 if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
3304 i |= UPS_SUSPEND;
3305 if (v & EHCI_PS_OCA)
3306 i |= UPS_OVERCURRENT_INDICATOR;
3307 if (v & EHCI_PS_PR)
3308 i |= UPS_RESET;
3309 if (v & EHCI_PS_PP)
3310 i |= UPS_PORT_POWER;
3311 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3312 i = 0;
3313 if (v & EHCI_PS_CSC)
3314 i |= UPS_C_CONNECT_STATUS;
3315 if (v & EHCI_PS_PEC)
3316 i |= UPS_C_PORT_ENABLED;
3317 if (v & EHCI_PS_OCC)
3318 i |= UPS_C_OVERCURRENT_INDICATOR;
3319 if (v & EHCI_PS_FPR)
3320 i |= UPS_C_SUSPEND;
3321 if (sc->sc_isreset)
3322 i |= UPS_C_PORT_RESET;
3323 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3324 len = sizeof(sc->sc_hub_desc.ps);
3325 break;
3326 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3327 err = USB_ERR_IOERROR;
3328 goto done;
3329 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3330 break;
3331 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3332 if ((index < 1) ||
3333 (index > sc->sc_noport)) {
3334 err = USB_ERR_IOERROR;
3335 goto done;
3337 port = EHCI_PORTSC(index);
3338 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3339 switch (value) {
3340 case UHF_PORT_ENABLE:
3341 EOWRITE4(sc, port, v | EHCI_PS_PE);
3342 break;
3343 case UHF_PORT_SUSPEND:
3344 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3345 break;
3346 case UHF_PORT_RESET:
3347 DPRINTFN(6, "reset port %d\n", index);
3348 #ifdef USB_DEBUG
3349 if (ehcinohighspeed) {
3351 * Connect USB device to companion
3352 * controller.
3354 ehci_disown(sc, index, 1);
3355 break;
3357 #endif
3358 if (EHCI_PS_IS_LOWSPEED(v) &&
3359 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3360 /* Low speed device, give up ownership. */
3361 ehci_disown(sc, index, 1);
3362 break;
3364 /* Start reset sequence. */
3365 v &= ~(EHCI_PS_PE | EHCI_PS_PR);
3366 EOWRITE4(sc, port, v | EHCI_PS_PR);
3368 /* Wait for reset to complete. */
3369 usb_pause_mtx(&sc->sc_bus.bus_lock,
3370 USB_MS_TO_TICKS(usb_port_root_reset_delay));
3372 /* Terminate reset sequence. */
3373 if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
3374 EOWRITE4(sc, port, v);
3376 /* Wait for HC to complete reset. */
3377 usb_pause_mtx(&sc->sc_bus.bus_lock,
3378 USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
3380 v = EOREAD4(sc, port);
3381 DPRINTF("ehci after reset, status=0x%08x\n", v);
3382 if (v & EHCI_PS_PR) {
3383 device_printf(sc->sc_bus.bdev,
3384 "port reset timeout\n");
3385 err = USB_ERR_TIMEOUT;
3386 goto done;
3388 if (!(v & EHCI_PS_PE) &&
3389 (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3390 /* Not a high speed device, give up ownership.*/
3391 ehci_disown(sc, index, 0);
3392 break;
3394 sc->sc_isreset = 1;
3395 DPRINTF("ehci port %d reset, status = 0x%08x\n",
3396 index, v);
3397 break;
3399 case UHF_PORT_POWER:
3400 DPRINTFN(3, "set port power %d\n", index);
3401 EOWRITE4(sc, port, v | EHCI_PS_PP);
3402 break;
3404 case UHF_PORT_TEST:
3405 DPRINTFN(3, "set port test %d\n", index);
3406 break;
3408 case UHF_PORT_INDICATOR:
3409 DPRINTFN(3, "set port ind %d\n", index);
3410 EOWRITE4(sc, port, v | EHCI_PS_PIC);
3411 break;
3413 default:
3414 err = USB_ERR_IOERROR;
3415 goto done;
3417 break;
3418 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3419 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3420 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3421 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3422 break;
3423 default:
3424 err = USB_ERR_IOERROR;
3425 goto done;
3427 done:
3428 *plength = len;
3429 *pptr = ptr;
3430 return (err);
3433 static void
3434 ehci_xfer_setup(struct usb_setup_params *parm)
3436 struct usb_page_search page_info;
3437 struct usb_page_cache *pc;
3438 ehci_softc_t *sc;
3439 struct usb_xfer *xfer;
3440 void *last_obj;
3441 uint32_t nqtd;
3442 uint32_t nqh;
3443 uint32_t nsitd;
3444 uint32_t nitd;
3445 uint32_t n;
3447 sc = EHCI_BUS2SC(parm->udev->bus);
3448 xfer = parm->curr_xfer;
3450 nqtd = 0;
3451 nqh = 0;
3452 nsitd = 0;
3453 nitd = 0;
3456 * compute maximum number of some structures
3458 if (parm->methods == &ehci_device_ctrl_methods) {
3461 * The proof for the "nqtd" formula is illustrated like
3462 * this:
3464 * +------------------------------------+
3465 * | |
3466 * | |remainder -> |
3467 * | +-----+---+ |
3468 * | | xxx | x | frm 0 |
3469 * | +-----+---++ |
3470 * | | xxx | xx | frm 1 |
3471 * | +-----+----+ |
3472 * | ... |
3473 * +------------------------------------+
3475 * "xxx" means a completely full USB transfer descriptor
3477 * "x" and "xx" means a short USB packet
3479 * For the remainder of an USB transfer modulo
3480 * "max_data_length" we need two USB transfer descriptors.
3481 * One to transfer the remaining data and one to finalise
3482 * with a zero length packet in case the "force_short_xfer"
3483 * flag is set. We only need two USB transfer descriptors in
3484 * the case where the transfer length of the first one is a
3485 * factor of "max_frame_size". The rest of the needed USB
3486 * transfer descriptors is given by the buffer size divided
3487 * by the maximum data payload.
3489 parm->hc_max_packet_size = 0x400;
3490 parm->hc_max_packet_count = 1;
3491 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3492 xfer->flags_int.bdma_enable = 1;
3494 usbd_transfer_setup_sub(parm);
3496 nqh = 1;
3497 nqtd = ((2 * xfer->nframes) + 1 /* STATUS */
3498 + (xfer->max_data_length / xfer->max_hc_frame_size));
3500 } else if (parm->methods == &ehci_device_bulk_methods) {
3502 parm->hc_max_packet_size = 0x400;
3503 parm->hc_max_packet_count = 1;
3504 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3505 xfer->flags_int.bdma_enable = 1;
3507 usbd_transfer_setup_sub(parm);
3509 nqh = 1;
3510 nqtd = ((2 * xfer->nframes)
3511 + (xfer->max_data_length / xfer->max_hc_frame_size));
3513 } else if (parm->methods == &ehci_device_intr_methods) {
3515 if (parm->speed == USB_SPEED_HIGH) {
3516 parm->hc_max_packet_size = 0x400;
3517 parm->hc_max_packet_count = 3;
3518 } else if (parm->speed == USB_SPEED_FULL) {
3519 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
3520 parm->hc_max_packet_count = 1;
3521 } else {
3522 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
3523 parm->hc_max_packet_count = 1;
3526 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3527 xfer->flags_int.bdma_enable = 1;
3529 usbd_transfer_setup_sub(parm);
3531 nqh = 1;
3532 nqtd = ((2 * xfer->nframes)
3533 + (xfer->max_data_length / xfer->max_hc_frame_size));
3535 } else if (parm->methods == &ehci_device_isoc_fs_methods) {
3537 parm->hc_max_packet_size = 0x3FF;
3538 parm->hc_max_packet_count = 1;
3539 parm->hc_max_frame_size = 0x3FF;
3540 xfer->flags_int.bdma_enable = 1;
3542 usbd_transfer_setup_sub(parm);
3544 nsitd = xfer->nframes;
3546 } else if (parm->methods == &ehci_device_isoc_hs_methods) {
3548 parm->hc_max_packet_size = 0x400;
3549 parm->hc_max_packet_count = 3;
3550 parm->hc_max_frame_size = 0xC00;
3551 xfer->flags_int.bdma_enable = 1;
3553 usbd_transfer_setup_sub(parm);
3555 nitd = ((xfer->nframes + 7) / 8) <<
3556 usbd_xfer_get_fps_shift(xfer);
3558 } else {
3560 parm->hc_max_packet_size = 0x400;
3561 parm->hc_max_packet_count = 1;
3562 parm->hc_max_frame_size = 0x400;
3564 usbd_transfer_setup_sub(parm);
3567 alloc_dma_set:
3569 if (parm->err) {
3570 return;
3573 * Allocate queue heads and transfer descriptors
3575 last_obj = NULL;
3577 if (usbd_transfer_setup_sub_malloc(
3578 parm, &pc, sizeof(ehci_itd_t),
3579 EHCI_ITD_ALIGN, nitd)) {
3580 parm->err = USB_ERR_NOMEM;
3581 return;
3583 if (parm->buf) {
3584 for (n = 0; n != nitd; n++) {
3585 ehci_itd_t *td;
3587 usbd_get_page(pc + n, 0, &page_info);
3589 td = page_info.buffer;
3591 /* init TD */
3592 td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
3593 td->obj_next = last_obj;
3594 td->page_cache = pc + n;
3596 last_obj = td;
3598 usb_pc_cpu_flush(pc + n);
3601 if (usbd_transfer_setup_sub_malloc(
3602 parm, &pc, sizeof(ehci_sitd_t),
3603 EHCI_SITD_ALIGN, nsitd)) {
3604 parm->err = USB_ERR_NOMEM;
3605 return;
3607 if (parm->buf) {
3608 for (n = 0; n != nsitd; n++) {
3609 ehci_sitd_t *td;
3611 usbd_get_page(pc + n, 0, &page_info);
3613 td = page_info.buffer;
3615 /* init TD */
3616 td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
3617 td->obj_next = last_obj;
3618 td->page_cache = pc + n;
3620 last_obj = td;
3622 usb_pc_cpu_flush(pc + n);
3625 if (usbd_transfer_setup_sub_malloc(
3626 parm, &pc, sizeof(ehci_qtd_t),
3627 EHCI_QTD_ALIGN, nqtd)) {
3628 parm->err = USB_ERR_NOMEM;
3629 return;
3631 if (parm->buf) {
3632 for (n = 0; n != nqtd; n++) {
3633 ehci_qtd_t *qtd;
3635 usbd_get_page(pc + n, 0, &page_info);
3637 qtd = page_info.buffer;
3639 /* init TD */
3640 qtd->qtd_self = htohc32(sc, page_info.physaddr);
3641 qtd->obj_next = last_obj;
3642 qtd->page_cache = pc + n;
3644 last_obj = qtd;
3646 usb_pc_cpu_flush(pc + n);
3649 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3651 last_obj = NULL;
3653 if (usbd_transfer_setup_sub_malloc(
3654 parm, &pc, sizeof(ehci_qh_t),
3655 EHCI_QH_ALIGN, nqh)) {
3656 parm->err = USB_ERR_NOMEM;
3657 return;
3659 if (parm->buf) {
3660 for (n = 0; n != nqh; n++) {
3661 ehci_qh_t *qh;
3663 usbd_get_page(pc + n, 0, &page_info);
3665 qh = page_info.buffer;
3667 /* init QH */
3668 qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
3669 qh->obj_next = last_obj;
3670 qh->page_cache = pc + n;
3672 last_obj = qh;
3674 usb_pc_cpu_flush(pc + n);
3677 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3679 if (!xfer->flags_int.curr_dma_set) {
3680 xfer->flags_int.curr_dma_set = 1;
3681 goto alloc_dma_set;
3685 static void
3686 ehci_xfer_unsetup(struct usb_xfer *xfer)
3688 return;
3691 static void
3692 ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3693 struct usb_endpoint *ep)
3695 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3697 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3698 ep, udev->address,
3699 edesc->bEndpointAddress, udev->flags.usb_mode,
3700 sc->sc_addr);
3702 if (udev->device_index != sc->sc_addr) {
3704 if ((udev->speed != USB_SPEED_HIGH) &&
3705 ((udev->hs_hub_addr == 0) ||
3706 (udev->hs_port_no == 0) ||
3707 (udev->parent_hs_hub == NULL) ||
3708 (udev->parent_hs_hub->hub == NULL))) {
3709 /* We need a transaction translator */
3710 goto done;
3712 switch (edesc->bmAttributes & UE_XFERTYPE) {
3713 case UE_CONTROL:
3714 ep->methods = &ehci_device_ctrl_methods;
3715 break;
3716 case UE_INTERRUPT:
3717 ep->methods = &ehci_device_intr_methods;
3718 break;
3719 case UE_ISOCHRONOUS:
3720 if (udev->speed == USB_SPEED_HIGH) {
3721 ep->methods = &ehci_device_isoc_hs_methods;
3722 } else if (udev->speed == USB_SPEED_FULL) {
3723 ep->methods = &ehci_device_isoc_fs_methods;
3725 break;
3726 case UE_BULK:
3727 ep->methods = &ehci_device_bulk_methods;
3728 break;
3729 default:
3730 /* do nothing */
3731 break;
3734 done:
3735 return;
3738 static void
3739 ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3742 * Wait until the hardware has finished any possible use of
3743 * the transfer descriptor(s) and QH
3745 *pus = (188); /* microseconds */
3748 static void
3749 ehci_device_resume(struct usb_device *udev)
3751 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3752 struct usb_xfer *xfer;
3753 const struct usb_pipe_methods *methods;
3755 DPRINTF("\n");
3757 USB_BUS_LOCK(udev->bus);
3759 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3761 if (xfer->xroot->udev == udev) {
3763 methods = xfer->endpoint->methods;
3765 if ((methods == &ehci_device_bulk_methods) ||
3766 (methods == &ehci_device_ctrl_methods)) {
3767 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3768 sc->sc_async_p_last);
3770 if (methods == &ehci_device_intr_methods) {
3771 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3772 sc->sc_intr_p_last[xfer->qh_pos]);
3777 USB_BUS_UNLOCK(udev->bus);
3779 return;
3782 static void
3783 ehci_device_suspend(struct usb_device *udev)
3785 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3786 struct usb_xfer *xfer;
3787 const struct usb_pipe_methods *methods;
3789 DPRINTF("\n");
3791 USB_BUS_LOCK(udev->bus);
3793 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3795 if (xfer->xroot->udev == udev) {
3797 methods = xfer->endpoint->methods;
3799 if ((methods == &ehci_device_bulk_methods) ||
3800 (methods == &ehci_device_ctrl_methods)) {
3801 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3802 sc->sc_async_p_last);
3804 if (methods == &ehci_device_intr_methods) {
3805 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3806 sc->sc_intr_p_last[xfer->qh_pos]);
3811 USB_BUS_UNLOCK(udev->bus);
3814 static void
3815 ehci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3817 struct ehci_softc *sc = EHCI_BUS2SC(bus);
3819 switch (state) {
3820 case USB_HW_POWER_SUSPEND:
3821 case USB_HW_POWER_SHUTDOWN:
3822 ehci_suspend(sc);
3823 break;
3824 case USB_HW_POWER_RESUME:
3825 ehci_resume(sc);
3826 break;
3827 default:
3828 break;
3832 static void
3833 ehci_set_hw_power(struct usb_bus *bus)
3835 ehci_softc_t *sc = EHCI_BUS2SC(bus);
3836 uint32_t temp;
3837 uint32_t flags;
3839 DPRINTF("\n");
3841 USB_BUS_LOCK(bus);
3843 flags = bus->hw_power_state;
3845 temp = EOREAD4(sc, EHCI_USBCMD);
3847 temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
3849 if (flags & (USB_HW_POWER_CONTROL |
3850 USB_HW_POWER_BULK)) {
3851 DPRINTF("Async is active\n");
3852 temp |= EHCI_CMD_ASE;
3854 if (flags & (USB_HW_POWER_INTERRUPT |
3855 USB_HW_POWER_ISOC)) {
3856 DPRINTF("Periodic is active\n");
3857 temp |= EHCI_CMD_PSE;
3859 EOWRITE4(sc, EHCI_USBCMD, temp);
3861 USB_BUS_UNLOCK(bus);
3863 return;
3866 static const struct usb_bus_methods ehci_bus_methods =
3868 .endpoint_init = ehci_ep_init,
3869 .xfer_setup = ehci_xfer_setup,
3870 .xfer_unsetup = ehci_xfer_unsetup,
3871 .get_dma_delay = ehci_get_dma_delay,
3872 .device_resume = ehci_device_resume,
3873 .device_suspend = ehci_device_suspend,
3874 .set_hw_power = ehci_set_hw_power,
3875 .set_hw_power_sleep = ehci_set_hw_power_sleep,
3876 .roothub_exec = ehci_roothub_exec,
3877 .xfer_poll = ehci_do_poll,