2 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
3 * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 2004 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
32 * The EHCI 0.96 spec can be found at
33 * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
34 * The EHCI 1.0 spec can be found at
35 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
36 * and the USB 2.0 spec at
37 * http://www.usb.org/developers/docs/usb_20.zip
43 * 1) command failures are not recovered correctly
46 #include <sys/stdint.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/types.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/module.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
62 #include <bus/u4b/usb.h>
63 #include <bus/u4b/usbdi.h>
65 #define USB_DEBUG_VAR ehcidebug
67 #include <bus/u4b/usb_core.h>
68 #include <bus/u4b/usb_debug.h>
69 #include <bus/u4b/usb_busdma.h>
70 #include <bus/u4b/usb_process.h>
71 #include <bus/u4b/usb_transfer.h>
72 #include <bus/u4b/usb_device.h>
73 #include <bus/u4b/usb_hub.h>
74 #include <bus/u4b/usb_util.h>
76 #include <bus/u4b/usb_controller.h>
77 #include <bus/u4b/usb_bus.h>
78 #include <bus/u4b/controller/ehci.h>
79 #include <bus/u4b/controller/ehcireg.h>
81 #define EHCI_BUS2SC(bus) \
82 ((ehci_softc_t *)(((uint8_t *)(bus)) - \
83 ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
86 static int ehcidebug
= 0;
87 static int ehcinohighspeed
= 0;
88 static int ehciiaadbug
= 0;
89 static int ehcilostintrbug
= 0;
91 static SYSCTL_NODE(_hw_usb
, OID_AUTO
, ehci
, CTLFLAG_RW
, 0, "USB ehci");
92 SYSCTL_INT(_hw_usb_ehci
, OID_AUTO
, debug
, CTLFLAG_RW
,
93 &ehcidebug
, 0, "Debug level");
94 SYSCTL_INT(_hw_usb_ehci
, OID_AUTO
, no_hs
, CTLFLAG_RW
,
95 &ehcinohighspeed
, 0, "Disable High Speed USB");
96 SYSCTL_INT(_hw_usb_ehci
, OID_AUTO
, iaadbug
, CTLFLAG_RW
,
97 &ehciiaadbug
, 0, "Enable doorbell bug workaround");
98 SYSCTL_INT(_hw_usb_ehci
, OID_AUTO
, lostintrbug
, CTLFLAG_RW
,
99 &ehcilostintrbug
, 0, "Enable lost interrupt bug workaround");
101 TUNABLE_INT("hw.usb.ehci.debug", &ehcidebug
);
102 TUNABLE_INT("hw.usb.ehci.no_hs", &ehcinohighspeed
);
103 TUNABLE_INT("hw.usb.ehci.iaadbug", &ehciiaadbug
);
104 TUNABLE_INT("hw.usb.ehci.lostintrbug", &ehcilostintrbug
);
106 static void ehci_dump_regs(ehci_softc_t
*sc
);
107 static void ehci_dump_sqh(ehci_softc_t
*sc
, ehci_qh_t
*sqh
);
111 #define EHCI_INTR_ENDPT 1
113 static const struct usb_bus_methods ehci_bus_methods
;
114 static const struct usb_pipe_methods ehci_device_bulk_methods
;
115 static const struct usb_pipe_methods ehci_device_ctrl_methods
;
116 static const struct usb_pipe_methods ehci_device_intr_methods
;
117 static const struct usb_pipe_methods ehci_device_isoc_fs_methods
;
118 static const struct usb_pipe_methods ehci_device_isoc_hs_methods
;
120 static void ehci_do_poll(struct usb_bus
*);
121 static void ehci_device_done(struct usb_xfer
*, usb_error_t
);
122 static uint8_t ehci_check_transfer(struct usb_xfer
*);
123 static void ehci_timeout(void *);
124 static void ehci_poll_timeout(void *);
126 static void ehci_root_intr(ehci_softc_t
*sc
);
128 struct ehci_std_temp
{
130 struct usb_page_cache
*pc
;
136 uint16_t max_frame_size
;
138 uint8_t auto_data_toggle
;
139 uint8_t setup_alt_next
;
144 ehci_iterate_hw_softc(struct usb_bus
*bus
, usb_bus_mem_sub_cb_t
*cb
)
146 ehci_softc_t
*sc
= EHCI_BUS2SC(bus
);
149 cb(bus
, &sc
->sc_hw
.pframes_pc
, &sc
->sc_hw
.pframes_pg
,
150 sizeof(uint32_t) * EHCI_FRAMELIST_COUNT
, EHCI_FRAMELIST_ALIGN
);
152 cb(bus
, &sc
->sc_hw
.terminate_pc
, &sc
->sc_hw
.terminate_pg
,
153 sizeof(struct ehci_qh_sub
), EHCI_QH_ALIGN
);
155 cb(bus
, &sc
->sc_hw
.async_start_pc
, &sc
->sc_hw
.async_start_pg
,
156 sizeof(ehci_qh_t
), EHCI_QH_ALIGN
);
158 for (i
= 0; i
!= EHCI_VIRTUAL_FRAMELIST_COUNT
; i
++) {
159 cb(bus
, sc
->sc_hw
.intr_start_pc
+ i
,
160 sc
->sc_hw
.intr_start_pg
+ i
,
161 sizeof(ehci_qh_t
), EHCI_QH_ALIGN
);
164 for (i
= 0; i
!= EHCI_VIRTUAL_FRAMELIST_COUNT
; i
++) {
165 cb(bus
, sc
->sc_hw
.isoc_hs_start_pc
+ i
,
166 sc
->sc_hw
.isoc_hs_start_pg
+ i
,
167 sizeof(ehci_itd_t
), EHCI_ITD_ALIGN
);
170 for (i
= 0; i
!= EHCI_VIRTUAL_FRAMELIST_COUNT
; i
++) {
171 cb(bus
, sc
->sc_hw
.isoc_fs_start_pc
+ i
,
172 sc
->sc_hw
.isoc_fs_start_pg
+ i
,
173 sizeof(ehci_sitd_t
), EHCI_SITD_ALIGN
);
178 ehci_reset(ehci_softc_t
*sc
)
183 EOWRITE4(sc
, EHCI_USBCMD
, EHCI_CMD_HCRESET
);
184 for (i
= 0; i
< 100; i
++) {
185 usb_pause_mtx(NULL
, hz
/ 128);
186 hcr
= EOREAD4(sc
, EHCI_USBCMD
) & EHCI_CMD_HCRESET
;
188 if (sc
->sc_flags
& (EHCI_SCFLG_SETMODE
| EHCI_SCFLG_BIGEMMIO
)) {
190 * Force USBMODE as requested. Controllers
191 * may have multiple operating modes.
193 uint32_t usbmode
= EOREAD4(sc
, EHCI_USBMODE
);
194 if (sc
->sc_flags
& EHCI_SCFLG_SETMODE
) {
195 usbmode
= (usbmode
&~ EHCI_UM_CM
) | EHCI_UM_CM_HOST
;
196 device_printf(sc
->sc_bus
.bdev
,
197 "set host controller mode\n");
199 if (sc
->sc_flags
& EHCI_SCFLG_BIGEMMIO
) {
200 usbmode
= (usbmode
&~ EHCI_UM_ES
) | EHCI_UM_ES_BE
;
201 device_printf(sc
->sc_bus
.bdev
,
202 "set big-endian mode\n");
204 EOWRITE4(sc
, EHCI_USBMODE
, usbmode
);
209 device_printf(sc
->sc_bus
.bdev
, "Reset timeout\n");
210 return (USB_ERR_IOERROR
);
214 ehci_hcreset(ehci_softc_t
*sc
)
219 EOWRITE4(sc
, EHCI_USBCMD
, 0); /* Halt controller */
220 for (i
= 0; i
< 100; i
++) {
221 usb_pause_mtx(NULL
, hz
/ 128);
222 hcr
= EOREAD4(sc
, EHCI_USBSTS
) & EHCI_STS_HCH
;
228 * Fall through and try reset anyway even though
229 * Table 2-9 in the EHCI spec says this will result
230 * in undefined behavior.
232 device_printf(sc
->sc_bus
.bdev
, "stop timeout\n");
234 return (ehci_reset(sc
));
238 ehci_init_sub(struct ehci_softc
*sc
)
240 struct usb_page_search buf_res
;
245 cparams
= EREAD4(sc
, EHCI_HCCPARAMS
);
247 DPRINTF("cparams=0x%x\n", cparams
);
249 if (EHCI_HCC_64BIT(cparams
)) {
250 DPRINTF("HCC uses 64-bit structures\n");
252 /* MUST clear segment register if 64 bit capable */
253 EOWRITE4(sc
, EHCI_CTRLDSSEGMENT
, 0);
256 usbd_get_page(&sc
->sc_hw
.pframes_pc
, 0, &buf_res
);
257 EOWRITE4(sc
, EHCI_PERIODICLISTBASE
, buf_res
.physaddr
);
259 usbd_get_page(&sc
->sc_hw
.async_start_pc
, 0, &buf_res
);
260 EOWRITE4(sc
, EHCI_ASYNCLISTADDR
, buf_res
.physaddr
| EHCI_LINK_QH
);
262 /* enable interrupts */
263 EOWRITE4(sc
, EHCI_USBINTR
, sc
->sc_eintrs
);
265 /* turn on controller */
266 EOWRITE4(sc
, EHCI_USBCMD
,
267 EHCI_CMD_ITC_1
| /* 1 microframes interrupt delay */
268 (EOREAD4(sc
, EHCI_USBCMD
) & EHCI_CMD_FLS_M
) |
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
;
284 device_printf(sc
->sc_bus
.bdev
, "Run timeout\n");
285 return (USB_ERR_IOERROR
);
287 return (USB_ERR_NORMAL_COMPLETION
);
291 ehci_init(ehci_softc_t
*sc
)
293 struct usb_page_search buf_res
;
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
));
311 sc
->sc_flags
|= EHCI_SCFLG_IAADBUG
;
313 sc
->sc_flags
|= EHCI_SCFLG_LOSTINTRBUG
;
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
);
336 device_printf(sc
->sc_bus
.bdev
, "reset timeout\n");
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
;
355 struct ehci_qh_sub
*qh
;
357 usbd_get_page(&sc
->sc_hw
.terminate_pc
, 0, &buf_res
);
361 sc
->sc_terminate_self
= htohc32(sc
, buf_res
.physaddr
);
363 /* init terminate TD */
365 htohc32(sc
, EHCI_LINK_TERMINATE
);
367 htohc32(sc
, EHCI_LINK_TERMINATE
);
369 htohc32(sc
, EHCI_QTD_HALTED
);
372 for (i
= 0; i
< EHCI_VIRTUAL_FRAMELIST_COUNT
; i
++) {
375 usbd_get_page(sc
->sc_hw
.intr_start_pc
+ i
, 0, &buf_res
);
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
;
388 htohc32(sc
, buf_res
.physaddr
) |
389 htohc32(sc
, EHCI_LINK_QH
);
392 htohc32(sc
, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH
));
394 htohc32(sc
, EHCI_QH_SET_MULT(1));
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;
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
;
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
++) {
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 */
458 htohc32(sc
, buf_res
.physaddr
) |
459 htohc32(sc
, EHCI_LINK_SITD
);
462 htohc32(sc
, EHCI_LINK_TERMINATE
);
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 */
483 htohc32(sc
, buf_res
.physaddr
) |
484 htohc32(sc
, EHCI_LINK_ITD
);
490 usbd_get_page(&sc
->sc_hw
.pframes_pc
, 0, &buf_res
);
495 pframes
= buf_res
.buffer
;
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
);
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 */
526 htohc32(sc
, buf_res
.physaddr
) |
527 htohc32(sc
, EHCI_LINK_QH
);
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
;
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
);
547 ehci_dump_sqh(sc
, sc
->sc_async_p_last
);
552 err
= ehci_init_sub(sc
);
555 /* catch any lost interrupts */
556 ehci_do_poll(&sc
->sc_bus
);
562 * shut down the controller when the system is going down
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
);
587 ehci_suspend(ehci_softc_t
*sc
)
589 DPRINTF("stopping the HC\n");
596 ehci_resume(ehci_softc_t
*sc
)
604 /* catch any lost interrupts */
605 ehci_do_poll(&sc
->sc_bus
);
610 ehci_dump_regs(ehci_softc_t
*sc
)
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");
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
)));
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
)
698 switch (EHCI_LINK_TYPE(link
)) {
718 ehci_dump_qtd(ehci_softc_t
*sc
, ehci_qtd_t
*qtd
)
723 ehci_dump_link(sc
, qtd
->qtd_next
, 0);
724 kprintf(" altnext=");
725 ehci_dump_link(sc
, qtd
->qtd_altnext
, 0);
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
]));
753 ehci_dump_sqtd(ehci_softc_t
*sc
, ehci_qtd_t
*sqtd
)
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;
765 ehci_dump_sqtds(ehci_softc_t
*sc
, ehci_qtd_t
*sqtd
)
771 for (i
= 0; sqtd
&& (i
< 20) && !stop
; sqtd
= sqtd
->obj_next
, i
++) {
772 stop
= ehci_dump_sqtd(sc
, sqtd
);
775 kprintf("dump aborted, too many TDs\n");
780 ehci_dump_sqh(ehci_softc_t
*sc
, ehci_qh_t
*qh
)
785 usb_pc_cpu_invalidate(qh
->page_cache
);
786 kprintf("QH(%p) at 0x%08x:\n", qh
, hc32toh(sc
, qh
->qh_self
) & ~0x1F);
788 ehci_dump_link(sc
, qh
->qh_link
, 1);
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
));
806 ehci_dump_link(sc
, qh
->qh_curqtd
, 0);
808 kprintf("Overlay qTD:\n");
809 ehci_dump_qtd(sc
, (void *)&qh
->qh_qtd
);
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
))
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]));
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]));
886 ehci_dump_isoc(ehci_softc_t
*sc
)
893 pos
= (EOREAD4(sc
, EHCI_FRINDEX
) / 8) &
894 (EHCI_VIRTUAL_FRAMELIST_COUNT
- 1);
896 kprintf("%s: isochronous dump from frame 0x%03x:\n",
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
);
907 while (sitd
&& max
&& max
--) {
908 ehci_dump_sitd(sc
, sitd
);
916 ehci_transfer_intr_enqueue(struct usb_xfer
*xfer
)
918 /* check for early completion */
919 if (ehci_check_transfer(xfer
)) {
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)
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
;
944 usb_pc_cpu_flush(std
->page_cache
);
947 * the last->next->prev is never followed: std->next->prev = std;
950 last
->sitd_next
= std
->sitd_self
;
952 usb_pc_cpu_flush(last
->page_cache
);
957 #define EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
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
;
970 usb_pc_cpu_flush(std
->page_cache
);
973 * the last->next->prev is never followed: std->next->prev = std;
976 last
->itd_next
= std
->itd_self
;
978 usb_pc_cpu_flush(last
->page_cache
);
983 #define EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
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");
994 /* (sc->sc_bus.mtx) must be locked */
996 sqh
->next
= last
->next
;
997 sqh
->qh_link
= last
->qh_link
;
1001 usb_pc_cpu_flush(sqh
->page_cache
);
1004 * the last->next->prev is never followed: sqh->next->prev = sqh;
1008 last
->qh_link
= sqh
->qh_self
;
1010 usb_pc_cpu_flush(last
->page_cache
);
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
);
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)
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
);
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)
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 */
1066 sqh
->prev
->next
= sqh
->next
;
1067 sqh
->prev
->qh_link
= sqh
->qh_link
;
1069 usb_pc_cpu_flush(sqh
->prev
->page_cache
);
1072 sqh
->next
->prev
= sqh
->prev
;
1073 usb_pc_cpu_flush(sqh
->next
->page_cache
);
1075 last
= ((last
== sqh
) ? sqh
->prev
: last
);
1079 usb_pc_cpu_flush(sqh
->page_cache
);
1085 ehci_data_toggle_update(struct usb_xfer
*xfer
, uint16_t actlen
, uint16_t xlen
)
1090 /* count number of full packets */
1091 dt
= (actlen
/ xfer
->max_packet_size
) & 1;
1093 /* compute remainder */
1094 rem
= actlen
% xfer
->max_packet_size
;
1097 dt
^= 1; /* short packet at the end */
1098 else if (actlen
!= xlen
)
1099 dt
^= 1; /* zero length packet at the end */
1101 dt
^= 1; /* zero length transfer */
1103 xfer
->endpoint
->toggle_next
^= dt
;
1107 ehci_non_isoc_done_sub(struct usb_xfer
*xfer
)
1109 ehci_softc_t
*sc
= EHCI_BUS2SC(xfer
->xroot
->bus
);
1111 ehci_qtd_t
*td_alt_next
;
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);
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
) {
1148 /* Check for transfer error */
1149 if (status
& EHCI_QTD_HALTED
) {
1150 /* the transfer is finished */
1154 /* Check for short transfer */
1156 if (xfer
->flags_int
.short_frames_ok
) {
1157 /* follow alt next */
1160 /* the transfer is finished */
1167 if (td
->alt_next
!= td_alt_next
) {
1168 /* this USB frame is complete */
1173 /* update transfer cache */
1175 xfer
->td_transfer_cache
= td
;
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]" : "");
1193 return ((status
& EHCI_QTD_HALTED
) ?
1194 USB_ERR_STALLED
: USB_ERR_NORMAL_COMPLETION
);
1198 ehci_non_isoc_done(struct usb_xfer
*xfer
)
1200 ehci_softc_t
*sc
= EHCI_BUS2SC(xfer
->xroot
->bus
);
1203 usb_error_t err
= 0;
1205 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1206 xfer
, xfer
->endpoint
);
1209 if (ehcidebug
> 10) {
1210 ehci_softc_t
*sc
= EHCI_BUS2SC(xfer
->xroot
->bus
);
1212 ehci_dump_sqtds(sc
, xfer
->td_transfer_first
);
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
);
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
);
1236 if (xfer
->td_transfer_cache
== NULL
) {
1240 while (xfer
->aframes
!= xfer
->nframes
) {
1242 err
= ehci_non_isoc_done_sub(xfer
);
1245 if (xfer
->td_transfer_cache
== NULL
) {
1250 if (xfer
->flags_int
.control_xfr
&&
1251 !xfer
->flags_int
.control_act
) {
1253 err
= ehci_non_isoc_done_sub(xfer
);
1256 ehci_device_done(xfer
, err
);
1259 /*------------------------------------------------------------------------*
1260 * ehci_check_transfer
1263 * 0: USB transfer is not finished
1264 * Else: USB transfer is finished
1265 *------------------------------------------------------------------------*/
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
);
1274 DPRINTFN(13, "xfer=%p checking transfer\n", xfer
);
1276 if (methods
== &ehci_device_isoc_fs_methods
) {
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
);
1295 } else if (methods
== &ehci_device_isoc_hs_methods
) {
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
);
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 */
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
) {
1361 xfer
->td_transfer_cache
= td
;
1365 * last transfer descriptor makes the transfer done
1367 if (((void *)td
) == xfer
->td_transfer_last
) {
1371 * any kind of error makes the transfer done
1373 if (status
& EHCI_QTD_HALTED
) {
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 */
1388 /* transfer is done */
1393 ehci_non_isoc_done(xfer
);
1398 DPRINTFN(13, "xfer=%p is still active\n", xfer
);
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
);
1420 ehci_interrupt_poll(ehci_softc_t
*sc
)
1422 struct usb_xfer
*xfer
;
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 */
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
1450 ehci_poll_timeout(void *arg
)
1452 ehci_softc_t
*sc
= arg
;
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"
1464 *------------------------------------------------------------------------*/
1466 ehci_interrupt(ehci_softc_t
*sc
)
1470 USB_BUS_LOCK(&sc
->sc_bus
);
1472 DPRINTFN(16, "real interrupt\n");
1475 if (ehcidebug
> 15) {
1480 status
= EHCI_STS_INTRS(EOREAD4(sc
, EHCI_USBSTS
));
1482 /* the interrupt was not for us */
1485 if (!(status
& sc
->sc_eintrs
)) {
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__
);
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
);
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
);
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
);
1531 USB_BUS_UNLOCK(&sc
->sc_bus
);
1535 * called when a request does not complete
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
);
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
);
1561 ehci_setup_standard_chain_sub(struct ehci_std_temp
*temp
)
1563 struct usb_page_search buf_res
;
1565 ehci_qtd_t
*td_next
;
1566 ehci_qtd_t
*td_alt_next
;
1567 uint32_t buf_offset
;
1571 uint32_t qtd_altnext
;
1572 uint8_t shortpkt_old
;
1575 terminate
= temp
->sc
->sc_terminate_self
;
1576 qtd_altnext
= temp
->sc
->sc_terminate_self
;
1579 shortpkt_old
= temp
->shortpkt
;
1580 len_old
= temp
->len
;
1586 td_next
= temp
->td_next
;
1590 if (temp
->len
== 0) {
1592 if (temp
->shortpkt
) {
1595 /* send a Zero Length Packet, ZLP, last */
1602 average
= temp
->average
;
1604 if (temp
->len
< average
) {
1605 if (temp
->len
% temp
->max_frame_size
) {
1608 average
= temp
->len
;
1612 if (td_next
== NULL
) {
1613 panic("%s: out of EHCI transfer descriptors!", __func__
);
1618 td_next
= td
->obj_next
;
1620 /* check if we are pre-computing */
1624 /* update remaining length */
1626 temp
->len
-= average
;
1630 /* fill out current TD */
1634 htohc32(temp
->sc
, EHCI_QTD_IOC
|
1635 EHCI_QTD_SET_BYTES(average
));
1639 if (temp
->auto_data_toggle
== 0) {
1641 /* update data toggle, ZLP case */
1644 htohc32(temp
->sc
, EHCI_QTD_TOGGLE_MASK
);
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;
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) {
1665 htohc32(temp
->sc
, EHCI_QTD_TOGGLE_MASK
);
1670 /* update remaining length */
1672 temp
->len
-= average
;
1674 /* fill out buffer pointers */
1676 usbd_get_page(temp
->pc
, buf_offset
, &buf_res
);
1678 htohc32(temp
->sc
, buf_res
.physaddr
);
1679 td
->qtd_buffer_hi
[0] = 0;
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
);
1689 buf_res
.physaddr
& (~0xFFF));
1690 td
->qtd_buffer_hi
[x
] = 0;
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
1702 buf_offset
+= average
;
1703 usbd_get_page(temp
->pc
, buf_offset
- 1, &buf_res
);
1706 buf_res
.physaddr
& (~0xFFF));
1707 td
->qtd_buffer_hi
[x
] = 0;
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
);
1723 /* setup alt next pointer, if any */
1724 if (temp
->last_frame
) {
1726 qtd_altnext
= terminate
;
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
;
1733 qtd_altnext
= terminate
;
1738 temp
->shortpkt
= shortpkt_old
;
1739 temp
->len
= len_old
;
1743 temp
->td_next
= td_next
;
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
;
1754 uint32_t qh_endphub
;
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
;
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
) {
1784 htohc32(temp
.sc
, EHCI_QTD_SET_TOGGLE(1));
1786 temp
.auto_data_toggle
= 0;
1788 temp
.auto_data_toggle
= 1;
1791 if ((xfer
->xroot
->udev
->parent_hs_hub
!= NULL
) ||
1792 (xfer
->xroot
->udev
->address
!= 0)) {
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;
1805 htohc32(temp
.sc
, EHCI_QTD_SET_CERR(3));
1806 temp
.qtd_status
|= htohc32(temp
.sc
,
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
);
1829 while (x
!= xfer
->nframes
) {
1831 /* DATA0 / DATA1 message */
1833 temp
.len
= xfer
->frlengths
[x
];
1834 temp
.pc
= xfer
->frbuffers
+ 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;
1846 temp
.last_frame
= 1;
1847 temp
.setup_alt_next
= 0;
1850 /* keep previous data toggle and error count */
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 */
1864 /* regular data transfer */
1866 temp
.shortpkt
= (xfer
->flags
.force_short_xfer
) ? 0 : 1;
1869 /* set endpoint direction */
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
1891 temp
.qtd_status
&= htohc32(temp
.sc
, EHCI_QTD_SET_CERR(3) |
1892 EHCI_QTD_SET_TOGGLE(1));
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));
1905 temp
.last_frame
= 1;
1906 temp
.setup_alt_next
= 0;
1908 ehci_setup_standard_chain_sub(&temp
);
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
;
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
);
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 */
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);
1948 if (usbd_get_speed(xfer
->xroot
->udev
) == USB_SPEED_FULL
) {
1949 qh_endp
|= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL
);
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
);
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
);
1980 /* fill the overlay qTD */
1982 if (temp
.auto_data_toggle
&& xfer
->endpoint
->toggle_next
) {
1984 qh
->qh_qtd
.qtd_status
= htohc32(temp
.sc
, EHCI_QTD_SET_TOGGLE(1));
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
);
2003 ehci_root_intr(ehci_softc_t
*sc
)
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
));
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
));
2030 ehci_isoc_fs_done(ehci_softc_t
*sc
, struct usb_xfer
*xfer
)
2032 uint32_t nframes
= xfer
->nframes
;
2034 uint32_t *plen
= xfer
->frlengths
;
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
);
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];
2051 if (ehcidebug
> 15) {
2052 DPRINTF("isoc FS-TD\n");
2053 ehci_dump_sitd(sc
, td
);
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
);
2071 /* remove FS-TD from schedule */
2072 EHCI_REMOVE_FS_TD(td
, *pp_last
);
2079 xfer
->aframes
= xfer
->nframes
;
2083 ehci_isoc_hs_done(ehci_softc_t
*sc
, struct usb_xfer
*xfer
)
2085 uint32_t nframes
= xfer
->nframes
;
2087 uint32_t *plen
= xfer
->frlengths
;
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
);
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];
2105 if (ehcidebug
> 15) {
2106 DPRINTF("isoc HS-TD\n");
2107 ehci_dump_itd(sc
, td
);
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
)) {
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
2129 /* Invalid length - truncate */
2140 if ((td_no
== 8) || (nframes
== 0)) {
2141 /* remove HS-TD from schedule */
2142 EHCI_REMOVE_HS_TD(td
, *pp_last
);
2149 xfer
->aframes
= xfer
->nframes
;
2152 /* NOTE: "done" can be run two times in a row,
2153 * from close and from interrupt
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
)) {
2169 if (ehcidebug
> 8) {
2170 DPRINTF("nexttog=%d; data after transfer:\n",
2171 xfer
->endpoint
->toggle_next
);
2173 xfer
->td_transfer_first
);
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 /*------------------------------------------------------------------------*
2205 *------------------------------------------------------------------------*/
2207 ehci_device_bulk_open(struct usb_xfer
*xfer
)
2213 ehci_device_bulk_close(struct usb_xfer
*xfer
)
2215 ehci_device_done(xfer
, USB_ERR_CANCELLED
);
2219 ehci_device_bulk_enter(struct usb_xfer
*xfer
)
2225 ehci_device_bulk_start(struct usb_xfer
*xfer
)
2227 ehci_softc_t
*sc
= EHCI_BUS2SC(xfer
->xroot
->bus
);
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
)
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 *------------------------------------------------------------------------*/
2264 ehci_device_ctrl_open(struct usb_xfer
*xfer
)
2270 ehci_device_ctrl_close(struct usb_xfer
*xfer
)
2272 ehci_device_done(xfer
, USB_ERR_CANCELLED
);
2276 ehci_device_ctrl_enter(struct usb_xfer
*xfer
)
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 *------------------------------------------------------------------------*/
2305 ehci_device_intr_open(struct usb_xfer
*xfer
)
2307 ehci_softc_t
*sc
= EHCI_BUS2SC(xfer
->xroot
->bus
);
2312 usb_hs_bandwidth_alloc(xfer
);
2315 * Find the best QH position corresponding to the given interval:
2319 bit
= EHCI_VIRTUAL_FRAMELIST_COUNT
/ 2;
2321 if (xfer
->interval
>= bit
) {
2325 if (sc
->sc_intr_stat
[x
] <
2326 sc
->sc_intr_stat
[best
]) {
2336 sc
->sc_intr_stat
[best
]++;
2337 xfer
->qh_pos
= best
;
2339 DPRINTFN(3, "best=%d interval=%d\n",
2340 best
, xfer
->interval
);
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
);
2357 ehci_device_intr_enter(struct usb_xfer
*xfer
)
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 *------------------------------------------------------------------------*/
2386 ehci_device_isoc_fs_open(struct usb_xfer
*xfer
)
2388 ehci_softc_t
*sc
= EHCI_BUS2SC(xfer
->xroot
->bus
);
2390 uint32_t 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
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
);
2427 ehci_device_isoc_fs_close(struct usb_xfer
*xfer
)
2429 ehci_device_done(xfer
, USB_ERR_CANCELLED
);
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
);
2438 ehci_sitd_t
*td_last
= NULL
;
2439 ehci_sitd_t
**pp_last
;
2441 uint32_t buf_offset
;
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
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
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
;
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
;
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
) {
2530 kprintf("%s: frame length(%d) exceeds %d "
2531 "bytes (frame truncated)\n",
2533 xfer
->max_frame_size
);
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);
2546 * Schedule is FULL, set length to zero:
2550 sa
= USB_FS_ISOC_UFRAME_MAX
- 1;
2554 * only call "usbd_get_page()" when we have a
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,
2566 temp
= buf_res
.physaddr
& ~0xFFF;
2572 if (UE_GET_DIR(xfer
->endpointno
) == UE_DIR_OUT
) {
2575 temp
|= 1; /* T-count = 1, TP = ALL */
2580 temp
|= tlen
; /* T-count = [1..6] */
2581 temp
|= 8; /* TP = Begin */
2593 sa
= (sb
- sa
) & 0x3F;
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
);
2608 td
->sitd_status
= htohc32(sc
,
2611 EHCI_SITD_SET_LEN(*plen
));
2613 td
->sitd_status
= htohc32(sc
,
2615 EHCI_SITD_SET_LEN(*plen
));
2617 usb_pc_cpu_flush(td
->page_cache
);
2620 if (ehcidebug
> 15) {
2621 DPRINTF("FS-TD %d\n", nframes
);
2622 ehci_dump_sitd(sc
, td
);
2625 /* insert TD into schedule */
2626 EHCI_APPEND_FS_TD(td
, *pp_last
);
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;
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 *------------------------------------------------------------------------*/
2678 ehci_device_isoc_hs_open(struct usb_xfer
*xfer
)
2680 ehci_softc_t
*sc
= EHCI_BUS2SC(xfer
->xroot
->bus
);
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
)));
2709 EHCI_ITD_SET_MPL(xfer
->max_packet_size
& 0x7FF);
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
);
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
);
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
);
2741 ehci_itd_t
*td_last
= NULL
;
2742 ehci_itd_t
**pp_last
;
2743 bus_size_t page_addr
;
2746 uint32_t buf_offset
;
2748 uint32_t itd_offset
[8 + 1];
2752 uint8_t shift
= usbd_xfer_get_fps_shift(xfer
);
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
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
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
;
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
;
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];
2831 if (*plen
> xfer
->max_frame_size
) {
2835 kprintf("%s: frame length(%d) exceeds %d bytes "
2836 "(frame truncated)\n",
2837 __func__
, *plen
, xfer
->max_frame_size
);
2840 *plen
= xfer
->max_frame_size
;
2843 if (xfer
->endpoint
->usb_smask
& (1 << td_no
)) {
2844 status
= (EHCI_ITD_SET_LEN(*plen
) |
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
;
2853 td
->itd_status
[td_no
] = 0; /* not active */
2854 itd_offset
[td_no
] = buf_offset
;
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
) {
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
2892 usbd_get_page(xfer
->frbuffers
, buf_offset
- 1, &buf_res
);
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;
2902 panic("%s: too many pages\n", __func__
);
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 */
2913 td
->itd_status
[td_no
- 1] |= htohc32(sc
, EHCI_ITD_IOC
);
2915 usb_pc_cpu_flush(td
->page_cache
);
2917 if (ehcidebug
> 15) {
2918 DPRINTF("HS-TD %d\n", nframes
);
2919 ehci_dump_itd(sc
, td
);
2922 /* insert TD into schedule */
2923 EHCI_APPEND_HS_TD(td
, *pp_last
);
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);
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 *------------------------------------------------------------------------*/
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 */
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 */
2985 0, /* # of configurations */
2989 static const struct ehci_config_desc ehci_confd
= {
2991 .bLength
= sizeof(struct usb_config_descriptor
),
2992 .bDescriptorType
= UDESC_CONFIG
,
2993 .wTotalLength
[0] = sizeof(ehci_confd
),
2995 .bConfigurationValue
= 1,
2996 .iConfiguration
= 0,
2997 .bmAttributes
= UC_SELF_POWERED
,
2998 .bMaxPower
= 0 /* max power */
3001 .bLength
= sizeof(struct usb_interface_descriptor
),
3002 .bDescriptorType
= UDESC_INTERFACE
,
3004 .bInterfaceClass
= UICLASS_HUB
,
3005 .bInterfaceSubClass
= UISUBCLASS_HUB
,
3006 .bInterfaceProtocol
= 0,
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) */
3019 struct usb_hub_descriptor ehci_hubd
=
3021 .bDescLength
= 0, /* dynamic length */
3022 .bDescriptorType
= UDESC_HUB
,
3026 ehci_disown(ehci_softc_t
*sc
, uint16_t index
, uint8_t lowspeed
)
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
);
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
;
3053 USB_BUS_LOCK_ASSERT(&sc
->sc_bus
);
3056 ptr
= (const void *)&sc
->sc_hub_desc
;
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.
3078 case C(UR_GET_CONFIG
, UT_READ_DEVICE
):
3080 sc
->sc_hub_desc
.temp
[0] = sc
->sc_conf
;
3082 case C(UR_GET_DESCRIPTOR
, UT_READ_DEVICE
):
3083 switch (value
>> 8) {
3085 if ((value
& 0xff) != 0) {
3086 err
= USB_ERR_IOERROR
;
3089 len
= sizeof(ehci_devd
);
3090 ptr
= (const void *)&ehci_devd
;
3093 * We can't really operate at another speed,
3094 * but the specification says we need this
3097 case UDESC_DEVICE_QUALIFIER
:
3098 if ((value
& 0xff) != 0) {
3099 err
= USB_ERR_IOERROR
;
3102 len
= sizeof(ehci_odevd
);
3103 ptr
= (const void *)&ehci_odevd
;
3107 if ((value
& 0xff) != 0) {
3108 err
= USB_ERR_IOERROR
;
3111 len
= sizeof(ehci_confd
);
3112 ptr
= (const void *)&ehci_confd
;
3116 switch (value
& 0xff) {
3117 case 0: /* Language table */
3121 case 1: /* Vendor */
3122 str_ptr
= sc
->sc_vendor
;
3125 case 2: /* Product */
3126 str_ptr
= "EHCI root HUB";
3134 len
= usb_make_str_desc(
3135 sc
->sc_hub_desc
.temp
,
3136 sizeof(sc
->sc_hub_desc
.temp
),
3140 err
= USB_ERR_IOERROR
;
3144 case C(UR_GET_INTERFACE
, UT_READ_INTERFACE
):
3146 sc
->sc_hub_desc
.temp
[0] = 0;
3148 case C(UR_GET_STATUS
, UT_READ_DEVICE
):
3150 USETW(sc
->sc_hub_desc
.stat
.wStatus
, UDS_SELF_POWERED
);
3152 case C(UR_GET_STATUS
, UT_READ_INTERFACE
):
3153 case C(UR_GET_STATUS
, UT_READ_ENDPOINT
):
3155 USETW(sc
->sc_hub_desc
.stat
.wStatus
, 0);
3157 case C(UR_SET_ADDRESS
, UT_WRITE_DEVICE
):
3158 if (value
>= EHCI_MAX_DEVICES
) {
3159 err
= USB_ERR_IOERROR
;
3162 sc
->sc_addr
= value
;
3164 case C(UR_SET_CONFIG
, UT_WRITE_DEVICE
):
3165 if ((value
!= 0) && (value
!= 1)) {
3166 err
= USB_ERR_IOERROR
;
3169 sc
->sc_conf
= value
;
3171 case C(UR_SET_DESCRIPTOR
, UT_WRITE_DEVICE
):
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
;
3178 case C(UR_SET_INTERFACE
, UT_WRITE_INTERFACE
):
3180 case C(UR_SYNCH_FRAME
, UT_WRITE_ENDPOINT
):
3183 case C(UR_CLEAR_FEATURE
, UT_WRITE_CLASS_DEVICE
):
3185 case C(UR_CLEAR_FEATURE
, UT_WRITE_CLASS_OTHER
):
3186 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3189 (index
> sc
->sc_noport
)) {
3190 err
= USB_ERR_IOERROR
;
3193 port
= EHCI_PORTSC(index
);
3194 v
= EOREAD4(sc
, port
) & ~EHCI_PS_CLEAR
;
3196 case UHF_PORT_ENABLE
:
3197 EOWRITE4(sc
, port
, v
& ~EHCI_PS_PE
);
3199 case UHF_PORT_SUSPEND
:
3200 if ((v
& EHCI_PS_SUSP
) && (!(v
& EHCI_PS_FPR
))) {
3203 * waking up a High Speed device is rather
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);
3217 case UHF_PORT_POWER
:
3218 EOWRITE4(sc
, port
, v
& ~EHCI_PS_PP
);
3221 DPRINTFN(3, "clear port test "
3224 case UHF_PORT_INDICATOR
:
3225 DPRINTFN(3, "clear port ind "
3227 EOWRITE4(sc
, port
, v
& ~EHCI_PS_PIC
);
3229 case UHF_C_PORT_CONNECTION
:
3230 EOWRITE4(sc
, port
, v
| EHCI_PS_CSC
);
3232 case UHF_C_PORT_ENABLE
:
3233 EOWRITE4(sc
, port
, v
| EHCI_PS_PEC
);
3235 case UHF_C_PORT_SUSPEND
:
3236 EOWRITE4(sc
, port
, v
| EHCI_PS_SUSP
);
3238 case UHF_C_PORT_OVER_CURRENT
:
3239 EOWRITE4(sc
, port
, v
| EHCI_PS_OCC
);
3241 case UHF_C_PORT_RESET
:
3245 err
= USB_ERR_IOERROR
;
3249 case C(UR_GET_DESCRIPTOR
, UT_READ_CLASS_DEVICE
):
3250 if ((value
& 0xff) != 0) {
3251 err
= USB_ERR_IOERROR
;
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
;
3262 i
= UHD_PWR_NO_SWITCH
;
3264 if (EHCI_HCS_P_INDICATOR(v
))
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
;
3275 case C(UR_GET_STATUS
, UT_READ_CLASS_DEVICE
):
3277 memset(sc
->sc_hub_desc
.temp
, 0, 16);
3279 case C(UR_GET_STATUS
, UT_READ_CLASS_OTHER
):
3280 DPRINTFN(9, "get port status i=%d\n",
3283 (index
> sc
->sc_noport
)) {
3284 err
= USB_ERR_IOERROR
;
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)
3292 else if ((v
& 0xc000000) == 0x4000000)
3300 i
|= UPS_CURRENT_CONNECT_STATUS
;
3302 i
|= UPS_PORT_ENABLED
;
3303 if ((v
& EHCI_PS_SUSP
) && !(v
& EHCI_PS_FPR
))
3305 if (v
& EHCI_PS_OCA
)
3306 i
|= UPS_OVERCURRENT_INDICATOR
;
3310 i
|= UPS_PORT_POWER
;
3311 USETW(sc
->sc_hub_desc
.ps
.wPortStatus
, i
);
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
)
3322 i
|= UPS_C_PORT_RESET
;
3323 USETW(sc
->sc_hub_desc
.ps
.wPortChange
, i
);
3324 len
= sizeof(sc
->sc_hub_desc
.ps
);
3326 case C(UR_SET_DESCRIPTOR
, UT_WRITE_CLASS_DEVICE
):
3327 err
= USB_ERR_IOERROR
;
3329 case C(UR_SET_FEATURE
, UT_WRITE_CLASS_DEVICE
):
3331 case C(UR_SET_FEATURE
, UT_WRITE_CLASS_OTHER
):
3333 (index
> sc
->sc_noport
)) {
3334 err
= USB_ERR_IOERROR
;
3337 port
= EHCI_PORTSC(index
);
3338 v
= EOREAD4(sc
, port
) & ~EHCI_PS_CLEAR
;
3340 case UHF_PORT_ENABLE
:
3341 EOWRITE4(sc
, port
, v
| EHCI_PS_PE
);
3343 case UHF_PORT_SUSPEND
:
3344 EOWRITE4(sc
, port
, v
| EHCI_PS_SUSP
);
3346 case UHF_PORT_RESET
:
3347 DPRINTFN(6, "reset port %d\n", index
);
3349 if (ehcinohighspeed
) {
3351 * Connect USB device to companion
3354 ehci_disown(sc
, index
, 1);
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);
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
;
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);
3395 DPRINTF("ehci port %d reset, status = 0x%08x\n",
3399 case UHF_PORT_POWER
:
3400 DPRINTFN(3, "set port power %d\n", index
);
3401 EOWRITE4(sc
, port
, v
| EHCI_PS_PP
);
3405 DPRINTFN(3, "set port test %d\n", index
);
3408 case UHF_PORT_INDICATOR
:
3409 DPRINTFN(3, "set port ind %d\n", index
);
3410 EOWRITE4(sc
, port
, v
| EHCI_PS_PIC
);
3414 err
= USB_ERR_IOERROR
;
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
):
3424 err
= USB_ERR_IOERROR
;
3434 ehci_xfer_setup(struct usb_setup_params
*parm
)
3436 struct usb_page_search page_info
;
3437 struct usb_page_cache
*pc
;
3439 struct usb_xfer
*xfer
;
3447 sc
= EHCI_BUS2SC(parm
->udev
->bus
);
3448 xfer
= parm
->curr_xfer
;
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
3464 * +------------------------------------+
3468 * | | xxx | x | frm 0 |
3470 * | | xxx | xx | frm 1 |
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
);
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
);
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;
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
);
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
);
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
);
3573 * Allocate queue heads and transfer descriptors
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
;
3584 for (n
= 0; n
!= nitd
; n
++) {
3587 usbd_get_page(pc
+ n
, 0, &page_info
);
3589 td
= page_info
.buffer
;
3592 td
->itd_self
= htohc32(sc
, page_info
.physaddr
| EHCI_LINK_ITD
);
3593 td
->obj_next
= last_obj
;
3594 td
->page_cache
= pc
+ n
;
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
;
3608 for (n
= 0; n
!= nsitd
; n
++) {
3611 usbd_get_page(pc
+ n
, 0, &page_info
);
3613 td
= page_info
.buffer
;
3616 td
->sitd_self
= htohc32(sc
, page_info
.physaddr
| EHCI_LINK_SITD
);
3617 td
->obj_next
= last_obj
;
3618 td
->page_cache
= pc
+ n
;
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
;
3632 for (n
= 0; n
!= nqtd
; n
++) {
3635 usbd_get_page(pc
+ n
, 0, &page_info
);
3637 qtd
= page_info
.buffer
;
3640 qtd
->qtd_self
= htohc32(sc
, page_info
.physaddr
);
3641 qtd
->obj_next
= last_obj
;
3642 qtd
->page_cache
= pc
+ n
;
3646 usb_pc_cpu_flush(pc
+ n
);
3649 xfer
->td_start
[xfer
->flags_int
.curr_dma_set
] = last_obj
;
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
;
3660 for (n
= 0; n
!= nqh
; n
++) {
3663 usbd_get_page(pc
+ n
, 0, &page_info
);
3665 qh
= page_info
.buffer
;
3668 qh
->qh_self
= htohc32(sc
, page_info
.physaddr
| EHCI_LINK_QH
);
3669 qh
->obj_next
= last_obj
;
3670 qh
->page_cache
= pc
+ n
;
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;
3686 ehci_xfer_unsetup(struct usb_xfer
*xfer
)
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",
3699 edesc
->bEndpointAddress
, udev
->flags
.usb_mode
,
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 */
3712 switch (edesc
->bmAttributes
& UE_XFERTYPE
) {
3714 ep
->methods
= &ehci_device_ctrl_methods
;
3717 ep
->methods
= &ehci_device_intr_methods
;
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
;
3727 ep
->methods
= &ehci_device_bulk_methods
;
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 */
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
;
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
);
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
;
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
);
3815 ehci_set_hw_power_sleep(struct usb_bus
*bus
, uint32_t state
)
3817 struct ehci_softc
*sc
= EHCI_BUS2SC(bus
);
3820 case USB_HW_POWER_SUSPEND
:
3821 case USB_HW_POWER_SHUTDOWN
:
3824 case USB_HW_POWER_RESUME
:
3833 ehci_set_hw_power(struct usb_bus
*bus
)
3835 ehci_softc_t
*sc
= EHCI_BUS2SC(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
);
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
,