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 ohci_free_sed(sc, sc->sc_isoc_head);
347 ohci_free_sed(sc, sc->sc_bulk_head);
348 ohci_free_sed(sc, sc->sc_ctrl_head);
349 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
355 ohci_alloc_sed(ohci_softc_t *sc)
362 if (sc->sc_freeeds == NULL) {
363 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
364 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
365 OHCI_ED_ALIGN, &dma);
368 for(i = 0; i < OHCI_SED_CHUNK; i++) {
369 offs = i * OHCI_SED_SIZE;
370 sed = KERNADDR(&dma, offs);
371 sed->physaddr = DMAADDR(&dma, offs);
372 sed->next = sc->sc_freeeds;
373 sc->sc_freeeds = sed;
376 sed = sc->sc_freeeds;
377 sc->sc_freeeds = sed->next;
378 memset(&sed->ed, 0, sizeof(ohci_ed_t));
384 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
386 sed->next = sc->sc_freeeds;
387 sc->sc_freeeds = sed;
391 ohci_alloc_std(ohci_softc_t *sc)
398 if (sc->sc_freetds == NULL) {
399 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
400 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
401 OHCI_TD_ALIGN, &dma);
405 for(i = 0; i < OHCI_STD_CHUNK; i++) {
406 offs = i * OHCI_STD_SIZE;
407 std = KERNADDR(&dma, offs);
408 std->physaddr = DMAADDR(&dma, offs);
409 std->nexttd = sc->sc_freetds;
410 sc->sc_freetds = std;
416 std = sc->sc_freetds;
417 sc->sc_freetds = std->nexttd;
418 memset(&std->td, 0, sizeof(ohci_td_t));
421 ohci_hash_add_td(sc, std);
428 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
431 ohci_hash_rem_td(sc, std);
432 std->nexttd = sc->sc_freetds;
433 sc->sc_freetds = std;
438 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
439 int alen, int rd, usbd_xfer_handle xfer,
440 ohci_soft_td_t *sp, ohci_soft_td_t **ep)
442 ohci_soft_td_t *next, *cur;
443 ohci_physaddr_t dataphys;
447 usb_dma_t *dma = &xfer->dmabuf;
448 u_int16_t flags = xfer->flags;
450 DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%d\n", alen));
456 (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
457 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
458 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_SET_DI(6));
461 next = ohci_alloc_std(sc);
465 dataphys = DMAADDR(dma, offset);
468 * The OHCI hardware can handle at most one 4k crossing.
469 * XXX - currently we only allocate contigous buffers, but
470 * the OHCI spec says: If during the data transfer the buffer
471 * address contained in the HC's working copy of
472 * CurrentBufferPointer crosses a 4K boundary, the upper 20
473 * bits of Buffer End are copied to the working value of
474 * CurrentBufferPointer causing the next buffer address to
475 * be the 0th byte in the same 4K page that contains the
476 * last byte of the buffer (the 4K boundary crossing may
477 * occur within a data packet transfer.)
479 * If/when dma has multiple segments, this will need to
480 * properly handle fragmenting TD's.
482 * Note that if we are gathering data from multiple SMALL
483 * segments, e.g. mbufs, we need to do special gymnastics,
484 * e.g. bounce buffering or data aggregation,
485 * BEFORE WE GET HERE because a bulk USB transfer must
486 * consist of maximally sized packets right up to the end.
487 * A shorter than maximal packet means that it is the end
488 * of the transfer. If the data transfer length is a
489 * multiple of the packet size, then a 0 byte
490 * packet will be the signal of the end of transfer.
491 * Since packets can't cross TDs this means that
492 * each TD except the last one must cover an exact multiple
493 * of the maximal packet length.
495 if (OHCI_PAGE_OFFSET(dataphys) + len <= (2 * OHCI_PAGE_SIZE)) {
496 /* We can handle all that remains in this TD */
499 /* must use multiple TDs, fill as much as possible. */
500 curlen = 2 * OHCI_PAGE_SIZE -
501 OHCI_PAGE_OFFSET(dataphys);
502 /* the length must be a multiple of the max size */
504 UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize);
505 KASSERT((curlen != 0), ("ohci_alloc_std: curlen == 0"));
507 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
508 "len=%d curlen=%d\n",
509 dataphys, len, curlen));
512 cur->td.td_flags = tdflags;
513 cur->td.td_cbp = htole32(dataphys);
515 cur->td.td_nexttd = htole32(next->physaddr);
516 cur->td.td_be = htole32(DMAADDR(dma, offset + curlen - 1));
518 cur->flags = OHCI_ADD_LEN;
520 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
521 dataphys, dataphys + curlen - 1));
525 panic("Length went negative: %d curlen %d dma %p offset %08x", len, curlen, dma, (int)0);
527 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
531 if ((flags & USBD_FORCE_SHORT_XFER) &&
532 alen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
533 /* Force a 0 length transfer at the end. */
537 next = ohci_alloc_std(sc);
541 cur->td.td_flags = tdflags;
542 cur->td.td_cbp = 0; /* indicate 0 length packet */
544 cur->td.td_nexttd = htole32(next->physaddr);
549 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
553 return (USBD_NORMAL_COMPLETION);
562 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
563 ohci_soft_td_t *stdend)
567 for (; std != stdend; std = p) {
569 ohci_free_std(sc, std);
575 ohci_alloc_sitd(ohci_softc_t *sc)
577 ohci_soft_itd_t *sitd;
583 if (sc->sc_freeitds == NULL) {
584 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
586 err = usb_allocmem(&sc->sc_bus,
587 OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
588 OHCI_ITD_ALIGN, &dma);
592 for (i = 0; i < OHCI_SITD_CHUNK; i++) {
593 offs = i * OHCI_SITD_SIZE;
594 sitd = KERNADDR(&dma, offs);
595 sitd->physaddr = DMAADDR(&dma, offs);
596 sitd->nextitd = sc->sc_freeitds;
597 sc->sc_freeitds = sitd;
600 sitd = sc->sc_freeitds;
601 sc->sc_freeitds = sitd->nextitd;
602 memset(&sitd->itd, 0, sizeof(ohci_itd_t));
603 sitd->nextitd = NULL;
605 ohci_hash_add_itd(sc, sitd);
616 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
618 DPRINTFN(10,("ohci_free_sitd: sitd=%p\n", sitd));
622 panic("ohci_free_sitd: sitd=%p not done", sitd);
625 /* Warn double free */
630 ohci_hash_rem_itd(sc, sitd);
631 sitd->nextitd = sc->sc_freeitds;
632 sc->sc_freeitds = sitd;
637 ohci_init(ohci_softc_t *sc)
639 ohci_soft_ed_t *sed, *psed;
644 DPRINTF(("ohci_init: start\n"));
645 rev = OREAD4(sc, OHCI_REVISION);
646 device_printf(sc->sc_bus.bdev, "OHCI version %d.%d%s\n",
647 OHCI_REV_HI(rev), OHCI_REV_LO(rev),
648 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
651 * Make sure all interrupts are disabled before we start messing
654 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, -1);
656 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
657 device_printf(sc->sc_bus.bdev, "unsupported OHCI revision\n");
658 sc->sc_bus.usbrev = USBREV_UNKNOWN;
661 sc->sc_bus.usbrev = USBREV_1_0;
663 for (i = 0; i < OHCI_HASH_SIZE; i++)
664 LIST_INIT(&sc->sc_hash_tds[i]);
665 for (i = 0; i < OHCI_HASH_SIZE; i++)
666 LIST_INIT(&sc->sc_hash_itds[i]);
668 STAILQ_INIT(&sc->sc_free_xfers);
670 /* XXX determine alignment by R/W */
671 /* Allocate the HCCA area. */
672 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
673 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
676 sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0);
677 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
679 sc->sc_eintrs = OHCI_NORMAL_INTRS;
681 /* Allocate dummy ED that starts the control list. */
682 sc->sc_ctrl_head = ohci_alloc_sed(sc);
683 if (sc->sc_ctrl_head == NULL) {
687 sc->sc_ctrl_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
689 /* Allocate dummy ED that starts the bulk list. */
690 sc->sc_bulk_head = ohci_alloc_sed(sc);
691 if (sc->sc_bulk_head == NULL) {
695 sc->sc_bulk_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
697 /* Allocate dummy ED that starts the isochronous list. */
698 sc->sc_isoc_head = ohci_alloc_sed(sc);
699 if (sc->sc_isoc_head == NULL) {
703 sc->sc_isoc_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
705 /* Allocate all the dummy EDs that make up the interrupt tree. */
706 for (i = 0; i < OHCI_NO_EDS; i++) {
707 sed = ohci_alloc_sed(sc);
710 ohci_free_sed(sc, sc->sc_eds[i]);
714 /* All ED fields are set to 0. */
716 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
718 psed = sc->sc_eds[(i-1) / 2];
720 psed= sc->sc_isoc_head;
722 sed->ed.ed_nexted = htole32(psed->physaddr);
725 * Fill HCCA interrupt table. The bit reversal is to get
726 * the tree set up properly to spread the interrupts.
728 for (i = 0; i < OHCI_NO_INTRS; i++)
729 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
730 htole32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
733 if (ohcidebug > 15) {
734 for (i = 0; i < OHCI_NO_EDS; i++) {
735 kprintf("ed#%d ", i);
736 ohci_dump_ed(sc->sc_eds[i]);
739 ohci_dump_ed(sc->sc_isoc_head);
743 err = ohci_controller_init(sc);
744 if (err != USBD_NORMAL_COMPLETION)
747 /* Set up the bus struct. */
748 sc->sc_bus.methods = &ohci_bus_methods;
749 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
751 callout_init(&sc->sc_tmo_rhsc);
754 * Finish up w/ interlocked done flag (the interrupt handler could
755 * be called due to other shared interrupts), enable interrupts,
756 * and run the handler in case a pending interrupt got cleared
757 * before we finished.
760 sc->sc_flags |= OHCI_SCFLG_DONEINIT;
761 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs);
765 return (USBD_NORMAL_COMPLETION);
768 for (i = 0; i < OHCI_NO_EDS; i++)
769 ohci_free_sed(sc, sc->sc_eds[i]);
771 ohci_free_sed(sc, sc->sc_isoc_head);
773 ohci_free_sed(sc, sc->sc_bulk_head);
775 ohci_free_sed(sc, sc->sc_ctrl_head);
777 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
782 ohci_controller_init(ohci_softc_t *sc)
785 u_int32_t s, ctl, ival, hcr, fm, per, desca;
787 /* Determine in what context we are running. */
788 ctl = OREAD4(sc, OHCI_CONTROL);
790 /* SMM active, request change */
791 DPRINTF(("ohci_init: SMM active, request owner change\n"));
792 s = OREAD4(sc, OHCI_COMMAND_STATUS);
793 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
794 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
795 usb_delay_ms(&sc->sc_bus, 1);
796 ctl = OREAD4(sc, OHCI_CONTROL);
798 if ((ctl & OHCI_IR) == 0) {
799 device_printf(sc->sc_bus.bdev,
800 "SMM does not respond, resetting\n");
801 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
805 /* Don't bother trying to reuse the BIOS init, we'll reset it anyway. */
806 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
807 /* BIOS started controller. */
808 DPRINTF(("ohci_init: BIOS active\n"));
809 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
810 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
811 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
815 DPRINTF(("ohci_init: cold started\n"));
817 /* Controller was cold started. */
818 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
822 * This reset should not be necessary according to the OHCI spec, but
823 * without it some controllers do not start.
825 DPRINTF(("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev)));
826 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
827 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
829 /* We now own the host controller and the bus has been reset. */
830 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
832 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
833 /* Nominal time for a reset is 10 us. */
834 for (i = 0; i < 10; i++) {
836 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
841 device_printf(sc->sc_bus.bdev, "reset timeout\n");
842 return (USBD_IOERROR);
849 /* The controller is now in SUSPEND state, we have 2ms to finish. */
851 /* Set up HC registers. */
852 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
853 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
854 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
855 /* disable all interrupts */
856 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
857 /* switch on desired functional features */
858 ctl = OREAD4(sc, OHCI_CONTROL);
859 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
860 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
861 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
862 /* And finally start it! */
863 OWRITE4(sc, OHCI_CONTROL, ctl);
866 * The controller is now OPERATIONAL. Set a some final
867 * registers that should be set earlier, but that the
868 * controller ignores when in the SUSPEND state.
870 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
871 fm |= OHCI_FSMPS(ival) | ival;
872 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
873 per = OHCI_PERIODIC(ival); /* 90% periodic */
874 OWRITE4(sc, OHCI_PERIODIC_START, per);
876 /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
877 desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
878 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
879 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
880 usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
881 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
884 * The AMD756 requires a delay before re-reading the register,
885 * otherwise it will occasionally report 0 ports.
888 for (i = 0; i < 10 && sc->sc_noport == 0; i++) {
889 usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
890 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
894 * Enable desired interrupts
896 sc->sc_eintrs |= OHCI_MIE;
897 if (sc->sc_flags & OHCI_SCFLG_DONEINIT)
898 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs);
904 return (USBD_NORMAL_COMPLETION);
908 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
910 return (usb_allocmem(bus, size, 0, dma));
914 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
916 usb_freemem(bus, dma);
920 ohci_allocx(struct usbd_bus *bus)
922 struct ohci_softc *sc = (struct ohci_softc *)bus;
923 usbd_xfer_handle xfer;
925 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
927 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
929 if (xfer->busy_free != XFER_FREE) {
930 kprintf("ohci_allocx: xfer=%p not free, 0x%08x\n", xfer,
935 xfer = kmalloc(sizeof(struct ohci_xfer), M_USB, M_INTWAIT);
938 memset(xfer, 0, sizeof (struct ohci_xfer));
939 usb_init_task(&OXFER(xfer)->abort_task, ohci_timeout_task,
941 OXFER(xfer)->ohci_xfer_flags = 0;
943 xfer->busy_free = XFER_BUSY;
950 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
952 struct ohci_softc *sc = (struct ohci_softc *)bus;
953 struct ohci_xfer *oxfer = (struct ohci_xfer *)xfer;
954 ohci_soft_itd_t *sitd;
956 if (oxfer->ohci_xfer_flags & OHCI_ISOC_DIRTY) {
958 for (sitd = xfer->hcpriv; sitd != NULL && sitd->xfer == xfer;
959 sitd = sitd->nextitd)
960 ohci_free_sitd(sc, sitd);
965 if (xfer->busy_free != XFER_BUSY) {
966 kprintf("ohci_freex: xfer=%p not busy, 0x%08x\n", xfer,
970 xfer->busy_free = XFER_FREE;
972 STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
976 * Shut down the controller when the system is going down.
979 ohci_shutdown(void *v)
981 ohci_softc_t *sc = v;
983 DPRINTF(("ohci_shutdown: stopping the HC\n"));
984 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
988 * Handle suspend/resume.
990 * We need to switch to polling mode here, because this routine is
991 * called from an intterupt context. This is all right since we
992 * are almost suspended anyway.
995 ohci_power(int why, void *v)
997 ohci_softc_t *sc = v;
1001 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
1006 if (why != PWR_RESUME) {
1007 sc->sc_bus.use_polling++;
1008 ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
1009 if (sc->sc_control == 0) {
1011 * Preserve register values, in case that APM BIOS
1012 * does not recover them.
1014 sc->sc_control = ctl;
1015 sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE);
1017 ctl |= OHCI_HCFS_SUSPEND;
1018 OWRITE4(sc, OHCI_CONTROL, ctl);
1019 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1020 sc->sc_bus.use_polling--;
1022 sc->sc_bus.use_polling++;
1024 /* Some broken BIOSes never initialize Controller chip */
1025 ohci_controller_init(sc);
1028 OWRITE4(sc, OHCI_INTERRUPT_ENABLE,
1029 sc->sc_intre & (OHCI_ALL_INTRS | OHCI_MIE));
1031 ctl = sc->sc_control;
1033 ctl = OREAD4(sc, OHCI_CONTROL);
1034 ctl |= OHCI_HCFS_RESUME;
1035 OWRITE4(sc, OHCI_CONTROL, ctl);
1036 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
1037 ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL;
1038 OWRITE4(sc, OHCI_CONTROL, ctl);
1039 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
1040 sc->sc_control = sc->sc_intre = 0;
1041 sc->sc_bus.use_polling--;
1048 ohci_dumpregs(ohci_softc_t *sc)
1050 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
1051 OREAD4(sc, OHCI_REVISION),
1052 OREAD4(sc, OHCI_CONTROL),
1053 OREAD4(sc, OHCI_COMMAND_STATUS)));
1054 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
1055 OREAD4(sc, OHCI_INTERRUPT_STATUS),
1056 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
1057 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
1058 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
1059 OREAD4(sc, OHCI_HCCA),
1060 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
1061 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
1062 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
1063 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
1064 OREAD4(sc, OHCI_BULK_HEAD_ED),
1065 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
1066 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
1067 OREAD4(sc, OHCI_DONE_HEAD),
1068 OREAD4(sc, OHCI_FM_INTERVAL),
1069 OREAD4(sc, OHCI_FM_REMAINING)));
1070 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
1071 OREAD4(sc, OHCI_FM_NUMBER),
1072 OREAD4(sc, OHCI_PERIODIC_START),
1073 OREAD4(sc, OHCI_LS_THRESHOLD)));
1074 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n",
1075 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
1076 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
1077 OREAD4(sc, OHCI_RH_STATUS)));
1078 DPRINTF((" port1=0x%08x port2=0x%08x\n",
1079 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
1080 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
1081 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n",
1082 le32toh(sc->sc_hcca->hcca_frame_number),
1083 le32toh(sc->sc_hcca->hcca_done_head)));
1087 static int ohci_intr1(ohci_softc_t *);
1092 ohci_softc_t *sc = p;
1094 if (sc->sc_dying || (sc->sc_flags & OHCI_SCFLG_DONEINIT) == 0)
1097 /* If we get an interrupt while polling, then just ignore it. */
1098 if (sc->sc_bus.use_polling) {
1100 DPRINTFN(16, ("ohci_intr: ignored interrupt while polling\n"));
1105 return (ohci_intr1(sc));
1109 ohci_intr1(ohci_softc_t *sc)
1111 u_int32_t intrs, eintrs;
1112 ohci_physaddr_t done;
1114 DPRINTFN(14,("ohci_intr1: enter\n"));
1116 /* In case the interrupt occurs before initialization has completed. */
1117 if (sc->sc_hcca == NULL) {
1119 kprintf("ohci_intr: sc->sc_hcca == NULL\n");
1125 done = le32toh(sc->sc_hcca->hcca_done_head);
1127 /* The LSb of done is used to inform the HC Driver that an interrupt
1128 * condition exists for both the Done list and for another event
1129 * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
1130 * Driver checks the HccaDoneHead Value. If this value is 0, then the
1131 * interrupt was caused by other than the HccaDoneHead update and the
1132 * HcInterruptStatus register needs to be accessed to determine that
1133 * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
1134 * update interrupt is indicated and if the LSb of done is nonzero,
1135 * then an additional interrupt event is indicated and
1136 * HcInterruptStatus should be checked to determine its cause.
1139 if (done & ~OHCI_DONE_INTRS)
1141 if (done & OHCI_DONE_INTRS) {
1142 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1143 done &= ~OHCI_DONE_INTRS;
1145 sc->sc_hcca->hcca_done_head = 0;
1147 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & ~OHCI_WDH;
1150 if (intrs == 0) /* nothing to be done (PCI shared interrupt) */
1154 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1155 eintrs = intrs & sc->sc_eintrs;
1159 sc->sc_bus.intr_context++;
1160 sc->sc_bus.no_intrs++;
1161 DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
1162 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1165 if (eintrs & OHCI_SO) {
1166 sc->sc_overrun_cnt++;
1167 if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
1168 device_printf(sc->sc_bus.bdev,
1169 "%u scheduling overruns\n", sc->sc_overrun_cnt);
1170 sc->sc_overrun_cnt = 0;
1175 if (eintrs & OHCI_WDH) {
1176 ohci_add_done(sc, done &~ OHCI_DONE_INTRS);
1177 usb_schedsoftintr(&sc->sc_bus);
1178 eintrs &= ~OHCI_WDH;
1180 if (eintrs & OHCI_RD) {
1181 device_printf(sc->sc_bus.bdev, "resume detect\n");
1182 /* XXX process resume detect */
1184 if (eintrs & OHCI_UE) {
1185 device_printf(sc->sc_bus.bdev,
1186 "unrecoverable error, controller halted\n");
1187 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1190 if (eintrs & OHCI_RHSC) {
1191 ohci_rhsc(sc, sc->sc_intrxfer);
1193 * Disable RHSC interrupt for now, because it will be
1194 * on until the port has been reset.
1196 ohci_rhsc_able(sc, 0);
1197 /* Do not allow RHSC interrupts > 1 per second */
1198 callout_reset(&sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);
1199 eintrs &= ~OHCI_RHSC;
1202 sc->sc_bus.intr_context--;
1205 /* Block unprocessed interrupts. XXX */
1206 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
1207 sc->sc_eintrs &= ~eintrs;
1208 device_printf(sc->sc_bus.bdev,
1209 "blocking intrs 0x%x\n", eintrs);
1216 ohci_rhsc_able(ohci_softc_t *sc, int on)
1218 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1220 sc->sc_eintrs |= OHCI_RHSC;
1221 if (sc->sc_flags & OHCI_SCFLG_DONEINIT)
1222 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1224 sc->sc_eintrs &= ~OHCI_RHSC;
1225 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1230 ohci_rhsc_enable(void *v_sc)
1232 ohci_softc_t *sc = v_sc;
1235 ohci_rhsc_able(sc, 1);
1240 char *ohci_cc_strs[] = {
1244 "DATA_TOGGLE_MISMATCH",
1246 "DEVICE_NOT_RESPONDING",
1247 "PID_CHECK_FAILURE",
1261 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done)
1263 ohci_soft_itd_t *sitd, *sidone, **ip;
1264 ohci_soft_td_t *std, *sdone, **p;
1266 /* Reverse the done list. */
1267 for (sdone = NULL, sidone = NULL; done != 0; ) {
1268 std = ohci_hash_find_td(sc, done);
1271 done = le32toh(std->td.td_nexttd);
1273 DPRINTFN(10,("add TD %p\n", std));
1276 sitd = ohci_hash_find_itd(sc, done);
1278 sitd->dnext = sidone;
1279 done = le32toh(sitd->itd.itd_nextitd);
1281 DPRINTFN(5,("add ITD %p\n", sitd));
1284 panic("ohci_add_done: addr 0x%08lx not found", (u_long)done);
1287 /* sdone & sidone now hold the done lists. */
1288 /* Put them on the already processed lists. */
1289 for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext)
1292 for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext)
1298 ohci_softintr(void *v)
1300 ohci_softc_t *sc = v;
1301 ohci_soft_itd_t *sitd, *sidone, *sitdnext;
1302 ohci_soft_td_t *std, *sdone, *stdnext, *p, *n;
1303 usbd_xfer_handle xfer;
1304 struct ohci_pipe *opipe;
1308 DPRINTFN(10,("ohci_softintr: enter\n"));
1310 sc->sc_bus.intr_context++;
1313 sdone = sc->sc_sdone;
1314 sc->sc_sdone = NULL;
1315 sidone = sc->sc_sidone;
1316 sc->sc_sidone = NULL;
1319 DPRINTFN(10,("ohci_softintr: sdone=%p sidone=%p\n", sdone, sidone));
1322 if (ohcidebug > 10) {
1323 DPRINTF(("ohci_process_done: TD done:\n"));
1324 ohci_dump_tds(sdone);
1328 for (std = sdone; std; std = stdnext) {
1330 stdnext = std->dnext;
1331 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1332 std, xfer, (xfer ? xfer->hcpriv : NULL)));
1335 * xfer == NULL: There seems to be no xfer associated
1336 * with this TD. It is tailp that happened to end up on
1341 if (xfer->status == USBD_CANCELLED ||
1342 xfer->status == USBD_TIMEOUT) {
1343 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1345 /* Handled by abort routine. */
1350 if (std->td.td_cbp != 0)
1351 len -= le32toh(std->td.td_be) -
1352 le32toh(std->td.td_cbp) + 1;
1353 DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n", len,
1355 if (std->flags & OHCI_ADD_LEN)
1356 xfer->actlen += len;
1358 cc = OHCI_TD_GET_CC(le32toh(std->td.td_flags));
1359 if (cc != OHCI_CC_NO_ERROR) {
1361 * Endpoint is halted. First unlink all the TDs
1362 * belonging to the failed transfer, and then restart
1365 opipe = (struct ohci_pipe *)xfer->pipe;
1367 DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1368 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1369 ohci_cc_strs[OHCI_TD_GET_CC(le32toh(std->td.td_flags))]));
1370 callout_stop(&xfer->timeout_handle);
1371 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1372 &OXFER(xfer)->abort_task);
1374 /* Remove all this xfer's TDs from the done queue. */
1375 for (p = std; p->dnext != NULL; p = p->dnext) {
1376 if (p->dnext->xfer != xfer)
1378 p->dnext = p->dnext->dnext;
1380 /* The next TD may have been removed. */
1381 stdnext = std->dnext;
1383 /* Remove all TDs belonging to this xfer. */
1384 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1386 ohci_free_std(sc, p);
1390 opipe->sed->ed.ed_headp = htole32(p->physaddr);
1391 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1393 if (cc == OHCI_CC_STALL)
1394 xfer->status = USBD_STALLED;
1396 xfer->status = USBD_IOERROR;
1398 usb_transfer_complete(xfer);
1403 * Skip intermediate TDs. They remain linked from
1404 * xfer->hcpriv and we free them when the transfer completes.
1406 if ((std->flags & OHCI_CALL_DONE) == 0)
1409 /* Normal transfer completion */
1410 callout_stop(&xfer->timeout_handle);
1411 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1412 &OXFER(xfer)->abort_task);
1413 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1415 ohci_free_std(sc, p);
1417 xfer->status = USBD_NORMAL_COMPLETION;
1419 usb_transfer_complete(xfer);
1424 if (ohcidebug > 10) {
1425 DPRINTF(("ohci_softintr: ITD done:\n"));
1426 ohci_dump_itds(sidone);
1430 for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
1432 sitdnext = sitd->dnext;
1433 sitd->flags |= OHCI_ITD_INTFIN;
1434 DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n",
1435 sitd, xfer, xfer ? xfer->hcpriv : 0));
1438 if (xfer->status == USBD_CANCELLED ||
1439 xfer->status == USBD_TIMEOUT) {
1440 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1442 /* Handled by abort routine. */
1446 if (xfer->pipe->aborting)
1447 continue; /*Ignore.*/
1450 kprintf("ohci_softintr: sitd=%p is done\n", sitd);
1453 opipe = (struct ohci_pipe *)xfer->pipe;
1454 if (opipe->aborting)
1457 if (sitd->flags & OHCI_CALL_DONE) {
1458 ohci_soft_itd_t *next;
1460 opipe->u.iso.inuse -= xfer->nframes;
1461 xfer->status = USBD_NORMAL_COMPLETION;
1462 for (i = 0, sitd = xfer->hcpriv;;sitd = next) {
1463 next = sitd->nextitd;
1464 if (OHCI_ITD_GET_CC(sitd->itd.itd_flags) != OHCI_CC_NO_ERROR)
1465 xfer->status = USBD_IOERROR;
1467 if (xfer->status == USBD_NORMAL_COMPLETION) {
1468 iframes = OHCI_ITD_GET_FC(sitd->itd.itd_flags);
1469 for (j = 0; j < iframes; i++, j++) {
1470 len = le16toh(sitd->itd.itd_offset[j]);
1472 (OHCI_ITD_PSW_GET_CC(len) ==
1473 OHCI_CC_NOT_ACCESSED) ? 0 :
1474 OHCI_ITD_PSW_LENGTH(len);
1475 xfer->frlengths[i] = len;
1478 if (sitd->flags & OHCI_CALL_DONE)
1483 usb_transfer_complete(xfer);
1488 #ifdef USB_USE_SOFTINTR
1489 if (sc->sc_softwake) {
1490 sc->sc_softwake = 0;
1491 wakeup(&sc->sc_softwake);
1493 #endif /* USB_USE_SOFTINTR */
1495 sc->sc_bus.intr_context--;
1496 DPRINTFN(10,("ohci_softintr: done:\n"));
1500 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1502 DPRINTFN(10,("ohci_device_ctrl_done: xfer=%p\n", xfer));
1505 if (!(xfer->rqflags & URQ_REQUEST)) {
1506 panic("ohci_device_ctrl_done: not a request");
1509 xfer->hcpriv = NULL;
1513 ohci_device_intr_done(usbd_xfer_handle xfer)
1515 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1516 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1517 ohci_soft_ed_t *sed = opipe->sed;
1518 ohci_soft_td_t *data, *tail;
1521 DPRINTFN(10,("ohci_device_intr_done: xfer=%p, actlen=%d\n",
1522 xfer, xfer->actlen));
1524 xfer->hcpriv = NULL;
1526 if (xfer->pipe->repeat) {
1527 data = opipe->tail.td;
1528 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1530 xfer->status = USBD_NOMEM;
1535 data->td.td_flags = htole32(
1536 OHCI_TD_IN | OHCI_TD_NOCC |
1537 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1538 if (xfer->flags & USBD_SHORT_XFER_OK)
1539 data->td.td_flags |= htole32(OHCI_TD_R);
1540 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
1541 data->nexttd = tail;
1542 data->td.td_nexttd = htole32(tail->physaddr);
1543 data->td.td_be = htole32(le32toh(data->td.td_cbp) +
1545 data->len = xfer->length;
1547 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1548 xfer->hcpriv = data;
1551 sed->ed.ed_tailp = htole32(tail->physaddr);
1552 opipe->tail.td = tail;
1557 ohci_device_bulk_done(usbd_xfer_handle xfer)
1559 DPRINTFN(10,("ohci_device_bulk_done: xfer=%p, actlen=%d\n",
1560 xfer, xfer->actlen));
1562 xfer->hcpriv = NULL;
1566 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1572 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1573 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1574 sc, xfer, hstatus));
1577 /* Just ignore the change. */
1581 p = KERNADDR(&xfer->dmabuf, 0);
1582 m = min(sc->sc_noport, xfer->length * 8 - 1);
1583 memset(p, 0, xfer->length);
1584 for (i = 1; i <= m; i++) {
1585 /* Pick out CHANGE bits from the status reg. */
1586 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1587 p[i/8] |= 1 << (i%8);
1589 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1590 xfer->actlen = xfer->length;
1591 xfer->status = USBD_NORMAL_COMPLETION;
1593 usb_transfer_complete(xfer);
1597 ohci_root_intr_done(usbd_xfer_handle xfer)
1599 xfer->hcpriv = NULL;
1603 ohci_root_ctrl_done(usbd_xfer_handle xfer)
1605 xfer->hcpriv = NULL;
1609 * Wait here until controller claims to have an interrupt.
1610 * Then call ohci_intr and return. Use timeout to avoid waiting
1614 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1616 int timo = xfer->timeout;
1620 xfer->status = USBD_IN_PROGRESS;
1621 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1622 usb_delay_ms(&sc->sc_bus, 1);
1625 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1626 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1633 if (xfer->status != USBD_IN_PROGRESS)
1639 DPRINTF(("ohci_waitintr: timeout\n"));
1640 xfer->status = USBD_TIMEOUT;
1641 usb_transfer_complete(xfer);
1642 /* XXX should free TD */
1646 ohci_poll(struct usbd_bus *bus)
1648 ohci_softc_t *sc = (ohci_softc_t *)bus;
1653 new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1655 DPRINTFN(10,("ohci_poll: intrs=0x%04x\n", new));
1666 ohci_device_request(usbd_xfer_handle xfer)
1668 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1669 usb_device_request_t *req = &xfer->request;
1670 usbd_device_handle dev = opipe->pipe.device;
1671 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1672 int addr = dev->address;
1673 ohci_soft_td_t *setup, *stat, *next, *tail;
1674 ohci_soft_ed_t *sed;
1679 isread = req->bmRequestType & UT_READ;
1680 len = UGETW(req->wLength);
1682 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1683 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1684 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1685 UGETW(req->wIndex), len, addr,
1686 opipe->pipe.endpoint->edesc->bEndpointAddress));
1688 setup = opipe->tail.td;
1689 stat = ohci_alloc_std(sc);
1694 tail = ohci_alloc_std(sc);
1702 opipe->u.ctl.length = len;
1704 /* Update device address and length since they may have changed
1705 during the setup of the control pipe in usbd_new_device(). */
1706 /* XXX This only needs to be done once, but it's too early in open. */
1707 /* XXXX Should not touch ED here! */
1708 sed->ed.ed_flags = htole32(
1709 (le32toh(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1710 OHCI_ED_SET_FA(addr) |
1711 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1715 /* Set up data transaction */
1717 ohci_soft_td_t *std = stat;
1719 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
1721 stat = stat->nexttd; /* point at free TD */
1724 /* Start toggle at 1 and then use the carried toggle. */
1725 std->td.td_flags &= htole32(~OHCI_TD_TOGGLE_MASK);
1726 std->td.td_flags |= htole32(OHCI_TD_TOGGLE_1);
1729 memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1731 setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1732 OHCI_TD_TOGGLE_0 | OHCI_TD_SET_DI(6));
1733 setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma, 0));
1734 setup->nexttd = next;
1735 setup->td.td_nexttd = htole32(next->physaddr);
1736 setup->td.td_be = htole32(le32toh(setup->td.td_cbp) + sizeof *req - 1);
1740 xfer->hcpriv = setup;
1742 stat->td.td_flags = htole32(
1743 (isread ? OHCI_TD_OUT : OHCI_TD_IN) |
1744 OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1745 stat->td.td_cbp = 0;
1746 stat->nexttd = tail;
1747 stat->td.td_nexttd = htole32(tail->physaddr);
1749 stat->flags = OHCI_CALL_DONE;
1754 if (ohcidebug > 5) {
1755 DPRINTF(("ohci_device_request:\n"));
1757 ohci_dump_tds(setup);
1761 /* Insert ED in schedule */
1763 sed->ed.ed_tailp = htole32(tail->physaddr);
1764 opipe->tail.td = tail;
1765 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1766 if (xfer->timeout && !sc->sc_bus.use_polling) {
1767 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1768 ohci_timeout, xfer);
1773 if (ohcidebug > 20) {
1775 DPRINTF(("ohci_device_request: status=%x\n",
1776 OREAD4(sc, OHCI_COMMAND_STATUS)));
1778 kprintf("ctrl head:\n");
1779 ohci_dump_ed(sc->sc_ctrl_head);
1782 ohci_dump_tds(setup);
1786 return (USBD_NORMAL_COMPLETION);
1789 ohci_free_std(sc, tail);
1791 ohci_free_std(sc, stat);
1797 * Add an ED to the schedule. Called from a critical section.
1800 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1802 DPRINTFN(8,("ohci_add_ed: sed=%p head=%p\n", sed, head));
1804 sed->next = head->next;
1805 sed->ed.ed_nexted = head->ed.ed_nexted;
1807 head->ed.ed_nexted = htole32(sed->physaddr);
1811 * Remove an ED from the schedule. Called from a critical section.
1814 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1820 for (p = head; p != NULL && p->next != sed; p = p->next)
1824 panic("ohci_rem_ed: ED not found");
1828 * p == NULL if ohci is detaching and there are still devices
1829 * using ohci (e.g. usb sticks are still plugged in). But
1830 * the real solution should be correcting ohci_free_sed() or
1834 p->next = sed->next;
1835 p->ed.ed_nexted = sed->ed.ed_nexted;
1841 * When a transfer is completed the TD is added to the done queue by
1842 * the host controller. This queue is the processed by software.
1843 * Unfortunately the queue contains the physical address of the TD
1844 * and we have no simple way to translate this back to a kernel address.
1845 * To make the translation possible (and fast) we use a hash table of
1846 * TDs currently in the schedule. The physical address is used as the
1850 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1852 * Called from a critical section
1855 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1857 int h = HASH(std->physaddr);
1859 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1863 * Called from a critical section
1866 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1868 LIST_REMOVE(std, hnext);
1872 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1875 ohci_soft_td_t *std;
1877 /* if these are present they should be masked out at an earlier
1880 KASSERT((a&~OHCI_HEADMASK) == 0, ("%s: 0x%b has lower bits set\n",
1881 device_get_nameunit(sc->sc_bus.bdev),
1882 (int) a, "\20\1HALT\2TOGGLE"));
1884 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1886 std = LIST_NEXT(std, hnext))
1887 if (std->physaddr == a)
1890 DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1891 device_get_nameunit(sc->sc_bus.bdev), (u_long) a));
1896 * Called from a critical section
1899 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1901 int h = HASH(sitd->physaddr);
1903 DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n",
1904 sitd, (u_long)sitd->physaddr));
1906 LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
1910 * Called from a critical section
1913 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1915 DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n",
1916 sitd, (u_long)sitd->physaddr));
1918 LIST_REMOVE(sitd, hnext);
1922 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
1925 ohci_soft_itd_t *sitd;
1927 for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
1929 sitd = LIST_NEXT(sitd, hnext))
1930 if (sitd->physaddr == a)
1936 ohci_timeout(void *addr)
1938 struct ohci_xfer *oxfer = addr;
1939 struct ohci_pipe *opipe = (struct ohci_pipe *)oxfer->xfer.pipe;
1940 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1942 DPRINTF(("ohci_timeout: oxfer=%p\n", oxfer));
1945 ohci_abort_xfer(&oxfer->xfer, USBD_TIMEOUT);
1949 /* Execute the abort in a process context. */
1950 usb_add_task(oxfer->xfer.pipe->device, &oxfer->abort_task,
1955 ohci_timeout_task(void *addr)
1957 usbd_xfer_handle xfer = addr;
1959 DPRINTF(("ohci_timeout_task: xfer=%p\n", xfer));
1962 ohci_abort_xfer(xfer, USBD_TIMEOUT);
1968 ohci_dump_tds(ohci_soft_td_t *std)
1970 for (; std; std = std->nexttd)
1975 ohci_dump_td(ohci_soft_td_t *std)
1979 bitmask_snprintf((u_int32_t)le32toh(std->td.td_flags),
1980 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1981 sbuf, sizeof(sbuf));
1983 kprintf("TD(%p) at %08lx: %s delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1984 "nexttd=0x%08lx be=0x%08lx\n",
1985 std, (u_long)std->physaddr, sbuf,
1986 OHCI_TD_GET_DI(le32toh(std->td.td_flags)),
1987 OHCI_TD_GET_EC(le32toh(std->td.td_flags)),
1988 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1989 (u_long)le32toh(std->td.td_cbp),
1990 (u_long)le32toh(std->td.td_nexttd),
1991 (u_long)le32toh(std->td.td_be));
1995 ohci_dump_itd(ohci_soft_itd_t *sitd)
1999 kprintf("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n"
2000 "bp0=0x%08lx next=0x%08lx be=0x%08lx\n",
2001 sitd, (u_long)sitd->physaddr,
2002 OHCI_ITD_GET_SF(le32toh(sitd->itd.itd_flags)),
2003 OHCI_ITD_GET_DI(le32toh(sitd->itd.itd_flags)),
2004 OHCI_ITD_GET_FC(le32toh(sitd->itd.itd_flags)),
2005 OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags)),
2006 (u_long)le32toh(sitd->itd.itd_bp0),
2007 (u_long)le32toh(sitd->itd.itd_nextitd),
2008 (u_long)le32toh(sitd->itd.itd_be));
2009 for (i = 0; i < OHCI_ITD_NOFFSET; i++)
2010 kprintf("offs[%d]=0x%04x ", i,
2011 (u_int)le16toh(sitd->itd.itd_offset[i]));
2016 ohci_dump_itds(ohci_soft_itd_t *sitd)
2018 for (; sitd; sitd = sitd->nextitd)
2019 ohci_dump_itd(sitd);
2023 ohci_dump_ed(ohci_soft_ed_t *sed)
2025 char sbuf[128], sbuf2[128];
2027 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_flags),
2028 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
2029 sbuf, sizeof(sbuf));
2030 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_headp),
2031 "\20\1HALT\2CARRY", sbuf2, sizeof(sbuf2));
2033 kprintf("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d flags=%s\ntailp=0x%08lx "
2034 "headflags=%s headp=0x%08lx nexted=0x%08lx\n",
2035 sed, (u_long)sed->physaddr,
2036 OHCI_ED_GET_FA(le32toh(sed->ed.ed_flags)),
2037 OHCI_ED_GET_EN(le32toh(sed->ed.ed_flags)),
2038 OHCI_ED_GET_MAXP(le32toh(sed->ed.ed_flags)), sbuf,
2039 (u_long)le32toh(sed->ed.ed_tailp), sbuf2,
2040 (u_long)le32toh(sed->ed.ed_headp),
2041 (u_long)le32toh(sed->ed.ed_nexted));
2046 ohci_open(usbd_pipe_handle pipe)
2048 usbd_device_handle dev = pipe->device;
2049 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2050 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2051 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2052 u_int8_t addr = dev->address;
2053 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
2054 ohci_soft_ed_t *sed;
2055 ohci_soft_td_t *std;
2056 ohci_soft_itd_t *sitd;
2057 ohci_physaddr_t tdphys;
2062 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2063 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
2066 return (USBD_IOERROR);
2071 if (addr == sc->sc_addr) {
2072 switch (ed->bEndpointAddress) {
2073 case USB_CONTROL_ENDPOINT:
2074 pipe->methods = &ohci_root_ctrl_methods;
2076 case UE_DIR_IN | OHCI_INTR_ENDPT:
2077 pipe->methods = &ohci_root_intr_methods;
2080 return (USBD_INVAL);
2083 sed = ohci_alloc_sed(sc);
2087 if (xfertype == UE_ISOCHRONOUS) {
2088 sitd = ohci_alloc_sitd(sc);
2091 opipe->tail.itd = sitd;
2092 opipe->aborting = 0;
2093 tdphys = sitd->physaddr;
2094 fmt = OHCI_ED_FORMAT_ISO;
2095 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
2096 fmt |= OHCI_ED_DIR_IN;
2098 fmt |= OHCI_ED_DIR_OUT;
2100 std = ohci_alloc_std(sc);
2103 opipe->tail.td = std;
2104 tdphys = std->physaddr;
2105 fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
2107 sed->ed.ed_flags = htole32(
2108 OHCI_ED_SET_FA(addr) |
2109 OHCI_ED_SET_EN(UE_GET_ADDR(ed->bEndpointAddress)) |
2110 (dev->speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) |
2112 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
2113 sed->ed.ed_headp = htole32(tdphys |
2114 (pipe->endpoint->savedtoggle ? OHCI_TOGGLECARRY : 0));
2115 sed->ed.ed_tailp = htole32(tdphys);
2119 pipe->methods = &ohci_device_ctrl_methods;
2120 err = usb_allocmem(&sc->sc_bus,
2121 sizeof(usb_device_request_t),
2122 0, &opipe->u.ctl.reqdma);
2126 ohci_add_ed(sed, sc->sc_ctrl_head);
2130 pipe->methods = &ohci_device_intr_methods;
2131 ival = pipe->interval;
2132 if (ival == USBD_DEFAULT_INTERVAL)
2133 ival = ed->bInterval;
2134 return (ohci_device_setintr(sc, opipe, ival));
2135 case UE_ISOCHRONOUS:
2136 pipe->methods = &ohci_device_isoc_methods;
2137 return (ohci_setup_isoc(pipe));
2139 pipe->methods = &ohci_device_bulk_methods;
2141 ohci_add_ed(sed, sc->sc_bulk_head);
2146 return (USBD_NORMAL_COMPLETION);
2150 ohci_free_std(sc, std);
2153 ohci_free_sed(sc, sed);
2155 return (USBD_NOMEM);
2160 * Close a reqular pipe.
2161 * Assumes that there are no pending transactions.
2164 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
2166 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2167 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2168 ohci_soft_ed_t *sed = opipe->sed;
2172 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2173 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2174 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK)) {
2175 ohci_soft_td_t *std;
2176 std = ohci_hash_find_td(sc, le32toh(sed->ed.ed_headp));
2177 kprintf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
2178 "tl=0x%x pipe=%p, std=%p\n", sed,
2179 (int)le32toh(sed->ed.ed_headp),
2180 (int)le32toh(sed->ed.ed_tailp),
2183 usbd_dump_pipe(&opipe->pipe);
2190 usb_delay_ms(&sc->sc_bus, 2);
2191 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2192 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2193 kprintf("ohci_close_pipe: pipe still not empty\n");
2196 ohci_rem_ed(sed, head);
2197 /* Make sure the host controller is not touching this ED */
2198 usb_delay_ms(&sc->sc_bus, 1);
2200 pipe->endpoint->savedtoggle =
2201 (le32toh(sed->ed.ed_headp) & OHCI_TOGGLECARRY) ? 1 : 0;
2202 ohci_free_sed(sc, opipe->sed);
2206 * Abort a device request.
2207 * If this routine is called from a critical section it guarantees that
2208 * the request will be removed from the hardware scheduling and that
2209 * the callback for it will be called with USBD_CANCELLED status.
2210 * It's impossible to guarantee that the requested transfer will not
2211 * have happened since the hardware runs concurrently.
2212 * If the transaction has already happened we rely on the ordinary
2213 * interrupt processing to process it.
2216 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2218 struct ohci_xfer *oxfer = OXFER(xfer);
2219 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2220 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
2221 ohci_soft_ed_t *sed = opipe->sed;
2222 ohci_soft_td_t *p, *n;
2223 ohci_physaddr_t headp;
2226 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p sed=%p\n", xfer, opipe,sed));
2229 /* If we're dying, just do the software part. */
2231 xfer->status = status; /* make software ignore it */
2232 callout_stop(&xfer->timeout_handle);
2233 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2234 usb_transfer_complete(xfer);
2239 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
2240 panic("ohci_abort_xfer: not in process context");
2243 * If an abort is already in progress then just wait for it to
2244 * complete and return.
2246 if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING) {
2247 DPRINTFN(2, ("ohci_abort_xfer: already aborting\n"));
2248 /* No need to wait if we're aborting from a timeout. */
2249 if (status == USBD_TIMEOUT)
2251 /* Override the status which might be USBD_TIMEOUT. */
2252 xfer->status = status;
2253 DPRINTFN(2, ("ohci_abort_xfer: waiting for abort to finish\n"));
2254 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTWAIT;
2255 while (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING)
2256 tsleep(&oxfer->ohci_xfer_flags, 0, "ohciaw", 0);
2261 * Step 1: Make interrupt routine and hardware ignore xfer.
2264 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTING;
2265 xfer->status = status; /* make software ignore it */
2266 callout_stop(&xfer->timeout_handle);
2267 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2269 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
2270 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
2273 * Step 2: Wait until we know hardware has finished any possible
2274 * use of the xfer. Also make sure the soft interrupt routine
2277 usb_delay_ms(opipe->pipe.device->bus, 20); /* Hardware finishes in 1ms */
2279 #ifdef USB_USE_SOFTINTR
2280 sc->sc_softwake = 1;
2281 #endif /* USB_USE_SOFTINTR */
2282 usb_schedsoftintr(&sc->sc_bus);
2283 #ifdef USB_USE_SOFTINTR
2284 tsleep(&sc->sc_softwake, 0, "ohciab", 0);
2285 #endif /* USB_USE_SOFTINTR */
2288 * Step 3: Remove any vestiges of the xfer from the hardware.
2289 * The complication here is that the hardware may have executed
2290 * beyond the xfer we're trying to abort. So as we're scanning
2291 * the TDs of this xfer we check if the hardware points to
2297 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING; /* XXX */
2299 kprintf("ohci_abort_xfer: hcpriv is NULL\n");
2304 if (ohcidebug > 1) {
2305 DPRINTF(("ohci_abort_xfer: sed=\n"));
2310 headp = le32toh(sed->ed.ed_headp) & OHCI_HEADMASK;
2312 for (; p->xfer == xfer; p = n) {
2313 hit |= headp == p->physaddr;
2315 ohci_free_std(sc, p);
2317 /* Zap headp register if hardware pointed inside the xfer. */
2319 DPRINTFN(1,("ohci_abort_xfer: set hd=0x08%x, tl=0x%08x\n",
2320 (int)p->physaddr, (int)le32toh(sed->ed.ed_tailp)));
2321 sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */
2323 DPRINTFN(1,("ohci_abort_xfer: no hit\n"));
2327 * Step 4: Turn on hardware again.
2329 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
2332 * Step 5: Execute callback.
2334 /* Do the wakeup first to avoid touching the xfer after the callback. */
2335 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING;
2336 if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTWAIT) {
2337 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTWAIT;
2338 wakeup(&oxfer->ohci_xfer_flags);
2340 usb_transfer_complete(xfer);
2346 * Data structures and routines to emulate the root hub.
2348 static usb_device_descriptor_t ohci_devd = {
2349 USB_DEVICE_DESCRIPTOR_SIZE,
2350 UDESC_DEVICE, /* type */
2351 {0x00, 0x01}, /* USB version */
2352 UDCLASS_HUB, /* class */
2353 UDSUBCLASS_HUB, /* subclass */
2354 UDPROTO_FSHUB, /* protocol */
2355 64, /* max packet */
2356 {0},{0},{0x00,0x01}, /* device id */
2357 1,2,0, /* string indicies */
2358 1 /* # of configurations */
2361 static usb_config_descriptor_t ohci_confd = {
2362 USB_CONFIG_DESCRIPTOR_SIZE,
2364 {USB_CONFIG_DESCRIPTOR_SIZE +
2365 USB_INTERFACE_DESCRIPTOR_SIZE +
2366 USB_ENDPOINT_DESCRIPTOR_SIZE},
2374 static usb_interface_descriptor_t ohci_ifcd = {
2375 USB_INTERFACE_DESCRIPTOR_SIZE,
2386 static usb_endpoint_descriptor_t ohci_endpd = {
2387 USB_ENDPOINT_DESCRIPTOR_SIZE,
2389 UE_DIR_IN | OHCI_INTR_ENDPT,
2391 {8, 0}, /* max packet */
2395 static usb_hub_descriptor_t ohci_hubd = {
2396 USB_HUB_DESCRIPTOR_SIZE,
2406 ohci_str(usb_string_descriptor_t *p, int l, const char *s)
2412 p->bLength = 2 * strlen(s) + 2;
2415 p->bDescriptorType = UDESC_STRING;
2417 for (i = 0; s[i] && l > 1; i++, l -= 2)
2418 USETW2(p->bString[i], 0, s[i]);
2423 * Simulate a hardware hub by handling all the necessary requests.
2426 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
2430 /* Insert last in queue. */
2431 err = usb_insert_transfer(xfer);
2435 /* Pipe isn't running, start first */
2436 return (ohci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2440 ohci_root_ctrl_start(usbd_xfer_handle xfer)
2442 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2443 usb_device_request_t *req;
2446 int len, value, index, l, totlen = 0;
2447 usb_port_status_t ps;
2448 usb_hub_descriptor_t hubd;
2453 return (USBD_IOERROR);
2456 if (!(xfer->rqflags & URQ_REQUEST))
2458 return (USBD_INVAL);
2460 req = &xfer->request;
2462 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2463 req->bmRequestType, req->bRequest));
2465 len = UGETW(req->wLength);
2466 value = UGETW(req->wValue);
2467 index = UGETW(req->wIndex);
2470 buf = KERNADDR(&xfer->dmabuf, 0);
2472 #define C(x,y) ((x) | ((y) << 8))
2473 switch(C(req->bRequest, req->bmRequestType)) {
2474 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2475 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2476 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2478 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2479 * for the integrated root hub.
2482 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2484 *(u_int8_t *)buf = sc->sc_conf;
2488 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2489 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2490 switch(value >> 8) {
2492 if ((value & 0xff) != 0) {
2496 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2497 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2498 memcpy(buf, &ohci_devd, l);
2501 if ((value & 0xff) != 0) {
2505 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2506 memcpy(buf, &ohci_confd, l);
2507 buf = (char *)buf + l;
2509 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2511 memcpy(buf, &ohci_ifcd, l);
2512 buf = (char *)buf + l;
2514 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2516 memcpy(buf, &ohci_endpd, l);
2521 *(u_int8_t *)buf = 0;
2523 switch (value & 0xff) {
2524 case 1: /* Vendor */
2525 totlen = ohci_str(buf, len, sc->sc_vendor);
2527 case 2: /* Product */
2528 totlen = ohci_str(buf, len, "OHCI root hub");
2537 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2539 *(u_int8_t *)buf = 0;
2543 case C(UR_GET_STATUS, UT_READ_DEVICE):
2545 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2549 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2550 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2552 USETW(((usb_status_t *)buf)->wStatus, 0);
2556 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2557 if (value >= USB_MAX_DEVICES) {
2561 sc->sc_addr = value;
2563 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2564 if (value != 0 && value != 1) {
2568 sc->sc_conf = value;
2570 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2572 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2573 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2574 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2577 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2579 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2582 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2584 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2585 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2586 "port=%d feature=%d\n",
2588 if (index < 1 || index > sc->sc_noport) {
2592 port = OHCI_RH_PORT_STATUS(index);
2594 case UHF_PORT_ENABLE:
2595 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2597 case UHF_PORT_SUSPEND:
2598 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2600 case UHF_PORT_POWER:
2601 /* Yes, writing to the LOW_SPEED bit clears power. */
2602 OWRITE4(sc, port, UPS_LOW_SPEED);
2604 case UHF_C_PORT_CONNECTION:
2605 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2607 case UHF_C_PORT_ENABLE:
2608 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2610 case UHF_C_PORT_SUSPEND:
2611 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2613 case UHF_C_PORT_OVER_CURRENT:
2614 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2616 case UHF_C_PORT_RESET:
2617 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2624 case UHF_C_PORT_CONNECTION:
2625 case UHF_C_PORT_ENABLE:
2626 case UHF_C_PORT_SUSPEND:
2627 case UHF_C_PORT_OVER_CURRENT:
2628 case UHF_C_PORT_RESET:
2629 /* Enable RHSC interrupt if condition is cleared. */
2630 if ((OREAD4(sc, port) >> 16) == 0)
2631 ohci_rhsc_able(sc, 1);
2637 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2638 if ((value & 0xff) != 0) {
2642 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2644 hubd.bNbrPorts = sc->sc_noport;
2645 USETW(hubd.wHubCharacteristics,
2646 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2647 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2648 /* XXX overcurrent */
2650 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2651 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2652 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2653 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2654 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2655 l = min(len, hubd.bDescLength);
2657 memcpy(buf, &hubd, l);
2659 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2664 memset(buf, 0, len); /* ? XXX */
2667 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2668 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2670 if (index < 1 || index > sc->sc_noport) {
2678 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2679 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2681 USETW(ps.wPortStatus, v);
2682 USETW(ps.wPortChange, v >> 16);
2683 l = min(len, sizeof ps);
2684 memcpy(buf, &ps, l);
2687 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2690 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2692 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2693 if (index < 1 || index > sc->sc_noport) {
2697 port = OHCI_RH_PORT_STATUS(index);
2699 case UHF_PORT_ENABLE:
2700 OWRITE4(sc, port, UPS_PORT_ENABLED);
2702 case UHF_PORT_SUSPEND:
2703 OWRITE4(sc, port, UPS_SUSPEND);
2705 case UHF_PORT_RESET:
2706 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2708 OWRITE4(sc, port, UPS_RESET);
2709 for (i = 0; i < 5; i++) {
2710 usb_delay_ms(&sc->sc_bus,
2711 USB_PORT_ROOT_RESET_DELAY);
2716 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2719 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2720 index, OREAD4(sc, port)));
2722 case UHF_PORT_POWER:
2723 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2725 OWRITE4(sc, port, UPS_PORT_POWER);
2736 xfer->actlen = totlen;
2737 err = USBD_NORMAL_COMPLETION;
2741 usb_transfer_complete(xfer);
2743 return (USBD_IN_PROGRESS);
2746 /* Abort a root control request. */
2748 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2750 /* Nothing to do, all transfers are synchronous. */
2753 /* Close the root pipe. */
2755 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2757 DPRINTF(("ohci_root_ctrl_close\n"));
2758 /* Nothing to do. */
2762 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2766 /* Insert last in queue. */
2767 err = usb_insert_transfer(xfer);
2771 /* Pipe isn't running, start first */
2772 return (ohci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
2776 ohci_root_intr_start(usbd_xfer_handle xfer)
2778 usbd_pipe_handle pipe = xfer->pipe;
2779 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2782 return (USBD_IOERROR);
2784 sc->sc_intrxfer = xfer;
2786 return (USBD_IN_PROGRESS);
2789 /* Abort a root interrupt request. */
2791 ohci_root_intr_abort(usbd_xfer_handle xfer)
2793 if (xfer->pipe->intrxfer == xfer) {
2794 DPRINTF(("ohci_root_intr_abort: remove\n"));
2795 xfer->pipe->intrxfer = NULL;
2797 xfer->status = USBD_CANCELLED;
2799 usb_transfer_complete(xfer);
2803 /* Close the root pipe. */
2805 ohci_root_intr_close(usbd_pipe_handle pipe)
2807 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2809 DPRINTF(("ohci_root_intr_close\n"));
2811 sc->sc_intrxfer = NULL;
2814 /************************/
2817 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2821 /* Insert last in queue. */
2822 err = usb_insert_transfer(xfer);
2826 /* Pipe isn't running, start first */
2827 return (ohci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2831 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2833 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2837 return (USBD_IOERROR);
2840 if (!(xfer->rqflags & URQ_REQUEST)) {
2842 kprintf("ohci_device_ctrl_transfer: not a request\n");
2843 return (USBD_INVAL);
2847 err = ohci_device_request(xfer);
2851 if (sc->sc_bus.use_polling)
2852 ohci_waitintr(sc, xfer);
2853 return (USBD_IN_PROGRESS);
2856 /* Abort a device control request. */
2858 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2860 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2861 ohci_abort_xfer(xfer, USBD_CANCELLED);
2864 /* Close a device control pipe. */
2866 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2868 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2869 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2871 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2872 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2873 ohci_free_std(sc, opipe->tail.td);
2876 /************************/
2879 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2881 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2883 opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY);
2887 ohci_noop(usbd_pipe_handle pipe)
2892 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2896 /* Insert last in queue. */
2897 err = usb_insert_transfer(xfer);
2901 /* Pipe isn't running, start first */
2902 return (ohci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
2906 ohci_device_bulk_start(usbd_xfer_handle xfer)
2908 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2909 usbd_device_handle dev = opipe->pipe.device;
2910 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2911 int addr = dev->address;
2912 ohci_soft_td_t *data, *tail, *tdp;
2913 ohci_soft_ed_t *sed;
2914 int len, isread, endpt;
2918 return (USBD_IOERROR);
2921 if (xfer->rqflags & URQ_REQUEST) {
2923 kprintf("ohci_device_bulk_start: a request\n");
2924 return (USBD_INVAL);
2929 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2930 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2933 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2934 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2937 opipe->u.bulk.isread = isread;
2938 opipe->u.bulk.length = len;
2940 /* Update device address */
2941 sed->ed.ed_flags = htole32(
2942 (le32toh(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2943 OHCI_ED_SET_FA(addr));
2945 /* Allocate a chain of new TDs (including a new tail). */
2946 data = opipe->tail.td;
2947 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
2949 /* We want interrupt at the end of the transfer. */
2950 tail->td.td_flags &= htole32(~OHCI_TD_INTR_MASK);
2951 tail->td.td_flags |= htole32(OHCI_TD_SET_DI(1));
2952 tail->flags |= OHCI_CALL_DONE;
2953 tail = tail->nexttd; /* point at sentinel */
2958 xfer->hcpriv = data;
2960 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2961 "td_cbp=0x%08x td_be=0x%08x\n",
2962 (int)le32toh(sed->ed.ed_flags),
2963 (int)le32toh(data->td.td_flags),
2964 (int)le32toh(data->td.td_cbp),
2965 (int)le32toh(data->td.td_be)));
2968 if (ohcidebug > 5) {
2970 ohci_dump_tds(data);
2974 /* Insert ED in schedule */
2976 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2979 sed->ed.ed_tailp = htole32(tail->physaddr);
2980 opipe->tail.td = tail;
2981 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
2982 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2983 if (xfer->timeout && !sc->sc_bus.use_polling) {
2984 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2985 ohci_timeout, xfer);
2989 /* This goes wrong if we are too slow. */
2990 if (ohcidebug > 10) {
2992 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2993 OREAD4(sc, OHCI_COMMAND_STATUS)));
2995 ohci_dump_tds(data);
3001 if (sc->sc_bus.use_polling)
3002 ohci_waitintr(sc, xfer);
3004 return (USBD_IN_PROGRESS);
3008 ohci_device_bulk_abort(usbd_xfer_handle xfer)
3010 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
3011 ohci_abort_xfer(xfer, USBD_CANCELLED);
3015 * Close a device bulk pipe.
3018 ohci_device_bulk_close(usbd_pipe_handle pipe)
3020 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3021 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3023 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
3024 ohci_close_pipe(pipe, sc->sc_bulk_head);
3025 ohci_free_std(sc, opipe->tail.td);
3028 /************************/
3031 ohci_device_intr_transfer(usbd_xfer_handle xfer)
3035 /* Insert last in queue. */
3036 err = usb_insert_transfer(xfer);
3040 /* Pipe isn't running, start first */
3041 return (ohci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3045 ohci_device_intr_start(usbd_xfer_handle xfer)
3047 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3048 usbd_device_handle dev = opipe->pipe.device;
3049 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3050 ohci_soft_ed_t *sed = opipe->sed;
3051 ohci_soft_td_t *data, *tail;
3055 return (USBD_IOERROR);
3057 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
3058 "flags=%d priv=%p\n",
3059 xfer, xfer->length, xfer->flags, xfer->priv));
3062 if (xfer->rqflags & URQ_REQUEST)
3063 panic("ohci_device_intr_transfer: a request");
3068 data = opipe->tail.td;
3069 tail = ohci_alloc_std(sc);
3071 return (USBD_NOMEM);
3074 data->td.td_flags = htole32(
3075 OHCI_TD_IN | OHCI_TD_NOCC |
3076 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
3077 if (xfer->flags & USBD_SHORT_XFER_OK)
3078 data->td.td_flags |= htole32(OHCI_TD_R);
3079 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
3080 data->nexttd = tail;
3081 data->td.td_nexttd = htole32(tail->physaddr);
3082 data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1);
3085 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
3086 xfer->hcpriv = data;
3089 if (ohcidebug > 5) {
3090 DPRINTF(("ohci_device_intr_transfer:\n"));
3092 ohci_dump_tds(data);
3096 /* Insert ED in schedule */
3098 sed->ed.ed_tailp = htole32(tail->physaddr);
3099 opipe->tail.td = tail;
3100 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3104 * This goes horribly wrong, printing thousands of descriptors,
3105 * because false references are followed due to the fact that the
3108 if (ohcidebug > 5) {
3109 usb_delay_ms(&sc->sc_bus, 5);
3110 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
3111 OREAD4(sc, OHCI_COMMAND_STATUS)));
3113 ohci_dump_tds(data);
3118 return (USBD_IN_PROGRESS);
3121 /* Abort a device control request. */
3123 ohci_device_intr_abort(usbd_xfer_handle xfer)
3125 if (xfer->pipe->intrxfer == xfer) {
3126 DPRINTF(("ohci_device_intr_abort: remove\n"));
3127 xfer->pipe->intrxfer = NULL;
3129 ohci_abort_xfer(xfer, USBD_CANCELLED);
3132 /* Close a device interrupt pipe. */
3134 ohci_device_intr_close(usbd_pipe_handle pipe)
3136 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3137 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3138 int nslots = opipe->u.intr.nslots;
3139 int pos = opipe->u.intr.pos;
3141 ohci_soft_ed_t *p, *sed = opipe->sed;
3143 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
3144 pipe, nslots, pos));
3146 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
3147 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3148 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3149 usb_delay_ms(&sc->sc_bus, 2);
3151 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3152 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3153 panic("%s: Intr pipe %p still has TDs queued",
3154 device_get_nameunit(sc->sc_bus.bdev), pipe);
3157 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
3161 panic("ohci_device_intr_close: ED not found");
3163 p->next = sed->next;
3164 p->ed.ed_nexted = sed->ed.ed_nexted;
3167 for (j = 0; j < nslots; j++)
3168 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
3170 ohci_free_std(sc, opipe->tail.td);
3171 ohci_free_sed(sc, opipe->sed);
3175 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
3178 u_int npoll, slow, shigh, nslots;
3180 ohci_soft_ed_t *hsed, *sed = opipe->sed;
3182 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
3184 kprintf("ohci_setintr: 0 interval\n");
3185 return (USBD_INVAL);
3188 npoll = OHCI_NO_INTRS;
3189 while (npoll > ival)
3191 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
3194 * We now know which level in the tree the ED must go into.
3195 * Figure out which slot has most bandwidth left over.
3201 * 8 7 8 9 10 11 12 13 14
3202 * N (N-1) .. (N-1+N-1)
3205 shigh = slow + npoll;
3206 nslots = OHCI_NO_INTRS / npoll;
3207 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
3209 for (j = 0; j < nslots; j++)
3210 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
3216 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
3217 best, slow, shigh, bestbw));
3220 hsed = sc->sc_eds[best];
3221 sed->next = hsed->next;
3222 sed->ed.ed_nexted = hsed->ed.ed_nexted;
3224 hsed->ed.ed_nexted = htole32(sed->physaddr);
3227 for (j = 0; j < nslots; j++)
3228 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
3229 opipe->u.intr.nslots = nslots;
3230 opipe->u.intr.pos = best;
3232 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
3233 return (USBD_NORMAL_COMPLETION);
3236 /***********************/
3239 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
3243 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
3245 /* Put it on our queue, */
3246 err = usb_insert_transfer(xfer);
3248 /* bail out on error, */
3249 if (err && err != USBD_IN_PROGRESS)
3252 /* XXX should check inuse here */
3254 /* insert into schedule, */
3255 ohci_device_isoc_enter(xfer);
3257 /* and start if the pipe wasn't running */
3259 ohci_device_isoc_start(STAILQ_FIRST(&xfer->pipe->queue));
3265 ohci_device_isoc_enter(usbd_xfer_handle xfer)
3267 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3268 usbd_device_handle dev = opipe->pipe.device;
3269 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3270 ohci_soft_ed_t *sed = opipe->sed;
3271 struct iso *iso = &opipe->u.iso;
3272 struct ohci_xfer *oxfer = (struct ohci_xfer *)xfer;
3273 ohci_soft_itd_t *sitd, *nsitd;
3274 ohci_physaddr_t buf, offs, noffs, bp0, tdphys;
3275 int i, ncur, nframes;
3277 DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p "
3279 iso->inuse, iso->next, xfer, xfer->nframes));
3284 if (iso->next == -1) {
3285 /* Not in use yet, schedule it a few frames ahead. */
3286 iso->next = le32toh(sc->sc_hcca->hcca_frame_number) + 5;
3287 DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n",
3293 for (sitd = xfer->hcpriv; sitd != NULL && sitd->xfer == xfer;
3294 sitd = sitd->nextitd)
3295 ohci_free_sitd(sc, sitd); /* Free ITDs in prev xfer*/
3299 sitd = ohci_alloc_sitd(sc);
3301 panic("can't alloc isoc");
3302 opipe->tail.itd = sitd;
3303 tdphys = sitd->physaddr;
3304 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop*/
3306 sed->ed.ed_tailp = htole32(tdphys);
3307 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* Start.*/
3311 sitd = opipe->tail.itd;
3312 buf = DMAADDR(&xfer->dmabuf, 0);
3313 bp0 = OHCI_PAGE(buf);
3314 offs = OHCI_PAGE_OFFSET(buf);
3315 nframes = xfer->nframes;
3316 xfer->hcpriv = sitd;
3317 for (i = ncur = 0; i < nframes; i++, ncur++) {
3318 noffs = offs + xfer->frlengths[i];
3319 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
3320 OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */
3322 /* Allocate next ITD */
3323 nsitd = ohci_alloc_sitd(sc);
3324 if (nsitd == NULL) {
3326 device_printf(sc->sc_bus.bdev,
3327 "isoc TD alloc failed\n");
3331 /* Fill current ITD */
3332 sitd->itd.itd_flags = htole32(
3334 OHCI_ITD_SET_SF(iso->next) |
3335 OHCI_ITD_SET_DI(6) | /* delay intr a little */
3336 OHCI_ITD_SET_FC(ncur));
3337 sitd->itd.itd_bp0 = htole32(bp0);
3338 sitd->nextitd = nsitd;
3339 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3340 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3342 sitd->flags = OHCI_ITD_ACTIVE;
3345 iso->next = iso->next + ncur;
3346 bp0 = OHCI_PAGE(buf + offs);
3349 sitd->itd.itd_offset[ncur] = htole16(OHCI_ITD_MK_OFFS(offs));
3352 nsitd = ohci_alloc_sitd(sc);
3353 if (nsitd == NULL) {
3355 device_printf(sc->sc_bus.bdev, "isoc TD alloc failed\n");
3358 /* Fixup last used ITD */
3359 sitd->itd.itd_flags = htole32(
3361 OHCI_ITD_SET_SF(iso->next) |
3362 OHCI_ITD_SET_DI(0) |
3363 OHCI_ITD_SET_FC(ncur));
3364 sitd->itd.itd_bp0 = htole32(bp0);
3365 sitd->nextitd = nsitd;
3366 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3367 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3369 sitd->flags = OHCI_CALL_DONE | OHCI_ITD_ACTIVE;
3371 iso->next = iso->next + ncur;
3372 iso->inuse += nframes;
3374 xfer->actlen = offs; /* XXX pretend we did it all */
3376 xfer->status = USBD_IN_PROGRESS;
3378 oxfer->ohci_xfer_flags |= OHCI_ISOC_DIRTY;
3381 if (ohcidebug > 5) {
3382 DPRINTF(("ohci_device_isoc_enter: frame=%d\n",
3383 le32toh(sc->sc_hcca->hcca_frame_number)));
3384 ohci_dump_itds(xfer->hcpriv);
3390 sed->ed.ed_tailp = htole32(nsitd->physaddr);
3391 opipe->tail.itd = nsitd;
3392 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3396 if (ohcidebug > 5) {
3398 DPRINTF(("ohci_device_isoc_enter: after frame=%d\n",
3399 le32toh(sc->sc_hcca->hcca_frame_number)));
3400 ohci_dump_itds(xfer->hcpriv);
3407 ohci_device_isoc_start(usbd_xfer_handle xfer)
3409 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3410 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3411 ohci_soft_ed_t *sed;
3413 DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer));
3416 return (USBD_IOERROR);
3419 if (xfer->status != USBD_IN_PROGRESS)
3420 kprintf("ohci_device_isoc_start: not in progress %p\n", xfer);
3423 /* XXX anything to do? */
3426 sed = opipe->sed; /* Turn off ED skip-bit to start processing */
3427 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* ED's ITD list.*/
3430 return (USBD_IN_PROGRESS);
3434 ohci_device_isoc_abort(usbd_xfer_handle xfer)
3436 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3437 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3438 ohci_soft_ed_t *sed;
3439 ohci_soft_itd_t *sitd, *tmp_sitd;
3440 int undone, num_sitds;
3443 opipe->aborting = 1;
3445 DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer));
3447 /* Transfer is already done. */
3448 if (xfer->status != USBD_NOT_STARTED &&
3449 xfer->status != USBD_IN_PROGRESS) {
3451 kprintf("ohci_device_isoc_abort: early return\n");
3455 /* Give xfer the requested abort code. */
3456 xfer->status = USBD_CANCELLED;
3459 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
3462 sitd = xfer->hcpriv;
3466 kprintf("ohci_device_isoc_abort: hcpriv==0\n");
3470 for (; sitd != NULL && sitd->xfer == xfer; sitd = sitd->nextitd) {
3473 DPRINTFN(1,("abort sets done sitd=%p\n", sitd));
3481 * Each sitd has up to OHCI_ITD_NOFFSET transfers, each can
3482 * take a usb 1ms cycle. Conservatively wait for it to drain.
3483 * Even with DMA done, it can take awhile for the "batch"
3484 * delivery of completion interrupts to occur thru the controller.
3488 usb_delay_ms(&sc->sc_bus, 2*(num_sitds*OHCI_ITD_NOFFSET));
3491 tmp_sitd = xfer->hcpriv;
3492 for (; tmp_sitd != NULL && tmp_sitd->xfer == xfer;
3493 tmp_sitd = tmp_sitd->nextitd) {
3494 if (OHCI_CC_NO_ERROR ==
3495 OHCI_ITD_GET_CC(le32toh(tmp_sitd->itd.itd_flags)) &&
3496 tmp_sitd->flags & OHCI_ITD_ACTIVE &&
3497 (tmp_sitd->flags & OHCI_ITD_INTFIN) == 0)
3500 } while( undone != 0 );
3505 usb_transfer_complete(xfer);
3509 * Only if there is a `next' sitd in next xfer...
3510 * unlink this xfer's sitds.
3512 sed->ed.ed_headp = htole32(sitd->physaddr);
3514 sed->ed.ed_headp = 0;
3516 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
3522 ohci_device_isoc_done(usbd_xfer_handle xfer)
3524 /* This null routine corresponds to non-isoc "done()" routines
3525 * that free the stds associated with an xfer after a completed
3526 * xfer interrupt. However, in the case of isoc transfers, the
3527 * sitds associated with the transfer have already been processed
3528 * and reallocated for the next iteration by
3529 * "ohci_device_isoc_transfer()".
3531 * Routine "usb_transfer_complete()" is called at the end of every
3532 * relevant usb interrupt. "usb_transfer_complete()" indirectly
3533 * calls 1) "ohci_device_isoc_transfer()" (which keeps pumping the
3534 * pipeline by setting up the next transfer iteration) and 2) then
3535 * calls "ohci_device_isoc_done()". Isoc transfers have not been
3536 * working for the ohci usb because this routine was trashing the
3537 * xfer set up for the next iteration (thus, only the first
3538 * UGEN_NISOREQS xfers outstanding on an open would work). Perhaps
3539 * this could all be re-factored, but that's another pass...
3544 ohci_setup_isoc(usbd_pipe_handle pipe)
3546 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3547 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3548 struct iso *iso = &opipe->u.iso;
3554 ohci_add_ed(opipe->sed, sc->sc_isoc_head);
3557 return (USBD_NORMAL_COMPLETION);
3561 ohci_device_isoc_close(usbd_pipe_handle pipe)
3563 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3564 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3565 ohci_soft_ed_t *sed;
3567 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
3570 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop device. */
3572 ohci_close_pipe(pipe, sc->sc_isoc_head); /* Stop isoc list, free ED.*/
3574 /* up to NISOREQs xfers still outstanding. */
3577 opipe->tail.itd->isdone = 1;
3579 ohci_free_sitd(sc, opipe->tail.itd); /* Next `avail free' sitd.*/