2 * $NetBSD: ehci.c,v 1.79 2004/10/23 13:36:43 augustss 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.12 2005/08/27 19:00:49 asmodai 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 complicated 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) do { if (ehcidebug) logprintf x; } while(0)
114 #define DPRINTFN(n,x) do { if (ehcidebug>(n)) logprintf x; } while(0)
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;*/
156 #if defined(__NetBSD__) || defined(__OpenBSD__)
157 Static void ehci_shutdown(void *);
158 Static void ehci_power(int, void *);
161 Static usbd_status ehci_open(usbd_pipe_handle);
162 Static void ehci_poll(struct usbd_bus *);
163 Static void ehci_softintr(void *);
164 Static int ehci_intr1(ehci_softc_t *);
165 Static void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
166 Static void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
167 Static void ehci_idone(struct ehci_xfer *);
168 Static void ehci_timeout(void *);
169 Static void ehci_timeout_task(void *);
171 Static usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
172 Static void ehci_freem(struct usbd_bus *, usb_dma_t *);
174 Static usbd_xfer_handle ehci_allocx(struct usbd_bus *);
175 Static void ehci_freex(struct usbd_bus *, usbd_xfer_handle);
177 Static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle);
178 Static usbd_status ehci_root_ctrl_start(usbd_xfer_handle);
179 Static void ehci_root_ctrl_abort(usbd_xfer_handle);
180 Static void ehci_root_ctrl_close(usbd_pipe_handle);
181 Static void ehci_root_ctrl_done(usbd_xfer_handle);
183 Static usbd_status ehci_root_intr_transfer(usbd_xfer_handle);
184 Static usbd_status ehci_root_intr_start(usbd_xfer_handle);
185 Static void ehci_root_intr_abort(usbd_xfer_handle);
186 Static void ehci_root_intr_close(usbd_pipe_handle);
187 Static void ehci_root_intr_done(usbd_xfer_handle);
189 Static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle);
190 Static usbd_status ehci_device_ctrl_start(usbd_xfer_handle);
191 Static void ehci_device_ctrl_abort(usbd_xfer_handle);
192 Static void ehci_device_ctrl_close(usbd_pipe_handle);
193 Static void ehci_device_ctrl_done(usbd_xfer_handle);
195 Static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle);
196 Static usbd_status ehci_device_bulk_start(usbd_xfer_handle);
197 Static void ehci_device_bulk_abort(usbd_xfer_handle);
198 Static void ehci_device_bulk_close(usbd_pipe_handle);
199 Static void ehci_device_bulk_done(usbd_xfer_handle);
201 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle);
202 Static usbd_status ehci_device_intr_start(usbd_xfer_handle);
203 Static void ehci_device_intr_abort(usbd_xfer_handle);
204 Static void ehci_device_intr_close(usbd_pipe_handle);
205 Static void ehci_device_intr_done(usbd_xfer_handle);
207 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle);
208 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle);
209 Static void ehci_device_isoc_abort(usbd_xfer_handle);
210 Static void ehci_device_isoc_close(usbd_pipe_handle);
211 Static void ehci_device_isoc_done(usbd_xfer_handle);
213 Static void ehci_device_clear_toggle(usbd_pipe_handle pipe);
214 Static void ehci_noop(usbd_pipe_handle pipe);
216 Static int ehci_str(usb_string_descriptor_t *, int, char *);
217 Static void ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
218 Static void ehci_pcd_able(ehci_softc_t *, int);
219 Static void ehci_pcd_enable(void *);
220 Static void ehci_disown(ehci_softc_t *, int, int);
222 Static ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *);
223 Static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
225 Static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
226 Static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
227 Static usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *,
228 ehci_softc_t *, int, int, usbd_xfer_handle,
229 ehci_soft_qtd_t **, ehci_soft_qtd_t **);
230 Static void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *,
233 Static usbd_status ehci_device_request(usbd_xfer_handle xfer);
235 Static usbd_status ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
238 Static void ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *);
239 Static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
241 Static void ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
242 Static void ehci_sync_hc(ehci_softc_t *);
244 Static void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
245 Static void ehci_abort_xfer(usbd_xfer_handle, usbd_status);
248 Static void ehci_dump_regs(ehci_softc_t *);
249 void ehci_dump(void);
250 Static ehci_softc_t *theehci;
251 Static void ehci_dump_link(ehci_link_t, int);
252 Static void ehci_dump_sqtds(ehci_soft_qtd_t *);
253 Static void ehci_dump_sqtd(ehci_soft_qtd_t *);
254 Static void ehci_dump_qtd(ehci_qtd_t *);
255 Static void ehci_dump_sqh(ehci_soft_qh_t *);
257 Static void ehci_dump_exfer(struct ehci_xfer *);
261 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
263 #define EHCI_INTR_ENDPT 1
265 #define ehci_add_intr_list(sc, ex) \
266 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext);
267 #define ehci_del_intr_list(ex) \
269 LIST_REMOVE((ex), inext); \
270 (ex)->inext.le_prev = NULL; \
272 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL)
274 Static struct usbd_bus_methods ehci_bus_methods = {
284 Static struct usbd_pipe_methods ehci_root_ctrl_methods = {
285 ehci_root_ctrl_transfer,
286 ehci_root_ctrl_start,
287 ehci_root_ctrl_abort,
288 ehci_root_ctrl_close,
293 Static struct usbd_pipe_methods ehci_root_intr_methods = {
294 ehci_root_intr_transfer,
295 ehci_root_intr_start,
296 ehci_root_intr_abort,
297 ehci_root_intr_close,
302 Static struct usbd_pipe_methods ehci_device_ctrl_methods = {
303 ehci_device_ctrl_transfer,
304 ehci_device_ctrl_start,
305 ehci_device_ctrl_abort,
306 ehci_device_ctrl_close,
308 ehci_device_ctrl_done,
311 Static struct usbd_pipe_methods ehci_device_intr_methods = {
312 ehci_device_intr_transfer,
313 ehci_device_intr_start,
314 ehci_device_intr_abort,
315 ehci_device_intr_close,
316 ehci_device_clear_toggle,
317 ehci_device_intr_done,
320 Static struct usbd_pipe_methods ehci_device_bulk_methods = {
321 ehci_device_bulk_transfer,
322 ehci_device_bulk_start,
323 ehci_device_bulk_abort,
324 ehci_device_bulk_close,
325 ehci_device_clear_toggle,
326 ehci_device_bulk_done,
329 Static struct usbd_pipe_methods ehci_device_isoc_methods = {
330 ehci_device_isoc_transfer,
331 ehci_device_isoc_start,
332 ehci_device_isoc_abort,
333 ehci_device_isoc_close,
335 ehci_device_isoc_done,
339 ehci_init(ehci_softc_t *sc)
341 u_int32_t version, sparams, cparams, hcr;
346 DPRINTF(("ehci_init: start\n"));
351 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
353 version = EREAD2(sc, EHCI_HCIVERSION);
354 printf("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev),
355 version >> 8, version & 0xff);
357 sparams = EREAD4(sc, EHCI_HCSPARAMS);
358 DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
359 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
360 if (EHCI_HCS_N_CC(sparams) != sc->sc_ncomp) {
361 printf("%s: wrong number of companions (%d != %d)\n",
362 USBDEVNAME(sc->sc_bus.bdev),
363 EHCI_HCS_N_CC(sparams), sc->sc_ncomp);
364 return (USBD_IOERROR);
366 if (sc->sc_ncomp > 0) {
367 printf("%s: companion controller%s, %d port%s each:",
368 USBDEVNAME(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
369 EHCI_HCS_N_PCC(sparams),
370 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
371 for (i = 0; i < sc->sc_ncomp; i++)
372 printf(" %s", USBDEVNAME(sc->sc_comps[i]->bdev));
375 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
376 cparams = EREAD4(sc, EHCI_HCCPARAMS);
377 DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
379 if (EHCI_HCC_64BIT(cparams)) {
380 /* MUST clear segment register if 64 bit capable. */
381 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
384 sc->sc_bus.usbrev = USBREV_2_0;
386 /* Reset the controller */
387 EOWRITE4(sc, EHCI_USBINTR, 0); /* disable interrupts */
388 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
389 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
390 usb_delay_ms(&sc->sc_bus, 1);
391 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
392 for (i = 0; i < 100; i++) {
393 usb_delay_ms(&sc->sc_bus, 1);
394 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
399 printf("%s: reset timeout\n",
400 USBDEVNAME(sc->sc_bus.bdev));
401 return (USBD_IOERROR);
404 /* XXX need proper intr scheduling */
407 /* frame list size at default, read back what we got and use that */
408 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
409 case 0: sc->sc_flsize = 1024; break;
410 case 1: sc->sc_flsize = 512; break;
411 case 2: sc->sc_flsize = 256; break;
412 case 3: return (USBD_IOERROR);
414 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t),
415 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
418 DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize));
419 sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
420 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
422 /* Set up the bus struct. */
423 sc->sc_bus.methods = &ehci_bus_methods;
424 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
426 #if defined(__NetBSD__) || defined(__OpenBSD__)
427 sc->sc_powerhook = powerhook_establish(ehci_power, sc);
428 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
431 sc->sc_eintrs = EHCI_NORMAL_INTRS;
434 * Allocate the interrupt dummy QHs. These are arranged to give poll
435 * intervals that are powers of 2 times 1ms.
437 for (i = 0; i < EHCI_INTRQHS; i++) {
438 sqh = ehci_alloc_sqh(sc);
443 sc->sc_islots[i].sqh = sqh;
445 for (i = 0; i < EHCI_INTRQHS; i++) {
446 sqh = sc->sc_islots[i].sqh;
448 /* The last (1ms) QH terminates. */
449 sqh->qh.qh_link = EHCI_NULL;
452 /* Otherwise the next QH has half the poll interval */
453 sqh->next = sc->sc_islots[(i + 1) / 2 - 1].sqh;
454 sqh->qh.qh_link = htole32(sqh->next->physaddr |
457 sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
458 sqh->qh.qh_link = EHCI_NULL;
459 sqh->qh.qh_curqtd = EHCI_NULL;
461 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
462 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
463 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
466 /* Point the frame list at the last level (128ms). */
467 for (i = 0; i < sc->sc_flsize; i++) {
468 sc->sc_flist[i] = htole32(EHCI_LINK_QH |
469 sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1,
473 /* Allocate dummy QH that starts the async list. */
474 sqh = ehci_alloc_sqh(sc);
481 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
483 htole32(sqh->physaddr | EHCI_LINK_QH);
484 sqh->qh.qh_curqtd = EHCI_NULL;
486 /* Fill the overlay qTD */
487 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
488 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
489 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
497 /* Point to async list */
498 sc->sc_async_head = sqh;
499 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
501 usb_callout_init(sc->sc_tmo_pcd);
503 lockinit(&sc->sc_doorbell_lock, 0, "ehcidb", 0, 0);
505 /* Turn on controller */
506 EOWRITE4(sc, EHCI_USBCMD,
507 EHCI_CMD_ITC_8 | /* 8 microframes */
508 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
513 /* Take over port ownership */
514 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
516 for (i = 0; i < 100; i++) {
517 usb_delay_ms(&sc->sc_bus, 1);
518 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
523 printf("%s: run timeout\n", USBDEVNAME(sc->sc_bus.bdev));
524 return (USBD_IOERROR);
527 return (USBD_NORMAL_COMPLETION);
531 ehci_free_sqh(sc, sc->sc_async_head);
534 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
539 ehci_init_intrs(ehci_softc_t *sc)
541 /* Enable interrupts */
542 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
548 ehci_softc_t *sc = v;
550 if (sc == NULL || sc->sc_dying)
553 /* If we get an interrupt while polling, then just ignore it. */
554 if (sc->sc_bus.use_polling) {
555 u_int32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
558 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
560 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
565 return (ehci_intr1(sc));
569 ehci_intr1(ehci_softc_t *sc)
571 u_int32_t intrs, eintrs;
573 DPRINTFN(20,("ehci_intr1: enter\n"));
575 /* In case the interrupt occurs before initialization has completed. */
578 printf("ehci_intr1: sc == NULL\n");
583 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
587 eintrs = intrs & sc->sc_eintrs;
588 DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
589 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
594 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
595 sc->sc_bus.intr_context++;
596 sc->sc_bus.no_intrs++;
597 if (eintrs & EHCI_STS_IAA) {
598 DPRINTF(("ehci_intr1: door bell\n"));
599 wakeup(&sc->sc_async_head);
600 eintrs &= ~EHCI_STS_IAA;
602 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
603 DPRINTFN(5,("ehci_intr1: %s %s\n",
604 eintrs & EHCI_STS_INT ? "INT" : "",
605 eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
606 usb_schedsoftintr(&sc->sc_bus);
607 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
609 if (eintrs & EHCI_STS_HSE) {
610 printf("%s: unrecoverable error, controller halted\n",
611 USBDEVNAME(sc->sc_bus.bdev));
614 if (eintrs & EHCI_STS_PCD) {
615 ehci_pcd(sc, sc->sc_intrxfer);
617 * Disable PCD interrupt for now, because it will be
618 * on until the port has been reset.
620 ehci_pcd_able(sc, 0);
621 /* Do not allow RHSC interrupts > 1 per second */
622 usb_callout(sc->sc_tmo_pcd, hz, ehci_pcd_enable, sc);
623 eintrs &= ~EHCI_STS_PCD;
626 sc->sc_bus.intr_context--;
629 /* Block unprocessed interrupts. */
630 sc->sc_eintrs &= ~eintrs;
631 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
632 printf("%s: blocking intrs 0x%x\n",
633 USBDEVNAME(sc->sc_bus.bdev), eintrs);
640 ehci_pcd_able(ehci_softc_t *sc, int on)
642 DPRINTFN(4, ("ehci_pcd_able: on=%d\n", on));
644 sc->sc_eintrs |= EHCI_STS_PCD;
646 sc->sc_eintrs &= ~EHCI_STS_PCD;
647 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
651 ehci_pcd_enable(void *v_sc)
653 ehci_softc_t *sc = v_sc;
655 ehci_pcd_able(sc, 1);
659 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
661 usbd_pipe_handle pipe;
666 /* Just ignore the change. */
672 p = KERNADDR(&xfer->dmabuf, 0);
673 m = min(sc->sc_noport, xfer->length * 8 - 1);
674 memset(p, 0, xfer->length);
675 for (i = 1; i <= m; i++) {
676 /* Pick out CHANGE bits from the status reg. */
677 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
678 p[i/8] |= 1 << (i%8);
680 DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
681 xfer->actlen = xfer->length;
682 xfer->status = USBD_NORMAL_COMPLETION;
684 usb_transfer_complete(xfer);
688 ehci_softintr(void *v)
690 ehci_softc_t *sc = v;
691 struct ehci_xfer *ex, *nextex;
693 DPRINTFN(10,("%s: ehci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
694 sc->sc_bus.intr_context));
696 sc->sc_bus.intr_context++;
699 * The only explanation I can think of for why EHCI is as brain dead
700 * as UHCI interrupt-wise is that Intel was involved in both.
701 * An interrupt just tells us that something is done, we have no
702 * clue what, so we need to scan through all active transfers. :-(
704 for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
705 nextex = LIST_NEXT(ex, inext);
706 ehci_check_intr(sc, ex);
709 #ifdef USB_USE_SOFTINTR
710 if (sc->sc_softwake) {
712 wakeup(&sc->sc_softwake);
714 #endif /* USB_USE_SOFTINTR */
716 sc->sc_bus.intr_context--;
719 /* Check for an interrupt. */
721 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
723 ehci_soft_qtd_t *sqtd, *lsqtd;
726 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
728 if (ex->sqtdstart == NULL) {
729 printf("ehci_check_intr: sqtdstart=NULL\n");
735 printf("ehci_check_intr: sqtd==0\n");
740 * If the last TD is still active we need to check whether there
741 * is a an error somewhere in the middle, or whether there was a
742 * short packet (SPD and not ACTIVE).
744 if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
745 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
746 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
747 status = le32toh(sqtd->qtd.qtd_status);
748 /* If there's an active QTD the xfer isn't done. */
749 if (status & EHCI_QTD_ACTIVE)
751 /* Any kind of error makes the xfer done. */
752 if (status & EHCI_QTD_HALTED)
754 /* We want short packets, and it is short: it's done */
755 if (EHCI_QTD_GET_BYTES(status) != 0)
758 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
763 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
764 usb_uncallout(ex->xfer.timeout_handle, ehci_timeout, ex);
769 ehci_idone(struct ehci_xfer *ex)
771 usbd_xfer_handle xfer = &ex->xfer;
772 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
773 ehci_soft_qtd_t *sqtd;
774 u_int32_t status = 0, nstatus;
777 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
784 printf("ehci_idone: ex is done!\n ");
787 printf("ehci_idone: ex=%p is done!\n", ex);
796 if (xfer->status == USBD_CANCELLED ||
797 xfer->status == USBD_TIMEOUT) {
798 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
803 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
805 ehci_dump_sqtds(ex->sqtdstart);
808 /* The transfer is done, compute actual length and status. */
810 for (sqtd = ex->sqtdstart; sqtd != NULL; sqtd = sqtd->nextqtd) {
811 nstatus = le32toh(sqtd->qtd.qtd_status);
812 if (nstatus & EHCI_QTD_ACTIVE)
816 /* halt is ok if descriptor is last, and complete */
817 if(sqtd->qtd.qtd_next == EHCI_NULL &&
818 EHCI_QTD_GET_BYTES(status) == 0)
819 status &= ~EHCI_QTD_HALTED;
820 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
821 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
824 /* If there are left over TDs we need to update the toggle. */
826 DPRINTF(("ehci_idone: need toggle update status=%08x nstatus=%08x\n", status, nstatus));
828 ehci_dump_sqh(epipe->sqh);
829 ehci_dump_sqtds(ex->sqtdstart);
831 epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
834 status &= EHCI_QTD_STATERRS;
835 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, status=0x%x\n",
836 xfer->length, actlen, status));
837 xfer->actlen = actlen;
842 bitmask_snprintf((u_int32_t)status,
843 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR"
844 "\3MISSED", sbuf, sizeof(sbuf));
846 DPRINTFN((status & EHCI_QTD_HALTED) ? 2 : 0,
847 ("ehci_idone: error, addr=%d, endpt=0x%02x, "
849 xfer->pipe->device->address,
850 xfer->pipe->endpoint->edesc->bEndpointAddress,
853 ehci_dump_sqh(epipe->sqh);
854 ehci_dump_sqtds(ex->sqtdstart);
857 if (status == EHCI_QTD_HALTED)
858 xfer->status = USBD_STALLED;
860 xfer->status = USBD_IOERROR; /* more info XXX */
862 xfer->status = USBD_NORMAL_COMPLETION;
865 usb_transfer_complete(xfer);
866 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
870 * Wait here until controller claims to have an interrupt.
871 * Then call ehci_intr and return. Use timeout to avoid waiting
875 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
877 int timo = xfer->timeout;
881 xfer->status = USBD_IN_PROGRESS;
882 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
883 usb_delay_ms(&sc->sc_bus, 1);
886 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
888 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
895 if (xfer->status != USBD_IN_PROGRESS)
901 DPRINTF(("ehci_waitintr: timeout\n"));
902 xfer->status = USBD_TIMEOUT;
903 usb_transfer_complete(xfer);
904 /* XXX should free TD */
908 ehci_poll(struct usbd_bus *bus)
910 ehci_softc_t *sc = (ehci_softc_t *)bus;
914 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
916 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
921 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
925 #if defined(__NetBSD__) || defined(__OpenBSD__)
927 ehci_detach(struct ehci_softc *sc, int flags)
931 if (sc->sc_child != NULL)
932 rv = config_detach(sc->sc_child, flags);
937 usb_uncallout(sc->sc_tmo_pcd, ehci_pcd_enable, sc);
939 if (sc->sc_powerhook != NULL)
940 powerhook_disestablish(sc->sc_powerhook);
941 if (sc->sc_shutdownhook != NULL)
942 shutdownhook_disestablish(sc->sc_shutdownhook);
944 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
946 /* XXX free other data structures XXX */
952 #if defined(__NetBSD__) || defined(__OpenBSD__)
954 ehci_activate(device_ptr_t self, enum devact act)
956 struct ehci_softc *sc = (struct ehci_softc *)self;
963 case DVACT_DEACTIVATE:
964 if (sc->sc_child != NULL)
965 rv = config_deactivate(sc->sc_child);
974 * Handle suspend/resume.
976 * We need to switch to polling mode here, because this routine is
977 * called from an interrupt context. This is all right since we
978 * are almost suspended anyway.
980 #if defined(__NetBSD__) || defined(__OpenBSD__)
982 ehci_power(int why, void *v)
984 ehci_softc_t *sc = v;
989 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
999 sc->sc_bus.use_polling++;
1001 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
1003 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
1004 EOWRITE4(sc, EHCI_USBCMD, cmd);
1006 for (i = 0; i < 100; i++) {
1007 hcr = EOREAD4(sc, EHCI_USBSTS) &
1008 (EHCI_STS_ASS | EHCI_STS_PSS);
1012 usb_delay_ms(&sc->sc_bus, 1);
1015 printf("%s: reset timeout\n",
1016 USBDEVNAME(sc->sc_bus.bdev));
1019 cmd &= ~EHCI_CMD_RS;
1020 EOWRITE4(sc, EHCI_USBCMD, cmd);
1022 for (i = 0; i < 100; i++) {
1023 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1024 if (hcr == EHCI_STS_HCH)
1027 usb_delay_ms(&sc->sc_bus, 1);
1029 if (hcr != EHCI_STS_HCH) {
1030 printf("%s: config timeout\n",
1031 USBDEVNAME(sc->sc_bus.bdev));
1034 sc->sc_bus.use_polling--;
1038 sc->sc_bus.use_polling++;
1040 /* restore things in case the bios sucks */
1041 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
1042 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
1043 EOWRITE4(sc, EHCI_ASYNCLISTADDR,
1044 sc->sc_async_head->physaddr | EHCI_LINK_QH);
1045 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1047 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
1049 for (i = 0; i < 100; i++) {
1050 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1051 if (hcr != EHCI_STS_HCH)
1054 usb_delay_ms(&sc->sc_bus, 1);
1056 if (hcr == EHCI_STS_HCH) {
1057 printf("%s: config timeout\n",
1058 USBDEVNAME(sc->sc_bus.bdev));
1061 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1063 sc->sc_bus.use_polling--;
1065 case PWR_SOFTSUSPEND:
1066 case PWR_SOFTSTANDBY:
1067 case PWR_SOFTRESUME:
1073 DPRINTF(("ehci_power: sc=%p\n", sc));
1081 * Shut down the controller when the system is going down.
1083 #if defined(__NetBSD__) || defined(__OpenBSD__)
1085 ehci_shutdown(void *v)
1087 ehci_softc_t *sc = v;
1089 DPRINTF(("ehci_shutdown: stopping the HC\n"));
1090 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
1091 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1096 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
1100 err = usb_allocmem(bus, size, 0, dma);
1103 printf("ehci_allocm: usb_allocmem()=%d\n", err);
1109 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
1111 usb_freemem(bus, dma);
1115 ehci_allocx(struct usbd_bus *bus)
1117 struct ehci_softc *sc = (struct ehci_softc *)bus;
1118 usbd_xfer_handle xfer;
1120 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
1122 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
1124 if (xfer->busy_free != XFER_FREE) {
1125 printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer,
1130 xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_INTWAIT);
1133 memset(xfer, 0, sizeof(struct ehci_xfer));
1135 EXFER(xfer)->isdone = 1;
1136 xfer->busy_free = XFER_BUSY;
1143 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1145 struct ehci_softc *sc = (struct ehci_softc *)bus;
1148 if (xfer->busy_free != XFER_BUSY) {
1149 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
1153 xfer->busy_free = XFER_FREE;
1154 if (!EXFER(xfer)->isdone) {
1155 printf("ehci_freex: !isdone\n");
1159 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
1163 ehci_device_clear_toggle(usbd_pipe_handle pipe)
1165 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1167 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
1168 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
1171 usbd_dump_pipe(pipe);
1173 epipe->nexttoggle = 0;
1177 ehci_noop(usbd_pipe_handle pipe)
1183 ehci_dump_regs(ehci_softc_t *sc)
1186 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1187 EOREAD4(sc, EHCI_USBCMD),
1188 EOREAD4(sc, EHCI_USBSTS),
1189 EOREAD4(sc, EHCI_USBINTR));
1190 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1191 EOREAD4(sc, EHCI_FRINDEX),
1192 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1193 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1194 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1195 for (i = 1; i <= sc->sc_noport; i++)
1196 printf("port %d status=0x%08x\n", i,
1197 EOREAD4(sc, EHCI_PORTSC(i)));
1201 * Unused function - this is meant to be called from a kernel
1207 ehci_dump_regs(theehci);
1211 ehci_dump_link(ehci_link_t link, int type)
1213 link = le32toh(link);
1214 printf("0x%08x", link);
1215 if (link & EHCI_LINK_TERMINATE)
1220 switch (EHCI_LINK_TYPE(link)) {
1221 case EHCI_LINK_ITD: printf("ITD"); break;
1222 case EHCI_LINK_QH: printf("QH"); break;
1223 case EHCI_LINK_SITD: printf("SITD"); break;
1224 case EHCI_LINK_FSTN: printf("FSTN"); break;
1232 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1238 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1239 ehci_dump_sqtd(sqtd);
1240 stop = sqtd->qtd.qtd_next & EHCI_LINK_TERMINATE;
1243 printf("dump aborted, too many TDs\n");
1247 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1249 printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr);
1250 ehci_dump_qtd(&sqtd->qtd);
1254 ehci_dump_qtd(ehci_qtd_t *qtd)
1259 printf(" next="); ehci_dump_link(qtd->qtd_next, 0);
1260 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
1262 s = le32toh(qtd->qtd_status);
1263 bitmask_snprintf(EHCI_QTD_GET_STATUS(s),
1264 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1265 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
1266 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
1267 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
1268 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
1269 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
1270 EHCI_QTD_GET_PID(s), sbuf);
1271 for (s = 0; s < 5; s++)
1272 printf(" buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
1276 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1278 ehci_qh_t *qh = &sqh->qh;
1279 u_int32_t endp, endphub;
1281 printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr);
1282 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
1283 endp = le32toh(qh->qh_endp);
1284 printf(" endp=0x%08x\n", endp);
1285 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
1286 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1287 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
1288 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
1289 printf(" mpl=0x%x ctl=%d nrl=%d\n",
1290 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
1291 EHCI_QH_GET_NRL(endp));
1292 endphub = le32toh(qh->qh_endphub);
1293 printf(" endphub=0x%08x\n", endphub);
1294 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
1295 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
1296 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1297 EHCI_QH_GET_MULT(endphub));
1298 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
1299 printf("Overlay qTD:\n");
1300 ehci_dump_qtd(&qh->qh_qtd);
1305 ehci_dump_exfer(struct ehci_xfer *ex)
1307 printf("ehci_dump_exfer: ex=%p\n", ex);
1313 ehci_open(usbd_pipe_handle pipe)
1315 usbd_device_handle dev = pipe->device;
1316 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
1317 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1318 u_int8_t addr = dev->address;
1319 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1320 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1321 ehci_soft_qh_t *sqh;
1323 int ival, speed, naks;
1325 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1326 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1329 return (USBD_IOERROR);
1331 epipe->nexttoggle = 0;
1333 if (addr == sc->sc_addr) {
1334 switch (ed->bEndpointAddress) {
1335 case USB_CONTROL_ENDPOINT:
1336 pipe->methods = &ehci_root_ctrl_methods;
1338 case UE_DIR_IN | EHCI_INTR_ENDPT:
1339 pipe->methods = &ehci_root_intr_methods;
1342 return (USBD_INVAL);
1344 return (USBD_NORMAL_COMPLETION);
1347 /* XXX All this stuff is only valid for async. */
1348 switch (dev->speed) {
1349 case USB_SPEED_LOW: speed = EHCI_QH_SPEED_LOW; break;
1350 case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
1351 case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
1352 default: panic("ehci_open: bad device speed %d", dev->speed);
1355 sqh = ehci_alloc_sqh(sc);
1358 /* qh_link filled when the QH is added */
1359 sqh->qh.qh_endp = htole32(
1360 EHCI_QH_SET_ADDR(addr) |
1361 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
1362 EHCI_QH_SET_EPS(speed) |
1364 EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
1365 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
1367 EHCI_QH_SET_NRL(naks)
1369 sqh->qh.qh_endphub = htole32(
1370 EHCI_QH_SET_MULT(1) |
1372 EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x01 : 0)
1374 sqh->qh.qh_curqtd = EHCI_NULL;
1375 /* Fill the overlay qTD */
1376 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
1377 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
1378 sqh->qh.qh_qtd.qtd_status = htole32(0);
1384 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
1385 0, &epipe->u.ctl.reqdma);
1388 printf("ehci_open: usb_allocmem()=%d\n", err);
1392 pipe->methods = &ehci_device_ctrl_methods;
1394 ehci_add_qh(sqh, sc->sc_async_head);
1398 pipe->methods = &ehci_device_bulk_methods;
1400 ehci_add_qh(sqh, sc->sc_async_head);
1404 pipe->methods = &ehci_device_intr_methods;
1405 ival = pipe->interval;
1406 if (ival == USBD_DEFAULT_INTERVAL)
1407 ival = ed->bInterval;
1408 return (ehci_device_setintr(sc, sqh, ival));
1409 case UE_ISOCHRONOUS:
1410 pipe->methods = &ehci_device_isoc_methods;
1411 return (USBD_INVAL);
1413 return (USBD_INVAL);
1415 return (USBD_NORMAL_COMPLETION);
1418 ehci_free_sqh(sc, sqh);
1420 return (USBD_NOMEM);
1424 * Add an ED to the schedule. Called while in a critical section.
1427 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1429 sqh->next = head->next;
1430 sqh->qh.qh_link = head->qh.qh_link;
1432 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
1435 if (ehcidebug > 5) {
1436 printf("ehci_add_qh:\n");
1443 * Remove an ED from the schedule. Called while in a critical section.
1446 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1451 for (p = head; p != NULL && p->next != sqh; p = p->next)
1454 panic("ehci_rem_qh: ED not found");
1455 p->next = sqh->next;
1456 p->qh.qh_link = sqh->qh.qh_link;
1462 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
1464 /* Halt while we are messing. */
1465 sqh->qh.qh_qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
1466 sqh->qh.qh_curqtd = 0;
1467 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
1470 sqh->qh.qh_qtd.qtd_status &= htole32(~EHCI_QTD_HALTED);
1474 * Ensure that the HC has released all references to the QH. We do this
1475 * by asking for a Async Advance Doorbell interrupt and then we wait for
1477 * To make this easier we first obtain exclusive use of the doorbell.
1480 ehci_sync_hc(ehci_softc_t *sc)
1485 DPRINTFN(2,("ehci_sync_hc: dying\n"));
1488 DPRINTFN(2,("ehci_sync_hc: enter\n"));
1490 usb_lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE, NULL, NULL);
1492 /* ask for doorbell */
1493 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
1494 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1495 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1496 error = tsleep(&sc->sc_async_head, 0, "ehcidi", hz); /* bell wait */
1497 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1498 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1500 /* release doorbell */
1501 usb_lockmgr(&sc->sc_doorbell_lock, LK_RELEASE, NULL, NULL);
1504 printf("ehci_sync_hc: tsleep() = %d\n", error);
1506 DPRINTFN(2,("ehci_sync_hc: exit\n"));
1512 * Data structures and routines to emulate the root hub.
1514 Static usb_device_descriptor_t ehci_devd = {
1515 USB_DEVICE_DESCRIPTOR_SIZE,
1516 UDESC_DEVICE, /* type */
1517 {0x00, 0x02}, /* USB version */
1518 UDCLASS_HUB, /* class */
1519 UDSUBCLASS_HUB, /* subclass */
1520 UDPROTO_HSHUBSTT, /* protocol */
1521 64, /* max packet */
1522 {0},{0},{0x00,0x01}, /* device id */
1523 1,2,0, /* string indicies */
1524 1 /* # of configurations */
1527 Static usb_device_qualifier_t ehci_odevd = {
1528 USB_DEVICE_DESCRIPTOR_SIZE,
1529 UDESC_DEVICE_QUALIFIER, /* type */
1530 {0x00, 0x02}, /* USB version */
1531 UDCLASS_HUB, /* class */
1532 UDSUBCLASS_HUB, /* subclass */
1533 UDPROTO_FSHUB, /* protocol */
1534 64, /* max packet */
1535 1, /* # of configurations */
1539 Static usb_config_descriptor_t ehci_confd = {
1540 USB_CONFIG_DESCRIPTOR_SIZE,
1542 {USB_CONFIG_DESCRIPTOR_SIZE +
1543 USB_INTERFACE_DESCRIPTOR_SIZE +
1544 USB_ENDPOINT_DESCRIPTOR_SIZE},
1552 Static usb_interface_descriptor_t ehci_ifcd = {
1553 USB_INTERFACE_DESCRIPTOR_SIZE,
1564 Static usb_endpoint_descriptor_t ehci_endpd = {
1565 USB_ENDPOINT_DESCRIPTOR_SIZE,
1567 UE_DIR_IN | EHCI_INTR_ENDPT,
1569 {8, 0}, /* max packet */
1573 Static usb_hub_descriptor_t ehci_hubd = {
1574 USB_HUB_DESCRIPTOR_SIZE,
1584 ehci_str( usb_string_descriptor_t *p, int l, char *s)
1590 p->bLength = 2 * strlen(s) + 2;
1593 p->bDescriptorType = UDESC_STRING;
1595 for (i = 0; s[i] && l > 1; i++, l -= 2)
1596 USETW2(p->bString[i], 0, s[i]);
1601 * Simulate a hardware hub by handling all the necessary requests.
1604 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
1608 /* Insert last in queue. */
1609 err = usb_insert_transfer(xfer);
1613 /* Pipe isn't running, start first */
1614 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1618 ehci_root_ctrl_start(usbd_xfer_handle xfer)
1620 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
1621 usb_device_request_t *req;
1624 int len, value, index, l, totlen = 0;
1625 usb_port_status_t ps;
1626 usb_hub_descriptor_t hubd;
1631 return (USBD_IOERROR);
1634 if (!(xfer->rqflags & URQ_REQUEST))
1636 return (USBD_INVAL);
1638 req = &xfer->request;
1640 DPRINTFN(4,("ehci_root_ctrl_start type=0x%02x request=%02x\n",
1641 req->bmRequestType, req->bRequest));
1643 len = UGETW(req->wLength);
1644 value = UGETW(req->wValue);
1645 index = UGETW(req->wIndex);
1648 buf = KERNADDR(&xfer->dmabuf, 0);
1650 #define C(x,y) ((x) | ((y) << 8))
1651 switch(C(req->bRequest, req->bmRequestType)) {
1652 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1653 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1654 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1656 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1657 * for the integrated root hub.
1660 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1662 *(u_int8_t *)buf = sc->sc_conf;
1666 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1667 DPRINTFN(8,("ehci_root_ctrl_start wValue=0x%04x\n", value));
1668 switch(value >> 8) {
1670 if ((value & 0xff) != 0) {
1674 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1675 USETW(ehci_devd.idVendor, sc->sc_id_vendor);
1676 memcpy(buf, &ehci_devd, l);
1679 * We can't really operate at another speed, but the spec says
1680 * we need this descriptor.
1682 case UDESC_DEVICE_QUALIFIER:
1683 if ((value & 0xff) != 0) {
1687 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1688 memcpy(buf, &ehci_odevd, l);
1691 * We can't really operate at another speed, but the spec says
1692 * we need this descriptor.
1694 case UDESC_OTHER_SPEED_CONFIGURATION:
1696 if ((value & 0xff) != 0) {
1700 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
1701 memcpy(buf, &ehci_confd, l);
1702 ((usb_config_descriptor_t *)buf)->bDescriptorType =
1704 buf = (char *)buf + l;
1706 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
1708 memcpy(buf, &ehci_ifcd, l);
1709 buf = (char *)buf + l;
1711 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
1713 memcpy(buf, &ehci_endpd, l);
1718 *(u_int8_t *)buf = 0;
1720 switch (value & 0xff) {
1721 case 1: /* Vendor */
1722 totlen = ehci_str(buf, len, sc->sc_vendor);
1724 case 2: /* Product */
1725 totlen = ehci_str(buf, len, "EHCI root hub");
1734 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
1736 *(u_int8_t *)buf = 0;
1740 case C(UR_GET_STATUS, UT_READ_DEVICE):
1742 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
1746 case C(UR_GET_STATUS, UT_READ_INTERFACE):
1747 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
1749 USETW(((usb_status_t *)buf)->wStatus, 0);
1753 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
1754 if (value >= USB_MAX_DEVICES) {
1758 sc->sc_addr = value;
1760 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
1761 if (value != 0 && value != 1) {
1765 sc->sc_conf = value;
1767 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
1769 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
1770 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
1771 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
1774 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
1776 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
1779 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
1781 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
1782 DPRINTFN(8, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
1783 "port=%d feature=%d\n",
1785 if (index < 1 || index > sc->sc_noport) {
1789 port = EHCI_PORTSC(index);
1790 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1792 case UHF_PORT_ENABLE:
1793 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
1795 case UHF_PORT_SUSPEND:
1796 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
1798 case UHF_PORT_POWER:
1799 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
1802 DPRINTFN(2,("ehci_root_ctrl_start: clear port test "
1805 case UHF_PORT_INDICATOR:
1806 DPRINTFN(2,("ehci_root_ctrl_start: clear port ind "
1808 EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
1810 case UHF_C_PORT_CONNECTION:
1811 EOWRITE4(sc, port, v | EHCI_PS_CSC);
1813 case UHF_C_PORT_ENABLE:
1814 EOWRITE4(sc, port, v | EHCI_PS_PEC);
1816 case UHF_C_PORT_SUSPEND:
1819 case UHF_C_PORT_OVER_CURRENT:
1820 EOWRITE4(sc, port, v | EHCI_PS_OCC);
1822 case UHF_C_PORT_RESET:
1831 case UHF_C_PORT_CONNECTION:
1832 case UHF_C_PORT_ENABLE:
1833 case UHF_C_PORT_SUSPEND:
1834 case UHF_C_PORT_OVER_CURRENT:
1835 case UHF_C_PORT_RESET:
1836 /* Enable RHSC interrupt if condition is cleared. */
1837 if ((OREAD4(sc, port) >> 16) == 0)
1838 ehci_pcd_able(sc, 1);
1845 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
1846 if ((value & 0xff) != 0) {
1851 hubd.bNbrPorts = sc->sc_noport;
1852 v = EOREAD4(sc, EHCI_HCSPARAMS);
1853 USETW(hubd.wHubCharacteristics,
1854 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
1855 EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
1856 ? UHD_PORT_IND : 0);
1857 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
1858 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
1859 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
1860 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
1861 l = min(len, hubd.bDescLength);
1863 memcpy(buf, &hubd, l);
1865 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
1870 memset(buf, 0, len); /* ? XXX */
1873 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
1874 DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n",
1876 if (index < 1 || index > sc->sc_noport) {
1884 v = EOREAD4(sc, EHCI_PORTSC(index));
1885 DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n", v));
1887 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
1888 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
1889 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
1890 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
1891 if (v & EHCI_PS_PR) i |= UPS_RESET;
1892 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
1893 USETW(ps.wPortStatus, i);
1895 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
1896 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
1897 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
1898 if (sc->sc_isreset) i |= UPS_C_PORT_RESET;
1899 USETW(ps.wPortChange, i);
1900 l = min(len, sizeof ps);
1901 memcpy(buf, &ps, l);
1904 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
1907 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
1909 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
1910 if (index < 1 || index > sc->sc_noport) {
1914 port = EHCI_PORTSC(index);
1915 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1917 case UHF_PORT_ENABLE:
1918 EOWRITE4(sc, port, v | EHCI_PS_PE);
1920 case UHF_PORT_SUSPEND:
1921 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
1923 case UHF_PORT_RESET:
1924 DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n",
1926 if (EHCI_PS_IS_LOWSPEED(v)) {
1927 /* Low speed device, give up ownership. */
1928 ehci_disown(sc, index, 1);
1931 /* Start reset sequence. */
1932 v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
1933 EOWRITE4(sc, port, v | EHCI_PS_PR);
1934 /* Wait for reset to complete. */
1935 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
1940 /* Terminate reset sequence. */
1941 EOWRITE4(sc, port, v);
1942 /* Wait for HC to complete reset. */
1943 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE);
1948 v = EOREAD4(sc, port);
1949 DPRINTF(("ehci after reset, status=0x%08x\n", v));
1950 if (v & EHCI_PS_PR) {
1951 printf("%s: port reset timeout\n",
1952 USBDEVNAME(sc->sc_bus.bdev));
1953 return (USBD_TIMEOUT);
1955 if (!(v & EHCI_PS_PE)) {
1956 /* Not a high speed device, give up ownership.*/
1957 ehci_disown(sc, index, 0);
1961 DPRINTF(("ehci port %d reset, status = 0x%08x\n",
1964 case UHF_PORT_POWER:
1965 DPRINTFN(2,("ehci_root_ctrl_start: set port power "
1967 EOWRITE4(sc, port, v | EHCI_PS_PP);
1970 DPRINTFN(2,("ehci_root_ctrl_start: set port test "
1973 case UHF_PORT_INDICATOR:
1974 DPRINTFN(2,("ehci_root_ctrl_start: set port ind "
1976 EOWRITE4(sc, port, v | EHCI_PS_PIC);
1983 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
1984 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
1985 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
1986 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
1992 xfer->actlen = totlen;
1993 err = USBD_NORMAL_COMPLETION;
1997 usb_transfer_complete(xfer);
1999 return (USBD_IN_PROGRESS);
2003 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
2008 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
2010 if (sc->sc_npcomp != 0) {
2011 int i = (index-1) / sc->sc_npcomp;
2012 if (i >= sc->sc_ncomp)
2013 printf("%s: strange port\n",
2014 USBDEVNAME(sc->sc_bus.bdev));
2016 printf("%s: handing over %s speed device on "
2018 USBDEVNAME(sc->sc_bus.bdev),
2019 lowspeed ? "low" : "full",
2020 index, USBDEVNAME(sc->sc_comps[i]->bdev));
2022 printf("%s: npcomp == 0\n", USBDEVNAME(sc->sc_bus.bdev));
2025 port = EHCI_PORTSC(index);
2026 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
2027 EOWRITE4(sc, port, v | EHCI_PS_PO);
2030 /* Abort a root control request. */
2032 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
2034 /* Nothing to do, all transfers are synchronous. */
2037 /* Close the root pipe. */
2039 ehci_root_ctrl_close(usbd_pipe_handle pipe)
2041 DPRINTF(("ehci_root_ctrl_close\n"));
2042 /* Nothing to do. */
2046 ehci_root_intr_done(usbd_xfer_handle xfer)
2048 xfer->hcpriv = NULL;
2052 ehci_root_intr_transfer(usbd_xfer_handle xfer)
2056 /* Insert last in queue. */
2057 err = usb_insert_transfer(xfer);
2061 /* Pipe isn't running, start first */
2062 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2066 ehci_root_intr_start(usbd_xfer_handle xfer)
2068 usbd_pipe_handle pipe = xfer->pipe;
2069 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2072 return (USBD_IOERROR);
2074 sc->sc_intrxfer = xfer;
2076 return (USBD_IN_PROGRESS);
2079 /* Abort a root interrupt request. */
2081 ehci_root_intr_abort(usbd_xfer_handle xfer)
2083 if (xfer->pipe->intrxfer == xfer) {
2084 DPRINTF(("ehci_root_intr_abort: remove\n"));
2085 xfer->pipe->intrxfer = NULL;
2087 xfer->status = USBD_CANCELLED;
2089 usb_transfer_complete(xfer);
2093 /* Close the root pipe. */
2095 ehci_root_intr_close(usbd_pipe_handle pipe)
2097 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2099 DPRINTF(("ehci_root_intr_close\n"));
2101 sc->sc_intrxfer = NULL;
2105 ehci_root_ctrl_done(usbd_xfer_handle xfer)
2107 xfer->hcpriv = NULL;
2110 /************************/
2113 ehci_alloc_sqh(ehci_softc_t *sc)
2115 ehci_soft_qh_t *sqh;
2120 if (sc->sc_freeqhs == NULL) {
2121 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
2122 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
2123 EHCI_PAGE_SIZE, &dma);
2126 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2130 for(i = 0; i < EHCI_SQH_CHUNK; i++) {
2131 offs = i * EHCI_SQH_SIZE;
2132 sqh = KERNADDR(&dma, offs);
2133 sqh->physaddr = DMAADDR(&dma, offs);
2134 sqh->next = sc->sc_freeqhs;
2135 sc->sc_freeqhs = sqh;
2138 sqh = sc->sc_freeqhs;
2139 sc->sc_freeqhs = sqh->next;
2140 memset(&sqh->qh, 0, sizeof(ehci_qh_t));
2146 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2148 sqh->next = sc->sc_freeqhs;
2149 sc->sc_freeqhs = sqh;
2153 ehci_alloc_sqtd(ehci_softc_t *sc)
2155 ehci_soft_qtd_t *sqtd;
2160 if (sc->sc_freeqtds == NULL) {
2161 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
2162 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
2163 EHCI_PAGE_SIZE, &dma);
2166 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
2171 for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
2172 offs = i * EHCI_SQTD_SIZE;
2173 sqtd = KERNADDR(&dma, offs);
2174 sqtd->physaddr = DMAADDR(&dma, offs);
2175 sqtd->nextqtd = sc->sc_freeqtds;
2176 sc->sc_freeqtds = sqtd;
2182 sqtd = sc->sc_freeqtds;
2183 sc->sc_freeqtds = sqtd->nextqtd;
2184 memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
2185 sqtd->nextqtd = NULL;
2193 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2196 sqtd->nextqtd = sc->sc_freeqtds;
2197 sc->sc_freeqtds = sqtd;
2202 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
2203 int alen, int rd, usbd_xfer_handle xfer,
2204 ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
2206 ehci_soft_qtd_t *next, *cur;
2207 ehci_physaddr_t dataphys, dataphyspage, dataphyslastpage, nextphys;
2208 u_int32_t qtdstatus;
2209 int len, curlen, mps;
2212 usb_dma_t *dma = &xfer->dmabuf;
2214 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
2218 dataphys = DMAADDR(dma, 0);
2219 dataphyslastpage = EHCI_PAGE(DMAADDR(dma, len - 1));
2221 printf("status=%08x toggle=%d\n", epipe->sqh->qh.qh_qtd.qtd_status,
2224 qtdstatus = EHCI_QTD_ACTIVE |
2225 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2226 EHCI_QTD_SET_CERR(3)
2228 /* BYTES set below */
2230 mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
2231 tog = epipe->nexttoggle;
2232 qtdstatus |= EHCI_QTD_SET_TOGGLE(tog);
2234 cur = ehci_alloc_sqtd(sc);
2239 dataphyspage = EHCI_PAGE(dataphys);
2240 /* The EHCI hardware can handle at most 5 pages. */
2241 #if defined(__NetBSD__) || defined(__OpenBSD__)
2242 if (dataphyslastpage - dataphyspage <
2243 EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE) {
2244 /* we can handle it in this QTD */
2246 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2247 /* XXX This is pretty broken: Because we do not allocate
2248 * a contiguous buffer (contiguous in physical pages) we
2249 * can only transfer one page in one go.
2250 * So check whether the start and end of the buffer are on
2253 if (dataphyspage == dataphyslastpage) {
2257 #if defined(__NetBSD__) || defined(__OpenBSD__)
2258 /* must use multiple TDs, fill as much as possible. */
2259 curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE -
2260 EHCI_PAGE_OFFSET(dataphys);
2263 printf("ehci_alloc_sqtd_chain: curlen=0x%x "
2264 "len=0x%x offs=0x%x\n", curlen, len,
2265 EHCI_PAGE_OFFSET(dataphys));
2266 printf("lastpage=0x%x page=0x%x phys=0x%x\n",
2267 dataphyslastpage, dataphyspage,
2272 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2273 /* See comment above (XXX) */
2274 curlen = EHCI_PAGE_SIZE -
2275 EHCI_PAGE_MASK(dataphys);
2277 /* the length must be a multiple of the max size */
2278 curlen -= curlen % mps;
2279 DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, "
2280 "curlen=%d\n", curlen));
2283 panic("ehci_alloc_std: curlen == 0");
2286 DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x "
2287 "dataphyslastpage=0x%08x len=%d curlen=%d\n",
2288 dataphys, dataphyslastpage,
2293 next = ehci_alloc_sqtd(sc);
2296 nextphys = htole32(next->physaddr);
2299 nextphys = EHCI_NULL;
2302 for (i = 0; i * EHCI_PAGE_SIZE < curlen; i++) {
2303 ehci_physaddr_t a = dataphys + i * EHCI_PAGE_SIZE;
2304 if (i != 0) /* use offset only in first buffer */
2306 cur->qtd.qtd_buffer[i] = htole32(a);
2307 cur->qtd.qtd_buffer_hi[i] = 0;
2309 if (i >= EHCI_QTD_NBUFFERS) {
2310 printf("ehci_alloc_sqtd_chain: i=%d\n", i);
2315 cur->nextqtd = next;
2316 cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys;
2317 cur->qtd.qtd_status =
2318 htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
2321 DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n",
2322 dataphys, dataphys + curlen));
2323 /* adjust the toggle based on the number of packets in this
2325 if (((curlen + mps - 1) / mps) & 1) {
2327 qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
2331 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
2333 dataphys = DMAADDR(dma, offset);
2336 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
2338 epipe->nexttoggle = tog;
2340 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
2343 return (USBD_NORMAL_COMPLETION);
2346 /* XXX free chain */
2347 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
2348 return (USBD_NOMEM);
2352 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd,
2353 ehci_soft_qtd_t *sqtdend)
2358 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
2361 for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
2363 ehci_free_sqtd(sc, sqtd);
2370 * Close a reqular pipe.
2371 * Assumes that there are no pending transactions.
2374 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
2376 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
2377 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2378 ehci_soft_qh_t *sqh = epipe->sqh;
2381 ehci_rem_qh(sc, sqh, head);
2383 ehci_free_sqh(sc, epipe->sqh);
2387 * Abort a device request.
2388 * If this routine is called from a critical section it guarantees that the
2389 * request will be removed from the hardware scheduling and that the callback
2390 * for it will be called with USBD_CANCELLED status.
2391 * It's impossible to guarantee that the requested transfer will not
2392 * have happened since the hardware runs concurrently.
2393 * If the transaction has already happened we rely on the ordinary
2394 * interrupt processing to process it.
2395 * XXX This is most probably wrong.
2398 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2400 #define exfer EXFER(xfer)
2401 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2402 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2403 ehci_soft_qh_t *sqh = epipe->sqh;
2404 ehci_soft_qtd_t *sqtd;
2405 ehci_physaddr_t cur;
2409 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
2412 /* If we're dying, just do the software part. */
2414 xfer->status = status; /* make software ignore it */
2415 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2416 usb_transfer_complete(xfer);
2421 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
2422 panic("ehci_abort_xfer: not in process context");
2425 * Step 1: Make interrupt routine and hardware ignore xfer.
2428 xfer->status = status; /* make software ignore it */
2429 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2430 qhstatus = sqh->qh.qh_qtd.qtd_status;
2431 sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
2432 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2433 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
2434 if (sqtd == exfer->sqtdend)
2440 * Step 2: Wait until we know hardware has finished any possible
2441 * use of the xfer. Also make sure the soft interrupt routine
2446 #ifdef USB_USE_SOFTINTR
2447 sc->sc_softwake = 1;
2448 #endif /* USB_USE_SOFTINTR */
2449 usb_schedsoftintr(&sc->sc_bus);
2450 #ifdef USB_USE_SOFTINTR
2451 tsleep(&sc->sc_softwake, 0, "ehciab", 0);
2452 #endif /* USB_USE_SOFTINTR */
2456 * Step 3: Remove any vestiges of the xfer from the hardware.
2457 * The complication here is that the hardware may have executed
2458 * beyond the xfer we're trying to abort. So as we're scanning
2459 * the TDs of this xfer we check if the hardware points to
2462 crit_enter(); /* XXX why? */
2463 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
2465 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2466 hit |= cur == sqtd->physaddr;
2467 if (sqtd == exfer->sqtdend)
2470 sqtd = sqtd->nextqtd;
2471 /* Zap curqtd register if hardware pointed inside the xfer. */
2472 if (hit && sqtd != NULL) {
2473 DPRINTFN(1,("ehci_abort_xfer: cur=0x%08x\n", sqtd->physaddr));
2474 sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
2475 sqh->qh.qh_qtd.qtd_status = qhstatus;
2477 DPRINTFN(1,("ehci_abort_xfer: no hit\n"));
2481 * Step 4: Execute callback.
2486 usb_transfer_complete(xfer);
2493 ehci_timeout(void *addr)
2495 struct ehci_xfer *exfer = addr;
2496 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
2497 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2499 DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
2502 usbd_dump_pipe(exfer->xfer.pipe);
2506 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
2510 /* Execute the abort in a process context. */
2511 usb_init_task(&exfer->abort_task, ehci_timeout_task, addr);
2512 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task);
2516 ehci_timeout_task(void *addr)
2518 usbd_xfer_handle xfer = addr;
2520 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
2522 ehci_abort_xfer(xfer, USBD_TIMEOUT);
2526 /************************/
2529 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
2533 /* Insert last in queue. */
2534 err = usb_insert_transfer(xfer);
2538 /* Pipe isn't running, start first */
2539 return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2543 ehci_device_ctrl_start(usbd_xfer_handle xfer)
2545 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2549 return (USBD_IOERROR);
2552 if (!(xfer->rqflags & URQ_REQUEST)) {
2554 printf("ehci_device_ctrl_transfer: not a request\n");
2555 return (USBD_INVAL);
2559 err = ehci_device_request(xfer);
2563 if (sc->sc_bus.use_polling)
2564 ehci_waitintr(sc, xfer);
2565 return (USBD_IN_PROGRESS);
2569 ehci_device_ctrl_done(usbd_xfer_handle xfer)
2571 struct ehci_xfer *ex = EXFER(xfer);
2572 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2573 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
2575 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
2578 if (!(xfer->rqflags & URQ_REQUEST)) {
2579 panic("ehci_ctrl_done: not a request");
2583 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2584 ehci_del_intr_list(ex); /* remove from active list */
2585 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
2588 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
2591 /* Abort a device control request. */
2593 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
2595 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
2596 ehci_abort_xfer(xfer, USBD_CANCELLED);
2599 /* Close a device control pipe. */
2601 ehci_device_ctrl_close(usbd_pipe_handle pipe)
2603 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2604 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
2606 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
2607 ehci_close_pipe(pipe, sc->sc_async_head);
2611 ehci_device_request(usbd_xfer_handle xfer)
2613 #define exfer EXFER(xfer)
2614 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2615 usb_device_request_t *req = &xfer->request;
2616 usbd_device_handle dev = epipe->pipe.device;
2617 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2618 int addr = dev->address;
2619 ehci_soft_qtd_t *setup, *stat, *next;
2620 ehci_soft_qh_t *sqh;
2625 isread = req->bmRequestType & UT_READ;
2626 len = UGETW(req->wLength);
2628 DPRINTFN(3,("ehci_device_request type=0x%02x, request=0x%02x, "
2629 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2630 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2631 UGETW(req->wIndex), len, addr,
2632 epipe->pipe.endpoint->edesc->bEndpointAddress));
2634 setup = ehci_alloc_sqtd(sc);
2635 if (setup == NULL) {
2639 stat = ehci_alloc_sqtd(sc);
2646 epipe->u.ctl.length = len;
2648 /* Update device address and length since they may have changed
2649 during the setup of the control pipe in usbd_new_device(). */
2650 /* XXX This only needs to be done once, but it's too early in open. */
2651 /* XXXX Should not touch ED here! */
2653 (sqh->qh.qh_endp & htole32(~(EHCI_QH_ADDRMASK | EHCI_QH_MPLMASK))) |
2655 EHCI_QH_SET_ADDR(addr) |
2656 EHCI_QH_SET_MPL(UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize))
2659 /* Set up data transaction */
2661 ehci_soft_qtd_t *end;
2663 /* Start toggle at 1. */
2664 epipe->nexttoggle = 1;
2665 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
2669 end->nextqtd = stat;
2671 end->qtd.qtd_altnext = htole32(stat->physaddr);
2676 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
2679 setup->qtd.qtd_status = htole32(
2681 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
2682 EHCI_QTD_SET_CERR(3) |
2683 EHCI_QTD_SET_TOGGLE(0) |
2684 EHCI_QTD_SET_BYTES(sizeof *req)
2686 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
2687 setup->qtd.qtd_buffer_hi[0] = 0;
2688 setup->nextqtd = next;
2689 setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
2691 setup->len = sizeof *req;
2693 stat->qtd.qtd_status = htole32(
2695 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
2696 EHCI_QTD_SET_CERR(3) |
2697 EHCI_QTD_SET_TOGGLE(1) |
2700 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
2701 stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */
2702 stat->nextqtd = NULL;
2703 stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL;
2708 if (ehcidebug > 5) {
2709 DPRINTF(("ehci_device_request:\n"));
2711 ehci_dump_sqtds(setup);
2715 exfer->sqtdstart = setup;
2716 exfer->sqtdend = stat;
2718 if (!exfer->isdone) {
2719 printf("ehci_device_request: not done, exfer=%p\n", exfer);
2724 /* Insert qTD in QH list. */
2726 ehci_set_qh_qtd(sqh, setup);
2727 if (xfer->timeout && !sc->sc_bus.use_polling) {
2728 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2729 ehci_timeout, xfer);
2731 ehci_add_intr_list(sc, exfer);
2732 xfer->status = USBD_IN_PROGRESS;
2736 if (ehcidebug > 10) {
2737 DPRINTF(("ehci_device_request: status=%x\n",
2738 EOREAD4(sc, EHCI_USBSTS)));
2741 ehci_dump_sqh(sc->sc_async_head);
2743 ehci_dump_sqtds(setup);
2747 return (USBD_NORMAL_COMPLETION);
2750 ehci_free_sqtd(sc, stat);
2752 ehci_free_sqtd(sc, setup);
2754 DPRINTFN(-1,("ehci_device_request: no memory\n"));
2756 usb_transfer_complete(xfer);
2761 /************************/
2764 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
2768 /* Insert last in queue. */
2769 err = usb_insert_transfer(xfer);
2773 /* Pipe isn't running, start first */
2774 return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2778 ehci_device_bulk_start(usbd_xfer_handle xfer)
2780 #define exfer EXFER(xfer)
2781 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2782 usbd_device_handle dev = epipe->pipe.device;
2783 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2784 ehci_soft_qtd_t *data, *dataend;
2785 ehci_soft_qh_t *sqh;
2787 int len, isread, endpt;
2789 DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n",
2790 xfer, xfer->length, xfer->flags));
2793 return (USBD_IOERROR);
2796 if (xfer->rqflags & URQ_REQUEST)
2797 panic("ehci_device_bulk_start: a request");
2801 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
2802 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2805 epipe->u.bulk.length = len;
2807 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
2810 DPRINTFN(-1,("ehci_device_bulk_start: no memory\n"));
2812 usb_transfer_complete(xfer);
2817 if (ehcidebug > 5) {
2818 DPRINTF(("ehci_device_bulk_start: data(1)\n"));
2820 ehci_dump_sqtds(data);
2824 /* Set up interrupt info. */
2825 exfer->sqtdstart = data;
2826 exfer->sqtdend = dataend;
2828 if (!exfer->isdone) {
2829 printf("ehci_device_bulk_start: not done, ex=%p\n", exfer);
2835 ehci_set_qh_qtd(sqh, data);
2836 if (xfer->timeout && !sc->sc_bus.use_polling) {
2837 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2838 ehci_timeout, xfer);
2840 ehci_add_intr_list(sc, exfer);
2841 xfer->status = USBD_IN_PROGRESS;
2845 if (ehcidebug > 10) {
2846 DPRINTF(("ehci_device_bulk_start: data(2)\n"));
2848 DPRINTF(("ehci_device_bulk_start: data(3)\n"));
2851 printf("async_head:\n");
2852 ehci_dump_sqh(sc->sc_async_head);
2856 ehci_dump_sqtds(data);
2860 if (sc->sc_bus.use_polling)
2861 ehci_waitintr(sc, xfer);
2863 return (USBD_IN_PROGRESS);
2868 ehci_device_bulk_abort(usbd_xfer_handle xfer)
2870 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
2871 ehci_abort_xfer(xfer, USBD_CANCELLED);
2875 * Close a device bulk pipe.
2878 ehci_device_bulk_close(usbd_pipe_handle pipe)
2880 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2882 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
2883 ehci_close_pipe(pipe, sc->sc_async_head);
2887 ehci_device_bulk_done(usbd_xfer_handle xfer)
2889 struct ehci_xfer *ex = EXFER(xfer);
2890 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2891 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
2893 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
2894 xfer, xfer->actlen));
2896 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2897 ehci_del_intr_list(ex); /* remove from active list */
2898 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
2901 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
2904 /************************/
2907 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
2909 struct ehci_soft_islot *isp;
2912 /* Find a poll rate that is large enough. */
2913 for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
2914 if (EHCI_ILEV_IVAL(lev) <= ival)
2917 /* Pick an interrupt slot at the right level. */
2918 /* XXX could do better than picking at random */
2919 sc->sc_rand = (sc->sc_rand + 191) % sc->sc_flsize;
2920 islot = EHCI_IQHIDX(lev, sc->sc_rand);
2923 isp = &sc->sc_islots[islot];
2924 ehci_add_qh(sqh, isp->sqh);
2926 return (USBD_NORMAL_COMPLETION);
2930 ehci_device_intr_transfer(usbd_xfer_handle xfer)
2934 /* Insert last in queue. */
2935 err = usb_insert_transfer(xfer);
2940 * Pipe isn't running (otherwise err would be USBD_INPROG),
2941 * so start it first.
2943 return (ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2947 ehci_device_intr_start(usbd_xfer_handle xfer)
2949 #define exfer EXFER(xfer)
2950 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2951 usbd_device_handle dev = xfer->pipe->device;
2952 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2953 ehci_soft_qtd_t *data, *dataend;
2954 ehci_soft_qh_t *sqh;
2956 int len, isread, endpt;
2959 DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%d flags=%d\n",
2960 xfer, xfer->length, xfer->flags));
2963 return (USBD_IOERROR);
2966 if (xfer->rqflags & URQ_REQUEST)
2967 panic("ehci_device_intr_start: a request");
2971 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
2972 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2975 epipe->u.intr.length = len;
2977 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
2980 DPRINTFN(-1, ("ehci_device_intr_start: no memory\n"));
2982 usb_transfer_complete(xfer);
2987 if (ehcidebug > 5) {
2988 DPRINTF(("ehci_device_intr_start: data(1)\n"));
2990 ehci_dump_sqtds(data);
2994 /* Set up interrupt info. */
2995 exfer->sqtdstart = data;
2996 exfer->sqtdend = dataend;
2998 if (!exfer->isdone) {
2999 printf("ehci_device_intr_start: not done, ex=%p\n", exfer);
3005 ehci_set_qh_qtd(sqh, data);
3006 if (xfer->timeout && !sc->sc_bus.use_polling) {
3007 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3008 ehci_timeout, xfer);
3010 ehci_add_intr_list(sc, exfer);
3011 xfer->status = USBD_IN_PROGRESS;
3015 if (ehcidebug > 10) {
3016 DPRINTF(("ehci_device_intr_start: data(2)\n"));
3018 DPRINTF(("ehci_device_intr_start: data(3)\n"));
3022 ehci_dump_sqtds(data);
3026 if (sc->sc_bus.use_polling)
3027 ehci_waitintr(sc, xfer);
3028 return (USBD_IN_PROGRESS);
3033 ehci_device_intr_abort(usbd_xfer_handle xfer)
3035 DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer));
3036 if (xfer->pipe->intrxfer == xfer) {
3037 DPRINTFN(1, ("echi_device_intr_abort: remove\n"));
3038 xfer->pipe->intrxfer = NULL;
3040 ehci_abort_xfer(xfer, USBD_CANCELLED);
3044 ehci_device_intr_close(usbd_pipe_handle pipe)
3046 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3047 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
3048 struct ehci_soft_islot *isp;
3050 isp = &sc->sc_islots[epipe->sqh->islot];
3051 ehci_close_pipe(pipe, isp->sqh);
3055 ehci_device_intr_done(usbd_xfer_handle xfer)
3057 #define exfer EXFER(xfer)
3058 struct ehci_xfer *ex = EXFER(xfer);
3059 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3060 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3061 ehci_soft_qtd_t *data, *dataend;
3062 ehci_soft_qh_t *sqh;
3064 int len, isread, endpt, s;
3066 DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n",
3067 xfer, xfer->actlen));
3069 if (xfer->pipe->repeat) {
3070 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
3072 len = epipe->u.intr.length;
3074 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3075 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3078 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3081 DPRINTFN(-1, ("ehci_device_intr_done: no memory\n"));
3086 /* Set up interrupt info. */
3087 exfer->sqtdstart = data;
3088 exfer->sqtdend = dataend;
3090 if (!exfer->isdone) {
3091 printf("ehci_device_intr_done: not done, ex=%p\n",
3098 ehci_set_qh_qtd(sqh, data);
3099 if (xfer->timeout && !sc->sc_bus.use_polling) {
3100 usb_callout(xfer->timeout_handle,
3101 MS_TO_TICKS(xfer->timeout), ehci_timeout,
3106 xfer->status = USBD_IN_PROGRESS;
3107 } else if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3108 ehci_del_intr_list(ex); /* remove from active list */
3109 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
3114 /************************/
3116 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
3117 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
3118 Static void ehci_device_isoc_abort(usbd_xfer_handle xfer) { }
3119 Static void ehci_device_isoc_close(usbd_pipe_handle pipe) { }
3120 Static void ehci_device_isoc_done(usbd_xfer_handle xfer) { }