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.18 2006/12/20 18:14:37 dillon 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 #if defined(__NetBSD__) || defined(__OpenBSD__)
66 #include <sys/device.h>
67 #include <sys/select.h>
68 #elif defined(__FreeBSD__) || defined(__DragonFly__)
69 #include <sys/endian.h>
70 #include <sys/module.h>
72 #if defined(DIAGNOSTIC) && defined(__i386__)
73 #include <machine/cpu.h>
77 #include <sys/queue.h>
78 #include <sys/sysctl.h>
80 #include <sys/thread2.h>
83 #include <machine/endian.h>
85 #include <bus/usb/usb.h>
86 #include <bus/usb/usbdi.h>
87 #include <bus/usb/usbdivar.h>
88 #include <bus/usb/usb_mem.h>
89 #include <bus/usb/usb_quirks.h>
91 #include <bus/usb/ohcireg.h>
92 #include <bus/usb/ohcivar.h>
94 #if defined(__FreeBSD__) || defined(__DragonFly__)
95 #define delay(d) DELAY(d)
98 #if defined(__OpenBSD__)
99 struct cfdriver ohci_cd = {
100 NULL, "ohci", DV_DULL
105 #define DPRINTF(x) if (ohcidebug) logprintf x
106 #define DPRINTFN(n,x) if (ohcidebug>(n)) logprintf x
108 SYSCTL_NODE(_hw_usb, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci");
109 SYSCTL_INT(_hw_usb_ohci, OID_AUTO, debug, CTLFLAG_RW,
110 &ohcidebug, 0, "ohci debug level");
112 #define bitmask_snprintf(q,f,b,l) ksnprintf((b), (l), "%b", (q), (f))
116 #define DPRINTFN(n,x)
120 * The OHCI controller is little endian, so on big endian machines
121 * the data strored in memory needs to be swapped.
123 #if defined(__OpenBSD__)
124 #if BYTE_ORDER == BIG_ENDIAN
125 #define htole32(x) (bswap32(x))
126 #define le32toh(x) (bswap32(x))
128 #define htole32(x) (x)
129 #define le32toh(x) (x)
135 Static ohci_soft_ed_t *ohci_alloc_sed(ohci_softc_t *);
136 Static void ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
138 Static ohci_soft_td_t *ohci_alloc_std(ohci_softc_t *);
139 Static void ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
141 Static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
142 Static void ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
145 Static void ohci_free_std_chain(ohci_softc_t *, ohci_soft_td_t *,
148 Static usbd_status ohci_alloc_std_chain(struct ohci_pipe *,
149 ohci_softc_t *, int, int, usbd_xfer_handle,
150 ohci_soft_td_t *, ohci_soft_td_t **);
152 #if defined(__NetBSD__) || defined(__OpenBSD__)
153 Static void ohci_shutdown(void *v);
154 Static void ohci_power(int, void *);
156 Static usbd_status ohci_open(usbd_pipe_handle);
157 Static void ohci_poll(struct usbd_bus *);
158 Static void ohci_softintr(void *);
159 Static void ohci_waitintr(ohci_softc_t *, usbd_xfer_handle);
160 Static void ohci_add_done(ohci_softc_t *, ohci_physaddr_t);
161 Static void ohci_rhsc(ohci_softc_t *, usbd_xfer_handle);
163 Static usbd_status ohci_device_request(usbd_xfer_handle xfer);
164 Static void ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
165 Static void ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
166 Static void ohci_hash_add_td(ohci_softc_t *, ohci_soft_td_t *);
167 Static void ohci_hash_rem_td(ohci_softc_t *, ohci_soft_td_t *);
168 Static ohci_soft_td_t *ohci_hash_find_td(ohci_softc_t *, ohci_physaddr_t);
169 Static void ohci_hash_add_itd(ohci_softc_t *, ohci_soft_itd_t *);
170 Static void ohci_hash_rem_itd(ohci_softc_t *, ohci_soft_itd_t *);
171 Static ohci_soft_itd_t *ohci_hash_find_itd(ohci_softc_t *, ohci_physaddr_t);
173 Static usbd_status ohci_setup_isoc(usbd_pipe_handle pipe);
174 Static void ohci_device_isoc_enter(usbd_xfer_handle);
176 Static usbd_status ohci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
177 Static void ohci_freem(struct usbd_bus *, usb_dma_t *);
179 Static usbd_xfer_handle ohci_allocx(struct usbd_bus *);
180 Static void ohci_freex(struct usbd_bus *, usbd_xfer_handle);
182 Static usbd_status ohci_root_ctrl_transfer(usbd_xfer_handle);
183 Static usbd_status ohci_root_ctrl_start(usbd_xfer_handle);
184 Static void ohci_root_ctrl_abort(usbd_xfer_handle);
185 Static void ohci_root_ctrl_close(usbd_pipe_handle);
186 Static void ohci_root_ctrl_done(usbd_xfer_handle);
188 Static usbd_status ohci_root_intr_transfer(usbd_xfer_handle);
189 Static usbd_status ohci_root_intr_start(usbd_xfer_handle);
190 Static void ohci_root_intr_abort(usbd_xfer_handle);
191 Static void ohci_root_intr_close(usbd_pipe_handle);
192 Static void ohci_root_intr_done(usbd_xfer_handle);
194 Static usbd_status ohci_device_ctrl_transfer(usbd_xfer_handle);
195 Static usbd_status ohci_device_ctrl_start(usbd_xfer_handle);
196 Static void ohci_device_ctrl_abort(usbd_xfer_handle);
197 Static void ohci_device_ctrl_close(usbd_pipe_handle);
198 Static void ohci_device_ctrl_done(usbd_xfer_handle);
200 Static usbd_status ohci_device_bulk_transfer(usbd_xfer_handle);
201 Static usbd_status ohci_device_bulk_start(usbd_xfer_handle);
202 Static void ohci_device_bulk_abort(usbd_xfer_handle);
203 Static void ohci_device_bulk_close(usbd_pipe_handle);
204 Static void ohci_device_bulk_done(usbd_xfer_handle);
206 Static usbd_status ohci_device_intr_transfer(usbd_xfer_handle);
207 Static usbd_status ohci_device_intr_start(usbd_xfer_handle);
208 Static void ohci_device_intr_abort(usbd_xfer_handle);
209 Static void ohci_device_intr_close(usbd_pipe_handle);
210 Static void ohci_device_intr_done(usbd_xfer_handle);
212 Static usbd_status ohci_device_isoc_transfer(usbd_xfer_handle);
213 Static usbd_status ohci_device_isoc_start(usbd_xfer_handle);
214 Static void ohci_device_isoc_abort(usbd_xfer_handle);
215 Static void ohci_device_isoc_close(usbd_pipe_handle);
216 Static void ohci_device_isoc_done(usbd_xfer_handle);
218 Static usbd_status ohci_device_setintr(ohci_softc_t *sc,
219 struct ohci_pipe *pipe, int ival);
221 Static int ohci_str(usb_string_descriptor_t *, int, const char *);
223 Static void ohci_timeout(void *);
224 Static void ohci_timeout_task(void *);
225 Static void ohci_rhsc_able(ohci_softc_t *, int);
226 Static void ohci_rhsc_enable(void *);
228 Static void ohci_close_pipe(usbd_pipe_handle, ohci_soft_ed_t *);
229 Static void ohci_abort_xfer(usbd_xfer_handle, usbd_status);
231 Static void ohci_device_clear_toggle(usbd_pipe_handle pipe);
232 Static void ohci_noop(usbd_pipe_handle pipe);
234 Static usbd_status ohci_controller_init(ohci_softc_t *sc);
237 Static void ohci_dumpregs(ohci_softc_t *);
238 Static void ohci_dump_tds(ohci_soft_td_t *);
239 Static void ohci_dump_td(ohci_soft_td_t *);
240 Static void ohci_dump_ed(ohci_soft_ed_t *);
241 Static void ohci_dump_itd(ohci_soft_itd_t *);
242 Static void ohci_dump_itds(ohci_soft_itd_t *);
245 #define OBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
246 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
247 #define OWRITE1(sc, r, x) \
248 do { OBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
249 #define OWRITE2(sc, r, x) \
250 do { OBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
251 #define OWRITE4(sc, r, x) \
252 do { OBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
253 #define OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
254 #define OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
255 #define OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
257 /* Reverse the bits in a value 0 .. 31 */
258 Static u_int8_t revbits[OHCI_NO_INTRS] =
259 { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
260 0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
261 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
262 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
265 struct usbd_pipe pipe;
270 ohci_soft_itd_t *itd;
272 /* Info needed for different pipe kinds. */
278 ohci_soft_td_t *setup, *data, *stat;
297 #define OHCI_INTR_ENDPT 1
299 Static struct usbd_bus_methods ohci_bus_methods = {
309 Static struct usbd_pipe_methods ohci_root_ctrl_methods = {
310 ohci_root_ctrl_transfer,
311 ohci_root_ctrl_start,
312 ohci_root_ctrl_abort,
313 ohci_root_ctrl_close,
318 Static struct usbd_pipe_methods ohci_root_intr_methods = {
319 ohci_root_intr_transfer,
320 ohci_root_intr_start,
321 ohci_root_intr_abort,
322 ohci_root_intr_close,
327 Static struct usbd_pipe_methods ohci_device_ctrl_methods = {
328 ohci_device_ctrl_transfer,
329 ohci_device_ctrl_start,
330 ohci_device_ctrl_abort,
331 ohci_device_ctrl_close,
333 ohci_device_ctrl_done,
336 Static struct usbd_pipe_methods ohci_device_intr_methods = {
337 ohci_device_intr_transfer,
338 ohci_device_intr_start,
339 ohci_device_intr_abort,
340 ohci_device_intr_close,
341 ohci_device_clear_toggle,
342 ohci_device_intr_done,
345 Static struct usbd_pipe_methods ohci_device_bulk_methods = {
346 ohci_device_bulk_transfer,
347 ohci_device_bulk_start,
348 ohci_device_bulk_abort,
349 ohci_device_bulk_close,
350 ohci_device_clear_toggle,
351 ohci_device_bulk_done,
354 Static struct usbd_pipe_methods ohci_device_isoc_methods = {
355 ohci_device_isoc_transfer,
356 ohci_device_isoc_start,
357 ohci_device_isoc_abort,
358 ohci_device_isoc_close,
360 ohci_device_isoc_done,
363 #if defined(__NetBSD__) || defined(__OpenBSD__)
365 ohci_activate(device_ptr_t self, enum devact act)
367 struct ohci_softc *sc = (struct ohci_softc *)self;
374 case DVACT_DEACTIVATE:
375 if (sc->sc_child != NULL)
376 rv = config_deactivate(sc->sc_child);
385 ohci_detach(struct ohci_softc *sc, int flags)
389 #if defined(__NetBSD__) || defined(__OpenBSD__)
390 if (sc->sc_child != NULL)
391 rv = config_detach(sc->sc_child, flags);
399 usb_uncallout(sc->sc_tmo_rhsc, ohci_rhsc_enable, sc);
401 #if defined(__NetBSD__) || defined(__OpenBSD__)
402 powerhook_disestablish(sc->sc_powerhook);
403 shutdownhook_disestablish(sc->sc_shutdownhook);
406 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
407 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
409 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
411 for (i = 0; i < OHCI_NO_EDS; i++)
412 ohci_free_sed(sc, sc->sc_eds[i]);
413 ohci_free_sed(sc, sc->sc_isoc_head);
414 ohci_free_sed(sc, sc->sc_bulk_head);
415 ohci_free_sed(sc, sc->sc_ctrl_head);
416 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
422 ohci_alloc_sed(ohci_softc_t *sc)
429 if (sc->sc_freeeds == NULL) {
430 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
431 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
432 OHCI_ED_ALIGN, &dma);
435 for(i = 0; i < OHCI_SED_CHUNK; i++) {
436 offs = i * OHCI_SED_SIZE;
437 sed = KERNADDR(&dma, offs);
438 sed->physaddr = DMAADDR(&dma, offs);
439 sed->next = sc->sc_freeeds;
440 sc->sc_freeeds = sed;
443 sed = sc->sc_freeeds;
444 sc->sc_freeeds = sed->next;
445 memset(&sed->ed, 0, sizeof(ohci_ed_t));
451 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
453 sed->next = sc->sc_freeeds;
454 sc->sc_freeeds = sed;
458 ohci_alloc_std(ohci_softc_t *sc)
465 if (sc->sc_freetds == NULL) {
466 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
467 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
468 OHCI_TD_ALIGN, &dma);
472 for(i = 0; i < OHCI_STD_CHUNK; i++) {
473 offs = i * OHCI_STD_SIZE;
474 std = KERNADDR(&dma, offs);
475 std->physaddr = DMAADDR(&dma, offs);
476 std->nexttd = sc->sc_freetds;
477 sc->sc_freetds = std;
483 std = sc->sc_freetds;
484 sc->sc_freetds = std->nexttd;
485 memset(&std->td, 0, sizeof(ohci_td_t));
488 ohci_hash_add_td(sc, std);
495 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
498 ohci_hash_rem_td(sc, std);
499 std->nexttd = sc->sc_freetds;
500 sc->sc_freetds = std;
505 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
506 int alen, int rd, usbd_xfer_handle xfer,
507 ohci_soft_td_t *sp, ohci_soft_td_t **ep)
509 ohci_soft_td_t *next, *cur;
510 ohci_physaddr_t dataphys;
514 usb_dma_t *dma = &xfer->dmabuf;
515 u_int16_t flags = xfer->flags;
517 DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%d\n", alen));
523 (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
524 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
525 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_SET_DI(6));
528 next = ohci_alloc_std(sc);
532 dataphys = DMAADDR(dma, offset);
535 * The OHCI hardware can handle at most one 4k crossing.
536 * XXX - currently we only allocate contigous buffers, but
537 * the OHCI spec says: If during the data transfer the buffer
538 * address contained in the HC's working copy of
539 * CurrentBufferPointer crosses a 4K boundary, the upper 20
540 * bits of Buffer End are copied to the working value of
541 * CurrentBufferPointer causing the next buffer address to
542 * be the 0th byte in the same 4K page that contains the
543 * last byte of the buffer (the 4K boundary crossing may
544 * occur within a data packet transfer.)
546 * If/when dma has multiple segments, this will need to
547 * properly handle fragmenting TD's.
549 * Note that if we are gathering data from multiple SMALL
550 * segments, e.g. mbufs, we need to do special gymnastics,
551 * e.g. bounce buffering or data aggregation,
552 * BEFORE WE GET HERE because a bulk USB transfer must
553 * consist of maximally sized packets right up to the end.
554 * A shorter than maximal packet means that it is the end
555 * of the transfer. If the data transfer length is a
556 * multiple of the packet size, then a 0 byte
557 * packet will be the signal of the end of transfer.
558 * Since packets can't cross TDs this means that
559 * each TD except the last one must cover an exact multiple
560 * of the maximal packet length.
562 if (OHCI_PAGE_OFFSET(dataphys) + len <= (2 * OHCI_PAGE_SIZE)) {
563 /* We can handle all that remains in this TD */
566 /* must use multiple TDs, fill as much as possible. */
567 curlen = 2 * OHCI_PAGE_SIZE -
568 OHCI_PAGE_OFFSET(dataphys);
569 /* the length must be a multiple of the max size */
571 UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize);
572 KASSERT((curlen != 0), ("ohci_alloc_std: curlen == 0"));
574 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
575 "len=%d curlen=%d\n",
576 dataphys, len, curlen));
579 cur->td.td_flags = tdflags;
580 cur->td.td_cbp = htole32(dataphys);
582 cur->td.td_nexttd = htole32(next->physaddr);
583 cur->td.td_be = htole32(DMAADDR(dma, offset + curlen - 1));
585 cur->flags = OHCI_ADD_LEN;
587 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
588 dataphys, dataphys + curlen - 1));
592 panic("Length went negative: %d curlen %d dma %p offset %08x", len, curlen, dma, (int)0);
594 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
598 if ((flags & USBD_FORCE_SHORT_XFER) &&
599 alen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
600 /* Force a 0 length transfer at the end. */
604 next = ohci_alloc_std(sc);
608 cur->td.td_flags = tdflags;
609 cur->td.td_cbp = 0; /* indicate 0 length packet */
611 cur->td.td_nexttd = htole32(next->physaddr);
616 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
620 return (USBD_NORMAL_COMPLETION);
629 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
630 ohci_soft_td_t *stdend)
634 for (; std != stdend; std = p) {
636 ohci_free_std(sc, std);
642 ohci_alloc_sitd(ohci_softc_t *sc)
644 ohci_soft_itd_t *sitd;
650 if (sc->sc_freeitds == NULL) {
651 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
653 err = usb_allocmem(&sc->sc_bus,
654 OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
655 OHCI_ITD_ALIGN, &dma);
659 for (i = 0; i < OHCI_SITD_CHUNK; i++) {
660 offs = i * OHCI_SITD_SIZE;
661 sitd = KERNADDR(&dma, offs);
662 sitd->physaddr = DMAADDR(&dma, offs);
663 sitd->nextitd = sc->sc_freeitds;
664 sc->sc_freeitds = sitd;
667 sitd = sc->sc_freeitds;
668 sc->sc_freeitds = sitd->nextitd;
669 memset(&sitd->itd, 0, sizeof(ohci_itd_t));
670 sitd->nextitd = NULL;
672 ohci_hash_add_itd(sc, sitd);
683 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
685 DPRINTFN(10,("ohci_free_sitd: sitd=%p\n", sitd));
689 panic("ohci_free_sitd: sitd=%p not done", sitd);
692 /* Warn double free */
697 ohci_hash_rem_itd(sc, sitd);
698 sitd->nextitd = sc->sc_freeitds;
699 sc->sc_freeitds = sitd;
704 ohci_init(ohci_softc_t *sc)
706 ohci_soft_ed_t *sed, *psed;
711 DPRINTF(("ohci_init: start\n"));
712 #if defined(__OpenBSD__)
715 printf("%s:", USBDEVNAME(sc->sc_bus.bdev));
717 rev = OREAD4(sc, OHCI_REVISION);
718 printf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
719 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
721 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
722 printf("%s: unsupported OHCI revision\n",
723 USBDEVNAME(sc->sc_bus.bdev));
724 sc->sc_bus.usbrev = USBREV_UNKNOWN;
727 sc->sc_bus.usbrev = USBREV_1_0;
729 for (i = 0; i < OHCI_HASH_SIZE; i++)
730 LIST_INIT(&sc->sc_hash_tds[i]);
731 for (i = 0; i < OHCI_HASH_SIZE; i++)
732 LIST_INIT(&sc->sc_hash_itds[i]);
734 SIMPLEQ_INIT(&sc->sc_free_xfers);
736 /* XXX determine alignment by R/W */
737 /* Allocate the HCCA area. */
738 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
739 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
742 sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0);
743 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
745 sc->sc_eintrs = OHCI_NORMAL_INTRS;
747 /* Allocate dummy ED that starts the control list. */
748 sc->sc_ctrl_head = ohci_alloc_sed(sc);
749 if (sc->sc_ctrl_head == NULL) {
753 sc->sc_ctrl_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
755 /* Allocate dummy ED that starts the bulk list. */
756 sc->sc_bulk_head = ohci_alloc_sed(sc);
757 if (sc->sc_bulk_head == NULL) {
761 sc->sc_bulk_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
763 /* Allocate dummy ED that starts the isochronous list. */
764 sc->sc_isoc_head = ohci_alloc_sed(sc);
765 if (sc->sc_isoc_head == NULL) {
769 sc->sc_isoc_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
771 /* Allocate all the dummy EDs that make up the interrupt tree. */
772 for (i = 0; i < OHCI_NO_EDS; i++) {
773 sed = ohci_alloc_sed(sc);
776 ohci_free_sed(sc, sc->sc_eds[i]);
780 /* All ED fields are set to 0. */
782 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
784 psed = sc->sc_eds[(i-1) / 2];
786 psed= sc->sc_isoc_head;
788 sed->ed.ed_nexted = htole32(psed->physaddr);
791 * Fill HCCA interrupt table. The bit reversal is to get
792 * the tree set up properly to spread the interrupts.
794 for (i = 0; i < OHCI_NO_INTRS; i++)
795 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
796 htole32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
799 if (ohcidebug > 15) {
800 for (i = 0; i < OHCI_NO_EDS; i++) {
802 ohci_dump_ed(sc->sc_eds[i]);
805 ohci_dump_ed(sc->sc_isoc_head);
809 err = ohci_controller_init(sc);
810 if (err != USBD_NORMAL_COMPLETION)
813 /* Set up the bus struct. */
814 sc->sc_bus.methods = &ohci_bus_methods;
815 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
817 #if defined(__NetBSD__) || defined(__OpenBSD__)
818 sc->sc_control = sc->sc_intre = 0;
819 sc->sc_powerhook = powerhook_establish(ohci_power, sc);
820 sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc);
823 usb_callout_init(sc->sc_tmo_rhsc);
825 return (USBD_NORMAL_COMPLETION);
828 for (i = 0; i < OHCI_NO_EDS; i++)
829 ohci_free_sed(sc, sc->sc_eds[i]);
831 ohci_free_sed(sc, sc->sc_isoc_head);
833 ohci_free_sed(sc, sc->sc_bulk_head);
835 ohci_free_sed(sc, sc->sc_ctrl_head);
837 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
842 ohci_controller_init(ohci_softc_t *sc)
845 u_int32_t s, ctl, ival, hcr, fm, per, desca;
847 /* Determine in what context we are running. */
848 ctl = OREAD4(sc, OHCI_CONTROL);
850 /* SMM active, request change */
851 DPRINTF(("ohci_init: SMM active, request owner change\n"));
852 s = OREAD4(sc, OHCI_COMMAND_STATUS);
853 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
854 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
855 usb_delay_ms(&sc->sc_bus, 1);
856 ctl = OREAD4(sc, OHCI_CONTROL);
858 if ((ctl & OHCI_IR) == 0) {
859 printf("%s: SMM does not respond, resetting\n",
860 USBDEVNAME(sc->sc_bus.bdev));
861 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
865 /* Don't bother trying to reuse the BIOS init, we'll reset it anyway. */
866 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
867 /* BIOS started controller. */
868 DPRINTF(("ohci_init: BIOS active\n"));
869 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
870 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
871 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
875 DPRINTF(("ohci_init: cold started\n"));
877 /* Controller was cold started. */
878 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
882 * This reset should not be necessary according to the OHCI spec, but
883 * without it some controllers do not start.
885 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
886 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
887 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
889 /* We now own the host controller and the bus has been reset. */
890 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
892 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
893 /* Nominal time for a reset is 10 us. */
894 for (i = 0; i < 10; i++) {
896 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
901 printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
902 return (USBD_IOERROR);
909 /* The controller is now in SUSPEND state, we have 2ms to finish. */
911 /* Set up HC registers. */
912 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
913 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
914 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
915 /* disable all interrupts and then switch on all desired interrupts */
916 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
917 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
918 /* switch on desired functional features */
919 ctl = OREAD4(sc, OHCI_CONTROL);
920 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
921 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
922 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
923 /* And finally start it! */
924 OWRITE4(sc, OHCI_CONTROL, ctl);
927 * The controller is now OPERATIONAL. Set a some final
928 * registers that should be set earlier, but that the
929 * controller ignores when in the SUSPEND state.
931 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
932 fm |= OHCI_FSMPS(ival) | ival;
933 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
934 per = OHCI_PERIODIC(ival); /* 90% periodic */
935 OWRITE4(sc, OHCI_PERIODIC_START, per);
937 /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
938 desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
939 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
940 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
941 usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
942 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
945 * The AMD756 requires a delay before re-reading the register,
946 * otherwise it will occasionally report 0 ports.
949 for (i = 0; i < 10 && sc->sc_noport == 0; i++) {
950 usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
951 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
958 return (USBD_NORMAL_COMPLETION);
962 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
964 return (usb_allocmem(bus, size, 0, dma));
968 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
970 usb_freemem(bus, dma);
974 ohci_allocx(struct usbd_bus *bus)
976 struct ohci_softc *sc = (struct ohci_softc *)bus;
977 usbd_xfer_handle xfer;
979 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
981 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
983 if (xfer->busy_free != XFER_FREE) {
984 printf("ohci_allocx: xfer=%p not free, 0x%08x\n", xfer,
989 xfer = kmalloc(sizeof(struct ohci_xfer), M_USB, M_INTWAIT);
992 memset(xfer, 0, sizeof (struct ohci_xfer));
993 usb_init_task(&OXFER(xfer)->abort_task, ohci_timeout_task,
995 OXFER(xfer)->ohci_xfer_flags = 0;
997 xfer->busy_free = XFER_BUSY;
1004 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1006 struct ohci_softc *sc = (struct ohci_softc *)bus;
1007 struct ohci_xfer *oxfer = (struct ohci_xfer *)xfer;
1008 ohci_soft_itd_t *sitd;
1010 if (oxfer->ohci_xfer_flags & OHCI_ISOC_DIRTY) {
1012 for (sitd = xfer->hcpriv; sitd != NULL && sitd->xfer == xfer;
1013 sitd = sitd->nextitd)
1014 ohci_free_sitd(sc, sitd);
1019 if (xfer->busy_free != XFER_BUSY) {
1020 printf("ohci_freex: xfer=%p not busy, 0x%08x\n", xfer,
1024 xfer->busy_free = XFER_FREE;
1026 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
1030 * Shut down the controller when the system is going down.
1033 ohci_shutdown(void *v)
1035 ohci_softc_t *sc = v;
1037 DPRINTF(("ohci_shutdown: stopping the HC\n"));
1038 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1042 * Handle suspend/resume.
1044 * We need to switch to polling mode here, because this routine is
1045 * called from an intterupt context. This is all right since we
1046 * are almost suspended anyway.
1049 ohci_power(int why, void *v)
1051 ohci_softc_t *sc = v;
1055 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
1060 if (why != PWR_RESUME) {
1061 sc->sc_bus.use_polling++;
1062 ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
1063 if (sc->sc_control == 0) {
1065 * Preserve register values, in case that APM BIOS
1066 * does not recover them.
1068 sc->sc_control = ctl;
1069 sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE);
1071 ctl |= OHCI_HCFS_SUSPEND;
1072 OWRITE4(sc, OHCI_CONTROL, ctl);
1073 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1074 sc->sc_bus.use_polling--;
1076 sc->sc_bus.use_polling++;
1078 /* Some broken BIOSes never initialize Controller chip */
1079 ohci_controller_init(sc);
1082 OWRITE4(sc, OHCI_INTERRUPT_ENABLE,
1083 sc->sc_intre & (OHCI_ALL_INTRS | OHCI_MIE));
1085 ctl = sc->sc_control;
1087 ctl = OREAD4(sc, OHCI_CONTROL);
1088 ctl |= OHCI_HCFS_RESUME;
1089 OWRITE4(sc, OHCI_CONTROL, ctl);
1090 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
1091 ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL;
1092 OWRITE4(sc, OHCI_CONTROL, ctl);
1093 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
1094 sc->sc_control = sc->sc_intre = 0;
1095 sc->sc_bus.use_polling--;
1102 ohci_dumpregs(ohci_softc_t *sc)
1104 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
1105 OREAD4(sc, OHCI_REVISION),
1106 OREAD4(sc, OHCI_CONTROL),
1107 OREAD4(sc, OHCI_COMMAND_STATUS)));
1108 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
1109 OREAD4(sc, OHCI_INTERRUPT_STATUS),
1110 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
1111 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
1112 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
1113 OREAD4(sc, OHCI_HCCA),
1114 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
1115 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
1116 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
1117 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
1118 OREAD4(sc, OHCI_BULK_HEAD_ED),
1119 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
1120 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
1121 OREAD4(sc, OHCI_DONE_HEAD),
1122 OREAD4(sc, OHCI_FM_INTERVAL),
1123 OREAD4(sc, OHCI_FM_REMAINING)));
1124 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
1125 OREAD4(sc, OHCI_FM_NUMBER),
1126 OREAD4(sc, OHCI_PERIODIC_START),
1127 OREAD4(sc, OHCI_LS_THRESHOLD)));
1128 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n",
1129 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
1130 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
1131 OREAD4(sc, OHCI_RH_STATUS)));
1132 DPRINTF((" port1=0x%08x port2=0x%08x\n",
1133 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
1134 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
1135 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n",
1136 le32toh(sc->sc_hcca->hcca_frame_number),
1137 le32toh(sc->sc_hcca->hcca_done_head)));
1141 Static int ohci_intr1(ohci_softc_t *);
1146 ohci_softc_t *sc = p;
1148 if (sc->sc_dying || (sc->sc_flags & OHCI_SCFLG_DONEINIT) == 0)
1151 /* If we get an interrupt while polling, then just ignore it. */
1152 if (sc->sc_bus.use_polling) {
1154 DPRINTFN(16, ("ohci_intr: ignored interrupt while polling\n"));
1159 return (ohci_intr1(sc));
1163 ohci_intr1(ohci_softc_t *sc)
1165 u_int32_t intrs, eintrs;
1166 ohci_physaddr_t done;
1168 DPRINTFN(14,("ohci_intr1: enter\n"));
1170 /* In case the interrupt occurs before initialization has completed. */
1171 if (sc->sc_hcca == NULL) {
1173 printf("ohci_intr: sc->sc_hcca == NULL\n");
1179 done = le32toh(sc->sc_hcca->hcca_done_head);
1181 /* The LSb of done is used to inform the HC Driver that an interrupt
1182 * condition exists for both the Done list and for another event
1183 * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
1184 * Driver checks the HccaDoneHead Value. If this value is 0, then the
1185 * interrupt was caused by other than the HccaDoneHead update and the
1186 * HcInterruptStatus register needs to be accessed to determine that
1187 * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
1188 * update interrupt is indicated and if the LSb of done is nonzero,
1189 * then an additional interrupt event is indicated and
1190 * HcInterruptStatus should be checked to determine its cause.
1193 if (done & ~OHCI_DONE_INTRS)
1195 if (done & OHCI_DONE_INTRS) {
1196 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1197 done &= ~OHCI_DONE_INTRS;
1199 sc->sc_hcca->hcca_done_head = 0;
1201 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & ~OHCI_WDH;
1203 if (intrs == 0) /* nothing to be done (PCI shared interrupt) */
1207 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1208 eintrs = intrs & sc->sc_eintrs;
1212 sc->sc_bus.intr_context++;
1213 sc->sc_bus.no_intrs++;
1214 DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
1215 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1218 if (eintrs & OHCI_SO) {
1219 sc->sc_overrun_cnt++;
1220 if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
1221 printf("%s: %u scheduling overruns\n",
1222 USBDEVNAME(sc->sc_bus.bdev), sc->sc_overrun_cnt);
1223 sc->sc_overrun_cnt = 0;
1228 if (eintrs & OHCI_WDH) {
1229 ohci_add_done(sc, done &~ OHCI_DONE_INTRS);
1230 usb_schedsoftintr(&sc->sc_bus);
1231 eintrs &= ~OHCI_WDH;
1233 if (eintrs & OHCI_RD) {
1234 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1235 /* XXX process resume detect */
1237 if (eintrs & OHCI_UE) {
1238 printf("%s: unrecoverable error, controller halted\n",
1239 USBDEVNAME(sc->sc_bus.bdev));
1240 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1243 if (eintrs & OHCI_RHSC) {
1244 ohci_rhsc(sc, sc->sc_intrxfer);
1246 * Disable RHSC interrupt for now, because it will be
1247 * on until the port has been reset.
1249 ohci_rhsc_able(sc, 0);
1250 /* Do not allow RHSC interrupts > 1 per second */
1251 usb_callout(sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);
1252 eintrs &= ~OHCI_RHSC;
1255 sc->sc_bus.intr_context--;
1258 /* Block unprocessed interrupts. XXX */
1259 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
1260 sc->sc_eintrs &= ~eintrs;
1261 printf("%s: blocking intrs 0x%x\n",
1262 USBDEVNAME(sc->sc_bus.bdev), eintrs);
1269 ohci_rhsc_able(ohci_softc_t *sc, int on)
1271 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1273 sc->sc_eintrs |= OHCI_RHSC;
1274 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1276 sc->sc_eintrs &= ~OHCI_RHSC;
1277 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1282 ohci_rhsc_enable(void *v_sc)
1284 ohci_softc_t *sc = v_sc;
1287 ohci_rhsc_able(sc, 1);
1292 char *ohci_cc_strs[] = {
1296 "DATA_TOGGLE_MISMATCH",
1298 "DEVICE_NOT_RESPONDING",
1299 "PID_CHECK_FAILURE",
1313 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done)
1315 ohci_soft_itd_t *sitd, *sidone, **ip;
1316 ohci_soft_td_t *std, *sdone, **p;
1318 /* Reverse the done list. */
1319 for (sdone = NULL, sidone = NULL; done != 0; ) {
1320 std = ohci_hash_find_td(sc, done);
1323 done = le32toh(std->td.td_nexttd);
1325 DPRINTFN(10,("add TD %p\n", std));
1328 sitd = ohci_hash_find_itd(sc, done);
1330 sitd->dnext = sidone;
1331 done = le32toh(sitd->itd.itd_nextitd);
1333 DPRINTFN(5,("add ITD %p\n", sitd));
1336 panic("ohci_add_done: addr 0x%08lx not found", (u_long)done);
1339 /* sdone & sidone now hold the done lists. */
1340 /* Put them on the already processed lists. */
1341 for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext)
1344 for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext)
1350 ohci_softintr(void *v)
1352 ohci_softc_t *sc = v;
1353 ohci_soft_itd_t *sitd, *sidone, *sitdnext;
1354 ohci_soft_td_t *std, *sdone, *stdnext, *p, *n;
1355 usbd_xfer_handle xfer;
1356 struct ohci_pipe *opipe;
1360 DPRINTFN(10,("ohci_softintr: enter\n"));
1362 sc->sc_bus.intr_context++;
1365 sdone = sc->sc_sdone;
1366 sc->sc_sdone = NULL;
1367 sidone = sc->sc_sidone;
1368 sc->sc_sidone = NULL;
1371 DPRINTFN(10,("ohci_softintr: sdone=%p sidone=%p\n", sdone, sidone));
1374 if (ohcidebug > 10) {
1375 DPRINTF(("ohci_process_done: TD done:\n"));
1376 ohci_dump_tds(sdone);
1380 for (std = sdone; std; std = stdnext) {
1382 stdnext = std->dnext;
1383 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1384 std, xfer, (xfer ? xfer->hcpriv : NULL)));
1387 * xfer == NULL: There seems to be no xfer associated
1388 * with this TD. It is tailp that happened to end up on
1393 if (xfer->status == USBD_CANCELLED ||
1394 xfer->status == USBD_TIMEOUT) {
1395 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1397 /* Handled by abort routine. */
1402 if (std->td.td_cbp != 0)
1403 len -= le32toh(std->td.td_be) -
1404 le32toh(std->td.td_cbp) + 1;
1405 DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n", len,
1407 if (std->flags & OHCI_ADD_LEN)
1408 xfer->actlen += len;
1410 cc = OHCI_TD_GET_CC(le32toh(std->td.td_flags));
1411 if (cc != OHCI_CC_NO_ERROR) {
1413 * Endpoint is halted. First unlink all the TDs
1414 * belonging to the failed transfer, and then restart
1417 opipe = (struct ohci_pipe *)xfer->pipe;
1419 DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1420 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1421 ohci_cc_strs[OHCI_TD_GET_CC(le32toh(std->td.td_flags))]));
1422 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
1423 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1424 &OXFER(xfer)->abort_task);
1426 /* Remove all this xfer's TDs from the done queue. */
1427 for (p = std; p->dnext != NULL; p = p->dnext) {
1428 if (p->dnext->xfer != xfer)
1430 p->dnext = p->dnext->dnext;
1432 /* The next TD may have been removed. */
1433 stdnext = std->dnext;
1435 /* Remove all TDs belonging to this xfer. */
1436 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1438 ohci_free_std(sc, p);
1442 opipe->sed->ed.ed_headp = htole32(p->physaddr);
1443 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1445 if (cc == OHCI_CC_STALL)
1446 xfer->status = USBD_STALLED;
1448 xfer->status = USBD_IOERROR;
1450 usb_transfer_complete(xfer);
1455 * Skip intermediate TDs. They remain linked from
1456 * xfer->hcpriv and we free them when the transfer completes.
1458 if ((std->flags & OHCI_CALL_DONE) == 0)
1461 /* Normal transfer completion */
1462 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
1463 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1464 &OXFER(xfer)->abort_task);
1465 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1467 ohci_free_std(sc, p);
1469 xfer->status = USBD_NORMAL_COMPLETION;
1471 usb_transfer_complete(xfer);
1476 if (ohcidebug > 10) {
1477 DPRINTF(("ohci_softintr: ITD done:\n"));
1478 ohci_dump_itds(sidone);
1482 for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
1484 sitdnext = sitd->dnext;
1485 sitd->flags |= OHCI_ITD_INTFIN;
1486 DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n",
1487 sitd, xfer, xfer ? xfer->hcpriv : 0));
1490 if (xfer->status == USBD_CANCELLED ||
1491 xfer->status == USBD_TIMEOUT) {
1492 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1494 /* Handled by abort routine. */
1498 if (xfer->pipe->aborting)
1499 continue; /*Ignore.*/
1502 printf("ohci_softintr: sitd=%p is done\n", sitd);
1505 opipe = (struct ohci_pipe *)xfer->pipe;
1506 if (opipe->aborting)
1509 if (sitd->flags & OHCI_CALL_DONE) {
1510 ohci_soft_itd_t *next;
1512 opipe->u.iso.inuse -= xfer->nframes;
1513 xfer->status = USBD_NORMAL_COMPLETION;
1514 for (i = 0, sitd = xfer->hcpriv;;sitd = next) {
1515 next = sitd->nextitd;
1516 if (OHCI_ITD_GET_CC(sitd->itd.itd_flags) != OHCI_CC_NO_ERROR)
1517 xfer->status = USBD_IOERROR;
1519 if (xfer->status == USBD_NORMAL_COMPLETION) {
1520 iframes = OHCI_ITD_GET_FC(sitd->itd.itd_flags);
1521 for (j = 0; j < iframes; i++, j++) {
1522 len = le16toh(sitd->itd.itd_offset[j]);
1524 (OHCI_ITD_PSW_GET_CC(len) ==
1525 OHCI_CC_NOT_ACCESSED) ? 0 :
1526 OHCI_ITD_PSW_LENGTH(len);
1527 xfer->frlengths[i] = len;
1530 if (sitd->flags & OHCI_CALL_DONE)
1535 usb_transfer_complete(xfer);
1540 #ifdef USB_USE_SOFTINTR
1541 if (sc->sc_softwake) {
1542 sc->sc_softwake = 0;
1543 wakeup(&sc->sc_softwake);
1545 #endif /* USB_USE_SOFTINTR */
1547 sc->sc_bus.intr_context--;
1548 DPRINTFN(10,("ohci_softintr: done:\n"));
1552 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1554 DPRINTFN(10,("ohci_device_ctrl_done: xfer=%p\n", xfer));
1557 if (!(xfer->rqflags & URQ_REQUEST)) {
1558 panic("ohci_device_ctrl_done: not a request");
1561 xfer->hcpriv = NULL;
1565 ohci_device_intr_done(usbd_xfer_handle xfer)
1567 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1568 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1569 ohci_soft_ed_t *sed = opipe->sed;
1570 ohci_soft_td_t *data, *tail;
1573 DPRINTFN(10,("ohci_device_intr_done: xfer=%p, actlen=%d\n",
1574 xfer, xfer->actlen));
1576 xfer->hcpriv = NULL;
1578 if (xfer->pipe->repeat) {
1579 data = opipe->tail.td;
1580 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1582 xfer->status = USBD_NOMEM;
1587 data->td.td_flags = htole32(
1588 OHCI_TD_IN | OHCI_TD_NOCC |
1589 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1590 if (xfer->flags & USBD_SHORT_XFER_OK)
1591 data->td.td_flags |= htole32(OHCI_TD_R);
1592 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
1593 data->nexttd = tail;
1594 data->td.td_nexttd = htole32(tail->physaddr);
1595 data->td.td_be = htole32(le32toh(data->td.td_cbp) +
1597 data->len = xfer->length;
1599 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1600 xfer->hcpriv = data;
1603 sed->ed.ed_tailp = htole32(tail->physaddr);
1604 opipe->tail.td = tail;
1609 ohci_device_bulk_done(usbd_xfer_handle xfer)
1611 DPRINTFN(10,("ohci_device_bulk_done: xfer=%p, actlen=%d\n",
1612 xfer, xfer->actlen));
1614 xfer->hcpriv = NULL;
1618 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1620 usbd_pipe_handle pipe;
1625 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1626 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1627 sc, xfer, hstatus));
1630 /* Just ignore the change. */
1636 p = KERNADDR(&xfer->dmabuf, 0);
1637 m = min(sc->sc_noport, xfer->length * 8 - 1);
1638 memset(p, 0, xfer->length);
1639 for (i = 1; i <= m; i++) {
1640 /* Pick out CHANGE bits from the status reg. */
1641 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1642 p[i/8] |= 1 << (i%8);
1644 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1645 xfer->actlen = xfer->length;
1646 xfer->status = USBD_NORMAL_COMPLETION;
1648 usb_transfer_complete(xfer);
1652 ohci_root_intr_done(usbd_xfer_handle xfer)
1654 xfer->hcpriv = NULL;
1658 ohci_root_ctrl_done(usbd_xfer_handle xfer)
1660 xfer->hcpriv = NULL;
1664 * Wait here until controller claims to have an interrupt.
1665 * Then call ohci_intr and return. Use timeout to avoid waiting
1669 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1671 int timo = xfer->timeout;
1675 xfer->status = USBD_IN_PROGRESS;
1676 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1677 usb_delay_ms(&sc->sc_bus, 1);
1680 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1681 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1688 if (xfer->status != USBD_IN_PROGRESS)
1694 DPRINTF(("ohci_waitintr: timeout\n"));
1695 xfer->status = USBD_TIMEOUT;
1696 usb_transfer_complete(xfer);
1697 /* XXX should free TD */
1701 ohci_poll(struct usbd_bus *bus)
1703 ohci_softc_t *sc = (ohci_softc_t *)bus;
1707 new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1709 DPRINTFN(10,("ohci_poll: intrs=0x%04x\n", new));
1714 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1719 ohci_device_request(usbd_xfer_handle xfer)
1721 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1722 usb_device_request_t *req = &xfer->request;
1723 usbd_device_handle dev = opipe->pipe.device;
1724 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1725 int addr = dev->address;
1726 ohci_soft_td_t *setup, *stat, *next, *tail;
1727 ohci_soft_ed_t *sed;
1732 isread = req->bmRequestType & UT_READ;
1733 len = UGETW(req->wLength);
1735 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1736 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1737 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1738 UGETW(req->wIndex), len, addr,
1739 opipe->pipe.endpoint->edesc->bEndpointAddress));
1741 setup = opipe->tail.td;
1742 stat = ohci_alloc_std(sc);
1747 tail = ohci_alloc_std(sc);
1755 opipe->u.ctl.length = len;
1757 /* Update device address and length since they may have changed
1758 during the setup of the control pipe in usbd_new_device(). */
1759 /* XXX This only needs to be done once, but it's too early in open. */
1760 /* XXXX Should not touch ED here! */
1761 sed->ed.ed_flags = htole32(
1762 (le32toh(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1763 OHCI_ED_SET_FA(addr) |
1764 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1768 /* Set up data transaction */
1770 ohci_soft_td_t *std = stat;
1772 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
1774 stat = stat->nexttd; /* point at free TD */
1777 /* Start toggle at 1 and then use the carried toggle. */
1778 std->td.td_flags &= htole32(~OHCI_TD_TOGGLE_MASK);
1779 std->td.td_flags |= htole32(OHCI_TD_TOGGLE_1);
1782 memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1784 setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1785 OHCI_TD_TOGGLE_0 | OHCI_TD_SET_DI(6));
1786 setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma, 0));
1787 setup->nexttd = next;
1788 setup->td.td_nexttd = htole32(next->physaddr);
1789 setup->td.td_be = htole32(le32toh(setup->td.td_cbp) + sizeof *req - 1);
1793 xfer->hcpriv = setup;
1795 stat->td.td_flags = htole32(
1796 (isread ? OHCI_TD_OUT : OHCI_TD_IN) |
1797 OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1798 stat->td.td_cbp = 0;
1799 stat->nexttd = tail;
1800 stat->td.td_nexttd = htole32(tail->physaddr);
1802 stat->flags = OHCI_CALL_DONE;
1807 if (ohcidebug > 5) {
1808 DPRINTF(("ohci_device_request:\n"));
1810 ohci_dump_tds(setup);
1814 /* Insert ED in schedule */
1816 sed->ed.ed_tailp = htole32(tail->physaddr);
1817 opipe->tail.td = tail;
1818 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1819 if (xfer->timeout && !sc->sc_bus.use_polling) {
1820 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1821 ohci_timeout, xfer);
1826 if (ohcidebug > 20) {
1828 DPRINTF(("ohci_device_request: status=%x\n",
1829 OREAD4(sc, OHCI_COMMAND_STATUS)));
1831 printf("ctrl head:\n");
1832 ohci_dump_ed(sc->sc_ctrl_head);
1835 ohci_dump_tds(setup);
1839 return (USBD_NORMAL_COMPLETION);
1842 ohci_free_std(sc, tail);
1844 ohci_free_std(sc, stat);
1850 * Add an ED to the schedule. Called from a critical section.
1853 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1855 DPRINTFN(8,("ohci_add_ed: sed=%p head=%p\n", sed, head));
1857 sed->next = head->next;
1858 sed->ed.ed_nexted = head->ed.ed_nexted;
1860 head->ed.ed_nexted = htole32(sed->physaddr);
1864 * Remove an ED from the schedule. Called from a critical section.
1867 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1873 for (p = head; p != NULL && p->next != sed; p = p->next)
1877 panic("ohci_rem_ed: ED not found");
1881 * p == NULL if ohci is detaching and there are still devices
1882 * using ohci (e.g. usb sticks are still plugged in). But
1883 * the real solution should be correcting ohci_free_sed() or
1887 p->next = sed->next;
1888 p->ed.ed_nexted = sed->ed.ed_nexted;
1894 * When a transfer is completed the TD is added to the done queue by
1895 * the host controller. This queue is the processed by software.
1896 * Unfortunately the queue contains the physical address of the TD
1897 * and we have no simple way to translate this back to a kernel address.
1898 * To make the translation possible (and fast) we use a hash table of
1899 * TDs currently in the schedule. The physical address is used as the
1903 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1905 * Called from a critical section
1908 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1910 int h = HASH(std->physaddr);
1912 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1916 * Called from a critical section
1919 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1921 LIST_REMOVE(std, hnext);
1925 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1928 ohci_soft_td_t *std;
1930 /* if these are present they should be masked out at an earlier
1933 KASSERT((a&~OHCI_HEADMASK) == 0, ("%s: 0x%b has lower bits set\n",
1934 USBDEVNAME(sc->sc_bus.bdev),
1935 (int) a, "\20\1HALT\2TOGGLE"));
1937 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1939 std = LIST_NEXT(std, hnext))
1940 if (std->physaddr == a)
1943 DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1944 USBDEVNAME(sc->sc_bus.bdev), (u_long) a));
1949 * Called from a critical section
1952 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1954 int h = HASH(sitd->physaddr);
1956 DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n",
1957 sitd, (u_long)sitd->physaddr));
1959 LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
1963 * Called from a critical section
1966 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1968 DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n",
1969 sitd, (u_long)sitd->physaddr));
1971 LIST_REMOVE(sitd, hnext);
1975 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
1978 ohci_soft_itd_t *sitd;
1980 for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
1982 sitd = LIST_NEXT(sitd, hnext))
1983 if (sitd->physaddr == a)
1989 ohci_timeout(void *addr)
1991 struct ohci_xfer *oxfer = addr;
1992 struct ohci_pipe *opipe = (struct ohci_pipe *)oxfer->xfer.pipe;
1993 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1995 DPRINTF(("ohci_timeout: oxfer=%p\n", oxfer));
1998 ohci_abort_xfer(&oxfer->xfer, USBD_TIMEOUT);
2002 /* Execute the abort in a process context. */
2003 usb_add_task(oxfer->xfer.pipe->device, &oxfer->abort_task,
2008 ohci_timeout_task(void *addr)
2010 usbd_xfer_handle xfer = addr;
2012 DPRINTF(("ohci_timeout_task: xfer=%p\n", xfer));
2015 ohci_abort_xfer(xfer, USBD_TIMEOUT);
2021 ohci_dump_tds(ohci_soft_td_t *std)
2023 for (; std; std = std->nexttd)
2028 ohci_dump_td(ohci_soft_td_t *std)
2032 bitmask_snprintf((u_int32_t)le32toh(std->td.td_flags),
2033 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
2034 sbuf, sizeof(sbuf));
2036 printf("TD(%p) at %08lx: %s delay=%d ec=%d cc=%d\ncbp=0x%08lx "
2037 "nexttd=0x%08lx be=0x%08lx\n",
2038 std, (u_long)std->physaddr, sbuf,
2039 OHCI_TD_GET_DI(le32toh(std->td.td_flags)),
2040 OHCI_TD_GET_EC(le32toh(std->td.td_flags)),
2041 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
2042 (u_long)le32toh(std->td.td_cbp),
2043 (u_long)le32toh(std->td.td_nexttd),
2044 (u_long)le32toh(std->td.td_be));
2048 ohci_dump_itd(ohci_soft_itd_t *sitd)
2052 printf("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n"
2053 "bp0=0x%08lx next=0x%08lx be=0x%08lx\n",
2054 sitd, (u_long)sitd->physaddr,
2055 OHCI_ITD_GET_SF(le32toh(sitd->itd.itd_flags)),
2056 OHCI_ITD_GET_DI(le32toh(sitd->itd.itd_flags)),
2057 OHCI_ITD_GET_FC(le32toh(sitd->itd.itd_flags)),
2058 OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags)),
2059 (u_long)le32toh(sitd->itd.itd_bp0),
2060 (u_long)le32toh(sitd->itd.itd_nextitd),
2061 (u_long)le32toh(sitd->itd.itd_be));
2062 for (i = 0; i < OHCI_ITD_NOFFSET; i++)
2063 printf("offs[%d]=0x%04x ", i,
2064 (u_int)le16toh(sitd->itd.itd_offset[i]));
2069 ohci_dump_itds(ohci_soft_itd_t *sitd)
2071 for (; sitd; sitd = sitd->nextitd)
2072 ohci_dump_itd(sitd);
2076 ohci_dump_ed(ohci_soft_ed_t *sed)
2078 char sbuf[128], sbuf2[128];
2080 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_flags),
2081 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
2082 sbuf, sizeof(sbuf));
2083 bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_headp),
2084 "\20\1HALT\2CARRY", sbuf2, sizeof(sbuf2));
2086 printf("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d flags=%s\ntailp=0x%08lx "
2087 "headflags=%s headp=0x%08lx nexted=0x%08lx\n",
2088 sed, (u_long)sed->physaddr,
2089 OHCI_ED_GET_FA(le32toh(sed->ed.ed_flags)),
2090 OHCI_ED_GET_EN(le32toh(sed->ed.ed_flags)),
2091 OHCI_ED_GET_MAXP(le32toh(sed->ed.ed_flags)), sbuf,
2092 (u_long)le32toh(sed->ed.ed_tailp), sbuf2,
2093 (u_long)le32toh(sed->ed.ed_headp),
2094 (u_long)le32toh(sed->ed.ed_nexted));
2099 ohci_open(usbd_pipe_handle pipe)
2101 usbd_device_handle dev = pipe->device;
2102 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2103 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2104 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2105 u_int8_t addr = dev->address;
2106 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
2107 ohci_soft_ed_t *sed;
2108 ohci_soft_td_t *std;
2109 ohci_soft_itd_t *sitd;
2110 ohci_physaddr_t tdphys;
2115 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2116 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
2119 return (USBD_IOERROR);
2124 if (addr == sc->sc_addr) {
2125 switch (ed->bEndpointAddress) {
2126 case USB_CONTROL_ENDPOINT:
2127 pipe->methods = &ohci_root_ctrl_methods;
2129 case UE_DIR_IN | OHCI_INTR_ENDPT:
2130 pipe->methods = &ohci_root_intr_methods;
2133 return (USBD_INVAL);
2136 sed = ohci_alloc_sed(sc);
2140 if (xfertype == UE_ISOCHRONOUS) {
2141 sitd = ohci_alloc_sitd(sc);
2144 opipe->tail.itd = sitd;
2145 opipe->aborting = 0;
2146 tdphys = sitd->physaddr;
2147 fmt = OHCI_ED_FORMAT_ISO;
2148 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
2149 fmt |= OHCI_ED_DIR_IN;
2151 fmt |= OHCI_ED_DIR_OUT;
2153 std = ohci_alloc_std(sc);
2156 opipe->tail.td = std;
2157 tdphys = std->physaddr;
2158 fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
2160 sed->ed.ed_flags = htole32(
2161 OHCI_ED_SET_FA(addr) |
2162 OHCI_ED_SET_EN(UE_GET_ADDR(ed->bEndpointAddress)) |
2163 (dev->speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) |
2165 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
2166 sed->ed.ed_headp = htole32(tdphys |
2167 (pipe->endpoint->savedtoggle ? OHCI_TOGGLECARRY : 0));
2168 sed->ed.ed_tailp = htole32(tdphys);
2172 pipe->methods = &ohci_device_ctrl_methods;
2173 err = usb_allocmem(&sc->sc_bus,
2174 sizeof(usb_device_request_t),
2175 0, &opipe->u.ctl.reqdma);
2179 ohci_add_ed(sed, sc->sc_ctrl_head);
2183 pipe->methods = &ohci_device_intr_methods;
2184 ival = pipe->interval;
2185 if (ival == USBD_DEFAULT_INTERVAL)
2186 ival = ed->bInterval;
2187 return (ohci_device_setintr(sc, opipe, ival));
2188 case UE_ISOCHRONOUS:
2189 pipe->methods = &ohci_device_isoc_methods;
2190 return (ohci_setup_isoc(pipe));
2192 pipe->methods = &ohci_device_bulk_methods;
2194 ohci_add_ed(sed, sc->sc_bulk_head);
2199 return (USBD_NORMAL_COMPLETION);
2203 ohci_free_std(sc, std);
2206 ohci_free_sed(sc, sed);
2208 return (USBD_NOMEM);
2213 * Close a reqular pipe.
2214 * Assumes that there are no pending transactions.
2217 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
2219 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2220 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2221 ohci_soft_ed_t *sed = opipe->sed;
2225 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2226 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2227 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK)) {
2228 ohci_soft_td_t *std;
2229 std = ohci_hash_find_td(sc, le32toh(sed->ed.ed_headp));
2230 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
2231 "tl=0x%x pipe=%p, std=%p\n", sed,
2232 (int)le32toh(sed->ed.ed_headp),
2233 (int)le32toh(sed->ed.ed_tailp),
2236 usbd_dump_pipe(&opipe->pipe);
2243 usb_delay_ms(&sc->sc_bus, 2);
2244 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2245 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2246 printf("ohci_close_pipe: pipe still not empty\n");
2249 ohci_rem_ed(sed, head);
2250 /* Make sure the host controller is not touching this ED */
2251 usb_delay_ms(&sc->sc_bus, 1);
2253 pipe->endpoint->savedtoggle =
2254 (le32toh(sed->ed.ed_headp) & OHCI_TOGGLECARRY) ? 1 : 0;
2255 ohci_free_sed(sc, opipe->sed);
2259 * Abort a device request.
2260 * If this routine is called from a critical section it guarantees that
2261 * the request will be removed from the hardware scheduling and that
2262 * the callback for it will be called with USBD_CANCELLED status.
2263 * It's impossible to guarantee that the requested transfer will not
2264 * have happened since the hardware runs concurrently.
2265 * If the transaction has already happened we rely on the ordinary
2266 * interrupt processing to process it.
2269 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2271 struct ohci_xfer *oxfer = OXFER(xfer);
2272 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2273 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
2274 ohci_soft_ed_t *sed = opipe->sed;
2275 ohci_soft_td_t *p, *n;
2276 ohci_physaddr_t headp;
2279 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p sed=%p\n", xfer, opipe,sed));
2282 /* If we're dying, just do the software part. */
2284 xfer->status = status; /* make software ignore it */
2285 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
2286 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2287 usb_transfer_complete(xfer);
2291 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
2292 panic("ohci_abort_xfer: not in process context");
2295 * If an abort is already in progress then just wait for it to
2296 * complete and return.
2298 if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING) {
2299 DPRINTFN(2, ("ohci_abort_xfer: already aborting\n"));
2300 /* No need to wait if we're aborting from a timeout. */
2301 if (status == USBD_TIMEOUT)
2303 /* Override the status which might be USBD_TIMEOUT. */
2304 xfer->status = status;
2305 DPRINTFN(2, ("ohci_abort_xfer: waiting for abort to finish\n"));
2306 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTWAIT;
2307 while (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING)
2308 tsleep(&oxfer->ohci_xfer_flags, 0, "ohciaw", 0);
2313 * Step 1: Make interrupt routine and hardware ignore xfer.
2316 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTING;
2317 xfer->status = status; /* make software ignore it */
2318 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
2319 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2321 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
2322 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
2325 * Step 2: Wait until we know hardware has finished any possible
2326 * use of the xfer. Also make sure the soft interrupt routine
2329 usb_delay_ms(opipe->pipe.device->bus, 20); /* Hardware finishes in 1ms */
2331 #ifdef USB_USE_SOFTINTR
2332 sc->sc_softwake = 1;
2333 #endif /* USB_USE_SOFTINTR */
2334 usb_schedsoftintr(&sc->sc_bus);
2335 #ifdef USB_USE_SOFTINTR
2336 tsleep(&sc->sc_softwake, 0, "ohciab", 0);
2337 #endif /* USB_USE_SOFTINTR */
2340 * Step 3: Remove any vestiges of the xfer from the hardware.
2341 * The complication here is that the hardware may have executed
2342 * beyond the xfer we're trying to abort. So as we're scanning
2343 * the TDs of this xfer we check if the hardware points to
2349 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING; /* XXX */
2351 printf("ohci_abort_xfer: hcpriv is NULL\n");
2356 if (ohcidebug > 1) {
2357 DPRINTF(("ohci_abort_xfer: sed=\n"));
2362 headp = le32toh(sed->ed.ed_headp) & OHCI_HEADMASK;
2364 for (; p->xfer == xfer; p = n) {
2365 hit |= headp == p->physaddr;
2367 ohci_free_std(sc, p);
2369 /* Zap headp register if hardware pointed inside the xfer. */
2371 DPRINTFN(1,("ohci_abort_xfer: set hd=0x08%x, tl=0x%08x\n",
2372 (int)p->physaddr, (int)le32toh(sed->ed.ed_tailp)));
2373 sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */
2375 DPRINTFN(1,("ohci_abort_xfer: no hit\n"));
2379 * Step 4: Turn on hardware again.
2381 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
2384 * Step 5: Execute callback.
2386 /* Do the wakeup first to avoid touching the xfer after the callback. */
2387 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING;
2388 if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTWAIT) {
2389 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTWAIT;
2390 wakeup(&oxfer->ohci_xfer_flags);
2392 usb_transfer_complete(xfer);
2398 * Data structures and routines to emulate the root hub.
2400 Static usb_device_descriptor_t ohci_devd = {
2401 USB_DEVICE_DESCRIPTOR_SIZE,
2402 UDESC_DEVICE, /* type */
2403 {0x00, 0x01}, /* USB version */
2404 UDCLASS_HUB, /* class */
2405 UDSUBCLASS_HUB, /* subclass */
2406 UDPROTO_FSHUB, /* protocol */
2407 64, /* max packet */
2408 {0},{0},{0x00,0x01}, /* device id */
2409 1,2,0, /* string indicies */
2410 1 /* # of configurations */
2413 Static usb_config_descriptor_t ohci_confd = {
2414 USB_CONFIG_DESCRIPTOR_SIZE,
2416 {USB_CONFIG_DESCRIPTOR_SIZE +
2417 USB_INTERFACE_DESCRIPTOR_SIZE +
2418 USB_ENDPOINT_DESCRIPTOR_SIZE},
2426 Static usb_interface_descriptor_t ohci_ifcd = {
2427 USB_INTERFACE_DESCRIPTOR_SIZE,
2438 Static usb_endpoint_descriptor_t ohci_endpd = {
2439 USB_ENDPOINT_DESCRIPTOR_SIZE,
2441 UE_DIR_IN | OHCI_INTR_ENDPT,
2443 {8, 0}, /* max packet */
2447 Static usb_hub_descriptor_t ohci_hubd = {
2448 USB_HUB_DESCRIPTOR_SIZE,
2458 ohci_str(usb_string_descriptor_t *p, int l, const char *s)
2464 p->bLength = 2 * strlen(s) + 2;
2467 p->bDescriptorType = UDESC_STRING;
2469 for (i = 0; s[i] && l > 1; i++, l -= 2)
2470 USETW2(p->bString[i], 0, s[i]);
2475 * Simulate a hardware hub by handling all the necessary requests.
2478 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
2482 /* Insert last in queue. */
2483 err = usb_insert_transfer(xfer);
2487 /* Pipe isn't running, start first */
2488 return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2492 ohci_root_ctrl_start(usbd_xfer_handle xfer)
2494 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2495 usb_device_request_t *req;
2498 int len, value, index, l, totlen = 0;
2499 usb_port_status_t ps;
2500 usb_hub_descriptor_t hubd;
2505 return (USBD_IOERROR);
2508 if (!(xfer->rqflags & URQ_REQUEST))
2510 return (USBD_INVAL);
2512 req = &xfer->request;
2514 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2515 req->bmRequestType, req->bRequest));
2517 len = UGETW(req->wLength);
2518 value = UGETW(req->wValue);
2519 index = UGETW(req->wIndex);
2522 buf = KERNADDR(&xfer->dmabuf, 0);
2524 #define C(x,y) ((x) | ((y) << 8))
2525 switch(C(req->bRequest, req->bmRequestType)) {
2526 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2527 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2528 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2530 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2531 * for the integrated root hub.
2534 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2536 *(u_int8_t *)buf = sc->sc_conf;
2540 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2541 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2542 switch(value >> 8) {
2544 if ((value & 0xff) != 0) {
2548 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2549 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2550 memcpy(buf, &ohci_devd, l);
2553 if ((value & 0xff) != 0) {
2557 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2558 memcpy(buf, &ohci_confd, l);
2559 buf = (char *)buf + l;
2561 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2563 memcpy(buf, &ohci_ifcd, l);
2564 buf = (char *)buf + l;
2566 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2568 memcpy(buf, &ohci_endpd, l);
2573 *(u_int8_t *)buf = 0;
2575 switch (value & 0xff) {
2576 case 1: /* Vendor */
2577 totlen = ohci_str(buf, len, sc->sc_vendor);
2579 case 2: /* Product */
2580 totlen = ohci_str(buf, len, "OHCI root hub");
2589 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2591 *(u_int8_t *)buf = 0;
2595 case C(UR_GET_STATUS, UT_READ_DEVICE):
2597 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2601 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2602 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2604 USETW(((usb_status_t *)buf)->wStatus, 0);
2608 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2609 if (value >= USB_MAX_DEVICES) {
2613 sc->sc_addr = value;
2615 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2616 if (value != 0 && value != 1) {
2620 sc->sc_conf = value;
2622 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2624 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2625 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2626 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2629 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2631 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2634 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2636 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2637 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2638 "port=%d feature=%d\n",
2640 if (index < 1 || index > sc->sc_noport) {
2644 port = OHCI_RH_PORT_STATUS(index);
2646 case UHF_PORT_ENABLE:
2647 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2649 case UHF_PORT_SUSPEND:
2650 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2652 case UHF_PORT_POWER:
2653 /* Yes, writing to the LOW_SPEED bit clears power. */
2654 OWRITE4(sc, port, UPS_LOW_SPEED);
2656 case UHF_C_PORT_CONNECTION:
2657 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2659 case UHF_C_PORT_ENABLE:
2660 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2662 case UHF_C_PORT_SUSPEND:
2663 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2665 case UHF_C_PORT_OVER_CURRENT:
2666 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2668 case UHF_C_PORT_RESET:
2669 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2676 case UHF_C_PORT_CONNECTION:
2677 case UHF_C_PORT_ENABLE:
2678 case UHF_C_PORT_SUSPEND:
2679 case UHF_C_PORT_OVER_CURRENT:
2680 case UHF_C_PORT_RESET:
2681 /* Enable RHSC interrupt if condition is cleared. */
2682 if ((OREAD4(sc, port) >> 16) == 0)
2683 ohci_rhsc_able(sc, 1);
2689 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2690 if ((value & 0xff) != 0) {
2694 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2696 hubd.bNbrPorts = sc->sc_noport;
2697 USETW(hubd.wHubCharacteristics,
2698 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2699 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2700 /* XXX overcurrent */
2702 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2703 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2704 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2705 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2706 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2707 l = min(len, hubd.bDescLength);
2709 memcpy(buf, &hubd, l);
2711 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2716 memset(buf, 0, len); /* ? XXX */
2719 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2720 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2722 if (index < 1 || index > sc->sc_noport) {
2730 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2731 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2733 USETW(ps.wPortStatus, v);
2734 USETW(ps.wPortChange, v >> 16);
2735 l = min(len, sizeof ps);
2736 memcpy(buf, &ps, l);
2739 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2742 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2744 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2745 if (index < 1 || index > sc->sc_noport) {
2749 port = OHCI_RH_PORT_STATUS(index);
2751 case UHF_PORT_ENABLE:
2752 OWRITE4(sc, port, UPS_PORT_ENABLED);
2754 case UHF_PORT_SUSPEND:
2755 OWRITE4(sc, port, UPS_SUSPEND);
2757 case UHF_PORT_RESET:
2758 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2760 OWRITE4(sc, port, UPS_RESET);
2761 for (i = 0; i < 5; i++) {
2762 usb_delay_ms(&sc->sc_bus,
2763 USB_PORT_ROOT_RESET_DELAY);
2768 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2771 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2772 index, OREAD4(sc, port)));
2774 case UHF_PORT_POWER:
2775 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2777 OWRITE4(sc, port, UPS_PORT_POWER);
2788 xfer->actlen = totlen;
2789 err = USBD_NORMAL_COMPLETION;
2793 usb_transfer_complete(xfer);
2795 return (USBD_IN_PROGRESS);
2798 /* Abort a root control request. */
2800 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2802 /* Nothing to do, all transfers are synchronous. */
2805 /* Close the root pipe. */
2807 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2809 DPRINTF(("ohci_root_ctrl_close\n"));
2810 /* Nothing to do. */
2814 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2818 /* Insert last in queue. */
2819 err = usb_insert_transfer(xfer);
2823 /* Pipe isn't running, start first */
2824 return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2828 ohci_root_intr_start(usbd_xfer_handle xfer)
2830 usbd_pipe_handle pipe = xfer->pipe;
2831 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2834 return (USBD_IOERROR);
2836 sc->sc_intrxfer = xfer;
2838 return (USBD_IN_PROGRESS);
2841 /* Abort a root interrupt request. */
2843 ohci_root_intr_abort(usbd_xfer_handle xfer)
2845 if (xfer->pipe->intrxfer == xfer) {
2846 DPRINTF(("ohci_root_intr_abort: remove\n"));
2847 xfer->pipe->intrxfer = NULL;
2849 xfer->status = USBD_CANCELLED;
2851 usb_transfer_complete(xfer);
2855 /* Close the root pipe. */
2857 ohci_root_intr_close(usbd_pipe_handle pipe)
2859 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2861 DPRINTF(("ohci_root_intr_close\n"));
2863 sc->sc_intrxfer = NULL;
2866 /************************/
2869 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2873 /* Insert last in queue. */
2874 err = usb_insert_transfer(xfer);
2878 /* Pipe isn't running, start first */
2879 return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2883 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2885 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2889 return (USBD_IOERROR);
2892 if (!(xfer->rqflags & URQ_REQUEST)) {
2894 printf("ohci_device_ctrl_transfer: not a request\n");
2895 return (USBD_INVAL);
2899 err = ohci_device_request(xfer);
2903 if (sc->sc_bus.use_polling)
2904 ohci_waitintr(sc, xfer);
2905 return (USBD_IN_PROGRESS);
2908 /* Abort a device control request. */
2910 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2912 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2913 ohci_abort_xfer(xfer, USBD_CANCELLED);
2916 /* Close a device control pipe. */
2918 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2920 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2921 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2923 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2924 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2925 ohci_free_std(sc, opipe->tail.td);
2928 /************************/
2931 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2933 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2935 opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY);
2939 ohci_noop(usbd_pipe_handle pipe)
2944 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2948 /* Insert last in queue. */
2949 err = usb_insert_transfer(xfer);
2953 /* Pipe isn't running, start first */
2954 return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2958 ohci_device_bulk_start(usbd_xfer_handle xfer)
2960 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2961 usbd_device_handle dev = opipe->pipe.device;
2962 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2963 int addr = dev->address;
2964 ohci_soft_td_t *data, *tail, *tdp;
2965 ohci_soft_ed_t *sed;
2966 int len, isread, endpt;
2970 return (USBD_IOERROR);
2973 if (xfer->rqflags & URQ_REQUEST) {
2975 printf("ohci_device_bulk_start: a request\n");
2976 return (USBD_INVAL);
2981 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2982 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2985 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2986 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2989 opipe->u.bulk.isread = isread;
2990 opipe->u.bulk.length = len;
2992 /* Update device address */
2993 sed->ed.ed_flags = htole32(
2994 (le32toh(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2995 OHCI_ED_SET_FA(addr));
2997 /* Allocate a chain of new TDs (including a new tail). */
2998 data = opipe->tail.td;
2999 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
3001 /* We want interrupt at the end of the transfer. */
3002 tail->td.td_flags &= htole32(~OHCI_TD_INTR_MASK);
3003 tail->td.td_flags |= htole32(OHCI_TD_SET_DI(1));
3004 tail->flags |= OHCI_CALL_DONE;
3005 tail = tail->nexttd; /* point at sentinel */
3010 xfer->hcpriv = data;
3012 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
3013 "td_cbp=0x%08x td_be=0x%08x\n",
3014 (int)le32toh(sed->ed.ed_flags),
3015 (int)le32toh(data->td.td_flags),
3016 (int)le32toh(data->td.td_cbp),
3017 (int)le32toh(data->td.td_be)));
3020 if (ohcidebug > 5) {
3022 ohci_dump_tds(data);
3026 /* Insert ED in schedule */
3028 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
3031 sed->ed.ed_tailp = htole32(tail->physaddr);
3032 opipe->tail.td = tail;
3033 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3034 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
3035 if (xfer->timeout && !sc->sc_bus.use_polling) {
3036 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3037 ohci_timeout, xfer);
3041 /* This goes wrong if we are too slow. */
3042 if (ohcidebug > 10) {
3044 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
3045 OREAD4(sc, OHCI_COMMAND_STATUS)));
3047 ohci_dump_tds(data);
3053 if (sc->sc_bus.use_polling)
3054 ohci_waitintr(sc, xfer);
3056 return (USBD_IN_PROGRESS);
3060 ohci_device_bulk_abort(usbd_xfer_handle xfer)
3062 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
3063 ohci_abort_xfer(xfer, USBD_CANCELLED);
3067 * Close a device bulk pipe.
3070 ohci_device_bulk_close(usbd_pipe_handle pipe)
3072 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3073 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3075 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
3076 ohci_close_pipe(pipe, sc->sc_bulk_head);
3077 ohci_free_std(sc, opipe->tail.td);
3080 /************************/
3083 ohci_device_intr_transfer(usbd_xfer_handle xfer)
3087 /* Insert last in queue. */
3088 err = usb_insert_transfer(xfer);
3092 /* Pipe isn't running, start first */
3093 return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3097 ohci_device_intr_start(usbd_xfer_handle xfer)
3099 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3100 usbd_device_handle dev = opipe->pipe.device;
3101 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3102 ohci_soft_ed_t *sed = opipe->sed;
3103 ohci_soft_td_t *data, *tail;
3107 return (USBD_IOERROR);
3109 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
3110 "flags=%d priv=%p\n",
3111 xfer, xfer->length, xfer->flags, xfer->priv));
3114 if (xfer->rqflags & URQ_REQUEST)
3115 panic("ohci_device_intr_transfer: a request");
3120 data = opipe->tail.td;
3121 tail = ohci_alloc_std(sc);
3123 return (USBD_NOMEM);
3126 data->td.td_flags = htole32(
3127 OHCI_TD_IN | OHCI_TD_NOCC |
3128 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
3129 if (xfer->flags & USBD_SHORT_XFER_OK)
3130 data->td.td_flags |= htole32(OHCI_TD_R);
3131 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0));
3132 data->nexttd = tail;
3133 data->td.td_nexttd = htole32(tail->physaddr);
3134 data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1);
3137 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
3138 xfer->hcpriv = data;
3141 if (ohcidebug > 5) {
3142 DPRINTF(("ohci_device_intr_transfer:\n"));
3144 ohci_dump_tds(data);
3148 /* Insert ED in schedule */
3150 sed->ed.ed_tailp = htole32(tail->physaddr);
3151 opipe->tail.td = tail;
3152 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3156 * This goes horribly wrong, printing thousands of descriptors,
3157 * because false references are followed due to the fact that the
3160 if (ohcidebug > 5) {
3161 usb_delay_ms(&sc->sc_bus, 5);
3162 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
3163 OREAD4(sc, OHCI_COMMAND_STATUS)));
3165 ohci_dump_tds(data);
3170 return (USBD_IN_PROGRESS);
3173 /* Abort a device control request. */
3175 ohci_device_intr_abort(usbd_xfer_handle xfer)
3177 if (xfer->pipe->intrxfer == xfer) {
3178 DPRINTF(("ohci_device_intr_abort: remove\n"));
3179 xfer->pipe->intrxfer = NULL;
3181 ohci_abort_xfer(xfer, USBD_CANCELLED);
3184 /* Close a device interrupt pipe. */
3186 ohci_device_intr_close(usbd_pipe_handle pipe)
3188 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3189 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3190 int nslots = opipe->u.intr.nslots;
3191 int pos = opipe->u.intr.pos;
3193 ohci_soft_ed_t *p, *sed = opipe->sed;
3195 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
3196 pipe, nslots, pos));
3198 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
3199 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3200 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3201 usb_delay_ms(&sc->sc_bus, 2);
3203 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3204 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3205 panic("%s: Intr pipe %p still has TDs queued",
3206 USBDEVNAME(sc->sc_bus.bdev), pipe);
3209 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
3213 panic("ohci_device_intr_close: ED not found");
3215 p->next = sed->next;
3216 p->ed.ed_nexted = sed->ed.ed_nexted;
3219 for (j = 0; j < nslots; j++)
3220 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
3222 ohci_free_std(sc, opipe->tail.td);
3223 ohci_free_sed(sc, opipe->sed);
3227 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
3230 u_int npoll, slow, shigh, nslots;
3232 ohci_soft_ed_t *hsed, *sed = opipe->sed;
3234 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
3236 printf("ohci_setintr: 0 interval\n");
3237 return (USBD_INVAL);
3240 npoll = OHCI_NO_INTRS;
3241 while (npoll > ival)
3243 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
3246 * We now know which level in the tree the ED must go into.
3247 * Figure out which slot has most bandwidth left over.
3253 * 8 7 8 9 10 11 12 13 14
3254 * N (N-1) .. (N-1+N-1)
3257 shigh = slow + npoll;
3258 nslots = OHCI_NO_INTRS / npoll;
3259 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
3261 for (j = 0; j < nslots; j++)
3262 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
3268 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
3269 best, slow, shigh, bestbw));
3272 hsed = sc->sc_eds[best];
3273 sed->next = hsed->next;
3274 sed->ed.ed_nexted = hsed->ed.ed_nexted;
3276 hsed->ed.ed_nexted = htole32(sed->physaddr);
3279 for (j = 0; j < nslots; j++)
3280 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
3281 opipe->u.intr.nslots = nslots;
3282 opipe->u.intr.pos = best;
3284 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
3285 return (USBD_NORMAL_COMPLETION);
3288 /***********************/
3291 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
3295 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
3297 /* Put it on our queue, */
3298 err = usb_insert_transfer(xfer);
3300 /* bail out on error, */
3301 if (err && err != USBD_IN_PROGRESS)
3304 /* XXX should check inuse here */
3306 /* insert into schedule, */
3307 ohci_device_isoc_enter(xfer);
3309 /* and start if the pipe wasn't running */
3311 ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
3317 ohci_device_isoc_enter(usbd_xfer_handle xfer)
3319 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3320 usbd_device_handle dev = opipe->pipe.device;
3321 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3322 ohci_soft_ed_t *sed = opipe->sed;
3323 struct iso *iso = &opipe->u.iso;
3324 struct ohci_xfer *oxfer = (struct ohci_xfer *)xfer;
3325 ohci_soft_itd_t *sitd, *nsitd;
3326 ohci_physaddr_t buf, offs, noffs, bp0, tdphys;
3327 int i, ncur, nframes;
3329 DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p "
3331 iso->inuse, iso->next, xfer, xfer->nframes));
3336 if (iso->next == -1) {
3337 /* Not in use yet, schedule it a few frames ahead. */
3338 iso->next = le32toh(sc->sc_hcca->hcca_frame_number) + 5;
3339 DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n",
3345 for (sitd = xfer->hcpriv; sitd != NULL && sitd->xfer == xfer;
3346 sitd = sitd->nextitd)
3347 ohci_free_sitd(sc, sitd); /* Free ITDs in prev xfer*/
3351 sitd = ohci_alloc_sitd(sc);
3353 panic("cant alloc isoc");
3354 opipe->tail.itd = sitd;
3355 tdphys = sitd->physaddr;
3356 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop*/
3358 sed->ed.ed_tailp = htole32(tdphys);
3359 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* Start.*/
3363 sitd = opipe->tail.itd;
3364 buf = DMAADDR(&xfer->dmabuf, 0);
3365 bp0 = OHCI_PAGE(buf);
3366 offs = OHCI_PAGE_OFFSET(buf);
3367 nframes = xfer->nframes;
3368 xfer->hcpriv = sitd;
3369 for (i = ncur = 0; i < nframes; i++, ncur++) {
3370 noffs = offs + xfer->frlengths[i];
3371 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
3372 OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */
3374 /* Allocate next ITD */
3375 nsitd = ohci_alloc_sitd(sc);
3376 if (nsitd == NULL) {
3378 printf("%s: isoc TD alloc failed\n",
3379 USBDEVNAME(sc->sc_bus.bdev));
3383 /* Fill current ITD */
3384 sitd->itd.itd_flags = htole32(
3386 OHCI_ITD_SET_SF(iso->next) |
3387 OHCI_ITD_SET_DI(6) | /* delay intr a little */
3388 OHCI_ITD_SET_FC(ncur));
3389 sitd->itd.itd_bp0 = htole32(bp0);
3390 sitd->nextitd = nsitd;
3391 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3392 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3394 sitd->flags = OHCI_ITD_ACTIVE;
3397 iso->next = iso->next + ncur;
3398 bp0 = OHCI_PAGE(buf + offs);
3401 sitd->itd.itd_offset[ncur] = htole16(OHCI_ITD_MK_OFFS(offs));
3404 nsitd = ohci_alloc_sitd(sc);
3405 if (nsitd == NULL) {
3407 printf("%s: isoc TD alloc failed\n",
3408 USBDEVNAME(sc->sc_bus.bdev));
3411 /* Fixup last used ITD */
3412 sitd->itd.itd_flags = htole32(
3414 OHCI_ITD_SET_SF(iso->next) |
3415 OHCI_ITD_SET_DI(0) |
3416 OHCI_ITD_SET_FC(ncur));
3417 sitd->itd.itd_bp0 = htole32(bp0);
3418 sitd->nextitd = nsitd;
3419 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3420 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3422 sitd->flags = OHCI_CALL_DONE | OHCI_ITD_ACTIVE;
3424 iso->next = iso->next + ncur;
3425 iso->inuse += nframes;
3427 xfer->actlen = offs; /* XXX pretend we did it all */
3429 xfer->status = USBD_IN_PROGRESS;
3431 oxfer->ohci_xfer_flags |= OHCI_ISOC_DIRTY;
3434 if (ohcidebug > 5) {
3435 DPRINTF(("ohci_device_isoc_enter: frame=%d\n",
3436 le32toh(sc->sc_hcca->hcca_frame_number)));
3437 ohci_dump_itds(xfer->hcpriv);
3443 sed->ed.ed_tailp = htole32(nsitd->physaddr);
3444 opipe->tail.itd = nsitd;
3445 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3449 if (ohcidebug > 5) {
3451 DPRINTF(("ohci_device_isoc_enter: after frame=%d\n",
3452 le32toh(sc->sc_hcca->hcca_frame_number)));
3453 ohci_dump_itds(xfer->hcpriv);
3460 ohci_device_isoc_start(usbd_xfer_handle xfer)
3462 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3463 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3464 ohci_soft_ed_t *sed;
3466 DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer));
3469 return (USBD_IOERROR);
3472 if (xfer->status != USBD_IN_PROGRESS)
3473 printf("ohci_device_isoc_start: not in progress %p\n", xfer);
3476 /* XXX anything to do? */
3479 sed = opipe->sed; /* Turn off ED skip-bit to start processing */
3480 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* ED's ITD list.*/
3483 return (USBD_IN_PROGRESS);
3487 ohci_device_isoc_abort(usbd_xfer_handle xfer)
3489 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3490 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3491 ohci_soft_ed_t *sed;
3492 ohci_soft_itd_t *sitd, *tmp_sitd;
3493 int undone, num_sitds;
3496 opipe->aborting = 1;
3498 DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer));
3500 /* Transfer is already done. */
3501 if (xfer->status != USBD_NOT_STARTED &&
3502 xfer->status != USBD_IN_PROGRESS) {
3504 printf("ohci_device_isoc_abort: early return\n");
3508 /* Give xfer the requested abort code. */
3509 xfer->status = USBD_CANCELLED;
3512 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
3515 sitd = xfer->hcpriv;
3519 printf("ohci_device_isoc_abort: hcpriv==0\n");
3523 for (; sitd != NULL && sitd->xfer == xfer; sitd = sitd->nextitd) {
3526 DPRINTFN(1,("abort sets done sitd=%p\n", sitd));
3534 * Each sitd has up to OHCI_ITD_NOFFSET transfers, each can
3535 * take a usb 1ms cycle. Conservatively wait for it to drain.
3536 * Even with DMA done, it can take awhile for the "batch"
3537 * delivery of completion interrupts to occur thru the controller.
3541 usb_delay_ms(&sc->sc_bus, 2*(num_sitds*OHCI_ITD_NOFFSET));
3544 tmp_sitd = xfer->hcpriv;
3545 for (; tmp_sitd != NULL && tmp_sitd->xfer == xfer;
3546 tmp_sitd = tmp_sitd->nextitd) {
3547 if (OHCI_CC_NO_ERROR ==
3548 OHCI_ITD_GET_CC(le32toh(tmp_sitd->itd.itd_flags)) &&
3549 tmp_sitd->flags & OHCI_ITD_ACTIVE &&
3550 (tmp_sitd->flags & OHCI_ITD_INTFIN) == 0)
3553 } while( undone != 0 );
3558 usb_transfer_complete(xfer);
3562 * Only if there is a `next' sitd in next xfer...
3563 * unlink this xfer's sitds.
3565 sed->ed.ed_headp = htole32(sitd->physaddr);
3567 sed->ed.ed_headp = 0;
3569 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
3575 ohci_device_isoc_done(usbd_xfer_handle xfer)
3577 /* This null routine corresponds to non-isoc "done()" routines
3578 * that free the stds associated with an xfer after a completed
3579 * xfer interrupt. However, in the case of isoc transfers, the
3580 * sitds associated with the transfer have already been processed
3581 * and reallocated for the next iteration by
3582 * "ohci_device_isoc_transfer()".
3584 * Routine "usb_transfer_complete()" is called at the end of every
3585 * relevant usb interrupt. "usb_transfer_complete()" indirectly
3586 * calls 1) "ohci_device_isoc_transfer()" (which keeps pumping the
3587 * pipeline by setting up the next transfer iteration) and 2) then
3588 * calls "ohci_device_isoc_done()". Isoc transfers have not been
3589 * working for the ohci usb because this routine was trashing the
3590 * xfer set up for the next iteration (thus, only the first
3591 * UGEN_NISOREQS xfers outstanding on an open would work). Perhaps
3592 * this could all be re-factored, but that's another pass...
3597 ohci_setup_isoc(usbd_pipe_handle pipe)
3599 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3600 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3601 struct iso *iso = &opipe->u.iso;
3607 ohci_add_ed(opipe->sed, sc->sc_isoc_head);
3610 return (USBD_NORMAL_COMPLETION);
3614 ohci_device_isoc_close(usbd_pipe_handle pipe)
3616 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3617 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3618 ohci_soft_ed_t *sed;
3620 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
3623 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop device. */
3625 ohci_close_pipe(pipe, sc->sc_isoc_head); /* Stop isoc list, free ED.*/
3627 /* up to NISOREQs xfers still outstanding. */
3630 opipe->tail.itd->isdone = 1;
3632 ohci_free_sitd(sc, opipe->tail.itd); /* Next `avail free' sitd.*/