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.6 2004/07/08 03:47:09 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>
93 #include <machine/bus.h>
94 #include <machine/endian.h>
96 #include <bus/usb/usb.h>
97 #include <bus/usb/usbdi.h>
98 #include <bus/usb/usbdivar.h>
99 #include <bus/usb/usb_mem.h>
100 #include <bus/usb/usb_quirks.h>
102 #include <bus/usb/ehcireg.h>
103 #include <bus/usb/ehcivar.h>
105 #if defined(__FreeBSD__) || defined(__DragonFly__)
106 #include <machine/clock.h>
108 #define delay(d) DELAY(d)
112 #define DPRINTF(x) if (ehcidebug) logprintf x
113 #define DPRINTFN(n,x) if (ehcidebug>(n)) logprintf x
115 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
116 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
117 &ehcidebug, 0, "ehci debug level");
119 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
123 #define DPRINTFN(n,x)
127 struct usbd_pipe pipe;
132 ehci_soft_qtd_t *qtd;
133 /* ehci_soft_itd_t *itd; */
140 /*ehci_soft_qtd_t *setup, *data, *stat;*/
153 #if defined(__NetBSD__) || defined(__OpenBSD__)
154 Static void ehci_shutdown(void *);
155 Static void ehci_power(int, void *);
158 Static usbd_status ehci_open(usbd_pipe_handle);
159 Static void ehci_poll(struct usbd_bus *);
160 Static void ehci_softintr(void *);
161 Static int ehci_intr1(ehci_softc_t *);
162 Static void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
163 Static void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
164 Static void ehci_idone(struct ehci_xfer *);
165 Static void ehci_timeout(void *);
166 Static void ehci_timeout_task(void *);
168 Static usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
169 Static void ehci_freem(struct usbd_bus *, usb_dma_t *);
171 Static usbd_xfer_handle ehci_allocx(struct usbd_bus *);
172 Static void ehci_freex(struct usbd_bus *, usbd_xfer_handle);
174 Static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle);
175 Static usbd_status ehci_root_ctrl_start(usbd_xfer_handle);
176 Static void ehci_root_ctrl_abort(usbd_xfer_handle);
177 Static void ehci_root_ctrl_close(usbd_pipe_handle);
178 Static void ehci_root_ctrl_done(usbd_xfer_handle);
180 Static usbd_status ehci_root_intr_transfer(usbd_xfer_handle);
181 Static usbd_status ehci_root_intr_start(usbd_xfer_handle);
182 Static void ehci_root_intr_abort(usbd_xfer_handle);
183 Static void ehci_root_intr_close(usbd_pipe_handle);
184 Static void ehci_root_intr_done(usbd_xfer_handle);
186 Static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle);
187 Static usbd_status ehci_device_ctrl_start(usbd_xfer_handle);
188 Static void ehci_device_ctrl_abort(usbd_xfer_handle);
189 Static void ehci_device_ctrl_close(usbd_pipe_handle);
190 Static void ehci_device_ctrl_done(usbd_xfer_handle);
192 Static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle);
193 Static usbd_status ehci_device_bulk_start(usbd_xfer_handle);
194 Static void ehci_device_bulk_abort(usbd_xfer_handle);
195 Static void ehci_device_bulk_close(usbd_pipe_handle);
196 Static void ehci_device_bulk_done(usbd_xfer_handle);
198 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle);
199 Static usbd_status ehci_device_intr_start(usbd_xfer_handle);
200 Static void ehci_device_intr_abort(usbd_xfer_handle);
201 Static void ehci_device_intr_close(usbd_pipe_handle);
202 Static void ehci_device_intr_done(usbd_xfer_handle);
204 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle);
205 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle);
206 Static void ehci_device_isoc_abort(usbd_xfer_handle);
207 Static void ehci_device_isoc_close(usbd_pipe_handle);
208 Static void ehci_device_isoc_done(usbd_xfer_handle);
210 Static void ehci_device_clear_toggle(usbd_pipe_handle pipe);
211 Static void ehci_noop(usbd_pipe_handle pipe);
213 Static int ehci_str(usb_string_descriptor_t *, int, char *);
214 Static void ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
215 Static void ehci_pcd_able(ehci_softc_t *, int);
216 Static void ehci_pcd_enable(void *);
217 Static void ehci_disown(ehci_softc_t *, int, int);
219 Static ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *);
220 Static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
222 Static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
223 Static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
224 Static usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *,
225 ehci_softc_t *, int, int, usbd_xfer_handle,
226 ehci_soft_qtd_t **, ehci_soft_qtd_t **);
227 Static void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *,
230 Static usbd_status ehci_device_request(usbd_xfer_handle xfer);
232 Static void ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *);
233 Static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
235 Static void ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
236 Static void ehci_sync_hc(ehci_softc_t *);
238 Static void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
239 Static void ehci_abort_xfer(usbd_xfer_handle, usbd_status);
242 Static void ehci_dump_regs(ehci_softc_t *);
243 void ehci_dump(void);
244 Static ehci_softc_t *theehci;
245 Static void ehci_dump_link(ehci_link_t, int);
246 Static void ehci_dump_sqtds(ehci_soft_qtd_t *);
247 Static void ehci_dump_sqtd(ehci_soft_qtd_t *);
248 Static void ehci_dump_qtd(ehci_qtd_t *);
249 Static void ehci_dump_sqh(ehci_soft_qh_t *);
251 Static void ehci_dump_exfer(struct ehci_xfer *);
255 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
257 #define EHCI_INTR_ENDPT 1
259 #define ehci_add_intr_list(sc, ex) \
260 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext);
261 #define ehci_del_intr_list(ex) \
263 LIST_REMOVE((ex), inext); \
264 (ex)->inext.le_prev = NULL; \
266 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL)
268 Static struct usbd_bus_methods ehci_bus_methods = {
278 Static struct usbd_pipe_methods ehci_root_ctrl_methods = {
279 ehci_root_ctrl_transfer,
280 ehci_root_ctrl_start,
281 ehci_root_ctrl_abort,
282 ehci_root_ctrl_close,
287 Static struct usbd_pipe_methods ehci_root_intr_methods = {
288 ehci_root_intr_transfer,
289 ehci_root_intr_start,
290 ehci_root_intr_abort,
291 ehci_root_intr_close,
296 Static struct usbd_pipe_methods ehci_device_ctrl_methods = {
297 ehci_device_ctrl_transfer,
298 ehci_device_ctrl_start,
299 ehci_device_ctrl_abort,
300 ehci_device_ctrl_close,
302 ehci_device_ctrl_done,
305 Static struct usbd_pipe_methods ehci_device_intr_methods = {
306 ehci_device_intr_transfer,
307 ehci_device_intr_start,
308 ehci_device_intr_abort,
309 ehci_device_intr_close,
310 ehci_device_clear_toggle,
311 ehci_device_intr_done,
314 Static struct usbd_pipe_methods ehci_device_bulk_methods = {
315 ehci_device_bulk_transfer,
316 ehci_device_bulk_start,
317 ehci_device_bulk_abort,
318 ehci_device_bulk_close,
319 ehci_device_clear_toggle,
320 ehci_device_bulk_done,
323 Static struct usbd_pipe_methods ehci_device_isoc_methods = {
324 ehci_device_isoc_transfer,
325 ehci_device_isoc_start,
326 ehci_device_isoc_abort,
327 ehci_device_isoc_close,
329 ehci_device_isoc_done,
333 ehci_init(ehci_softc_t *sc)
335 u_int32_t version, sparams, cparams, hcr;
340 DPRINTF(("ehci_init: start\n"));
345 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
347 version = EREAD2(sc, EHCI_HCIVERSION);
348 printf("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev),
349 version >> 8, version & 0xff);
351 sparams = EREAD4(sc, EHCI_HCSPARAMS);
352 DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
353 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
354 if (EHCI_HCS_N_CC(sparams) != sc->sc_ncomp) {
355 printf("%s: wrong number of companions (%d != %d)\n",
356 USBDEVNAME(sc->sc_bus.bdev),
357 EHCI_HCS_N_CC(sparams), sc->sc_ncomp);
358 return (USBD_IOERROR);
360 if (sc->sc_ncomp > 0) {
361 printf("%s: companion controller%s, %d port%s each:",
362 USBDEVNAME(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
363 EHCI_HCS_N_PCC(sparams),
364 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
365 for (i = 0; i < sc->sc_ncomp; i++)
366 printf(" %s", USBDEVNAME(sc->sc_comps[i]->bdev));
369 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
370 cparams = EREAD4(sc, EHCI_HCCPARAMS);
371 DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
373 if (EHCI_HCC_64BIT(cparams)) {
374 /* MUST clear segment register if 64 bit capable. */
375 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
378 sc->sc_bus.usbrev = USBREV_2_0;
380 /* Reset the controller */
381 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
382 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
383 usb_delay_ms(&sc->sc_bus, 1);
384 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
385 for (i = 0; i < 100; i++) {
386 usb_delay_ms(&sc->sc_bus, 1);
387 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
392 printf("%s: reset timeout\n",
393 USBDEVNAME(sc->sc_bus.bdev));
394 return (USBD_IOERROR);
397 /* frame list size at default, read back what we got and use that */
398 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
399 case 0: sc->sc_flsize = 1024*4; break;
400 case 1: sc->sc_flsize = 512*4; break;
401 case 2: sc->sc_flsize = 256*4; break;
402 case 3: return (USBD_IOERROR);
404 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize,
405 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
408 DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize));
410 /* Set up the bus struct. */
411 sc->sc_bus.methods = &ehci_bus_methods;
412 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
414 #if defined(__NetBSD__) || defined(__OpenBSD__)
415 sc->sc_powerhook = powerhook_establish(ehci_power, sc);
416 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
419 sc->sc_eintrs = EHCI_NORMAL_INTRS;
421 /* Allocate dummy QH that starts the async list. */
422 sqh = ehci_alloc_sqh(sc);
429 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
431 htole32(sqh->physaddr | EHCI_LINK_QH);
432 sqh->qh.qh_curqtd = EHCI_NULL;
434 /* Fill the overlay qTD */
435 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
436 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
437 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
445 /* Point to async list */
446 sc->sc_async_head = sqh;
447 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
449 usb_callout_init(sc->sc_tmo_pcd);
451 lockinit(&sc->sc_doorbell_lock, 0, "ehcidb", 0, 0);
453 /* Enable interrupts */
454 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
456 /* Turn on controller */
457 EOWRITE4(sc, EHCI_USBCMD,
458 EHCI_CMD_ITC_8 | /* 8 microframes */
459 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
464 /* Take over port ownership */
465 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
467 for (i = 0; i < 100; i++) {
468 usb_delay_ms(&sc->sc_bus, 1);
469 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
474 printf("%s: run timeout\n", USBDEVNAME(sc->sc_bus.bdev));
475 return (USBD_IOERROR);
478 return (USBD_NORMAL_COMPLETION);
482 ehci_free_sqh(sc, sc->sc_async_head);
485 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
492 ehci_softc_t *sc = v;
494 if (sc == NULL || sc->sc_dying)
497 /* If we get an interrupt while polling, then just ignore it. */
498 if (sc->sc_bus.use_polling) {
500 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
505 return (ehci_intr1(sc));
509 ehci_intr1(ehci_softc_t *sc)
511 u_int32_t intrs, eintrs;
513 DPRINTFN(20,("ehci_intr1: enter\n"));
515 /* In case the interrupt occurs before initialization has completed. */
518 printf("ehci_intr: sc == NULL\n");
523 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
528 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
529 eintrs = intrs & sc->sc_eintrs;
530 DPRINTFN(7, ("ehci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
531 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
536 sc->sc_bus.intr_context++;
537 sc->sc_bus.no_intrs++;
538 if (eintrs & EHCI_STS_IAA) {
539 DPRINTF(("ehci_intr1: door bell\n"));
540 wakeup(&sc->sc_async_head);
541 eintrs &= ~EHCI_STS_IAA;
543 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
544 DPRINTFN(5,("ehci_intr1: %s %s\n",
545 eintrs & EHCI_STS_INT ? "INT" : "",
546 eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
547 usb_schedsoftintr(&sc->sc_bus);
548 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
550 if (eintrs & EHCI_STS_HSE) {
551 printf("%s: unrecoverable error, controller halted\n",
552 USBDEVNAME(sc->sc_bus.bdev));
555 if (eintrs & EHCI_STS_PCD) {
556 ehci_pcd(sc, sc->sc_intrxfer);
558 * Disable PCD interrupt for now, because it will be
559 * on until the port has been reset.
561 ehci_pcd_able(sc, 0);
562 /* Do not allow RHSC interrupts > 1 per second */
563 usb_callout(sc->sc_tmo_pcd, hz, ehci_pcd_enable, sc);
564 eintrs &= ~EHCI_STS_PCD;
567 sc->sc_bus.intr_context--;
570 /* Block unprocessed interrupts. */
571 sc->sc_eintrs &= ~eintrs;
572 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
573 printf("%s: blocking intrs 0x%x\n",
574 USBDEVNAME(sc->sc_bus.bdev), eintrs);
581 ehci_pcd_able(ehci_softc_t *sc, int on)
583 DPRINTFN(4, ("ehci_pcd_able: on=%d\n", on));
585 sc->sc_eintrs |= EHCI_STS_PCD;
587 sc->sc_eintrs &= ~EHCI_STS_PCD;
588 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
592 ehci_pcd_enable(void *v_sc)
594 ehci_softc_t *sc = v_sc;
596 ehci_pcd_able(sc, 1);
600 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
602 usbd_pipe_handle pipe;
607 /* Just ignore the change. */
613 p = KERNADDR(&xfer->dmabuf, 0);
614 m = min(sc->sc_noport, xfer->length * 8 - 1);
615 memset(p, 0, xfer->length);
616 for (i = 1; i <= m; i++) {
617 /* Pick out CHANGE bits from the status reg. */
618 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
619 p[i/8] |= 1 << (i%8);
621 DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
622 xfer->actlen = xfer->length;
623 xfer->status = USBD_NORMAL_COMPLETION;
625 usb_transfer_complete(xfer);
629 ehci_softintr(void *v)
631 ehci_softc_t *sc = v;
632 struct ehci_xfer *ex, *nextex;
634 DPRINTFN(10,("%s: ehci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
635 sc->sc_bus.intr_context));
637 sc->sc_bus.intr_context++;
640 * The only explanation I can think of for why EHCI is as brain dead
641 * as UHCI interrupt-wise is that Intel was involved in both.
642 * An interrupt just tells us that something is done, we have no
643 * clue what, so we need to scan through all active transfers. :-(
645 for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
646 nextex = LIST_NEXT(ex, inext);
647 ehci_check_intr(sc, ex);
650 #ifdef USB_USE_SOFTINTR
651 if (sc->sc_softwake) {
653 wakeup(&sc->sc_softwake);
655 #endif /* USB_USE_SOFTINTR */
657 sc->sc_bus.intr_context--;
660 /* Check for an interrupt. */
662 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
664 ehci_soft_qtd_t *sqtd, *lsqtd;
667 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
669 if (ex->sqtdstart == NULL) {
670 printf("ehci_check_intr: sqtdstart=NULL\n");
676 printf("ehci_check_intr: sqtd==0\n");
681 * If the last TD is still active we need to check whether there
682 * is a an error somewhere in the middle, or whether there was a
683 * short packet (SPD and not ACTIVE).
685 if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
686 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
687 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
688 status = le32toh(sqtd->qtd.qtd_status);
689 /* If there's an active QTD the xfer isn't done. */
690 if (status & EHCI_QTD_ACTIVE)
692 /* Any kind of error makes the xfer done. */
693 if (status & EHCI_QTD_HALTED)
695 /* We want short packets, and it is short: it's done */
696 if (EHCI_QTD_GET_BYTES(status) != 0)
699 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
704 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
705 usb_uncallout(ex->xfer.timeout_handle, ehci_timeout, ex);
710 ehci_idone(struct ehci_xfer *ex)
712 usbd_xfer_handle xfer = &ex->xfer;
713 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
714 ehci_soft_qtd_t *sqtd;
715 u_int32_t status = 0, nstatus;
718 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
725 printf("ehci_idone: ex is done!\n ");
728 printf("ehci_idone: ex=%p is done!\n", ex);
737 if (xfer->status == USBD_CANCELLED ||
738 xfer->status == USBD_TIMEOUT) {
739 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
744 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
746 ehci_dump_sqtds(ex->sqtdstart);
749 /* The transfer is done, compute actual length and status. */
751 for (sqtd = ex->sqtdstart; sqtd != NULL; sqtd = sqtd->nextqtd) {
752 nstatus = le32toh(sqtd->qtd.qtd_status);
753 if (nstatus & EHCI_QTD_ACTIVE)
757 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
758 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
761 /* If there are left over TDs we need to update the toggle. */
763 printf("ehci_idone: need toggle update status=%08x nstatus=%08x\n", status, nstatus);
765 ehci_dump_sqh(epipe->sqh);
766 ehci_dump_sqtds(ex->sqtdstart);
768 epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
771 status &= EHCI_QTD_STATERRS;
772 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, status=0x%x\n",
773 xfer->length, actlen, status));
774 xfer->actlen = actlen;
779 bitmask_snprintf((u_int32_t)status,
780 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR"
781 "\3MISSED", sbuf, sizeof(sbuf));
783 DPRINTFN((status == EHCI_QTD_HALTED)*/*10*/2,
784 ("ehci_idone: error, addr=%d, endpt=0x%02x, "
786 xfer->pipe->device->address,
787 xfer->pipe->endpoint->edesc->bEndpointAddress,
790 ehci_dump_sqh(epipe->sqh);
791 ehci_dump_sqtds(ex->sqtdstart);
794 if (status == EHCI_QTD_HALTED)
795 xfer->status = USBD_STALLED;
797 xfer->status = USBD_IOERROR; /* more info XXX */
799 xfer->status = USBD_NORMAL_COMPLETION;
802 usb_transfer_complete(xfer);
803 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
807 * Wait here until controller claims to have an interrupt.
808 * Then call ehci_intr and return. Use timeout to avoid waiting
812 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
814 int timo = xfer->timeout;
818 xfer->status = USBD_IN_PROGRESS;
819 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
820 usb_delay_ms(&sc->sc_bus, 1);
823 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
825 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
832 if (xfer->status != USBD_IN_PROGRESS)
838 DPRINTF(("ehci_waitintr: timeout\n"));
839 xfer->status = USBD_TIMEOUT;
840 usb_transfer_complete(xfer);
841 /* XXX should free TD */
845 ehci_poll(struct usbd_bus *bus)
847 ehci_softc_t *sc = (ehci_softc_t *)bus;
851 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
853 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
858 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
862 #if defined(__NetBSD__) || defined(__OpenBSD__)
864 ehci_detach(struct ehci_softc *sc, int flags)
868 if (sc->sc_child != NULL)
869 rv = config_detach(sc->sc_child, flags);
874 usb_uncallout(sc->sc_tmo_pcd, ehci_pcd_enable, sc);
876 if (sc->sc_powerhook != NULL)
877 powerhook_disestablish(sc->sc_powerhook);
878 if (sc->sc_shutdownhook != NULL)
879 shutdownhook_disestablish(sc->sc_shutdownhook);
881 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
883 /* XXX free other data structures XXX */
889 #if defined(__NetBSD__) || defined(__OpenBSD__)
891 ehci_activate(device_ptr_t self, enum devact act)
893 struct ehci_softc *sc = (struct ehci_softc *)self;
900 case DVACT_DEACTIVATE:
901 if (sc->sc_child != NULL)
902 rv = config_deactivate(sc->sc_child);
911 * Handle suspend/resume.
913 * We need to switch to polling mode here, because this routine is
914 * called from an intterupt context. This is all right since we
915 * are almost suspended anyway.
917 #if defined(__NetBSD__) || defined(__OpenBSD__)
919 ehci_power(int why, void *v)
921 ehci_softc_t *sc = v;
926 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
934 sc->sc_bus.use_polling++;
937 ctl = OREAD4(sc, EHCI_CONTROL) & ~EHCI_HCFS_MASK;
938 if (sc->sc_control == 0) {
940 * Preserve register values, in case that APM BIOS
941 * does not recover them.
943 sc->sc_control = ctl;
944 sc->sc_intre = OREAD4(sc, EHCI_INTERRUPT_ENABLE);
946 ctl |= EHCI_HCFS_SUSPEND;
947 OWRITE4(sc, EHCI_CONTROL, ctl);
949 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
950 sc->sc_bus.use_polling--;
953 sc->sc_bus.use_polling++;
956 /* Some broken BIOSes do not recover these values */
957 OWRITE4(sc, EHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
958 OWRITE4(sc, EHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
959 OWRITE4(sc, EHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
961 OWRITE4(sc, EHCI_INTERRUPT_ENABLE,
962 sc->sc_intre & (EHCI_ALL_INTRS | EHCI_MIE));
964 ctl = sc->sc_control;
966 ctl = OREAD4(sc, EHCI_CONTROL);
967 ctl |= EHCI_HCFS_RESUME;
968 OWRITE4(sc, EHCI_CONTROL, ctl);
969 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
970 ctl = (ctl & ~EHCI_HCFS_MASK) | EHCI_HCFS_OPERATIONAL;
971 OWRITE4(sc, EHCI_CONTROL, ctl);
972 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
973 sc->sc_control = sc->sc_intre = 0;
975 sc->sc_bus.use_polling--;
977 case PWR_SOFTSUSPEND:
978 case PWR_SOFTSTANDBY:
987 * Shut down the controller when the system is going down.
989 #if defined(__NetBSD__) || defined(__OpenBSD__)
991 ehci_shutdown(void *v)
993 ehci_softc_t *sc = v;
995 DPRINTF(("ehci_shutdown: stopping the HC\n"));
996 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
997 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1002 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
1006 err = usb_allocmem(bus, size, 0, dma);
1009 printf("ehci_allocm: usb_allocmem()=%d\n", err);
1015 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
1017 usb_freemem(bus, dma);
1021 ehci_allocx(struct usbd_bus *bus)
1023 struct ehci_softc *sc = (struct ehci_softc *)bus;
1024 usbd_xfer_handle xfer;
1026 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
1028 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
1030 if (xfer->busy_free != XFER_FREE) {
1031 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
1036 xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_INTWAIT);
1039 memset(xfer, 0, sizeof (struct ehci_xfer));
1041 EXFER(xfer)->isdone = 1;
1042 xfer->busy_free = XFER_BUSY;
1049 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1051 struct ehci_softc *sc = (struct ehci_softc *)bus;
1054 if (xfer->busy_free != XFER_BUSY) {
1055 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
1059 xfer->busy_free = XFER_FREE;
1060 if (!EXFER(xfer)->isdone) {
1061 printf("ehci_freex: !isdone\n");
1065 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
1069 ehci_device_clear_toggle(usbd_pipe_handle pipe)
1071 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1073 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
1074 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
1077 usbd_dump_pipe(pipe);
1079 epipe->nexttoggle = 0;
1083 ehci_noop(usbd_pipe_handle pipe)
1089 ehci_dump_regs(ehci_softc_t *sc)
1092 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1093 EOREAD4(sc, EHCI_USBCMD),
1094 EOREAD4(sc, EHCI_USBSTS),
1095 EOREAD4(sc, EHCI_USBINTR));
1096 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1097 EOREAD4(sc, EHCI_FRINDEX),
1098 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1099 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1100 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1101 for (i = 1; i <= sc->sc_noport; i++)
1102 printf("port %d status=0x%08x\n", i,
1103 EOREAD4(sc, EHCI_PORTSC(i)));
1107 * Unused function - this is meant to be called from a kernel
1113 ehci_dump_regs(theehci);
1117 ehci_dump_link(ehci_link_t link, int type)
1119 link = le32toh(link);
1120 printf("0x%08x", link);
1121 if (link & EHCI_LINK_TERMINATE)
1126 switch (EHCI_LINK_TYPE(link)) {
1127 case EHCI_LINK_ITD: printf("ITD"); break;
1128 case EHCI_LINK_QH: printf("QH"); break;
1129 case EHCI_LINK_SITD: printf("SITD"); break;
1130 case EHCI_LINK_FSTN: printf("FSTN"); break;
1138 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1144 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1145 ehci_dump_sqtd(sqtd);
1146 stop = sqtd->qtd.qtd_next & EHCI_LINK_TERMINATE;
1149 printf("dump aborted, too many TDs\n");
1153 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1155 printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr);
1156 ehci_dump_qtd(&sqtd->qtd);
1160 ehci_dump_qtd(ehci_qtd_t *qtd)
1165 printf(" next="); ehci_dump_link(qtd->qtd_next, 0);
1166 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
1168 s = le32toh(qtd->qtd_status);
1169 bitmask_snprintf(EHCI_QTD_GET_STATUS(s),
1170 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1171 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
1172 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
1173 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
1174 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
1175 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
1176 EHCI_QTD_GET_PID(s), sbuf);
1177 for (s = 0; s < 5; s++)
1178 printf(" buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
1182 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1184 ehci_qh_t *qh = &sqh->qh;
1185 u_int32_t endp, endphub;
1187 printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr);
1188 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
1189 endp = le32toh(qh->qh_endp);
1190 printf(" endp=0x%08x\n", endp);
1191 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
1192 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1193 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
1194 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
1195 printf(" mpl=0x%x ctl=%d nrl=%d\n",
1196 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
1197 EHCI_QH_GET_NRL(endp));
1198 endphub = le32toh(qh->qh_endphub);
1199 printf(" endphub=0x%08x\n", endphub);
1200 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
1201 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
1202 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1203 EHCI_QH_GET_MULT(endphub));
1204 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
1205 printf("Overlay qTD:\n");
1206 ehci_dump_qtd(&qh->qh_qtd);
1211 ehci_dump_exfer(struct ehci_xfer *ex)
1213 printf("ehci_dump_exfer: ex=%p\n", ex);
1219 ehci_open(usbd_pipe_handle pipe)
1221 usbd_device_handle dev = pipe->device;
1222 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
1223 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1224 u_int8_t addr = dev->address;
1225 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1226 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1227 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 at splusb().
1331 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1335 sqh->next = head->next;
1336 sqh->qh.qh_link = head->qh.qh_link;
1338 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
1341 if (ehcidebug > 5) {
1342 printf("ehci_add_qh:\n");
1349 * Remove an ED from the schedule. Called at splusb().
1352 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1358 for (p = head; p != NULL && p->next != sqh; p = p->next)
1361 panic("ehci_rem_qh: ED not found");
1362 p->next = sqh->next;
1363 p->qh.qh_link = sqh->qh.qh_link;
1369 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
1371 /* Halt while we are messing. */
1372 sqh->qh.qh_qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
1373 sqh->qh.qh_curqtd = 0;
1374 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
1377 sqh->qh.qh_qtd.qtd_status &= htole32(~EHCI_QTD_HALTED);
1381 * Ensure that the HC has released all references to the QH. We do this
1382 * by asking for a Async Advance Doorbell interrupt and then we wait for
1384 * To make this easier we first obtain exclusive use of the doorbell.
1387 ehci_sync_hc(ehci_softc_t *sc)
1392 DPRINTFN(2,("ehci_sync_hc: dying\n"));
1395 DPRINTFN(2,("ehci_sync_hc: enter\n"));
1397 lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE, NULL, NULL);
1399 /* ask for doorbell */
1400 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
1401 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1402 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1403 error = tsleep(&sc->sc_async_head, 0, "ehcidi", hz); /* bell wait */
1404 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1405 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1407 /* release doorbell */
1408 lockmgr(&sc->sc_doorbell_lock, LK_RELEASE, NULL, NULL);
1411 printf("ehci_sync_hc: tsleep() = %d\n", error);
1413 DPRINTFN(2,("ehci_sync_hc: exit\n"));
1419 * Data structures and routines to emulate the root hub.
1421 Static usb_device_descriptor_t ehci_devd = {
1422 USB_DEVICE_DESCRIPTOR_SIZE,
1423 UDESC_DEVICE, /* type */
1424 {0x00, 0x02}, /* USB version */
1425 UDCLASS_HUB, /* class */
1426 UDSUBCLASS_HUB, /* subclass */
1427 UDPROTO_HSHUBSTT, /* protocol */
1428 64, /* max packet */
1429 {0},{0},{0x00,0x01}, /* device id */
1430 1,2,0, /* string indicies */
1431 1 /* # of configurations */
1434 Static usb_device_qualifier_t ehci_odevd = {
1435 USB_DEVICE_DESCRIPTOR_SIZE,
1436 UDESC_DEVICE_QUALIFIER, /* type */
1437 {0x00, 0x02}, /* USB version */
1438 UDCLASS_HUB, /* class */
1439 UDSUBCLASS_HUB, /* subclass */
1440 UDPROTO_FSHUB, /* protocol */
1441 64, /* max packet */
1442 1, /* # of configurations */
1446 Static usb_config_descriptor_t ehci_confd = {
1447 USB_CONFIG_DESCRIPTOR_SIZE,
1449 {USB_CONFIG_DESCRIPTOR_SIZE +
1450 USB_INTERFACE_DESCRIPTOR_SIZE +
1451 USB_ENDPOINT_DESCRIPTOR_SIZE},
1459 Static usb_interface_descriptor_t ehci_ifcd = {
1460 USB_INTERFACE_DESCRIPTOR_SIZE,
1471 Static usb_endpoint_descriptor_t ehci_endpd = {
1472 USB_ENDPOINT_DESCRIPTOR_SIZE,
1474 UE_DIR_IN | EHCI_INTR_ENDPT,
1476 {8, 0}, /* max packet */
1480 Static usb_hub_descriptor_t ehci_hubd = {
1481 USB_HUB_DESCRIPTOR_SIZE,
1492 usb_string_descriptor_t *p;
1500 p->bLength = 2 * strlen(s) + 2;
1503 p->bDescriptorType = UDESC_STRING;
1505 for (i = 0; s[i] && l > 1; i++, l -= 2)
1506 USETW2(p->bString[i], 0, s[i]);
1511 * Simulate a hardware hub by handling all the necessary requests.
1514 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
1518 /* Insert last in queue. */
1519 err = usb_insert_transfer(xfer);
1523 /* Pipe isn't running, start first */
1524 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1528 ehci_root_ctrl_start(usbd_xfer_handle xfer)
1530 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
1531 usb_device_request_t *req;
1534 int s, len, value, index, l, totlen = 0;
1535 usb_port_status_t ps;
1536 usb_hub_descriptor_t hubd;
1541 return (USBD_IOERROR);
1544 if (!(xfer->rqflags & URQ_REQUEST))
1546 return (USBD_INVAL);
1548 req = &xfer->request;
1550 DPRINTFN(4,("ehci_root_ctrl_control type=0x%02x request=%02x\n",
1551 req->bmRequestType, req->bRequest));
1553 len = UGETW(req->wLength);
1554 value = UGETW(req->wValue);
1555 index = UGETW(req->wIndex);
1558 buf = KERNADDR(&xfer->dmabuf, 0);
1560 #define C(x,y) ((x) | ((y) << 8))
1561 switch(C(req->bRequest, req->bmRequestType)) {
1562 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1563 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1564 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1566 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1567 * for the integrated root hub.
1570 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1572 *(u_int8_t *)buf = sc->sc_conf;
1576 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1577 DPRINTFN(8,("ehci_root_ctrl_control wValue=0x%04x\n", value));
1578 switch(value >> 8) {
1580 if ((value & 0xff) != 0) {
1584 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1585 USETW(ehci_devd.idVendor, sc->sc_id_vendor);
1586 memcpy(buf, &ehci_devd, l);
1589 * We can't really operate at another speed, but the spec says
1590 * we need this descriptor.
1592 case UDESC_DEVICE_QUALIFIER:
1593 if ((value & 0xff) != 0) {
1597 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1598 memcpy(buf, &ehci_odevd, l);
1601 * We can't really operate at another speed, but the spec says
1602 * we need this descriptor.
1604 case UDESC_OTHER_SPEED_CONFIGURATION:
1606 if ((value & 0xff) != 0) {
1610 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
1611 memcpy(buf, &ehci_confd, l);
1612 ((usb_config_descriptor_t *)buf)->bDescriptorType =
1614 buf = (char *)buf + l;
1616 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
1618 memcpy(buf, &ehci_ifcd, l);
1619 buf = (char *)buf + l;
1621 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
1623 memcpy(buf, &ehci_endpd, l);
1628 *(u_int8_t *)buf = 0;
1630 switch (value & 0xff) {
1631 case 1: /* Vendor */
1632 totlen = ehci_str(buf, len, sc->sc_vendor);
1634 case 2: /* Product */
1635 totlen = ehci_str(buf, len, "EHCI root hub");
1644 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
1646 *(u_int8_t *)buf = 0;
1650 case C(UR_GET_STATUS, UT_READ_DEVICE):
1652 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
1656 case C(UR_GET_STATUS, UT_READ_INTERFACE):
1657 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
1659 USETW(((usb_status_t *)buf)->wStatus, 0);
1663 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
1664 if (value >= USB_MAX_DEVICES) {
1668 sc->sc_addr = value;
1670 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
1671 if (value != 0 && value != 1) {
1675 sc->sc_conf = value;
1677 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
1679 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
1680 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
1681 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
1684 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
1686 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
1689 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
1691 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
1692 DPRINTFN(8, ("ehci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
1693 "port=%d feature=%d\n",
1695 if (index < 1 || index > sc->sc_noport) {
1699 port = EHCI_PORTSC(index);
1700 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1702 case UHF_PORT_ENABLE:
1703 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
1705 case UHF_PORT_SUSPEND:
1706 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
1708 case UHF_PORT_POWER:
1709 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
1712 DPRINTFN(2,("ehci_root_ctrl_transfer: clear port test "
1715 case UHF_PORT_INDICATOR:
1716 DPRINTFN(2,("ehci_root_ctrl_transfer: clear port ind "
1718 EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
1720 case UHF_C_PORT_CONNECTION:
1721 EOWRITE4(sc, port, v | EHCI_PS_CSC);
1723 case UHF_C_PORT_ENABLE:
1724 EOWRITE4(sc, port, v | EHCI_PS_PEC);
1726 case UHF_C_PORT_SUSPEND:
1729 case UHF_C_PORT_OVER_CURRENT:
1730 EOWRITE4(sc, port, v | EHCI_PS_OCC);
1732 case UHF_C_PORT_RESET:
1741 case UHF_C_PORT_CONNECTION:
1742 case UHF_C_PORT_ENABLE:
1743 case UHF_C_PORT_SUSPEND:
1744 case UHF_C_PORT_OVER_CURRENT:
1745 case UHF_C_PORT_RESET:
1746 /* Enable RHSC interrupt if condition is cleared. */
1747 if ((OREAD4(sc, port) >> 16) == 0)
1748 ehci_pcd_able(sc, 1);
1755 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
1756 if ((value & 0xff) != 0) {
1761 hubd.bNbrPorts = sc->sc_noport;
1762 v = EOREAD4(sc, EHCI_HCSPARAMS);
1763 USETW(hubd.wHubCharacteristics,
1764 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
1765 EHCI_HCS_P_INCICATOR(EREAD4(sc, EHCI_HCSPARAMS))
1766 ? UHD_PORT_IND : 0);
1767 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
1768 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
1769 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
1770 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
1771 l = min(len, hubd.bDescLength);
1773 memcpy(buf, &hubd, l);
1775 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
1780 memset(buf, 0, len); /* ? XXX */
1783 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
1784 DPRINTFN(8,("ehci_root_ctrl_transfer: get port status i=%d\n",
1786 if (index < 1 || index > sc->sc_noport) {
1794 v = EOREAD4(sc, EHCI_PORTSC(index));
1795 DPRINTFN(8,("ehci_root_ctrl_transfer: port status=0x%04x\n",
1798 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
1799 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
1800 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
1801 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
1802 if (v & EHCI_PS_PR) i |= UPS_RESET;
1803 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
1804 USETW(ps.wPortStatus, i);
1806 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
1807 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
1808 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
1809 if (sc->sc_isreset) i |= UPS_C_PORT_RESET;
1810 USETW(ps.wPortChange, i);
1811 l = min(len, sizeof ps);
1812 memcpy(buf, &ps, l);
1815 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
1818 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
1820 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
1821 if (index < 1 || index > sc->sc_noport) {
1825 port = EHCI_PORTSC(index);
1826 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1828 case UHF_PORT_ENABLE:
1829 EOWRITE4(sc, port, v | EHCI_PS_PE);
1831 case UHF_PORT_SUSPEND:
1832 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
1834 case UHF_PORT_RESET:
1835 DPRINTFN(5,("ehci_root_ctrl_transfer: reset port %d\n",
1837 if (EHCI_PS_IS_LOWSPEED(v)) {
1838 /* Low speed device, give up ownership. */
1839 ehci_disown(sc, index, 1);
1842 /* Start reset sequence. */
1843 v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
1844 EOWRITE4(sc, port, v | EHCI_PS_PR);
1845 /* Wait for reset to complete. */
1846 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
1851 /* Terminate reset sequence. */
1852 EOWRITE4(sc, port, v);
1853 /* Wait for HC to complete reset. */
1854 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE);
1859 v = EOREAD4(sc, port);
1860 DPRINTF(("ehci after reset, status=0x%08x\n", v));
1861 if (v & EHCI_PS_PR) {
1862 printf("%s: port reset timeout\n",
1863 USBDEVNAME(sc->sc_bus.bdev));
1864 return (USBD_TIMEOUT);
1866 if (!(v & EHCI_PS_PE)) {
1867 /* Not a high speed device, give up ownership.*/
1868 ehci_disown(sc, index, 0);
1872 DPRINTF(("ehci port %d reset, status = 0x%08x\n",
1875 case UHF_PORT_POWER:
1876 DPRINTFN(2,("ehci_root_ctrl_transfer: set port power "
1878 EOWRITE4(sc, port, v | EHCI_PS_PP);
1881 DPRINTFN(2,("ehci_root_ctrl_transfer: set port test "
1884 case UHF_PORT_INDICATOR:
1885 DPRINTFN(2,("ehci_root_ctrl_transfer: set port ind "
1887 EOWRITE4(sc, port, v | EHCI_PS_PIC);
1894 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
1895 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
1896 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
1897 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
1903 xfer->actlen = totlen;
1904 err = USBD_NORMAL_COMPLETION;
1908 usb_transfer_complete(xfer);
1910 return (USBD_IN_PROGRESS);
1914 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
1919 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
1921 if (sc->sc_npcomp != 0) {
1922 int i = (index-1) / sc->sc_npcomp;
1923 if (i >= sc->sc_ncomp)
1924 printf("%s: strange port\n",
1925 USBDEVNAME(sc->sc_bus.bdev));
1927 printf("%s: handing over %s speed device on "
1929 USBDEVNAME(sc->sc_bus.bdev),
1930 lowspeed ? "low" : "full",
1931 index, USBDEVNAME(sc->sc_comps[i]->bdev));
1933 printf("%s: npcomp == 0\n", USBDEVNAME(sc->sc_bus.bdev));
1936 port = EHCI_PORTSC(index);
1937 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1938 EOWRITE4(sc, port, v | EHCI_PS_PO);
1941 /* Abort a root control request. */
1943 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
1945 /* Nothing to do, all transfers are synchronous. */
1948 /* Close the root pipe. */
1950 ehci_root_ctrl_close(usbd_pipe_handle pipe)
1952 DPRINTF(("ehci_root_ctrl_close\n"));
1953 /* Nothing to do. */
1957 ehci_root_intr_done(usbd_xfer_handle xfer)
1959 xfer->hcpriv = NULL;
1963 ehci_root_intr_transfer(usbd_xfer_handle xfer)
1967 /* Insert last in queue. */
1968 err = usb_insert_transfer(xfer);
1972 /* Pipe isn't running, start first */
1973 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1977 ehci_root_intr_start(usbd_xfer_handle xfer)
1979 usbd_pipe_handle pipe = xfer->pipe;
1980 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
1983 return (USBD_IOERROR);
1985 sc->sc_intrxfer = xfer;
1987 return (USBD_IN_PROGRESS);
1990 /* Abort a root interrupt request. */
1992 ehci_root_intr_abort(usbd_xfer_handle xfer)
1996 if (xfer->pipe->intrxfer == xfer) {
1997 DPRINTF(("ehci_root_intr_abort: remove\n"));
1998 xfer->pipe->intrxfer = NULL;
2000 xfer->status = USBD_CANCELLED;
2002 usb_transfer_complete(xfer);
2006 /* Close the root pipe. */
2008 ehci_root_intr_close(usbd_pipe_handle pipe)
2010 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2012 DPRINTF(("ehci_root_intr_close\n"));
2014 sc->sc_intrxfer = NULL;
2018 ehci_root_ctrl_done(usbd_xfer_handle xfer)
2020 xfer->hcpriv = NULL;
2023 /************************/
2026 ehci_alloc_sqh(ehci_softc_t *sc)
2028 ehci_soft_qh_t *sqh;
2033 if (sc->sc_freeqhs == NULL) {
2034 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
2035 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
2036 EHCI_PAGE_SIZE, &dma);
2039 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2043 for(i = 0; i < EHCI_SQH_CHUNK; i++) {
2044 offs = i * EHCI_SQH_SIZE;
2045 sqh = KERNADDR(&dma, offs);
2046 sqh->physaddr = DMAADDR(&dma, offs);
2047 sqh->next = sc->sc_freeqhs;
2048 sc->sc_freeqhs = sqh;
2051 sqh = sc->sc_freeqhs;
2052 sc->sc_freeqhs = sqh->next;
2053 memset(&sqh->qh, 0, sizeof(ehci_qh_t));
2059 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2061 sqh->next = sc->sc_freeqhs;
2062 sc->sc_freeqhs = sqh;
2066 ehci_alloc_sqtd(ehci_softc_t *sc)
2068 ehci_soft_qtd_t *sqtd;
2074 if (sc->sc_freeqtds == NULL) {
2075 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
2076 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
2077 EHCI_PAGE_SIZE, &dma);
2080 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
2085 for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
2086 offs = i * EHCI_SQTD_SIZE;
2087 sqtd = KERNADDR(&dma, offs);
2088 sqtd->physaddr = DMAADDR(&dma, offs);
2089 sqtd->nextqtd = sc->sc_freeqtds;
2090 sc->sc_freeqtds = sqtd;
2096 sqtd = sc->sc_freeqtds;
2097 sc->sc_freeqtds = sqtd->nextqtd;
2098 memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
2099 sqtd->nextqtd = NULL;
2107 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2112 sqtd->nextqtd = sc->sc_freeqtds;
2113 sc->sc_freeqtds = sqtd;
2118 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
2119 int alen, int rd, usbd_xfer_handle xfer,
2120 ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
2122 ehci_soft_qtd_t *next, *cur;
2123 ehci_physaddr_t dataphys, dataphyspage, dataphyslastpage, nextphys;
2124 u_int32_t qtdstatus;
2125 int len, curlen, mps;
2128 usb_dma_t *dma = &xfer->dmabuf;
2130 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
2134 dataphys = DMAADDR(dma, 0);
2135 dataphyslastpage = EHCI_PAGE(DMAADDR(dma, len - 1));
2137 printf("status=%08x toggle=%d\n", epipe->sqh->qh.qh_qtd.qtd_status,
2140 qtdstatus = EHCI_QTD_ACTIVE |
2141 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2142 EHCI_QTD_SET_CERR(3)
2144 /* BYTES set below */
2146 mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
2147 tog = epipe->nexttoggle;
2148 qtdstatus |= EHCI_QTD_SET_TOGGLE(tog);
2150 cur = ehci_alloc_sqtd(sc);
2155 dataphyspage = EHCI_PAGE(dataphys);
2156 /* The EHCI hardware can handle at most 5 pages. */
2157 #if defined(__NetBSD__) || defined(__OpenBSD__)
2158 if (dataphyslastpage - dataphyspage <
2159 EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE) {
2160 /* we can handle it in this QTD */
2162 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2163 /* XXX This is pretty broken: Because we do not allocate
2164 * a contiguous buffer (contiguous in physical pages) we
2165 * can only transfer one page in one go.
2166 * So check whether the start and end of the buffer are on
2169 if (dataphyspage == dataphyslastpage) {
2173 #if defined(__NetBSD__) || defined(__OpenBSD__)
2174 /* must use multiple TDs, fill as much as possible. */
2175 curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE -
2176 EHCI_PAGE_OFFSET(dataphys);
2179 printf("ehci_alloc_sqtd_chain: curlen=0x%x "
2180 "len=0x%x offs=0x%x\n", curlen, len,
2181 EHCI_PAGE_OFFSET(dataphys));
2182 printf("lastpage=0x%x page=0x%x phys=0x%x\n",
2183 dataphyslastpage, dataphyspage,
2188 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2189 /* See comment above (XXX) */
2190 curlen = EHCI_PAGE_SIZE -
2191 EHCI_PAGE_MASK(dataphys);
2193 /* the length must be a multiple of the max size */
2194 curlen -= curlen % mps;
2195 DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, "
2196 "curlen=%d\n", curlen));
2199 panic("ehci_alloc_std: curlen == 0");
2202 DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x "
2203 "dataphyslastpage=0x%08x len=%d curlen=%d\n",
2204 dataphys, dataphyslastpage,
2209 next = ehci_alloc_sqtd(sc);
2212 nextphys = htole32(next->physaddr);
2215 nextphys = EHCI_NULL;
2218 for (i = 0; i * EHCI_PAGE_SIZE < curlen; i++) {
2219 ehci_physaddr_t a = dataphys + i * EHCI_PAGE_SIZE;
2220 if (i != 0) /* use offset only in first buffer */
2222 cur->qtd.qtd_buffer[i] = htole32(a);
2224 if (i >= EHCI_QTD_NBUFFERS) {
2225 printf("ehci_alloc_sqtd_chain: i=%d\n", i);
2230 cur->nextqtd = next;
2231 cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys;
2232 cur->qtd.qtd_status =
2233 htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
2236 DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n",
2237 dataphys, dataphys + curlen));
2238 /* adjust the toggle based on the number of packets in this
2240 if (((curlen + mps - 1) / mps) & 1) {
2242 qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
2246 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
2248 dataphys = DMAADDR(dma, offset);
2251 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
2253 epipe->nexttoggle = tog;
2255 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
2258 return (USBD_NORMAL_COMPLETION);
2261 /* XXX free chain */
2262 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
2263 return (USBD_NOMEM);
2267 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd,
2268 ehci_soft_qtd_t *sqtdend)
2273 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
2276 for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
2278 ehci_free_sqtd(sc, sqtd);
2285 * Close a reqular pipe.
2286 * Assumes that there are no pending transactions.
2289 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
2291 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
2292 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2293 ehci_soft_qh_t *sqh = epipe->sqh;
2297 ehci_rem_qh(sc, sqh, head);
2299 ehci_free_sqh(sc, epipe->sqh);
2303 * Abort a device request.
2304 * If this routine is called at splusb() it guarantees that the request
2305 * will be removed from the hardware scheduling and that the callback
2306 * for it will be called with USBD_CANCELLED status.
2307 * It's impossible to guarantee that the requested transfer will not
2308 * have happened since the hardware runs concurrently.
2309 * If the transaction has already happened we rely on the ordinary
2310 * interrupt processing to process it.
2311 * XXX This is most probably wrong.
2314 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2316 #define exfer EXFER(xfer)
2317 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2318 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2319 ehci_soft_qh_t *sqh = epipe->sqh;
2320 ehci_soft_qtd_t *sqtd;
2321 ehci_physaddr_t cur;
2326 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
2329 /* If we're dying, just do the software part. */
2331 xfer->status = status; /* make software ignore it */
2332 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2333 usb_transfer_complete(xfer);
2338 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
2339 panic("ehci_abort_xfer: not in process context");
2342 * Step 1: Make interrupt routine and hardware ignore xfer.
2345 xfer->status = status; /* make software ignore it */
2346 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2347 qhstatus = sqh->qh.qh_qtd.qtd_status;
2348 sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
2349 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2350 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
2351 if (sqtd == exfer->sqtdend)
2357 * Step 2: Wait until we know hardware has finished any possible
2358 * use of the xfer. Also make sure the soft interrupt routine
2363 #ifdef USB_USE_SOFTINTR
2364 sc->sc_softwake = 1;
2365 #endif /* USB_USE_SOFTINTR */
2366 usb_schedsoftintr(&sc->sc_bus);
2367 #ifdef USB_USE_SOFTINTR
2368 tsleep(&sc->sc_softwake, 0, "ehciab", 0);
2369 #endif /* USB_USE_SOFTINTR */
2373 * Step 3: Remove any vestiges of the xfer from the hardware.
2374 * The complication here is that the hardware may have executed
2375 * beyond the xfer we're trying to abort. So as we're scanning
2376 * the TDs of this xfer we check if the hardware points to
2379 s = splusb(); /* XXX why? */
2380 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
2382 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2383 hit |= cur == sqtd->physaddr;
2384 if (sqtd == exfer->sqtdend)
2387 sqtd = sqtd->nextqtd;
2388 /* Zap curqtd register if hardware pointed inside the xfer. */
2389 if (hit && sqtd != NULL) {
2390 DPRINTFN(1,("ehci_abort_xfer: cur=0x%08x\n", sqtd->physaddr));
2391 sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
2392 sqh->qh.qh_qtd.qtd_status = qhstatus;
2394 DPRINTFN(1,("ehci_abort_xfer: no hit\n"));
2398 * Step 4: Execute callback.
2403 usb_transfer_complete(xfer);
2410 ehci_timeout(void *addr)
2412 struct ehci_xfer *exfer = addr;
2413 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
2414 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2416 DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
2419 usbd_dump_pipe(exfer->xfer.pipe);
2423 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
2427 /* Execute the abort in a process context. */
2428 usb_init_task(&exfer->abort_task, ehci_timeout_task, addr);
2429 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task);
2433 ehci_timeout_task(void *addr)
2435 usbd_xfer_handle xfer = addr;
2438 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
2441 ehci_abort_xfer(xfer, USBD_TIMEOUT);
2445 /************************/
2448 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
2452 /* Insert last in queue. */
2453 err = usb_insert_transfer(xfer);
2457 /* Pipe isn't running, start first */
2458 return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2462 ehci_device_ctrl_start(usbd_xfer_handle xfer)
2464 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2468 return (USBD_IOERROR);
2471 if (!(xfer->rqflags & URQ_REQUEST)) {
2473 printf("ehci_device_ctrl_transfer: not a request\n");
2474 return (USBD_INVAL);
2478 err = ehci_device_request(xfer);
2482 if (sc->sc_bus.use_polling)
2483 ehci_waitintr(sc, xfer);
2484 return (USBD_IN_PROGRESS);
2488 ehci_device_ctrl_done(usbd_xfer_handle xfer)
2490 struct ehci_xfer *ex = EXFER(xfer);
2491 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2492 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
2494 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
2497 if (!(xfer->rqflags & URQ_REQUEST)) {
2498 panic("ehci_ctrl_done: not a request");
2502 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2503 ehci_del_intr_list(ex); /* remove from active list */
2504 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
2507 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
2510 /* Abort a device control request. */
2512 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
2514 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
2515 ehci_abort_xfer(xfer, USBD_CANCELLED);
2518 /* Close a device control pipe. */
2520 ehci_device_ctrl_close(usbd_pipe_handle pipe)
2522 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2523 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
2525 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
2526 ehci_close_pipe(pipe, sc->sc_async_head);
2530 ehci_device_request(usbd_xfer_handle xfer)
2532 #define exfer EXFER(xfer)
2533 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2534 usb_device_request_t *req = &xfer->request;
2535 usbd_device_handle dev = epipe->pipe.device;
2536 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2537 int addr = dev->address;
2538 ehci_soft_qtd_t *setup, *stat, *next;
2539 ehci_soft_qh_t *sqh;
2545 isread = req->bmRequestType & UT_READ;
2546 len = UGETW(req->wLength);
2548 DPRINTFN(3,("ehci_device_control type=0x%02x, request=0x%02x, "
2549 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2550 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2551 UGETW(req->wIndex), len, addr,
2552 epipe->pipe.endpoint->edesc->bEndpointAddress));
2554 setup = ehci_alloc_sqtd(sc);
2555 if (setup == NULL) {
2559 stat = ehci_alloc_sqtd(sc);
2566 epipe->u.ctl.length = len;
2568 /* Update device address and length since they may have changed
2569 during the setup of the control pipe in usbd_new_device(). */
2570 /* XXX This only needs to be done once, but it's too early in open. */
2571 /* XXXX Should not touch ED here! */
2573 (sqh->qh.qh_endp & htole32(~(EHCI_QH_ADDRMASK | EHCI_QH_MPLMASK))) |
2575 EHCI_QH_SET_ADDR(addr) |
2576 EHCI_QH_SET_MPL(UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize))
2579 /* Set up data transaction */
2581 ehci_soft_qtd_t *end;
2583 /* Start toggle at 1. */
2584 epipe->nexttoggle = 1;
2585 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
2589 end->nextqtd = stat;
2591 end->qtd.qtd_altnext = htole32(stat->physaddr);
2596 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
2599 setup->qtd.qtd_status = htole32(
2601 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
2602 EHCI_QTD_SET_CERR(3) |
2603 EHCI_QTD_SET_TOGGLE(0) |
2604 EHCI_QTD_SET_BYTES(sizeof *req)
2606 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
2607 setup->nextqtd = next;
2608 setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
2610 setup->len = sizeof *req;
2612 stat->qtd.qtd_status = htole32(
2614 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
2615 EHCI_QTD_SET_CERR(3) |
2616 EHCI_QTD_SET_TOGGLE(1) |
2619 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
2620 stat->nextqtd = NULL;
2621 stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL;
2626 if (ehcidebug > 5) {
2627 DPRINTF(("ehci_device_request:\n"));
2629 ehci_dump_sqtds(setup);
2633 exfer->sqtdstart = setup;
2634 exfer->sqtdend = stat;
2636 if (!exfer->isdone) {
2637 printf("ehci_device_request: not done, exfer=%p\n", exfer);
2642 /* Insert qTD in QH list. */
2644 ehci_set_qh_qtd(sqh, setup);
2645 if (xfer->timeout && !sc->sc_bus.use_polling) {
2646 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2647 ehci_timeout, xfer);
2649 ehci_add_intr_list(sc, exfer);
2650 xfer->status = USBD_IN_PROGRESS;
2654 if (ehcidebug > 10) {
2655 DPRINTF(("ehci_device_request: status=%x\n",
2656 EOREAD4(sc, EHCI_USBSTS)));
2659 ehci_dump_sqh(sc->sc_async_head);
2661 ehci_dump_sqtds(setup);
2665 return (USBD_NORMAL_COMPLETION);
2668 ehci_free_sqtd(sc, stat);
2670 ehci_free_sqtd(sc, setup);
2672 DPRINTFN(-1,("ehci_device_request: no memory\n"));
2674 usb_transfer_complete(xfer);
2679 /************************/
2682 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
2686 /* Insert last in queue. */
2687 err = usb_insert_transfer(xfer);
2691 /* Pipe isn't running, start first */
2692 return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2696 ehci_device_bulk_start(usbd_xfer_handle xfer)
2698 #define exfer EXFER(xfer)
2699 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2700 usbd_device_handle dev = epipe->pipe.device;
2701 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
2702 ehci_soft_qtd_t *data, *dataend;
2703 ehci_soft_qh_t *sqh;
2705 int len, isread, endpt;
2708 DPRINTFN(2, ("ehci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
2709 xfer, xfer->length, xfer->flags));
2712 return (USBD_IOERROR);
2715 if (xfer->rqflags & URQ_REQUEST)
2716 panic("ehci_device_bulk_transfer: a request");
2720 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
2721 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2724 epipe->u.bulk.length = len;
2726 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
2729 DPRINTFN(-1,("ehci_device_bulk_transfer: no memory\n"));
2731 usb_transfer_complete(xfer);
2736 if (ehcidebug > 5) {
2737 DPRINTF(("ehci_device_bulk_transfer: data(1)\n"));
2739 ehci_dump_sqtds(data);
2743 /* Set up interrupt info. */
2744 exfer->sqtdstart = data;
2745 exfer->sqtdend = dataend;
2747 if (!exfer->isdone) {
2748 printf("ehci_device_bulk_transfer: not done, ex=%p\n", exfer);
2754 ehci_set_qh_qtd(sqh, data);
2755 if (xfer->timeout && !sc->sc_bus.use_polling) {
2756 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2757 ehci_timeout, xfer);
2759 ehci_add_intr_list(sc, exfer);
2760 xfer->status = USBD_IN_PROGRESS;
2764 if (ehcidebug > 10) {
2765 DPRINTF(("ehci_device_bulk_transfer: data(2)\n"));
2767 DPRINTF(("ehci_device_bulk_transfer: data(3)\n"));
2770 printf("async_head:\n");
2771 ehci_dump_sqh(sc->sc_async_head);
2775 ehci_dump_sqtds(data);
2779 if (sc->sc_bus.use_polling)
2780 ehci_waitintr(sc, xfer);
2782 return (USBD_IN_PROGRESS);
2787 ehci_device_bulk_abort(usbd_xfer_handle xfer)
2789 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
2790 ehci_abort_xfer(xfer, USBD_CANCELLED);
2794 * Close a device bulk pipe.
2797 ehci_device_bulk_close(usbd_pipe_handle pipe)
2799 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2801 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
2802 ehci_close_pipe(pipe, sc->sc_async_head);
2806 ehci_device_bulk_done(usbd_xfer_handle xfer)
2808 struct ehci_xfer *ex = EXFER(xfer);
2809 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2810 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
2812 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
2813 xfer, xfer->actlen));
2815 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
2816 ehci_del_intr_list(ex); /* remove from active list */
2817 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
2820 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
2823 /************************/
2825 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
2826 Static usbd_status ehci_device_intr_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
2827 Static void ehci_device_intr_abort(usbd_xfer_handle xfer) { }
2828 Static void ehci_device_intr_close(usbd_pipe_handle pipe) { }
2829 Static void ehci_device_intr_done(usbd_xfer_handle xfer) { }
2831 /************************/
2833 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
2834 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
2835 Static void ehci_device_isoc_abort(usbd_xfer_handle xfer) { }
2836 Static void ehci_device_isoc_close(usbd_pipe_handle pipe) { }
2837 Static void ehci_device_isoc_done(usbd_xfer_handle xfer) { }