2 * $NetBSD: ehci.c,v 1.67 2004/07/06 04:18:05 mycroft Exp $
3 * $FreeBSD: src/sys/dev/usb/ehci.c,v 1.5 2003/11/10 00:20:52 joe Exp $
4 * $DragonFly: src/sys/bus/usb/ehci.c,v 1.23 2006/08/03 16:40:46 swildner Exp $
8 * Copyright (c) 2004 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Lennart Augustsson (lennart@augustsson.net) and by Charles M. Hannum.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
46 * The EHCI 1.0 spec can be found at
47 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
48 * and the USB 2.0 spec at
49 * http://www.usb.org/developers/docs/usb_20.zip
55 * 1) hold off explorations by companion controllers until ehci has started.
57 * 2) The EHCI driver lacks support for isochronous transfers, so
58 * devices using them don't work.
60 * 3) The hub driver needs to handle and schedule the transaction translator,
61 * to assign place in frame where different devices get to go. See chapter
62 * on hubs in USB 2.0 for details.
64 * 4) command failures are not recovered correctly
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
70 #include <sys/kernel.h>
71 #if defined(__NetBSD__) || defined(__OpenBSD__)
72 #include <sys/device.h>
73 #include <sys/select.h>
74 #elif defined(__FreeBSD__) || defined(__DragonFly__)
75 #include <sys/endian.h>
76 #include <sys/module.h>
78 #include <machine/bus_pio.h>
79 #include <machine/bus_memio.h>
81 #if defined(DIAGNOSTIC) && defined(__i386__)
82 #include <machine/cpu.h>
86 #include <sys/queue.h>
87 #include <sys/sysctl.h>
88 #include <sys/thread2.h>
90 #include <machine/bus.h>
91 #include <machine/endian.h>
93 #include <bus/usb/usb.h>
94 #include <bus/usb/usbdi.h>
95 #include <bus/usb/usbdivar.h>
96 #include <bus/usb/usb_mem.h>
97 #include <bus/usb/usb_quirks.h>
99 #include <bus/usb/ehcireg.h>
100 #include <bus/usb/ehcivar.h>
102 #if defined(__FreeBSD__) || defined(__DragonFly__)
103 #include <machine/clock.h>
105 #define delay(d) DELAY(d)
109 #define DPRINTF(x) do { if (ehcidebug) logprintf x; } while(0)
110 #define DPRINTFN(n,x) do { if (ehcidebug>(n)) logprintf x; } while(0)
112 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
113 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
114 &ehcidebug, 0, "ehci debug level");
116 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
120 #define DPRINTFN(n,x)
124 struct usbd_pipe pipe;
129 ehci_soft_qtd_t *qtd;
130 /* ehci_soft_itd_t *itd; */
137 /*ehci_soft_qtd_t *setup, *data, *stat;*/
150 #if defined(__NetBSD__) || defined(__OpenBSD__)
151 Static void ehci_shutdown(void *);
152 Static void ehci_power(int, void *);
155 Static usbd_status ehci_open(usbd_pipe_handle);
156 Static void ehci_poll(struct usbd_bus *);
157 Static void ehci_softintr(void *);
158 Static int ehci_intr1(ehci_softc_t *);
159 Static void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
160 Static void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
161 Static void ehci_idone(struct ehci_xfer *);
162 Static void ehci_timeout(void *);
163 Static void ehci_timeout_task(void *);
164 Static void ehci_intrlist_timeout(void *);
166 Static usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
167 Static void ehci_freem(struct usbd_bus *, usb_dma_t *);
169 Static usbd_xfer_handle ehci_allocx(struct usbd_bus *);
170 Static void ehci_freex(struct usbd_bus *, usbd_xfer_handle);
172 Static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle);
173 Static usbd_status ehci_root_ctrl_start(usbd_xfer_handle);
174 Static void ehci_root_ctrl_abort(usbd_xfer_handle);
175 Static void ehci_root_ctrl_close(usbd_pipe_handle);
176 Static void ehci_root_ctrl_done(usbd_xfer_handle);
178 Static usbd_status ehci_root_intr_transfer(usbd_xfer_handle);
179 Static usbd_status ehci_root_intr_start(usbd_xfer_handle);
180 Static void ehci_root_intr_abort(usbd_xfer_handle);
181 Static void ehci_root_intr_close(usbd_pipe_handle);
182 Static void ehci_root_intr_done(usbd_xfer_handle);
184 Static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle);
185 Static usbd_status ehci_device_ctrl_start(usbd_xfer_handle);
186 Static void ehci_device_ctrl_abort(usbd_xfer_handle);
187 Static void ehci_device_ctrl_close(usbd_pipe_handle);
188 Static void ehci_device_ctrl_done(usbd_xfer_handle);
190 Static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle);
191 Static usbd_status ehci_device_bulk_start(usbd_xfer_handle);
192 Static void ehci_device_bulk_abort(usbd_xfer_handle);
193 Static void ehci_device_bulk_close(usbd_pipe_handle);
194 Static void ehci_device_bulk_done(usbd_xfer_handle);
196 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle);
197 Static usbd_status ehci_device_intr_start(usbd_xfer_handle);
198 Static void ehci_device_intr_abort(usbd_xfer_handle);
199 Static void ehci_device_intr_close(usbd_pipe_handle);
200 Static void ehci_device_intr_done(usbd_xfer_handle);
202 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle);
203 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle);
204 Static void ehci_device_isoc_abort(usbd_xfer_handle);
205 Static void ehci_device_isoc_close(usbd_pipe_handle);
206 Static void ehci_device_isoc_done(usbd_xfer_handle);
208 Static void ehci_device_clear_toggle(usbd_pipe_handle pipe);
209 Static void ehci_noop(usbd_pipe_handle pipe);
211 Static int ehci_str(usb_string_descriptor_t *, int, const char *);
212 Static void ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
213 Static void ehci_pcd_able(ehci_softc_t *, int);
214 Static void ehci_pcd_enable(void *);
215 Static void ehci_disown(ehci_softc_t *, int, int);
217 Static ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *);
218 Static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
220 Static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
221 Static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
222 Static usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *,
223 ehci_softc_t *, int, int, usbd_xfer_handle,
224 ehci_soft_qtd_t **, ehci_soft_qtd_t **);
225 Static void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *,
228 Static usbd_status ehci_device_request(usbd_xfer_handle xfer);
230 Static void ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *);
231 Static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
233 Static void ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
234 Static void ehci_sync_hc(ehci_softc_t *);
236 Static void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
237 Static void ehci_abort_xfer(usbd_xfer_handle, usbd_status);
240 Static void ehci_dump_regs(ehci_softc_t *);
241 void ehci_dump(void);
242 Static ehci_softc_t *theehci;
243 Static void ehci_dump_link(ehci_link_t, int);
244 Static void ehci_dump_sqtds(ehci_soft_qtd_t *);
245 Static void ehci_dump_sqtd(ehci_soft_qtd_t *);
246 Static void ehci_dump_qtd(ehci_qtd_t *);
247 Static void ehci_dump_sqh(ehci_soft_qh_t *);
249 Static void ehci_dump_exfer(struct ehci_xfer *);
253 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
255 #define EHCI_INTR_ENDPT 1
257 #define ehci_add_intr_list(sc, ex) \
258 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext);
259 #define ehci_del_intr_list(ex) \
261 LIST_REMOVE((ex), inext); \
262 (ex)->inext.le_prev = NULL; \
264 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL)
266 Static struct usbd_bus_methods ehci_bus_methods = {
276 Static struct usbd_pipe_methods ehci_root_ctrl_methods = {
277 ehci_root_ctrl_transfer,
278 ehci_root_ctrl_start,
279 ehci_root_ctrl_abort,
280 ehci_root_ctrl_close,
285 Static struct usbd_pipe_methods ehci_root_intr_methods = {
286 ehci_root_intr_transfer,
287 ehci_root_intr_start,
288 ehci_root_intr_abort,
289 ehci_root_intr_close,
294 Static struct usbd_pipe_methods ehci_device_ctrl_methods = {
295 ehci_device_ctrl_transfer,
296 ehci_device_ctrl_start,
297 ehci_device_ctrl_abort,
298 ehci_device_ctrl_close,
300 ehci_device_ctrl_done,
303 Static struct usbd_pipe_methods ehci_device_intr_methods = {
304 ehci_device_intr_transfer,
305 ehci_device_intr_start,
306 ehci_device_intr_abort,
307 ehci_device_intr_close,
308 ehci_device_clear_toggle,
309 ehci_device_intr_done,
312 Static struct usbd_pipe_methods ehci_device_bulk_methods = {
313 ehci_device_bulk_transfer,
314 ehci_device_bulk_start,
315 ehci_device_bulk_abort,
316 ehci_device_bulk_close,
317 ehci_device_clear_toggle,
318 ehci_device_bulk_done,
321 Static struct usbd_pipe_methods ehci_device_isoc_methods = {
322 ehci_device_isoc_transfer,
323 ehci_device_isoc_start,
324 ehci_device_isoc_abort,
325 ehci_device_isoc_close,
327 ehci_device_isoc_done,
331 ehci_init(ehci_softc_t *sc)
333 u_int32_t vers, sparams, cparams, hcr;
339 DPRINTF(("ehci_init: start\n"));
344 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
346 vers = EREAD2(sc, EHCI_HCIVERSION);
347 printf("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev),
348 vers >> 8, vers & 0xff);
350 sparams = EREAD4(sc, EHCI_HCSPARAMS);
351 DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
352 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
353 ncomp = EHCI_HCS_N_CC(sparams);
354 if (ncomp != sc->sc_ncomp) {
355 printf("%s: wrong number of companions (%d != %d)\n",
356 USBDEVNAME(sc->sc_bus.bdev),
357 ncomp, sc->sc_ncomp);
358 if (ncomp < sc->sc_ncomp)
359 sc->sc_ncomp = ncomp;
361 if (sc->sc_ncomp > 0) {
362 printf("%s: companion controller%s, %d port%s each:",
363 USBDEVNAME(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
364 EHCI_HCS_N_PCC(sparams),
365 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
366 for (i = 0; i < sc->sc_ncomp; i++)
367 printf(" %s", USBDEVNAME(sc->sc_comps[i]->bdev));
370 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
371 cparams = EREAD4(sc, EHCI_HCCPARAMS);
372 DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
374 if (EHCI_HCC_64BIT(cparams)) {
375 /* MUST clear segment register if 64 bit capable. */
376 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
379 sc->sc_bus.usbrev = USBREV_2_0;
381 /* Reset the controller */
382 EOWRITE4(sc, EHCI_USBINTR, 0); /* disable interrupts */
383 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
384 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
385 usb_delay_ms(&sc->sc_bus, 1);
386 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
387 for (i = 0; i < 100; i++) {
388 usb_delay_ms(&sc->sc_bus, 1);
389 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
394 printf("%s: reset timeout\n",
395 USBDEVNAME(sc->sc_bus.bdev));
396 return (USBD_IOERROR);
399 /* frame list size at default, read back what we got and use that */
400 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
401 case 0: sc->sc_flsize = 1024*4; break;
402 case 1: sc->sc_flsize = 512*4; break;
403 case 2: sc->sc_flsize = 256*4; break;
404 case 3: return (USBD_IOERROR);
406 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize,
407 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
410 DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize));
412 /* Set up the bus struct. */
413 sc->sc_bus.methods = &ehci_bus_methods;
414 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
416 #if defined(__NetBSD__) || defined(__OpenBSD__)
417 sc->sc_powerhook = powerhook_establish(ehci_power, sc);
418 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
421 sc->sc_eintrs = EHCI_NORMAL_INTRS;
423 /* Allocate dummy QH that starts the async list. */
424 sqh = ehci_alloc_sqh(sc);
431 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
433 htole32(sqh->physaddr | EHCI_LINK_QH);
434 sqh->qh.qh_curqtd = EHCI_NULL;
436 /* Fill the overlay qTD */
437 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
438 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
439 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
447 /* Point to async list */
448 sc->sc_async_head = sqh;
449 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
451 usb_callout_init(sc->sc_tmo_pcd);
452 usb_callout_init(sc->sc_tmo_intrlist);
454 lockinit(&sc->sc_doorbell_lock, "ehcidb", 0, 0);
456 /* Turn on controller */
457 EOWRITE4(sc, EHCI_USBCMD,
458 EHCI_CMD_ITC_8 | /* 8 microframes */
459 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
464 /* Take over port ownership */
465 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
467 for (i = 0; i < 100; i++) {
468 usb_delay_ms(&sc->sc_bus, 1);
469 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
474 printf("%s: run timeout\n", USBDEVNAME(sc->sc_bus.bdev));
475 return (USBD_IOERROR);
478 return (USBD_NORMAL_COMPLETION);
482 ehci_free_sqh(sc, sc->sc_async_head);
485 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
490 ehci_init_intrs(ehci_softc_t *sc)
492 /* Enable interrupts */
493 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
499 ehci_softc_t *sc = v;
501 if (sc == NULL || sc->sc_dying)
504 /* If we get an interrupt while polling, then just ignore it. */
505 if (sc->sc_bus.use_polling) {
507 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
512 return (ehci_intr1(sc));
516 ehci_intr1(ehci_softc_t *sc)
518 u_int32_t intrs, eintrs;
520 DPRINTFN(20,("ehci_intr1: enter\n"));
522 /* In case the interrupt occurs before initialization has completed. */
525 printf("ehci_intr1: sc == NULL\n");
530 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
534 eintrs = intrs & sc->sc_eintrs;
535 DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
536 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
541 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
542 sc->sc_bus.intr_context++;
543 sc->sc_bus.no_intrs++;
544 if (eintrs & EHCI_STS_IAA) {
545 DPRINTF(("ehci_intr1: door bell\n"));
546 wakeup(&sc->sc_async_head);
547 eintrs &= ~EHCI_STS_IAA;
549 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
550 DPRINTFN(5,("ehci_intr1: %s %s\n",
551 eintrs & EHCI_STS_INT ? "INT" : "",
552 eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
553 usb_schedsoftintr(&sc->sc_bus);
554 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
556 if (eintrs & EHCI_STS_HSE) {
557 printf("%s: unrecoverable error, controller halted\n",
558 USBDEVNAME(sc->sc_bus.bdev));
561 if (eintrs & EHCI_STS_PCD) {
562 ehci_pcd(sc, sc->sc_intrxfer);
564 * Disable PCD interrupt for now, because it will be
565 * on until the port has been reset.
567 ehci_pcd_able(sc, 0);
568 /* Do not allow RHSC interrupts > 1 per second */
569 usb_callout(sc->sc_tmo_pcd, hz, ehci_pcd_enable, sc);
570 eintrs &= ~EHCI_STS_PCD;
573 sc->sc_bus.intr_context--;
576 /* Block unprocessed interrupts. */
577 sc->sc_eintrs &= ~eintrs;
578 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
579 printf("%s: blocking intrs 0x%x\n",
580 USBDEVNAME(sc->sc_bus.bdev), eintrs);
587 ehci_pcd_able(ehci_softc_t *sc, int on)
589 DPRINTFN(4, ("ehci_pcd_able: on=%d\n", on));
591 sc->sc_eintrs |= EHCI_STS_PCD;
593 sc->sc_eintrs &= ~EHCI_STS_PCD;
594 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
598 ehci_pcd_enable(void *v_sc)
600 ehci_softc_t *sc = v_sc;
602 ehci_pcd_able(sc, 1);
606 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
608 usbd_pipe_handle pipe;
613 /* Just ignore the change. */
619 p = KERNADDR(&xfer->dmabuf, 0);
620 m = min(sc->sc_noport, xfer->length * 8 - 1);
621 memset(p, 0, xfer->length);
622 for (i = 1; i <= m; i++) {
623 /* Pick out CHANGE bits from the status reg. */
624 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
625 p[i/8] |= 1 << (i%8);
627 DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
628 xfer->actlen = xfer->length;
629 xfer->status = USBD_NORMAL_COMPLETION;
631 usb_transfer_complete(xfer);
635 ehci_softintr(void *v)
637 ehci_softc_t *sc = v;
638 struct ehci_xfer *ex, *nextex;
640 DPRINTFN(10,("%s: ehci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
641 sc->sc_bus.intr_context));
643 sc->sc_bus.intr_context++;
646 * The only explanation I can think of for why EHCI is as brain dead
647 * as UHCI interrupt-wise is that Intel was involved in both.
648 * An interrupt just tells us that something is done, we have no
649 * clue what, so we need to scan through all active transfers. :-(
651 for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
652 nextex = LIST_NEXT(ex, inext);
653 ehci_check_intr(sc, ex);
656 /* Schedule a callout to catch any dropped transactions. */
657 if ((sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) &&
658 !LIST_EMPTY(&sc->sc_intrhead))
659 usb_callout(sc->sc_tmo_intrlist, hz / 5, ehci_intrlist_timeout,
662 #ifdef USB_USE_SOFTINTR
663 if (sc->sc_softwake) {
665 wakeup(&sc->sc_softwake);
667 #endif /* USB_USE_SOFTINTR */
669 sc->sc_bus.intr_context--;
672 /* Check for an interrupt. */
674 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
676 ehci_soft_qtd_t *sqtd, *lsqtd;
679 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
681 if (ex->sqtdstart == NULL) {
682 printf("ehci_check_intr: sqtdstart=NULL\n");
688 printf("ehci_check_intr: sqtd==0\n");
693 * If the last TD is still active we need to check whether there
694 * is a an error somewhere in the middle, or whether there was a
695 * short packet (SPD and not ACTIVE).
697 if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
698 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
699 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
700 status = le32toh(sqtd->qtd.qtd_status);
701 /* If there's an active QTD the xfer isn't done. */
702 if (status & EHCI_QTD_ACTIVE)
704 /* Any kind of error makes the xfer done. */
705 if (status & EHCI_QTD_HALTED)
707 /* We want short packets, and it is short: it's done */
708 if (EHCI_QTD_GET_BYTES(status) != 0)
711 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
716 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
717 usb_uncallout(ex->xfer.timeout_handle, ehci_timeout, ex);
722 ehci_idone(struct ehci_xfer *ex)
724 usbd_xfer_handle xfer = &ex->xfer;
725 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
726 ehci_soft_qtd_t *sqtd;
727 u_int32_t status = 0, nstatus;
730 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
737 printf("ehci_idone: ex is done!\n ");
740 printf("ehci_idone: ex=%p is done!\n", ex);
749 if (xfer->status == USBD_CANCELLED ||
750 xfer->status == USBD_TIMEOUT) {
751 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
756 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
758 ehci_dump_sqtds(ex->sqtdstart);
761 /* The transfer is done, compute actual length and status. */
763 for (sqtd = ex->sqtdstart; sqtd != NULL; sqtd = sqtd->nextqtd) {
764 nstatus = le32toh(sqtd->qtd.qtd_status);
765 if (nstatus & EHCI_QTD_ACTIVE)
769 /* halt is ok if descriptor is last, and complete */
770 if(sqtd->qtd.qtd_next == EHCI_NULL &&
771 EHCI_QTD_GET_BYTES(status) == 0)
772 status &= ~EHCI_QTD_HALTED;
773 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
774 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
777 /* If there are left over TDs we need to update the toggle. */
779 printf("ehci_idone: need toggle update status=%08x nstatus=%08x\n", status, nstatus);
781 ehci_dump_sqh(epipe->sqh);
782 ehci_dump_sqtds(ex->sqtdstart);
784 epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
787 status &= EHCI_QTD_STATERRS;
788 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, status=0x%x\n",
789 xfer->length, actlen, status));
790 xfer->actlen = actlen;
795 bitmask_snprintf((u_int32_t)status,
796 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR"
797 "\3MISSED", sbuf, sizeof(sbuf));
799 DPRINTFN((status == EHCI_QTD_HALTED)*/*10*/2,
800 ("ehci_idone: error, addr=%d, endpt=0x%02x, "
802 xfer->pipe->device->address,
803 xfer->pipe->endpoint->edesc->bEndpointAddress,
806 ehci_dump_sqh(epipe->sqh);
807 ehci_dump_sqtds(ex->sqtdstart);
810 if (status == EHCI_QTD_HALTED)
811 xfer->status = USBD_STALLED;
813 xfer->status = USBD_IOERROR; /* more info XXX */
815 xfer->status = USBD_NORMAL_COMPLETION;
818 usb_transfer_complete(xfer);
819 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
823 * Wait here until controller claims to have an interrupt.
824 * Then call ehci_intr and return. Use timeout to avoid waiting
828 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
833 xfer->status = USBD_IN_PROGRESS;
834 for (timo = xfer->timeout; timo >= 0; timo--) {
835 usb_delay_ms(&sc->sc_bus, 1);
838 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
840 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
847 if (xfer->status != USBD_IN_PROGRESS)
853 DPRINTF(("ehci_waitintr: timeout\n"));
854 xfer->status = USBD_TIMEOUT;
855 usb_transfer_complete(xfer);
856 /* XXX should free TD */
860 ehci_poll(struct usbd_bus *bus)
862 ehci_softc_t *sc = (ehci_softc_t *)bus;
866 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
868 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
873 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
877 #if defined(__NetBSD__) || defined(__OpenBSD__)
879 ehci_detach(struct ehci_softc *sc, int flags)
883 if (sc->sc_child != NULL)
884 rv = config_detach(sc->sc_child, flags);
889 usb_uncallout(sc->sc_tmo_intrlist, ehci_intrlist_timeout, sc);
890 usb_uncallout(sc->sc_tmo_pcd, ehci_pcd_enable, sc);
892 if (sc->sc_powerhook != NULL)
893 powerhook_disestablish(sc->sc_powerhook);
894 if (sc->sc_shutdownhook != NULL)
895 shutdownhook_disestablish(sc->sc_shutdownhook);
897 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
899 /* XXX free other data structures XXX */
905 #if defined(__NetBSD__) || defined(__OpenBSD__)
907 ehci_activate(device_ptr_t self, enum devact act)
909 struct ehci_softc *sc = (struct ehci_softc *)self;
916 case DVACT_DEACTIVATE:
917 if (sc->sc_child != NULL)
918 rv = config_deactivate(sc->sc_child);
927 * Handle suspend/resume.
929 * We need to switch to polling mode here, because this routine is
930 * called from an interrupt context. This is all right since we
931 * are almost suspended anyway.
933 #if defined(__NetBSD__) || defined(__OpenBSD__)
935 ehci_power(int why, void *v)
937 ehci_softc_t *sc = v;
941 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
950 sc->sc_bus.use_polling++;
953 ctl = OREAD4(sc, EHCI_CONTROL) & ~EHCI_HCFS_MASK;
954 if (sc->sc_control == 0) {
956 * Preserve register values, in case that APM BIOS
957 * does not recover them.
959 sc->sc_control = ctl;
960 sc->sc_intre = OREAD4(sc, EHCI_INTERRUPT_ENABLE);
962 ctl |= EHCI_HCFS_SUSPEND;
963 OWRITE4(sc, EHCI_CONTROL, ctl);
965 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
966 sc->sc_bus.use_polling--;
969 sc->sc_bus.use_polling++;
972 /* Some broken BIOSes do not recover these values */
973 OWRITE4(sc, EHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
974 OWRITE4(sc, EHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
975 OWRITE4(sc, EHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
977 OWRITE4(sc, EHCI_INTERRUPT_ENABLE,
978 sc->sc_intre & (EHCI_ALL_INTRS | EHCI_MIE));
980 ctl = sc->sc_control;
982 ctl = OREAD4(sc, EHCI_CONTROL);
983 ctl |= EHCI_HCFS_RESUME;
984 OWRITE4(sc, EHCI_CONTROL, ctl);
985 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
986 ctl = (ctl & ~EHCI_HCFS_MASK) | EHCI_HCFS_OPERATIONAL;
987 OWRITE4(sc, EHCI_CONTROL, ctl);
988 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
989 sc->sc_control = sc->sc_intre = 0;
991 sc->sc_bus.use_polling--;
993 case PWR_SOFTSUSPEND:
994 case PWR_SOFTSTANDBY:
1003 * Shut down the controller when the system is going down.
1005 #if defined(__NetBSD__) || defined(__OpenBSD__)
1007 ehci_shutdown(void *v)
1009 ehci_softc_t *sc = v;
1011 DPRINTF(("ehci_shutdown: stopping the HC\n"));
1012 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
1013 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1018 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
1022 err = usb_allocmem(bus, size, 0, dma);
1025 printf("ehci_allocm: usb_allocmem()=%d\n", err);
1031 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
1033 usb_freemem(bus, dma);
1037 ehci_allocx(struct usbd_bus *bus)
1039 struct ehci_softc *sc = (struct ehci_softc *)bus;
1040 usbd_xfer_handle xfer;
1042 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
1044 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
1046 if (xfer->busy_free != XFER_FREE) {
1047 printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer,
1052 xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_INTWAIT);
1055 memset(xfer, 0, sizeof(struct ehci_xfer));
1057 EXFER(xfer)->isdone = 1;
1058 xfer->busy_free = XFER_BUSY;
1065 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1067 struct ehci_softc *sc = (struct ehci_softc *)bus;
1070 if (xfer->busy_free != XFER_BUSY) {
1071 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
1075 xfer->busy_free = XFER_FREE;
1076 if (!EXFER(xfer)->isdone) {
1077 printf("ehci_freex: !isdone\n");
1081 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
1085 ehci_device_clear_toggle(usbd_pipe_handle pipe)
1087 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1089 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
1090 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
1093 usbd_dump_pipe(pipe);
1095 epipe->nexttoggle = 0;
1099 ehci_noop(usbd_pipe_handle pipe)
1105 ehci_dump_regs(ehci_softc_t *sc)
1108 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1109 EOREAD4(sc, EHCI_USBCMD),
1110 EOREAD4(sc, EHCI_USBSTS),
1111 EOREAD4(sc, EHCI_USBINTR));
1112 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1113 EOREAD4(sc, EHCI_FRINDEX),
1114 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1115 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1116 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1117 for (i = 1; i <= sc->sc_noport; i++)
1118 printf("port %d status=0x%08x\n", i,
1119 EOREAD4(sc, EHCI_PORTSC(i)));
1123 * Unused function - this is meant to be called from a kernel
1129 ehci_dump_regs(theehci);
1133 ehci_dump_link(ehci_link_t link, int type)
1135 link = le32toh(link);
1136 printf("0x%08x", link);
1137 if (link & EHCI_LINK_TERMINATE)
1142 switch (EHCI_LINK_TYPE(link)) {
1143 case EHCI_LINK_ITD: printf("ITD"); break;
1144 case EHCI_LINK_QH: printf("QH"); break;
1145 case EHCI_LINK_SITD: printf("SITD"); break;
1146 case EHCI_LINK_FSTN: printf("FSTN"); break;
1154 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1160 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1161 ehci_dump_sqtd(sqtd);
1162 stop = sqtd->qtd.qtd_next & EHCI_LINK_TERMINATE;
1165 printf("dump aborted, too many TDs\n");
1169 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1171 printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr);
1172 ehci_dump_qtd(&sqtd->qtd);
1176 ehci_dump_qtd(ehci_qtd_t *qtd)
1181 printf(" next="); ehci_dump_link(qtd->qtd_next, 0);
1182 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
1184 s = le32toh(qtd->qtd_status);
1185 bitmask_snprintf(EHCI_QTD_GET_STATUS(s),
1186 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1187 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
1188 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
1189 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
1190 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
1191 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
1192 EHCI_QTD_GET_PID(s), sbuf);
1193 for (s = 0; s < 5; s++)
1194 printf(" buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
1198 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1200 ehci_qh_t *qh = &sqh->qh;
1201 u_int32_t endp, endphub;
1203 printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr);
1204 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
1205 endp = le32toh(qh->qh_endp);
1206 printf(" endp=0x%08x\n", endp);
1207 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
1208 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1209 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
1210 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
1211 printf(" mpl=0x%x ctl=%d nrl=%d\n",
1212 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
1213 EHCI_QH_GET_NRL(endp));
1214 endphub = le32toh(qh->qh_endphub);
1215 printf(" endphub=0x%08x\n", endphub);
1216 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
1217 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
1218 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1219 EHCI_QH_GET_MULT(endphub));
1220 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
1221 printf("Overlay qTD:\n");
1222 ehci_dump_qtd(&qh->qh_qtd);
1227 ehci_dump_exfer(struct ehci_xfer *ex)
1229 printf("ehci_dump_exfer: ex=%p\n", ex);
1235 ehci_open(usbd_pipe_handle pipe)
1237 usbd_device_handle dev = pipe->device;
1238 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
1239 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1240 u_int8_t addr = dev->address;
1241 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1242 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1243 ehci_soft_qh_t *sqh;
1247 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1248 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1251 return (USBD_IOERROR);
1253 epipe->nexttoggle = 0;
1255 if (addr == sc->sc_addr) {
1256 switch (ed->bEndpointAddress) {
1257 case USB_CONTROL_ENDPOINT:
1258 pipe->methods = &ehci_root_ctrl_methods;
1260 case UE_DIR_IN | EHCI_INTR_ENDPT:
1261 pipe->methods = &ehci_root_intr_methods;
1264 return (USBD_INVAL);
1266 return (USBD_NORMAL_COMPLETION);
1269 /* XXX All this stuff is only valid for async. */
1270 switch (dev->speed) {
1271 case USB_SPEED_LOW: speed = EHCI_QH_SPEED_LOW; break;
1272 case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
1273 case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
1274 default: panic("ehci_open: bad device speed %d", dev->speed);
1277 sqh = ehci_alloc_sqh(sc);
1280 /* qh_link filled when the QH is added */
1281 sqh->qh.qh_endp = htole32(
1282 EHCI_QH_SET_ADDR(addr) |
1283 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
1284 EHCI_QH_SET_EPS(speed) |
1286 EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
1287 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
1289 EHCI_QH_SET_NRL(naks)
1291 sqh->qh.qh_endphub = htole32(
1294 /* XXX interrupt mask */
1296 sqh->qh.qh_curqtd = EHCI_NULL;
1297 /* Fill the overlay qTD */
1298 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
1299 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
1300 sqh->qh.qh_qtd.qtd_status = htole32(0);
1306 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
1307 0, &epipe->u.ctl.reqdma);
1310 printf("ehci_open: usb_allocmem()=%d\n", err);
1314 pipe->methods = &ehci_device_ctrl_methods;
1316 ehci_add_qh(sqh, sc->sc_async_head);
1320 pipe->methods = &ehci_device_bulk_methods;
1322 ehci_add_qh(sqh, sc->sc_async_head);
1326 pipe->methods = &ehci_device_intr_methods;
1327 return (USBD_INVAL);
1328 case UE_ISOCHRONOUS:
1329 pipe->methods = &ehci_device_isoc_methods;
1330 return (USBD_INVAL);
1332 return (USBD_INVAL);
1334 return (USBD_NORMAL_COMPLETION);
1337 ehci_free_sqh(sc, sqh);
1339 return (USBD_NOMEM);
1343 * Add an ED to the schedule. Called while in a critical section.
1346 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1348 sqh->next = head->next;
1349 sqh->qh.qh_link = head->qh.qh_link;
1351 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
1354 if (ehcidebug > 5) {
1355 printf("ehci_add_qh:\n");
1362 * Remove an ED from the schedule. Called while in a critical section.
1365 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1370 for (p = head; p != NULL && p->next != sqh; p = p->next)
1373 panic("ehci_rem_qh: ED not found");
1374 p->next = sqh->next;
1375 p->qh.qh_link = sqh->qh.qh_link;
1381 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
1383 /* Halt while we are messing. */
1384 sqh->qh.qh_qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
1385 sqh->qh.qh_curqtd = 0;
1386 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
1389 sqh->qh.qh_qtd.qtd_status &= htole32(~EHCI_QTD_HALTED);
1393 * Ensure that the HC has released all references to the QH. We do this
1394 * by asking for a Async Advance Doorbell interrupt and then we wait for
1396 * To make this easier we first obtain exclusive use of the doorbell.
1399 ehci_sync_hc(ehci_softc_t *sc)
1404 DPRINTFN(2,("ehci_sync_hc: dying\n"));
1407 DPRINTFN(2,("ehci_sync_hc: enter\n"));
1409 lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE);
1411 /* ask for doorbell */
1412 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
1413 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1414 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1415 error = tsleep(&sc->sc_async_head, 0, "ehcidi", hz); /* bell wait */
1416 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1417 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1419 /* release doorbell */
1420 lockmgr(&sc->sc_doorbell_lock, LK_RELEASE);
1423 printf("ehci_sync_hc: tsleep() = %d\n", error);
1425 DPRINTFN(2,("ehci_sync_hc: exit\n"));
1431 * Data structures and routines to emulate the root hub.
1433 Static usb_device_descriptor_t ehci_devd = {
1434 USB_DEVICE_DESCRIPTOR_SIZE,
1435 UDESC_DEVICE, /* type */
1436 {0x00, 0x02}, /* USB version */
1437 UDCLASS_HUB, /* class */
1438 UDSUBCLASS_HUB, /* subclass */
1439 UDPROTO_HSHUBSTT, /* protocol */
1440 64, /* max packet */
1441 {0},{0},{0x00,0x01}, /* device id */
1442 1,2,0, /* string indicies */
1443 1 /* # of configurations */
1446 Static usb_device_qualifier_t ehci_odevd = {
1447 USB_DEVICE_DESCRIPTOR_SIZE,
1448 UDESC_DEVICE_QUALIFIER, /* type */
1449 {0x00, 0x02}, /* USB version */
1450 UDCLASS_HUB, /* class */
1451 UDSUBCLASS_HUB, /* subclass */
1452 UDPROTO_FSHUB, /* protocol */
1453 64, /* max packet */
1454 1, /* # of configurations */
1458 Static usb_config_descriptor_t ehci_confd = {
1459 USB_CONFIG_DESCRIPTOR_SIZE,
1461 {USB_CONFIG_DESCRIPTOR_SIZE +
1462 USB_INTERFACE_DESCRIPTOR_SIZE +
1463 USB_ENDPOINT_DESCRIPTOR_SIZE},
1471 Static usb_interface_descriptor_t ehci_ifcd = {
1472 USB_INTERFACE_DESCRIPTOR_SIZE,
1483 Static usb_endpoint_descriptor_t ehci_endpd = {
1484 USB_ENDPOINT_DESCRIPTOR_SIZE,
1486 UE_DIR_IN | EHCI_INTR_ENDPT,
1488 {8, 0}, /* max packet */
1492 Static usb_hub_descriptor_t ehci_hubd = {
1493 USB_HUB_DESCRIPTOR_SIZE,
1503 ehci_str( usb_string_descriptor_t *p, int l, const char *s)
1509 p->bLength = 2 * strlen(s) + 2;
1512 p->bDescriptorType = UDESC_STRING;
1514 for (i = 0; s[i] && l > 1; i++, l -= 2)
1515 USETW2(p->bString[i], 0, s[i]);
1520 * Simulate a hardware hub by handling all the necessary requests.
1523 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
1527 /* Insert last in queue. */
1528 err = usb_insert_transfer(xfer);
1532 /* Pipe isn't running, start first */
1533 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1537 ehci_root_ctrl_start(usbd_xfer_handle xfer)
1539 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
1540 usb_device_request_t *req;
1543 int len, value, index, l, totlen = 0;
1544 usb_port_status_t ps;
1545 usb_hub_descriptor_t hubd;
1550 return (USBD_IOERROR);
1553 if (!(xfer->rqflags & URQ_REQUEST))
1555 return (USBD_INVAL);
1557 req = &xfer->request;
1559 DPRINTFN(4,("ehci_root_ctrl_start type=0x%02x request=%02x\n",
1560 req->bmRequestType, req->bRequest));
1562 len = UGETW(req->wLength);
1563 value = UGETW(req->wValue);
1564 index = UGETW(req->wIndex);
1567 buf = KERNADDR(&xfer->dmabuf, 0);
1569 #define C(x,y) ((x) | ((y) << 8))
1570 switch(C(req->bRequest, req->bmRequestType)) {
1571 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1572 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1573 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1575 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1576 * for the integrated root hub.
1579 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1581 *(u_int8_t *)buf = sc->sc_conf;
1585 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1586 DPRINTFN(8,("ehci_root_ctrl_start wValue=0x%04x\n", value));
1589 switch(value >> 8) {
1591 if ((value & 0xff) != 0) {
1595 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1596 USETW(ehci_devd.idVendor, sc->sc_id_vendor);
1597 memcpy(buf, &ehci_devd, l);
1600 * We can't really operate at another speed, but the spec says
1601 * we need this descriptor.
1603 case UDESC_DEVICE_QUALIFIER:
1604 if ((value & 0xff) != 0) {
1608 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1609 memcpy(buf, &ehci_odevd, l);
1612 * We can't really operate at another speed, but the spec says
1613 * we need this descriptor.
1615 case UDESC_OTHER_SPEED_CONFIGURATION:
1617 if ((value & 0xff) != 0) {
1621 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
1622 memcpy(buf, &ehci_confd, l);
1623 ((usb_config_descriptor_t *)buf)->bDescriptorType =
1625 buf = (char *)buf + l;
1627 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
1629 memcpy(buf, &ehci_ifcd, l);
1630 buf = (char *)buf + l;
1632 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
1634 memcpy(buf, &ehci_endpd, l);
1637 *(u_int8_t *)buf = 0;
1639 switch (value & 0xff) {
1640 case 1: /* Vendor */
1641 totlen = ehci_str(buf, len, sc->sc_vendor);
1643 case 2: /* Product */
1644 totlen = ehci_str(buf, len, "EHCI root hub");
1653 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
1655 *(u_int8_t *)buf = 0;
1659 case C(UR_GET_STATUS, UT_READ_DEVICE):
1661 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
1665 case C(UR_GET_STATUS, UT_READ_INTERFACE):
1666 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
1668 USETW(((usb_status_t *)buf)->wStatus, 0);
1672 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
1673 if (value >= USB_MAX_DEVICES) {
1677 sc->sc_addr = value;
1679 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
1680 if (value != 0 && value != 1) {
1684 sc->sc_conf = value;
1686 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
1688 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
1689 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
1690 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
1693 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
1695 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
1698 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
1700 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
1701 DPRINTFN(8, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
1702 "port=%d feature=%d\n",
1704 if (index < 1 || index > sc->sc_noport) {
1708 port = EHCI_PORTSC(index);
1709 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1711 case UHF_PORT_ENABLE:
1712 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
1714 case UHF_PORT_SUSPEND:
1715 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
1717 case UHF_PORT_POWER:
1718 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
1721 DPRINTFN(2,("ehci_root_ctrl_start: clear port test "
1724 case UHF_PORT_INDICATOR:
1725 DPRINTFN(2,("ehci_root_ctrl_start: clear port ind "
1727 EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
1729 case UHF_C_PORT_CONNECTION:
1730 EOWRITE4(sc, port, v | EHCI_PS_CSC);
1732 case UHF_C_PORT_ENABLE:
1733 EOWRITE4(sc, port, v | EHCI_PS_PEC);
1735 case UHF_C_PORT_SUSPEND:
1738 case UHF_C_PORT_OVER_CURRENT:
1739 EOWRITE4(sc, port, v | EHCI_PS_OCC);
1741 case UHF_C_PORT_RESET:
1750 case UHF_C_PORT_CONNECTION:
1751 case UHF_C_PORT_ENABLE:
1752 case UHF_C_PORT_SUSPEND:
1753 case UHF_C_PORT_OVER_CURRENT:
1754 case UHF_C_PORT_RESET:
1755 /* Enable RHSC interrupt if condition is cleared. */
1756 if ((OREAD4(sc, port) >> 16) == 0)
1757 ehci_pcd_able(sc, 1);
1764 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
1767 if ((value & 0xff) != 0) {
1772 hubd.bNbrPorts = sc->sc_noport;
1773 v = EOREAD4(sc, EHCI_HCSPARAMS);
1774 USETW(hubd.wHubCharacteristics,
1775 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
1776 EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
1777 ? UHD_PORT_IND : 0);
1778 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
1779 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
1780 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
1781 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
1782 l = min(len, hubd.bDescLength);
1784 memcpy(buf, &hubd, l);
1786 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
1791 memset(buf, 0, len); /* ? XXX */
1794 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
1795 DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n",
1797 if (index < 1 || index > sc->sc_noport) {
1805 v = EOREAD4(sc, EHCI_PORTSC(index));
1806 DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n", v));
1808 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
1809 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
1810 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
1811 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
1812 if (v & EHCI_PS_PR) i |= UPS_RESET;
1813 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
1814 USETW(ps.wPortStatus, i);
1816 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
1817 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
1818 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
1819 if (sc->sc_isreset) i |= UPS_C_PORT_RESET;
1820 USETW(ps.wPortChange, i);
1821 l = min(len, sizeof ps);
1822 memcpy(buf, &ps, l);
1825 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
1828 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
1830 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
1831 if (index < 1 || index > sc->sc_noport) {
1835 port = EHCI_PORTSC(index);
1836 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1838 case UHF_PORT_ENABLE:
1839 EOWRITE4(sc, port, v | EHCI_PS_PE);
1841 case UHF_PORT_SUSPEND:
1842 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
1844 case UHF_PORT_RESET:
1845 DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n",
1847 if (EHCI_PS_IS_LOWSPEED(v)) {
1848 /* Low speed device, give up ownership. */
1849 ehci_disown(sc, index, 1);
1852 /* Start reset sequence. */
1853 v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
1854 EOWRITE4(sc, port, v | EHCI_PS_PR);
1855 /* Wait for reset to complete. */
1856 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
1861 /* Terminate reset sequence. */
1862 EOWRITE4(sc, port, v);
1863 /* Wait for HC to complete reset. */
1864 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE);
1869 v = EOREAD4(sc, port);
1870 DPRINTF(("ehci after reset, status=0x%08x\n", v));
1871 if (v & EHCI_PS_PR) {
1872 printf("%s: port reset timeout\n",
1873 USBDEVNAME(sc->sc_bus.bdev));
1874 return (USBD_TIMEOUT);
1876 if (!(v & EHCI_PS_PE)) {
1877 /* Not a high speed device, give up ownership.*/
1878 ehci_disown(sc, index, 0);
1882 DPRINTF(("ehci port %d reset, status = 0x%08x\n",
1885 case UHF_PORT_POWER:
1886 DPRINTFN(2,("ehci_root_ctrl_start: set port power "
1888 EOWRITE4(sc, port, v | EHCI_PS_PP);
1891 DPRINTFN(2,("ehci_root_ctrl_start: set port test "
1894 case UHF_PORT_INDICATOR:
1895 DPRINTFN(2,("ehci_root_ctrl_start: set port ind "
1897 EOWRITE4(sc, port, v | EHCI_PS_PIC);
1904 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
1905 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
1906 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
1907 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
1913 xfer->actlen = totlen;
1914 err = USBD_NORMAL_COMPLETION;
1918 usb_transfer_complete(xfer);
1920 return (USBD_IN_PROGRESS);
1924 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
1929 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
1931 if (sc->sc_npcomp != 0) {
1932 int i = (index-1) / sc->sc_npcomp;
1933 if (i >= sc->sc_ncomp)
1934 printf("%s: strange port\n",
1935 USBDEVNAME(sc->sc_bus.bdev));
1937 printf("%s: handing over %s speed device on "
1939 USBDEVNAME(sc->sc_bus.bdev),
1940 lowspeed ? "low" : "full",
1941 index, USBDEVNAME(sc->sc_comps[i]->bdev));
1943 printf("%s: npcomp == 0\n", USBDEVNAME(sc->sc_bus.bdev));
1946 port = EHCI_PORTSC(index);
1947 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1948 EOWRITE4(sc, port, v | EHCI_PS_PO);
1951 /* Abort a root control request. */
1953 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
1955 /* Nothing to do, all transfers are synchronous. */
1958 /* Close the root pipe. */
1960 ehci_root_ctrl_close(usbd_pipe_handle pipe)
1962 DPRINTF(("ehci_root_ctrl_close\n"));
1963 /* Nothing to do. */
1967 ehci_root_intr_done(usbd_xfer_handle xfer)
1972 ehci_root_intr_transfer(usbd_xfer_handle xfer)
1976 /* Insert last in queue. */
1977 err = usb_insert_transfer(xfer);
1981 /* Pipe isn't running, start first */
1982 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1986 ehci_root_intr_start(usbd_xfer_handle xfer)
1988 usbd_pipe_handle pipe = xfer->pipe;
1989 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
1992 return (USBD_IOERROR);
1994 sc->sc_intrxfer = xfer;
1996 return (USBD_IN_PROGRESS);
1999 /* Abort a root interrupt request. */
2001 ehci_root_intr_abort(usbd_xfer_handle xfer)
2003 if (xfer->pipe->intrxfer == xfer) {
2004 DPRINTF(("ehci_root_intr_abort: remove\n"));
2005 xfer->pipe->intrxfer = NULL;
2007 xfer->status = USBD_CANCELLED;
2009 usb_transfer_complete(xfer);
2013 /* Close the root pipe. */
2015 ehci_root_intr_close(usbd_pipe_handle pipe)
2017 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2019 DPRINTF(("ehci_root_intr_close\n"));
2021 sc->sc_intrxfer = NULL;
2025 ehci_root_ctrl_done(usbd_xfer_handle xfer)
2029 /************************/
2032 ehci_alloc_sqh(ehci_softc_t *sc)
2034 ehci_soft_qh_t *sqh;
2039 if (sc->sc_freeqhs == NULL) {
2040 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
2041 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
2042 EHCI_PAGE_SIZE, &dma);
2045 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2049 for(i = 0; i < EHCI_SQH_CHUNK; i++) {
2050 offs = i * EHCI_SQH_SIZE;
2051 sqh = KERNADDR(&dma, offs);
2052 sqh->physaddr = DMAADDR(&dma, offs);
2053 sqh->next = sc->sc_freeqhs;
2054 sc->sc_freeqhs = sqh;
2057 sqh = sc->sc_freeqhs;
2058 sc->sc_freeqhs = sqh->next;
2059 memset(&sqh->qh, 0, sizeof(ehci_qh_t));
2065 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2067 sqh->next = sc->sc_freeqhs;
2068 sc->sc_freeqhs = sqh;
2072 ehci_alloc_sqtd(ehci_softc_t *sc)
2074 ehci_soft_qtd_t *sqtd;
2079 if (sc->sc_freeqtds == NULL) {
2080 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
2081 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
2082 EHCI_PAGE_SIZE, &dma);
2085 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
2090 for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
2091 offs = i * EHCI_SQTD_SIZE;
2092 sqtd = KERNADDR(&dma, offs);
2093 sqtd->physaddr = DMAADDR(&dma, offs);
2094 sqtd->nextqtd = sc->sc_freeqtds;
2095 sc->sc_freeqtds = sqtd;
2101 sqtd = sc->sc_freeqtds;
2102 sc->sc_freeqtds = sqtd->nextqtd;
2103 memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
2104 sqtd->nextqtd = NULL;
2112 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2115 sqtd->nextqtd = sc->sc_freeqtds;
2116 sc->sc_freeqtds = sqtd;
2121 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
2122 int alen, int rd, usbd_xfer_handle xfer,
2123 ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
2125 ehci_soft_qtd_t *next, *cur;
2126 ehci_physaddr_t dataphys, dataphyspage, dataphyslastpage, nextphys;
2127 u_int32_t qtdstatus;
2128 int len, curlen, mps;
2131 usb_dma_t *dma = &xfer->dmabuf;
2132 u_int16_t flags = xfer->flags;
2134 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
2138 dataphys = DMAADDR(dma, 0);
2139 dataphyslastpage = EHCI_PAGE(DMAADDR(dma, len - 1));
2140 qtdstatus = EHCI_QTD_ACTIVE |
2141 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2142 EHCI_QTD_SET_CERR(3)
2144 /* BYTES set below */
2146 mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
2147 tog = epipe->nexttoggle;
2148 qtdstatus |= EHCI_QTD_SET_TOGGLE(tog);
2150 cur = ehci_alloc_sqtd(sc);
2155 dataphyspage = EHCI_PAGE(dataphys);
2156 /* The EHCI hardware can handle at most 5 pages. */
2157 #if defined(__NetBSD__) || defined(__OpenBSD__)
2158 if (dataphyslastpage - dataphyspage <
2159 EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE) {
2160 /* we can handle it in this QTD */
2162 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2163 /* XXX This is pretty broken: Because we do not allocate
2164 * a contiguous buffer (contiguous in physical pages) we
2165 * can only transfer one page in one go.
2166 * So check whether the start and end of the buffer are on
2169 if (dataphyspage == dataphyslastpage) {
2173 #if defined(__NetBSD__) || defined(__OpenBSD__)
2174 /* must use multiple TDs, fill as much as possible. */
2175 curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE -
2176 EHCI_PAGE_OFFSET(dataphys);
2179 printf("ehci_alloc_sqtd_chain: curlen=0x%x "
2180 "len=0x%x offs=0x%x\n", curlen, len,
2181 EHCI_PAGE_OFFSET(dataphys));
2182 printf("lastpage=0x%x page=0x%x phys=0x%x\n",
2183 dataphyslastpage, dataphyspage,
2188 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2189 /* See comment above (XXX) */
2190 curlen = EHCI_PAGE_SIZE -
2191 EHCI_PAGE_MASK(dataphys);
2193 /* the length must be a multiple of the max size */
2194 curlen -= curlen % mps;
2195 DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, "
2196 "curlen=%d\n", curlen));
2199 panic("ehci_alloc_sqtd_chain: curlen == 0");
2202 DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x "
2203 "dataphyslastpage=0x%08x len=%d curlen=%d\n",
2204 dataphys, dataphyslastpage,
2209 * Allocate another transfer if there's more data left,
2210 * or if force last short transfer flag is set and we're
2211 * allocating a multiple of the max packet size.
2214 ((flags & USBD_FORCE_SHORT_XFER) &&
2215 (curlen % mps) == 0 && !rd && curlen != 0)
2217 next = ehci_alloc_sqtd(sc);
2220 nextphys = htole32(next->physaddr);
2223 nextphys = EHCI_NULL;
2226 for (i = 0; i * EHCI_PAGE_SIZE <
2227 curlen + EHCI_PAGE_OFFSET(dataphys); i++) {
2228 ehci_physaddr_t a = dataphys + i * EHCI_PAGE_SIZE;
2229 if (i != 0) /* use offset only in first buffer */
2231 cur->qtd.qtd_buffer[i] = htole32(a);
2232 cur->qtd.qtd_buffer_hi[i] = 0;
2234 if (i >= EHCI_QTD_NBUFFERS) {
2235 printf("ehci_alloc_sqtd_chain: i=%d\n", i);
2240 cur->nextqtd = next;
2241 cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys;
2242 cur->qtd.qtd_status =
2243 htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
2246 DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n",
2247 dataphys, dataphys + curlen));
2248 /* adjust the toggle based on the number of packets in this
2250 if (((curlen + mps - 1) / mps) & 1) {
2252 qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
2256 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
2258 dataphys = DMAADDR(dma, offset);
2261 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
2263 epipe->nexttoggle = tog;
2265 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
2268 return (USBD_NORMAL_COMPLETION);
2271 /* XXX free chain */
2272 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
2273 return (USBD_NOMEM);
2277 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd,
2278 ehci_soft_qtd_t *sqtdend)
2283 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
2286 for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
2288 ehci_free_sqtd(sc, sqtd);
2295 * Close a reqular pipe.
2296 * Assumes that there are no pending transactions.
2299 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
2301 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
2302 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2303 ehci_soft_qh_t *sqh = epipe->sqh;
2306 ehci_rem_qh(sc, sqh, head);
2308 ehci_free_sqh(sc, epipe->sqh);
2312 * Abort a device request.
2313 * If this routine is called from a critical section it guarantees that the
2314 * request will be removed from the hardware scheduling and that the callback
2315 * for it will be called with USBD_CANCELLED status.
2316 * It's impossible to guarantee that the requested transfer will not
2317 * have happened since the hardware runs concurrently.
2318 * If the transaction has already happened we rely on the ordinary
2319 * interrupt processing to process it.
2320 * XXX This is most probably wrong.
2323 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2325 #define exfer EXFER(xfer)
2326 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2327 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2328 ehci_soft_qh_t *sqh = epipe->sqh;
2329 ehci_soft_qtd_t *sqtd;
2330 ehci_physaddr_t cur;
2334 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
2337 /* If we're dying, just do the software part. */
2339 xfer->status = status; /* make software ignore it */
2340 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2341 usb_transfer_complete(xfer);
2346 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
2347 panic("ehci_abort_xfer: not in process context");
2350 * Step 1: Make interrupt routine and hardware ignore xfer.
2353 xfer->status = status; /* make software ignore it */
2354 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2355 qhstatus = sqh->qh.qh_qtd.qtd_status;
2356 sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
2357 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2358 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
2359 if (sqtd == exfer->sqtdend)
2365 * Step 2: Wait until we know hardware has finished any possible
2366 * use of the xfer. Also make sure the soft interrupt routine
2371 #ifdef USB_USE_SOFTINTR
2372 sc->sc_softwake = 1;
2373 #endif /* USB_USE_SOFTINTR */
2374 usb_schedsoftintr(&sc->sc_bus);
2375 #ifdef USB_USE_SOFTINTR
2376 tsleep(&sc->sc_softwake, 0, "ehciab", 0);
2377 #endif /* USB_USE_SOFTINTR */
2381 * Step 3: Remove any vestiges of the xfer from the hardware.
2382 * The complication here is that the hardware may have executed
2383 * beyond the xfer we're trying to abort. So as we're scanning
2384 * the TDs of this xfer we check if the hardware points to
2387 crit_enter(); /* XXX why? */
2388 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
2390 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2391 hit |= cur == sqtd->physaddr;
2392 if (sqtd == exfer->sqtdend)
2395 sqtd = sqtd->nextqtd;
2396 /* Zap curqtd register if hardware pointed inside the xfer. */
2397 if (hit && sqtd != NULL) {
2398 DPRINTFN(1,("ehci_abort_xfer: cur=0x%08x\n", sqtd->physaddr));
2399 sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
2400 sqh->qh.qh_qtd.qtd_status = qhstatus;
2402 DPRINTFN(1,("ehci_abort_xfer: no hit\n"));
2406 * Step 4: Execute callback.
2411 usb_transfer_complete(xfer);
2418 ehci_timeout(void *addr)
2420 struct ehci_xfer *exfer = addr;
2421 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
2422 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2424 DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
2427 usbd_dump_pipe(exfer->xfer.pipe);
2431 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
2435 /* Execute the abort in a process context. */
2436 usb_init_task(&exfer->abort_task, ehci_timeout_task, addr);
2437 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task);
2441 ehci_timeout_task(void *addr)
2443 usbd_xfer_handle xfer = addr;
2445 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
2447 ehci_abort_xfer(xfer, USBD_TIMEOUT);
2452 * Some EHCI chips from VIA seem to trigger interrupts before writing back the
2453 * qTD status, or miss signalling occasionally under heavy load. If the host
2454 * machine is too fast, we we can miss transaction completion - when we scan
2455 * the active list the transaction still seems to be active. This generally
2456 * exhibits itself as a umass stall that never recovers.
2458 * We work around this behaviour by setting up this callback after any softintr
2459 * that completes with transactions still pending, giving us another chance to
2460 * check for completion after the writeback has taken place.
2463 ehci_intrlist_timeout(void *arg)
2465 ehci_softc_t *sc = arg;
2467 DPRINTFN(3, ("ehci_intrlist_timeout\n"));
2468 usb_schedsoftintr(&sc->sc_bus);
2471 /************************/
2474 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
2478 /* Insert last in queue. */
2479 err = usb_insert_transfer(xfer);
2483 /* Pipe isn't running, start first */
2484 return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2488 ehci_device_ctrl_start(usbd_xfer_handle xfer)
2490 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2494 return (USBD_IOERROR);
2497 if (!(xfer->rqflags & URQ_REQUEST)) {
2499 printf("ehci_device_ctrl_transfer: not a request\n");
2500 return (USBD_INVAL);
2504 err = ehci_device_request(xfer);
2508 if (sc->sc_bus.use_polling)
2509 ehci_waitintr(sc, xfer);
2510 return (USBD_IN_PROGRESS);
2514 ehci_device_ctrl_done(usbd_xfer_handle xfer)
2516 struct ehci_xfer *ex = EXFER(xfer);
2517 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2518 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
2520 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
2523 if (!(xfer->rqflags & URQ_REQUEST)) {
2524 panic("ehci_ctrl_done: not a request");
2528 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2529 ehci_del_intr_list(ex); /* remove from active list */
2530 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
2533 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
2536 /* Abort a device control request. */
2538 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
2540 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
2541 ehci_abort_xfer(xfer, USBD_CANCELLED);
2544 /* Close a device control pipe. */
2546 ehci_device_ctrl_close(usbd_pipe_handle pipe)
2548 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2549 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
2551 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
2552 ehci_close_pipe(pipe, sc->sc_async_head);
2556 ehci_device_request(usbd_xfer_handle xfer)
2558 #define exfer EXFER(xfer)
2559 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2560 usb_device_request_t *req = &xfer->request;
2561 usbd_device_handle dev = epipe->pipe.device;
2562 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2563 int addr = dev->address;
2564 ehci_soft_qtd_t *setup, *stat, *next;
2565 ehci_soft_qh_t *sqh;
2570 isread = req->bmRequestType & UT_READ;
2571 len = UGETW(req->wLength);
2573 DPRINTFN(3,("ehci_device_request type=0x%02x, request=0x%02x, "
2574 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2575 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2576 UGETW(req->wIndex), len, addr,
2577 epipe->pipe.endpoint->edesc->bEndpointAddress));
2579 setup = ehci_alloc_sqtd(sc);
2580 if (setup == NULL) {
2584 stat = ehci_alloc_sqtd(sc);
2591 epipe->u.ctl.length = len;
2593 /* Update device address and length since they may have changed
2594 during the setup of the control pipe in usbd_new_device(). */
2595 /* XXX This only needs to be done once, but it's too early in open. */
2596 /* XXXX Should not touch ED here! */
2598 (sqh->qh.qh_endp & htole32(~(EHCI_QH_ADDRMASK | EHCI_QH_MPLMASK))) |
2600 EHCI_QH_SET_ADDR(addr) |
2601 EHCI_QH_SET_MPL(UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize))
2604 /* Set up data transaction */
2606 ehci_soft_qtd_t *end;
2608 /* Start toggle at 1. */
2609 epipe->nexttoggle = 1;
2610 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
2614 end->nextqtd = stat;
2616 end->qtd.qtd_altnext = htole32(stat->physaddr);
2621 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
2624 setup->qtd.qtd_status = htole32(
2626 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
2627 EHCI_QTD_SET_CERR(3) |
2628 EHCI_QTD_SET_TOGGLE(0) |
2629 EHCI_QTD_SET_BYTES(sizeof *req)
2631 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
2632 setup->qtd.qtd_buffer_hi[0] = 0;
2633 setup->nextqtd = next;
2634 setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
2636 setup->len = sizeof *req;
2638 stat->qtd.qtd_status = htole32(
2640 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
2641 EHCI_QTD_SET_CERR(3) |
2642 EHCI_QTD_SET_TOGGLE(1) |
2645 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
2646 stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */
2647 stat->nextqtd = NULL;
2648 stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL;
2653 if (ehcidebug > 5) {
2654 DPRINTF(("ehci_device_request:\n"));
2656 ehci_dump_sqtds(setup);
2660 exfer->sqtdstart = setup;
2661 exfer->sqtdend = stat;
2663 if (!exfer->isdone) {
2664 printf("ehci_device_request: not done, exfer=%p\n", exfer);
2669 /* Insert qTD in QH list. */
2671 ehci_set_qh_qtd(sqh, setup);
2672 if (xfer->timeout && !sc->sc_bus.use_polling) {
2673 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2674 ehci_timeout, xfer);
2676 ehci_add_intr_list(sc, exfer);
2677 xfer->status = USBD_IN_PROGRESS;
2681 if (ehcidebug > 10) {
2682 DPRINTF(("ehci_device_request: status=%x\n",
2683 EOREAD4(sc, EHCI_USBSTS)));
2686 ehci_dump_sqh(sc->sc_async_head);
2688 ehci_dump_sqtds(setup);
2692 return (USBD_NORMAL_COMPLETION);
2695 ehci_free_sqtd(sc, stat);
2697 ehci_free_sqtd(sc, setup);
2699 DPRINTFN(-1,("ehci_device_request: no memory\n"));
2701 usb_transfer_complete(xfer);
2706 /************************/
2709 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
2713 /* Insert last in queue. */
2714 err = usb_insert_transfer(xfer);
2718 /* Pipe isn't running, start first */
2719 return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2723 ehci_device_bulk_start(usbd_xfer_handle xfer)
2725 #define exfer EXFER(xfer)
2726 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2727 usbd_device_handle dev = epipe->pipe.device;
2728 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2729 ehci_soft_qtd_t *data, *dataend;
2730 ehci_soft_qh_t *sqh;
2732 int len, isread, endpt;
2734 DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n",
2735 xfer, xfer->length, xfer->flags));
2738 return (USBD_IOERROR);
2741 if (xfer->rqflags & URQ_REQUEST)
2742 panic("ehci_device_bulk_start: a request");
2746 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
2747 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2750 epipe->u.bulk.length = len;
2752 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
2755 DPRINTFN(-1,("ehci_device_bulk_start: no memory\n"));
2757 usb_transfer_complete(xfer);
2762 if (ehcidebug > 5) {
2763 DPRINTF(("ehci_device_bulk_start: data(1)\n"));
2765 ehci_dump_sqtds(data);
2769 /* Set up interrupt info. */
2770 exfer->sqtdstart = data;
2771 exfer->sqtdend = dataend;
2773 if (!exfer->isdone) {
2774 printf("ehci_device_bulk_start: not done, ex=%p\n", exfer);
2780 ehci_set_qh_qtd(sqh, data);
2781 if (xfer->timeout && !sc->sc_bus.use_polling) {
2782 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2783 ehci_timeout, xfer);
2785 ehci_add_intr_list(sc, exfer);
2786 xfer->status = USBD_IN_PROGRESS;
2790 if (ehcidebug > 10) {
2791 DPRINTF(("ehci_device_bulk_start: data(2)\n"));
2793 DPRINTF(("ehci_device_bulk_start: data(3)\n"));
2796 printf("async_head:\n");
2797 ehci_dump_sqh(sc->sc_async_head);
2801 ehci_dump_sqtds(data);
2805 if (sc->sc_bus.use_polling)
2806 ehci_waitintr(sc, xfer);
2808 return (USBD_IN_PROGRESS);
2813 ehci_device_bulk_abort(usbd_xfer_handle xfer)
2815 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
2816 ehci_abort_xfer(xfer, USBD_CANCELLED);
2820 * Close a device bulk pipe.
2823 ehci_device_bulk_close(usbd_pipe_handle pipe)
2825 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2827 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
2828 ehci_close_pipe(pipe, sc->sc_async_head);
2832 ehci_device_bulk_done(usbd_xfer_handle xfer)
2834 struct ehci_xfer *ex = EXFER(xfer);
2835 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2836 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
2838 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
2839 xfer, xfer->actlen));
2841 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2842 ehci_del_intr_list(ex); /* remove from active list */
2843 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
2846 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
2849 /************************/
2852 ehci_device_intr_transfer(usbd_xfer_handle xfer)
2854 return USBD_IOERROR;
2858 ehci_device_intr_start(usbd_xfer_handle xfer)
2860 return USBD_IOERROR;
2864 ehci_device_intr_abort(usbd_xfer_handle xfer)
2869 ehci_device_intr_close(usbd_pipe_handle pipe)
2874 ehci_device_intr_done(usbd_xfer_handle xfer)
2878 /************************/
2881 ehci_device_isoc_transfer(usbd_xfer_handle xfer)
2883 return USBD_IOERROR;
2887 ehci_device_isoc_start(usbd_xfer_handle xfer)
2889 return USBD_IOERROR;
2893 ehci_device_isoc_abort(usbd_xfer_handle xfer)
2898 ehci_device_isoc_close(usbd_pipe_handle pipe)
2903 ehci_device_isoc_done(usbd_xfer_handle xfer)