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.25 2008/04/20 13:44:25 swildner 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)
335 callout_stop(&sc->sc_tmo_rhsc);
337 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
338 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
340 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
342 for (i = 0; i < OHCI_NO_EDS; i++)
343 ohci_free_sed(sc, sc->sc_eds[i]);
344 ohci_free_sed(sc, sc->sc_isoc_head);
345 ohci_free_sed(sc, sc->sc_bulk_head);
346 ohci_free_sed(sc, sc->sc_ctrl_head);
347 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
353 ohci_alloc_sed(ohci_softc_t *sc)
360 if (sc->sc_freeeds == NULL) {
361 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
362 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
363 OHCI_ED_ALIGN, &dma);
366 for(i = 0; i < OHCI_SED_CHUNK; i++) {
367 offs = i * OHCI_SED_SIZE;
368 sed = KERNADDR(&dma, offs);
369 sed->physaddr = DMAADDR(&dma, offs);
370 sed->next = sc->sc_freeeds;
371 sc->sc_freeeds = sed;
374 sed = sc->sc_freeeds;
375 sc->sc_freeeds = sed->next;
376 memset(&sed->ed, 0, sizeof(ohci_ed_t));
382 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
384 sed->next = sc->sc_freeeds;
385 sc->sc_freeeds = sed;
389 ohci_alloc_std(ohci_softc_t *sc)
396 if (sc->sc_freetds == NULL) {
397 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
398 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
399 OHCI_TD_ALIGN, &dma);
403 for(i = 0; i < OHCI_STD_CHUNK; i++) {
404 offs = i * OHCI_STD_SIZE;
405 std = KERNADDR(&dma, offs);
406 std->physaddr = DMAADDR(&dma, offs);
407 std->nexttd = sc->sc_freetds;
408 sc->sc_freetds = std;
414 std = sc->sc_freetds;
415 sc->sc_freetds = std->nexttd;
416 memset(&std->td, 0, sizeof(ohci_td_t));
419 ohci_hash_add_td(sc, std);
426 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
429 ohci_hash_rem_td(sc, std);
430 std->nexttd = sc->sc_freetds;
431 sc->sc_freetds = std;
436 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
437 int alen, int rd, usbd_xfer_handle xfer,
438 ohci_soft_td_t *sp, ohci_soft_td_t **ep)
440 ohci_soft_td_t *next, *cur;
441 ohci_physaddr_t dataphys;
445 usb_dma_t *dma = &xfer->dmabuf;
446 u_int16_t flags = xfer->flags;
448 DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%d\n", alen));
454 (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
455 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
456 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_SET_DI(6));
459 next = ohci_alloc_std(sc);
463 dataphys = DMAADDR(dma, offset);
466 * The OHCI hardware can handle at most one 4k crossing.
467 * XXX - currently we only allocate contigous buffers, but
468 * the OHCI spec says: If during the data transfer the buffer
469 * address contained in the HC's working copy of
470 * CurrentBufferPointer crosses a 4K boundary, the upper 20
471 * bits of Buffer End are copied to the working value of
472 * CurrentBufferPointer causing the next buffer address to
473 * be the 0th byte in the same 4K page that contains the
474 * last byte of the buffer (the 4K boundary crossing may
475 * occur within a data packet transfer.)
477 * If/when dma has multiple segments, this will need to
478 * properly handle fragmenting TD's.
480 * Note that if we are gathering data from multiple SMALL
481 * segments, e.g. mbufs, we need to do special gymnastics,
482 * e.g. bounce buffering or data aggregation,
483 * BEFORE WE GET HERE because a bulk USB transfer must
484 * consist of maximally sized packets right up to the end.
485 * A shorter than maximal packet means that it is the end
486 * of the transfer. If the data transfer length is a
487 * multiple of the packet size, then a 0 byte
488 * packet will be the signal of the end of transfer.
489 * Since packets can't cross TDs this means that
490 * each TD except the last one must cover an exact multiple
491 * of the maximal packet length.
493 if (OHCI_PAGE_OFFSET(dataphys) + len <= (2 * OHCI_PAGE_SIZE)) {
494 /* We can handle all that remains in this TD */
497 /* must use multiple TDs, fill as much as possible. */
498 curlen = 2 * OHCI_PAGE_SIZE -
499 OHCI_PAGE_OFFSET(dataphys);
500 /* the length must be a multiple of the max size */
502 UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize);
503 KASSERT((curlen != 0), ("ohci_alloc_std: curlen == 0"));
505 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
506 "len=%d curlen=%d\n",
507 dataphys, len, curlen));
510 cur->td.td_flags = tdflags;
511 cur->td.td_cbp = htole32(dataphys);
513 cur->td.td_nexttd = htole32(next->physaddr);
514 cur->td.td_be = htole32(DMAADDR(dma, offset + curlen - 1));
516 cur->flags = OHCI_ADD_LEN;
518 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
519 dataphys, dataphys + curlen - 1));
523 panic("Length went negative: %d curlen %d dma %p offset %08x", len, curlen, dma, (int)0);
525 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
529 if ((flags & USBD_FORCE_SHORT_XFER) &&
530 alen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
531 /* Force a 0 length transfer at the end. */
535 next = ohci_alloc_std(sc);
539 cur->td.td_flags = tdflags;
540 cur->td.td_cbp = 0; /* indicate 0 length packet */
542 cur->td.td_nexttd = htole32(next->physaddr);
547 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
551 return (USBD_NORMAL_COMPLETION);
560 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
561 ohci_soft_td_t *stdend)
565 for (; std != stdend; std = p) {
567 ohci_free_std(sc, std);
573 ohci_alloc_sitd(ohci_softc_t *sc)
575 ohci_soft_itd_t *sitd;
581 if (sc->sc_freeitds == NULL) {
582 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
584 err = usb_allocmem(&sc->sc_bus,
585 OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
586 OHCI_ITD_ALIGN, &dma);
590 for (i = 0; i < OHCI_SITD_CHUNK; i++) {
591 offs = i * OHCI_SITD_SIZE;
592 sitd = KERNADDR(&dma, offs);
593 sitd->physaddr = DMAADDR(&dma, offs);
594 sitd->nextitd = sc->sc_freeitds;
595 sc->sc_freeitds = sitd;
598 sitd = sc->sc_freeitds;
599 sc->sc_freeitds = sitd->nextitd;
600 memset(&sitd->itd, 0, sizeof(ohci_itd_t));
601 sitd->nextitd = NULL;
603 ohci_hash_add_itd(sc, sitd);
614 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
616 DPRINTFN(10,("ohci_free_sitd: sitd=%p\n", sitd));
620 panic("ohci_free_sitd: sitd=%p not done", sitd);
623 /* Warn double free */
628 ohci_hash_rem_itd(sc, sitd);
629 sitd->nextitd = sc->sc_freeitds;
630 sc->sc_freeitds = sitd;
635 ohci_init(ohci_softc_t *sc)
637 ohci_soft_ed_t *sed, *psed;
642 DPRINTF(("ohci_init: start\n"));
643 kprintf("%s:", device_get_nameunit(sc->sc_bus.bdev));
644 rev = OREAD4(sc, OHCI_REVISION);
645 kprintf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
646 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
648 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
649 kprintf("%s: unsupported OHCI revision\n",
650 device_get_nameunit(sc->sc_bus.bdev));
651 sc->sc_bus.usbrev = USBREV_UNKNOWN;
654 sc->sc_bus.usbrev = USBREV_1_0;
656 for (i = 0; i < OHCI_HASH_SIZE; i++)
657 LIST_INIT(&sc->sc_hash_tds[i]);
658 for (i = 0; i < OHCI_HASH_SIZE; i++)
659 LIST_INIT(&sc->sc_hash_itds[i]);
661 STAILQ_INIT(&sc->sc_free_xfers);
663 /* XXX determine alignment by R/W */
664 /* Allocate the HCCA area. */
665 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
666 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
669 sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0);
670 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
672 sc->sc_eintrs = OHCI_NORMAL_INTRS;
674 /* Allocate dummy ED that starts the control list. */
675 sc->sc_ctrl_head = ohci_alloc_sed(sc);
676 if (sc->sc_ctrl_head == NULL) {
680 sc->sc_ctrl_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
682 /* Allocate dummy ED that starts the bulk list. */
683 sc->sc_bulk_head = ohci_alloc_sed(sc);
684 if (sc->sc_bulk_head == NULL) {
688 sc->sc_bulk_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
690 /* Allocate dummy ED that starts the isochronous list. */
691 sc->sc_isoc_head = ohci_alloc_sed(sc);
692 if (sc->sc_isoc_head == NULL) {
696 sc->sc_isoc_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
698 /* Allocate all the dummy EDs that make up the interrupt tree. */
699 for (i = 0; i < OHCI_NO_EDS; i++) {
700 sed = ohci_alloc_sed(sc);
703 ohci_free_sed(sc, sc->sc_eds[i]);
707 /* All ED fields are set to 0. */
709 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
711 psed = sc->sc_eds[(i-1) / 2];
713 psed= sc->sc_isoc_head;
715 sed->ed.ed_nexted = htole32(psed->physaddr);
718 * Fill HCCA interrupt table. The bit reversal is to get
719 * the tree set up properly to spread the interrupts.
721 for (i = 0; i < OHCI_NO_INTRS; i++)
722 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
723 htole32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
726 if (ohcidebug > 15) {
727 for (i = 0; i < OHCI_NO_EDS; i++) {
728 kprintf("ed#%d ", i);
729 ohci_dump_ed(sc->sc_eds[i]);
732 ohci_dump_ed(sc->sc_isoc_head);
736 err = ohci_controller_init(sc);
737 if (err != USBD_NORMAL_COMPLETION)
740 /* Set up the bus struct. */
741 sc->sc_bus.methods = &ohci_bus_methods;
742 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
744 callout_init(&sc->sc_tmo_rhsc);
746 return (USBD_NORMAL_COMPLETION);
749 for (i = 0; i < OHCI_NO_EDS; i++)
750 ohci_free_sed(sc, sc->sc_eds[i]);
752 ohci_free_sed(sc, sc->sc_isoc_head);
754 ohci_free_sed(sc, sc->sc_bulk_head);
756 ohci_free_sed(sc, sc->sc_ctrl_head);
758 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
763 ohci_controller_init(ohci_softc_t *sc)
766 u_int32_t s, ctl, ival, hcr, fm, per, desca;
768 /* Determine in what context we are running. */
769 ctl = OREAD4(sc, OHCI_CONTROL);
771 /* SMM active, request change */
772 DPRINTF(("ohci_init: SMM active, request owner change\n"));
773 s = OREAD4(sc, OHCI_COMMAND_STATUS);
774 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
775 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
776 usb_delay_ms(&sc->sc_bus, 1);
777 ctl = OREAD4(sc, OHCI_CONTROL);
779 if ((ctl & OHCI_IR) == 0) {
780 kprintf("%s: SMM does not respond, resetting\n",
781 device_get_nameunit(sc->sc_bus.bdev));
782 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
786 /* Don't bother trying to reuse the BIOS init, we'll reset it anyway. */
787 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
788 /* BIOS started controller. */
789 DPRINTF(("ohci_init: BIOS active\n"));
790 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
791 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
792 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
796 DPRINTF(("ohci_init: cold started\n"));
798 /* Controller was cold started. */
799 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
803 * This reset should not be necessary according to the OHCI spec, but
804 * without it some controllers do not start.
806 DPRINTF(("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev)));
807 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
808 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
810 /* We now own the host controller and the bus has been reset. */
811 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
813 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
814 /* Nominal time for a reset is 10 us. */
815 for (i = 0; i < 10; i++) {
817 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
822 kprintf("%s: reset timeout\n", device_get_nameunit(sc->sc_bus.bdev));
823 return (USBD_IOERROR);
830 /* The controller is now in SUSPEND state, we have 2ms to finish. */
832 /* Set up HC registers. */
833 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
834 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
835 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
836 /* disable all interrupts and then switch on all desired interrupts */
837 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
838 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
839 /* switch on desired functional features */
840 ctl = OREAD4(sc, OHCI_CONTROL);
841 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
842 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
843 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
844 /* And finally start it! */
845 OWRITE4(sc, OHCI_CONTROL, ctl);
848 * The controller is now OPERATIONAL. Set a some final
849 * registers that should be set earlier, but that the
850 * controller ignores when in the SUSPEND state.
852 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
853 fm |= OHCI_FSMPS(ival) | ival;
854 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
855 per = OHCI_PERIODIC(ival); /* 90% periodic */
856 OWRITE4(sc, OHCI_PERIODIC_START, per);
858 /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
859 desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
860 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
861 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
862 usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
863 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
866 * The AMD756 requires a delay before re-reading the register,
867 * otherwise it will occasionally report 0 ports.
870 for (i = 0; i < 10 && sc->sc_noport == 0; i++) {
871 usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
872 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
879 return (USBD_NORMAL_COMPLETION);
883 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
885 return (usb_allocmem(bus, size, 0, dma));
889 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
891 usb_freemem(bus, dma);
895 ohci_allocx(struct usbd_bus *bus)
897 struct ohci_softc *sc = (struct ohci_softc *)bus;
898 usbd_xfer_handle xfer;
900 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
902 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
904 if (xfer->busy_free != XFER_FREE) {
905 kprintf("ohci_allocx: xfer=%p not free, 0x%08x\n", xfer,
910 xfer = kmalloc(sizeof(struct ohci_xfer), M_USB, M_INTWAIT);
913 memset(xfer, 0, sizeof (struct ohci_xfer));
914 usb_init_task(&OXFER(xfer)->abort_task, ohci_timeout_task,
916 OXFER(xfer)->ohci_xfer_flags = 0;
918 xfer->busy_free = XFER_BUSY;
925 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
927 struct ohci_softc *sc = (struct ohci_softc *)bus;
928 struct ohci_xfer *oxfer = (struct ohci_xfer *)xfer;
929 ohci_soft_itd_t *sitd;
931 if (oxfer->ohci_xfer_flags & OHCI_ISOC_DIRTY) {
933 for (sitd = xfer->hcpriv; sitd != NULL && sitd->xfer == xfer;
934 sitd = sitd->nextitd)
935 ohci_free_sitd(sc, sitd);
940 if (xfer->busy_free != XFER_BUSY) {
941 kprintf("ohci_freex: xfer=%p not busy, 0x%08x\n", xfer,
945 xfer->busy_free = XFER_FREE;
947 STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
951 * Shut down the controller when the system is going down.
954 ohci_shutdown(void *v)
956 ohci_softc_t *sc = v;
958 DPRINTF(("ohci_shutdown: stopping the HC\n"));
959 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
963 * Handle suspend/resume.
965 * We need to switch to polling mode here, because this routine is
966 * called from an intterupt context. This is all right since we
967 * are almost suspended anyway.
970 ohci_power(int why, void *v)
972 ohci_softc_t *sc = v;
976 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
981 if (why != PWR_RESUME) {
982 sc->sc_bus.use_polling++;
983 ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
984 if (sc->sc_control == 0) {
986 * Preserve register values, in case that APM BIOS
987 * does not recover them.
989 sc->sc_control = ctl;
990 sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE);
992 ctl |= OHCI_HCFS_SUSPEND;
993 OWRITE4(sc, OHCI_CONTROL, ctl);
994 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
995 sc->sc_bus.use_polling--;
997 sc->sc_bus.use_polling++;
999 /* Some broken BIOSes never initialize Controller chip */
1000 ohci_controller_init(sc);
1003 OWRITE4(sc, OHCI_INTERRUPT_ENABLE,
1004 sc->sc_intre & (OHCI_ALL_INTRS | OHCI_MIE));
1006 ctl = sc->sc_control;
1008 ctl = OREAD4(sc, OHCI_CONTROL);
1009 ctl |= OHCI_HCFS_RESUME;
1010 OWRITE4(sc, OHCI_CONTROL, ctl);
1011 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
1012 ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL;
1013 OWRITE4(sc, OHCI_CONTROL, ctl);
1014 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
1015 sc->sc_control = sc->sc_intre = 0;
1016 sc->sc_bus.use_polling--;
1023 ohci_dumpregs(ohci_softc_t *sc)
1025 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
1026 OREAD4(sc, OHCI_REVISION),
1027 OREAD4(sc, OHCI_CONTROL),
1028 OREAD4(sc, OHCI_COMMAND_STATUS)));
1029 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
1030 OREAD4(sc, OHCI_INTERRUPT_STATUS),
1031 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
1032 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
1033 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
1034 OREAD4(sc, OHCI_HCCA),
1035 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
1036 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
1037 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
1038 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
1039 OREAD4(sc, OHCI_BULK_HEAD_ED),
1040 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
1041 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
1042 OREAD4(sc, OHCI_DONE_HEAD),
1043 OREAD4(sc, OHCI_FM_INTERVAL),
1044 OREAD4(sc, OHCI_FM_REMAINING)));
1045 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
1046 OREAD4(sc, OHCI_FM_NUMBER),
1047 OREAD4(sc, OHCI_PERIODIC_START),
1048 OREAD4(sc, OHCI_LS_THRESHOLD)));
1049 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n",
1050 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
1051 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
1052 OREAD4(sc, OHCI_RH_STATUS)));
1053 DPRINTF((" port1=0x%08x port2=0x%08x\n",
1054 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
1055 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
1056 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n",
1057 le32toh(sc->sc_hcca->hcca_frame_number),
1058 le32toh(sc->sc_hcca->hcca_done_head)));
1062 static int ohci_intr1(ohci_softc_t *);
1067 ohci_softc_t *sc = p;
1069 if (sc->sc_dying || (sc->sc_flags & OHCI_SCFLG_DONEINIT) == 0)
1072 /* If we get an interrupt while polling, then just ignore it. */
1073 if (sc->sc_bus.use_polling) {
1075 DPRINTFN(16, ("ohci_intr: ignored interrupt while polling\n"));
1080 return (ohci_intr1(sc));
1084 ohci_intr1(ohci_softc_t *sc)
1086 u_int32_t intrs, eintrs;
1087 ohci_physaddr_t done;
1089 DPRINTFN(14,("ohci_intr1: enter\n"));
1091 /* In case the interrupt occurs before initialization has completed. */
1092 if (sc->sc_hcca == NULL) {
1094 kprintf("ohci_intr: sc->sc_hcca == NULL\n");
1100 done = le32toh(sc->sc_hcca->hcca_done_head);
1102 /* The LSb of done is used to inform the HC Driver that an interrupt
1103 * condition exists for both the Done list and for another event
1104 * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
1105 * Driver checks the HccaDoneHead Value. If this value is 0, then the
1106 * interrupt was caused by other than the HccaDoneHead update and the
1107 * HcInterruptStatus register needs to be accessed to determine that
1108 * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
1109 * update interrupt is indicated and if the LSb of done is nonzero,
1110 * then an additional interrupt event is indicated and
1111 * HcInterruptStatus should be checked to determine its cause.
1114 if (done & ~OHCI_DONE_INTRS)
1116 if (done & OHCI_DONE_INTRS) {
1117 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1118 done &= ~OHCI_DONE_INTRS;
1120 sc->sc_hcca->hcca_done_head = 0;
1122 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & ~OHCI_WDH;
1124 if (intrs == 0) /* nothing to be done (PCI shared interrupt) */
1128 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1129 eintrs = intrs & sc->sc_eintrs;
1133 sc->sc_bus.intr_context++;
1134 sc->sc_bus.no_intrs++;
1135 DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
1136 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1139 if (eintrs & OHCI_SO) {
1140 sc->sc_overrun_cnt++;
1141 if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
1142 kprintf("%s: %u scheduling overruns\n",
1143 device_get_nameunit(sc->sc_bus.bdev), sc->sc_overrun_cnt);
1144 sc->sc_overrun_cnt = 0;
1149 if (eintrs & OHCI_WDH) {
1150 ohci_add_done(sc, done &~ OHCI_DONE_INTRS);
1151 usb_schedsoftintr(&sc->sc_bus);
1152 eintrs &= ~OHCI_WDH;
1154 if (eintrs & OHCI_RD) {
1155 kprintf("%s: resume detect\n", device_get_nameunit(sc->sc_bus.bdev));
1156 /* XXX process resume detect */
1158 if (eintrs & OHCI_UE) {
1159 kprintf("%s: unrecoverable error, controller halted\n",
1160 device_get_nameunit(sc->sc_bus.bdev));
1161 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1164 if (eintrs & OHCI_RHSC) {
1165 ohci_rhsc(sc, sc->sc_intrxfer);
1167 * Disable RHSC interrupt for now, because it will be
1168 * on until the port has been reset.
1170 ohci_rhsc_able(sc, 0);
1171 /* Do not allow RHSC interrupts > 1 per second */
1172 callout_reset(&sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);
1173 eintrs &= ~OHCI_RHSC;
1176 sc->sc_bus.intr_context--;
1179 /* Block unprocessed interrupts. XXX */
1180 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
1181 sc->sc_eintrs &= ~eintrs;
1182 kprintf("%s: blocking intrs 0x%x\n",
1183 device_get_nameunit(sc->sc_bus.bdev), eintrs);
1190 ohci_rhsc_able(ohci_softc_t *sc, int on)
1192 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1194 sc->sc_eintrs |= OHCI_RHSC;
1195 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1197 sc->sc_eintrs &= ~OHCI_RHSC;
1198 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1203 ohci_rhsc_enable(void *v_sc)
1205 ohci_softc_t *sc = v_sc;
1208 ohci_rhsc_able(sc, 1);
1213 char *ohci_cc_strs[] = {
1217 "DATA_TOGGLE_MISMATCH",
1219 "DEVICE_NOT_RESPONDING",
1220 "PID_CHECK_FAILURE",
1234 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done)
1236 ohci_soft_itd_t *sitd, *sidone, **ip;
1237 ohci_soft_td_t *std, *sdone, **p;
1239 /* Reverse the done list. */
1240 for (sdone = NULL, sidone = NULL; done != 0; ) {
1241 std = ohci_hash_find_td(sc, done);
1244 done = le32toh(std->td.td_nexttd);
1246 DPRINTFN(10,("add TD %p\n", std));
1249 sitd = ohci_hash_find_itd(sc, done);
1251 sitd->dnext = sidone;
1252 done = le32toh(sitd->itd.itd_nextitd);
1254 DPRINTFN(5,("add ITD %p\n", sitd));
1257 panic("ohci_add_done: addr 0x%08lx not found", (u_long)done);
1260 /* sdone & sidone now hold the done lists. */
1261 /* Put them on the already processed lists. */
1262 for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext)
1265 for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext)
1271 ohci_softintr(void *v)
1273 ohci_softc_t *sc = v;
1274 ohci_soft_itd_t *sitd, *sidone, *sitdnext;
1275 ohci_soft_td_t *std, *sdone, *stdnext, *p, *n;
1276 usbd_xfer_handle xfer;
1277 struct ohci_pipe *opipe;
1281 DPRINTFN(10,("ohci_softintr: enter\n"));
1283 sc->sc_bus.intr_context++;
1286 sdone = sc->sc_sdone;
1287 sc->sc_sdone = NULL;
1288 sidone = sc->sc_sidone;
1289 sc->sc_sidone = NULL;
1292 DPRINTFN(10,("ohci_softintr: sdone=%p sidone=%p\n", sdone, sidone));
1295 if (ohcidebug > 10) {
1296 DPRINTF(("ohci_process_done: TD done:\n"));
1297 ohci_dump_tds(sdone);
1301 for (std = sdone; std; std = stdnext) {
1303 stdnext = std->dnext;
1304 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1305 std, xfer, (xfer ? xfer->hcpriv : NULL)));
1308 * xfer == NULL: There seems to be no xfer associated
1309 * with this TD. It is tailp that happened to end up on
1314 if (xfer->status == USBD_CANCELLED ||
1315 xfer->status == USBD_TIMEOUT) {
1316 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1318 /* Handled by abort routine. */
1323 if (std->td.td_cbp != 0)
1324 len -= le32toh(std->td.td_be) -
1325 le32toh(std->td.td_cbp) + 1;
1326 DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n", len,
1328 if (std->flags & OHCI_ADD_LEN)
1329 xfer->actlen += len;
1331 cc = OHCI_TD_GET_CC(le32toh(std->td.td_flags));
1332 if (cc != OHCI_CC_NO_ERROR) {
1334 * Endpoint is halted. First unlink all the TDs
1335 * belonging to the failed transfer, and then restart
1338 opipe = (struct ohci_pipe *)xfer->pipe;
1340 DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1341 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1342 ohci_cc_strs[OHCI_TD_GET_CC(le32toh(std->td.td_flags))]));
1343 callout_stop(&xfer->timeout_handle);
1344 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1345 &OXFER(xfer)->abort_task);
1347 /* Remove all this xfer's TDs from the done queue. */
1348 for (p = std; p->dnext != NULL; p = p->dnext) {
1349 if (p->dnext->xfer != xfer)
1351 p->dnext = p->dnext->dnext;
1353 /* The next TD may have been removed. */
1354 stdnext = std->dnext;
1356 /* Remove all TDs belonging to this xfer. */
1357 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1359 ohci_free_std(sc, p);
1363 opipe->sed->ed.ed_headp = htole32(p->physaddr);
1364 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1366 if (cc == OHCI_CC_STALL)
1367 xfer->status = USBD_STALLED;
1369 xfer->status = USBD_IOERROR;
1371 usb_transfer_complete(xfer);
1376 * Skip intermediate TDs. They remain linked from
1377 * xfer->hcpriv and we free them when the transfer completes.
1379 if ((std->flags & OHCI_CALL_DONE) == 0)
1382 /* Normal transfer completion */
1383 callout_stop(&xfer->timeout_handle);
1384 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1385 &OXFER(xfer)->abort_task);
1386 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1388 ohci_free_std(sc, p);
1390 xfer->status = USBD_NORMAL_COMPLETION;
1392 usb_transfer_complete(xfer);
1397 if (ohcidebug > 10) {
1398 DPRINTF(("ohci_softintr: ITD done:\n"));
1399 ohci_dump_itds(sidone);
1403 for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
1405 sitdnext = sitd->dnext;
1406 sitd->flags |= OHCI_ITD_INTFIN;
1407 DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n",
1408 sitd, xfer, xfer ? xfer->hcpriv : 0));
1411 if (xfer->status == USBD_CANCELLED ||
1412 xfer->status == USBD_TIMEOUT) {
1413 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1415 /* Handled by abort routine. */
1419 if (xfer->pipe->aborting)
1420 continue; /*Ignore.*/
1423 kprintf("ohci_softintr: sitd=%p is done\n", sitd);
1426 opipe = (struct ohci_pipe *)xfer->pipe;
1427 if (opipe->aborting)
1430 if (sitd->flags & OHCI_CALL_DONE) {
1431 ohci_soft_itd_t *next;
1433 opipe->u.iso.inuse -= xfer->nframes;
1434 xfer->status = USBD_NORMAL_COMPLETION;
1435 for (i = 0, sitd = xfer->hcpriv;;sitd = next) {
1436 next = sitd->nextitd;
1437 if (OHCI_ITD_GET_CC(sitd->itd.itd_flags) != OHCI_CC_NO_ERROR)
1438 xfer->status = USBD_IOERROR;
1440 if (xfer->status == USBD_NORMAL_COMPLETION) {
1441 iframes = OHCI_ITD_GET_FC(sitd->itd.itd_flags);
1442 for (j = 0; j < iframes; i++, j++) {
1443 len = le16toh(sitd->itd.itd_offset[j]);
1445 (OHCI_ITD_PSW_GET_CC(len) ==
1446 OHCI_CC_NOT_ACCESSED) ? 0 :
1447 OHCI_ITD_PSW_LENGTH(len);
1448 xfer->frlengths[i] = len;
1451 if (sitd->flags & OHCI_CALL_DONE)
1456 usb_transfer_complete(xfer);
1461 #ifdef USB_USE_SOFTINTR
1462 if (sc->sc_softwake) {
1463 sc->sc_softwake = 0;
1464 wakeup(&sc->sc_softwake);
1466 #endif /* USB_USE_SOFTINTR */
1468 sc->sc_bus.intr_context--;
1469 DPRINTFN(10,("ohci_softintr: done:\n"));
1473 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1475 DPRINTFN(10,("ohci_device_ctrl_done: xfer=%p\n", xfer));
1478 if (!(xfer->rqflags & URQ_REQUEST)) {
1479 panic("ohci_device_ctrl_done: not a request");
1482 xfer->hcpriv = NULL;
1486 ohci_device_intr_done(usbd_xfer_handle xfer)
1488 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1489 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1490 ohci_soft_ed_t *sed = opipe->sed;
1491 ohci_soft_td_t *data, *tail;
1494 DPRINTFN(10,("ohci_device_intr_done: xfer=%p, actlen=%d\n",
1495 xfer, xfer->actlen));
1497 xfer->hcpriv = NULL;
1499 if (xfer->pipe->repeat) {
1500 data = opipe->tail.td;
1501 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1503 xfer->status = USBD_NOMEM;
1508 data->td.td_flags = htole32(
1509 OHCI_TD_IN | OHCI_TD_NOCC |
1510 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1511 if (xfer->flags & USBD_SHORT_XFER_OK)
1512 data->td.td_flags |= htole32(OHCI_TD_R);
1513 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
1514 data->nexttd = tail;
1515 data->td.td_nexttd = htole32(tail->physaddr);
1516 data->td.td_be = htole32(le32toh(data->td.td_cbp) +
1518 data->len = xfer->length;
1520 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1521 xfer->hcpriv = data;
1524 sed->ed.ed_tailp = htole32(tail->physaddr);
1525 opipe->tail.td = tail;
1530 ohci_device_bulk_done(usbd_xfer_handle xfer)
1532 DPRINTFN(10,("ohci_device_bulk_done: xfer=%p, actlen=%d\n",
1533 xfer, xfer->actlen));
1535 xfer->hcpriv = NULL;
1539 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1541 usbd_pipe_handle pipe;
1546 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1547 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1548 sc, xfer, hstatus));
1551 /* Just ignore the change. */
1557 p = KERNADDR(&xfer->dmabuf, 0);
1558 m = min(sc->sc_noport, xfer->length * 8 - 1);
1559 memset(p, 0, xfer->length);
1560 for (i = 1; i <= m; i++) {
1561 /* Pick out CHANGE bits from the status reg. */
1562 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1563 p[i/8] |= 1 << (i%8);
1565 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1566 xfer->actlen = xfer->length;
1567 xfer->status = USBD_NORMAL_COMPLETION;
1569 usb_transfer_complete(xfer);
1573 ohci_root_intr_done(usbd_xfer_handle xfer)
1575 xfer->hcpriv = NULL;
1579 ohci_root_ctrl_done(usbd_xfer_handle xfer)
1581 xfer->hcpriv = NULL;
1585 * Wait here until controller claims to have an interrupt.
1586 * Then call ohci_intr and return. Use timeout to avoid waiting
1590 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1592 int timo = xfer->timeout;
1596 xfer->status = USBD_IN_PROGRESS;
1597 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1598 usb_delay_ms(&sc->sc_bus, 1);
1601 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1602 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1609 if (xfer->status != USBD_IN_PROGRESS)
1615 DPRINTF(("ohci_waitintr: timeout\n"));
1616 xfer->status = USBD_TIMEOUT;
1617 usb_transfer_complete(xfer);
1618 /* XXX should free TD */
1622 ohci_poll(struct usbd_bus *bus)
1624 ohci_softc_t *sc = (ohci_softc_t *)bus;
1628 new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1630 DPRINTFN(10,("ohci_poll: intrs=0x%04x\n", new));
1635 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1640 ohci_device_request(usbd_xfer_handle xfer)
1642 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1643 usb_device_request_t *req = &xfer->request;
1644 usbd_device_handle dev = opipe->pipe.device;
1645 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1646 int addr = dev->address;
1647 ohci_soft_td_t *setup, *stat, *next, *tail;
1648 ohci_soft_ed_t *sed;
1653 isread = req->bmRequestType & UT_READ;
1654 len = UGETW(req->wLength);
1656 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1657 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1658 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1659 UGETW(req->wIndex), len, addr,
1660 opipe->pipe.endpoint->edesc->bEndpointAddress));
1662 setup = opipe->tail.td;
1663 stat = ohci_alloc_std(sc);
1668 tail = ohci_alloc_std(sc);
1676 opipe->u.ctl.length = len;
1678 /* Update device address and length since they may have changed
1679 during the setup of the control pipe in usbd_new_device(). */
1680 /* XXX This only needs to be done once, but it's too early in open. */
1681 /* XXXX Should not touch ED here! */
1682 sed->ed.ed_flags = htole32(
1683 (le32toh(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1684 OHCI_ED_SET_FA(addr) |
1685 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1689 /* Set up data transaction */
1691 ohci_soft_td_t *std = stat;
1693 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
1695 stat = stat->nexttd; /* point at free TD */
1698 /* Start toggle at 1 and then use the carried toggle. */
1699 std->td.td_flags &= htole32(~OHCI_TD_TOGGLE_MASK);
1700 std->td.td_flags |= htole32(OHCI_TD_TOGGLE_1);
1703 memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1705 setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1706 OHCI_TD_TOGGLE_0 | OHCI_TD_SET_DI(6));
1707 setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma, 0));
1708 setup->nexttd = next;
1709 setup->td.td_nexttd = htole32(next->physaddr);
1710 setup->td.td_be = htole32(le32toh(setup->td.td_cbp) + sizeof *req - 1);
1714 xfer->hcpriv = setup;
1716 stat->td.td_flags = htole32(
1717 (isread ? OHCI_TD_OUT : OHCI_TD_IN) |
1718 OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1719 stat->td.td_cbp = 0;
1720 stat->nexttd = tail;
1721 stat->td.td_nexttd = htole32(tail->physaddr);
1723 stat->flags = OHCI_CALL_DONE;
1728 if (ohcidebug > 5) {
1729 DPRINTF(("ohci_device_request:\n"));
1731 ohci_dump_tds(setup);
1735 /* Insert ED in schedule */
1737 sed->ed.ed_tailp = htole32(tail->physaddr);
1738 opipe->tail.td = tail;
1739 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1740 if (xfer->timeout && !sc->sc_bus.use_polling) {
1741 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1742 ohci_timeout, xfer);
1747 if (ohcidebug > 20) {
1749 DPRINTF(("ohci_device_request: status=%x\n",
1750 OREAD4(sc, OHCI_COMMAND_STATUS)));
1752 kprintf("ctrl head:\n");
1753 ohci_dump_ed(sc->sc_ctrl_head);
1756 ohci_dump_tds(setup);
1760 return (USBD_NORMAL_COMPLETION);
1763 ohci_free_std(sc, tail);
1765 ohci_free_std(sc, stat);
1771 * Add an ED to the schedule. Called from a critical section.
1774 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1776 DPRINTFN(8,("ohci_add_ed: sed=%p head=%p\n", sed, head));
1778 sed->next = head->next;
1779 sed->ed.ed_nexted = head->ed.ed_nexted;
1781 head->ed.ed_nexted = htole32(sed->physaddr);
1785 * Remove an ED from the schedule. Called from a critical section.
1788 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1794 for (p = head; p != NULL && p->next != sed; p = p->next)
1798 panic("ohci_rem_ed: ED not found");
1802 * p == NULL if ohci is detaching and there are still devices
1803 * using ohci (e.g. usb sticks are still plugged in). But
1804 * the real solution should be correcting ohci_free_sed() or
1808 p->next = sed->next;
1809 p->ed.ed_nexted = sed->ed.ed_nexted;
1815 * When a transfer is completed the TD is added to the done queue by
1816 * the host controller. This queue is the processed by software.
1817 * Unfortunately the queue contains the physical address of the TD
1818 * and we have no simple way to translate this back to a kernel address.
1819 * To make the translation possible (and fast) we use a hash table of
1820 * TDs currently in the schedule. The physical address is used as the
1824 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1826 * Called from a critical section
1829 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1831 int h = HASH(std->physaddr);
1833 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1837 * Called from a critical section
1840 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1842 LIST_REMOVE(std, hnext);
1846 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1849 ohci_soft_td_t *std;
1851 /* if these are present they should be masked out at an earlier
1854 KASSERT((a&~OHCI_HEADMASK) == 0, ("%s: 0x%b has lower bits set\n",
1855 device_get_nameunit(sc->sc_bus.bdev),
1856 (int) a, "\20\1HALT\2TOGGLE"));
1858 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1860 std = LIST_NEXT(std, hnext))
1861 if (std->physaddr == a)
1864 DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1865 device_get_nameunit(sc->sc_bus.bdev), (u_long) a));
1870 * Called from a critical section
1873 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1875 int h = HASH(sitd->physaddr);
1877 DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n",
1878 sitd, (u_long)sitd->physaddr));
1880 LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
1884 * Called from a critical section
1887 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1889 DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n",
1890 sitd, (u_long)sitd->physaddr));
1892 LIST_REMOVE(sitd, hnext);
1896 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
1899 ohci_soft_itd_t *sitd;
1901 for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
1903 sitd = LIST_NEXT(sitd, hnext))
1904 if (sitd->physaddr == a)
1910 ohci_timeout(void *addr)
1912 struct ohci_xfer *oxfer = addr;
1913 struct ohci_pipe *opipe = (struct ohci_pipe *)oxfer->xfer.pipe;
1914 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1916 DPRINTF(("ohci_timeout: oxfer=%p\n", oxfer));
1919 ohci_abort_xfer(&oxfer->xfer, USBD_TIMEOUT);
1923 /* Execute the abort in a process context. */
1924 usb_add_task(oxfer->xfer.pipe->device, &oxfer->abort_task,
1929 ohci_timeout_task(void *addr)
1931 usbd_xfer_handle xfer = addr;
1933 DPRINTF(("ohci_timeout_task: xfer=%p\n", xfer));
1936 ohci_abort_xfer(xfer, USBD_TIMEOUT);
1942 ohci_dump_tds(ohci_soft_td_t *std)
1944 for (; std; std = std->nexttd)
1949 ohci_dump_td(ohci_soft_td_t *std)
1953 bitmask_snprintf((u_int32_t)le32toh(std->td.td_flags),
1954 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1955 sbuf, sizeof(sbuf));
1957 kprintf("TD(%p) at %08lx: %s delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1958 "nexttd=0x%08lx be=0x%08lx\n",
1959 std, (u_long)std->physaddr, sbuf,
1960 OHCI_TD_GET_DI(le32toh(std->td.td_flags)),
1961 OHCI_TD_GET_EC(le32toh(std->td.td_flags)),
1962 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1963 (u_long)le32toh(std->td.td_cbp),
1964 (u_long)le32toh(std->td.td_nexttd),
1965 (u_long)le32toh(std->td.td_be));
1969 ohci_dump_itd(ohci_soft_itd_t *sitd)
1973 kprintf("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n"
1974 "bp0=0x%08lx next=0x%08lx be=0x%08lx\n",
1975 sitd, (u_long)sitd->physaddr,
1976 OHCI_ITD_GET_SF(le32toh(sitd->itd.itd_flags)),
1977 OHCI_ITD_GET_DI(le32toh(sitd->itd.itd_flags)),
1978 OHCI_ITD_GET_FC(le32toh(sitd->itd.itd_flags)),
1979 OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags)),
1980 (u_long)le32toh(sitd->itd.itd_bp0),
1981 (u_long)le32toh(sitd->itd.itd_nextitd),
1982 (u_long)le32toh(sitd->itd.itd_be));
1983 for (i = 0; i < OHCI_ITD_NOFFSET; i++)
1984 kprintf("offs[%d]=0x%04x ", i,
1985 (u_int)le16toh(sitd->itd.itd_offset[i]));
1990 ohci_dump_itds(ohci_soft_itd_t *sitd)
1992 for (; sitd; sitd = sitd->nextitd)
1993 ohci_dump_itd(sitd);
1997 ohci_dump_ed(ohci_soft_ed_t *sed)
1999 char sbuf[128], sbuf2[128];
2001 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_flags),
2002 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
2003 sbuf, sizeof(sbuf));
2004 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_headp),
2005 "\20\1HALT\2CARRY", sbuf2, sizeof(sbuf2));
2007 kprintf("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d flags=%s\ntailp=0x%08lx "
2008 "headflags=%s headp=0x%08lx nexted=0x%08lx\n",
2009 sed, (u_long)sed->physaddr,
2010 OHCI_ED_GET_FA(le32toh(sed->ed.ed_flags)),
2011 OHCI_ED_GET_EN(le32toh(sed->ed.ed_flags)),
2012 OHCI_ED_GET_MAXP(le32toh(sed->ed.ed_flags)), sbuf,
2013 (u_long)le32toh(sed->ed.ed_tailp), sbuf2,
2014 (u_long)le32toh(sed->ed.ed_headp),
2015 (u_long)le32toh(sed->ed.ed_nexted));
2020 ohci_open(usbd_pipe_handle pipe)
2022 usbd_device_handle dev = pipe->device;
2023 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2024 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2025 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2026 u_int8_t addr = dev->address;
2027 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
2028 ohci_soft_ed_t *sed;
2029 ohci_soft_td_t *std;
2030 ohci_soft_itd_t *sitd;
2031 ohci_physaddr_t tdphys;
2036 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2037 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
2040 return (USBD_IOERROR);
2045 if (addr == sc->sc_addr) {
2046 switch (ed->bEndpointAddress) {
2047 case USB_CONTROL_ENDPOINT:
2048 pipe->methods = &ohci_root_ctrl_methods;
2050 case UE_DIR_IN | OHCI_INTR_ENDPT:
2051 pipe->methods = &ohci_root_intr_methods;
2054 return (USBD_INVAL);
2057 sed = ohci_alloc_sed(sc);
2061 if (xfertype == UE_ISOCHRONOUS) {
2062 sitd = ohci_alloc_sitd(sc);
2065 opipe->tail.itd = sitd;
2066 opipe->aborting = 0;
2067 tdphys = sitd->physaddr;
2068 fmt = OHCI_ED_FORMAT_ISO;
2069 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
2070 fmt |= OHCI_ED_DIR_IN;
2072 fmt |= OHCI_ED_DIR_OUT;
2074 std = ohci_alloc_std(sc);
2077 opipe->tail.td = std;
2078 tdphys = std->physaddr;
2079 fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
2081 sed->ed.ed_flags = htole32(
2082 OHCI_ED_SET_FA(addr) |
2083 OHCI_ED_SET_EN(UE_GET_ADDR(ed->bEndpointAddress)) |
2084 (dev->speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) |
2086 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
2087 sed->ed.ed_headp = htole32(tdphys |
2088 (pipe->endpoint->savedtoggle ? OHCI_TOGGLECARRY : 0));
2089 sed->ed.ed_tailp = htole32(tdphys);
2093 pipe->methods = &ohci_device_ctrl_methods;
2094 err = usb_allocmem(&sc->sc_bus,
2095 sizeof(usb_device_request_t),
2096 0, &opipe->u.ctl.reqdma);
2100 ohci_add_ed(sed, sc->sc_ctrl_head);
2104 pipe->methods = &ohci_device_intr_methods;
2105 ival = pipe->interval;
2106 if (ival == USBD_DEFAULT_INTERVAL)
2107 ival = ed->bInterval;
2108 return (ohci_device_setintr(sc, opipe, ival));
2109 case UE_ISOCHRONOUS:
2110 pipe->methods = &ohci_device_isoc_methods;
2111 return (ohci_setup_isoc(pipe));
2113 pipe->methods = &ohci_device_bulk_methods;
2115 ohci_add_ed(sed, sc->sc_bulk_head);
2120 return (USBD_NORMAL_COMPLETION);
2124 ohci_free_std(sc, std);
2127 ohci_free_sed(sc, sed);
2129 return (USBD_NOMEM);
2134 * Close a reqular pipe.
2135 * Assumes that there are no pending transactions.
2138 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
2140 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2141 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2142 ohci_soft_ed_t *sed = opipe->sed;
2146 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2147 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2148 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK)) {
2149 ohci_soft_td_t *std;
2150 std = ohci_hash_find_td(sc, le32toh(sed->ed.ed_headp));
2151 kprintf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
2152 "tl=0x%x pipe=%p, std=%p\n", sed,
2153 (int)le32toh(sed->ed.ed_headp),
2154 (int)le32toh(sed->ed.ed_tailp),
2157 usbd_dump_pipe(&opipe->pipe);
2164 usb_delay_ms(&sc->sc_bus, 2);
2165 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2166 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2167 kprintf("ohci_close_pipe: pipe still not empty\n");
2170 ohci_rem_ed(sed, head);
2171 /* Make sure the host controller is not touching this ED */
2172 usb_delay_ms(&sc->sc_bus, 1);
2174 pipe->endpoint->savedtoggle =
2175 (le32toh(sed->ed.ed_headp) & OHCI_TOGGLECARRY) ? 1 : 0;
2176 ohci_free_sed(sc, opipe->sed);
2180 * Abort a device request.
2181 * If this routine is called from a critical section it guarantees that
2182 * the request will be removed from the hardware scheduling and that
2183 * the callback for it will be called with USBD_CANCELLED status.
2184 * It's impossible to guarantee that the requested transfer will not
2185 * have happened since the hardware runs concurrently.
2186 * If the transaction has already happened we rely on the ordinary
2187 * interrupt processing to process it.
2190 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2192 struct ohci_xfer *oxfer = OXFER(xfer);
2193 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2194 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
2195 ohci_soft_ed_t *sed = opipe->sed;
2196 ohci_soft_td_t *p, *n;
2197 ohci_physaddr_t headp;
2200 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p sed=%p\n", xfer, opipe,sed));
2203 /* If we're dying, just do the software part. */
2205 xfer->status = status; /* make software ignore it */
2206 callout_stop(&xfer->timeout_handle);
2207 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2208 usb_transfer_complete(xfer);
2213 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
2214 panic("ohci_abort_xfer: not in process context");
2217 * If an abort is already in progress then just wait for it to
2218 * complete and return.
2220 if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING) {
2221 DPRINTFN(2, ("ohci_abort_xfer: already aborting\n"));
2222 /* No need to wait if we're aborting from a timeout. */
2223 if (status == USBD_TIMEOUT)
2225 /* Override the status which might be USBD_TIMEOUT. */
2226 xfer->status = status;
2227 DPRINTFN(2, ("ohci_abort_xfer: waiting for abort to finish\n"));
2228 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTWAIT;
2229 while (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING)
2230 tsleep(&oxfer->ohci_xfer_flags, 0, "ohciaw", 0);
2235 * Step 1: Make interrupt routine and hardware ignore xfer.
2238 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTING;
2239 xfer->status = status; /* make software ignore it */
2240 callout_stop(&xfer->timeout_handle);
2241 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2243 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
2244 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
2247 * Step 2: Wait until we know hardware has finished any possible
2248 * use of the xfer. Also make sure the soft interrupt routine
2251 usb_delay_ms(opipe->pipe.device->bus, 20); /* Hardware finishes in 1ms */
2253 #ifdef USB_USE_SOFTINTR
2254 sc->sc_softwake = 1;
2255 #endif /* USB_USE_SOFTINTR */
2256 usb_schedsoftintr(&sc->sc_bus);
2257 #ifdef USB_USE_SOFTINTR
2258 tsleep(&sc->sc_softwake, 0, "ohciab", 0);
2259 #endif /* USB_USE_SOFTINTR */
2262 * Step 3: Remove any vestiges of the xfer from the hardware.
2263 * The complication here is that the hardware may have executed
2264 * beyond the xfer we're trying to abort. So as we're scanning
2265 * the TDs of this xfer we check if the hardware points to
2271 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING; /* XXX */
2273 kprintf("ohci_abort_xfer: hcpriv is NULL\n");
2278 if (ohcidebug > 1) {
2279 DPRINTF(("ohci_abort_xfer: sed=\n"));
2284 headp = le32toh(sed->ed.ed_headp) & OHCI_HEADMASK;
2286 for (; p->xfer == xfer; p = n) {
2287 hit |= headp == p->physaddr;
2289 ohci_free_std(sc, p);
2291 /* Zap headp register if hardware pointed inside the xfer. */
2293 DPRINTFN(1,("ohci_abort_xfer: set hd=0x08%x, tl=0x%08x\n",
2294 (int)p->physaddr, (int)le32toh(sed->ed.ed_tailp)));
2295 sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */
2297 DPRINTFN(1,("ohci_abort_xfer: no hit\n"));
2301 * Step 4: Turn on hardware again.
2303 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
2306 * Step 5: Execute callback.
2308 /* Do the wakeup first to avoid touching the xfer after the callback. */
2309 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING;
2310 if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTWAIT) {
2311 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTWAIT;
2312 wakeup(&oxfer->ohci_xfer_flags);
2314 usb_transfer_complete(xfer);
2320 * Data structures and routines to emulate the root hub.
2322 static usb_device_descriptor_t ohci_devd = {
2323 USB_DEVICE_DESCRIPTOR_SIZE,
2324 UDESC_DEVICE, /* type */
2325 {0x00, 0x01}, /* USB version */
2326 UDCLASS_HUB, /* class */
2327 UDSUBCLASS_HUB, /* subclass */
2328 UDPROTO_FSHUB, /* protocol */
2329 64, /* max packet */
2330 {0},{0},{0x00,0x01}, /* device id */
2331 1,2,0, /* string indicies */
2332 1 /* # of configurations */
2335 static usb_config_descriptor_t ohci_confd = {
2336 USB_CONFIG_DESCRIPTOR_SIZE,
2338 {USB_CONFIG_DESCRIPTOR_SIZE +
2339 USB_INTERFACE_DESCRIPTOR_SIZE +
2340 USB_ENDPOINT_DESCRIPTOR_SIZE},
2348 static usb_interface_descriptor_t ohci_ifcd = {
2349 USB_INTERFACE_DESCRIPTOR_SIZE,
2360 static usb_endpoint_descriptor_t ohci_endpd = {
2361 USB_ENDPOINT_DESCRIPTOR_SIZE,
2363 UE_DIR_IN | OHCI_INTR_ENDPT,
2365 {8, 0}, /* max packet */
2369 static usb_hub_descriptor_t ohci_hubd = {
2370 USB_HUB_DESCRIPTOR_SIZE,
2380 ohci_str(usb_string_descriptor_t *p, int l, const char *s)
2386 p->bLength = 2 * strlen(s) + 2;
2389 p->bDescriptorType = UDESC_STRING;
2391 for (i = 0; s[i] && l > 1; i++, l -= 2)
2392 USETW2(p->bString[i], 0, s[i]);
2397 * Simulate a hardware hub by handling all the necessary requests.
2400 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
2404 /* Insert last in queue. */
2405 err = usb_insert_transfer(xfer);
2409 /* Pipe isn't running, start first */
2410 return (ohci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2414 ohci_root_ctrl_start(usbd_xfer_handle xfer)
2416 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2417 usb_device_request_t *req;
2420 int len, value, index, l, totlen = 0;
2421 usb_port_status_t ps;
2422 usb_hub_descriptor_t hubd;
2427 return (USBD_IOERROR);
2430 if (!(xfer->rqflags & URQ_REQUEST))
2432 return (USBD_INVAL);
2434 req = &xfer->request;
2436 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2437 req->bmRequestType, req->bRequest));
2439 len = UGETW(req->wLength);
2440 value = UGETW(req->wValue);
2441 index = UGETW(req->wIndex);
2444 buf = KERNADDR(&xfer->dmabuf, 0);
2446 #define C(x,y) ((x) | ((y) << 8))
2447 switch(C(req->bRequest, req->bmRequestType)) {
2448 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2449 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2450 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2452 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2453 * for the integrated root hub.
2456 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2458 *(u_int8_t *)buf = sc->sc_conf;
2462 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2463 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2464 switch(value >> 8) {
2466 if ((value & 0xff) != 0) {
2470 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2471 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2472 memcpy(buf, &ohci_devd, l);
2475 if ((value & 0xff) != 0) {
2479 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2480 memcpy(buf, &ohci_confd, l);
2481 buf = (char *)buf + l;
2483 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2485 memcpy(buf, &ohci_ifcd, l);
2486 buf = (char *)buf + l;
2488 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2490 memcpy(buf, &ohci_endpd, l);
2495 *(u_int8_t *)buf = 0;
2497 switch (value & 0xff) {
2498 case 1: /* Vendor */
2499 totlen = ohci_str(buf, len, sc->sc_vendor);
2501 case 2: /* Product */
2502 totlen = ohci_str(buf, len, "OHCI root hub");
2511 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2513 *(u_int8_t *)buf = 0;
2517 case C(UR_GET_STATUS, UT_READ_DEVICE):
2519 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2523 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2524 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2526 USETW(((usb_status_t *)buf)->wStatus, 0);
2530 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2531 if (value >= USB_MAX_DEVICES) {
2535 sc->sc_addr = value;
2537 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2538 if (value != 0 && value != 1) {
2542 sc->sc_conf = value;
2544 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2546 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2547 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2548 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2551 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2553 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2556 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2558 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2559 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2560 "port=%d feature=%d\n",
2562 if (index < 1 || index > sc->sc_noport) {
2566 port = OHCI_RH_PORT_STATUS(index);
2568 case UHF_PORT_ENABLE:
2569 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2571 case UHF_PORT_SUSPEND:
2572 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2574 case UHF_PORT_POWER:
2575 /* Yes, writing to the LOW_SPEED bit clears power. */
2576 OWRITE4(sc, port, UPS_LOW_SPEED);
2578 case UHF_C_PORT_CONNECTION:
2579 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2581 case UHF_C_PORT_ENABLE:
2582 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2584 case UHF_C_PORT_SUSPEND:
2585 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2587 case UHF_C_PORT_OVER_CURRENT:
2588 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2590 case UHF_C_PORT_RESET:
2591 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2598 case UHF_C_PORT_CONNECTION:
2599 case UHF_C_PORT_ENABLE:
2600 case UHF_C_PORT_SUSPEND:
2601 case UHF_C_PORT_OVER_CURRENT:
2602 case UHF_C_PORT_RESET:
2603 /* Enable RHSC interrupt if condition is cleared. */
2604 if ((OREAD4(sc, port) >> 16) == 0)
2605 ohci_rhsc_able(sc, 1);
2611 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2612 if ((value & 0xff) != 0) {
2616 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2618 hubd.bNbrPorts = sc->sc_noport;
2619 USETW(hubd.wHubCharacteristics,
2620 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2621 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2622 /* XXX overcurrent */
2624 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2625 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2626 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2627 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2628 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2629 l = min(len, hubd.bDescLength);
2631 memcpy(buf, &hubd, l);
2633 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2638 memset(buf, 0, len); /* ? XXX */
2641 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2642 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2644 if (index < 1 || index > sc->sc_noport) {
2652 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2653 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2655 USETW(ps.wPortStatus, v);
2656 USETW(ps.wPortChange, v >> 16);
2657 l = min(len, sizeof ps);
2658 memcpy(buf, &ps, l);
2661 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2664 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2666 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2667 if (index < 1 || index > sc->sc_noport) {
2671 port = OHCI_RH_PORT_STATUS(index);
2673 case UHF_PORT_ENABLE:
2674 OWRITE4(sc, port, UPS_PORT_ENABLED);
2676 case UHF_PORT_SUSPEND:
2677 OWRITE4(sc, port, UPS_SUSPEND);
2679 case UHF_PORT_RESET:
2680 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2682 OWRITE4(sc, port, UPS_RESET);
2683 for (i = 0; i < 5; i++) {
2684 usb_delay_ms(&sc->sc_bus,
2685 USB_PORT_ROOT_RESET_DELAY);
2690 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2693 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2694 index, OREAD4(sc, port)));
2696 case UHF_PORT_POWER:
2697 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2699 OWRITE4(sc, port, UPS_PORT_POWER);
2710 xfer->actlen = totlen;
2711 err = USBD_NORMAL_COMPLETION;
2715 usb_transfer_complete(xfer);
2717 return (USBD_IN_PROGRESS);
2720 /* Abort a root control request. */
2722 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2724 /* Nothing to do, all transfers are synchronous. */
2727 /* Close the root pipe. */
2729 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2731 DPRINTF(("ohci_root_ctrl_close\n"));
2732 /* Nothing to do. */
2736 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2740 /* Insert last in queue. */
2741 err = usb_insert_transfer(xfer);
2745 /* Pipe isn't running, start first */
2746 return (ohci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
2750 ohci_root_intr_start(usbd_xfer_handle xfer)
2752 usbd_pipe_handle pipe = xfer->pipe;
2753 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2756 return (USBD_IOERROR);
2758 sc->sc_intrxfer = xfer;
2760 return (USBD_IN_PROGRESS);
2763 /* Abort a root interrupt request. */
2765 ohci_root_intr_abort(usbd_xfer_handle xfer)
2767 if (xfer->pipe->intrxfer == xfer) {
2768 DPRINTF(("ohci_root_intr_abort: remove\n"));
2769 xfer->pipe->intrxfer = NULL;
2771 xfer->status = USBD_CANCELLED;
2773 usb_transfer_complete(xfer);
2777 /* Close the root pipe. */
2779 ohci_root_intr_close(usbd_pipe_handle pipe)
2781 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2783 DPRINTF(("ohci_root_intr_close\n"));
2785 sc->sc_intrxfer = NULL;
2788 /************************/
2791 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2795 /* Insert last in queue. */
2796 err = usb_insert_transfer(xfer);
2800 /* Pipe isn't running, start first */
2801 return (ohci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2805 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2807 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2811 return (USBD_IOERROR);
2814 if (!(xfer->rqflags & URQ_REQUEST)) {
2816 kprintf("ohci_device_ctrl_transfer: not a request\n");
2817 return (USBD_INVAL);
2821 err = ohci_device_request(xfer);
2825 if (sc->sc_bus.use_polling)
2826 ohci_waitintr(sc, xfer);
2827 return (USBD_IN_PROGRESS);
2830 /* Abort a device control request. */
2832 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2834 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2835 ohci_abort_xfer(xfer, USBD_CANCELLED);
2838 /* Close a device control pipe. */
2840 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2842 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2843 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2845 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2846 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2847 ohci_free_std(sc, opipe->tail.td);
2850 /************************/
2853 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2855 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2857 opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY);
2861 ohci_noop(usbd_pipe_handle pipe)
2866 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2870 /* Insert last in queue. */
2871 err = usb_insert_transfer(xfer);
2875 /* Pipe isn't running, start first */
2876 return (ohci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
2880 ohci_device_bulk_start(usbd_xfer_handle xfer)
2882 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2883 usbd_device_handle dev = opipe->pipe.device;
2884 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2885 int addr = dev->address;
2886 ohci_soft_td_t *data, *tail, *tdp;
2887 ohci_soft_ed_t *sed;
2888 int len, isread, endpt;
2892 return (USBD_IOERROR);
2895 if (xfer->rqflags & URQ_REQUEST) {
2897 kprintf("ohci_device_bulk_start: a request\n");
2898 return (USBD_INVAL);
2903 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2904 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2907 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2908 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2911 opipe->u.bulk.isread = isread;
2912 opipe->u.bulk.length = len;
2914 /* Update device address */
2915 sed->ed.ed_flags = htole32(
2916 (le32toh(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2917 OHCI_ED_SET_FA(addr));
2919 /* Allocate a chain of new TDs (including a new tail). */
2920 data = opipe->tail.td;
2921 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
2923 /* We want interrupt at the end of the transfer. */
2924 tail->td.td_flags &= htole32(~OHCI_TD_INTR_MASK);
2925 tail->td.td_flags |= htole32(OHCI_TD_SET_DI(1));
2926 tail->flags |= OHCI_CALL_DONE;
2927 tail = tail->nexttd; /* point at sentinel */
2932 xfer->hcpriv = data;
2934 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2935 "td_cbp=0x%08x td_be=0x%08x\n",
2936 (int)le32toh(sed->ed.ed_flags),
2937 (int)le32toh(data->td.td_flags),
2938 (int)le32toh(data->td.td_cbp),
2939 (int)le32toh(data->td.td_be)));
2942 if (ohcidebug > 5) {
2944 ohci_dump_tds(data);
2948 /* Insert ED in schedule */
2950 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2953 sed->ed.ed_tailp = htole32(tail->physaddr);
2954 opipe->tail.td = tail;
2955 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
2956 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2957 if (xfer->timeout && !sc->sc_bus.use_polling) {
2958 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2959 ohci_timeout, xfer);
2963 /* This goes wrong if we are too slow. */
2964 if (ohcidebug > 10) {
2966 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2967 OREAD4(sc, OHCI_COMMAND_STATUS)));
2969 ohci_dump_tds(data);
2975 if (sc->sc_bus.use_polling)
2976 ohci_waitintr(sc, xfer);
2978 return (USBD_IN_PROGRESS);
2982 ohci_device_bulk_abort(usbd_xfer_handle xfer)
2984 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
2985 ohci_abort_xfer(xfer, USBD_CANCELLED);
2989 * Close a device bulk pipe.
2992 ohci_device_bulk_close(usbd_pipe_handle pipe)
2994 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2995 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2997 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
2998 ohci_close_pipe(pipe, sc->sc_bulk_head);
2999 ohci_free_std(sc, opipe->tail.td);
3002 /************************/
3005 ohci_device_intr_transfer(usbd_xfer_handle xfer)
3009 /* Insert last in queue. */
3010 err = usb_insert_transfer(xfer);
3014 /* Pipe isn't running, start first */
3015 return (ohci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3019 ohci_device_intr_start(usbd_xfer_handle xfer)
3021 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3022 usbd_device_handle dev = opipe->pipe.device;
3023 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3024 ohci_soft_ed_t *sed = opipe->sed;
3025 ohci_soft_td_t *data, *tail;
3029 return (USBD_IOERROR);
3031 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
3032 "flags=%d priv=%p\n",
3033 xfer, xfer->length, xfer->flags, xfer->priv));
3036 if (xfer->rqflags & URQ_REQUEST)
3037 panic("ohci_device_intr_transfer: a request");
3042 data = opipe->tail.td;
3043 tail = ohci_alloc_std(sc);
3045 return (USBD_NOMEM);
3048 data->td.td_flags = htole32(
3049 OHCI_TD_IN | OHCI_TD_NOCC |
3050 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
3051 if (xfer->flags & USBD_SHORT_XFER_OK)
3052 data->td.td_flags |= htole32(OHCI_TD_R);
3053 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
3054 data->nexttd = tail;
3055 data->td.td_nexttd = htole32(tail->physaddr);
3056 data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1);
3059 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
3060 xfer->hcpriv = data;
3063 if (ohcidebug > 5) {
3064 DPRINTF(("ohci_device_intr_transfer:\n"));
3066 ohci_dump_tds(data);
3070 /* Insert ED in schedule */
3072 sed->ed.ed_tailp = htole32(tail->physaddr);
3073 opipe->tail.td = tail;
3074 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3078 * This goes horribly wrong, printing thousands of descriptors,
3079 * because false references are followed due to the fact that the
3082 if (ohcidebug > 5) {
3083 usb_delay_ms(&sc->sc_bus, 5);
3084 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
3085 OREAD4(sc, OHCI_COMMAND_STATUS)));
3087 ohci_dump_tds(data);
3092 return (USBD_IN_PROGRESS);
3095 /* Abort a device control request. */
3097 ohci_device_intr_abort(usbd_xfer_handle xfer)
3099 if (xfer->pipe->intrxfer == xfer) {
3100 DPRINTF(("ohci_device_intr_abort: remove\n"));
3101 xfer->pipe->intrxfer = NULL;
3103 ohci_abort_xfer(xfer, USBD_CANCELLED);
3106 /* Close a device interrupt pipe. */
3108 ohci_device_intr_close(usbd_pipe_handle pipe)
3110 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3111 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3112 int nslots = opipe->u.intr.nslots;
3113 int pos = opipe->u.intr.pos;
3115 ohci_soft_ed_t *p, *sed = opipe->sed;
3117 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
3118 pipe, nslots, pos));
3120 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
3121 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3122 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3123 usb_delay_ms(&sc->sc_bus, 2);
3125 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3126 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3127 panic("%s: Intr pipe %p still has TDs queued",
3128 device_get_nameunit(sc->sc_bus.bdev), pipe);
3131 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
3135 panic("ohci_device_intr_close: ED not found");
3137 p->next = sed->next;
3138 p->ed.ed_nexted = sed->ed.ed_nexted;
3141 for (j = 0; j < nslots; j++)
3142 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
3144 ohci_free_std(sc, opipe->tail.td);
3145 ohci_free_sed(sc, opipe->sed);
3149 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
3152 u_int npoll, slow, shigh, nslots;
3154 ohci_soft_ed_t *hsed, *sed = opipe->sed;
3156 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
3158 kprintf("ohci_setintr: 0 interval\n");
3159 return (USBD_INVAL);
3162 npoll = OHCI_NO_INTRS;
3163 while (npoll > ival)
3165 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
3168 * We now know which level in the tree the ED must go into.
3169 * Figure out which slot has most bandwidth left over.
3175 * 8 7 8 9 10 11 12 13 14
3176 * N (N-1) .. (N-1+N-1)
3179 shigh = slow + npoll;
3180 nslots = OHCI_NO_INTRS / npoll;
3181 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
3183 for (j = 0; j < nslots; j++)
3184 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
3190 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
3191 best, slow, shigh, bestbw));
3194 hsed = sc->sc_eds[best];
3195 sed->next = hsed->next;
3196 sed->ed.ed_nexted = hsed->ed.ed_nexted;
3198 hsed->ed.ed_nexted = htole32(sed->physaddr);
3201 for (j = 0; j < nslots; j++)
3202 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
3203 opipe->u.intr.nslots = nslots;
3204 opipe->u.intr.pos = best;
3206 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
3207 return (USBD_NORMAL_COMPLETION);
3210 /***********************/
3213 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
3217 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
3219 /* Put it on our queue, */
3220 err = usb_insert_transfer(xfer);
3222 /* bail out on error, */
3223 if (err && err != USBD_IN_PROGRESS)
3226 /* XXX should check inuse here */
3228 /* insert into schedule, */
3229 ohci_device_isoc_enter(xfer);
3231 /* and start if the pipe wasn't running */
3233 ohci_device_isoc_start(STAILQ_FIRST(&xfer->pipe->queue));
3239 ohci_device_isoc_enter(usbd_xfer_handle xfer)
3241 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3242 usbd_device_handle dev = opipe->pipe.device;
3243 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3244 ohci_soft_ed_t *sed = opipe->sed;
3245 struct iso *iso = &opipe->u.iso;
3246 struct ohci_xfer *oxfer = (struct ohci_xfer *)xfer;
3247 ohci_soft_itd_t *sitd, *nsitd;
3248 ohci_physaddr_t buf, offs, noffs, bp0, tdphys;
3249 int i, ncur, nframes;
3251 DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p "
3253 iso->inuse, iso->next, xfer, xfer->nframes));
3258 if (iso->next == -1) {
3259 /* Not in use yet, schedule it a few frames ahead. */
3260 iso->next = le32toh(sc->sc_hcca->hcca_frame_number) + 5;
3261 DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n",
3267 for (sitd = xfer->hcpriv; sitd != NULL && sitd->xfer == xfer;
3268 sitd = sitd->nextitd)
3269 ohci_free_sitd(sc, sitd); /* Free ITDs in prev xfer*/
3273 sitd = ohci_alloc_sitd(sc);
3275 panic("can't alloc isoc");
3276 opipe->tail.itd = sitd;
3277 tdphys = sitd->physaddr;
3278 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop*/
3280 sed->ed.ed_tailp = htole32(tdphys);
3281 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* Start.*/
3285 sitd = opipe->tail.itd;
3286 buf = DMAADDR(&xfer->dmabuf, 0);
3287 bp0 = OHCI_PAGE(buf);
3288 offs = OHCI_PAGE_OFFSET(buf);
3289 nframes = xfer->nframes;
3290 xfer->hcpriv = sitd;
3291 for (i = ncur = 0; i < nframes; i++, ncur++) {
3292 noffs = offs + xfer->frlengths[i];
3293 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
3294 OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */
3296 /* Allocate next ITD */
3297 nsitd = ohci_alloc_sitd(sc);
3298 if (nsitd == NULL) {
3300 kprintf("%s: isoc TD alloc failed\n",
3301 device_get_nameunit(sc->sc_bus.bdev));
3305 /* Fill current ITD */
3306 sitd->itd.itd_flags = htole32(
3308 OHCI_ITD_SET_SF(iso->next) |
3309 OHCI_ITD_SET_DI(6) | /* delay intr a little */
3310 OHCI_ITD_SET_FC(ncur));
3311 sitd->itd.itd_bp0 = htole32(bp0);
3312 sitd->nextitd = nsitd;
3313 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3314 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3316 sitd->flags = OHCI_ITD_ACTIVE;
3319 iso->next = iso->next + ncur;
3320 bp0 = OHCI_PAGE(buf + offs);
3323 sitd->itd.itd_offset[ncur] = htole16(OHCI_ITD_MK_OFFS(offs));
3326 nsitd = ohci_alloc_sitd(sc);
3327 if (nsitd == NULL) {
3329 kprintf("%s: isoc TD alloc failed\n",
3330 device_get_nameunit(sc->sc_bus.bdev));
3333 /* Fixup last used ITD */
3334 sitd->itd.itd_flags = htole32(
3336 OHCI_ITD_SET_SF(iso->next) |
3337 OHCI_ITD_SET_DI(0) |
3338 OHCI_ITD_SET_FC(ncur));
3339 sitd->itd.itd_bp0 = htole32(bp0);
3340 sitd->nextitd = nsitd;
3341 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3342 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3344 sitd->flags = OHCI_CALL_DONE | OHCI_ITD_ACTIVE;
3346 iso->next = iso->next + ncur;
3347 iso->inuse += nframes;
3349 xfer->actlen = offs; /* XXX pretend we did it all */
3351 xfer->status = USBD_IN_PROGRESS;
3353 oxfer->ohci_xfer_flags |= OHCI_ISOC_DIRTY;
3356 if (ohcidebug > 5) {
3357 DPRINTF(("ohci_device_isoc_enter: frame=%d\n",
3358 le32toh(sc->sc_hcca->hcca_frame_number)));
3359 ohci_dump_itds(xfer->hcpriv);
3365 sed->ed.ed_tailp = htole32(nsitd->physaddr);
3366 opipe->tail.itd = nsitd;
3367 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3371 if (ohcidebug > 5) {
3373 DPRINTF(("ohci_device_isoc_enter: after frame=%d\n",
3374 le32toh(sc->sc_hcca->hcca_frame_number)));
3375 ohci_dump_itds(xfer->hcpriv);
3382 ohci_device_isoc_start(usbd_xfer_handle xfer)
3384 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3385 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3386 ohci_soft_ed_t *sed;
3388 DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer));
3391 return (USBD_IOERROR);
3394 if (xfer->status != USBD_IN_PROGRESS)
3395 kprintf("ohci_device_isoc_start: not in progress %p\n", xfer);
3398 /* XXX anything to do? */
3401 sed = opipe->sed; /* Turn off ED skip-bit to start processing */
3402 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* ED's ITD list.*/
3405 return (USBD_IN_PROGRESS);
3409 ohci_device_isoc_abort(usbd_xfer_handle xfer)
3411 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3412 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3413 ohci_soft_ed_t *sed;
3414 ohci_soft_itd_t *sitd, *tmp_sitd;
3415 int undone, num_sitds;
3418 opipe->aborting = 1;
3420 DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer));
3422 /* Transfer is already done. */
3423 if (xfer->status != USBD_NOT_STARTED &&
3424 xfer->status != USBD_IN_PROGRESS) {
3426 kprintf("ohci_device_isoc_abort: early return\n");
3430 /* Give xfer the requested abort code. */
3431 xfer->status = USBD_CANCELLED;
3434 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
3437 sitd = xfer->hcpriv;
3441 kprintf("ohci_device_isoc_abort: hcpriv==0\n");
3445 for (; sitd != NULL && sitd->xfer == xfer; sitd = sitd->nextitd) {
3448 DPRINTFN(1,("abort sets done sitd=%p\n", sitd));
3456 * Each sitd has up to OHCI_ITD_NOFFSET transfers, each can
3457 * take a usb 1ms cycle. Conservatively wait for it to drain.
3458 * Even with DMA done, it can take awhile for the "batch"
3459 * delivery of completion interrupts to occur thru the controller.
3463 usb_delay_ms(&sc->sc_bus, 2*(num_sitds*OHCI_ITD_NOFFSET));
3466 tmp_sitd = xfer->hcpriv;
3467 for (; tmp_sitd != NULL && tmp_sitd->xfer == xfer;
3468 tmp_sitd = tmp_sitd->nextitd) {
3469 if (OHCI_CC_NO_ERROR ==
3470 OHCI_ITD_GET_CC(le32toh(tmp_sitd->itd.itd_flags)) &&
3471 tmp_sitd->flags & OHCI_ITD_ACTIVE &&
3472 (tmp_sitd->flags & OHCI_ITD_INTFIN) == 0)
3475 } while( undone != 0 );
3480 usb_transfer_complete(xfer);
3484 * Only if there is a `next' sitd in next xfer...
3485 * unlink this xfer's sitds.
3487 sed->ed.ed_headp = htole32(sitd->physaddr);
3489 sed->ed.ed_headp = 0;
3491 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
3497 ohci_device_isoc_done(usbd_xfer_handle xfer)
3499 /* This null routine corresponds to non-isoc "done()" routines
3500 * that free the stds associated with an xfer after a completed
3501 * xfer interrupt. However, in the case of isoc transfers, the
3502 * sitds associated with the transfer have already been processed
3503 * and reallocated for the next iteration by
3504 * "ohci_device_isoc_transfer()".
3506 * Routine "usb_transfer_complete()" is called at the end of every
3507 * relevant usb interrupt. "usb_transfer_complete()" indirectly
3508 * calls 1) "ohci_device_isoc_transfer()" (which keeps pumping the
3509 * pipeline by setting up the next transfer iteration) and 2) then
3510 * calls "ohci_device_isoc_done()". Isoc transfers have not been
3511 * working for the ohci usb because this routine was trashing the
3512 * xfer set up for the next iteration (thus, only the first
3513 * UGEN_NISOREQS xfers outstanding on an open would work). Perhaps
3514 * this could all be re-factored, but that's another pass...
3519 ohci_setup_isoc(usbd_pipe_handle pipe)
3521 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3522 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3523 struct iso *iso = &opipe->u.iso;
3529 ohci_add_ed(opipe->sed, sc->sc_isoc_head);
3532 return (USBD_NORMAL_COMPLETION);
3536 ohci_device_isoc_close(usbd_pipe_handle pipe)
3538 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3539 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3540 ohci_soft_ed_t *sed;
3542 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
3545 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop device. */
3547 ohci_close_pipe(pipe, sc->sc_isoc_head); /* Stop isoc list, free ED.*/
3549 /* up to NISOREQs xfers still outstanding. */
3552 opipe->tail.itd->isdone = 1;
3554 ohci_free_sitd(sc, opipe->tail.itd); /* Next `avail free' sitd.*/