1 /* $NetBSD: ohci.c,v 1.64 2000/01/19 00:23:58 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/ohci.c,v 1.39.2.9 2003/03/05 17:09:44 shiba Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * USB Open Host Controller driver.
44 * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
45 * USB spec: http://www.usb.org/developers/data/usb11.pdf
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/select.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
58 #include <machine/bus_pio.h>
59 #include <machine/bus_memio.h>
60 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
61 #include <machine/cpu.h>
65 #include <sys/queue.h>
66 #include <sys/sysctl.h>
68 #include <machine/bus.h>
69 #include <machine/endian.h>
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdivar.h>
74 #include <dev/usb/usb_mem.h>
75 #include <dev/usb/usb_quirks.h>
77 #include <dev/usb/ohcireg.h>
78 #include <dev/usb/ohcivar.h>
80 #if defined(__FreeBSD__)
81 #include <machine/clock.h>
83 #define delay(d) DELAY(d)
86 #if defined(__OpenBSD__)
87 struct cfdriver ohci_cd = {
93 #define DPRINTF(x) if (ohcidebug) logprintf x
94 #define DPRINTFN(n,x) if (ohcidebug>(n)) logprintf x
96 SYSCTL_NODE(_hw_usb, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci");
97 SYSCTL_INT(_hw_usb_ohci, OID_AUTO, debug, CTLFLAG_RW,
98 &ohcidebug, 0, "ohci debug level");
101 #define DPRINTFN(n,x)
105 * The OHCI controller is little endian, so on big endian machines
106 * the data strored in memory needs to be swapped.
108 #if BYTE_ORDER == BIG_ENDIAN
109 #define LE(x) (bswap32(x))
116 Static ohci_soft_ed_t *ohci_alloc_sed(ohci_softc_t *);
117 Static void ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
119 Static ohci_soft_td_t *ohci_alloc_std(ohci_softc_t *);
120 Static void ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
122 Static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
123 Static void ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
126 Static void ohci_free_std_chain(ohci_softc_t *,
127 ohci_soft_td_t *, ohci_soft_td_t *);
129 Static usbd_status ohci_alloc_std_chain(struct ohci_pipe *,
130 ohci_softc_t *, int, int, u_int16_t, usb_dma_t *,
131 ohci_soft_td_t *, ohci_soft_td_t **);
133 #if defined(__NetBSD__) || defined(__OpenBSD__)
134 Static void ohci_shutdown(void *v);
135 Static void ohci_power(int, void *);
137 Static usbd_status ohci_open(usbd_pipe_handle);
138 Static void ohci_poll(struct usbd_bus *);
139 Static void ohci_waitintr(ohci_softc_t *,
141 Static void ohci_rhsc(ohci_softc_t *, usbd_xfer_handle);
142 Static void ohci_process_done(ohci_softc_t *,
145 Static usbd_status ohci_device_request(usbd_xfer_handle xfer);
146 Static void ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
147 Static void ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
148 Static void ohci_hash_add_td(ohci_softc_t *,
150 Static void ohci_hash_rem_td(ohci_softc_t *,
152 Static ohci_soft_td_t *ohci_hash_find_td(ohci_softc_t *,
155 Static usbd_status ohci_setup_isoc(usbd_pipe_handle pipe);
156 Static void ohci_device_isoc_enter(usbd_xfer_handle);
158 Static usbd_status ohci_allocm(struct usbd_bus *, usb_dma_t *,
160 Static void ohci_freem(struct usbd_bus *, usb_dma_t *);
162 Static usbd_xfer_handle ohci_allocx(struct usbd_bus *);
163 Static void ohci_freex(struct usbd_bus *, usbd_xfer_handle);
165 Static usbd_status ohci_root_ctrl_transfer(usbd_xfer_handle);
166 Static usbd_status ohci_root_ctrl_start(usbd_xfer_handle);
167 Static void ohci_root_ctrl_abort(usbd_xfer_handle);
168 Static void ohci_root_ctrl_close(usbd_pipe_handle);
170 Static usbd_status ohci_root_intr_transfer(usbd_xfer_handle);
171 Static usbd_status ohci_root_intr_start(usbd_xfer_handle);
172 Static void ohci_root_intr_abort(usbd_xfer_handle);
173 Static void ohci_root_intr_close(usbd_pipe_handle);
174 Static void ohci_root_intr_done (usbd_xfer_handle);
176 Static usbd_status ohci_device_ctrl_transfer(usbd_xfer_handle);
177 Static usbd_status ohci_device_ctrl_start(usbd_xfer_handle);
178 Static void ohci_device_ctrl_abort(usbd_xfer_handle);
179 Static void ohci_device_ctrl_close(usbd_pipe_handle);
180 Static void ohci_device_ctrl_done (usbd_xfer_handle);
182 Static usbd_status ohci_device_bulk_transfer(usbd_xfer_handle);
183 Static usbd_status ohci_device_bulk_start(usbd_xfer_handle);
184 Static void ohci_device_bulk_abort(usbd_xfer_handle);
185 Static void ohci_device_bulk_close(usbd_pipe_handle);
186 Static void ohci_device_bulk_done (usbd_xfer_handle);
188 Static usbd_status ohci_device_intr_transfer(usbd_xfer_handle);
189 Static usbd_status ohci_device_intr_start(usbd_xfer_handle);
190 Static void ohci_device_intr_abort(usbd_xfer_handle);
191 Static void ohci_device_intr_close(usbd_pipe_handle);
192 Static void ohci_device_intr_done (usbd_xfer_handle);
194 Static usbd_status ohci_device_isoc_transfer(usbd_xfer_handle);
195 Static usbd_status ohci_device_isoc_start(usbd_xfer_handle);
196 Static void ohci_device_isoc_abort(usbd_xfer_handle);
197 Static void ohci_device_isoc_close(usbd_pipe_handle);
198 Static void ohci_device_isoc_done (usbd_xfer_handle);
200 Static usbd_status ohci_device_setintr(ohci_softc_t *sc,
201 struct ohci_pipe *pipe, int ival);
203 Static int ohci_str(usb_string_descriptor_t *, int, char *);
205 Static void ohci_timeout(void *);
206 Static void ohci_rhsc_able(ohci_softc_t *, int);
208 Static void ohci_close_pipe(usbd_pipe_handle pipe,
209 ohci_soft_ed_t *head);
210 Static void ohci_abort_xfer(usbd_xfer_handle xfer,
212 Static void ohci_abort_xfer_end(void *);
214 Static void ohci_device_clear_toggle(usbd_pipe_handle pipe);
215 Static void ohci_noop(usbd_pipe_handle pipe);
218 Static void ohci_dumpregs(ohci_softc_t *);
219 Static void ohci_dump_tds(ohci_soft_td_t *);
220 Static void ohci_dump_td(ohci_soft_td_t *);
221 Static void ohci_dump_ed(ohci_soft_ed_t *);
224 #define OWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
225 #define OREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
226 #define OREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
228 /* Reverse the bits in a value 0 .. 31 */
229 Static u_int8_t revbits[OHCI_NO_INTRS] =
230 { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
231 0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
232 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
233 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
236 struct usbd_pipe pipe;
240 ohci_soft_itd_t *itd;
242 /* Info needed for different pipe kinds. */
248 ohci_soft_td_t *setup, *data, *stat;
267 #define OHCI_INTR_ENDPT 1
269 Static struct usbd_bus_methods ohci_bus_methods = {
278 Static struct usbd_pipe_methods ohci_root_ctrl_methods = {
279 ohci_root_ctrl_transfer,
280 ohci_root_ctrl_start,
281 ohci_root_ctrl_abort,
282 ohci_root_ctrl_close,
287 Static struct usbd_pipe_methods ohci_root_intr_methods = {
288 ohci_root_intr_transfer,
289 ohci_root_intr_start,
290 ohci_root_intr_abort,
291 ohci_root_intr_close,
296 Static struct usbd_pipe_methods ohci_device_ctrl_methods = {
297 ohci_device_ctrl_transfer,
298 ohci_device_ctrl_start,
299 ohci_device_ctrl_abort,
300 ohci_device_ctrl_close,
302 ohci_device_ctrl_done,
305 Static struct usbd_pipe_methods ohci_device_intr_methods = {
306 ohci_device_intr_transfer,
307 ohci_device_intr_start,
308 ohci_device_intr_abort,
309 ohci_device_intr_close,
310 ohci_device_clear_toggle,
311 ohci_device_intr_done,
314 Static struct usbd_pipe_methods ohci_device_bulk_methods = {
315 ohci_device_bulk_transfer,
316 ohci_device_bulk_start,
317 ohci_device_bulk_abort,
318 ohci_device_bulk_close,
319 ohci_device_clear_toggle,
320 ohci_device_bulk_done,
323 Static struct usbd_pipe_methods ohci_device_isoc_methods = {
324 ohci_device_isoc_transfer,
325 ohci_device_isoc_start,
326 ohci_device_isoc_abort,
327 ohci_device_isoc_close,
329 ohci_device_isoc_done,
332 #if defined(__NetBSD__) || defined(__OpenBSD__)
334 ohci_activate(device_ptr_t self, enum devact act)
336 struct ohci_softc *sc = (struct ohci_softc *)self;
344 case DVACT_DEACTIVATE:
345 if (sc->sc_child != NULL)
346 rv = config_deactivate(sc->sc_child);
353 ohci_detach(struct ohci_softc *sc, int flags)
357 if (sc->sc_child != NULL)
358 rv = config_detach(sc->sc_child, flags);
363 #if defined(__NetBSD__)
364 powerhook_disestablish(sc->sc_powerhook);
365 shutdownhook_disestablish(sc->sc_shutdownhook);
367 /* free data structures XXX */
374 ohci_alloc_sed(ohci_softc_t *sc)
381 if (sc->sc_freeeds == NULL) {
382 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
383 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
384 OHCI_ED_ALIGN, &dma);
387 for(i = 0; i < OHCI_SED_CHUNK; i++) {
388 offs = i * OHCI_SED_SIZE;
389 sed = (ohci_soft_ed_t *)((char *)KERNADDR(&dma, offs));
390 sed->physaddr = DMAADDR(&dma, offs);
391 sed->next = sc->sc_freeeds;
392 sc->sc_freeeds = sed;
395 sed = sc->sc_freeeds;
396 sc->sc_freeeds = sed->next;
397 memset(&sed->ed, 0, sizeof(ohci_ed_t));
403 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
405 sed->next = sc->sc_freeeds;
406 sc->sc_freeeds = sed;
410 ohci_alloc_std(ohci_softc_t *sc)
418 if (sc->sc_freetds == NULL) {
419 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
420 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
421 OHCI_TD_ALIGN, &dma);
424 for(i = 0; i < OHCI_STD_CHUNK; i++) {
425 offs = i * OHCI_STD_SIZE;
426 std = (ohci_soft_td_t *)((char *)KERNADDR(&dma, offs));
427 std->physaddr = DMAADDR(&dma, offs);
428 std->nexttd = sc->sc_freetds;
429 sc->sc_freetds = std;
432 std = sc->sc_freetds;
433 sc->sc_freetds = std->nexttd;
434 memset(&std->td, 0, sizeof(ohci_td_t));
438 ohci_hash_add_td(sc, std);
445 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
450 ohci_hash_rem_td(sc, std);
453 std->nexttd = sc->sc_freetds;
454 sc->sc_freetds = std;
458 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
459 int len, int rd, u_int16_t flags, usb_dma_t *dma,
460 ohci_soft_td_t *std, ohci_soft_td_t **rstd)
462 ohci_soft_td_t *next, *cur;
463 ohci_physaddr_t dataphys, dataphysend;
464 u_int32_t intr, tdflags;
468 DPRINTFN(len < 4096,("ohci_alloc_std_chain: start len=%d\n", len));
472 dataphysend = OHCI_PAGE(DMAADDR(dma, len - 1));
474 (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
475 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY |
476 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0);
479 next = ohci_alloc_std(sc);
483 dataphys = DMAADDR(dma, offset);
485 /* The OHCI hardware can handle at most one page crossing. */
486 #if defined(__NetBSD__) || defined(__OpenBSD__)
487 if (OHCI_PAGE(dataphys) == dataphysend ||
488 OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == dataphysend)
489 #elif defined(__FreeBSD__)
490 /* XXX This is pretty broken: Because we do not allocate
491 * a contiguous buffer (contiguous in physical pages) we
492 * can only transfer one page in one go.
493 * So check whether the start and end of the buffer are on
496 if (OHCI_PAGE(dataphys) == dataphysend)
499 /* we can handle it in this TD */
502 /* XXX The calculation below is wrong and could
503 * result in a packet that is not a multiple of the
504 * MaxPacketSize in the case where the buffer does not
505 * start on an appropriate address (like for example in
506 * the case of an mbuf cluster). You'll get an early
509 #if defined(__NetBSD__) || defined(__OpenBSD__)
510 /* must use multiple TDs, fill as much as possible. */
511 curlen = 2 * OHCI_PAGE_SIZE -
512 OHCI_PAGE_MASK(dataphys);
513 if (curlen > len) /* may have fit in one page */
515 #elif defined(__FreeBSD__)
516 /* See comment above (XXX) */
517 curlen = OHCI_PAGE_SIZE -
518 OHCI_PAGE_MASK(dataphys);
521 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
522 "dataphysend=0x%08x len=%d curlen=%d\n",
523 dataphys, dataphysend,
527 intr = len == 0 ? OHCI_TD_SET_DI(1) : OHCI_TD_NOINTR;
528 cur->td.td_flags = LE(tdflags | intr);
529 cur->td.td_cbp = LE(dataphys);
531 cur->td.td_nexttd = LE(next->physaddr);
532 cur->td.td_be = LE(dataphys + curlen - 1);
534 cur->flags = OHCI_ADD_LEN;
535 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
536 dataphys, dataphys + curlen - 1));
539 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
543 if ((flags & USBD_FORCE_SHORT_XFER) &&
544 len % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
545 /* Force a 0 length transfer at the end. */
547 cur->td.td_flags = LE(tdflags | OHCI_TD_NOINTR);
550 next = ohci_alloc_std(sc);
554 cur->td.td_flags = LE(tdflags | OHCI_TD_SET_DI(1));
555 cur->td.td_cbp = 0; /* indicate 0 length packet */
557 cur->td.td_nexttd = LE(next->physaddr);
561 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
563 cur->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
566 return (USBD_NORMAL_COMPLETION);
575 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
576 ohci_soft_td_t *stdend)
580 for (; std != stdend; std = p) {
582 ohci_free_std(sc, std);
588 ohci_alloc_sitd(ohci_softc_t *sc)
590 ohci_soft_itd_t *sitd;
595 if (sc->sc_freeitds == NULL) {
596 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
597 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
598 OHCI_TD_ALIGN, &dma);
601 for(i = 0; i < OHCI_STD_CHUNK; i++) {
602 offs = i * OHCI_STD_SIZE;
603 sitd = (ohci_soft_itd_t *)((char*)KERNADDR(&dma, offs));
604 sitd->physaddr = DMAADDR(&dma, offs);
605 sitd->nextitd = sc->sc_freeitds;
606 sc->sc_freeitds = sitd;
609 sitd = sc->sc_freeitds;
610 sc->sc_freeitds = sitd->nextitd;
611 memset(&sitd->itd, 0, sizeof(ohci_itd_t));
617 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
619 sitd->nextitd = sc->sc_freeitds;
620 sc->sc_freeitds = sitd;
624 ohci_init(ohci_softc_t *sc)
626 ohci_soft_ed_t *sed, *psed;
629 u_int32_t s, ctl, ival, hcr, fm, per, rev;
631 DPRINTF(("ohci_init: start\n"));
632 #if defined(__OpenBSD__)
635 printf("%s:", USBDEVNAME(sc->sc_bus.bdev));
637 rev = OREAD4(sc, OHCI_REVISION);
638 printf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
639 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
641 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
642 printf("%s: unsupported OHCI revision\n",
643 USBDEVNAME(sc->sc_bus.bdev));
644 sc->sc_bus.usbrev = USBREV_UNKNOWN;
647 sc->sc_bus.usbrev = USBREV_1_0;
649 for (i = 0; i < OHCI_HASH_SIZE; i++)
650 LIST_INIT(&sc->sc_hash_tds[i]);
652 SIMPLEQ_INIT(&sc->sc_free_xfers);
654 /* Allocate the HCCA area. */
655 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
656 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
659 sc->sc_hcca = (struct ohci_hcca *)KERNADDR(&sc->sc_hccadma, 0);
660 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
662 sc->sc_eintrs = OHCI_NORMAL_INTRS;
664 /* Allocate dummy ED that starts the control list. */
665 sc->sc_ctrl_head = ohci_alloc_sed(sc);
666 if (sc->sc_ctrl_head == NULL) {
670 sc->sc_ctrl_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
672 /* Allocate dummy ED that starts the bulk list. */
673 sc->sc_bulk_head = ohci_alloc_sed(sc);
674 if (sc->sc_bulk_head == NULL) {
678 sc->sc_bulk_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
680 /* Allocate dummy ED that starts the isochronous list. */
681 sc->sc_isoc_head = ohci_alloc_sed(sc);
682 if (sc->sc_isoc_head == NULL) {
686 sc->sc_isoc_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
688 /* Allocate all the dummy EDs that make up the interrupt tree. */
689 for (i = 0; i < OHCI_NO_EDS; i++) {
690 sed = ohci_alloc_sed(sc);
693 ohci_free_sed(sc, sc->sc_eds[i]);
697 /* All ED fields are set to 0. */
699 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
701 psed = sc->sc_eds[(i-1) / 2];
703 psed= sc->sc_isoc_head;
705 sed->ed.ed_nexted = LE(psed->physaddr);
708 * Fill HCCA interrupt table. The bit reversal is to get
709 * the tree set up properly to spread the interrupts.
711 for (i = 0; i < OHCI_NO_INTRS; i++)
712 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
713 LE(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
715 /* Determine in what context we are running. */
716 ctl = OREAD4(sc, OHCI_CONTROL);
718 /* SMM active, request change */
719 DPRINTF(("ohci_init: SMM active, request owner change\n"));
720 s = OREAD4(sc, OHCI_COMMAND_STATUS);
721 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
722 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
723 usb_delay_ms(&sc->sc_bus, 1);
724 ctl = OREAD4(sc, OHCI_CONTROL);
726 if ((ctl & OHCI_IR) == 0) {
727 printf("%s: SMM does not respond, resetting\n",
728 USBDEVNAME(sc->sc_bus.bdev));
729 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
732 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
733 /* BIOS started controller. */
734 DPRINTF(("ohci_init: BIOS active\n"));
735 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
736 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
737 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
740 DPRINTF(("ohci_init: cold started\n"));
742 /* Controller was cold started. */
743 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
747 * This reset should not be necessary according to the OHCI spec, but
748 * without it some controllers do not start.
750 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
751 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
752 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
754 /* We now own the host controller and the bus has been reset. */
755 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
757 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
758 /* Nominal time for a reset is 10 us. */
759 for (i = 0; i < 10; i++) {
761 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
766 printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
775 /* The controller is now in SUSPEND state, we have 2ms to finish. */
777 /* Set up HC registers. */
778 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
779 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
780 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
781 /* disable all interrupts and then switch on all desired interrupts */
782 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
783 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
784 /* switch on desired functional features */
785 ctl = OREAD4(sc, OHCI_CONTROL);
786 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
787 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
788 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
789 /* And finally start it! */
790 OWRITE4(sc, OHCI_CONTROL, ctl);
793 * The controller is now OPERATIONAL. Set a some final
794 * registers that should be set earlier, but that the
795 * controller ignores when in the SUSPEND state.
797 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
798 fm |= OHCI_FSMPS(ival) | ival;
799 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
800 per = OHCI_PERIODIC(ival); /* 90% periodic */
801 OWRITE4(sc, OHCI_PERIODIC_START, per);
803 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
805 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
812 /* Set up the bus struct. */
813 sc->sc_bus.methods = &ohci_bus_methods;
814 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
816 #if defined(__NetBSD__)
817 sc->sc_powerhook = powerhook_establish(ohci_power, sc);
818 sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc);
821 return (USBD_NORMAL_COMPLETION);
824 for (i = 0; i < OHCI_NO_EDS; i++)
825 ohci_free_sed(sc, sc->sc_eds[i]);
827 ohci_free_sed(sc, sc->sc_isoc_head);
829 ohci_free_sed(sc, sc->sc_ctrl_head);
831 ohci_free_sed(sc, sc->sc_bulk_head);
833 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
838 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
840 #if defined(__NetBSD__) || defined(__OpenBSD__)
841 struct ohci_softc *sc = (struct ohci_softc *)bus;
844 return (usb_allocmem(&sc->sc_bus, size, 0, dma));
848 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
850 #if defined(__NetBSD__) || defined(__OpenBSD__)
851 struct ohci_softc *sc = (struct ohci_softc *)bus;
854 usb_freemem(&sc->sc_bus, dma);
858 ohci_allocx(struct usbd_bus *bus)
860 struct ohci_softc *sc = (struct ohci_softc *)bus;
861 usbd_xfer_handle xfer;
863 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
865 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
867 xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
869 memset(xfer, 0, sizeof *xfer);
874 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
876 struct ohci_softc *sc = (struct ohci_softc *)bus;
878 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
882 * Shut down the controller when the system is going down.
884 #if defined(__NetBSD__) || defined(__OpenBSD__)
886 ohci_shutdown(void *v)
888 ohci_softc_t *sc = v;
890 DPRINTF(("ohci_shutdown: stopping the HC\n"));
891 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
895 * Handle suspend/resume.
897 * We need to switch to polling mode here, because this routine is
898 * called from an intterupt context. This is all right since we
899 * are almost suspended anyway.
902 ohci_power(int why, void *v)
905 ohci_softc_t *sc = v;
907 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
908 /* XXX should suspend/resume */
916 ohci_dumpregs(ohci_softc_t *sc)
918 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
919 OREAD4(sc, OHCI_REVISION),
920 OREAD4(sc, OHCI_CONTROL),
921 OREAD4(sc, OHCI_COMMAND_STATUS)));
922 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
923 OREAD4(sc, OHCI_INTERRUPT_STATUS),
924 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
925 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
926 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
927 OREAD4(sc, OHCI_HCCA),
928 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
929 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
930 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
931 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
932 OREAD4(sc, OHCI_BULK_HEAD_ED),
933 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
934 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
935 OREAD4(sc, OHCI_DONE_HEAD),
936 OREAD4(sc, OHCI_FM_INTERVAL),
937 OREAD4(sc, OHCI_FM_REMAINING)));
938 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
939 OREAD4(sc, OHCI_FM_NUMBER),
940 OREAD4(sc, OHCI_PERIODIC_START),
941 OREAD4(sc, OHCI_LS_THRESHOLD)));
942 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n",
943 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
944 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
945 OREAD4(sc, OHCI_RH_STATUS)));
946 DPRINTF((" port1=0x%08x port2=0x%08x\n",
947 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
948 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
949 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n",
950 LE(sc->sc_hcca->hcca_frame_number),
951 LE(sc->sc_hcca->hcca_done_head)));
955 Static int ohci_intr1(ohci_softc_t *);
960 ohci_softc_t *sc = p;
962 /* If we get an interrupt while polling, then just ignore it. */
963 if (sc->sc_bus.use_polling) {
965 printf("ohci_intr: ignored interrupt while polling\n");
970 return (ohci_intr1(sc));
974 ohci_intr1(ohci_softc_t *sc)
976 u_int32_t intrs, eintrs;
977 ohci_physaddr_t done;
979 /* In case the interrupt occurs before initialization has completed. */
980 if (sc == NULL || sc->sc_hcca == NULL) {
982 printf("ohci_intr: sc->sc_hcca == NULL\n");
988 done = LE(sc->sc_hcca->hcca_done_head);
990 /* The LSb of done is used to inform the HC Driver that an interrupt
991 * condition exists for both the Done list and for another event
992 * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
993 * Driver checks the HccaDoneHead Value. If this value is 0, then the
994 * interrupt was caused by other than the HccaDoneHead update and the
995 * HcInterruptStatus register needs to be accessed to determine that
996 * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
997 * update interrupt is indicated and if the LSb of done is nonzero,
998 * then an additional interrupt event is indicated and
999 * HcInterruptStatus should be checked to determine its cause.
1002 sc->sc_hcca->hcca_done_head = 0;
1003 if (done & ~OHCI_DONE_INTRS)
1005 if (done & OHCI_DONE_INTRS) {
1006 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1007 done &= ~OHCI_DONE_INTRS;
1010 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1014 /* nothing to be done ?! */
1018 intrs &= ~OHCI_MIE; /* mask out Master Interrupt Enable */
1020 /* Acknowledge any interrupts that have happened */
1021 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs);
1023 /* Any interrupts we had enabled? */
1024 eintrs = intrs & sc->sc_eintrs;
1028 sc->sc_bus.intr_context++;
1029 sc->sc_bus.no_intrs++;
1030 DPRINTFN(7, ("ohci_intr: sc=%p intrs=%x(%x) eintr=%x\n",
1031 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1034 if (eintrs & OHCI_SO) {
1035 printf("%s: scheduling overrun\n",USBDEVNAME(sc->sc_bus.bdev));
1039 if (eintrs & OHCI_WDH) {
1040 ohci_process_done(sc, done);
1043 if (eintrs & OHCI_RD) {
1044 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1045 /* XXX process resume detect */
1047 if (eintrs & OHCI_UE) {
1048 printf("%s: unrecoverable error, controller halted\n",
1049 USBDEVNAME(sc->sc_bus.bdev));
1050 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1053 if (eintrs & OHCI_RHSC) {
1054 ohci_rhsc(sc, sc->sc_intrxfer);
1055 intrs &= ~OHCI_RHSC;
1058 * Disable RHSC interrupt for now, because it will be
1059 * on until the port has been reset.
1061 ohci_rhsc_able(sc, 0);
1064 sc->sc_bus.intr_context--;
1066 /* Block unprocessed interrupts. XXX */
1067 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, intrs);
1068 sc->sc_eintrs &= ~intrs;
1074 ohci_rhsc_able(ohci_softc_t *sc, int on)
1076 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1078 sc->sc_eintrs |= OHCI_RHSC;
1079 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1081 sc->sc_eintrs &= ~OHCI_RHSC;
1082 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1087 char *ohci_cc_strs[] = {
1091 "DATA_TOGGLE_MISMATCH",
1093 "DEVICE_NOT_RESPONDING",
1094 "PID_CHECK_FAILURE",
1108 ohci_process_done(ohci_softc_t *sc, ohci_physaddr_t done)
1110 ohci_soft_td_t *std, *sdone, *stdnext;
1111 usbd_xfer_handle xfer;
1114 DPRINTFN(10,("ohci_process_done: done=0x%08lx\n", (u_long)done));
1116 /* Reverse the done list and store the reversed list in sdone */
1118 for (; done; done = LE(std->td.td_nexttd)) {
1119 std = ohci_hash_find_td(sc, done & LE(OHCI_TAILMASK));
1122 DPRINTF(("%s: Invalid done queue 0x%08x",
1123 USBDEVNAME(sc->sc_bus.bdev), done));
1126 /* XXX Should we compare the list of active TDs with
1127 * the list of TDs queued at EDs to handle the ones that
1128 * are not listed on any of the ED queues and therefore
1139 if (ohcidebug > 10) {
1140 DPRINTF(("ohci_process_done: TD done:\n"));
1141 for (std = sdone; std; std = std->dnext)
1142 ohci_dump_td(sdone);
1146 for (std = sdone; std; std = stdnext) {
1148 stdnext = std->dnext;
1149 DPRINTFN(5, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1150 std, xfer, (xfer? xfer->hcpriv:NULL)));
1151 if (xfer == NULL || (std->flags & OHCI_TD_HANDLED)) {
1152 /* xfer == NULL: There seems to be no xfer associated
1153 * with this TD. It is tailp that happened to end up on
1155 * flags & OHCI_TD_HANDLED: The TD has already been
1156 * handled by process_done and should not be done again.
1160 cc = OHCI_TD_GET_CC(LE(std->td.td_flags));
1161 usb_untimeout(ohci_timeout, xfer, xfer->timo_handle);
1162 if (xfer->status == USBD_CANCELLED ||
1163 xfer->status == USBD_TIMEOUT) {
1164 DPRINTF(("ohci_process_done: cancel/timeout, xfer=%p\n",
1166 /* Handled by abort routine. */
1167 } else if (cc == OHCI_CC_NO_ERROR) {
1168 DPRINTFN(15, ("ohci_process_done: no error, xfer=%p\n",
1171 if (std->td.td_cbp != 0)
1172 len -= LE(std->td.td_be) -
1173 LE(std->td.td_cbp) + 1;
1174 if (std->flags & OHCI_ADD_LEN)
1175 xfer->actlen += len;
1176 if (std->flags & OHCI_CALL_DONE) {
1177 xfer->status = USBD_NORMAL_COMPLETION;
1178 usb_transfer_complete(xfer);
1180 ohci_free_std(sc, std);
1183 * Endpoint is halted. First unlink all the TDs
1184 * belonging to the failed transfer, and then restart
1187 ohci_soft_td_t *p, *n;
1188 struct ohci_pipe *opipe =
1189 (struct ohci_pipe *)xfer->pipe;
1191 DPRINTF(("ohci_process_done: err cc=%d (%s), xfer=%p\n",
1192 OHCI_TD_GET_CC(LE(std->td.td_flags)),
1193 ohci_cc_strs[OHCI_TD_GET_CC(LE(std->td.td_flags))],
1196 /* Mark all the TDs in the done queue for the current
1199 for (p = stdnext; p; p = p->dnext) {
1200 if (p->xfer == xfer)
1201 p->flags |= OHCI_TD_HANDLED;
1204 /* remove TDs for the current xfer from the ED */
1205 for (p = std; p->xfer == xfer; p = n) {
1207 ohci_free_std(sc, p);
1209 opipe->sed->ed.ed_headp = LE(p->physaddr);
1211 /* XXX why is this being done? Why not OHCI_BLF too */
1212 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1214 if (cc == OHCI_CC_STALL)
1215 xfer->status = USBD_STALLED;
1217 xfer->status = USBD_IOERROR;
1219 usb_transfer_complete(xfer);
1225 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1227 DPRINTFN(10,("ohci_ctrl_done: xfer=%p\n", xfer));
1230 if (!(xfer->rqflags & URQ_REQUEST)) {
1231 panic("ohci_ctrl_done: not a request\n");
1234 xfer->hcpriv = NULL;
1238 ohci_device_intr_done(usbd_xfer_handle xfer)
1240 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1241 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1242 ohci_soft_ed_t *sed = opipe->sed;
1243 ohci_soft_td_t *data, *tail;
1246 DPRINTFN(10,("ohci_intr_done: xfer=%p, actlen=%d\n",
1247 xfer, xfer->actlen));
1249 xfer->hcpriv = NULL;
1251 if (xfer->pipe->repeat) {
1252 data = opipe->tail.td;
1253 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1255 xfer->status = USBD_NOMEM;
1260 data->td.td_flags = LE(
1261 OHCI_TD_IN | OHCI_TD_NOCC |
1262 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1263 if (xfer->flags & USBD_SHORT_XFER_OK)
1264 data->td.td_flags |= LE(OHCI_TD_R);
1265 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
1266 data->nexttd = tail;
1267 data->td.td_nexttd = LE(tail->physaddr);
1268 data->td.td_be = LE(LE(data->td.td_cbp) + xfer->length - 1);
1269 data->len = xfer->length;
1271 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1272 xfer->hcpriv = data;
1275 ohci_hash_add_td(sc, data);
1276 sed->ed.ed_tailp = LE(tail->physaddr);
1277 opipe->tail.td = tail;
1282 ohci_device_bulk_done(usbd_xfer_handle xfer)
1284 DPRINTFN(10,("ohci_bulk_done: xfer=%p, actlen=%d\n",
1285 xfer, xfer->actlen));
1287 xfer->hcpriv = NULL;
1291 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1293 usbd_pipe_handle pipe;
1294 struct ohci_pipe *opipe;
1299 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1300 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1301 sc, xfer, hstatus));
1304 /* Just ignore the change. */
1309 opipe = (struct ohci_pipe *)pipe;
1311 p = KERNADDR(&xfer->dmabuf, 0);
1312 m = min(sc->sc_noport, xfer->length * 8 - 1);
1313 memset(p, 0, xfer->length);
1314 for (i = 1; i <= m; i++) {
1315 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1316 p[i/8] |= 1 << (i%8);
1318 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1319 xfer->actlen = xfer->length;
1320 xfer->status = USBD_NORMAL_COMPLETION;
1322 usb_transfer_complete(xfer);
1326 ohci_root_intr_done(usbd_xfer_handle xfer)
1328 xfer->hcpriv = NULL;
1332 * Wait here until controller claims to have an interrupt.
1333 * Then call ohci_intr and return. Use timeout to avoid waiting
1337 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1339 int timo = xfer->timeout;
1343 xfer->status = USBD_IN_PROGRESS;
1344 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1345 usb_delay_ms(&sc->sc_bus, 1);
1346 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1347 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1354 if (xfer->status != USBD_IN_PROGRESS)
1360 DPRINTF(("ohci_waitintr: timeout\n"));
1364 xfer->status = USBD_TIMEOUT;
1365 usb_transfer_complete(xfer);
1366 /* XXX should free TD */
1370 ohci_poll(struct usbd_bus *bus)
1372 ohci_softc_t *sc = (ohci_softc_t *)bus;
1374 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1379 ohci_device_request(usbd_xfer_handle xfer)
1381 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1382 usb_device_request_t *req = &xfer->request;
1383 usbd_device_handle dev = opipe->pipe.device;
1384 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1385 int addr = dev->address;
1386 ohci_soft_td_t *setup, *data = 0, *stat, *next, *tail;
1387 ohci_soft_ed_t *sed;
1393 isread = req->bmRequestType & UT_READ;
1394 len = UGETW(req->wLength);
1396 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1397 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1398 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1399 UGETW(req->wIndex), len, addr,
1400 opipe->pipe.endpoint->edesc->bEndpointAddress));
1402 setup = opipe->tail.td;
1403 stat = ohci_alloc_std(sc);
1408 tail = ohci_alloc_std(sc);
1416 opipe->u.ctl.length = len;
1418 /* Update device address and length since they may have changed. */
1419 /* XXX This only needs to be done once, but it's too early in open. */
1420 sed->ed.ed_flags = LE(
1421 (LE(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1422 OHCI_ED_SET_FA(addr) |
1423 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1425 /* Set up data transaction */
1427 data = ohci_alloc_std(sc);
1432 data->td.td_flags = LE(
1433 (isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
1434 OHCI_TD_TOGGLE_1 | OHCI_TD_NOINTR |
1435 (xfer->flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0));
1436 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
1437 data->nexttd = stat;
1438 data->td.td_nexttd = LE(stat->physaddr);
1439 data->td.td_be = LE(LE(data->td.td_cbp) + len - 1);
1442 data->flags = OHCI_ADD_LEN;
1445 stat->flags = OHCI_CALL_DONE;
1449 stat->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1452 memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1454 setup->td.td_flags = LE(OHCI_TD_SETUP | OHCI_TD_NOCC |
1455 OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1456 setup->td.td_cbp = LE(DMAADDR(&opipe->u.ctl.reqdma, 0));
1457 setup->nexttd = next;
1458 setup->td.td_nexttd = LE(next->physaddr);
1459 setup->td.td_be = LE(LE(setup->td.td_cbp) + sizeof *req - 1);
1460 setup->len = 0; /* XXX The number of byte we count */
1463 xfer->hcpriv = setup;
1465 stat->td.td_flags = LE(
1466 (isread ? OHCI_TD_OUT : OHCI_TD_IN) | OHCI_TD_NOCC |
1467 OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1468 stat->td.td_cbp = 0;
1469 stat->nexttd = tail;
1470 stat->td.td_nexttd = LE(tail->physaddr);
1476 if (ohcidebug > 5) {
1477 DPRINTF(("ohci_device_request:\n"));
1479 ohci_dump_tds(setup);
1483 /* Insert ED in schedule */
1485 sed->ed.ed_tailp = LE(tail->physaddr);
1486 opipe->tail.td = tail;
1487 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1488 if (xfer->timeout && !sc->sc_bus.use_polling) {
1489 usb_timeout(ohci_timeout, xfer,
1490 MS_TO_TICKS(xfer->timeout), xfer->timo_handle);
1495 if (ohcidebug > 25) {
1496 usb_delay_ms(&sc->sc_bus, 5);
1497 DPRINTF(("ohci_device_request: status=%x\n",
1498 OREAD4(sc, OHCI_COMMAND_STATUS)));
1500 ohci_dump_tds(setup);
1504 return (USBD_NORMAL_COMPLETION);
1507 ohci_free_std(sc, tail);
1509 ohci_free_std(sc, stat);
1515 * Add an ED to the schedule. Called at splusb().
1518 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1521 sed->next = head->next;
1522 sed->ed.ed_nexted = head->ed.ed_nexted;
1524 head->ed.ed_nexted = LE(sed->physaddr);
1528 * Remove an ED from the schedule. Called at splusb().
1531 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1538 for (p = head; p == NULL && p->next != sed; p = p->next)
1541 panic("ohci_rem_ed: ED not found\n");
1542 p->next = sed->next;
1543 p->ed.ed_nexted = sed->ed.ed_nexted;
1547 * When a transfer is completed the TD is added to the done queue by
1548 * the host controller. This queue is the processed by software.
1549 * Unfortunately the queue contains the physical address of the TD
1550 * and we have no simple way to translate this back to a kernel address.
1551 * To make the translation possible (and fast) we use a hash table of
1552 * TDs currently in the schedule. The physical address is used as the
1556 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1557 /* Called at splusb() */
1559 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1561 int h = HASH(std->physaddr);
1565 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1568 /* Called at splusb() */
1570 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1574 LIST_REMOVE(std, hnext);
1578 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1581 ohci_soft_td_t *std;
1583 /* if these are present they should be masked out at an earlier
1586 KASSERT((a&~OHCI_TAILMASK) == 0, ("%s: 0x%b has lower bits set\n",
1587 USBDEVNAME(sc->sc_bus.bdev),
1588 (int) a, "\20\1HALT\2TOGGLE"));
1590 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1592 std = LIST_NEXT(std, hnext))
1593 if (std->physaddr == a)
1596 DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1597 USBDEVNAME(sc->sc_bus.bdev), (u_long) a));
1602 ohci_timeout(void *addr)
1604 usbd_xfer_handle xfer = addr;
1607 DPRINTF(("ohci_timeout: xfer=%p\n", xfer));
1610 xfer->device->bus->intr_context++;
1611 ohci_abort_xfer(xfer, USBD_TIMEOUT);
1612 xfer->device->bus->intr_context--;
1618 ohci_dump_tds(ohci_soft_td_t *std)
1620 for (; std; std = std->nexttd)
1625 ohci_dump_td(ohci_soft_td_t *std)
1627 DPRINTF(("TD(%p) at %08lx: %b delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1628 "nexttd=0x%08lx be=0x%08lx\n",
1629 std, (u_long)std->physaddr,
1630 (int)LE(std->td.td_flags),
1631 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1632 OHCI_TD_GET_DI(LE(std->td.td_flags)),
1633 OHCI_TD_GET_EC(LE(std->td.td_flags)),
1634 OHCI_TD_GET_CC(LE(std->td.td_flags)),
1635 (u_long)LE(std->td.td_cbp),
1636 (u_long)LE(std->td.td_nexttd), (u_long)LE(std->td.td_be)));
1640 ohci_dump_ed(ohci_soft_ed_t *sed)
1642 DPRINTF(("ED(%p) at %08lx: addr=%d endpt=%d maxp=%d %b\n"
1643 "tailp=0x%8b headp=0x%8b nexted=0x%08lx\n",
1644 sed, (u_long)sed->physaddr,
1645 OHCI_ED_GET_FA(LE(sed->ed.ed_flags)),
1646 OHCI_ED_GET_EN(LE(sed->ed.ed_flags)),
1647 OHCI_ED_GET_MAXP(LE(sed->ed.ed_flags)),
1648 (int)LE(sed->ed.ed_flags),
1649 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
1650 (int)(uintptr_t)LE(sed->ed.ed_tailp),
1652 (int)(uintptr_t)LE(sed->ed.ed_headp),
1654 (u_long)LE(sed->ed.ed_nexted)));
1659 ohci_open(usbd_pipe_handle pipe)
1661 usbd_device_handle dev = pipe->device;
1662 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1663 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1664 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1665 u_int8_t addr = dev->address;
1666 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1667 ohci_soft_ed_t *sed;
1668 ohci_soft_td_t *std = NULL;
1669 ohci_soft_itd_t *sitd;
1670 ohci_physaddr_t tdphys;
1676 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1677 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1678 if (addr == sc->sc_addr) {
1679 switch (ed->bEndpointAddress) {
1680 case USB_CONTROL_ENDPOINT:
1681 pipe->methods = &ohci_root_ctrl_methods;
1683 case UE_DIR_IN | OHCI_INTR_ENDPT:
1684 pipe->methods = &ohci_root_intr_methods;
1687 return (USBD_INVAL);
1690 sed = ohci_alloc_sed(sc);
1694 if (xfertype == UE_ISOCHRONOUS) {
1695 sitd = ohci_alloc_sitd(sc);
1697 ohci_free_sitd(sc, sitd);
1700 opipe->tail.itd = sitd;
1701 tdphys = LE(sitd->physaddr);
1702 fmt = OHCI_ED_FORMAT_ISO;
1704 std = ohci_alloc_std(sc);
1706 ohci_free_std(sc, std);
1709 opipe->tail.td = std;
1710 tdphys = LE(std->physaddr);
1711 fmt = OHCI_ED_FORMAT_GEN;
1713 sed->ed.ed_flags = LE(
1714 OHCI_ED_SET_FA(addr) |
1715 OHCI_ED_SET_EN(ed->bEndpointAddress) |
1717 (dev->lowspeed ? OHCI_ED_SPEED : 0) | fmt |
1718 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
1719 sed->ed.ed_headp = sed->ed.ed_tailp = tdphys;
1723 pipe->methods = &ohci_device_ctrl_methods;
1724 err = usb_allocmem(&sc->sc_bus,
1725 sizeof(usb_device_request_t),
1726 0, &opipe->u.ctl.reqdma);
1730 ohci_add_ed(sed, sc->sc_ctrl_head);
1734 pipe->methods = &ohci_device_intr_methods;
1735 ival = pipe->interval;
1736 if (ival == USBD_DEFAULT_INTERVAL)
1737 ival = ed->bInterval;
1738 return (ohci_device_setintr(sc, opipe, ival));
1739 case UE_ISOCHRONOUS:
1740 pipe->methods = &ohci_device_isoc_methods;
1741 return (ohci_setup_isoc(pipe));
1743 pipe->methods = &ohci_device_bulk_methods;
1745 ohci_add_ed(sed, sc->sc_bulk_head);
1750 return (USBD_NORMAL_COMPLETION);
1753 ohci_free_std(sc, std);
1755 ohci_free_sed(sc, sed);
1757 return (USBD_NOMEM);
1762 * Close a reqular pipe.
1763 * Assumes that there are no pending transactions.
1766 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
1768 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1769 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
1770 ohci_soft_ed_t *sed = opipe->sed;
1775 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
1776 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
1777 != (sed->ed.ed_headp & LE(OHCI_HEADMASK))) {
1778 ohci_physaddr_t td = sed->ed.ed_headp;
1779 ohci_soft_td_t *std;
1780 for (std = LIST_FIRST(&sc->sc_hash_tds[HASH(td)]);
1782 std = LIST_NEXT(std, hnext))
1783 if (std->physaddr == td)
1785 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
1786 "tl=0x%x pipe=%p, std=%p\n", sed,
1787 (int)LE(sed->ed.ed_headp), (int)LE(sed->ed.ed_tailp),
1789 usb_delay_ms(&sc->sc_bus, 2);
1790 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
1791 != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
1792 printf("ohci_close_pipe: pipe still not empty\n");
1795 ohci_rem_ed(sed, head);
1797 ohci_free_sed(sc, opipe->sed);
1801 * Abort a device request.
1802 * If this routine is called at splusb() it guarantees that the request
1803 * will be removed from the hardware scheduling and that the callback
1804 * for it will be called with USBD_CANCELLED status.
1805 * It's impossible to guarantee that the requested transfer will not
1806 * have happened since the hardware runs concurrently.
1807 * If the transaction has already happened we rely on the ordinary
1808 * interrupt processing to process it.
1811 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1813 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1814 ohci_soft_ed_t *sed;
1816 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p\n", xfer, opipe));
1818 xfer->status = status;
1820 usb_untimeout(ohci_timeout, xfer, xfer->timo_handle);
1823 sed->ed.ed_flags |= LE(OHCI_ED_SKIP); /* force hardware skip */
1825 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
1830 if (xfer->device->bus->intr_context) {
1831 /* We have no process context, so we can't use tsleep(). */
1832 timeout(ohci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
1834 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
1835 KASSERT(intr_nesting_level == 0,
1836 ("ohci_abort_req in interrupt context"));
1838 usb_delay_ms(opipe->pipe.device->bus, 1);
1839 ohci_abort_xfer_end(xfer);
1843 ohci_abort_xfer_end(xfer);
1848 ohci_abort_xfer_end(void *v)
1850 usbd_xfer_handle xfer = v;
1851 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1852 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1853 ohci_soft_ed_t *sed;
1854 ohci_soft_td_t *p, *n;
1862 printf("ohci_abort_xfer: hcpriv==0\n");
1867 for (; p->xfer == xfer; p = n) {
1869 ohci_free_std(sc, p);
1873 DPRINTFN(2,("ohci_abort_xfer: set hd=%x, tl=%x\n",
1874 (int)LE(p->physaddr), (int)LE(sed->ed.ed_tailp)));
1875 sed->ed.ed_headp = p->physaddr; /* unlink TDs */
1876 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP); /* remove hardware skip */
1878 usb_transfer_complete(xfer);
1884 * Data structures and routines to emulate the root hub.
1886 Static usb_device_descriptor_t ohci_devd = {
1887 USB_DEVICE_DESCRIPTOR_SIZE,
1888 UDESC_DEVICE, /* type */
1889 {0x00, 0x01}, /* USB version */
1890 UDCLASS_HUB, /* class */
1891 UDSUBCLASS_HUB, /* subclass */
1892 UDPROTO_FSHUB, /* protocol */
1893 64, /* max packet */
1894 {0},{0},{0x00,0x01}, /* device id */
1895 1,2,0, /* string indicies */
1896 1 /* # of configurations */
1899 Static usb_config_descriptor_t ohci_confd = {
1900 USB_CONFIG_DESCRIPTOR_SIZE,
1902 {USB_CONFIG_DESCRIPTOR_SIZE +
1903 USB_INTERFACE_DESCRIPTOR_SIZE +
1904 USB_ENDPOINT_DESCRIPTOR_SIZE},
1912 Static usb_interface_descriptor_t ohci_ifcd = {
1913 USB_INTERFACE_DESCRIPTOR_SIZE,
1924 Static usb_endpoint_descriptor_t ohci_endpd = {
1925 USB_ENDPOINT_DESCRIPTOR_SIZE,
1927 UE_DIR_IN | OHCI_INTR_ENDPT,
1929 {8, 0}, /* max packet */
1933 Static usb_hub_descriptor_t ohci_hubd = {
1934 USB_HUB_DESCRIPTOR_SIZE,
1944 ohci_str(usb_string_descriptor_t *p, int l, char *s)
1950 p->bLength = 2 * strlen(s) + 2;
1953 p->bDescriptorType = UDESC_STRING;
1955 for (i = 0; s[i] && l > 1; i++, l -= 2)
1956 USETW2(p->bString[i], 0, s[i]);
1961 * Simulate a hardware hub by handling all the necessary requests.
1964 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
1968 /* Insert last in queue. */
1969 err = usb_insert_transfer(xfer);
1973 /* Pipe isn't running, start first */
1974 return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1978 ohci_root_ctrl_start(usbd_xfer_handle xfer)
1980 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
1981 usb_device_request_t *req;
1984 int s, len, value, index, l, totlen = 0;
1985 usb_port_status_t ps;
1986 usb_hub_descriptor_t hubd;
1991 if (!(xfer->rqflags & URQ_REQUEST))
1993 return (USBD_INVAL);
1995 req = &xfer->request;
1997 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
1998 req->bmRequestType, req->bRequest));
2000 len = UGETW(req->wLength);
2001 value = UGETW(req->wValue);
2002 index = UGETW(req->wIndex);
2005 buf = KERNADDR(&xfer->dmabuf, 0);
2007 #define C(x,y) ((x) | ((y) << 8))
2008 switch(C(req->bRequest, req->bmRequestType)) {
2009 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2010 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2011 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2013 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2014 * for the integrated root hub.
2017 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2019 *(u_int8_t *)buf = sc->sc_conf;
2023 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2024 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2025 switch(value >> 8) {
2027 if ((value & 0xff) != 0) {
2031 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2032 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2033 memcpy(buf, &ohci_devd, l);
2036 if ((value & 0xff) != 0) {
2040 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2041 memcpy(buf, &ohci_confd, l);
2042 buf = (char *)buf + l;
2044 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2046 memcpy(buf, &ohci_ifcd, l);
2047 buf = (char *)buf + l;
2049 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2051 memcpy(buf, &ohci_endpd, l);
2056 *(u_int8_t *)buf = 0;
2058 switch (value & 0xff) {
2059 case 1: /* Vendor */
2060 totlen = ohci_str(buf, len, sc->sc_vendor);
2062 case 2: /* Product */
2063 totlen = ohci_str(buf, len, "OHCI root hub");
2072 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2074 *(u_int8_t *)buf = 0;
2078 case C(UR_GET_STATUS, UT_READ_DEVICE):
2080 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2084 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2085 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2087 USETW(((usb_status_t *)buf)->wStatus, 0);
2091 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2092 if (value >= USB_MAX_DEVICES) {
2096 sc->sc_addr = value;
2098 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2099 if (value != 0 && value != 1) {
2103 sc->sc_conf = value;
2105 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2107 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2108 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2109 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2112 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2114 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2117 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2119 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2120 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2121 "port=%d feature=%d\n",
2123 if (index < 1 || index > sc->sc_noport) {
2127 port = OHCI_RH_PORT_STATUS(index);
2129 case UHF_PORT_ENABLE:
2130 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2132 case UHF_PORT_SUSPEND:
2133 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2135 case UHF_PORT_POWER:
2136 OWRITE4(sc, port, UPS_LOW_SPEED);
2138 case UHF_C_PORT_CONNECTION:
2139 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2141 case UHF_C_PORT_ENABLE:
2142 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2144 case UHF_C_PORT_SUSPEND:
2145 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2147 case UHF_C_PORT_OVER_CURRENT:
2148 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2150 case UHF_C_PORT_RESET:
2151 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2158 case UHF_C_PORT_CONNECTION:
2159 case UHF_C_PORT_ENABLE:
2160 case UHF_C_PORT_SUSPEND:
2161 case UHF_C_PORT_OVER_CURRENT:
2162 case UHF_C_PORT_RESET:
2163 /* Enable RHSC interrupt if condition is cleared. */
2164 if ((OREAD4(sc, port) >> 16) == 0)
2165 ohci_rhsc_able(sc, 1);
2171 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2176 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2178 hubd.bNbrPorts = sc->sc_noport;
2179 USETW(hubd.wHubCharacteristics,
2180 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2181 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2182 /* XXX overcurrent */
2184 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2185 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2186 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2187 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2188 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2189 l = min(len, hubd.bDescLength);
2191 memcpy(buf, &hubd, l);
2193 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2198 memset(buf, 0, len); /* ? XXX */
2201 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2202 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2204 if (index < 1 || index > sc->sc_noport) {
2212 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2213 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2215 USETW(ps.wPortStatus, v);
2216 USETW(ps.wPortChange, v >> 16);
2217 l = min(len, sizeof ps);
2218 memcpy(buf, &ps, l);
2221 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2224 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2226 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2227 if (index < 1 || index > sc->sc_noport) {
2231 port = OHCI_RH_PORT_STATUS(index);
2233 case UHF_PORT_ENABLE:
2234 OWRITE4(sc, port, UPS_PORT_ENABLED);
2236 case UHF_PORT_SUSPEND:
2237 OWRITE4(sc, port, UPS_SUSPEND);
2239 case UHF_PORT_RESET:
2240 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2242 OWRITE4(sc, port, UPS_RESET);
2243 for (i = 0; i < 10; i++) {
2244 usb_delay_ms(&sc->sc_bus, 10);
2245 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2248 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2249 index, OREAD4(sc, port)));
2251 case UHF_PORT_POWER:
2252 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2254 OWRITE4(sc, port, UPS_PORT_POWER);
2265 xfer->actlen = totlen;
2266 err = USBD_NORMAL_COMPLETION;
2270 usb_transfer_complete(xfer);
2272 return (USBD_IN_PROGRESS);
2275 /* Abort a root control request. */
2277 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2279 /* Nothing to do, all transfers are synchronous. */
2282 /* Close the root pipe. */
2284 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2286 DPRINTF(("ohci_root_ctrl_close\n"));
2287 /* Nothing to do. */
2291 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2295 /* Insert last in queue. */
2296 err = usb_insert_transfer(xfer);
2300 /* Pipe isn't running, start first */
2301 return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2305 ohci_root_intr_start(usbd_xfer_handle xfer)
2307 usbd_pipe_handle pipe = xfer->pipe;
2308 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2310 sc->sc_intrxfer = xfer;
2312 return (USBD_IN_PROGRESS);
2315 /* Abort a root interrupt request. */
2317 ohci_root_intr_abort(usbd_xfer_handle xfer)
2321 if (xfer->pipe->intrxfer == xfer) {
2322 DPRINTF(("ohci_root_intr_abort: remove\n"));
2323 xfer->pipe->intrxfer = NULL;
2325 xfer->status = USBD_CANCELLED;
2327 usb_transfer_complete(xfer);
2331 /* Close the root pipe. */
2333 ohci_root_intr_close(usbd_pipe_handle pipe)
2335 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2337 DPRINTF(("ohci_root_intr_close\n"));
2339 sc->sc_intrxfer = NULL;
2342 /************************/
2345 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2349 /* Insert last in queue. */
2350 err = usb_insert_transfer(xfer);
2354 /* Pipe isn't running, start first */
2355 return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2359 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2361 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2365 if (!(xfer->rqflags & URQ_REQUEST)) {
2367 printf("ohci_device_ctrl_transfer: not a request\n");
2368 return (USBD_INVAL);
2372 err = ohci_device_request(xfer);
2376 if (sc->sc_bus.use_polling)
2377 ohci_waitintr(sc, xfer);
2378 return (USBD_IN_PROGRESS);
2381 /* Abort a device control request. */
2383 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2385 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2386 ohci_abort_xfer(xfer, USBD_CANCELLED);
2389 /* Close a device control pipe. */
2391 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2393 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2394 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2396 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2397 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2398 ohci_free_std(sc, opipe->tail.td);
2401 /************************/
2404 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2406 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2408 opipe->sed->ed.ed_headp &= LE(~OHCI_TOGGLECARRY);
2412 ohci_noop(usbd_pipe_handle pipe)
2417 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2421 /* Insert last in queue. */
2422 err = usb_insert_transfer(xfer);
2426 /* Pipe isn't running, start first */
2427 return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2431 ohci_device_bulk_start(usbd_xfer_handle xfer)
2433 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2434 usbd_device_handle dev = opipe->pipe.device;
2435 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2436 int addr = dev->address;
2437 ohci_soft_td_t *data, *tail, *tdp;
2438 ohci_soft_ed_t *sed;
2439 int s, len, isread, endpt;
2443 if (xfer->rqflags & URQ_REQUEST) {
2445 printf("ohci_device_bulk_start: a request\n");
2446 return (USBD_INVAL);
2451 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2452 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2455 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2456 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2459 opipe->u.bulk.isread = isread;
2460 opipe->u.bulk.length = len;
2462 /* Update device address */
2463 sed->ed.ed_flags = LE(
2464 (LE(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2465 OHCI_ED_SET_FA(addr));
2467 /* Allocate a chain of new TDs (including a new tail). */
2468 data = opipe->tail.td;
2469 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer->flags,
2470 &xfer->dmabuf, data, &tail);
2475 xfer->hcpriv = data;
2477 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2478 "td_cbp=0x%08x td_be=0x%08x\n",
2479 (int)LE(sed->ed.ed_flags), (int)LE(data->td.td_flags),
2480 (int)LE(data->td.td_cbp), (int)LE(data->td.td_be)));
2483 if (ohcidebug > 4) {
2485 ohci_dump_tds(data);
2489 /* Insert ED in schedule */
2491 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2493 ohci_hash_add_td(sc, tdp);
2495 sed->ed.ed_tailp = LE(tail->physaddr);
2496 opipe->tail.td = tail;
2497 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP);
2498 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2499 if (xfer->timeout && !sc->sc_bus.use_polling) {
2500 usb_timeout(ohci_timeout, xfer,
2501 MS_TO_TICKS(xfer->timeout), xfer->timo_handle);
2505 /* This goes wrong if we are too slow. */
2506 if (ohcidebug > 5) {
2507 usb_delay_ms(&sc->sc_bus, 5);
2508 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2509 OREAD4(sc, OHCI_COMMAND_STATUS)));
2511 ohci_dump_tds(data);
2517 return (USBD_IN_PROGRESS);
2521 ohci_device_bulk_abort(usbd_xfer_handle xfer)
2523 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
2524 ohci_abort_xfer(xfer, USBD_CANCELLED);
2528 * Close a device bulk pipe.
2531 ohci_device_bulk_close(usbd_pipe_handle pipe)
2533 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2534 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2536 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
2537 ohci_close_pipe(pipe, sc->sc_bulk_head);
2538 ohci_free_std(sc, opipe->tail.td);
2541 /************************/
2544 ohci_device_intr_transfer(usbd_xfer_handle xfer)
2548 /* Insert last in queue. */
2549 err = usb_insert_transfer(xfer);
2553 /* Pipe isn't running, start first */
2554 return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2558 ohci_device_intr_start(usbd_xfer_handle xfer)
2560 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2561 usbd_device_handle dev = opipe->pipe.device;
2562 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2563 ohci_soft_ed_t *sed = opipe->sed;
2564 ohci_soft_td_t *data, *tail;
2568 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
2569 "flags=%d priv=%p\n",
2570 xfer, xfer->length, xfer->flags, xfer->priv));
2573 if (xfer->rqflags & URQ_REQUEST)
2574 panic("ohci_device_intr_transfer: a request\n");
2579 data = opipe->tail.td;
2580 tail = ohci_alloc_std(sc);
2582 return (USBD_NOMEM);
2585 data->td.td_flags = LE(
2586 OHCI_TD_IN | OHCI_TD_NOCC |
2587 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
2588 if (xfer->flags & USBD_SHORT_XFER_OK)
2589 data->td.td_flags |= LE(OHCI_TD_R);
2590 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
2591 data->nexttd = tail;
2592 data->td.td_nexttd = LE(tail->physaddr);
2593 data->td.td_be = LE(LE(data->td.td_cbp) + len - 1);
2596 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
2597 xfer->hcpriv = data;
2600 if (ohcidebug > 5) {
2601 DPRINTF(("ohci_device_intr_transfer:\n"));
2603 ohci_dump_tds(data);
2607 /* Insert ED in schedule */
2609 ohci_hash_add_td(sc, data);
2610 sed->ed.ed_tailp = LE(tail->physaddr);
2611 opipe->tail.td = tail;
2612 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP);
2616 * This goes horribly wrong, printing thousands of descriptors,
2617 * because false references are followed due to the fact that the
2620 if (ohcidebug > 5) {
2621 usb_delay_ms(&sc->sc_bus, 5);
2622 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2623 OREAD4(sc, OHCI_COMMAND_STATUS)));
2625 ohci_dump_tds(data);
2630 return (USBD_IN_PROGRESS);
2633 /* Abort a device control request. */
2635 ohci_device_intr_abort(usbd_xfer_handle xfer)
2637 if (xfer->pipe->intrxfer == xfer) {
2638 DPRINTF(("ohci_device_intr_abort: remove\n"));
2639 xfer->pipe->intrxfer = NULL;
2641 ohci_abort_xfer(xfer, USBD_CANCELLED);
2644 /* Close a device interrupt pipe. */
2646 ohci_device_intr_close(usbd_pipe_handle pipe)
2648 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2649 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2650 int nslots = opipe->u.intr.nslots;
2651 int pos = opipe->u.intr.pos;
2653 ohci_soft_ed_t *p, *sed = opipe->sed;
2656 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
2657 pipe, nslots, pos));
2659 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
2660 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
2661 != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
2662 usb_delay_ms(&sc->sc_bus, 2);
2664 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
2665 != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
2666 panic("%s: Intr pipe %p still has TDs queued\n",
2667 USBDEVNAME(sc->sc_bus.bdev), pipe);
2670 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
2674 panic("ohci_device_intr_close: ED not found\n");
2676 p->next = sed->next;
2677 p->ed.ed_nexted = sed->ed.ed_nexted;
2680 for (j = 0; j < nslots; j++)
2681 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
2683 ohci_free_std(sc, opipe->tail.td);
2684 ohci_free_sed(sc, opipe->sed);
2688 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
2691 u_int npoll, slow, shigh, nslots;
2693 ohci_soft_ed_t *hsed, *sed = opipe->sed;
2695 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
2697 printf("ohci_setintr: 0 interval\n");
2698 return (USBD_INVAL);
2701 npoll = OHCI_NO_INTRS;
2702 while (npoll > ival)
2704 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
2707 * We now know which level in the tree the ED must go into.
2708 * Figure out which slot has most bandwidth left over.
2714 * 8 7 8 9 10 11 12 13 14
2715 * N (N-1) .. (N-1+N-1)
2718 shigh = slow + npoll;
2719 nslots = OHCI_NO_INTRS / npoll;
2720 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
2722 for (j = 0; j < nslots; j++)
2723 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
2729 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
2730 best, slow, shigh, bestbw));
2733 hsed = sc->sc_eds[best];
2734 sed->next = hsed->next;
2735 sed->ed.ed_nexted = hsed->ed.ed_nexted;
2737 hsed->ed.ed_nexted = LE(sed->physaddr);
2740 for (j = 0; j < nslots; j++)
2741 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
2742 opipe->u.intr.nslots = nslots;
2743 opipe->u.intr.pos = best;
2745 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
2746 return (USBD_NORMAL_COMPLETION);
2749 /***********************/
2752 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
2756 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
2758 /* Put it on our queue, */
2759 err = usb_insert_transfer(xfer);
2761 /* bail out on error, */
2762 if (err && err != USBD_IN_PROGRESS)
2765 /* XXX should check inuse here */
2767 /* insert into schedule, */
2768 ohci_device_isoc_enter(xfer);
2770 /* and put on interrupt list if the pipe wasn't running */
2772 ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2778 ohci_device_isoc_enter(usbd_xfer_handle xfer)
2780 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2781 usbd_device_handle dev = opipe->pipe.device;
2782 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2783 ohci_soft_ed_t *sed = opipe->sed;
2784 struct iso *iso = &opipe->u.iso;
2785 ohci_soft_itd_t *sitd, *nsitd;
2786 ohci_physaddr_t buf, offs;
2787 int i, ncur, nframes;
2792 sitd = opipe->tail.itd;
2793 buf = DMAADDR(&xfer->dmabuf, 0);
2794 sitd->itd.itd_bp0 = LE(buf & OHCI_ITD_PAGE_MASK);
2795 nframes = xfer->nframes;
2796 offs = buf & OHCI_ITD_OFFSET_MASK;
2797 for (i = ncur = 0; i < nframes; i++, ncur++) {
2798 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
2799 ncross > 1) { /* too many page crossings */
2801 nsitd = ohci_alloc_sitd(sc);
2802 if (nsitd == NULL) {
2807 sitd->nextitd = nsitd;
2808 sitd->itd.itd_nextitd = LE(nsitd->physaddr);
2809 sitd->itd.itd_flags = LE(
2811 OHCI_ITD_SET_SF(iso->next) |
2813 OHCI_ITD_SET_FC(OHCI_ITD_NOFFSET));
2814 sitd->itd.itd_be = LE(LE(sitd->itd.itd_bp0) + offs - 1);
2815 nsitd->itd.itd_bp0 = LE((buf + offs) & OHCI_ITD_PAGE_MASK);
2817 iso->next = iso->next + ncur;
2821 /* XXX byte order */
2822 sitd->itd.itd_offset[i] =
2823 offs | (ncross == 1 ? OHCI_ITD_PAGE_SELECT : 0);
2824 offs += xfer->frlengths[i];
2825 /* XXX update ncross */
2827 nsitd = ohci_alloc_sitd(sc);
2828 if (nsitd == NULL) {
2833 sitd->nextitd = nsitd;
2834 sitd->itd.itd_nextitd = LE(nsitd->physaddr);
2835 sitd->itd.itd_flags = LE(
2837 OHCI_ITD_SET_SF(iso->next) |
2838 OHCI_ITD_SET_DI(0) |
2839 OHCI_ITD_SET_FC(ncur));
2840 sitd->itd.itd_be = LE(LE(sitd->itd.itd_bp0) + offs - 1);
2841 iso->next = iso->next + ncur;
2843 opipe->tail.itd = nsitd;
2844 sed->ed.ed_tailp = LE(nsitd->physaddr);
2850 ohci_device_isoc_start(usbd_xfer_handle xfer)
2852 printf("ohci_device_isoc_start: not implemented\n");
2853 return (USBD_INVAL);
2857 ohci_device_isoc_abort(usbd_xfer_handle xfer)
2862 ohci_device_isoc_done(usbd_xfer_handle xfer)
2864 printf("ohci_device_isoc_done: not implemented\n");
2868 ohci_setup_isoc(usbd_pipe_handle pipe)
2870 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2871 struct iso *iso = &opipe->u.iso;
2876 return (USBD_NORMAL_COMPLETION);
2880 ohci_device_isoc_close(usbd_pipe_handle pipe)
2882 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2883 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2885 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
2886 ohci_close_pipe(pipe, sc->sc_isoc_head);
2887 ohci_free_sitd(sc, opipe->tail.itd);