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.7 2005/06/02 20:40:40 dillon 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 interrupt isochronous transfers, so
58 * devices using them don't work.
59 * Interrupt transfers are not difficult, it's just not done.
61 * 3) The meaty part to implement is the support for USB 2.0 hubs.
62 * They are quite compolicated since the need to be able to do
63 * "transaction translation", i.e., converting to/from USB 2 and USB 1.
64 * So the hub driver needs to handle and schedule these things, to
65 * assign place in frame where different devices get to go. See chapter
66 * on hubs in USB 2.0 for details.
68 * 4) command failures are not recovered correctly
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #if defined(__NetBSD__) || defined(__OpenBSD__)
76 #include <sys/device.h>
77 #include <sys/select.h>
78 #elif defined(__FreeBSD__) || defined(__DragonFly__)
79 #include <sys/endian.h>
80 #include <sys/module.h>
82 #include <machine/bus_pio.h>
83 #include <machine/bus_memio.h>
85 #if defined(DIAGNOSTIC) && defined(__i386__)
86 #include <machine/cpu.h>
90 #include <sys/queue.h>
91 #include <sys/sysctl.h>
92 #include <sys/thread2.h>
94 #include <machine/bus.h>
95 #include <machine/endian.h>
97 #include <bus/usb/usb.h>
98 #include <bus/usb/usbdi.h>
99 #include <bus/usb/usbdivar.h>
100 #include <bus/usb/usb_mem.h>
101 #include <bus/usb/usb_quirks.h>
103 #include <bus/usb/ehcireg.h>
104 #include <bus/usb/ehcivar.h>
106 #if defined(__FreeBSD__) || defined(__DragonFly__)
107 #include <machine/clock.h>
109 #define delay(d) DELAY(d)
113 #define DPRINTF(x) if (ehcidebug) logprintf x
114 #define DPRINTFN(n,x) if (ehcidebug>(n)) logprintf x
116 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
117 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
118 &ehcidebug, 0, "ehci debug level");
120 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
124 #define DPRINTFN(n,x)
128 struct usbd_pipe pipe;
133 ehci_soft_qtd_t *qtd;
134 /* ehci_soft_itd_t *itd; */
141 /*ehci_soft_qtd_t *setup, *data, *stat;*/
154 #if defined(__NetBSD__) || defined(__OpenBSD__)
155 Static void ehci_shutdown(void *);
156 Static void ehci_power(int, void *);
159 Static usbd_status ehci_open(usbd_pipe_handle);
160 Static void ehci_poll(struct usbd_bus *);
161 Static void ehci_softintr(void *);
162 Static int ehci_intr1(ehci_softc_t *);
163 Static void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
164 Static void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
165 Static void ehci_idone(struct ehci_xfer *);
166 Static void ehci_timeout(void *);
167 Static void ehci_timeout_task(void *);
169 Static usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
170 Static void ehci_freem(struct usbd_bus *, usb_dma_t *);
172 Static usbd_xfer_handle ehci_allocx(struct usbd_bus *);
173 Static void ehci_freex(struct usbd_bus *, usbd_xfer_handle);
175 Static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle);
176 Static usbd_status ehci_root_ctrl_start(usbd_xfer_handle);
177 Static void ehci_root_ctrl_abort(usbd_xfer_handle);
178 Static void ehci_root_ctrl_close(usbd_pipe_handle);
179 Static void ehci_root_ctrl_done(usbd_xfer_handle);
181 Static usbd_status ehci_root_intr_transfer(usbd_xfer_handle);
182 Static usbd_status ehci_root_intr_start(usbd_xfer_handle);
183 Static void ehci_root_intr_abort(usbd_xfer_handle);
184 Static void ehci_root_intr_close(usbd_pipe_handle);
185 Static void ehci_root_intr_done(usbd_xfer_handle);
187 Static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle);
188 Static usbd_status ehci_device_ctrl_start(usbd_xfer_handle);
189 Static void ehci_device_ctrl_abort(usbd_xfer_handle);
190 Static void ehci_device_ctrl_close(usbd_pipe_handle);
191 Static void ehci_device_ctrl_done(usbd_xfer_handle);
193 Static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle);
194 Static usbd_status ehci_device_bulk_start(usbd_xfer_handle);
195 Static void ehci_device_bulk_abort(usbd_xfer_handle);
196 Static void ehci_device_bulk_close(usbd_pipe_handle);
197 Static void ehci_device_bulk_done(usbd_xfer_handle);
199 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle);
200 Static usbd_status ehci_device_intr_start(usbd_xfer_handle);
201 Static void ehci_device_intr_abort(usbd_xfer_handle);
202 Static void ehci_device_intr_close(usbd_pipe_handle);
203 Static void ehci_device_intr_done(usbd_xfer_handle);
205 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle);
206 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle);
207 Static void ehci_device_isoc_abort(usbd_xfer_handle);
208 Static void ehci_device_isoc_close(usbd_pipe_handle);
209 Static void ehci_device_isoc_done(usbd_xfer_handle);
211 Static void ehci_device_clear_toggle(usbd_pipe_handle pipe);
212 Static void ehci_noop(usbd_pipe_handle pipe);
214 Static int ehci_str(usb_string_descriptor_t *, int, char *);
215 Static void ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
216 Static void ehci_pcd_able(ehci_softc_t *, int);
217 Static void ehci_pcd_enable(void *);
218 Static void ehci_disown(ehci_softc_t *, int, int);
220 Static ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *);
221 Static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
223 Static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
224 Static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
225 Static usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *,
226 ehci_softc_t *, int, int, usbd_xfer_handle,
227 ehci_soft_qtd_t **, ehci_soft_qtd_t **);
228 Static void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *,
231 Static usbd_status ehci_device_request(usbd_xfer_handle xfer);
233 Static void ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *);
234 Static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
236 Static void ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
237 Static void ehci_sync_hc(ehci_softc_t *);
239 Static void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
240 Static void ehci_abort_xfer(usbd_xfer_handle, usbd_status);
243 Static void ehci_dump_regs(ehci_softc_t *);
244 void ehci_dump(void);
245 Static ehci_softc_t *theehci;
246 Static void ehci_dump_link(ehci_link_t, int);
247 Static void ehci_dump_sqtds(ehci_soft_qtd_t *);
248 Static void ehci_dump_sqtd(ehci_soft_qtd_t *);
249 Static void ehci_dump_qtd(ehci_qtd_t *);
250 Static void ehci_dump_sqh(ehci_soft_qh_t *);
252 Static void ehci_dump_exfer(struct ehci_xfer *);
256 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
258 #define EHCI_INTR_ENDPT 1
260 #define ehci_add_intr_list(sc, ex) \
261 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext);
262 #define ehci_del_intr_list(ex) \
264 LIST_REMOVE((ex), inext); \
265 (ex)->inext.le_prev = NULL; \
267 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL)
269 Static struct usbd_bus_methods ehci_bus_methods = {
279 Static struct usbd_pipe_methods ehci_root_ctrl_methods = {
280 ehci_root_ctrl_transfer,
281 ehci_root_ctrl_start,
282 ehci_root_ctrl_abort,
283 ehci_root_ctrl_close,
288 Static struct usbd_pipe_methods ehci_root_intr_methods = {
289 ehci_root_intr_transfer,
290 ehci_root_intr_start,
291 ehci_root_intr_abort,
292 ehci_root_intr_close,
297 Static struct usbd_pipe_methods ehci_device_ctrl_methods = {
298 ehci_device_ctrl_transfer,
299 ehci_device_ctrl_start,
300 ehci_device_ctrl_abort,
301 ehci_device_ctrl_close,
303 ehci_device_ctrl_done,
306 Static struct usbd_pipe_methods ehci_device_intr_methods = {
307 ehci_device_intr_transfer,
308 ehci_device_intr_start,
309 ehci_device_intr_abort,
310 ehci_device_intr_close,
311 ehci_device_clear_toggle,
312 ehci_device_intr_done,
315 Static struct usbd_pipe_methods ehci_device_bulk_methods = {
316 ehci_device_bulk_transfer,
317 ehci_device_bulk_start,
318 ehci_device_bulk_abort,
319 ehci_device_bulk_close,
320 ehci_device_clear_toggle,
321 ehci_device_bulk_done,
324 Static struct usbd_pipe_methods ehci_device_isoc_methods = {
325 ehci_device_isoc_transfer,
326 ehci_device_isoc_start,
327 ehci_device_isoc_abort,
328 ehci_device_isoc_close,
330 ehci_device_isoc_done,
334 ehci_init(ehci_softc_t *sc)
336 u_int32_t version, sparams, cparams, hcr;
341 DPRINTF(("ehci_init: start\n"));
346 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
348 version = EREAD2(sc, EHCI_HCIVERSION);
349 printf("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev),
350 version >> 8, version & 0xff);
352 sparams = EREAD4(sc, EHCI_HCSPARAMS);
353 DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
354 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
355 if (EHCI_HCS_N_CC(sparams) != sc->sc_ncomp) {
356 printf("%s: wrong number of companions (%d != %d)\n",
357 USBDEVNAME(sc->sc_bus.bdev),
358 EHCI_HCS_N_CC(sparams), sc->sc_ncomp);
359 return (USBD_IOERROR);
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 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
383 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
384 usb_delay_ms(&sc->sc_bus, 1);
385 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
386 for (i = 0; i < 100; i++) {
387 usb_delay_ms(&sc->sc_bus, 1);
388 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
393 printf("%s: reset timeout\n",
394 USBDEVNAME(sc->sc_bus.bdev));
395 return (USBD_IOERROR);
398 /* frame list size at default, read back what we got and use that */
399 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
400 case 0: sc->sc_flsize = 1024*4; break;
401 case 1: sc->sc_flsize = 512*4; break;
402 case 2: sc->sc_flsize = 256*4; break;
403 case 3: return (USBD_IOERROR);
405 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize,
406 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
409 DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize));
411 /* Set up the bus struct. */
412 sc->sc_bus.methods = &ehci_bus_methods;
413 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
415 #if defined(__NetBSD__) || defined(__OpenBSD__)
416 sc->sc_powerhook = powerhook_establish(ehci_power, sc);
417 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
420 sc->sc_eintrs = EHCI_NORMAL_INTRS;
422 /* Allocate dummy QH that starts the async list. */
423 sqh = ehci_alloc_sqh(sc);
430 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
432 htole32(sqh->physaddr | EHCI_LINK_QH);
433 sqh->qh.qh_curqtd = EHCI_NULL;
435 /* Fill the overlay qTD */
436 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
437 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
438 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
446 /* Point to async list */
447 sc->sc_async_head = sqh;
448 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
450 usb_callout_init(sc->sc_tmo_pcd);
452 lockinit(&sc->sc_doorbell_lock, 0, "ehcidb", 0, 0);
454 /* Enable interrupts */
455 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
457 /* Turn on controller */
458 EOWRITE4(sc, EHCI_USBCMD,
459 EHCI_CMD_ITC_8 | /* 8 microframes */
460 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
465 /* Take over port ownership */
466 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
468 for (i = 0; i < 100; i++) {
469 usb_delay_ms(&sc->sc_bus, 1);
470 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
475 printf("%s: run timeout\n", USBDEVNAME(sc->sc_bus.bdev));
476 return (USBD_IOERROR);
479 return (USBD_NORMAL_COMPLETION);
483 ehci_free_sqh(sc, sc->sc_async_head);
486 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
493 ehci_softc_t *sc = v;
495 if (sc == NULL || sc->sc_dying)
498 /* If we get an interrupt while polling, then just ignore it. */
499 if (sc->sc_bus.use_polling) {
501 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
506 return (ehci_intr1(sc));
510 ehci_intr1(ehci_softc_t *sc)
512 u_int32_t intrs, eintrs;
514 DPRINTFN(20,("ehci_intr1: enter\n"));
516 /* In case the interrupt occurs before initialization has completed. */
519 printf("ehci_intr: sc == NULL\n");
524 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
529 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
530 eintrs = intrs & sc->sc_eintrs;
531 DPRINTFN(7, ("ehci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
532 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
537 sc->sc_bus.intr_context++;
538 sc->sc_bus.no_intrs++;
539 if (eintrs & EHCI_STS_IAA) {
540 DPRINTF(("ehci_intr1: door bell\n"));
541 wakeup(&sc->sc_async_head);
542 eintrs &= ~EHCI_STS_IAA;
544 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
545 DPRINTFN(5,("ehci_intr1: %s %s\n",
546 eintrs & EHCI_STS_INT ? "INT" : "",
547 eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
548 usb_schedsoftintr(&sc->sc_bus);
549 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
551 if (eintrs & EHCI_STS_HSE) {
552 printf("%s: unrecoverable error, controller halted\n",
553 USBDEVNAME(sc->sc_bus.bdev));
556 if (eintrs & EHCI_STS_PCD) {
557 ehci_pcd(sc, sc->sc_intrxfer);
559 * Disable PCD interrupt for now, because it will be
560 * on until the port has been reset.
562 ehci_pcd_able(sc, 0);
563 /* Do not allow RHSC interrupts > 1 per second */
564 usb_callout(sc->sc_tmo_pcd, hz, ehci_pcd_enable, sc);
565 eintrs &= ~EHCI_STS_PCD;
568 sc->sc_bus.intr_context--;
571 /* Block unprocessed interrupts. */
572 sc->sc_eintrs &= ~eintrs;
573 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
574 printf("%s: blocking intrs 0x%x\n",
575 USBDEVNAME(sc->sc_bus.bdev), eintrs);
582 ehci_pcd_able(ehci_softc_t *sc, int on)
584 DPRINTFN(4, ("ehci_pcd_able: on=%d\n", on));
586 sc->sc_eintrs |= EHCI_STS_PCD;
588 sc->sc_eintrs &= ~EHCI_STS_PCD;
589 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
593 ehci_pcd_enable(void *v_sc)
595 ehci_softc_t *sc = v_sc;
597 ehci_pcd_able(sc, 1);
601 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
603 usbd_pipe_handle pipe;
608 /* Just ignore the change. */
614 p = KERNADDR(&xfer->dmabuf, 0);
615 m = min(sc->sc_noport, xfer->length * 8 - 1);
616 memset(p, 0, xfer->length);
617 for (i = 1; i <= m; i++) {
618 /* Pick out CHANGE bits from the status reg. */
619 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
620 p[i/8] |= 1 << (i%8);
622 DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
623 xfer->actlen = xfer->length;
624 xfer->status = USBD_NORMAL_COMPLETION;
626 usb_transfer_complete(xfer);
630 ehci_softintr(void *v)
632 ehci_softc_t *sc = v;
633 struct ehci_xfer *ex, *nextex;
635 DPRINTFN(10,("%s: ehci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
636 sc->sc_bus.intr_context));
638 sc->sc_bus.intr_context++;
641 * The only explanation I can think of for why EHCI is as brain dead
642 * as UHCI interrupt-wise is that Intel was involved in both.
643 * An interrupt just tells us that something is done, we have no
644 * clue what, so we need to scan through all active transfers. :-(
646 for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
647 nextex = LIST_NEXT(ex, inext);
648 ehci_check_intr(sc, ex);
651 #ifdef USB_USE_SOFTINTR
652 if (sc->sc_softwake) {
654 wakeup(&sc->sc_softwake);
656 #endif /* USB_USE_SOFTINTR */
658 sc->sc_bus.intr_context--;
661 /* Check for an interrupt. */
663 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
665 ehci_soft_qtd_t *sqtd, *lsqtd;
668 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
670 if (ex->sqtdstart == NULL) {
671 printf("ehci_check_intr: sqtdstart=NULL\n");
677 printf("ehci_check_intr: sqtd==0\n");
682 * If the last TD is still active we need to check whether there
683 * is a an error somewhere in the middle, or whether there was a
684 * short packet (SPD and not ACTIVE).
686 if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
687 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
688 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
689 status = le32toh(sqtd->qtd.qtd_status);
690 /* If there's an active QTD the xfer isn't done. */
691 if (status & EHCI_QTD_ACTIVE)
693 /* Any kind of error makes the xfer done. */
694 if (status & EHCI_QTD_HALTED)
696 /* We want short packets, and it is short: it's done */
697 if (EHCI_QTD_GET_BYTES(status) != 0)
700 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
705 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
706 usb_uncallout(ex->xfer.timeout_handle, ehci_timeout, ex);
711 ehci_idone(struct ehci_xfer *ex)
713 usbd_xfer_handle xfer = &ex->xfer;
714 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
715 ehci_soft_qtd_t *sqtd;
716 u_int32_t status = 0, nstatus;
719 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
726 printf("ehci_idone: ex is done!\n ");
729 printf("ehci_idone: ex=%p is done!\n", ex);
738 if (xfer->status == USBD_CANCELLED ||
739 xfer->status == USBD_TIMEOUT) {
740 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
745 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
747 ehci_dump_sqtds(ex->sqtdstart);
750 /* The transfer is done, compute actual length and status. */
752 for (sqtd = ex->sqtdstart; sqtd != NULL; sqtd = sqtd->nextqtd) {
753 nstatus = le32toh(sqtd->qtd.qtd_status);
754 if (nstatus & EHCI_QTD_ACTIVE)
758 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
759 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
762 /* If there are left over TDs we need to update the toggle. */
764 printf("ehci_idone: need toggle update status=%08x nstatus=%08x\n", status, nstatus);
766 ehci_dump_sqh(epipe->sqh);
767 ehci_dump_sqtds(ex->sqtdstart);
769 epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
772 status &= EHCI_QTD_STATERRS;
773 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, status=0x%x\n",
774 xfer->length, actlen, status));
775 xfer->actlen = actlen;
780 bitmask_snprintf((u_int32_t)status,
781 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR"
782 "\3MISSED", sbuf, sizeof(sbuf));
784 DPRINTFN((status == EHCI_QTD_HALTED)*/*10*/2,
785 ("ehci_idone: error, addr=%d, endpt=0x%02x, "
787 xfer->pipe->device->address,
788 xfer->pipe->endpoint->edesc->bEndpointAddress,
791 ehci_dump_sqh(epipe->sqh);
792 ehci_dump_sqtds(ex->sqtdstart);
795 if (status == EHCI_QTD_HALTED)
796 xfer->status = USBD_STALLED;
798 xfer->status = USBD_IOERROR; /* more info XXX */
800 xfer->status = USBD_NORMAL_COMPLETION;
803 usb_transfer_complete(xfer);
804 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
808 * Wait here until controller claims to have an interrupt.
809 * Then call ehci_intr and return. Use timeout to avoid waiting
813 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
815 int timo = xfer->timeout;
819 xfer->status = USBD_IN_PROGRESS;
820 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
821 usb_delay_ms(&sc->sc_bus, 1);
824 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
826 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
833 if (xfer->status != USBD_IN_PROGRESS)
839 DPRINTF(("ehci_waitintr: timeout\n"));
840 xfer->status = USBD_TIMEOUT;
841 usb_transfer_complete(xfer);
842 /* XXX should free TD */
846 ehci_poll(struct usbd_bus *bus)
848 ehci_softc_t *sc = (ehci_softc_t *)bus;
852 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
854 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
859 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
863 #if defined(__NetBSD__) || defined(__OpenBSD__)
865 ehci_detach(struct ehci_softc *sc, int flags)
869 if (sc->sc_child != NULL)
870 rv = config_detach(sc->sc_child, flags);
875 usb_uncallout(sc->sc_tmo_pcd, ehci_pcd_enable, sc);
877 if (sc->sc_powerhook != NULL)
878 powerhook_disestablish(sc->sc_powerhook);
879 if (sc->sc_shutdownhook != NULL)
880 shutdownhook_disestablish(sc->sc_shutdownhook);
882 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
884 /* XXX free other data structures XXX */
890 #if defined(__NetBSD__) || defined(__OpenBSD__)
892 ehci_activate(device_ptr_t self, enum devact act)
894 struct ehci_softc *sc = (struct ehci_softc *)self;
901 case DVACT_DEACTIVATE:
902 if (sc->sc_child != NULL)
903 rv = config_deactivate(sc->sc_child);
912 * Handle suspend/resume.
914 * We need to switch to polling mode here, because this routine is
915 * called from an intterupt context. This is all right since we
916 * are almost suspended anyway.
918 #if defined(__NetBSD__) || defined(__OpenBSD__)
920 ehci_power(int why, void *v)
922 ehci_softc_t *sc = v;
926 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
935 sc->sc_bus.use_polling++;
938 ctl = OREAD4(sc, EHCI_CONTROL) & ~EHCI_HCFS_MASK;
939 if (sc->sc_control == 0) {
941 * Preserve register values, in case that APM BIOS
942 * does not recover them.
944 sc->sc_control = ctl;
945 sc->sc_intre = OREAD4(sc, EHCI_INTERRUPT_ENABLE);
947 ctl |= EHCI_HCFS_SUSPEND;
948 OWRITE4(sc, EHCI_CONTROL, ctl);
950 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
951 sc->sc_bus.use_polling--;
954 sc->sc_bus.use_polling++;
957 /* Some broken BIOSes do not recover these values */
958 OWRITE4(sc, EHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
959 OWRITE4(sc, EHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
960 OWRITE4(sc, EHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
962 OWRITE4(sc, EHCI_INTERRUPT_ENABLE,
963 sc->sc_intre & (EHCI_ALL_INTRS | EHCI_MIE));
965 ctl = sc->sc_control;
967 ctl = OREAD4(sc, EHCI_CONTROL);
968 ctl |= EHCI_HCFS_RESUME;
969 OWRITE4(sc, EHCI_CONTROL, ctl);
970 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
971 ctl = (ctl & ~EHCI_HCFS_MASK) | EHCI_HCFS_OPERATIONAL;
972 OWRITE4(sc, EHCI_CONTROL, ctl);
973 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
974 sc->sc_control = sc->sc_intre = 0;
976 sc->sc_bus.use_polling--;
978 case PWR_SOFTSUSPEND:
979 case PWR_SOFTSTANDBY:
988 * Shut down the controller when the system is going down.
990 #if defined(__NetBSD__) || defined(__OpenBSD__)
992 ehci_shutdown(void *v)
994 ehci_softc_t *sc = v;
996 DPRINTF(("ehci_shutdown: stopping the HC\n"));
997 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
998 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1003 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
1007 err = usb_allocmem(bus, size, 0, dma);
1010 printf("ehci_allocm: usb_allocmem()=%d\n", err);
1016 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
1018 usb_freemem(bus, dma);
1022 ehci_allocx(struct usbd_bus *bus)
1024 struct ehci_softc *sc = (struct ehci_softc *)bus;
1025 usbd_xfer_handle xfer;
1027 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
1029 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
1031 if (xfer->busy_free != XFER_FREE) {
1032 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
1037 xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_INTWAIT);
1040 memset(xfer, 0, sizeof (struct ehci_xfer));
1042 EXFER(xfer)->isdone = 1;
1043 xfer->busy_free = XFER_BUSY;
1050 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1052 struct ehci_softc *sc = (struct ehci_softc *)bus;
1055 if (xfer->busy_free != XFER_BUSY) {
1056 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
1060 xfer->busy_free = XFER_FREE;
1061 if (!EXFER(xfer)->isdone) {
1062 printf("ehci_freex: !isdone\n");
1066 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
1070 ehci_device_clear_toggle(usbd_pipe_handle pipe)
1072 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1074 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
1075 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
1078 usbd_dump_pipe(pipe);
1080 epipe->nexttoggle = 0;
1084 ehci_noop(usbd_pipe_handle pipe)
1090 ehci_dump_regs(ehci_softc_t *sc)
1093 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1094 EOREAD4(sc, EHCI_USBCMD),
1095 EOREAD4(sc, EHCI_USBSTS),
1096 EOREAD4(sc, EHCI_USBINTR));
1097 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1098 EOREAD4(sc, EHCI_FRINDEX),
1099 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1100 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1101 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1102 for (i = 1; i <= sc->sc_noport; i++)
1103 printf("port %d status=0x%08x\n", i,
1104 EOREAD4(sc, EHCI_PORTSC(i)));
1108 * Unused function - this is meant to be called from a kernel
1114 ehci_dump_regs(theehci);
1118 ehci_dump_link(ehci_link_t link, int type)
1120 link = le32toh(link);
1121 printf("0x%08x", link);
1122 if (link & EHCI_LINK_TERMINATE)
1127 switch (EHCI_LINK_TYPE(link)) {
1128 case EHCI_LINK_ITD: printf("ITD"); break;
1129 case EHCI_LINK_QH: printf("QH"); break;
1130 case EHCI_LINK_SITD: printf("SITD"); break;
1131 case EHCI_LINK_FSTN: printf("FSTN"); break;
1139 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1145 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1146 ehci_dump_sqtd(sqtd);
1147 stop = sqtd->qtd.qtd_next & EHCI_LINK_TERMINATE;
1150 printf("dump aborted, too many TDs\n");
1154 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1156 printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr);
1157 ehci_dump_qtd(&sqtd->qtd);
1161 ehci_dump_qtd(ehci_qtd_t *qtd)
1166 printf(" next="); ehci_dump_link(qtd->qtd_next, 0);
1167 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
1169 s = le32toh(qtd->qtd_status);
1170 bitmask_snprintf(EHCI_QTD_GET_STATUS(s),
1171 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1172 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
1173 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
1174 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
1175 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
1176 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
1177 EHCI_QTD_GET_PID(s), sbuf);
1178 for (s = 0; s < 5; s++)
1179 printf(" buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
1183 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1185 ehci_qh_t *qh = &sqh->qh;
1186 u_int32_t endp, endphub;
1188 printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr);
1189 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
1190 endp = le32toh(qh->qh_endp);
1191 printf(" endp=0x%08x\n", endp);
1192 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
1193 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1194 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
1195 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
1196 printf(" mpl=0x%x ctl=%d nrl=%d\n",
1197 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
1198 EHCI_QH_GET_NRL(endp));
1199 endphub = le32toh(qh->qh_endphub);
1200 printf(" endphub=0x%08x\n", endphub);
1201 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
1202 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
1203 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1204 EHCI_QH_GET_MULT(endphub));
1205 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
1206 printf("Overlay qTD:\n");
1207 ehci_dump_qtd(&qh->qh_qtd);
1212 ehci_dump_exfer(struct ehci_xfer *ex)
1214 printf("ehci_dump_exfer: ex=%p\n", ex);
1220 ehci_open(usbd_pipe_handle pipe)
1222 usbd_device_handle dev = pipe->device;
1223 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
1224 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1225 u_int8_t addr = dev->address;
1226 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1227 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1228 ehci_soft_qh_t *sqh;
1232 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1233 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1236 return (USBD_IOERROR);
1238 epipe->nexttoggle = 0;
1240 if (addr == sc->sc_addr) {
1241 switch (ed->bEndpointAddress) {
1242 case USB_CONTROL_ENDPOINT:
1243 pipe->methods = &ehci_root_ctrl_methods;
1245 case UE_DIR_IN | EHCI_INTR_ENDPT:
1246 pipe->methods = &ehci_root_intr_methods;
1249 return (USBD_INVAL);
1251 return (USBD_NORMAL_COMPLETION);
1254 /* XXX All this stuff is only valid for async. */
1255 switch (dev->speed) {
1256 case USB_SPEED_LOW: speed = EHCI_QH_SPEED_LOW; break;
1257 case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
1258 case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
1259 default: panic("ehci_open: bad device speed %d", dev->speed);
1262 sqh = ehci_alloc_sqh(sc);
1265 /* qh_link filled when the QH is added */
1266 sqh->qh.qh_endp = htole32(
1267 EHCI_QH_SET_ADDR(addr) |
1268 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
1269 EHCI_QH_SET_EPS(speed) |
1271 EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
1272 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
1274 EHCI_QH_SET_NRL(naks)
1276 sqh->qh.qh_endphub = htole32(
1279 /* XXX interrupt mask */
1281 sqh->qh.qh_curqtd = EHCI_NULL;
1282 /* Fill the overlay qTD */
1283 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
1284 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
1285 sqh->qh.qh_qtd.qtd_status = htole32(0);
1291 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
1292 0, &epipe->u.ctl.reqdma);
1295 printf("ehci_open: usb_allocmem()=%d\n", err);
1299 pipe->methods = &ehci_device_ctrl_methods;
1301 ehci_add_qh(sqh, sc->sc_async_head);
1305 pipe->methods = &ehci_device_bulk_methods;
1307 ehci_add_qh(sqh, sc->sc_async_head);
1311 pipe->methods = &ehci_device_intr_methods;
1312 return (USBD_INVAL);
1313 case UE_ISOCHRONOUS:
1314 pipe->methods = &ehci_device_isoc_methods;
1315 return (USBD_INVAL);
1317 return (USBD_INVAL);
1319 return (USBD_NORMAL_COMPLETION);
1322 ehci_free_sqh(sc, sqh);
1324 return (USBD_NOMEM);
1328 * Add an ED to the schedule. Called while in a critical section.
1331 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1333 sqh->next = head->next;
1334 sqh->qh.qh_link = head->qh.qh_link;
1336 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
1339 if (ehcidebug > 5) {
1340 printf("ehci_add_qh:\n");
1347 * Remove an ED from the schedule. Called while in a critical section.
1350 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1355 for (p = head; p != NULL && p->next != sqh; p = p->next)
1358 panic("ehci_rem_qh: ED not found");
1359 p->next = sqh->next;
1360 p->qh.qh_link = sqh->qh.qh_link;
1366 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
1368 /* Halt while we are messing. */
1369 sqh->qh.qh_qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
1370 sqh->qh.qh_curqtd = 0;
1371 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
1374 sqh->qh.qh_qtd.qtd_status &= htole32(~EHCI_QTD_HALTED);
1378 * Ensure that the HC has released all references to the QH. We do this
1379 * by asking for a Async Advance Doorbell interrupt and then we wait for
1381 * To make this easier we first obtain exclusive use of the doorbell.
1384 ehci_sync_hc(ehci_softc_t *sc)
1389 DPRINTFN(2,("ehci_sync_hc: dying\n"));
1392 DPRINTFN(2,("ehci_sync_hc: enter\n"));
1394 lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE, NULL, NULL);
1396 /* ask for doorbell */
1397 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
1398 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1399 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1400 error = tsleep(&sc->sc_async_head, 0, "ehcidi", hz); /* bell wait */
1401 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1402 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1404 /* release doorbell */
1405 lockmgr(&sc->sc_doorbell_lock, LK_RELEASE, NULL, NULL);
1408 printf("ehci_sync_hc: tsleep() = %d\n", error);
1410 DPRINTFN(2,("ehci_sync_hc: exit\n"));
1416 * Data structures and routines to emulate the root hub.
1418 Static usb_device_descriptor_t ehci_devd = {
1419 USB_DEVICE_DESCRIPTOR_SIZE,
1420 UDESC_DEVICE, /* type */
1421 {0x00, 0x02}, /* USB version */
1422 UDCLASS_HUB, /* class */
1423 UDSUBCLASS_HUB, /* subclass */
1424 UDPROTO_HSHUBSTT, /* protocol */
1425 64, /* max packet */
1426 {0},{0},{0x00,0x01}, /* device id */
1427 1,2,0, /* string indicies */
1428 1 /* # of configurations */
1431 Static usb_device_qualifier_t ehci_odevd = {
1432 USB_DEVICE_DESCRIPTOR_SIZE,
1433 UDESC_DEVICE_QUALIFIER, /* type */
1434 {0x00, 0x02}, /* USB version */
1435 UDCLASS_HUB, /* class */
1436 UDSUBCLASS_HUB, /* subclass */
1437 UDPROTO_FSHUB, /* protocol */
1438 64, /* max packet */
1439 1, /* # of configurations */
1443 Static usb_config_descriptor_t ehci_confd = {
1444 USB_CONFIG_DESCRIPTOR_SIZE,
1446 {USB_CONFIG_DESCRIPTOR_SIZE +
1447 USB_INTERFACE_DESCRIPTOR_SIZE +
1448 USB_ENDPOINT_DESCRIPTOR_SIZE},
1456 Static usb_interface_descriptor_t ehci_ifcd = {
1457 USB_INTERFACE_DESCRIPTOR_SIZE,
1468 Static usb_endpoint_descriptor_t ehci_endpd = {
1469 USB_ENDPOINT_DESCRIPTOR_SIZE,
1471 UE_DIR_IN | EHCI_INTR_ENDPT,
1473 {8, 0}, /* max packet */
1477 Static usb_hub_descriptor_t ehci_hubd = {
1478 USB_HUB_DESCRIPTOR_SIZE,
1489 usb_string_descriptor_t *p;
1497 p->bLength = 2 * strlen(s) + 2;
1500 p->bDescriptorType = UDESC_STRING;
1502 for (i = 0; s[i] && l > 1; i++, l -= 2)
1503 USETW2(p->bString[i], 0, s[i]);
1508 * Simulate a hardware hub by handling all the necessary requests.
1511 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
1515 /* Insert last in queue. */
1516 err = usb_insert_transfer(xfer);
1520 /* Pipe isn't running, start first */
1521 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1525 ehci_root_ctrl_start(usbd_xfer_handle xfer)
1527 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
1528 usb_device_request_t *req;
1531 int len, value, index, l, totlen = 0;
1532 usb_port_status_t ps;
1533 usb_hub_descriptor_t hubd;
1538 return (USBD_IOERROR);
1541 if (!(xfer->rqflags & URQ_REQUEST))
1543 return (USBD_INVAL);
1545 req = &xfer->request;
1547 DPRINTFN(4,("ehci_root_ctrl_control type=0x%02x request=%02x\n",
1548 req->bmRequestType, req->bRequest));
1550 len = UGETW(req->wLength);
1551 value = UGETW(req->wValue);
1552 index = UGETW(req->wIndex);
1555 buf = KERNADDR(&xfer->dmabuf, 0);
1557 #define C(x,y) ((x) | ((y) << 8))
1558 switch(C(req->bRequest, req->bmRequestType)) {
1559 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1560 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1561 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1563 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1564 * for the integrated root hub.
1567 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1569 *(u_int8_t *)buf = sc->sc_conf;
1573 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1574 DPRINTFN(8,("ehci_root_ctrl_control wValue=0x%04x\n", value));
1575 switch(value >> 8) {
1577 if ((value & 0xff) != 0) {
1581 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1582 USETW(ehci_devd.idVendor, sc->sc_id_vendor);
1583 memcpy(buf, &ehci_devd, l);
1586 * We can't really operate at another speed, but the spec says
1587 * we need this descriptor.
1589 case UDESC_DEVICE_QUALIFIER:
1590 if ((value & 0xff) != 0) {
1594 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1595 memcpy(buf, &ehci_odevd, l);
1598 * We can't really operate at another speed, but the spec says
1599 * we need this descriptor.
1601 case UDESC_OTHER_SPEED_CONFIGURATION:
1603 if ((value & 0xff) != 0) {
1607 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
1608 memcpy(buf, &ehci_confd, l);
1609 ((usb_config_descriptor_t *)buf)->bDescriptorType =
1611 buf = (char *)buf + l;
1613 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
1615 memcpy(buf, &ehci_ifcd, l);
1616 buf = (char *)buf + l;
1618 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
1620 memcpy(buf, &ehci_endpd, l);
1625 *(u_int8_t *)buf = 0;
1627 switch (value & 0xff) {
1628 case 1: /* Vendor */
1629 totlen = ehci_str(buf, len, sc->sc_vendor);
1631 case 2: /* Product */
1632 totlen = ehci_str(buf, len, "EHCI root hub");
1641 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
1643 *(u_int8_t *)buf = 0;
1647 case C(UR_GET_STATUS, UT_READ_DEVICE):
1649 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
1653 case C(UR_GET_STATUS, UT_READ_INTERFACE):
1654 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
1656 USETW(((usb_status_t *)buf)->wStatus, 0);
1660 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
1661 if (value >= USB_MAX_DEVICES) {
1665 sc->sc_addr = value;
1667 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
1668 if (value != 0 && value != 1) {
1672 sc->sc_conf = value;
1674 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
1676 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
1677 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
1678 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
1681 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
1683 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
1686 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
1688 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
1689 DPRINTFN(8, ("ehci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
1690 "port=%d feature=%d\n",
1692 if (index < 1 || index > sc->sc_noport) {
1696 port = EHCI_PORTSC(index);
1697 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1699 case UHF_PORT_ENABLE:
1700 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
1702 case UHF_PORT_SUSPEND:
1703 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
1705 case UHF_PORT_POWER:
1706 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
1709 DPRINTFN(2,("ehci_root_ctrl_transfer: clear port test "
1712 case UHF_PORT_INDICATOR:
1713 DPRINTFN(2,("ehci_root_ctrl_transfer: clear port ind "
1715 EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
1717 case UHF_C_PORT_CONNECTION:
1718 EOWRITE4(sc, port, v | EHCI_PS_CSC);
1720 case UHF_C_PORT_ENABLE:
1721 EOWRITE4(sc, port, v | EHCI_PS_PEC);
1723 case UHF_C_PORT_SUSPEND:
1726 case UHF_C_PORT_OVER_CURRENT:
1727 EOWRITE4(sc, port, v | EHCI_PS_OCC);
1729 case UHF_C_PORT_RESET:
1738 case UHF_C_PORT_CONNECTION:
1739 case UHF_C_PORT_ENABLE:
1740 case UHF_C_PORT_SUSPEND:
1741 case UHF_C_PORT_OVER_CURRENT:
1742 case UHF_C_PORT_RESET:
1743 /* Enable RHSC interrupt if condition is cleared. */
1744 if ((OREAD4(sc, port) >> 16) == 0)
1745 ehci_pcd_able(sc, 1);
1752 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
1753 if ((value & 0xff) != 0) {
1758 hubd.bNbrPorts = sc->sc_noport;
1759 v = EOREAD4(sc, EHCI_HCSPARAMS);
1760 USETW(hubd.wHubCharacteristics,
1761 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
1762 EHCI_HCS_P_INCICATOR(EREAD4(sc, EHCI_HCSPARAMS))
1763 ? UHD_PORT_IND : 0);
1764 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
1765 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
1766 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
1767 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
1768 l = min(len, hubd.bDescLength);
1770 memcpy(buf, &hubd, l);
1772 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
1777 memset(buf, 0, len); /* ? XXX */
1780 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
1781 DPRINTFN(8,("ehci_root_ctrl_transfer: get port status i=%d\n",
1783 if (index < 1 || index > sc->sc_noport) {
1791 v = EOREAD4(sc, EHCI_PORTSC(index));
1792 DPRINTFN(8,("ehci_root_ctrl_transfer: port status=0x%04x\n",
1795 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
1796 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
1797 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
1798 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
1799 if (v & EHCI_PS_PR) i |= UPS_RESET;
1800 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
1801 USETW(ps.wPortStatus, i);
1803 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
1804 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
1805 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
1806 if (sc->sc_isreset) i |= UPS_C_PORT_RESET;
1807 USETW(ps.wPortChange, i);
1808 l = min(len, sizeof ps);
1809 memcpy(buf, &ps, l);
1812 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
1815 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
1817 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
1818 if (index < 1 || index > sc->sc_noport) {
1822 port = EHCI_PORTSC(index);
1823 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1825 case UHF_PORT_ENABLE:
1826 EOWRITE4(sc, port, v | EHCI_PS_PE);
1828 case UHF_PORT_SUSPEND:
1829 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
1831 case UHF_PORT_RESET:
1832 DPRINTFN(5,("ehci_root_ctrl_transfer: reset port %d\n",
1834 if (EHCI_PS_IS_LOWSPEED(v)) {
1835 /* Low speed device, give up ownership. */
1836 ehci_disown(sc, index, 1);
1839 /* Start reset sequence. */
1840 v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
1841 EOWRITE4(sc, port, v | EHCI_PS_PR);
1842 /* Wait for reset to complete. */
1843 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
1848 /* Terminate reset sequence. */
1849 EOWRITE4(sc, port, v);
1850 /* Wait for HC to complete reset. */
1851 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE);
1856 v = EOREAD4(sc, port);
1857 DPRINTF(("ehci after reset, status=0x%08x\n", v));
1858 if (v & EHCI_PS_PR) {
1859 printf("%s: port reset timeout\n",
1860 USBDEVNAME(sc->sc_bus.bdev));
1861 return (USBD_TIMEOUT);
1863 if (!(v & EHCI_PS_PE)) {
1864 /* Not a high speed device, give up ownership.*/
1865 ehci_disown(sc, index, 0);
1869 DPRINTF(("ehci port %d reset, status = 0x%08x\n",
1872 case UHF_PORT_POWER:
1873 DPRINTFN(2,("ehci_root_ctrl_transfer: set port power "
1875 EOWRITE4(sc, port, v | EHCI_PS_PP);
1878 DPRINTFN(2,("ehci_root_ctrl_transfer: set port test "
1881 case UHF_PORT_INDICATOR:
1882 DPRINTFN(2,("ehci_root_ctrl_transfer: set port ind "
1884 EOWRITE4(sc, port, v | EHCI_PS_PIC);
1891 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
1892 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
1893 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
1894 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
1900 xfer->actlen = totlen;
1901 err = USBD_NORMAL_COMPLETION;
1905 usb_transfer_complete(xfer);
1907 return (USBD_IN_PROGRESS);
1911 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
1916 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
1918 if (sc->sc_npcomp != 0) {
1919 int i = (index-1) / sc->sc_npcomp;
1920 if (i >= sc->sc_ncomp)
1921 printf("%s: strange port\n",
1922 USBDEVNAME(sc->sc_bus.bdev));
1924 printf("%s: handing over %s speed device on "
1926 USBDEVNAME(sc->sc_bus.bdev),
1927 lowspeed ? "low" : "full",
1928 index, USBDEVNAME(sc->sc_comps[i]->bdev));
1930 printf("%s: npcomp == 0\n", USBDEVNAME(sc->sc_bus.bdev));
1933 port = EHCI_PORTSC(index);
1934 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1935 EOWRITE4(sc, port, v | EHCI_PS_PO);
1938 /* Abort a root control request. */
1940 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
1942 /* Nothing to do, all transfers are synchronous. */
1945 /* Close the root pipe. */
1947 ehci_root_ctrl_close(usbd_pipe_handle pipe)
1949 DPRINTF(("ehci_root_ctrl_close\n"));
1950 /* Nothing to do. */
1954 ehci_root_intr_done(usbd_xfer_handle xfer)
1956 xfer->hcpriv = NULL;
1960 ehci_root_intr_transfer(usbd_xfer_handle xfer)
1964 /* Insert last in queue. */
1965 err = usb_insert_transfer(xfer);
1969 /* Pipe isn't running, start first */
1970 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1974 ehci_root_intr_start(usbd_xfer_handle xfer)
1976 usbd_pipe_handle pipe = xfer->pipe;
1977 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
1980 return (USBD_IOERROR);
1982 sc->sc_intrxfer = xfer;
1984 return (USBD_IN_PROGRESS);
1987 /* Abort a root interrupt request. */
1989 ehci_root_intr_abort(usbd_xfer_handle xfer)
1991 if (xfer->pipe->intrxfer == xfer) {
1992 DPRINTF(("ehci_root_intr_abort: remove\n"));
1993 xfer->pipe->intrxfer = NULL;
1995 xfer->status = USBD_CANCELLED;
1997 usb_transfer_complete(xfer);
2001 /* Close the root pipe. */
2003 ehci_root_intr_close(usbd_pipe_handle pipe)
2005 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2007 DPRINTF(("ehci_root_intr_close\n"));
2009 sc->sc_intrxfer = NULL;
2013 ehci_root_ctrl_done(usbd_xfer_handle xfer)
2015 xfer->hcpriv = NULL;
2018 /************************/
2021 ehci_alloc_sqh(ehci_softc_t *sc)
2023 ehci_soft_qh_t *sqh;
2028 if (sc->sc_freeqhs == NULL) {
2029 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
2030 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
2031 EHCI_PAGE_SIZE, &dma);
2034 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2038 for(i = 0; i < EHCI_SQH_CHUNK; i++) {
2039 offs = i * EHCI_SQH_SIZE;
2040 sqh = KERNADDR(&dma, offs);
2041 sqh->physaddr = DMAADDR(&dma, offs);
2042 sqh->next = sc->sc_freeqhs;
2043 sc->sc_freeqhs = sqh;
2046 sqh = sc->sc_freeqhs;
2047 sc->sc_freeqhs = sqh->next;
2048 memset(&sqh->qh, 0, sizeof(ehci_qh_t));
2054 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2056 sqh->next = sc->sc_freeqhs;
2057 sc->sc_freeqhs = sqh;
2061 ehci_alloc_sqtd(ehci_softc_t *sc)
2063 ehci_soft_qtd_t *sqtd;
2068 if (sc->sc_freeqtds == NULL) {
2069 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
2070 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
2071 EHCI_PAGE_SIZE, &dma);
2074 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
2079 for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
2080 offs = i * EHCI_SQTD_SIZE;
2081 sqtd = KERNADDR(&dma, offs);
2082 sqtd->physaddr = DMAADDR(&dma, offs);
2083 sqtd->nextqtd = sc->sc_freeqtds;
2084 sc->sc_freeqtds = sqtd;
2090 sqtd = sc->sc_freeqtds;
2091 sc->sc_freeqtds = sqtd->nextqtd;
2092 memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
2093 sqtd->nextqtd = NULL;
2101 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2104 sqtd->nextqtd = sc->sc_freeqtds;
2105 sc->sc_freeqtds = sqtd;
2110 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
2111 int alen, int rd, usbd_xfer_handle xfer,
2112 ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
2114 ehci_soft_qtd_t *next, *cur;
2115 ehci_physaddr_t dataphys, dataphyspage, dataphyslastpage, nextphys;
2116 u_int32_t qtdstatus;
2117 int len, curlen, mps;
2120 usb_dma_t *dma = &xfer->dmabuf;
2122 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
2126 dataphys = DMAADDR(dma, 0);
2127 dataphyslastpage = EHCI_PAGE(DMAADDR(dma, len - 1));
2129 printf("status=%08x toggle=%d\n", epipe->sqh->qh.qh_qtd.qtd_status,
2132 qtdstatus = EHCI_QTD_ACTIVE |
2133 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2134 EHCI_QTD_SET_CERR(3)
2136 /* BYTES set below */
2138 mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
2139 tog = epipe->nexttoggle;
2140 qtdstatus |= EHCI_QTD_SET_TOGGLE(tog);
2142 cur = ehci_alloc_sqtd(sc);
2147 dataphyspage = EHCI_PAGE(dataphys);
2148 /* The EHCI hardware can handle at most 5 pages. */
2149 #if defined(__NetBSD__) || defined(__OpenBSD__)
2150 if (dataphyslastpage - dataphyspage <
2151 EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE) {
2152 /* we can handle it in this QTD */
2154 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2155 /* XXX This is pretty broken: Because we do not allocate
2156 * a contiguous buffer (contiguous in physical pages) we
2157 * can only transfer one page in one go.
2158 * So check whether the start and end of the buffer are on
2161 if (dataphyspage == dataphyslastpage) {
2165 #if defined(__NetBSD__) || defined(__OpenBSD__)
2166 /* must use multiple TDs, fill as much as possible. */
2167 curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE -
2168 EHCI_PAGE_OFFSET(dataphys);
2171 printf("ehci_alloc_sqtd_chain: curlen=0x%x "
2172 "len=0x%x offs=0x%x\n", curlen, len,
2173 EHCI_PAGE_OFFSET(dataphys));
2174 printf("lastpage=0x%x page=0x%x phys=0x%x\n",
2175 dataphyslastpage, dataphyspage,
2180 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2181 /* See comment above (XXX) */
2182 curlen = EHCI_PAGE_SIZE -
2183 EHCI_PAGE_MASK(dataphys);
2185 /* the length must be a multiple of the max size */
2186 curlen -= curlen % mps;
2187 DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, "
2188 "curlen=%d\n", curlen));
2191 panic("ehci_alloc_std: curlen == 0");
2194 DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x "
2195 "dataphyslastpage=0x%08x len=%d curlen=%d\n",
2196 dataphys, dataphyslastpage,
2201 next = ehci_alloc_sqtd(sc);
2204 nextphys = htole32(next->physaddr);
2207 nextphys = EHCI_NULL;
2210 for (i = 0; i * EHCI_PAGE_SIZE < curlen; i++) {
2211 ehci_physaddr_t a = dataphys + i * EHCI_PAGE_SIZE;
2212 if (i != 0) /* use offset only in first buffer */
2214 cur->qtd.qtd_buffer[i] = htole32(a);
2216 if (i >= EHCI_QTD_NBUFFERS) {
2217 printf("ehci_alloc_sqtd_chain: i=%d\n", i);
2222 cur->nextqtd = next;
2223 cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys;
2224 cur->qtd.qtd_status =
2225 htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
2228 DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n",
2229 dataphys, dataphys + curlen));
2230 /* adjust the toggle based on the number of packets in this
2232 if (((curlen + mps - 1) / mps) & 1) {
2234 qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
2238 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
2240 dataphys = DMAADDR(dma, offset);
2243 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
2245 epipe->nexttoggle = tog;
2247 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
2250 return (USBD_NORMAL_COMPLETION);
2253 /* XXX free chain */
2254 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
2255 return (USBD_NOMEM);
2259 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd,
2260 ehci_soft_qtd_t *sqtdend)
2265 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
2268 for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
2270 ehci_free_sqtd(sc, sqtd);
2277 * Close a reqular pipe.
2278 * Assumes that there are no pending transactions.
2281 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
2283 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
2284 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2285 ehci_soft_qh_t *sqh = epipe->sqh;
2288 ehci_rem_qh(sc, sqh, head);
2290 ehci_free_sqh(sc, epipe->sqh);
2294 * Abort a device request.
2295 * If this routine is called from a critical section it guarantees that the
2296 * request will be removed from the hardware scheduling and that the callback
2297 * for it will be called with USBD_CANCELLED status.
2298 * It's impossible to guarantee that the requested transfer will not
2299 * have happened since the hardware runs concurrently.
2300 * If the transaction has already happened we rely on the ordinary
2301 * interrupt processing to process it.
2302 * XXX This is most probably wrong.
2305 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2307 #define exfer EXFER(xfer)
2308 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2309 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2310 ehci_soft_qh_t *sqh = epipe->sqh;
2311 ehci_soft_qtd_t *sqtd;
2312 ehci_physaddr_t cur;
2316 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
2319 /* If we're dying, just do the software part. */
2321 xfer->status = status; /* make software ignore it */
2322 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2323 usb_transfer_complete(xfer);
2328 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
2329 panic("ehci_abort_xfer: not in process context");
2332 * Step 1: Make interrupt routine and hardware ignore xfer.
2335 xfer->status = status; /* make software ignore it */
2336 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2337 qhstatus = sqh->qh.qh_qtd.qtd_status;
2338 sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
2339 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2340 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
2341 if (sqtd == exfer->sqtdend)
2347 * Step 2: Wait until we know hardware has finished any possible
2348 * use of the xfer. Also make sure the soft interrupt routine
2353 #ifdef USB_USE_SOFTINTR
2354 sc->sc_softwake = 1;
2355 #endif /* USB_USE_SOFTINTR */
2356 usb_schedsoftintr(&sc->sc_bus);
2357 #ifdef USB_USE_SOFTINTR
2358 tsleep(&sc->sc_softwake, 0, "ehciab", 0);
2359 #endif /* USB_USE_SOFTINTR */
2363 * Step 3: Remove any vestiges of the xfer from the hardware.
2364 * The complication here is that the hardware may have executed
2365 * beyond the xfer we're trying to abort. So as we're scanning
2366 * the TDs of this xfer we check if the hardware points to
2369 crit_enter(); /* XXX why? */
2370 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
2372 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2373 hit |= cur == sqtd->physaddr;
2374 if (sqtd == exfer->sqtdend)
2377 sqtd = sqtd->nextqtd;
2378 /* Zap curqtd register if hardware pointed inside the xfer. */
2379 if (hit && sqtd != NULL) {
2380 DPRINTFN(1,("ehci_abort_xfer: cur=0x%08x\n", sqtd->physaddr));
2381 sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
2382 sqh->qh.qh_qtd.qtd_status = qhstatus;
2384 DPRINTFN(1,("ehci_abort_xfer: no hit\n"));
2388 * Step 4: Execute callback.
2393 usb_transfer_complete(xfer);
2400 ehci_timeout(void *addr)
2402 struct ehci_xfer *exfer = addr;
2403 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
2404 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2406 DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
2409 usbd_dump_pipe(exfer->xfer.pipe);
2413 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
2417 /* Execute the abort in a process context. */
2418 usb_init_task(&exfer->abort_task, ehci_timeout_task, addr);
2419 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task);
2423 ehci_timeout_task(void *addr)
2425 usbd_xfer_handle xfer = addr;
2427 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
2429 ehci_abort_xfer(xfer, USBD_TIMEOUT);
2433 /************************/
2436 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
2440 /* Insert last in queue. */
2441 err = usb_insert_transfer(xfer);
2445 /* Pipe isn't running, start first */
2446 return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2450 ehci_device_ctrl_start(usbd_xfer_handle xfer)
2452 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2456 return (USBD_IOERROR);
2459 if (!(xfer->rqflags & URQ_REQUEST)) {
2461 printf("ehci_device_ctrl_transfer: not a request\n");
2462 return (USBD_INVAL);
2466 err = ehci_device_request(xfer);
2470 if (sc->sc_bus.use_polling)
2471 ehci_waitintr(sc, xfer);
2472 return (USBD_IN_PROGRESS);
2476 ehci_device_ctrl_done(usbd_xfer_handle xfer)
2478 struct ehci_xfer *ex = EXFER(xfer);
2479 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2480 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
2482 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
2485 if (!(xfer->rqflags & URQ_REQUEST)) {
2486 panic("ehci_ctrl_done: not a request");
2490 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2491 ehci_del_intr_list(ex); /* remove from active list */
2492 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
2495 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
2498 /* Abort a device control request. */
2500 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
2502 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
2503 ehci_abort_xfer(xfer, USBD_CANCELLED);
2506 /* Close a device control pipe. */
2508 ehci_device_ctrl_close(usbd_pipe_handle pipe)
2510 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2511 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
2513 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
2514 ehci_close_pipe(pipe, sc->sc_async_head);
2518 ehci_device_request(usbd_xfer_handle xfer)
2520 #define exfer EXFER(xfer)
2521 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2522 usb_device_request_t *req = &xfer->request;
2523 usbd_device_handle dev = epipe->pipe.device;
2524 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2525 int addr = dev->address;
2526 ehci_soft_qtd_t *setup, *stat, *next;
2527 ehci_soft_qh_t *sqh;
2532 isread = req->bmRequestType & UT_READ;
2533 len = UGETW(req->wLength);
2535 DPRINTFN(3,("ehci_device_control type=0x%02x, request=0x%02x, "
2536 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2537 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2538 UGETW(req->wIndex), len, addr,
2539 epipe->pipe.endpoint->edesc->bEndpointAddress));
2541 setup = ehci_alloc_sqtd(sc);
2542 if (setup == NULL) {
2546 stat = ehci_alloc_sqtd(sc);
2553 epipe->u.ctl.length = len;
2555 /* Update device address and length since they may have changed
2556 during the setup of the control pipe in usbd_new_device(). */
2557 /* XXX This only needs to be done once, but it's too early in open. */
2558 /* XXXX Should not touch ED here! */
2560 (sqh->qh.qh_endp & htole32(~(EHCI_QH_ADDRMASK | EHCI_QH_MPLMASK))) |
2562 EHCI_QH_SET_ADDR(addr) |
2563 EHCI_QH_SET_MPL(UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize))
2566 /* Set up data transaction */
2568 ehci_soft_qtd_t *end;
2570 /* Start toggle at 1. */
2571 epipe->nexttoggle = 1;
2572 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
2576 end->nextqtd = stat;
2578 end->qtd.qtd_altnext = htole32(stat->physaddr);
2583 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
2586 setup->qtd.qtd_status = htole32(
2588 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
2589 EHCI_QTD_SET_CERR(3) |
2590 EHCI_QTD_SET_TOGGLE(0) |
2591 EHCI_QTD_SET_BYTES(sizeof *req)
2593 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
2594 setup->nextqtd = next;
2595 setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
2597 setup->len = sizeof *req;
2599 stat->qtd.qtd_status = htole32(
2601 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
2602 EHCI_QTD_SET_CERR(3) |
2603 EHCI_QTD_SET_TOGGLE(1) |
2606 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
2607 stat->nextqtd = NULL;
2608 stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL;
2613 if (ehcidebug > 5) {
2614 DPRINTF(("ehci_device_request:\n"));
2616 ehci_dump_sqtds(setup);
2620 exfer->sqtdstart = setup;
2621 exfer->sqtdend = stat;
2623 if (!exfer->isdone) {
2624 printf("ehci_device_request: not done, exfer=%p\n", exfer);
2629 /* Insert qTD in QH list. */
2631 ehci_set_qh_qtd(sqh, setup);
2632 if (xfer->timeout && !sc->sc_bus.use_polling) {
2633 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2634 ehci_timeout, xfer);
2636 ehci_add_intr_list(sc, exfer);
2637 xfer->status = USBD_IN_PROGRESS;
2641 if (ehcidebug > 10) {
2642 DPRINTF(("ehci_device_request: status=%x\n",
2643 EOREAD4(sc, EHCI_USBSTS)));
2646 ehci_dump_sqh(sc->sc_async_head);
2648 ehci_dump_sqtds(setup);
2652 return (USBD_NORMAL_COMPLETION);
2655 ehci_free_sqtd(sc, stat);
2657 ehci_free_sqtd(sc, setup);
2659 DPRINTFN(-1,("ehci_device_request: no memory\n"));
2661 usb_transfer_complete(xfer);
2666 /************************/
2669 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
2673 /* Insert last in queue. */
2674 err = usb_insert_transfer(xfer);
2678 /* Pipe isn't running, start first */
2679 return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2683 ehci_device_bulk_start(usbd_xfer_handle xfer)
2685 #define exfer EXFER(xfer)
2686 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2687 usbd_device_handle dev = epipe->pipe.device;
2688 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2689 ehci_soft_qtd_t *data, *dataend;
2690 ehci_soft_qh_t *sqh;
2692 int len, isread, endpt;
2694 DPRINTFN(2, ("ehci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
2695 xfer, xfer->length, xfer->flags));
2698 return (USBD_IOERROR);
2701 if (xfer->rqflags & URQ_REQUEST)
2702 panic("ehci_device_bulk_transfer: a request");
2706 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
2707 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2710 epipe->u.bulk.length = len;
2712 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
2715 DPRINTFN(-1,("ehci_device_bulk_transfer: no memory\n"));
2717 usb_transfer_complete(xfer);
2722 if (ehcidebug > 5) {
2723 DPRINTF(("ehci_device_bulk_transfer: data(1)\n"));
2725 ehci_dump_sqtds(data);
2729 /* Set up interrupt info. */
2730 exfer->sqtdstart = data;
2731 exfer->sqtdend = dataend;
2733 if (!exfer->isdone) {
2734 printf("ehci_device_bulk_transfer: not done, ex=%p\n", exfer);
2740 ehci_set_qh_qtd(sqh, data);
2741 if (xfer->timeout && !sc->sc_bus.use_polling) {
2742 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2743 ehci_timeout, xfer);
2745 ehci_add_intr_list(sc, exfer);
2746 xfer->status = USBD_IN_PROGRESS;
2750 if (ehcidebug > 10) {
2751 DPRINTF(("ehci_device_bulk_transfer: data(2)\n"));
2753 DPRINTF(("ehci_device_bulk_transfer: data(3)\n"));
2756 printf("async_head:\n");
2757 ehci_dump_sqh(sc->sc_async_head);
2761 ehci_dump_sqtds(data);
2765 if (sc->sc_bus.use_polling)
2766 ehci_waitintr(sc, xfer);
2768 return (USBD_IN_PROGRESS);
2773 ehci_device_bulk_abort(usbd_xfer_handle xfer)
2775 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
2776 ehci_abort_xfer(xfer, USBD_CANCELLED);
2780 * Close a device bulk pipe.
2783 ehci_device_bulk_close(usbd_pipe_handle pipe)
2785 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2787 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
2788 ehci_close_pipe(pipe, sc->sc_async_head);
2792 ehci_device_bulk_done(usbd_xfer_handle xfer)
2794 struct ehci_xfer *ex = EXFER(xfer);
2795 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2796 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
2798 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
2799 xfer, xfer->actlen));
2801 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2802 ehci_del_intr_list(ex); /* remove from active list */
2803 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
2806 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
2809 /************************/
2811 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
2812 Static usbd_status ehci_device_intr_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
2813 Static void ehci_device_intr_abort(usbd_xfer_handle xfer) { }
2814 Static void ehci_device_intr_close(usbd_pipe_handle pipe) { }
2815 Static void ehci_device_intr_done(usbd_xfer_handle xfer) { }
2817 /************************/
2819 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
2820 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
2821 Static void ehci_device_isoc_abort(usbd_xfer_handle xfer) { }
2822 Static void ehci_device_isoc_close(usbd_pipe_handle pipe) { }
2823 Static void ehci_device_isoc_done(usbd_xfer_handle xfer) { }