1 /* $NetBSD: ohci.c,v 1.125 2002/05/28 12:42:38 augustss Exp $ */
4 /* Also, already ported:
5 * $NetBSD: ohci.c,v 1.127 2002/08/07 20:03:19 augustss Exp $
9 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * All rights reserved.
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Lennart Augustsson (lennart@augustsson.net) at
14 * Carlstedt Research & Technology.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the NetBSD
27 * Foundation, Inc. and its contributors.
28 * 4. Neither the name of The NetBSD Foundation nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
33 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
34 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
36 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
46 * USB Open Host Controller driver.
48 * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html
49 * USB spec: http://www.usb.org/developers/data/usbspec.zip
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #if defined(__NetBSD__) || defined(__OpenBSD__)
56 #include <sys/kernel.h>
57 #include <sys/device.h>
58 #include <sys/select.h>
59 #elif defined(__FreeBSD__)
60 #include <sys/endian.h>
61 #include <sys/module.h>
63 #include <machine/bus_pio.h>
64 #include <machine/bus_memio.h>
65 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
66 #include <machine/cpu.h>
70 #include <sys/queue.h>
71 #include <sys/sysctl.h>
73 #include <machine/bus.h>
74 #include <machine/endian.h>
76 #include <dev/usb/usb.h>
77 #include <dev/usb/usbdi.h>
78 #include <dev/usb/usbdivar.h>
79 #include <dev/usb/usb_mem.h>
80 #include <dev/usb/usb_quirks.h>
82 #include <dev/usb/ohcireg.h>
83 #include <dev/usb/ohcivar.h>
85 #if defined(__FreeBSD__)
86 #include <machine/clock.h>
88 #define delay(d) DELAY(d)
91 #if defined(__OpenBSD__)
92 struct cfdriver ohci_cd = {
98 #define DPRINTF(x) if (ohcidebug) logprintf x
99 #define DPRINTFN(n,x) if (ohcidebug>(n)) logprintf x
101 SYSCTL_NODE(_hw_usb, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci");
102 SYSCTL_INT(_hw_usb_ohci, OID_AUTO, debug, CTLFLAG_RW,
103 &ohcidebug, 0, "ohci debug level");
105 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
109 #define DPRINTFN(n,x)
113 * The OHCI controller is little endian, so on big endian machines
114 * the data strored in memory needs to be swapped.
116 #if defined(__OpenBSD__)
117 #if BYTE_ORDER == BIG_ENDIAN
118 #define htole32(x) (bswap32(x))
119 #define le32toh(x) (bswap32(x))
121 #define htole32(x) (x)
122 #define le32toh(x) (x)
128 Static ohci_soft_ed_t *ohci_alloc_sed(ohci_softc_t *);
129 Static void ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
131 Static ohci_soft_td_t *ohci_alloc_std(ohci_softc_t *);
132 Static void ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
134 Static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
135 Static void ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
138 Static void ohci_free_std_chain(ohci_softc_t *, ohci_soft_td_t *,
141 Static usbd_status ohci_alloc_std_chain(struct ohci_pipe *,
142 ohci_softc_t *, int, int, usbd_xfer_handle,
143 ohci_soft_td_t *, ohci_soft_td_t **);
145 #if defined(__NetBSD__) || defined(__OpenBSD__)
146 Static void ohci_shutdown(void *v);
147 Static void ohci_power(int, void *);
149 Static usbd_status ohci_open(usbd_pipe_handle);
150 Static void ohci_poll(struct usbd_bus *);
151 Static void ohci_softintr(void *);
152 Static void ohci_waitintr(ohci_softc_t *, usbd_xfer_handle);
153 Static void ohci_add_done(ohci_softc_t *, ohci_physaddr_t);
154 Static void ohci_rhsc(ohci_softc_t *, usbd_xfer_handle);
156 Static usbd_status ohci_device_request(usbd_xfer_handle xfer);
157 Static void ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
158 Static void ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
159 Static void ohci_hash_add_td(ohci_softc_t *, ohci_soft_td_t *);
160 Static void ohci_hash_rem_td(ohci_softc_t *, ohci_soft_td_t *);
161 Static ohci_soft_td_t *ohci_hash_find_td(ohci_softc_t *, ohci_physaddr_t);
162 Static void ohci_hash_add_itd(ohci_softc_t *, ohci_soft_itd_t *);
163 Static void ohci_hash_rem_itd(ohci_softc_t *, ohci_soft_itd_t *);
164 Static ohci_soft_itd_t *ohci_hash_find_itd(ohci_softc_t *, ohci_physaddr_t);
166 Static usbd_status ohci_setup_isoc(usbd_pipe_handle pipe);
167 Static void ohci_device_isoc_enter(usbd_xfer_handle);
169 Static usbd_status ohci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
170 Static void ohci_freem(struct usbd_bus *, usb_dma_t *);
172 Static usbd_xfer_handle ohci_allocx(struct usbd_bus *);
173 Static void ohci_freex(struct usbd_bus *, usbd_xfer_handle);
175 Static usbd_status ohci_root_ctrl_transfer(usbd_xfer_handle);
176 Static usbd_status ohci_root_ctrl_start(usbd_xfer_handle);
177 Static void ohci_root_ctrl_abort(usbd_xfer_handle);
178 Static void ohci_root_ctrl_close(usbd_pipe_handle);
179 Static void ohci_root_ctrl_done(usbd_xfer_handle);
181 Static usbd_status ohci_root_intr_transfer(usbd_xfer_handle);
182 Static usbd_status ohci_root_intr_start(usbd_xfer_handle);
183 Static void ohci_root_intr_abort(usbd_xfer_handle);
184 Static void ohci_root_intr_close(usbd_pipe_handle);
185 Static void ohci_root_intr_done(usbd_xfer_handle);
187 Static usbd_status ohci_device_ctrl_transfer(usbd_xfer_handle);
188 Static usbd_status ohci_device_ctrl_start(usbd_xfer_handle);
189 Static void ohci_device_ctrl_abort(usbd_xfer_handle);
190 Static void ohci_device_ctrl_close(usbd_pipe_handle);
191 Static void ohci_device_ctrl_done(usbd_xfer_handle);
193 Static usbd_status ohci_device_bulk_transfer(usbd_xfer_handle);
194 Static usbd_status ohci_device_bulk_start(usbd_xfer_handle);
195 Static void ohci_device_bulk_abort(usbd_xfer_handle);
196 Static void ohci_device_bulk_close(usbd_pipe_handle);
197 Static void ohci_device_bulk_done(usbd_xfer_handle);
199 Static usbd_status ohci_device_intr_transfer(usbd_xfer_handle);
200 Static usbd_status ohci_device_intr_start(usbd_xfer_handle);
201 Static void ohci_device_intr_abort(usbd_xfer_handle);
202 Static void ohci_device_intr_close(usbd_pipe_handle);
203 Static void ohci_device_intr_done(usbd_xfer_handle);
205 Static usbd_status ohci_device_isoc_transfer(usbd_xfer_handle);
206 Static usbd_status ohci_device_isoc_start(usbd_xfer_handle);
207 Static void ohci_device_isoc_abort(usbd_xfer_handle);
208 Static void ohci_device_isoc_close(usbd_pipe_handle);
209 Static void ohci_device_isoc_done(usbd_xfer_handle);
211 Static usbd_status ohci_device_setintr(ohci_softc_t *sc,
212 struct ohci_pipe *pipe, int ival);
214 Static int ohci_str(usb_string_descriptor_t *, int, const char *);
216 Static void ohci_timeout(void *);
217 Static void ohci_timeout_task(void *);
218 Static void ohci_rhsc_able(ohci_softc_t *, int);
219 Static void ohci_rhsc_enable(void *);
221 Static void ohci_close_pipe(usbd_pipe_handle, ohci_soft_ed_t *);
222 Static void ohci_abort_xfer(usbd_xfer_handle, usbd_status);
224 Static void ohci_device_clear_toggle(usbd_pipe_handle pipe);
225 Static void ohci_noop(usbd_pipe_handle pipe);
228 Static void ohci_dumpregs(ohci_softc_t *);
229 Static void ohci_dump_tds(ohci_soft_td_t *);
230 Static void ohci_dump_td(ohci_soft_td_t *);
231 Static void ohci_dump_ed(ohci_soft_ed_t *);
232 Static void ohci_dump_itd(ohci_soft_itd_t *);
233 Static void ohci_dump_itds(ohci_soft_itd_t *);
236 #define OBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
237 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
238 #define OWRITE1(sc, r, x) \
239 do { OBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
240 #define OWRITE2(sc, r, x) \
241 do { OBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
242 #define OWRITE4(sc, r, x) \
243 do { OBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
244 #define OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
245 #define OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
246 #define OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
248 /* Reverse the bits in a value 0 .. 31 */
249 Static u_int8_t revbits[OHCI_NO_INTRS] =
250 { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
251 0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
252 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
253 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
256 struct usbd_pipe pipe;
260 ohci_soft_itd_t *itd;
262 /* Info needed for different pipe kinds. */
268 ohci_soft_td_t *setup, *data, *stat;
287 #define OHCI_INTR_ENDPT 1
289 Static struct usbd_bus_methods ohci_bus_methods = {
299 Static struct usbd_pipe_methods ohci_root_ctrl_methods = {
300 ohci_root_ctrl_transfer,
301 ohci_root_ctrl_start,
302 ohci_root_ctrl_abort,
303 ohci_root_ctrl_close,
308 Static struct usbd_pipe_methods ohci_root_intr_methods = {
309 ohci_root_intr_transfer,
310 ohci_root_intr_start,
311 ohci_root_intr_abort,
312 ohci_root_intr_close,
317 Static struct usbd_pipe_methods ohci_device_ctrl_methods = {
318 ohci_device_ctrl_transfer,
319 ohci_device_ctrl_start,
320 ohci_device_ctrl_abort,
321 ohci_device_ctrl_close,
323 ohci_device_ctrl_done,
326 Static struct usbd_pipe_methods ohci_device_intr_methods = {
327 ohci_device_intr_transfer,
328 ohci_device_intr_start,
329 ohci_device_intr_abort,
330 ohci_device_intr_close,
331 ohci_device_clear_toggle,
332 ohci_device_intr_done,
335 Static struct usbd_pipe_methods ohci_device_bulk_methods = {
336 ohci_device_bulk_transfer,
337 ohci_device_bulk_start,
338 ohci_device_bulk_abort,
339 ohci_device_bulk_close,
340 ohci_device_clear_toggle,
341 ohci_device_bulk_done,
344 Static struct usbd_pipe_methods ohci_device_isoc_methods = {
345 ohci_device_isoc_transfer,
346 ohci_device_isoc_start,
347 ohci_device_isoc_abort,
348 ohci_device_isoc_close,
350 ohci_device_isoc_done,
353 #if defined(__NetBSD__) || defined(__OpenBSD__)
355 ohci_activate(device_ptr_t self, enum devact act)
357 struct ohci_softc *sc = (struct ohci_softc *)self;
365 case DVACT_DEACTIVATE:
366 if (sc->sc_child != NULL)
367 rv = config_deactivate(sc->sc_child);
375 ohci_detach(struct ohci_softc *sc, int flags)
379 if (sc->sc_child != NULL)
380 rv = config_detach(sc->sc_child, flags);
385 usb_uncallout(sc->sc_tmo_rhsc, ohci_rhsc_enable, sc);
387 #if defined(__NetBSD__) || defined(__OpenBSD__)
388 powerhook_disestablish(sc->sc_powerhook);
389 shutdownhook_disestablish(sc->sc_shutdownhook);
392 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
394 /* free data structures XXX */
401 ohci_alloc_sed(ohci_softc_t *sc)
408 if (sc->sc_freeeds == NULL) {
409 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
410 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
411 OHCI_ED_ALIGN, &dma);
414 for(i = 0; i < OHCI_SED_CHUNK; i++) {
415 offs = i * OHCI_SED_SIZE;
416 sed = KERNADDR(&dma, offs);
417 sed->physaddr = DMAADDR(&dma, offs);
418 sed->next = sc->sc_freeeds;
419 sc->sc_freeeds = sed;
422 sed = sc->sc_freeeds;
423 sc->sc_freeeds = sed->next;
424 memset(&sed->ed, 0, sizeof(ohci_ed_t));
430 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
432 sed->next = sc->sc_freeeds;
433 sc->sc_freeeds = sed;
437 ohci_alloc_std(ohci_softc_t *sc)
445 if (sc->sc_freetds == NULL) {
446 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
447 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
448 OHCI_TD_ALIGN, &dma);
452 for(i = 0; i < OHCI_STD_CHUNK; i++) {
453 offs = i * OHCI_STD_SIZE;
454 std = KERNADDR(&dma, offs);
455 std->physaddr = DMAADDR(&dma, offs);
456 std->nexttd = sc->sc_freetds;
457 sc->sc_freetds = std;
463 std = sc->sc_freetds;
464 sc->sc_freetds = std->nexttd;
465 memset(&std->td, 0, sizeof(ohci_td_t));
468 ohci_hash_add_td(sc, std);
475 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
480 ohci_hash_rem_td(sc, std);
481 std->nexttd = sc->sc_freetds;
482 sc->sc_freetds = std;
487 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
488 int alen, int rd, usbd_xfer_handle xfer,
489 ohci_soft_td_t *sp, ohci_soft_td_t **ep)
491 ohci_soft_td_t *next, *cur;
492 ohci_physaddr_t dataphys, dataphysend;
493 u_int32_t intr, tdflags;
496 usb_dma_t *dma = &xfer->dmabuf;
497 u_int16_t flags = xfer->flags;
499 DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%d\n", alen));
505 dataphys = DMAADDR(dma, 0);
506 dataphysend = OHCI_PAGE(dataphys + len - 1);
508 (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
509 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
510 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY);
513 next = ohci_alloc_std(sc);
517 dataphys = DMAADDR(dma, offset);
519 /* The OHCI hardware can handle at most one page crossing. */
520 #if defined(__NetBSD__) || defined(__OpenBSD__)
521 if (OHCI_PAGE(dataphys) == OHCI_PAGE(dataphysend) ||
522 OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == OHCI_PAGE(dataphysend))
523 #elif defined(__FreeBSD__)
524 /* XXX This is pretty broken: Because we do not allocate
525 * a contiguous buffer (contiguous in physical pages) we
526 * can only transfer one page in one go.
527 * So check whether the start and end of the buffer are on
530 if (OHCI_PAGE(dataphys) == OHCI_PAGE(dataphysend))
533 /* we can handle it in this TD */
536 /* XXX The calculation below is wrong and could
537 * result in a packet that is not a multiple of the
538 * MaxPacketSize in the case where the buffer does not
539 * start on an appropriate address (like for example in
540 * the case of an mbuf cluster). You'll get an early
543 #if defined(__NetBSD__) || defined(__OpenBSD__)
544 /* must use multiple TDs, fill as much as possible. */
545 curlen = 2 * OHCI_PAGE_SIZE -
546 OHCI_PAGE_MASK(dataphys);
547 #elif defined(__FreeBSD__)
548 /* See comment above (XXX) */
549 curlen = OHCI_PAGE_SIZE -
550 OHCI_PAGE_MASK(dataphys);
553 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
554 "dataphysend=0x%08x len=%d curlen=%d\n",
555 dataphys, dataphysend,
559 intr = len == 0 ? OHCI_TD_SET_DI(1) : OHCI_TD_NOINTR;
560 cur->td.td_flags = htole32(tdflags | intr);
561 cur->td.td_cbp = htole32(dataphys);
563 cur->td.td_nexttd = htole32(next->physaddr);
564 cur->td.td_be = htole32(dataphys + curlen - 1);
566 cur->flags = OHCI_ADD_LEN;
567 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
568 dataphys, dataphys + curlen - 1));
571 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
575 if ((flags & USBD_FORCE_SHORT_XFER) &&
576 alen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
577 /* Force a 0 length transfer at the end. */
578 next = ohci_alloc_std(sc);
582 cur->td.td_flags = htole32(tdflags | OHCI_TD_SET_DI(1));
583 cur->td.td_cbp = 0; /* indicate 0 length packet */
585 cur->td.td_nexttd = htole32(next->physaddr);
586 cur->td.td_be = htole32(dataphys - 1);
591 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
593 cur->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
596 return (USBD_NORMAL_COMPLETION);
605 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
606 ohci_soft_td_t *stdend)
610 for (; std != stdend; std = p) {
612 ohci_free_std(sc, std);
618 ohci_alloc_sitd(ohci_softc_t *sc)
620 ohci_soft_itd_t *sitd;
625 if (sc->sc_freeitds == NULL) {
626 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
627 err = usb_allocmem(&sc->sc_bus, OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
628 OHCI_ITD_ALIGN, &dma);
631 for(i = 0; i < OHCI_SITD_CHUNK; i++) {
632 offs = i * OHCI_SITD_SIZE;
633 sitd = KERNADDR(&dma, offs);
634 sitd->physaddr = DMAADDR(&dma, offs);
635 sitd->nextitd = sc->sc_freeitds;
636 sc->sc_freeitds = sitd;
641 sitd = sc->sc_freeitds;
642 sc->sc_freeitds = sitd->nextitd;
643 memset(&sitd->itd, 0, sizeof(ohci_itd_t));
644 sitd->nextitd = NULL;
646 ohci_hash_add_itd(sc, sitd);
657 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
661 DPRINTFN(10,("ohci_free_sitd: sitd=%p\n", sitd));
665 panic("ohci_free_sitd: sitd=%p not done\n", sitd);
671 ohci_hash_rem_itd(sc, sitd);
672 sitd->nextitd = sc->sc_freeitds;
673 sc->sc_freeitds = sitd;
678 ohci_init(ohci_softc_t *sc)
680 ohci_soft_ed_t *sed, *psed;
683 u_int32_t s, ctl, ival, hcr, fm, per, rev, desca;
685 DPRINTF(("ohci_init: start\n"));
686 #if defined(__OpenBSD__)
689 printf("%s:", USBDEVNAME(sc->sc_bus.bdev));
691 rev = OREAD4(sc, OHCI_REVISION);
692 printf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
693 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
695 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
696 printf("%s: unsupported OHCI revision\n",
697 USBDEVNAME(sc->sc_bus.bdev));
698 sc->sc_bus.usbrev = USBREV_UNKNOWN;
701 sc->sc_bus.usbrev = USBREV_1_0;
703 for (i = 0; i < OHCI_HASH_SIZE; i++)
704 LIST_INIT(&sc->sc_hash_itds[i]);
706 SIMPLEQ_INIT(&sc->sc_free_xfers);
708 /* XXX determine alignment by R/W */
709 /* Allocate the HCCA area. */
710 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
711 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
714 sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0);
715 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
717 sc->sc_eintrs = OHCI_NORMAL_INTRS;
719 /* Allocate dummy ED that starts the control list. */
720 sc->sc_ctrl_head = ohci_alloc_sed(sc);
721 if (sc->sc_ctrl_head == NULL) {
725 sc->sc_ctrl_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
727 /* Allocate dummy ED that starts the bulk list. */
728 sc->sc_bulk_head = ohci_alloc_sed(sc);
729 if (sc->sc_bulk_head == NULL) {
733 sc->sc_bulk_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
735 /* Allocate dummy ED that starts the isochronous list. */
736 sc->sc_isoc_head = ohci_alloc_sed(sc);
737 if (sc->sc_isoc_head == NULL) {
741 sc->sc_isoc_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
743 /* Allocate all the dummy EDs that make up the interrupt tree. */
744 for (i = 0; i < OHCI_NO_EDS; i++) {
745 sed = ohci_alloc_sed(sc);
748 ohci_free_sed(sc, sc->sc_eds[i]);
752 /* All ED fields are set to 0. */
754 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
756 psed = sc->sc_eds[(i-1) / 2];
758 psed= sc->sc_isoc_head;
760 sed->ed.ed_nexted = htole32(psed->physaddr);
763 * Fill HCCA interrupt table. The bit reversal is to get
764 * the tree set up properly to spread the interrupts.
766 for (i = 0; i < OHCI_NO_INTRS; i++)
767 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
768 htole32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
771 if (ohcidebug > 15) {
772 for (i = 0; i < OHCI_NO_EDS; i++) {
774 ohci_dump_ed(sc->sc_eds[i]);
777 ohci_dump_ed(sc->sc_isoc_head);
781 /* Determine in what context we are running. */
782 ctl = OREAD4(sc, OHCI_CONTROL);
784 /* SMM active, request change */
785 DPRINTF(("ohci_init: SMM active, request owner change\n"));
786 s = OREAD4(sc, OHCI_COMMAND_STATUS);
787 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
788 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
789 usb_delay_ms(&sc->sc_bus, 1);
790 ctl = OREAD4(sc, OHCI_CONTROL);
792 if ((ctl & OHCI_IR) == 0) {
793 printf("%s: SMM does not respond, resetting\n",
794 USBDEVNAME(sc->sc_bus.bdev));
795 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
799 /* Don't bother trying to reuse the BIOS init, we'll reset it anyway. */
800 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
801 /* BIOS started controller. */
802 DPRINTF(("ohci_init: BIOS active\n"));
803 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
804 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
805 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
809 DPRINTF(("ohci_init: cold started\n"));
811 /* Controller was cold started. */
812 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
816 * This reset should not be necessary according to the OHCI spec, but
817 * without it some controllers do not start.
819 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
820 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
821 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
823 /* We now own the host controller and the bus has been reset. */
824 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
826 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
827 /* Nominal time for a reset is 10 us. */
828 for (i = 0; i < 10; i++) {
830 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
835 printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
844 /* The controller is now in SUSPEND state, we have 2ms to finish. */
846 /* Set up HC registers. */
847 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
848 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
849 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
850 /* disable all interrupts and then switch on all desired interrupts */
851 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
852 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
853 /* switch on desired functional features */
854 ctl = OREAD4(sc, OHCI_CONTROL);
855 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
856 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
857 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
858 /* And finally start it! */
859 OWRITE4(sc, OHCI_CONTROL, ctl);
862 * The controller is now OPERATIONAL. Set a some final
863 * registers that should be set earlier, but that the
864 * controller ignores when in the SUSPEND state.
866 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
867 fm |= OHCI_FSMPS(ival) | ival;
868 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
869 per = OHCI_PERIODIC(ival); /* 90% periodic */
870 OWRITE4(sc, OHCI_PERIODIC_START, per);
872 /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
873 desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
874 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
875 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
876 usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
877 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
880 * The AMD756 requires a delay before re-reading the register,
881 * otherwise it will occasionally report 0 ports.
883 usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
884 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
891 /* Set up the bus struct. */
892 sc->sc_bus.methods = &ohci_bus_methods;
893 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
895 #if defined(__NetBSD__) || defined(__OpenBSD__)
896 sc->sc_control = sc->sc_intre = 0;
897 sc->sc_powerhook = powerhook_establish(ohci_power, sc);
898 sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc);
901 usb_callout_init(sc->sc_tmo_rhsc);
903 return (USBD_NORMAL_COMPLETION);
906 for (i = 0; i < OHCI_NO_EDS; i++)
907 ohci_free_sed(sc, sc->sc_eds[i]);
909 ohci_free_sed(sc, sc->sc_isoc_head);
911 ohci_free_sed(sc, sc->sc_ctrl_head);
913 ohci_free_sed(sc, sc->sc_bulk_head);
915 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
920 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
922 #if defined(__NetBSD__) || defined(__OpenBSD__)
923 struct ohci_softc *sc = (struct ohci_softc *)bus;
926 return (usb_allocmem(&sc->sc_bus, size, 0, dma));
930 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
932 #if defined(__NetBSD__) || defined(__OpenBSD__)
933 struct ohci_softc *sc = (struct ohci_softc *)bus;
936 usb_freemem(&sc->sc_bus, dma);
940 ohci_allocx(struct usbd_bus *bus)
942 struct ohci_softc *sc = (struct ohci_softc *)bus;
943 usbd_xfer_handle xfer;
945 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
947 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
949 if (xfer->busy_free != XFER_FREE) {
950 printf("ohci_allocx: xfer=%p not free, 0x%08x\n", xfer,
955 xfer = malloc(sizeof(struct ohci_xfer), M_USB, M_NOWAIT);
958 memset(xfer, 0, sizeof (struct ohci_xfer));
960 xfer->busy_free = XFER_BUSY;
967 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
969 struct ohci_softc *sc = (struct ohci_softc *)bus;
972 if (xfer->busy_free != XFER_BUSY) {
973 printf("ohci_freex: xfer=%p not busy, 0x%08x\n", xfer,
977 xfer->busy_free = XFER_FREE;
979 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
983 * Shut down the controller when the system is going down.
985 #if defined(__NetBSD__) || defined(__OpenBSD__)
987 ohci_shutdown(void *v)
989 ohci_softc_t *sc = v;
991 DPRINTF(("ohci_shutdown: stopping the HC\n"));
992 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
996 * Handle suspend/resume.
998 * We need to switch to polling mode here, because this routine is
999 * called from an intterupt context. This is all right since we
1000 * are almost suspended anyway.
1003 ohci_power(int why, void *v)
1005 ohci_softc_t *sc = v;
1010 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
1018 sc->sc_bus.use_polling++;
1019 ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
1020 if (sc->sc_control == 0) {
1022 * Preserve register values, in case that APM BIOS
1023 * does not recover them.
1025 sc->sc_control = ctl;
1026 sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE);
1028 ctl |= OHCI_HCFS_SUSPEND;
1029 OWRITE4(sc, OHCI_CONTROL, ctl);
1030 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1031 sc->sc_bus.use_polling--;
1034 sc->sc_bus.use_polling++;
1035 /* Some broken BIOSes do not recover these values */
1036 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
1037 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
1038 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
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--;
1055 case PWR_SOFTSUSPEND:
1056 case PWR_SOFTSTANDBY:
1057 case PWR_SOFTRESUME:
1066 ohci_dumpregs(ohci_softc_t *sc)
1068 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
1069 OREAD4(sc, OHCI_REVISION),
1070 OREAD4(sc, OHCI_CONTROL),
1071 OREAD4(sc, OHCI_COMMAND_STATUS)));
1072 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
1073 OREAD4(sc, OHCI_INTERRUPT_STATUS),
1074 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
1075 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
1076 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
1077 OREAD4(sc, OHCI_HCCA),
1078 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
1079 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
1080 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
1081 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
1082 OREAD4(sc, OHCI_BULK_HEAD_ED),
1083 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
1084 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
1085 OREAD4(sc, OHCI_DONE_HEAD),
1086 OREAD4(sc, OHCI_FM_INTERVAL),
1087 OREAD4(sc, OHCI_FM_REMAINING)));
1088 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
1089 OREAD4(sc, OHCI_FM_NUMBER),
1090 OREAD4(sc, OHCI_PERIODIC_START),
1091 OREAD4(sc, OHCI_LS_THRESHOLD)));
1092 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n",
1093 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
1094 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
1095 OREAD4(sc, OHCI_RH_STATUS)));
1096 DPRINTF((" port1=0x%08x port2=0x%08x\n",
1097 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
1098 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
1099 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n",
1100 le32toh(sc->sc_hcca->hcca_frame_number),
1101 le32toh(sc->sc_hcca->hcca_done_head)));
1105 Static int ohci_intr1(ohci_softc_t *);
1110 ohci_softc_t *sc = p;
1112 if (sc == NULL || sc->sc_dying)
1115 /* If we get an interrupt while polling, then just ignore it. */
1116 if (sc->sc_bus.use_polling) {
1118 printf("ohci_intr: ignored interrupt while polling\n");
1123 return (ohci_intr1(sc));
1127 ohci_intr1(ohci_softc_t *sc)
1129 u_int32_t intrs, eintrs;
1130 ohci_physaddr_t done;
1132 DPRINTFN(14,("ohci_intr1: enter\n"));
1134 /* In case the interrupt occurs before initialization has completed. */
1135 if (sc == NULL || sc->sc_hcca == NULL) {
1137 printf("ohci_intr: sc->sc_hcca == NULL\n");
1143 done = le32toh(sc->sc_hcca->hcca_done_head);
1145 /* The LSb of done is used to inform the HC Driver that an interrupt
1146 * condition exists for both the Done list and for another event
1147 * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
1148 * Driver checks the HccaDoneHead Value. If this value is 0, then the
1149 * interrupt was caused by other than the HccaDoneHead update and the
1150 * HcInterruptStatus register needs to be accessed to determine that
1151 * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
1152 * update interrupt is indicated and if the LSb of done is nonzero,
1153 * then an additional interrupt event is indicated and
1154 * HcInterruptStatus should be checked to determine its cause.
1157 if (done & ~OHCI_DONE_INTRS)
1159 if (done & OHCI_DONE_INTRS) {
1160 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1161 done &= ~OHCI_DONE_INTRS;
1163 sc->sc_hcca->hcca_done_head = 0;
1165 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1167 if (intrs == 0) /* nothing to be done (PCI shared interrupt) */
1171 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1172 eintrs = intrs & sc->sc_eintrs;
1176 sc->sc_bus.intr_context++;
1177 sc->sc_bus.no_intrs++;
1178 DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
1179 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1182 if (eintrs & OHCI_SO) {
1183 sc->sc_overrun_cnt++;
1184 if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
1185 printf("%s: %u scheduling overruns\n",
1186 USBDEVNAME(sc->sc_bus.bdev), sc->sc_overrun_cnt);
1187 sc->sc_overrun_cnt = 0;
1192 if (eintrs & OHCI_WDH) {
1193 ohci_add_done(sc, done &~ OHCI_DONE_INTRS);
1194 usb_schedsoftintr(&sc->sc_bus);
1195 eintrs &= ~OHCI_WDH;
1197 if (eintrs & OHCI_RD) {
1198 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1199 /* XXX process resume detect */
1201 if (eintrs & OHCI_UE) {
1202 printf("%s: unrecoverable error, controller halted\n",
1203 USBDEVNAME(sc->sc_bus.bdev));
1204 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1207 if (eintrs & OHCI_RHSC) {
1208 ohci_rhsc(sc, sc->sc_intrxfer);
1210 * Disable RHSC interrupt for now, because it will be
1211 * on until the port has been reset.
1213 ohci_rhsc_able(sc, 0);
1214 /* Do not allow RHSC interrupts > 1 per second */
1215 usb_callout(sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);
1216 eintrs &= ~OHCI_RHSC;
1219 sc->sc_bus.intr_context--;
1222 /* Block unprocessed interrupts. XXX */
1223 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
1224 sc->sc_eintrs &= ~eintrs;
1225 printf("%s: blocking intrs 0x%x\n",
1226 USBDEVNAME(sc->sc_bus.bdev), eintrs);
1233 ohci_rhsc_able(ohci_softc_t *sc, int on)
1235 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1237 sc->sc_eintrs |= OHCI_RHSC;
1238 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1240 sc->sc_eintrs &= ~OHCI_RHSC;
1241 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1246 ohci_rhsc_enable(void *v_sc)
1248 ohci_softc_t *sc = v_sc;
1250 ohci_rhsc_able(sc, 1);
1254 char *ohci_cc_strs[] = {
1258 "DATA_TOGGLE_MISMATCH",
1260 "DEVICE_NOT_RESPONDING",
1261 "PID_CHECK_FAILURE",
1275 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done)
1277 ohci_soft_itd_t *sitd, *sidone, **ip;
1278 ohci_soft_td_t *std, *sdone, **p;
1280 /* Reverse the done list. */
1281 for (sdone = NULL, sidone = NULL; done != 0; ) {
1282 std = ohci_hash_find_td(sc, done);
1285 done = le32toh(std->td.td_nexttd);
1287 DPRINTFN(10,("add TD %p\n", std));
1290 sitd = ohci_hash_find_itd(sc, done);
1292 sitd->dnext = sidone;
1293 done = le32toh(sitd->itd.itd_nextitd);
1295 DPRINTFN(5,("add ITD %p\n", sitd));
1298 panic("ohci_add_done: addr 0x%08lx not found\n", (u_long)done);
1301 /* sdone & sidone now hold the done lists. */
1302 /* Put them on the already processed lists. */
1303 for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext)
1306 for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext)
1312 ohci_softintr(void *v)
1314 ohci_softc_t *sc = v;
1315 ohci_soft_itd_t *sitd, *sidone, *sitdnext;
1316 ohci_soft_td_t *std, *sdone, *stdnext;
1317 usbd_xfer_handle xfer;
1320 DPRINTFN(10,("ohci_softintr: enter\n:"));
1322 sc->sc_bus.intr_context++;
1325 sdone = sc->sc_sdone;
1326 sc->sc_sdone = NULL;
1327 sidone = sc->sc_sidone;
1328 sc->sc_sidone = NULL;
1331 DPRINTFN(10,("ohci_softintr: sdone=%p sidone=%p\n", sdone, sidone));
1334 if (ohcidebug > 10) {
1335 DPRINTF(("ohci_process_done: TD done:\n"));
1336 ohci_dump_tds(sdone);
1340 for (std = sdone; std; std = stdnext) {
1342 stdnext = std->dnext;
1343 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1344 std, xfer, (xfer ? xfer->hcpriv : NULL)));
1345 if (xfer == NULL || (std->flags & OHCI_TD_HANDLED)) {
1347 * xfer == NULL: There seems to be no xfer associated
1348 * with this TD. It is tailp that happened to end up on
1350 * flags & OHCI_TD_HANDLED: The TD has already been
1351 * handled by process_done and should not be done again.
1352 * Shouldn't happen, but some chips are broken(?).
1356 if (xfer->status == USBD_CANCELLED ||
1357 xfer->status == USBD_TIMEOUT) {
1358 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1360 /* Handled by abort routine. */
1363 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
1364 cc = OHCI_TD_GET_CC(le32toh(std->td.td_flags));
1365 if (cc == OHCI_CC_NO_ERROR) {
1367 if (std->td.td_cbp != 0)
1368 len -= le32toh(std->td.td_be) -
1369 le32toh(std->td.td_cbp) + 1;
1370 DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n",
1372 if (std->flags & OHCI_ADD_LEN)
1373 xfer->actlen += len;
1374 if (std->flags & OHCI_CALL_DONE) {
1375 xfer->status = USBD_NORMAL_COMPLETION;
1376 usb_transfer_complete(xfer);
1378 ohci_free_std(sc, std);
1381 * Endpoint is halted. First unlink all the TDs
1382 * belonging to the failed transfer, and then restart
1385 ohci_soft_td_t *p, *n;
1386 struct ohci_pipe *opipe =
1387 (struct ohci_pipe *)xfer->pipe;
1389 DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1390 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1391 ohci_cc_strs[OHCI_TD_GET_CC(le32toh(std->td.td_flags))]));
1394 /* Mark all the TDs in the done queue for the current
1397 for (p = stdnext; p; p = p->dnext) {
1398 if (p->xfer == xfer)
1399 p->flags |= OHCI_TD_HANDLED;
1403 for (p = std; p->xfer == xfer; p = n) {
1405 ohci_free_std(sc, p);
1409 opipe->sed->ed.ed_headp = htole32(p->physaddr);
1410 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1412 if (cc == OHCI_CC_STALL)
1413 xfer->status = USBD_STALLED;
1415 xfer->status = USBD_IOERROR;
1416 usb_transfer_complete(xfer);
1421 if (ohcidebug > 10) {
1422 DPRINTF(("ohci_softintr: ITD done:\n"));
1423 ohci_dump_itds(sidone);
1427 for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
1429 sitdnext = sitd->dnext;
1430 DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n",
1431 sitd, xfer, xfer ? xfer->hcpriv : 0));
1434 if (xfer->status == USBD_CANCELLED ||
1435 xfer->status == USBD_TIMEOUT) {
1436 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1438 /* Handled by abort routine. */
1443 printf("ohci_softintr: sitd=%p is done\n", sitd);
1446 cc = OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags));
1447 if (cc == OHCI_CC_NO_ERROR) {
1448 /* XXX compute length for input */
1449 struct ohci_pipe *opipe =
1450 (struct ohci_pipe *)xfer->pipe;
1451 if (sitd->flags & OHCI_CALL_DONE) {
1452 opipe->u.iso.inuse -= xfer->nframes;
1453 /* XXX update frlengths with actual length */
1454 /* XXX xfer->actlen = actlen; */
1455 xfer->status = USBD_NORMAL_COMPLETION;
1456 usb_transfer_complete(xfer);
1460 xfer->status = USBD_IOERROR;
1461 usb_transfer_complete(xfer);
1465 #ifdef USB_USE_SOFTINTR
1466 if (sc->sc_softwake) {
1467 sc->sc_softwake = 0;
1468 wakeup(&sc->sc_softwake);
1470 #endif /* USB_USE_SOFTINTR */
1472 sc->sc_bus.intr_context--;
1473 DPRINTFN(10,("ohci_softintr: done:\n"));
1477 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1479 DPRINTFN(10,("ohci_ctrl_done: xfer=%p\n", xfer));
1482 if (!(xfer->rqflags & URQ_REQUEST)) {
1483 panic("ohci_ctrl_done: not a request\n");
1486 xfer->hcpriv = NULL;
1490 ohci_device_intr_done(usbd_xfer_handle xfer)
1492 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1493 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1494 ohci_soft_ed_t *sed = opipe->sed;
1495 ohci_soft_td_t *data, *tail;
1498 DPRINTFN(10,("ohci_intr_done: xfer=%p, actlen=%d\n",
1499 xfer, xfer->actlen));
1501 xfer->hcpriv = NULL;
1503 if (xfer->pipe->repeat) {
1504 data = opipe->tail.td;
1505 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1507 xfer->status = USBD_NOMEM;
1512 data->td.td_flags = htole32(
1513 OHCI_TD_IN | OHCI_TD_NOCC |
1514 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1515 if (xfer->flags & USBD_SHORT_XFER_OK)
1516 data->td.td_flags |= htole32(OHCI_TD_R);
1517 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
1518 data->nexttd = tail;
1519 data->td.td_nexttd = htole32(tail->physaddr);
1520 data->td.td_be = htole32(le32toh(data->td.td_cbp) +
1522 data->len = xfer->length;
1524 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1525 xfer->hcpriv = data;
1528 sed->ed.ed_tailp = htole32(tail->physaddr);
1529 opipe->tail.td = tail;
1534 ohci_device_bulk_done(usbd_xfer_handle xfer)
1536 DPRINTFN(10,("ohci_bulk_done: xfer=%p, actlen=%d\n",
1537 xfer, xfer->actlen));
1539 xfer->hcpriv = NULL;
1543 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1545 usbd_pipe_handle pipe;
1546 struct ohci_pipe *opipe;
1551 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1552 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1553 sc, xfer, hstatus));
1556 /* Just ignore the change. */
1561 opipe = (struct ohci_pipe *)pipe;
1563 p = KERNADDR(&xfer->dmabuf, 0);
1564 m = min(sc->sc_noport, xfer->length * 8 - 1);
1565 memset(p, 0, xfer->length);
1566 for (i = 1; i <= m; i++) {
1567 /* Pick out CHANGE bits from the status reg. */
1568 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1569 p[i/8] |= 1 << (i%8);
1571 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1572 xfer->actlen = xfer->length;
1573 xfer->status = USBD_NORMAL_COMPLETION;
1575 usb_transfer_complete(xfer);
1579 ohci_root_intr_done(usbd_xfer_handle xfer)
1581 xfer->hcpriv = NULL;
1585 ohci_root_ctrl_done(usbd_xfer_handle xfer)
1587 xfer->hcpriv = NULL;
1591 * Wait here until controller claims to have an interrupt.
1592 * Then call ohci_intr and return. Use timeout to avoid waiting
1596 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1598 int timo = xfer->timeout;
1602 xfer->status = USBD_IN_PROGRESS;
1603 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1604 usb_delay_ms(&sc->sc_bus, 1);
1607 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1608 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1615 if (xfer->status != USBD_IN_PROGRESS)
1621 DPRINTF(("ohci_waitintr: timeout\n"));
1622 xfer->status = USBD_TIMEOUT;
1623 usb_transfer_complete(xfer);
1624 /* XXX should free TD */
1628 ohci_poll(struct usbd_bus *bus)
1630 ohci_softc_t *sc = (ohci_softc_t *)bus;
1634 new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1636 DPRINTFN(10,("ohci_poll: intrs=0x%04x\n", new));
1641 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1646 ohci_device_request(usbd_xfer_handle xfer)
1648 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1649 usb_device_request_t *req = &xfer->request;
1650 usbd_device_handle dev = opipe->pipe.device;
1651 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1652 int addr = dev->address;
1653 ohci_soft_td_t *data = 0;
1654 ohci_soft_td_t *setup, *stat, *next, *tail;
1655 ohci_soft_ed_t *sed;
1661 isread = req->bmRequestType & UT_READ;
1662 len = UGETW(req->wLength);
1664 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1665 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1666 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1667 UGETW(req->wIndex), len, addr,
1668 opipe->pipe.endpoint->edesc->bEndpointAddress));
1670 setup = opipe->tail.td;
1671 stat = ohci_alloc_std(sc);
1676 tail = ohci_alloc_std(sc);
1684 opipe->u.ctl.length = len;
1686 /* Update device address and length since they may have changed. */
1687 /* XXX This only needs to be done once, but it's too early in open. */
1688 /* XXXX Should not touch ED here! */
1689 sed->ed.ed_flags = htole32(
1690 (le32toh(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1691 OHCI_ED_SET_FA(addr) |
1692 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1694 /* Set up data transaction */
1696 data = ohci_alloc_std(sc);
1701 data->td.td_flags = htole32(
1702 (isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
1703 OHCI_TD_TOGGLE_1 | OHCI_TD_NOINTR |
1704 (xfer->flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0));
1705 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
1706 data->nexttd = stat;
1707 data->td.td_nexttd = htole32(stat->physaddr);
1708 data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1);
1711 data->flags = OHCI_ADD_LEN;
1714 stat->flags = OHCI_CALL_DONE;
1718 stat->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1721 memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1723 setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1724 OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1725 setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma, 0));
1726 setup->nexttd = next;
1727 setup->td.td_nexttd = htole32(next->physaddr);
1728 setup->td.td_be = htole32(le32toh(setup->td.td_cbp) + sizeof *req - 1);
1732 xfer->hcpriv = setup;
1734 stat->td.td_flags = htole32(
1735 (isread ? OHCI_TD_OUT : OHCI_TD_IN) |
1736 OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1737 stat->td.td_cbp = 0;
1738 stat->nexttd = tail;
1739 stat->td.td_nexttd = htole32(tail->physaddr);
1745 if (ohcidebug > 5) {
1746 DPRINTF(("ohci_device_request:\n"));
1748 ohci_dump_tds(setup);
1752 /* Insert ED in schedule */
1754 sed->ed.ed_tailp = htole32(tail->physaddr);
1755 opipe->tail.td = tail;
1756 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1757 if (xfer->timeout && !sc->sc_bus.use_polling) {
1758 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1759 ohci_timeout, xfer);
1764 if (ohcidebug > 20) {
1766 DPRINTF(("ohci_device_request: status=%x\n",
1767 OREAD4(sc, OHCI_COMMAND_STATUS)));
1769 printf("ctrl head:\n");
1770 ohci_dump_ed(sc->sc_ctrl_head);
1773 ohci_dump_tds(setup);
1777 return (USBD_NORMAL_COMPLETION);
1780 ohci_free_std(sc, tail);
1782 ohci_free_std(sc, stat);
1788 * Add an ED to the schedule. Called at splusb().
1791 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1793 DPRINTFN(8,("ohci_add_ed: sed=%p head=%p\n", sed, head));
1796 sed->next = head->next;
1797 sed->ed.ed_nexted = head->ed.ed_nexted;
1799 head->ed.ed_nexted = htole32(sed->physaddr);
1803 * Remove an ED from the schedule. Called at splusb().
1806 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1813 for (p = head; p == NULL && p->next != sed; p = p->next)
1816 panic("ohci_rem_ed: ED not found\n");
1817 p->next = sed->next;
1818 p->ed.ed_nexted = sed->ed.ed_nexted;
1822 * When a transfer is completed the TD is added to the done queue by
1823 * the host controller. This queue is the processed by software.
1824 * Unfortunately the queue contains the physical address of the TD
1825 * and we have no simple way to translate this back to a kernel address.
1826 * To make the translation possible (and fast) we use a hash table of
1827 * TDs currently in the schedule. The physical address is used as the
1831 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1832 /* Called at splusb() */
1834 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1836 int h = HASH(std->physaddr);
1840 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1843 /* Called at splusb() */
1845 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1849 LIST_REMOVE(std, hnext);
1853 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1856 ohci_soft_td_t *std;
1858 /* if these are present they should be masked out at an earlier
1861 KASSERT((a&~OHCI_HEADMASK) == 0, ("%s: 0x%b has lower bits set\n",
1862 USBDEVNAME(sc->sc_bus.bdev),
1863 (int) a, "\20\1HALT\2TOGGLE"));
1865 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1867 std = LIST_NEXT(std, hnext))
1868 if (std->physaddr == a)
1871 DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1872 USBDEVNAME(sc->sc_bus.bdev), (u_long) a));
1876 /* Called at splusb() */
1878 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1880 int h = HASH(sitd->physaddr);
1884 DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n",
1885 sitd, (u_long)sitd->physaddr));
1887 LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
1890 /* Called at splusb() */
1892 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1896 DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n",
1897 sitd, (u_long)sitd->physaddr));
1899 LIST_REMOVE(sitd, hnext);
1903 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
1906 ohci_soft_itd_t *sitd;
1908 for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
1910 sitd = LIST_NEXT(sitd, hnext))
1911 if (sitd->physaddr == a)
1917 ohci_timeout(void *addr)
1919 struct ohci_xfer *oxfer = addr;
1920 struct ohci_pipe *opipe = (struct ohci_pipe *)oxfer->xfer.pipe;
1921 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1923 DPRINTF(("ohci_timeout: oxfer=%p\n", oxfer));
1926 ohci_abort_xfer(&oxfer->xfer, USBD_TIMEOUT);
1930 /* Execute the abort in a process context. */
1931 usb_init_task(&oxfer->abort_task, ohci_timeout_task, addr);
1932 usb_add_task(oxfer->xfer.pipe->device, &oxfer->abort_task);
1936 ohci_timeout_task(void *addr)
1938 usbd_xfer_handle xfer = addr;
1941 DPRINTF(("ohci_timeout_task: xfer=%p\n", xfer));
1944 ohci_abort_xfer(xfer, USBD_TIMEOUT);
1950 ohci_dump_tds(ohci_soft_td_t *std)
1952 for (; std; std = std->nexttd)
1957 ohci_dump_td(ohci_soft_td_t *std)
1961 bitmask_snprintf((u_int32_t)le32toh(std->td.td_flags),
1962 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1963 sbuf, sizeof(sbuf));
1965 printf("TD(%p) at %08lx: %s delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1966 "nexttd=0x%08lx be=0x%08lx\n",
1967 std, (u_long)std->physaddr, sbuf,
1968 OHCI_TD_GET_DI(le32toh(std->td.td_flags)),
1969 OHCI_TD_GET_EC(le32toh(std->td.td_flags)),
1970 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1971 (u_long)le32toh(std->td.td_cbp),
1972 (u_long)le32toh(std->td.td_nexttd),
1973 (u_long)le32toh(std->td.td_be));
1977 ohci_dump_itd(ohci_soft_itd_t *sitd)
1981 printf("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n"
1982 "bp0=0x%08lx next=0x%08lx be=0x%08lx\n",
1983 sitd, (u_long)sitd->physaddr,
1984 OHCI_ITD_GET_SF(le32toh(sitd->itd.itd_flags)),
1985 OHCI_ITD_GET_DI(le32toh(sitd->itd.itd_flags)),
1986 OHCI_ITD_GET_FC(le32toh(sitd->itd.itd_flags)),
1987 OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags)),
1988 (u_long)le32toh(sitd->itd.itd_bp0),
1989 (u_long)le32toh(sitd->itd.itd_nextitd),
1990 (u_long)le32toh(sitd->itd.itd_be));
1991 for (i = 0; i < OHCI_ITD_NOFFSET; i++)
1992 printf("offs[%d]=0x%04x ", i,
1993 (u_int)le16toh(sitd->itd.itd_offset[i]));
1998 ohci_dump_itds(ohci_soft_itd_t *sitd)
2000 for (; sitd; sitd = sitd->nextitd)
2001 ohci_dump_itd(sitd);
2005 ohci_dump_ed(ohci_soft_ed_t *sed)
2007 char sbuf[128], sbuf2[128];
2009 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_flags),
2010 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
2011 sbuf, sizeof(sbuf));
2012 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_headp),
2013 "\20\1HALT\2CARRY", sbuf2, sizeof(sbuf2));
2015 printf("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d flags=%s\ntailp=0x%08lx "
2016 "headflags=%s headp=0x%08lx nexted=0x%08lx\n",
2017 sed, (u_long)sed->physaddr,
2018 OHCI_ED_GET_FA(le32toh(sed->ed.ed_flags)),
2019 OHCI_ED_GET_EN(le32toh(sed->ed.ed_flags)),
2020 OHCI_ED_GET_MAXP(le32toh(sed->ed.ed_flags)), sbuf,
2021 (u_long)le32toh(sed->ed.ed_tailp), sbuf2,
2022 (u_long)le32toh(sed->ed.ed_headp),
2023 (u_long)le32toh(sed->ed.ed_nexted));
2028 ohci_open(usbd_pipe_handle pipe)
2030 usbd_device_handle dev = pipe->device;
2031 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2032 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2033 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2034 u_int8_t addr = dev->address;
2035 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
2036 ohci_soft_ed_t *sed;
2037 ohci_soft_td_t *std;
2038 ohci_soft_itd_t *sitd;
2039 ohci_physaddr_t tdphys;
2045 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2046 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
2049 return (USBD_IOERROR);
2054 if (addr == sc->sc_addr) {
2055 switch (ed->bEndpointAddress) {
2056 case USB_CONTROL_ENDPOINT:
2057 pipe->methods = &ohci_root_ctrl_methods;
2059 case UE_DIR_IN | OHCI_INTR_ENDPT:
2060 pipe->methods = &ohci_root_intr_methods;
2063 return (USBD_INVAL);
2066 sed = ohci_alloc_sed(sc);
2070 if (xfertype == UE_ISOCHRONOUS) {
2071 sitd = ohci_alloc_sitd(sc);
2074 opipe->tail.itd = sitd;
2075 tdphys = sitd->physaddr;
2076 fmt = OHCI_ED_FORMAT_ISO;
2077 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
2078 fmt |= OHCI_ED_DIR_IN;
2080 fmt |= OHCI_ED_DIR_OUT;
2082 std = ohci_alloc_std(sc);
2085 opipe->tail.td = std;
2086 tdphys = std->physaddr;
2087 fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
2089 sed->ed.ed_flags = htole32(
2090 OHCI_ED_SET_FA(addr) |
2091 OHCI_ED_SET_EN(ed->bEndpointAddress) |
2092 (dev->speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) |
2094 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
2095 sed->ed.ed_headp = sed->ed.ed_tailp = htole32(tdphys);
2099 pipe->methods = &ohci_device_ctrl_methods;
2100 err = usb_allocmem(&sc->sc_bus,
2101 sizeof(usb_device_request_t),
2102 0, &opipe->u.ctl.reqdma);
2106 ohci_add_ed(sed, sc->sc_ctrl_head);
2110 pipe->methods = &ohci_device_intr_methods;
2111 ival = pipe->interval;
2112 if (ival == USBD_DEFAULT_INTERVAL)
2113 ival = ed->bInterval;
2114 return (ohci_device_setintr(sc, opipe, ival));
2115 case UE_ISOCHRONOUS:
2116 pipe->methods = &ohci_device_isoc_methods;
2117 return (ohci_setup_isoc(pipe));
2119 pipe->methods = &ohci_device_bulk_methods;
2121 ohci_add_ed(sed, sc->sc_bulk_head);
2126 return (USBD_NORMAL_COMPLETION);
2130 ohci_free_std(sc, std);
2133 ohci_free_sed(sc, sed);
2135 return (USBD_NOMEM);
2140 * Close a reqular pipe.
2141 * Assumes that there are no pending transactions.
2144 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
2146 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2147 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2148 ohci_soft_ed_t *sed = opipe->sed;
2153 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2154 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2155 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK)) {
2156 ohci_soft_td_t *std;
2157 std = ohci_hash_find_td(sc, le32toh(sed->ed.ed_headp));
2158 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
2159 "tl=0x%x pipe=%p, std=%p\n", sed,
2160 (int)le32toh(sed->ed.ed_headp),
2161 (int)le32toh(sed->ed.ed_tailp),
2164 usbd_dump_pipe(&opipe->pipe);
2171 usb_delay_ms(&sc->sc_bus, 2);
2172 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2173 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2174 printf("ohci_close_pipe: pipe still not empty\n");
2177 ohci_rem_ed(sed, head);
2179 ohci_free_sed(sc, opipe->sed);
2183 * Abort a device request.
2184 * If this routine is called at splusb() it guarantees that the request
2185 * will be removed from the hardware scheduling and that the callback
2186 * for it will be called with USBD_CANCELLED status.
2187 * It's impossible to guarantee that the requested transfer will not
2188 * have happened since the hardware runs concurrently.
2189 * If the transaction has already happened we rely on the ordinary
2190 * interrupt processing to process it.
2193 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2195 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2196 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
2197 ohci_soft_ed_t *sed = opipe->sed;
2198 ohci_soft_td_t *p, *n;
2199 ohci_physaddr_t headp;
2202 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p sed=%p\n", xfer, opipe,sed));
2205 /* If we're dying, just do the software part. */
2207 xfer->status = status; /* make software ignore it */
2208 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
2209 usb_transfer_complete(xfer);
2213 if (xfer->device->bus->intr_context || !curproc)
2214 panic("ohci_abort_xfer: not in process context\n");
2217 * Step 1: Make interrupt routine and hardware ignore xfer.
2220 xfer->status = status; /* make software ignore it */
2221 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
2223 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
2224 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
2227 * Step 2: Wait until we know hardware has finished any possible
2228 * use of the xfer. Also make sure the soft interrupt routine
2231 usb_delay_ms(opipe->pipe.device->bus, 20); /* Hardware finishes in 1ms */
2233 #ifdef USB_USE_SOFTINTR
2234 sc->sc_softwake = 1;
2235 #endif /* USB_USE_SOFTINTR */
2236 usb_schedsoftintr(&sc->sc_bus);
2237 #ifdef USB_USE_SOFTINTR
2238 tsleep(&sc->sc_softwake, PZERO, "ohciab", 0);
2239 #endif /* USB_USE_SOFTINTR */
2243 * Step 3: Remove any vestiges of the xfer from the hardware.
2244 * The complication here is that the hardware may have executed
2245 * beyond the xfer we're trying to abort. So as we're scanning
2246 * the TDs of this xfer we check if the hardware points to
2249 s = splusb(); /* XXX why? */
2254 printf("ohci_abort_xfer: hcpriv is NULL\n");
2259 if (ohcidebug > 1) {
2260 DPRINTF(("ohci_abort_xfer: sed=\n"));
2265 headp = le32toh(sed->ed.ed_headp) & OHCI_HEADMASK;
2267 for (; p->xfer == xfer; p = n) {
2268 hit |= headp == p->physaddr;
2270 ohci_free_std(sc, p);
2272 /* Zap headp register if hardware pointed inside the xfer. */
2274 DPRINTFN(1,("ohci_abort_xfer: set hd=0x08%x, tl=0x%08x\n",
2275 (int)p->physaddr, (int)le32toh(sed->ed.ed_tailp)));
2276 sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */
2278 DPRINTFN(1,("ohci_abort_xfer: no hit\n"));
2282 * Step 4: Turn on hardware again.
2284 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
2287 * Step 5: Execute callback.
2289 usb_transfer_complete(xfer);
2295 * Data structures and routines to emulate the root hub.
2297 Static usb_device_descriptor_t ohci_devd = {
2298 USB_DEVICE_DESCRIPTOR_SIZE,
2299 UDESC_DEVICE, /* type */
2300 {0x00, 0x01}, /* USB version */
2301 UDCLASS_HUB, /* class */
2302 UDSUBCLASS_HUB, /* subclass */
2303 UDPROTO_FSHUB, /* protocol */
2304 64, /* max packet */
2305 {0},{0},{0x00,0x01}, /* device id */
2306 1,2,0, /* string indicies */
2307 1 /* # of configurations */
2310 Static usb_config_descriptor_t ohci_confd = {
2311 USB_CONFIG_DESCRIPTOR_SIZE,
2313 {USB_CONFIG_DESCRIPTOR_SIZE +
2314 USB_INTERFACE_DESCRIPTOR_SIZE +
2315 USB_ENDPOINT_DESCRIPTOR_SIZE},
2323 Static usb_interface_descriptor_t ohci_ifcd = {
2324 USB_INTERFACE_DESCRIPTOR_SIZE,
2335 Static usb_endpoint_descriptor_t ohci_endpd = {
2336 USB_ENDPOINT_DESCRIPTOR_SIZE,
2338 UE_DIR_IN | OHCI_INTR_ENDPT,
2340 {8, 0}, /* max packet */
2344 Static usb_hub_descriptor_t ohci_hubd = {
2345 USB_HUB_DESCRIPTOR_SIZE,
2355 ohci_str(usb_string_descriptor_t *p, int l, const char *s)
2361 p->bLength = 2 * strlen(s) + 2;
2364 p->bDescriptorType = UDESC_STRING;
2366 for (i = 0; s[i] && l > 1; i++, l -= 2)
2367 USETW2(p->bString[i], 0, s[i]);
2372 * Simulate a hardware hub by handling all the necessary requests.
2375 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
2379 /* Insert last in queue. */
2380 err = usb_insert_transfer(xfer);
2384 /* Pipe isn't running, start first */
2385 return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2389 ohci_root_ctrl_start(usbd_xfer_handle xfer)
2391 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2392 usb_device_request_t *req;
2395 int s, len, value, index, l, totlen = 0;
2396 usb_port_status_t ps;
2397 usb_hub_descriptor_t hubd;
2402 return (USBD_IOERROR);
2405 if (!(xfer->rqflags & URQ_REQUEST))
2407 return (USBD_INVAL);
2409 req = &xfer->request;
2411 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2412 req->bmRequestType, req->bRequest));
2414 len = UGETW(req->wLength);
2415 value = UGETW(req->wValue);
2416 index = UGETW(req->wIndex);
2419 buf = KERNADDR(&xfer->dmabuf, 0);
2421 #define C(x,y) ((x) | ((y) << 8))
2422 switch(C(req->bRequest, req->bmRequestType)) {
2423 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2424 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2425 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2427 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2428 * for the integrated root hub.
2431 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2433 *(u_int8_t *)buf = sc->sc_conf;
2437 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2438 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2439 switch(value >> 8) {
2441 if ((value & 0xff) != 0) {
2445 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2446 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2447 memcpy(buf, &ohci_devd, l);
2450 if ((value & 0xff) != 0) {
2454 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2455 memcpy(buf, &ohci_confd, l);
2456 buf = (char *)buf + l;
2458 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2460 memcpy(buf, &ohci_ifcd, l);
2461 buf = (char *)buf + l;
2463 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2465 memcpy(buf, &ohci_endpd, l);
2470 *(u_int8_t *)buf = 0;
2472 switch (value & 0xff) {
2473 case 1: /* Vendor */
2474 totlen = ohci_str(buf, len, sc->sc_vendor);
2476 case 2: /* Product */
2477 totlen = ohci_str(buf, len, "OHCI root hub");
2486 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2488 *(u_int8_t *)buf = 0;
2492 case C(UR_GET_STATUS, UT_READ_DEVICE):
2494 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2498 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2499 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2501 USETW(((usb_status_t *)buf)->wStatus, 0);
2505 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2506 if (value >= USB_MAX_DEVICES) {
2510 sc->sc_addr = value;
2512 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2513 if (value != 0 && value != 1) {
2517 sc->sc_conf = value;
2519 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2521 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2522 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2523 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2526 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2528 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2531 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2533 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2534 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2535 "port=%d feature=%d\n",
2537 if (index < 1 || index > sc->sc_noport) {
2541 port = OHCI_RH_PORT_STATUS(index);
2543 case UHF_PORT_ENABLE:
2544 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2546 case UHF_PORT_SUSPEND:
2547 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2549 case UHF_PORT_POWER:
2550 /* Yes, writing to the LOW_SPEED bit clears power. */
2551 OWRITE4(sc, port, UPS_LOW_SPEED);
2553 case UHF_C_PORT_CONNECTION:
2554 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2556 case UHF_C_PORT_ENABLE:
2557 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2559 case UHF_C_PORT_SUSPEND:
2560 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2562 case UHF_C_PORT_OVER_CURRENT:
2563 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2565 case UHF_C_PORT_RESET:
2566 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2573 case UHF_C_PORT_CONNECTION:
2574 case UHF_C_PORT_ENABLE:
2575 case UHF_C_PORT_SUSPEND:
2576 case UHF_C_PORT_OVER_CURRENT:
2577 case UHF_C_PORT_RESET:
2578 /* Enable RHSC interrupt if condition is cleared. */
2579 if ((OREAD4(sc, port) >> 16) == 0)
2580 ohci_rhsc_able(sc, 1);
2586 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2591 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2593 hubd.bNbrPorts = sc->sc_noport;
2594 USETW(hubd.wHubCharacteristics,
2595 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2596 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2597 /* XXX overcurrent */
2599 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2600 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2601 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2602 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2603 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2604 l = min(len, hubd.bDescLength);
2606 memcpy(buf, &hubd, l);
2608 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2613 memset(buf, 0, len); /* ? XXX */
2616 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2617 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2619 if (index < 1 || index > sc->sc_noport) {
2627 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2628 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2630 USETW(ps.wPortStatus, v);
2631 USETW(ps.wPortChange, v >> 16);
2632 l = min(len, sizeof ps);
2633 memcpy(buf, &ps, l);
2636 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2639 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2641 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2642 if (index < 1 || index > sc->sc_noport) {
2646 port = OHCI_RH_PORT_STATUS(index);
2648 case UHF_PORT_ENABLE:
2649 OWRITE4(sc, port, UPS_PORT_ENABLED);
2651 case UHF_PORT_SUSPEND:
2652 OWRITE4(sc, port, UPS_SUSPEND);
2654 case UHF_PORT_RESET:
2655 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2657 OWRITE4(sc, port, UPS_RESET);
2658 for (i = 0; i < 5; i++) {
2659 usb_delay_ms(&sc->sc_bus,
2660 USB_PORT_ROOT_RESET_DELAY);
2665 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2668 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2669 index, OREAD4(sc, port)));
2671 case UHF_PORT_POWER:
2672 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2674 OWRITE4(sc, port, UPS_PORT_POWER);
2685 xfer->actlen = totlen;
2686 err = USBD_NORMAL_COMPLETION;
2690 usb_transfer_complete(xfer);
2692 return (USBD_IN_PROGRESS);
2695 /* Abort a root control request. */
2697 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2699 /* Nothing to do, all transfers are synchronous. */
2702 /* Close the root pipe. */
2704 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2706 DPRINTF(("ohci_root_ctrl_close\n"));
2707 /* Nothing to do. */
2711 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2715 /* Insert last in queue. */
2716 err = usb_insert_transfer(xfer);
2720 /* Pipe isn't running, start first */
2721 return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2725 ohci_root_intr_start(usbd_xfer_handle xfer)
2727 usbd_pipe_handle pipe = xfer->pipe;
2728 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2731 return (USBD_IOERROR);
2733 sc->sc_intrxfer = xfer;
2735 return (USBD_IN_PROGRESS);
2738 /* Abort a root interrupt request. */
2740 ohci_root_intr_abort(usbd_xfer_handle xfer)
2744 if (xfer->pipe->intrxfer == xfer) {
2745 DPRINTF(("ohci_root_intr_abort: remove\n"));
2746 xfer->pipe->intrxfer = NULL;
2748 xfer->status = USBD_CANCELLED;
2750 usb_transfer_complete(xfer);
2754 /* Close the root pipe. */
2756 ohci_root_intr_close(usbd_pipe_handle pipe)
2758 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2760 DPRINTF(("ohci_root_intr_close\n"));
2762 sc->sc_intrxfer = NULL;
2765 /************************/
2768 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2772 /* Insert last in queue. */
2773 err = usb_insert_transfer(xfer);
2777 /* Pipe isn't running, start first */
2778 return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2782 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2784 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2788 return (USBD_IOERROR);
2791 if (!(xfer->rqflags & URQ_REQUEST)) {
2793 printf("ohci_device_ctrl_transfer: not a request\n");
2794 return (USBD_INVAL);
2798 err = ohci_device_request(xfer);
2802 if (sc->sc_bus.use_polling)
2803 ohci_waitintr(sc, xfer);
2804 return (USBD_IN_PROGRESS);
2807 /* Abort a device control request. */
2809 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2811 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2812 ohci_abort_xfer(xfer, USBD_CANCELLED);
2815 /* Close a device control pipe. */
2817 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2819 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2820 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2822 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2823 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2824 ohci_free_std(sc, opipe->tail.td);
2827 /************************/
2830 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2832 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2834 opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY);
2838 ohci_noop(usbd_pipe_handle pipe)
2843 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2847 /* Insert last in queue. */
2848 err = usb_insert_transfer(xfer);
2852 /* Pipe isn't running, start first */
2853 return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2857 ohci_device_bulk_start(usbd_xfer_handle xfer)
2859 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2860 usbd_device_handle dev = opipe->pipe.device;
2861 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2862 int addr = dev->address;
2863 ohci_soft_td_t *data, *tail, *tdp;
2864 ohci_soft_ed_t *sed;
2865 int s, len, isread, endpt;
2869 return (USBD_IOERROR);
2872 if (xfer->rqflags & URQ_REQUEST) {
2874 printf("ohci_device_bulk_start: a request\n");
2875 return (USBD_INVAL);
2880 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2881 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2884 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2885 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2888 opipe->u.bulk.isread = isread;
2889 opipe->u.bulk.length = len;
2891 /* Update device address */
2892 sed->ed.ed_flags = htole32(
2893 (le32toh(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2894 OHCI_ED_SET_FA(addr));
2896 /* Allocate a chain of new TDs (including a new tail). */
2897 data = opipe->tail.td;
2898 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
2904 xfer->hcpriv = data;
2906 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2907 "td_cbp=0x%08x td_be=0x%08x\n",
2908 (int)le32toh(sed->ed.ed_flags),
2909 (int)le32toh(data->td.td_flags),
2910 (int)le32toh(data->td.td_cbp),
2911 (int)le32toh(data->td.td_be)));
2914 if (ohcidebug > 5) {
2916 ohci_dump_tds(data);
2920 /* Insert ED in schedule */
2922 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2925 sed->ed.ed_tailp = htole32(tail->physaddr);
2926 opipe->tail.td = tail;
2927 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
2928 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2929 if (xfer->timeout && !sc->sc_bus.use_polling) {
2930 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2931 ohci_timeout, xfer);
2935 /* This goes wrong if we are too slow. */
2936 if (ohcidebug > 10) {
2938 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2939 OREAD4(sc, OHCI_COMMAND_STATUS)));
2941 ohci_dump_tds(data);
2947 return (USBD_IN_PROGRESS);
2951 ohci_device_bulk_abort(usbd_xfer_handle xfer)
2953 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
2954 ohci_abort_xfer(xfer, USBD_CANCELLED);
2958 * Close a device bulk pipe.
2961 ohci_device_bulk_close(usbd_pipe_handle pipe)
2963 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2964 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2966 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
2967 ohci_close_pipe(pipe, sc->sc_bulk_head);
2968 ohci_free_std(sc, opipe->tail.td);
2971 /************************/
2974 ohci_device_intr_transfer(usbd_xfer_handle xfer)
2978 /* Insert last in queue. */
2979 err = usb_insert_transfer(xfer);
2983 /* Pipe isn't running, start first */
2984 return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2988 ohci_device_intr_start(usbd_xfer_handle xfer)
2990 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2991 usbd_device_handle dev = opipe->pipe.device;
2992 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2993 ohci_soft_ed_t *sed = opipe->sed;
2994 ohci_soft_td_t *data, *tail;
2999 return (USBD_IOERROR);
3001 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
3002 "flags=%d priv=%p\n",
3003 xfer, xfer->length, xfer->flags, xfer->priv));
3006 if (xfer->rqflags & URQ_REQUEST)
3007 panic("ohci_device_intr_transfer: a request\n");
3012 data = opipe->tail.td;
3013 tail = ohci_alloc_std(sc);
3015 return (USBD_NOMEM);
3018 data->td.td_flags = htole32(
3019 OHCI_TD_IN | OHCI_TD_NOCC |
3020 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
3021 if (xfer->flags & USBD_SHORT_XFER_OK)
3022 data->td.td_flags |= htole32(OHCI_TD_R);
3023 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
3024 data->nexttd = tail;
3025 data->td.td_nexttd = htole32(tail->physaddr);
3026 data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1);
3029 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
3030 xfer->hcpriv = data;
3033 if (ohcidebug > 5) {
3034 DPRINTF(("ohci_device_intr_transfer:\n"));
3036 ohci_dump_tds(data);
3040 /* Insert ED in schedule */
3042 sed->ed.ed_tailp = htole32(tail->physaddr);
3043 opipe->tail.td = tail;
3044 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3048 * This goes horribly wrong, printing thousands of descriptors,
3049 * because false references are followed due to the fact that the
3052 if (ohcidebug > 5) {
3053 usb_delay_ms(&sc->sc_bus, 5);
3054 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
3055 OREAD4(sc, OHCI_COMMAND_STATUS)));
3057 ohci_dump_tds(data);
3062 return (USBD_IN_PROGRESS);
3065 /* Abort a device control request. */
3067 ohci_device_intr_abort(usbd_xfer_handle xfer)
3069 if (xfer->pipe->intrxfer == xfer) {
3070 DPRINTF(("ohci_device_intr_abort: remove\n"));
3071 xfer->pipe->intrxfer = NULL;
3073 ohci_abort_xfer(xfer, USBD_CANCELLED);
3076 /* Close a device interrupt pipe. */
3078 ohci_device_intr_close(usbd_pipe_handle pipe)
3080 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3081 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3082 int nslots = opipe->u.intr.nslots;
3083 int pos = opipe->u.intr.pos;
3085 ohci_soft_ed_t *p, *sed = opipe->sed;
3088 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
3089 pipe, nslots, pos));
3091 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
3092 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3093 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3094 usb_delay_ms(&sc->sc_bus, 2);
3096 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3097 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3098 panic("%s: Intr pipe %p still has TDs queued\n",
3099 USBDEVNAME(sc->sc_bus.bdev), pipe);
3102 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
3106 panic("ohci_device_intr_close: ED not found\n");
3108 p->next = sed->next;
3109 p->ed.ed_nexted = sed->ed.ed_nexted;
3112 for (j = 0; j < nslots; j++)
3113 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
3115 ohci_free_std(sc, opipe->tail.td);
3116 ohci_free_sed(sc, opipe->sed);
3120 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
3123 u_int npoll, slow, shigh, nslots;
3125 ohci_soft_ed_t *hsed, *sed = opipe->sed;
3127 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
3129 printf("ohci_setintr: 0 interval\n");
3130 return (USBD_INVAL);
3133 npoll = OHCI_NO_INTRS;
3134 while (npoll > ival)
3136 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
3139 * We now know which level in the tree the ED must go into.
3140 * Figure out which slot has most bandwidth left over.
3146 * 8 7 8 9 10 11 12 13 14
3147 * N (N-1) .. (N-1+N-1)
3150 shigh = slow + npoll;
3151 nslots = OHCI_NO_INTRS / npoll;
3152 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
3154 for (j = 0; j < nslots; j++)
3155 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
3161 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
3162 best, slow, shigh, bestbw));
3165 hsed = sc->sc_eds[best];
3166 sed->next = hsed->next;
3167 sed->ed.ed_nexted = hsed->ed.ed_nexted;
3169 hsed->ed.ed_nexted = htole32(sed->physaddr);
3172 for (j = 0; j < nslots; j++)
3173 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
3174 opipe->u.intr.nslots = nslots;
3175 opipe->u.intr.pos = best;
3177 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
3178 return (USBD_NORMAL_COMPLETION);
3181 /***********************/
3184 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
3188 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
3190 /* Put it on our queue, */
3191 err = usb_insert_transfer(xfer);
3193 /* bail out on error, */
3194 if (err && err != USBD_IN_PROGRESS)
3197 /* XXX should check inuse here */
3199 /* insert into schedule, */
3200 ohci_device_isoc_enter(xfer);
3202 /* and start if the pipe wasn't running */
3204 ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
3210 ohci_device_isoc_enter(usbd_xfer_handle xfer)
3212 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3213 usbd_device_handle dev = opipe->pipe.device;
3214 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3215 ohci_soft_ed_t *sed = opipe->sed;
3216 struct iso *iso = &opipe->u.iso;
3217 ohci_soft_itd_t *sitd, *nsitd;
3218 ohci_physaddr_t buf, offs, noffs, bp0;
3219 int i, ncur, nframes;
3222 DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p "
3224 iso->inuse, iso->next, xfer, xfer->nframes));
3229 if (iso->next == -1) {
3230 /* Not in use yet, schedule it a few frames ahead. */
3231 iso->next = le32toh(sc->sc_hcca->hcca_frame_number) + 5;
3232 DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n",
3236 sitd = opipe->tail.itd;
3237 buf = DMAADDR(&xfer->dmabuf, 0);
3238 bp0 = OHCI_PAGE(buf);
3239 offs = OHCI_PAGE_OFFSET(buf);
3240 nframes = xfer->nframes;
3241 xfer->hcpriv = sitd;
3242 for (i = ncur = 0; i < nframes; i++, ncur++) {
3243 noffs = offs + xfer->frlengths[i];
3244 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
3245 OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */
3247 /* Allocate next ITD */
3248 nsitd = ohci_alloc_sitd(sc);
3249 if (nsitd == NULL) {
3251 printf("%s: isoc TD alloc failed\n",
3252 USBDEVNAME(sc->sc_bus.bdev));
3256 /* Fill current ITD */
3257 sitd->itd.itd_flags = htole32(
3259 OHCI_ITD_SET_SF(iso->next) |
3260 OHCI_ITD_SET_DI(6) | /* delay intr a little */
3261 OHCI_ITD_SET_FC(ncur));
3262 sitd->itd.itd_bp0 = htole32(bp0);
3263 sitd->nextitd = nsitd;
3264 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3265 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3270 iso->next = iso->next + ncur;
3271 bp0 = OHCI_PAGE(buf + offs);
3274 sitd->itd.itd_offset[ncur] = htole16(OHCI_ITD_MK_OFFS(offs));
3277 nsitd = ohci_alloc_sitd(sc);
3278 if (nsitd == NULL) {
3280 printf("%s: isoc TD alloc failed\n",
3281 USBDEVNAME(sc->sc_bus.bdev));
3284 /* Fixup last used ITD */
3285 sitd->itd.itd_flags = htole32(
3287 OHCI_ITD_SET_SF(iso->next) |
3288 OHCI_ITD_SET_DI(0) |
3289 OHCI_ITD_SET_FC(ncur));
3290 sitd->itd.itd_bp0 = htole32(bp0);
3291 sitd->nextitd = nsitd;
3292 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3293 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3295 sitd->flags = OHCI_CALL_DONE;
3297 iso->next = iso->next + ncur;
3298 iso->inuse += nframes;
3300 xfer->actlen = offs; /* XXX pretend we did it all */
3302 xfer->status = USBD_IN_PROGRESS;
3305 if (ohcidebug > 5) {
3306 DPRINTF(("ohci_device_isoc_enter: frame=%d\n",
3307 le32toh(sc->sc_hcca->hcca_frame_number)));
3308 ohci_dump_itds(xfer->hcpriv);
3314 opipe->tail.itd = nsitd;
3315 sed->ed.ed_tailp = htole32(nsitd->physaddr);
3319 if (ohcidebug > 5) {
3321 DPRINTF(("ohci_device_isoc_enter: after frame=%d\n",
3322 le32toh(sc->sc_hcca->hcca_frame_number)));
3323 ohci_dump_itds(xfer->hcpriv);
3330 ohci_device_isoc_start(usbd_xfer_handle xfer)
3332 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3333 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3335 DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer));
3338 return (USBD_IOERROR);
3341 if (xfer->status != USBD_IN_PROGRESS)
3342 printf("ohci_device_isoc_start: not in progress %p\n", xfer);
3345 /* XXX anything to do? */
3347 return (USBD_IN_PROGRESS);
3351 ohci_device_isoc_abort(usbd_xfer_handle xfer)
3353 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3354 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3355 ohci_soft_ed_t *sed;
3356 ohci_soft_itd_t *sitd;
3361 DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer));
3363 /* Transfer is already done. */
3364 if (xfer->status != USBD_NOT_STARTED &&
3365 xfer->status != USBD_IN_PROGRESS) {
3367 printf("ohci_device_isoc_abort: early return\n");
3371 /* Give xfer the requested abort code. */
3372 xfer->status = USBD_CANCELLED;
3375 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
3377 sitd = xfer->hcpriv;
3381 printf("ohci_device_isoc_abort: hcpriv==0\n");
3385 for (; sitd->xfer == xfer; sitd = sitd->nextitd) {
3387 DPRINTFN(1,("abort sets done sitd=%p\n", sitd));
3394 usb_delay_ms(&sc->sc_bus, OHCI_ITD_NOFFSET);
3399 usb_transfer_complete(xfer);
3401 sed->ed.ed_headp = htole32(sitd->physaddr); /* unlink TDs */
3402 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
3408 ohci_device_isoc_done(usbd_xfer_handle xfer)
3410 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3411 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3412 ohci_soft_itd_t *sitd, *nsitd;
3414 DPRINTFN(1,("ohci_device_isoc_done: xfer=%p\n", xfer));
3416 for (sitd = xfer->hcpriv;
3417 !(sitd->flags & OHCI_CALL_DONE);
3419 nsitd = sitd->nextitd;
3420 DPRINTFN(1,("ohci_device_isoc_done: free sitd=%p\n", sitd));
3421 ohci_free_sitd(sc, sitd);
3423 ohci_free_sitd(sc, sitd);
3424 xfer->hcpriv = NULL;
3428 ohci_setup_isoc(usbd_pipe_handle pipe)
3430 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3431 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3432 struct iso *iso = &opipe->u.iso;
3439 ohci_add_ed(opipe->sed, sc->sc_isoc_head);
3442 return (USBD_NORMAL_COMPLETION);
3446 ohci_device_isoc_close(usbd_pipe_handle pipe)
3448 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3449 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3452 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
3455 ohci_rem_ed(opipe->sed, sc->sc_isoc_head);
3457 ohci_close_pipe(pipe, sc->sc_isoc_head);
3459 opipe->tail.itd->isdone = 1;
3461 ohci_free_sitd(sc, opipe->tail.itd);