1 /* $NetBSD: ohci.c,v 1.138 2003/02/08 03:32:50 ichiro Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/ohci.c,v 1.154.2.4 2006/06/26 00:31:25 iedowse Exp $ */
3 /* $DragonFly: src/sys/bus/usb/ohci.c,v 1.28 2008/08/14 20:55:53 hasso Exp $ */
5 /* Also, already ported:
6 * $NetBSD: ohci.c,v 1.140 2003/05/13 04:42:00 gson Exp $
7 * $NetBSD: ohci.c,v 1.141 2003/09/10 20:08:29 mycroft Exp $
8 * $NetBSD: ohci.c,v 1.142 2003/10/11 03:04:26 toshii Exp $
9 * $NetBSD: ohci.c,v 1.143 2003/10/18 04:50:35 simonb Exp $
10 * $NetBSD: ohci.c,v 1.144 2003/11/23 19:18:06 augustss Exp $
11 * $NetBSD: ohci.c,v 1.145 2003/11/23 19:20:25 augustss Exp $
12 * $NetBSD: ohci.c,v 1.146 2003/12/29 08:17:10 toshii Exp $
13 * $NetBSD: ohci.c,v 1.147 2004/06/22 07:20:35 mycroft Exp $
14 * $NetBSD: ohci.c,v 1.148 2004/06/22 18:27:46 mycroft Exp $
18 * Copyright (c) 1998 The NetBSD Foundation, Inc.
19 * All rights reserved.
21 * This code is derived from software contributed to The NetBSD Foundation
22 * by Lennart Augustsson (lennart@augustsson.net) at
23 * Carlstedt Research & Technology.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
35 * This product includes software developed by the NetBSD
36 * Foundation, Inc. and its contributors.
37 * 4. Neither the name of The NetBSD Foundation nor the names of its
38 * contributors may be used to endorse or promote products derived
39 * from this software without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
42 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
43 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
45 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 * POSSIBILITY OF SUCH DAMAGE.
55 * USB Open Host Controller driver.
57 * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html
58 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/malloc.h>
64 #include <sys/kernel.h>
65 #include <sys/endian.h>
66 #include <sys/module.h>
68 #if defined(DIAGNOSTIC) && defined(__i386__)
69 #include <machine/cpu.h>
72 #include <sys/queue.h>
73 #include <sys/sysctl.h>
74 #include <sys/thread2.h>
76 #include <machine/endian.h>
78 #include <bus/usb/usb.h>
79 #include <bus/usb/usbdi.h>
80 #include <bus/usb/usbdivar.h>
81 #include <bus/usb/usb_mem.h>
82 #include <bus/usb/usb_quirks.h>
84 #include <bus/usb/ohcireg.h>
85 #include <bus/usb/ohcivar.h>
87 #define delay(d) DELAY(d)
90 #define DPRINTF(x) if (ohcidebug) kprintf x
91 #define DPRINTFN(n,x) if (ohcidebug>(n)) kprintf x
93 SYSCTL_NODE(_hw_usb, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci");
94 SYSCTL_INT(_hw_usb_ohci, OID_AUTO, debug, CTLFLAG_RW,
95 &ohcidebug, 0, "ohci debug level");
96 #define bitmask_snprintf(q,f,b,l) ksnprintf((b), (l), "%b", (q), (f))
104 static ohci_soft_ed_t *ohci_alloc_sed(ohci_softc_t *);
105 static void ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
107 static ohci_soft_td_t *ohci_alloc_std(ohci_softc_t *);
108 static void ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
110 static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
111 static void ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
114 static void ohci_free_std_chain(ohci_softc_t *, ohci_soft_td_t *,
117 static usbd_status ohci_alloc_std_chain(struct ohci_pipe *,
118 ohci_softc_t *, int, int, usbd_xfer_handle,
119 ohci_soft_td_t *, ohci_soft_td_t **);
121 static usbd_status ohci_open(usbd_pipe_handle);
122 static void ohci_poll(struct usbd_bus *);
123 static void ohci_softintr(void *);
124 static void ohci_waitintr(ohci_softc_t *, usbd_xfer_handle);
125 static void ohci_add_done(ohci_softc_t *, ohci_physaddr_t);
126 static void ohci_rhsc(ohci_softc_t *, usbd_xfer_handle);
128 static usbd_status ohci_device_request(usbd_xfer_handle xfer);
129 static void ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
130 static void ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
131 static void ohci_hash_add_td(ohci_softc_t *, ohci_soft_td_t *);
132 static void ohci_hash_rem_td(ohci_softc_t *, ohci_soft_td_t *);
133 static ohci_soft_td_t *ohci_hash_find_td(ohci_softc_t *, ohci_physaddr_t);
134 static void ohci_hash_add_itd(ohci_softc_t *, ohci_soft_itd_t *);
135 static void ohci_hash_rem_itd(ohci_softc_t *, ohci_soft_itd_t *);
136 static ohci_soft_itd_t *ohci_hash_find_itd(ohci_softc_t *, ohci_physaddr_t);
138 static usbd_status ohci_setup_isoc(usbd_pipe_handle pipe);
139 static void ohci_device_isoc_enter(usbd_xfer_handle);
141 static usbd_status ohci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
142 static void ohci_freem(struct usbd_bus *, usb_dma_t *);
144 static usbd_xfer_handle ohci_allocx(struct usbd_bus *);
145 static void ohci_freex(struct usbd_bus *, usbd_xfer_handle);
147 static usbd_status ohci_root_ctrl_transfer(usbd_xfer_handle);
148 static usbd_status ohci_root_ctrl_start(usbd_xfer_handle);
149 static void ohci_root_ctrl_abort(usbd_xfer_handle);
150 static void ohci_root_ctrl_close(usbd_pipe_handle);
151 static void ohci_root_ctrl_done(usbd_xfer_handle);
153 static usbd_status ohci_root_intr_transfer(usbd_xfer_handle);
154 static usbd_status ohci_root_intr_start(usbd_xfer_handle);
155 static void ohci_root_intr_abort(usbd_xfer_handle);
156 static void ohci_root_intr_close(usbd_pipe_handle);
157 static void ohci_root_intr_done(usbd_xfer_handle);
159 static usbd_status ohci_device_ctrl_transfer(usbd_xfer_handle);
160 static usbd_status ohci_device_ctrl_start(usbd_xfer_handle);
161 static void ohci_device_ctrl_abort(usbd_xfer_handle);
162 static void ohci_device_ctrl_close(usbd_pipe_handle);
163 static void ohci_device_ctrl_done(usbd_xfer_handle);
165 static usbd_status ohci_device_bulk_transfer(usbd_xfer_handle);
166 static usbd_status ohci_device_bulk_start(usbd_xfer_handle);
167 static void ohci_device_bulk_abort(usbd_xfer_handle);
168 static void ohci_device_bulk_close(usbd_pipe_handle);
169 static void ohci_device_bulk_done(usbd_xfer_handle);
171 static usbd_status ohci_device_intr_transfer(usbd_xfer_handle);
172 static usbd_status ohci_device_intr_start(usbd_xfer_handle);
173 static void ohci_device_intr_abort(usbd_xfer_handle);
174 static void ohci_device_intr_close(usbd_pipe_handle);
175 static void ohci_device_intr_done(usbd_xfer_handle);
177 static usbd_status ohci_device_isoc_transfer(usbd_xfer_handle);
178 static usbd_status ohci_device_isoc_start(usbd_xfer_handle);
179 static void ohci_device_isoc_abort(usbd_xfer_handle);
180 static void ohci_device_isoc_close(usbd_pipe_handle);
181 static void ohci_device_isoc_done(usbd_xfer_handle);
183 static usbd_status ohci_device_setintr(ohci_softc_t *sc,
184 struct ohci_pipe *pipe, int ival);
186 static int ohci_str(usb_string_descriptor_t *, int, const char *);
188 static void ohci_timeout(void *);
189 static void ohci_timeout_task(void *);
190 static void ohci_rhsc_able(ohci_softc_t *, int);
191 static void ohci_rhsc_enable(void *);
193 static void ohci_close_pipe(usbd_pipe_handle, ohci_soft_ed_t *);
194 static void ohci_abort_xfer(usbd_xfer_handle, usbd_status);
196 static void ohci_device_clear_toggle(usbd_pipe_handle pipe);
197 static void ohci_noop(usbd_pipe_handle pipe);
199 static usbd_status ohci_controller_init(ohci_softc_t *sc);
202 static void ohci_dumpregs(ohci_softc_t *);
203 static void ohci_dump_tds(ohci_soft_td_t *);
204 static void ohci_dump_td(ohci_soft_td_t *);
205 static void ohci_dump_ed(ohci_soft_ed_t *);
206 static void ohci_dump_itd(ohci_soft_itd_t *);
207 static void ohci_dump_itds(ohci_soft_itd_t *);
210 #define OBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
211 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
212 #define OWRITE1(sc, r, x) \
213 do { OBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
214 #define OWRITE2(sc, r, x) \
215 do { OBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
216 #define OWRITE4(sc, r, x) \
217 do { OBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
218 #define OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
219 #define OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
220 #define OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
222 /* Reverse the bits in a value 0 .. 31 */
223 static u_int8_t revbits[OHCI_NO_INTRS] =
224 { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
225 0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
226 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
227 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
230 struct usbd_pipe pipe;
235 ohci_soft_itd_t *itd;
237 /* Info needed for different pipe kinds. */
243 ohci_soft_td_t *setup, *data, *stat;
262 #define OHCI_INTR_ENDPT 1
264 static struct usbd_bus_methods ohci_bus_methods = {
274 static struct usbd_pipe_methods ohci_root_ctrl_methods = {
275 ohci_root_ctrl_transfer,
276 ohci_root_ctrl_start,
277 ohci_root_ctrl_abort,
278 ohci_root_ctrl_close,
283 static struct usbd_pipe_methods ohci_root_intr_methods = {
284 ohci_root_intr_transfer,
285 ohci_root_intr_start,
286 ohci_root_intr_abort,
287 ohci_root_intr_close,
292 static struct usbd_pipe_methods ohci_device_ctrl_methods = {
293 ohci_device_ctrl_transfer,
294 ohci_device_ctrl_start,
295 ohci_device_ctrl_abort,
296 ohci_device_ctrl_close,
298 ohci_device_ctrl_done,
301 static struct usbd_pipe_methods ohci_device_intr_methods = {
302 ohci_device_intr_transfer,
303 ohci_device_intr_start,
304 ohci_device_intr_abort,
305 ohci_device_intr_close,
306 ohci_device_clear_toggle,
307 ohci_device_intr_done,
310 static struct usbd_pipe_methods ohci_device_bulk_methods = {
311 ohci_device_bulk_transfer,
312 ohci_device_bulk_start,
313 ohci_device_bulk_abort,
314 ohci_device_bulk_close,
315 ohci_device_clear_toggle,
316 ohci_device_bulk_done,
319 static struct usbd_pipe_methods ohci_device_isoc_methods = {
320 ohci_device_isoc_transfer,
321 ohci_device_isoc_start,
322 ohci_device_isoc_abort,
323 ohci_device_isoc_close,
325 ohci_device_isoc_done,
329 ohci_detach(struct ohci_softc *sc, int flags)
336 callout_stop(&sc->sc_tmo_rhsc);
338 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
339 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
342 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
344 for (i = 0; i < OHCI_NO_EDS; i++) {
345 ohci_free_sed(sc, sc->sc_eds[i]);
346 sc->sc_eds[i] = NULL;
348 ohci_free_sed(sc, sc->sc_isoc_head);
349 sc->sc_isoc_head = NULL;
350 ohci_free_sed(sc, sc->sc_bulk_head);
351 sc->sc_bulk_head = NULL;
352 ohci_free_sed(sc, sc->sc_ctrl_head);
353 sc->sc_ctrl_head = NULL;
354 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
360 ohci_alloc_sed(ohci_softc_t *sc)
367 if (sc->sc_freeeds == NULL) {
368 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
369 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
370 OHCI_ED_ALIGN, &dma);
373 for(i = 0; i < OHCI_SED_CHUNK; i++) {
374 offs = i * OHCI_SED_SIZE;
375 sed = KERNADDR(&dma, offs);
376 sed->physaddr = DMAADDR(&dma, offs);
377 sed->next = sc->sc_freeeds;
378 sc->sc_freeeds = sed;
381 sed = sc->sc_freeeds;
382 sc->sc_freeeds = sed->next;
383 memset(&sed->ed, 0, sizeof(ohci_ed_t));
389 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
391 sed->next = sc->sc_freeeds;
392 sc->sc_freeeds = sed;
396 ohci_alloc_std(ohci_softc_t *sc)
403 if (sc->sc_freetds == NULL) {
404 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
405 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
406 OHCI_TD_ALIGN, &dma);
410 for(i = 0; i < OHCI_STD_CHUNK; i++) {
411 offs = i * OHCI_STD_SIZE;
412 std = KERNADDR(&dma, offs);
413 std->physaddr = DMAADDR(&dma, offs);
414 std->nexttd = sc->sc_freetds;
415 sc->sc_freetds = std;
421 std = sc->sc_freetds;
422 sc->sc_freetds = std->nexttd;
423 memset(&std->td, 0, sizeof(ohci_td_t));
426 ohci_hash_add_td(sc, std);
433 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
436 ohci_hash_rem_td(sc, std);
437 std->nexttd = sc->sc_freetds;
438 sc->sc_freetds = std;
443 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
444 int alen, int rd, usbd_xfer_handle xfer,
445 ohci_soft_td_t *sp, ohci_soft_td_t **ep)
447 ohci_soft_td_t *next, *cur;
448 ohci_physaddr_t dataphys;
452 usb_dma_t *dma = &xfer->dmabuf;
453 u_int16_t flags = xfer->flags;
455 DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%d\n", alen));
461 (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
462 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
463 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_SET_DI(6));
466 next = ohci_alloc_std(sc);
470 dataphys = DMAADDR(dma, offset);
473 * The OHCI hardware can handle at most one 4k crossing.
474 * XXX - currently we only allocate contigous buffers, but
475 * the OHCI spec says: If during the data transfer the buffer
476 * address contained in the HC's working copy of
477 * CurrentBufferPointer crosses a 4K boundary, the upper 20
478 * bits of Buffer End are copied to the working value of
479 * CurrentBufferPointer causing the next buffer address to
480 * be the 0th byte in the same 4K page that contains the
481 * last byte of the buffer (the 4K boundary crossing may
482 * occur within a data packet transfer.)
484 * If/when dma has multiple segments, this will need to
485 * properly handle fragmenting TD's.
487 * Note that if we are gathering data from multiple SMALL
488 * segments, e.g. mbufs, we need to do special gymnastics,
489 * e.g. bounce buffering or data aggregation,
490 * BEFORE WE GET HERE because a bulk USB transfer must
491 * consist of maximally sized packets right up to the end.
492 * A shorter than maximal packet means that it is the end
493 * of the transfer. If the data transfer length is a
494 * multiple of the packet size, then a 0 byte
495 * packet will be the signal of the end of transfer.
496 * Since packets can't cross TDs this means that
497 * each TD except the last one must cover an exact multiple
498 * of the maximal packet length.
500 if (OHCI_PAGE_OFFSET(dataphys) + len <= (2 * OHCI_PAGE_SIZE)) {
501 /* We can handle all that remains in this TD */
504 /* must use multiple TDs, fill as much as possible. */
505 curlen = 2 * OHCI_PAGE_SIZE -
506 OHCI_PAGE_OFFSET(dataphys);
507 /* the length must be a multiple of the max size */
509 UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize);
510 KASSERT((curlen != 0), ("ohci_alloc_std: curlen == 0"));
512 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
513 "len=%d curlen=%d\n",
514 dataphys, len, curlen));
517 cur->td.td_flags = tdflags;
518 cur->td.td_cbp = htole32(dataphys);
520 cur->td.td_nexttd = htole32(next->physaddr);
521 cur->td.td_be = htole32(DMAADDR(dma, offset + curlen - 1));
523 cur->flags = OHCI_ADD_LEN;
525 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
526 dataphys, dataphys + curlen - 1));
530 panic("Length went negative: %d curlen %d dma %p offset %08x", len, curlen, dma, (int)0);
532 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
536 if ((flags & USBD_FORCE_SHORT_XFER) &&
537 alen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
538 /* Force a 0 length transfer at the end. */
542 next = ohci_alloc_std(sc);
546 cur->td.td_flags = tdflags;
547 cur->td.td_cbp = 0; /* indicate 0 length packet */
549 cur->td.td_nexttd = htole32(next->physaddr);
554 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
558 return (USBD_NORMAL_COMPLETION);
567 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
568 ohci_soft_td_t *stdend)
572 for (; std != stdend; std = p) {
574 ohci_free_std(sc, std);
580 ohci_alloc_sitd(ohci_softc_t *sc)
582 ohci_soft_itd_t *sitd;
588 if (sc->sc_freeitds == NULL) {
589 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
591 err = usb_allocmem(&sc->sc_bus,
592 OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
593 OHCI_ITD_ALIGN, &dma);
597 for (i = 0; i < OHCI_SITD_CHUNK; i++) {
598 offs = i * OHCI_SITD_SIZE;
599 sitd = KERNADDR(&dma, offs);
600 sitd->physaddr = DMAADDR(&dma, offs);
601 sitd->nextitd = sc->sc_freeitds;
602 sc->sc_freeitds = sitd;
605 sitd = sc->sc_freeitds;
606 sc->sc_freeitds = sitd->nextitd;
607 memset(&sitd->itd, 0, sizeof(ohci_itd_t));
608 sitd->nextitd = NULL;
610 ohci_hash_add_itd(sc, sitd);
621 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
623 DPRINTFN(10,("ohci_free_sitd: sitd=%p\n", sitd));
627 panic("ohci_free_sitd: sitd=%p not done", sitd);
630 /* Warn double free */
635 ohci_hash_rem_itd(sc, sitd);
636 sitd->nextitd = sc->sc_freeitds;
637 sc->sc_freeitds = sitd;
642 ohci_init(ohci_softc_t *sc)
644 ohci_soft_ed_t *sed, *psed;
649 DPRINTF(("ohci_init: start\n"));
650 rev = OREAD4(sc, OHCI_REVISION);
651 device_printf(sc->sc_bus.bdev, "OHCI version %d.%d%s\n",
652 OHCI_REV_HI(rev), OHCI_REV_LO(rev),
653 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
656 * Make sure all interrupts are disabled before we start messing
659 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, -1);
661 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
662 device_printf(sc->sc_bus.bdev, "unsupported OHCI revision\n");
663 sc->sc_bus.usbrev = USBREV_UNKNOWN;
666 sc->sc_bus.usbrev = USBREV_1_0;
668 for (i = 0; i < OHCI_HASH_SIZE; i++)
669 LIST_INIT(&sc->sc_hash_tds[i]);
670 for (i = 0; i < OHCI_HASH_SIZE; i++)
671 LIST_INIT(&sc->sc_hash_itds[i]);
673 STAILQ_INIT(&sc->sc_free_xfers);
675 /* XXX determine alignment by R/W */
676 /* Allocate the HCCA area. */
677 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
678 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
681 sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0);
682 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
684 sc->sc_eintrs = OHCI_NORMAL_INTRS;
686 /* Allocate dummy ED that starts the control list. */
687 sc->sc_ctrl_head = ohci_alloc_sed(sc);
688 if (sc->sc_ctrl_head == NULL) {
692 sc->sc_ctrl_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
694 /* Allocate dummy ED that starts the bulk list. */
695 sc->sc_bulk_head = ohci_alloc_sed(sc);
696 if (sc->sc_bulk_head == NULL) {
700 sc->sc_bulk_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
702 /* Allocate dummy ED that starts the isochronous list. */
703 sc->sc_isoc_head = ohci_alloc_sed(sc);
704 if (sc->sc_isoc_head == NULL) {
708 sc->sc_isoc_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
710 /* Allocate all the dummy EDs that make up the interrupt tree. */
711 for (i = 0; i < OHCI_NO_EDS; i++) {
712 sed = ohci_alloc_sed(sc);
715 ohci_free_sed(sc, sc->sc_eds[i]);
716 sc->sc_eds[i] = NULL;
721 /* All ED fields are set to 0. */
723 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
725 psed = sc->sc_eds[(i-1) / 2];
727 psed= sc->sc_isoc_head;
729 sed->ed.ed_nexted = htole32(psed->physaddr);
732 * Fill HCCA interrupt table. The bit reversal is to get
733 * the tree set up properly to spread the interrupts.
735 for (i = 0; i < OHCI_NO_INTRS; i++)
736 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
737 htole32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
740 if (ohcidebug > 15) {
741 for (i = 0; i < OHCI_NO_EDS; i++) {
742 kprintf("ed#%d ", i);
743 ohci_dump_ed(sc->sc_eds[i]);
746 ohci_dump_ed(sc->sc_isoc_head);
750 err = ohci_controller_init(sc);
751 if (err != USBD_NORMAL_COMPLETION)
754 /* Set up the bus struct. */
755 sc->sc_bus.methods = &ohci_bus_methods;
756 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
758 callout_init(&sc->sc_tmo_rhsc);
761 * Finish up w/ interlocked done flag (the interrupt handler could
762 * be called due to other shared interrupts), enable interrupts,
763 * and run the handler in case a pending interrupt got cleared
764 * before we finished.
767 sc->sc_flags |= OHCI_SCFLG_DONEINIT;
768 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs);
772 return (USBD_NORMAL_COMPLETION);
775 for (i = 0; i < OHCI_NO_EDS; i++) {
776 ohci_free_sed(sc, sc->sc_eds[i]);
777 sc->sc_eds[i] = NULL;
780 ohci_free_sed(sc, sc->sc_isoc_head);
781 sc->sc_isoc_head = NULL;
783 ohci_free_sed(sc, sc->sc_bulk_head);
784 sc->sc_bulk_head = NULL;
786 ohci_free_sed(sc, sc->sc_ctrl_head);
787 sc->sc_ctrl_head = NULL;
789 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
794 ohci_controller_init(ohci_softc_t *sc)
797 u_int32_t s, ctl, ival, hcr, fm, per, desca;
799 /* Determine in what context we are running. */
800 ctl = OREAD4(sc, OHCI_CONTROL);
802 /* SMM active, request change */
803 DPRINTF(("ohci_init: SMM active, request owner change\n"));
804 s = OREAD4(sc, OHCI_COMMAND_STATUS);
805 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
806 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
807 usb_delay_ms(&sc->sc_bus, 1);
808 ctl = OREAD4(sc, OHCI_CONTROL);
810 if ((ctl & OHCI_IR) == 0) {
811 device_printf(sc->sc_bus.bdev,
812 "SMM does not respond, resetting\n");
813 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
817 /* Don't bother trying to reuse the BIOS init, we'll reset it anyway. */
818 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
819 /* BIOS started controller. */
820 DPRINTF(("ohci_init: BIOS active\n"));
821 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
822 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
823 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
827 DPRINTF(("ohci_init: cold started\n"));
829 /* Controller was cold started. */
830 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
834 * This reset should not be necessary according to the OHCI spec, but
835 * without it some controllers do not start.
837 DPRINTF(("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev)));
838 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
839 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
841 /* We now own the host controller and the bus has been reset. */
842 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
844 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
845 /* Nominal time for a reset is 10 us. */
846 for (i = 0; i < 10; i++) {
848 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
853 device_printf(sc->sc_bus.bdev, "reset timeout\n");
854 return (USBD_IOERROR);
861 /* The controller is now in SUSPEND state, we have 2ms to finish. */
863 /* Set up HC registers. */
864 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
865 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
866 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
867 /* disable all interrupts */
868 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
869 /* switch on desired functional features */
870 ctl = OREAD4(sc, OHCI_CONTROL);
871 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
872 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
873 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
874 /* And finally start it! */
875 OWRITE4(sc, OHCI_CONTROL, ctl);
878 * The controller is now OPERATIONAL. Set a some final
879 * registers that should be set earlier, but that the
880 * controller ignores when in the SUSPEND state.
882 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
883 fm |= OHCI_FSMPS(ival) | ival;
884 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
885 per = OHCI_PERIODIC(ival); /* 90% periodic */
886 OWRITE4(sc, OHCI_PERIODIC_START, per);
888 /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
889 desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
890 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
891 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
892 usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
893 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
896 * The AMD756 requires a delay before re-reading the register,
897 * otherwise it will occasionally report 0 ports.
900 for (i = 0; i < 10 && sc->sc_noport == 0; i++) {
901 usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
902 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
906 * Enable desired interrupts
908 sc->sc_eintrs |= OHCI_MIE;
909 if (sc->sc_flags & OHCI_SCFLG_DONEINIT)
910 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs);
916 return (USBD_NORMAL_COMPLETION);
920 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
922 return (usb_allocmem(bus, size, 0, dma));
926 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
928 usb_freemem(bus, dma);
932 ohci_allocx(struct usbd_bus *bus)
934 struct ohci_softc *sc = (struct ohci_softc *)bus;
935 usbd_xfer_handle xfer;
937 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
939 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
941 if (xfer->busy_free != XFER_FREE) {
942 kprintf("ohci_allocx: xfer=%p not free, 0x%08x\n", xfer,
947 xfer = kmalloc(sizeof(struct ohci_xfer), M_USB, M_INTWAIT);
950 memset(xfer, 0, sizeof (struct ohci_xfer));
951 usb_init_task(&OXFER(xfer)->abort_task, ohci_timeout_task,
953 OXFER(xfer)->ohci_xfer_flags = 0;
955 xfer->busy_free = XFER_BUSY;
962 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
964 struct ohci_softc *sc = (struct ohci_softc *)bus;
965 struct ohci_xfer *oxfer = (struct ohci_xfer *)xfer;
966 ohci_soft_itd_t *sitd;
968 if (oxfer->ohci_xfer_flags & OHCI_ISOC_DIRTY) {
970 for (sitd = xfer->hcpriv; sitd != NULL && sitd->xfer == xfer;
971 sitd = sitd->nextitd)
972 ohci_free_sitd(sc, sitd);
977 if (xfer->busy_free != XFER_BUSY) {
978 kprintf("ohci_freex: xfer=%p not busy, 0x%08x\n", xfer,
982 xfer->busy_free = XFER_FREE;
984 STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
988 * Shut down the controller when the system is going down.
991 ohci_shutdown(void *v)
993 ohci_softc_t *sc = v;
995 DPRINTF(("ohci_shutdown: stopping the HC\n"));
996 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1000 * Handle suspend/resume.
1002 * We need to switch to polling mode here, because this routine is
1003 * called from an intterupt context. This is all right since we
1004 * are almost suspended anyway.
1007 ohci_power(int why, void *v)
1009 ohci_softc_t *sc = v;
1013 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
1018 if (why != PWR_RESUME) {
1019 sc->sc_bus.use_polling++;
1020 ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
1021 if (sc->sc_control == 0) {
1023 * Preserve register values, in case that APM BIOS
1024 * does not recover them.
1026 sc->sc_control = ctl;
1027 sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE);
1029 ctl |= OHCI_HCFS_SUSPEND;
1030 OWRITE4(sc, OHCI_CONTROL, ctl);
1031 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1032 sc->sc_bus.use_polling--;
1034 sc->sc_bus.use_polling++;
1036 /* Some broken BIOSes never initialize Controller chip */
1037 ohci_controller_init(sc);
1040 OWRITE4(sc, OHCI_INTERRUPT_ENABLE,
1041 sc->sc_intre & (OHCI_ALL_INTRS | OHCI_MIE));
1043 ctl = sc->sc_control;
1045 ctl = OREAD4(sc, OHCI_CONTROL);
1046 ctl |= OHCI_HCFS_RESUME;
1047 OWRITE4(sc, OHCI_CONTROL, ctl);
1048 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
1049 ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL;
1050 OWRITE4(sc, OHCI_CONTROL, ctl);
1051 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
1052 sc->sc_control = sc->sc_intre = 0;
1053 sc->sc_bus.use_polling--;
1060 ohci_dumpregs(ohci_softc_t *sc)
1062 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
1063 OREAD4(sc, OHCI_REVISION),
1064 OREAD4(sc, OHCI_CONTROL),
1065 OREAD4(sc, OHCI_COMMAND_STATUS)));
1066 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
1067 OREAD4(sc, OHCI_INTERRUPT_STATUS),
1068 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
1069 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
1070 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
1071 OREAD4(sc, OHCI_HCCA),
1072 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
1073 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
1074 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
1075 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
1076 OREAD4(sc, OHCI_BULK_HEAD_ED),
1077 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
1078 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
1079 OREAD4(sc, OHCI_DONE_HEAD),
1080 OREAD4(sc, OHCI_FM_INTERVAL),
1081 OREAD4(sc, OHCI_FM_REMAINING)));
1082 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
1083 OREAD4(sc, OHCI_FM_NUMBER),
1084 OREAD4(sc, OHCI_PERIODIC_START),
1085 OREAD4(sc, OHCI_LS_THRESHOLD)));
1086 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n",
1087 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
1088 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
1089 OREAD4(sc, OHCI_RH_STATUS)));
1090 DPRINTF((" port1=0x%08x port2=0x%08x\n",
1091 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
1092 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
1093 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n",
1094 le32toh(sc->sc_hcca->hcca_frame_number),
1095 le32toh(sc->sc_hcca->hcca_done_head)));
1099 static int ohci_intr1(ohci_softc_t *);
1104 ohci_softc_t *sc = p;
1106 if (sc->sc_dying || (sc->sc_flags & OHCI_SCFLG_DONEINIT) == 0)
1110 * If we get an interrupt while polling, then remember what it
1111 * was and acknowledge it.
1113 if (sc->sc_bus.use_polling) {
1114 u_int32_t intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1118 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs);
1119 sc->sc_dintrs |= intrs;
1121 DPRINTFN(16, ("ohci_intr: ignored interrupt while polling\n"));
1126 return (ohci_intr1(sc));
1130 ohci_intr1(ohci_softc_t *sc)
1132 u_int32_t intrs, eintrs;
1133 ohci_physaddr_t done;
1135 DPRINTFN(14,("ohci_intr1: enter\n"));
1137 /* In case the interrupt occurs before initialization has completed. */
1138 if (sc->sc_hcca == NULL) {
1140 kprintf("ohci_intr: sc->sc_hcca == NULL\n");
1146 done = le32toh(sc->sc_hcca->hcca_done_head);
1148 /* The LSb of done is used to inform the HC Driver that an interrupt
1149 * condition exists for both the Done list and for another event
1150 * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
1151 * Driver checks the HccaDoneHead Value. If this value is 0, then the
1152 * interrupt was caused by other than the HccaDoneHead update and the
1153 * HcInterruptStatus register needs to be accessed to determine that
1154 * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
1155 * update interrupt is indicated and if the LSb of done is nonzero,
1156 * then an additional interrupt event is indicated and
1157 * HcInterruptStatus should be checked to determine its cause.
1160 if (done & ~OHCI_DONE_INTRS)
1162 if (done & OHCI_DONE_INTRS) {
1163 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1164 intrs |= sc->sc_dintrs;
1165 done &= ~OHCI_DONE_INTRS;
1167 sc->sc_hcca->hcca_done_head = 0;
1169 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1170 intrs |= sc->sc_dintrs;
1174 if (intrs == 0) /* nothing to be done (PCI shared interrupt) */
1178 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1179 sc->sc_dintrs &= ~intrs;
1180 eintrs = intrs & sc->sc_eintrs;
1184 sc->sc_bus.intr_context++;
1185 sc->sc_bus.no_intrs++;
1186 DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
1187 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1190 if (eintrs & OHCI_SO) {
1191 sc->sc_overrun_cnt++;
1192 if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
1193 device_printf(sc->sc_bus.bdev,
1194 "%u scheduling overruns\n", sc->sc_overrun_cnt);
1195 sc->sc_overrun_cnt = 0;
1200 if (eintrs & OHCI_WDH) {
1201 ohci_add_done(sc, done &~ OHCI_DONE_INTRS);
1202 usb_schedsoftintr(&sc->sc_bus);
1203 eintrs &= ~OHCI_WDH;
1205 if (eintrs & OHCI_RD) {
1206 device_printf(sc->sc_bus.bdev, "resume detect\n");
1207 /* XXX process resume detect */
1209 if (eintrs & OHCI_UE) {
1210 device_printf(sc->sc_bus.bdev,
1211 "unrecoverable error, controller halted\n");
1212 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1215 if (eintrs & OHCI_RHSC) {
1216 ohci_rhsc(sc, sc->sc_intrxfer);
1218 * Disable RHSC interrupt for now, because it will be
1219 * on until the port has been reset.
1221 ohci_rhsc_able(sc, 0);
1222 /* Do not allow RHSC interrupts > 1 per second */
1223 callout_reset(&sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);
1224 eintrs &= ~OHCI_RHSC;
1227 sc->sc_bus.intr_context--;
1230 /* Block unprocessed interrupts. XXX */
1231 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
1232 sc->sc_eintrs &= ~eintrs;
1233 device_printf(sc->sc_bus.bdev,
1234 "blocking intrs 0x%x\n", eintrs);
1241 ohci_rhsc_able(ohci_softc_t *sc, int on)
1243 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1245 sc->sc_eintrs |= OHCI_RHSC;
1246 if (sc->sc_flags & OHCI_SCFLG_DONEINIT)
1247 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1249 sc->sc_eintrs &= ~OHCI_RHSC;
1250 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1255 ohci_rhsc_enable(void *v_sc)
1257 ohci_softc_t *sc = v_sc;
1260 ohci_rhsc_able(sc, 1);
1265 char *ohci_cc_strs[] = {
1269 "DATA_TOGGLE_MISMATCH",
1271 "DEVICE_NOT_RESPONDING",
1272 "PID_CHECK_FAILURE",
1286 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done)
1288 ohci_soft_itd_t *sitd, *sidone, **ip;
1289 ohci_soft_td_t *std, *sdone, **p;
1291 /* Reverse the done list. */
1292 for (sdone = NULL, sidone = NULL; done != 0; ) {
1293 std = ohci_hash_find_td(sc, done);
1296 done = le32toh(std->td.td_nexttd);
1298 DPRINTFN(10,("add TD %p\n", std));
1301 sitd = ohci_hash_find_itd(sc, done);
1303 sitd->dnext = sidone;
1304 done = le32toh(sitd->itd.itd_nextitd);
1306 DPRINTFN(5,("add ITD %p\n", sitd));
1309 panic("ohci_add_done: addr 0x%08lx not found", (u_long)done);
1312 /* sdone & sidone now hold the done lists. */
1313 /* Put them on the already processed lists. */
1314 for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext)
1317 for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext)
1323 ohci_softintr(void *v)
1325 ohci_softc_t *sc = v;
1326 ohci_soft_itd_t *sitd, *sidone, *sitdnext;
1327 ohci_soft_td_t *std, *sdone, *stdnext, *p, *n;
1328 usbd_xfer_handle xfer;
1329 struct ohci_pipe *opipe;
1333 DPRINTFN(10,("ohci_softintr: enter\n"));
1335 sc->sc_bus.intr_context++;
1338 sdone = sc->sc_sdone;
1339 sc->sc_sdone = NULL;
1340 sidone = sc->sc_sidone;
1341 sc->sc_sidone = NULL;
1344 DPRINTFN(10,("ohci_softintr: sdone=%p sidone=%p\n", sdone, sidone));
1347 if (ohcidebug > 10) {
1348 DPRINTF(("ohci_process_done: TD done:\n"));
1349 ohci_dump_tds(sdone);
1353 for (std = sdone; std; std = stdnext) {
1355 stdnext = std->dnext;
1356 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1357 std, xfer, (xfer ? xfer->hcpriv : NULL)));
1360 * xfer == NULL: There seems to be no xfer associated
1361 * with this TD. It is tailp that happened to end up on
1366 if (xfer->status == USBD_CANCELLED ||
1367 xfer->status == USBD_TIMEOUT) {
1368 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1370 /* Handled by abort routine. */
1375 if (std->td.td_cbp != 0)
1376 len -= le32toh(std->td.td_be) -
1377 le32toh(std->td.td_cbp) + 1;
1378 DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n", len,
1380 if (std->flags & OHCI_ADD_LEN)
1381 xfer->actlen += len;
1383 cc = OHCI_TD_GET_CC(le32toh(std->td.td_flags));
1384 if (cc != OHCI_CC_NO_ERROR) {
1386 * Endpoint is halted. First unlink all the TDs
1387 * belonging to the failed transfer, and then restart
1390 opipe = (struct ohci_pipe *)xfer->pipe;
1392 DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1393 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1394 ohci_cc_strs[OHCI_TD_GET_CC(le32toh(std->td.td_flags))]));
1395 callout_stop(&xfer->timeout_handle);
1396 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1397 &OXFER(xfer)->abort_task);
1399 /* Remove all this xfer's TDs from the done queue. */
1400 for (p = std; p->dnext != NULL; p = p->dnext) {
1401 if (p->dnext->xfer != xfer)
1403 p->dnext = p->dnext->dnext;
1405 /* The next TD may have been removed. */
1406 stdnext = std->dnext;
1408 /* Remove all TDs belonging to this xfer. */
1409 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1411 ohci_free_std(sc, p);
1415 opipe->sed->ed.ed_headp = htole32(p->physaddr);
1416 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1418 if (cc == OHCI_CC_STALL)
1419 xfer->status = USBD_STALLED;
1421 xfer->status = USBD_IOERROR;
1423 usb_transfer_complete(xfer);
1428 * Skip intermediate TDs. They remain linked from
1429 * xfer->hcpriv and we free them when the transfer completes.
1431 if ((std->flags & OHCI_CALL_DONE) == 0)
1434 /* Normal transfer completion */
1435 callout_stop(&xfer->timeout_handle);
1436 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1437 &OXFER(xfer)->abort_task);
1438 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1440 ohci_free_std(sc, p);
1442 xfer->status = USBD_NORMAL_COMPLETION;
1444 usb_transfer_complete(xfer);
1449 if (ohcidebug > 10) {
1450 DPRINTF(("ohci_softintr: ITD done:\n"));
1451 ohci_dump_itds(sidone);
1455 for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
1457 sitdnext = sitd->dnext;
1458 sitd->flags |= OHCI_ITD_INTFIN;
1459 DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n",
1460 sitd, xfer, xfer ? xfer->hcpriv : 0));
1463 if (xfer->status == USBD_CANCELLED ||
1464 xfer->status == USBD_TIMEOUT) {
1465 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1467 /* Handled by abort routine. */
1471 if (xfer->pipe->aborting)
1472 continue; /*Ignore.*/
1475 kprintf("ohci_softintr: sitd=%p is done\n", sitd);
1478 opipe = (struct ohci_pipe *)xfer->pipe;
1479 if (opipe->aborting)
1482 if (sitd->flags & OHCI_CALL_DONE) {
1483 ohci_soft_itd_t *next;
1485 opipe->u.iso.inuse -= xfer->nframes;
1486 xfer->status = USBD_NORMAL_COMPLETION;
1487 for (i = 0, sitd = xfer->hcpriv;;sitd = next) {
1488 next = sitd->nextitd;
1489 if (OHCI_ITD_GET_CC(sitd->itd.itd_flags) != OHCI_CC_NO_ERROR)
1490 xfer->status = USBD_IOERROR;
1492 if (xfer->status == USBD_NORMAL_COMPLETION) {
1493 iframes = OHCI_ITD_GET_FC(sitd->itd.itd_flags);
1494 for (j = 0; j < iframes; i++, j++) {
1495 len = le16toh(sitd->itd.itd_offset[j]);
1497 (OHCI_ITD_PSW_GET_CC(len) ==
1498 OHCI_CC_NOT_ACCESSED) ? 0 :
1499 OHCI_ITD_PSW_LENGTH(len);
1500 xfer->frlengths[i] = len;
1503 if (sitd->flags & OHCI_CALL_DONE)
1508 usb_transfer_complete(xfer);
1513 #ifdef USB_USE_SOFTINTR
1514 if (sc->sc_softwake) {
1515 sc->sc_softwake = 0;
1516 wakeup(&sc->sc_softwake);
1518 #endif /* USB_USE_SOFTINTR */
1520 sc->sc_bus.intr_context--;
1521 DPRINTFN(10,("ohci_softintr: done:\n"));
1525 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1527 DPRINTFN(10,("ohci_device_ctrl_done: xfer=%p\n", xfer));
1530 if (!(xfer->rqflags & URQ_REQUEST)) {
1531 panic("ohci_device_ctrl_done: not a request");
1534 xfer->hcpriv = NULL;
1538 ohci_device_intr_done(usbd_xfer_handle xfer)
1540 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1541 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1542 ohci_soft_ed_t *sed = opipe->sed;
1543 ohci_soft_td_t *data, *tail;
1546 DPRINTFN(10,("ohci_device_intr_done: xfer=%p, actlen=%d\n",
1547 xfer, xfer->actlen));
1549 xfer->hcpriv = NULL;
1551 if (xfer->pipe->repeat) {
1552 data = opipe->tail.td;
1553 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1555 xfer->status = USBD_NOMEM;
1560 data->td.td_flags = htole32(
1561 OHCI_TD_IN | OHCI_TD_NOCC |
1562 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1563 if (xfer->flags & USBD_SHORT_XFER_OK)
1564 data->td.td_flags |= htole32(OHCI_TD_R);
1565 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
1566 data->nexttd = tail;
1567 data->td.td_nexttd = htole32(tail->physaddr);
1568 data->td.td_be = htole32(le32toh(data->td.td_cbp) +
1570 data->len = xfer->length;
1572 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1573 xfer->hcpriv = data;
1576 sed->ed.ed_tailp = htole32(tail->physaddr);
1577 opipe->tail.td = tail;
1582 ohci_device_bulk_done(usbd_xfer_handle xfer)
1584 DPRINTFN(10,("ohci_device_bulk_done: xfer=%p, actlen=%d\n",
1585 xfer, xfer->actlen));
1587 xfer->hcpriv = NULL;
1591 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1597 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1598 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1599 sc, xfer, hstatus));
1602 /* Just ignore the change. */
1606 p = KERNADDR(&xfer->dmabuf, 0);
1607 m = min(sc->sc_noport, xfer->length * 8 - 1);
1608 memset(p, 0, xfer->length);
1609 for (i = 1; i <= m; i++) {
1610 /* Pick out CHANGE bits from the status reg. */
1611 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1612 p[i/8] |= 1 << (i%8);
1614 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1615 xfer->actlen = xfer->length;
1616 xfer->status = USBD_NORMAL_COMPLETION;
1618 usb_transfer_complete(xfer);
1622 ohci_root_intr_done(usbd_xfer_handle xfer)
1624 xfer->hcpriv = NULL;
1628 ohci_root_ctrl_done(usbd_xfer_handle xfer)
1630 xfer->hcpriv = NULL;
1634 * Wait here until controller claims to have an interrupt.
1635 * Then call ohci_intr and return. Use timeout to avoid waiting
1639 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1641 int timo = xfer->timeout;
1645 xfer->status = USBD_IN_PROGRESS;
1646 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1647 usb_delay_ms(&sc->sc_bus, 1);
1650 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1651 intrs |= sc->sc_dintrs;
1652 intrs &= sc->sc_eintrs;
1653 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1660 if (xfer->status != USBD_IN_PROGRESS)
1666 DPRINTF(("ohci_waitintr: timeout\n"));
1667 xfer->status = USBD_TIMEOUT;
1668 usb_transfer_complete(xfer);
1669 /* XXX should free TD */
1673 ohci_poll(struct usbd_bus *bus)
1675 ohci_softc_t *sc = (ohci_softc_t *)bus;
1680 new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1682 DPRINTFN(10,("ohci_poll: intrs=0x%04x\n", new));
1693 ohci_device_request(usbd_xfer_handle xfer)
1695 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1696 usb_device_request_t *req = &xfer->request;
1697 usbd_device_handle dev = opipe->pipe.device;
1698 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1699 int addr = dev->address;
1700 ohci_soft_td_t *setup, *stat, *next, *tail;
1701 ohci_soft_ed_t *sed;
1706 isread = req->bmRequestType & UT_READ;
1707 len = UGETW(req->wLength);
1709 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1710 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1711 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1712 UGETW(req->wIndex), len, addr,
1713 opipe->pipe.endpoint->edesc->bEndpointAddress));
1715 setup = opipe->tail.td;
1716 stat = ohci_alloc_std(sc);
1721 tail = ohci_alloc_std(sc);
1729 opipe->u.ctl.length = len;
1732 /* Update device address and length since they may have changed
1733 during the setup of the control pipe in usbd_new_device(). */
1734 /* XXX This only needs to be done once, but it's too early in open. */
1735 /* XXXX Should not touch ED here! */
1736 sed->ed.ed_flags = htole32(
1737 (le32toh(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1738 OHCI_ED_SET_FA(addr) |
1739 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1744 /* Set up data transaction */
1746 ohci_soft_td_t *std = stat;
1748 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
1750 stat = stat->nexttd; /* point at free TD */
1753 /* Start toggle at 1 and then use the carried toggle. */
1754 std->td.td_flags &= htole32(~OHCI_TD_TOGGLE_MASK);
1755 std->td.td_flags |= htole32(OHCI_TD_TOGGLE_1);
1758 memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1760 setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1761 OHCI_TD_TOGGLE_0 | OHCI_TD_SET_DI(6));
1762 setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma, 0));
1763 setup->nexttd = next;
1764 setup->td.td_nexttd = htole32(next->physaddr);
1765 setup->td.td_be = htole32(le32toh(setup->td.td_cbp) + sizeof *req - 1);
1769 xfer->hcpriv = setup;
1771 stat->td.td_flags = htole32(
1772 (isread ? OHCI_TD_OUT : OHCI_TD_IN) |
1773 OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1774 stat->td.td_cbp = 0;
1775 stat->nexttd = tail;
1776 stat->td.td_nexttd = htole32(tail->physaddr);
1778 stat->flags = OHCI_CALL_DONE;
1783 if (ohcidebug > 5) {
1784 DPRINTF(("ohci_device_request:\n"));
1786 ohci_dump_tds(setup);
1790 /* Insert ED in schedule */
1792 sed->ed.ed_tailp = htole32(tail->physaddr);
1793 opipe->tail.td = tail;
1794 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1795 if (xfer->timeout && !sc->sc_bus.use_polling) {
1796 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1797 ohci_timeout, xfer);
1802 if (ohcidebug > 20) {
1804 DPRINTF(("ohci_device_request: status=%x\n",
1805 OREAD4(sc, OHCI_COMMAND_STATUS)));
1807 kprintf("ctrl head:\n");
1808 ohci_dump_ed(sc->sc_ctrl_head);
1811 ohci_dump_tds(setup);
1815 return (USBD_NORMAL_COMPLETION);
1818 ohci_free_std(sc, tail);
1820 ohci_free_std(sc, stat);
1826 * Add an ED to the schedule. Called from a critical section.
1829 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1831 DPRINTFN(8,("ohci_add_ed: sed=%p head=%p\n", sed, head));
1833 sed->next = head->next;
1834 sed->ed.ed_nexted = head->ed.ed_nexted;
1836 head->ed.ed_nexted = htole32(sed->physaddr);
1840 * Remove an ED from the schedule. Called from a critical section.
1843 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1849 for (p = head; p != NULL && p->next != sed; p = p->next)
1853 panic("ohci_rem_ed: ED not found");
1857 * p == NULL if ohci is detaching and there are still devices
1858 * using ohci (e.g. usb sticks are still plugged in). But
1859 * the real solution should be correcting ohci_free_sed() or
1863 p->next = sed->next;
1864 p->ed.ed_nexted = sed->ed.ed_nexted;
1870 * When a transfer is completed the TD is added to the done queue by
1871 * the host controller. This queue is the processed by software.
1872 * Unfortunately the queue contains the physical address of the TD
1873 * and we have no simple way to translate this back to a kernel address.
1874 * To make the translation possible (and fast) we use a hash table of
1875 * TDs currently in the schedule. The physical address is used as the
1879 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1881 * Called from a critical section
1884 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1886 int h = HASH(std->physaddr);
1888 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1892 * Called from a critical section
1895 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1897 LIST_REMOVE(std, hnext);
1901 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1904 ohci_soft_td_t *std;
1906 /* if these are present they should be masked out at an earlier
1909 KASSERT((a&~OHCI_HEADMASK) == 0, ("%s: 0x%b has lower bits set\n",
1910 device_get_nameunit(sc->sc_bus.bdev),
1911 (int) a, "\20\1HALT\2TOGGLE"));
1913 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1915 std = LIST_NEXT(std, hnext))
1916 if (std->physaddr == a)
1919 DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1920 device_get_nameunit(sc->sc_bus.bdev), (u_long) a));
1925 * Called from a critical section
1928 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1930 int h = HASH(sitd->physaddr);
1932 DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n",
1933 sitd, (u_long)sitd->physaddr));
1935 LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
1939 * Called from a critical section
1942 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1944 DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n",
1945 sitd, (u_long)sitd->physaddr));
1947 LIST_REMOVE(sitd, hnext);
1951 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
1954 ohci_soft_itd_t *sitd;
1956 for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
1958 sitd = LIST_NEXT(sitd, hnext))
1959 if (sitd->physaddr == a)
1965 ohci_timeout(void *addr)
1967 struct ohci_xfer *oxfer = addr;
1968 struct ohci_pipe *opipe = (struct ohci_pipe *)oxfer->xfer.pipe;
1969 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1971 DPRINTF(("ohci_timeout: oxfer=%p\n", oxfer));
1974 ohci_abort_xfer(&oxfer->xfer, USBD_TIMEOUT);
1978 /* Execute the abort in a process context. */
1979 usb_add_task(oxfer->xfer.pipe->device, &oxfer->abort_task,
1984 ohci_timeout_task(void *addr)
1986 usbd_xfer_handle xfer = addr;
1988 DPRINTF(("ohci_timeout_task: xfer=%p\n", xfer));
1991 ohci_abort_xfer(xfer, USBD_TIMEOUT);
1997 ohci_dump_tds(ohci_soft_td_t *std)
1999 for (; std; std = std->nexttd)
2004 ohci_dump_td(ohci_soft_td_t *std)
2008 bitmask_snprintf((u_int32_t)le32toh(std->td.td_flags),
2009 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
2010 sbuf, sizeof(sbuf));
2012 kprintf("TD(%p) at %08lx: %s delay=%d ec=%d cc=%d\ncbp=0x%08lx "
2013 "nexttd=0x%08lx be=0x%08lx\n",
2014 std, (u_long)std->physaddr, sbuf,
2015 OHCI_TD_GET_DI(le32toh(std->td.td_flags)),
2016 OHCI_TD_GET_EC(le32toh(std->td.td_flags)),
2017 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
2018 (u_long)le32toh(std->td.td_cbp),
2019 (u_long)le32toh(std->td.td_nexttd),
2020 (u_long)le32toh(std->td.td_be));
2024 ohci_dump_itd(ohci_soft_itd_t *sitd)
2028 kprintf("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n"
2029 "bp0=0x%08lx next=0x%08lx be=0x%08lx\n",
2030 sitd, (u_long)sitd->physaddr,
2031 OHCI_ITD_GET_SF(le32toh(sitd->itd.itd_flags)),
2032 OHCI_ITD_GET_DI(le32toh(sitd->itd.itd_flags)),
2033 OHCI_ITD_GET_FC(le32toh(sitd->itd.itd_flags)),
2034 OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags)),
2035 (u_long)le32toh(sitd->itd.itd_bp0),
2036 (u_long)le32toh(sitd->itd.itd_nextitd),
2037 (u_long)le32toh(sitd->itd.itd_be));
2038 for (i = 0; i < OHCI_ITD_NOFFSET; i++)
2039 kprintf("offs[%d]=0x%04x ", i,
2040 (u_int)le16toh(sitd->itd.itd_offset[i]));
2045 ohci_dump_itds(ohci_soft_itd_t *sitd)
2047 for (; sitd; sitd = sitd->nextitd)
2048 ohci_dump_itd(sitd);
2052 ohci_dump_ed(ohci_soft_ed_t *sed)
2054 char sbuf[128], sbuf2[128];
2056 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_flags),
2057 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
2058 sbuf, sizeof(sbuf));
2059 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_headp),
2060 "\20\1HALT\2CARRY", sbuf2, sizeof(sbuf2));
2062 kprintf("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d flags=%s\ntailp=0x%08lx "
2063 "headflags=%s headp=0x%08lx nexted=0x%08lx\n",
2064 sed, (u_long)sed->physaddr,
2065 OHCI_ED_GET_FA(le32toh(sed->ed.ed_flags)),
2066 OHCI_ED_GET_EN(le32toh(sed->ed.ed_flags)),
2067 OHCI_ED_GET_MAXP(le32toh(sed->ed.ed_flags)), sbuf,
2068 (u_long)le32toh(sed->ed.ed_tailp), sbuf2,
2069 (u_long)le32toh(sed->ed.ed_headp),
2070 (u_long)le32toh(sed->ed.ed_nexted));
2075 ohci_open(usbd_pipe_handle pipe)
2077 usbd_device_handle dev = pipe->device;
2078 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2079 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2080 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2081 u_int8_t addr = dev->address;
2082 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
2083 ohci_soft_ed_t *sed;
2084 ohci_soft_td_t *std;
2085 ohci_soft_itd_t *sitd;
2086 ohci_physaddr_t tdphys;
2091 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2092 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
2095 return (USBD_IOERROR);
2100 if (addr == sc->sc_addr) {
2101 switch (ed->bEndpointAddress) {
2102 case USB_CONTROL_ENDPOINT:
2103 pipe->methods = &ohci_root_ctrl_methods;
2105 case UE_DIR_IN | OHCI_INTR_ENDPT:
2106 pipe->methods = &ohci_root_intr_methods;
2109 return (USBD_INVAL);
2112 sed = ohci_alloc_sed(sc);
2116 if (xfertype == UE_ISOCHRONOUS) {
2117 sitd = ohci_alloc_sitd(sc);
2120 opipe->tail.itd = sitd;
2121 opipe->aborting = 0;
2122 tdphys = sitd->physaddr;
2123 fmt = OHCI_ED_FORMAT_ISO;
2124 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
2125 fmt |= OHCI_ED_DIR_IN;
2127 fmt |= OHCI_ED_DIR_OUT;
2129 std = ohci_alloc_std(sc);
2132 opipe->tail.td = std;
2133 tdphys = std->physaddr;
2134 fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
2136 sed->ed.ed_flags = htole32(
2137 OHCI_ED_SET_FA(addr) |
2138 OHCI_ED_SET_EN(UE_GET_ADDR(ed->bEndpointAddress)) |
2139 (dev->speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) |
2141 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
2142 sed->ed.ed_headp = htole32(tdphys |
2143 (pipe->endpoint->savedtoggle ? OHCI_TOGGLECARRY : 0));
2144 sed->ed.ed_tailp = htole32(tdphys);
2148 pipe->methods = &ohci_device_ctrl_methods;
2149 err = usb_allocmem(&sc->sc_bus,
2150 sizeof(usb_device_request_t),
2151 0, &opipe->u.ctl.reqdma);
2155 ohci_add_ed(sed, sc->sc_ctrl_head);
2159 pipe->methods = &ohci_device_intr_methods;
2160 ival = pipe->interval;
2161 if (ival == USBD_DEFAULT_INTERVAL)
2162 ival = ed->bInterval;
2163 return (ohci_device_setintr(sc, opipe, ival));
2164 case UE_ISOCHRONOUS:
2165 pipe->methods = &ohci_device_isoc_methods;
2166 return (ohci_setup_isoc(pipe));
2168 pipe->methods = &ohci_device_bulk_methods;
2170 ohci_add_ed(sed, sc->sc_bulk_head);
2175 return (USBD_NORMAL_COMPLETION);
2179 ohci_free_std(sc, std);
2182 ohci_free_sed(sc, sed);
2186 return (USBD_NOMEM);
2191 * Close a reqular pipe.
2192 * Assumes that there are no pending transactions.
2195 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
2197 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2198 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2199 ohci_soft_ed_t *sed = opipe->sed;
2203 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2204 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2205 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK)) {
2206 ohci_soft_td_t *std;
2207 std = ohci_hash_find_td(sc, le32toh(sed->ed.ed_headp));
2208 kprintf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
2209 "tl=0x%x pipe=%p, std=%p\n", sed,
2210 (int)le32toh(sed->ed.ed_headp),
2211 (int)le32toh(sed->ed.ed_tailp),
2214 usbd_dump_pipe(&opipe->pipe);
2221 usb_delay_ms(&sc->sc_bus, 2);
2222 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2223 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2224 kprintf("ohci_close_pipe: pipe still not empty\n");
2227 ohci_rem_ed(sed, head);
2228 /* Make sure the host controller is not touching this ED */
2229 usb_delay_ms(&sc->sc_bus, 1);
2231 pipe->endpoint->savedtoggle =
2232 (le32toh(sed->ed.ed_headp) & OHCI_TOGGLECARRY) ? 1 : 0;
2233 ohci_free_sed(sc, opipe->sed);
2238 * Abort a device request.
2239 * If this routine is called from a critical section it guarantees that
2240 * the request will be removed from the hardware scheduling and that
2241 * the callback for it will be called with USBD_CANCELLED status.
2242 * It's impossible to guarantee that the requested transfer will not
2243 * have happened since the hardware runs concurrently.
2244 * If the transaction has already happened we rely on the ordinary
2245 * interrupt processing to process it.
2248 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2250 struct ohci_xfer *oxfer = OXFER(xfer);
2251 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2252 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
2253 ohci_soft_ed_t *sed = opipe->sed;
2254 ohci_soft_td_t *p, *n;
2255 ohci_physaddr_t headp;
2258 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p sed=%p\n", xfer, opipe,sed));
2261 /* If we're dying, just do the software part. */
2263 xfer->status = status; /* make software ignore it */
2264 callout_stop(&xfer->timeout_handle);
2265 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2266 usb_transfer_complete(xfer);
2271 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
2272 panic("ohci_abort_xfer: not in process context");
2275 * If an abort is already in progress then just wait for it to
2276 * complete and return.
2278 if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING) {
2279 DPRINTFN(2, ("ohci_abort_xfer: already aborting\n"));
2280 /* No need to wait if we're aborting from a timeout. */
2281 if (status == USBD_TIMEOUT)
2283 /* Override the status which might be USBD_TIMEOUT. */
2284 xfer->status = status;
2285 DPRINTFN(2, ("ohci_abort_xfer: waiting for abort to finish\n"));
2286 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTWAIT;
2287 while (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING)
2288 tsleep(&oxfer->ohci_xfer_flags, 0, "ohciaw", 0);
2293 * Step 1: Make interrupt routine and hardware ignore xfer.
2296 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTING;
2297 xfer->status = status; /* make software ignore it */
2298 callout_stop(&xfer->timeout_handle);
2299 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2301 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
2302 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
2305 * Step 2: Wait until we know hardware has finished any possible
2306 * use of the xfer. Also make sure the soft interrupt routine
2309 usb_delay_ms(opipe->pipe.device->bus, 20); /* Hardware finishes in 1ms */
2311 #ifdef USB_USE_SOFTINTR
2312 sc->sc_softwake = 1;
2313 #endif /* USB_USE_SOFTINTR */
2314 usb_schedsoftintr(&sc->sc_bus);
2315 #ifdef USB_USE_SOFTINTR
2316 tsleep(&sc->sc_softwake, 0, "ohciab", 0);
2317 #endif /* USB_USE_SOFTINTR */
2320 * Step 3: Remove any vestiges of the xfer from the hardware.
2321 * The complication here is that the hardware may have executed
2322 * beyond the xfer we're trying to abort. So as we're scanning
2323 * the TDs of this xfer we check if the hardware points to
2329 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING; /* XXX */
2331 kprintf("ohci_abort_xfer: hcpriv is NULL\n");
2336 if (ohcidebug > 1) {
2337 DPRINTF(("ohci_abort_xfer: sed=\n"));
2342 headp = le32toh(sed->ed.ed_headp) & OHCI_HEADMASK;
2344 for (; p->xfer == xfer; p = n) {
2345 hit |= headp == p->physaddr;
2347 ohci_free_std(sc, p);
2349 /* Zap headp register if hardware pointed inside the xfer. */
2351 DPRINTFN(1,("ohci_abort_xfer: set hd=0x08%x, tl=0x%08x\n",
2352 (int)p->physaddr, (int)le32toh(sed->ed.ed_tailp)));
2353 sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */
2355 DPRINTFN(1,("ohci_abort_xfer: no hit\n"));
2359 * Step 4: Turn on hardware again.
2361 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
2364 * Step 5: Execute callback.
2366 /* Do the wakeup first to avoid touching the xfer after the callback. */
2367 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING;
2368 if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTWAIT) {
2369 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTWAIT;
2370 wakeup(&oxfer->ohci_xfer_flags);
2372 usb_transfer_complete(xfer);
2378 * Data structures and routines to emulate the root hub.
2380 static usb_device_descriptor_t ohci_devd = {
2381 USB_DEVICE_DESCRIPTOR_SIZE,
2382 UDESC_DEVICE, /* type */
2383 {0x00, 0x01}, /* USB version */
2384 UDCLASS_HUB, /* class */
2385 UDSUBCLASS_HUB, /* subclass */
2386 UDPROTO_FSHUB, /* protocol */
2387 64, /* max packet */
2388 {0},{0},{0x00,0x01}, /* device id */
2389 1,2,0, /* string indicies */
2390 1 /* # of configurations */
2393 static usb_config_descriptor_t ohci_confd = {
2394 USB_CONFIG_DESCRIPTOR_SIZE,
2396 {USB_CONFIG_DESCRIPTOR_SIZE +
2397 USB_INTERFACE_DESCRIPTOR_SIZE +
2398 USB_ENDPOINT_DESCRIPTOR_SIZE},
2406 static usb_interface_descriptor_t ohci_ifcd = {
2407 USB_INTERFACE_DESCRIPTOR_SIZE,
2418 static usb_endpoint_descriptor_t ohci_endpd = {
2419 USB_ENDPOINT_DESCRIPTOR_SIZE,
2421 UE_DIR_IN | OHCI_INTR_ENDPT,
2423 {8, 0}, /* max packet */
2427 static usb_hub_descriptor_t ohci_hubd = {
2428 USB_HUB_DESCRIPTOR_SIZE,
2438 ohci_str(usb_string_descriptor_t *p, int l, const char *s)
2444 p->bLength = 2 * strlen(s) + 2;
2447 p->bDescriptorType = UDESC_STRING;
2449 for (i = 0; s[i] && l > 1; i++, l -= 2)
2450 USETW2(p->bString[i], 0, s[i]);
2455 * Simulate a hardware hub by handling all the necessary requests.
2458 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
2462 /* Insert last in queue. */
2463 err = usb_insert_transfer(xfer);
2467 /* Pipe isn't running, start first */
2468 return (ohci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2472 ohci_root_ctrl_start(usbd_xfer_handle xfer)
2474 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2475 usb_device_request_t *req;
2478 int len, value, index, l, totlen = 0;
2479 usb_port_status_t ps;
2480 usb_hub_descriptor_t hubd;
2485 return (USBD_IOERROR);
2488 if (!(xfer->rqflags & URQ_REQUEST))
2490 return (USBD_INVAL);
2492 req = &xfer->request;
2494 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2495 req->bmRequestType, req->bRequest));
2497 len = UGETW(req->wLength);
2498 value = UGETW(req->wValue);
2499 index = UGETW(req->wIndex);
2502 buf = KERNADDR(&xfer->dmabuf, 0);
2504 #define C(x,y) ((x) | ((y) << 8))
2505 switch(C(req->bRequest, req->bmRequestType)) {
2506 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2507 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2508 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2510 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2511 * for the integrated root hub.
2514 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2516 *(u_int8_t *)buf = sc->sc_conf;
2520 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2521 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2522 switch(value >> 8) {
2524 if ((value & 0xff) != 0) {
2528 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2529 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2530 memcpy(buf, &ohci_devd, l);
2533 if ((value & 0xff) != 0) {
2537 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2538 memcpy(buf, &ohci_confd, l);
2539 buf = (char *)buf + l;
2541 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2543 memcpy(buf, &ohci_ifcd, l);
2544 buf = (char *)buf + l;
2546 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2548 memcpy(buf, &ohci_endpd, l);
2553 *(u_int8_t *)buf = 0;
2555 switch (value & 0xff) {
2556 case 1: /* Vendor */
2557 totlen = ohci_str(buf, len, sc->sc_vendor);
2559 case 2: /* Product */
2560 totlen = ohci_str(buf, len, "OHCI root hub");
2569 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2571 *(u_int8_t *)buf = 0;
2575 case C(UR_GET_STATUS, UT_READ_DEVICE):
2577 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2581 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2582 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2584 USETW(((usb_status_t *)buf)->wStatus, 0);
2588 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2589 if (value >= USB_MAX_DEVICES) {
2593 sc->sc_addr = value;
2595 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2596 if (value != 0 && value != 1) {
2600 sc->sc_conf = value;
2602 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2604 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2605 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2606 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2609 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2611 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2614 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2616 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2617 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2618 "port=%d feature=%d\n",
2620 if (index < 1 || index > sc->sc_noport) {
2624 port = OHCI_RH_PORT_STATUS(index);
2626 case UHF_PORT_ENABLE:
2627 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2629 case UHF_PORT_SUSPEND:
2630 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2632 case UHF_PORT_POWER:
2633 /* Yes, writing to the LOW_SPEED bit clears power. */
2634 OWRITE4(sc, port, UPS_LOW_SPEED);
2636 case UHF_C_PORT_CONNECTION:
2637 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2639 case UHF_C_PORT_ENABLE:
2640 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2642 case UHF_C_PORT_SUSPEND:
2643 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2645 case UHF_C_PORT_OVER_CURRENT:
2646 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2648 case UHF_C_PORT_RESET:
2649 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2656 case UHF_C_PORT_CONNECTION:
2657 case UHF_C_PORT_ENABLE:
2658 case UHF_C_PORT_SUSPEND:
2659 case UHF_C_PORT_OVER_CURRENT:
2660 case UHF_C_PORT_RESET:
2661 /* Enable RHSC interrupt if condition is cleared. */
2662 if ((OREAD4(sc, port) >> 16) == 0)
2663 ohci_rhsc_able(sc, 1);
2669 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2670 if ((value & 0xff) != 0) {
2674 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2676 hubd.bNbrPorts = sc->sc_noport;
2677 USETW(hubd.wHubCharacteristics,
2678 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2679 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2680 /* XXX overcurrent */
2682 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2683 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2684 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2685 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2686 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2687 l = min(len, hubd.bDescLength);
2689 memcpy(buf, &hubd, l);
2691 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2696 memset(buf, 0, len); /* ? XXX */
2699 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2700 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2702 if (index < 1 || index > sc->sc_noport) {
2710 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2711 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2713 USETW(ps.wPortStatus, v);
2714 USETW(ps.wPortChange, v >> 16);
2715 l = min(len, sizeof ps);
2716 memcpy(buf, &ps, l);
2719 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2722 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2724 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2725 if (index < 1 || index > sc->sc_noport) {
2729 port = OHCI_RH_PORT_STATUS(index);
2731 case UHF_PORT_ENABLE:
2732 OWRITE4(sc, port, UPS_PORT_ENABLED);
2734 case UHF_PORT_SUSPEND:
2735 OWRITE4(sc, port, UPS_SUSPEND);
2737 case UHF_PORT_RESET:
2738 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2740 OWRITE4(sc, port, UPS_RESET);
2741 for (i = 0; i < 5; i++) {
2742 usb_delay_ms(&sc->sc_bus,
2743 USB_PORT_ROOT_RESET_DELAY);
2748 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2751 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2752 index, OREAD4(sc, port)));
2754 case UHF_PORT_POWER:
2755 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2757 OWRITE4(sc, port, UPS_PORT_POWER);
2768 xfer->actlen = totlen;
2769 err = USBD_NORMAL_COMPLETION;
2773 usb_transfer_complete(xfer);
2775 return (USBD_IN_PROGRESS);
2778 /* Abort a root control request. */
2780 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2782 /* Nothing to do, all transfers are synchronous. */
2785 /* Close the root pipe. */
2787 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2789 DPRINTF(("ohci_root_ctrl_close\n"));
2790 /* Nothing to do. */
2794 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2798 /* Insert last in queue. */
2799 err = usb_insert_transfer(xfer);
2803 /* Pipe isn't running, start first */
2804 return (ohci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
2808 ohci_root_intr_start(usbd_xfer_handle xfer)
2810 usbd_pipe_handle pipe = xfer->pipe;
2811 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2814 return (USBD_IOERROR);
2816 sc->sc_intrxfer = xfer;
2818 return (USBD_IN_PROGRESS);
2821 /* Abort a root interrupt request. */
2823 ohci_root_intr_abort(usbd_xfer_handle xfer)
2825 if (xfer->pipe->intrxfer == xfer) {
2826 DPRINTF(("ohci_root_intr_abort: remove\n"));
2827 xfer->pipe->intrxfer = NULL;
2829 xfer->status = USBD_CANCELLED;
2831 usb_transfer_complete(xfer);
2835 /* Close the root pipe. */
2837 ohci_root_intr_close(usbd_pipe_handle pipe)
2839 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2841 DPRINTF(("ohci_root_intr_close\n"));
2843 sc->sc_intrxfer = NULL;
2846 /************************/
2849 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2853 /* Insert last in queue. */
2854 err = usb_insert_transfer(xfer);
2858 /* Pipe isn't running, start first */
2859 return (ohci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2863 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2865 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2869 return (USBD_IOERROR);
2872 if (!(xfer->rqflags & URQ_REQUEST)) {
2874 kprintf("ohci_device_ctrl_transfer: not a request\n");
2875 return (USBD_INVAL);
2879 err = ohci_device_request(xfer);
2883 if (sc->sc_bus.use_polling)
2884 ohci_waitintr(sc, xfer);
2885 return (USBD_IN_PROGRESS);
2888 /* Abort a device control request. */
2890 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2892 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2893 ohci_abort_xfer(xfer, USBD_CANCELLED);
2896 /* Close a device control pipe. */
2898 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2900 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2901 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2903 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2904 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2905 ohci_free_std(sc, opipe->tail.td);
2908 /************************/
2911 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2913 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2915 opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY);
2919 ohci_noop(usbd_pipe_handle pipe)
2924 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2928 /* Insert last in queue. */
2929 err = usb_insert_transfer(xfer);
2933 /* Pipe isn't running, start first */
2934 return (ohci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
2938 ohci_device_bulk_start(usbd_xfer_handle xfer)
2940 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2941 usbd_device_handle dev = opipe->pipe.device;
2942 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2943 int addr = dev->address;
2944 ohci_soft_td_t *data, *tail, *tdp;
2945 ohci_soft_ed_t *sed;
2946 int len, isread, endpt;
2950 return (USBD_IOERROR);
2953 if (xfer->rqflags & URQ_REQUEST) {
2955 kprintf("ohci_device_bulk_start: a request\n");
2956 return (USBD_INVAL);
2961 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2962 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2965 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2966 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2969 opipe->u.bulk.isread = isread;
2970 opipe->u.bulk.length = len;
2972 /* Update device address */
2973 sed->ed.ed_flags = htole32(
2974 (le32toh(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2975 OHCI_ED_SET_FA(addr));
2977 /* Allocate a chain of new TDs (including a new tail). */
2978 data = opipe->tail.td;
2979 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
2981 /* We want interrupt at the end of the transfer. */
2982 tail->td.td_flags &= htole32(~OHCI_TD_INTR_MASK);
2983 tail->td.td_flags |= htole32(OHCI_TD_SET_DI(1));
2984 tail->flags |= OHCI_CALL_DONE;
2985 tail = tail->nexttd; /* point at sentinel */
2990 xfer->hcpriv = data;
2992 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2993 "td_cbp=0x%08x td_be=0x%08x\n",
2994 (int)le32toh(sed->ed.ed_flags),
2995 (int)le32toh(data->td.td_flags),
2996 (int)le32toh(data->td.td_cbp),
2997 (int)le32toh(data->td.td_be)));
3000 if (ohcidebug > 5) {
3002 ohci_dump_tds(data);
3006 /* Insert ED in schedule */
3008 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
3011 sed->ed.ed_tailp = htole32(tail->physaddr);
3012 opipe->tail.td = tail;
3013 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3014 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
3015 if (xfer->timeout && !sc->sc_bus.use_polling) {
3016 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3017 ohci_timeout, xfer);
3021 /* This goes wrong if we are too slow. */
3022 if (ohcidebug > 10) {
3024 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
3025 OREAD4(sc, OHCI_COMMAND_STATUS)));
3027 ohci_dump_tds(data);
3033 if (sc->sc_bus.use_polling)
3034 ohci_waitintr(sc, xfer);
3036 return (USBD_IN_PROGRESS);
3040 ohci_device_bulk_abort(usbd_xfer_handle xfer)
3042 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
3043 ohci_abort_xfer(xfer, USBD_CANCELLED);
3047 * Close a device bulk pipe.
3050 ohci_device_bulk_close(usbd_pipe_handle pipe)
3052 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3053 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3055 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
3056 ohci_close_pipe(pipe, sc->sc_bulk_head);
3057 ohci_free_std(sc, opipe->tail.td);
3060 /************************/
3063 ohci_device_intr_transfer(usbd_xfer_handle xfer)
3067 /* Insert last in queue. */
3068 err = usb_insert_transfer(xfer);
3072 /* Pipe isn't running, start first */
3073 return (ohci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3077 ohci_device_intr_start(usbd_xfer_handle xfer)
3079 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3080 usbd_device_handle dev = opipe->pipe.device;
3081 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3082 ohci_soft_ed_t *sed = opipe->sed;
3083 ohci_soft_td_t *data, *tail;
3087 return (USBD_IOERROR);
3089 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
3090 "flags=%d priv=%p\n",
3091 xfer, xfer->length, xfer->flags, xfer->priv));
3094 if (xfer->rqflags & URQ_REQUEST)
3095 panic("ohci_device_intr_transfer: a request");
3100 data = opipe->tail.td;
3101 tail = ohci_alloc_std(sc);
3103 return (USBD_NOMEM);
3106 data->td.td_flags = htole32(
3107 OHCI_TD_IN | OHCI_TD_NOCC |
3108 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
3109 if (xfer->flags & USBD_SHORT_XFER_OK)
3110 data->td.td_flags |= htole32(OHCI_TD_R);
3111 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
3112 data->nexttd = tail;
3113 data->td.td_nexttd = htole32(tail->physaddr);
3114 data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1);
3117 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
3118 xfer->hcpriv = data;
3121 if (ohcidebug > 5) {
3122 DPRINTF(("ohci_device_intr_transfer:\n"));
3124 ohci_dump_tds(data);
3128 /* Insert ED in schedule */
3130 sed->ed.ed_tailp = htole32(tail->physaddr);
3131 opipe->tail.td = tail;
3132 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3136 * This goes horribly wrong, printing thousands of descriptors,
3137 * because false references are followed due to the fact that the
3140 if (ohcidebug > 5) {
3141 usb_delay_ms(&sc->sc_bus, 5);
3142 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
3143 OREAD4(sc, OHCI_COMMAND_STATUS)));
3145 ohci_dump_tds(data);
3150 return (USBD_IN_PROGRESS);
3153 /* Abort a device control request. */
3155 ohci_device_intr_abort(usbd_xfer_handle xfer)
3157 if (xfer->pipe->intrxfer == xfer) {
3158 DPRINTF(("ohci_device_intr_abort: remove\n"));
3159 xfer->pipe->intrxfer = NULL;
3161 ohci_abort_xfer(xfer, USBD_CANCELLED);
3164 /* Close a device interrupt pipe. */
3166 ohci_device_intr_close(usbd_pipe_handle pipe)
3168 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3169 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3170 int nslots = opipe->u.intr.nslots;
3171 int pos = opipe->u.intr.pos;
3173 ohci_soft_ed_t *p, *sed = opipe->sed;
3175 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
3176 pipe, nslots, pos));
3178 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
3179 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3180 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3181 usb_delay_ms(&sc->sc_bus, 2);
3183 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3184 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3185 panic("%s: Intr pipe %p still has TDs queued",
3186 device_get_nameunit(sc->sc_bus.bdev), pipe);
3189 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
3193 panic("ohci_device_intr_close: ED not found");
3195 p->next = sed->next;
3196 p->ed.ed_nexted = sed->ed.ed_nexted;
3199 for (j = 0; j < nslots; j++)
3200 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
3202 ohci_free_std(sc, opipe->tail.td);
3203 opipe->tail.td = NULL;
3204 ohci_free_sed(sc, opipe->sed);
3209 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
3212 u_int npoll, slow, shigh, nslots;
3214 ohci_soft_ed_t *hsed, *sed = opipe->sed;
3216 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
3218 kprintf("ohci_setintr: 0 interval\n");
3219 return (USBD_INVAL);
3222 npoll = OHCI_NO_INTRS;
3223 while (npoll > ival)
3225 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
3228 * We now know which level in the tree the ED must go into.
3229 * Figure out which slot has most bandwidth left over.
3235 * 8 7 8 9 10 11 12 13 14
3236 * N (N-1) .. (N-1+N-1)
3239 shigh = slow + npoll;
3240 nslots = OHCI_NO_INTRS / npoll;
3241 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
3243 for (j = 0; j < nslots; j++)
3244 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
3250 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
3251 best, slow, shigh, bestbw));
3254 hsed = sc->sc_eds[best];
3255 sed->next = hsed->next;
3256 sed->ed.ed_nexted = hsed->ed.ed_nexted;
3258 hsed->ed.ed_nexted = htole32(sed->physaddr);
3261 for (j = 0; j < nslots; j++)
3262 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
3263 opipe->u.intr.nslots = nslots;
3264 opipe->u.intr.pos = best;
3266 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
3267 return (USBD_NORMAL_COMPLETION);
3270 /***********************/
3273 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
3277 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
3279 /* Put it on our queue, */
3280 err = usb_insert_transfer(xfer);
3282 /* bail out on error, */
3283 if (err && err != USBD_IN_PROGRESS)
3286 /* XXX should check inuse here */
3288 /* insert into schedule, */
3289 ohci_device_isoc_enter(xfer);
3291 /* and start if the pipe wasn't running */
3293 ohci_device_isoc_start(STAILQ_FIRST(&xfer->pipe->queue));
3299 ohci_device_isoc_enter(usbd_xfer_handle xfer)
3301 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3302 usbd_device_handle dev = opipe->pipe.device;
3303 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3304 ohci_soft_ed_t *sed = opipe->sed;
3305 struct iso *iso = &opipe->u.iso;
3306 struct ohci_xfer *oxfer = (struct ohci_xfer *)xfer;
3307 ohci_soft_itd_t *sitd, *nsitd;
3308 ohci_physaddr_t buf, offs, noffs, bp0, tdphys;
3309 int i, ncur, nframes;
3311 DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p "
3313 iso->inuse, iso->next, xfer, xfer->nframes));
3318 if (iso->next == -1) {
3319 /* Not in use yet, schedule it a few frames ahead. */
3320 iso->next = le32toh(sc->sc_hcca->hcca_frame_number) + 5;
3321 DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n",
3327 for (sitd = xfer->hcpriv; sitd != NULL && sitd->xfer == xfer;
3328 sitd = sitd->nextitd)
3329 ohci_free_sitd(sc, sitd); /* Free ITDs in prev xfer*/
3333 sitd = ohci_alloc_sitd(sc);
3335 panic("can't alloc isoc");
3336 opipe->tail.itd = sitd;
3337 tdphys = sitd->physaddr;
3338 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop*/
3340 sed->ed.ed_tailp = htole32(tdphys);
3341 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* Start.*/
3345 sitd = opipe->tail.itd;
3346 buf = DMAADDR(&xfer->dmabuf, 0);
3347 bp0 = OHCI_PAGE(buf);
3348 offs = OHCI_PAGE_OFFSET(buf);
3349 nframes = xfer->nframes;
3350 xfer->hcpriv = sitd;
3351 for (i = ncur = 0; i < nframes; i++, ncur++) {
3352 noffs = offs + xfer->frlengths[i];
3353 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
3354 OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */
3356 /* Allocate next ITD */
3357 nsitd = ohci_alloc_sitd(sc);
3358 if (nsitd == NULL) {
3360 device_printf(sc->sc_bus.bdev,
3361 "isoc TD alloc failed\n");
3365 /* Fill current ITD */
3366 sitd->itd.itd_flags = htole32(
3368 OHCI_ITD_SET_SF(iso->next) |
3369 OHCI_ITD_SET_DI(6) | /* delay intr a little */
3370 OHCI_ITD_SET_FC(ncur));
3371 sitd->itd.itd_bp0 = htole32(bp0);
3372 sitd->nextitd = nsitd;
3373 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3374 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3376 sitd->flags = OHCI_ITD_ACTIVE;
3379 iso->next = iso->next + ncur;
3380 bp0 = OHCI_PAGE(buf + offs);
3383 sitd->itd.itd_offset[ncur] = htole16(OHCI_ITD_MK_OFFS(offs));
3386 nsitd = ohci_alloc_sitd(sc);
3387 if (nsitd == NULL) {
3389 device_printf(sc->sc_bus.bdev, "isoc TD alloc failed\n");
3392 /* Fixup last used ITD */
3393 sitd->itd.itd_flags = htole32(
3395 OHCI_ITD_SET_SF(iso->next) |
3396 OHCI_ITD_SET_DI(0) |
3397 OHCI_ITD_SET_FC(ncur));
3398 sitd->itd.itd_bp0 = htole32(bp0);
3399 sitd->nextitd = nsitd;
3400 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3401 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3403 sitd->flags = OHCI_CALL_DONE | OHCI_ITD_ACTIVE;
3405 iso->next = iso->next + ncur;
3406 iso->inuse += nframes;
3408 xfer->actlen = offs; /* XXX pretend we did it all */
3410 xfer->status = USBD_IN_PROGRESS;
3412 oxfer->ohci_xfer_flags |= OHCI_ISOC_DIRTY;
3415 if (ohcidebug > 5) {
3416 DPRINTF(("ohci_device_isoc_enter: frame=%d\n",
3417 le32toh(sc->sc_hcca->hcca_frame_number)));
3418 ohci_dump_itds(xfer->hcpriv);
3424 sed->ed.ed_tailp = htole32(nsitd->physaddr);
3425 opipe->tail.itd = nsitd;
3426 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3430 if (ohcidebug > 5) {
3432 DPRINTF(("ohci_device_isoc_enter: after frame=%d\n",
3433 le32toh(sc->sc_hcca->hcca_frame_number)));
3434 ohci_dump_itds(xfer->hcpriv);
3441 ohci_device_isoc_start(usbd_xfer_handle xfer)
3443 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3444 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3445 ohci_soft_ed_t *sed;
3447 DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer));
3450 return (USBD_IOERROR);
3453 if (xfer->status != USBD_IN_PROGRESS)
3454 kprintf("ohci_device_isoc_start: not in progress %p\n", xfer);
3457 /* XXX anything to do? */
3460 sed = opipe->sed; /* Turn off ED skip-bit to start processing */
3461 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* ED's ITD list.*/
3464 return (USBD_IN_PROGRESS);
3468 ohci_device_isoc_abort(usbd_xfer_handle xfer)
3470 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3471 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3472 ohci_soft_ed_t *sed;
3473 ohci_soft_itd_t *sitd, *tmp_sitd;
3474 int undone, num_sitds;
3477 opipe->aborting = 1;
3479 DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer));
3481 /* Transfer is already done. */
3482 if (xfer->status != USBD_NOT_STARTED &&
3483 xfer->status != USBD_IN_PROGRESS) {
3485 kprintf("ohci_device_isoc_abort: early return\n");
3489 /* Give xfer the requested abort code. */
3490 xfer->status = USBD_CANCELLED;
3493 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
3496 sitd = xfer->hcpriv;
3500 kprintf("ohci_device_isoc_abort: hcpriv==0\n");
3504 for (; sitd != NULL && sitd->xfer == xfer; sitd = sitd->nextitd) {
3507 DPRINTFN(1,("abort sets done sitd=%p\n", sitd));
3515 * Each sitd has up to OHCI_ITD_NOFFSET transfers, each can
3516 * take a usb 1ms cycle. Conservatively wait for it to drain.
3517 * Even with DMA done, it can take awhile for the "batch"
3518 * delivery of completion interrupts to occur thru the controller.
3522 usb_delay_ms(&sc->sc_bus, 2*(num_sitds*OHCI_ITD_NOFFSET));
3525 tmp_sitd = xfer->hcpriv;
3526 for (; tmp_sitd != NULL && tmp_sitd->xfer == xfer;
3527 tmp_sitd = tmp_sitd->nextitd) {
3528 if (OHCI_CC_NO_ERROR ==
3529 OHCI_ITD_GET_CC(le32toh(tmp_sitd->itd.itd_flags)) &&
3530 tmp_sitd->flags & OHCI_ITD_ACTIVE &&
3531 (tmp_sitd->flags & OHCI_ITD_INTFIN) == 0)
3534 } while( undone != 0 );
3539 usb_transfer_complete(xfer);
3543 * Only if there is a `next' sitd in next xfer...
3544 * unlink this xfer's sitds.
3546 sed->ed.ed_headp = htole32(sitd->physaddr);
3548 sed->ed.ed_headp = 0;
3550 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
3556 ohci_device_isoc_done(usbd_xfer_handle xfer)
3558 /* This null routine corresponds to non-isoc "done()" routines
3559 * that free the stds associated with an xfer after a completed
3560 * xfer interrupt. However, in the case of isoc transfers, the
3561 * sitds associated with the transfer have already been processed
3562 * and reallocated for the next iteration by
3563 * "ohci_device_isoc_transfer()".
3565 * Routine "usb_transfer_complete()" is called at the end of every
3566 * relevant usb interrupt. "usb_transfer_complete()" indirectly
3567 * calls 1) "ohci_device_isoc_transfer()" (which keeps pumping the
3568 * pipeline by setting up the next transfer iteration) and 2) then
3569 * calls "ohci_device_isoc_done()". Isoc transfers have not been
3570 * working for the ohci usb because this routine was trashing the
3571 * xfer set up for the next iteration (thus, only the first
3572 * UGEN_NISOREQS xfers outstanding on an open would work). Perhaps
3573 * this could all be re-factored, but that's another pass...
3578 ohci_setup_isoc(usbd_pipe_handle pipe)
3580 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3581 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3582 struct iso *iso = &opipe->u.iso;
3588 ohci_add_ed(opipe->sed, sc->sc_isoc_head);
3591 return (USBD_NORMAL_COMPLETION);
3595 ohci_device_isoc_close(usbd_pipe_handle pipe)
3597 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3598 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3599 ohci_soft_ed_t *sed;
3601 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
3604 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop device. */
3606 ohci_close_pipe(pipe, sc->sc_isoc_head); /* Stop isoc list, free ED.*/
3608 /* up to NISOREQs xfers still outstanding. */
3611 opipe->tail.itd->isdone = 1;
3613 ohci_free_sitd(sc, opipe->tail.itd); /* Next `avail free' sitd.*/