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 $ */
3 /* $DragonFly: src/sys/bus/usb/ohci.c,v 1.4 2003/08/07 21:16:47 dillon Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 * USB Open Host Controller driver.
45 * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
46 * USB spec: http://www.usb.org/developers/data/usb11.pdf
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #if defined(__NetBSD__) || defined(__OpenBSD__)
53 #include <sys/kernel.h>
54 #include <sys/device.h>
55 #include <sys/select.h>
56 #elif defined(__FreeBSD__)
57 #include <sys/module.h>
59 #include <machine/bus_pio.h>
60 #include <machine/bus_memio.h>
61 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
62 #include <machine/cpu.h>
66 #include <sys/queue.h>
67 #include <sys/sysctl.h>
69 #include <machine/bus.h>
70 #include <machine/endian.h>
76 #include "usb_quirks.h"
81 #if defined(__FreeBSD__)
82 #include <machine/clock.h>
84 #define delay(d) DELAY(d)
87 #if defined(__OpenBSD__)
88 struct cfdriver ohci_cd = {
94 #define DPRINTF(x) if (ohcidebug) logprintf x
95 #define DPRINTFN(n,x) if (ohcidebug>(n)) logprintf x
97 SYSCTL_NODE(_hw_usb, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci");
98 SYSCTL_INT(_hw_usb_ohci, OID_AUTO, debug, CTLFLAG_RW,
99 &ohcidebug, 0, "ohci debug level");
102 #define DPRINTFN(n,x)
106 * The OHCI controller is little endian, so on big endian machines
107 * the data strored in memory needs to be swapped.
109 #if BYTE_ORDER == BIG_ENDIAN
110 #define LE(x) (bswap32(x))
117 Static ohci_soft_ed_t *ohci_alloc_sed(ohci_softc_t *);
118 Static void ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
120 Static ohci_soft_td_t *ohci_alloc_std(ohci_softc_t *);
121 Static void ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
123 Static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
124 Static void ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
127 Static void ohci_free_std_chain(ohci_softc_t *,
128 ohci_soft_td_t *, ohci_soft_td_t *);
130 Static usbd_status ohci_alloc_std_chain(struct ohci_pipe *,
131 ohci_softc_t *, int, int, u_int16_t, usb_dma_t *,
132 ohci_soft_td_t *, ohci_soft_td_t **);
134 #if defined(__NetBSD__) || defined(__OpenBSD__)
135 Static void ohci_shutdown(void *v);
136 Static void ohci_power(int, void *);
138 Static usbd_status ohci_open(usbd_pipe_handle);
139 Static void ohci_poll(struct usbd_bus *);
140 Static void ohci_waitintr(ohci_softc_t *,
142 Static void ohci_rhsc(ohci_softc_t *, usbd_xfer_handle);
143 Static void ohci_process_done(ohci_softc_t *,
146 Static usbd_status ohci_device_request(usbd_xfer_handle xfer);
147 Static void ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
148 Static void ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
149 Static void ohci_hash_add_td(ohci_softc_t *,
151 Static void ohci_hash_rem_td(ohci_softc_t *,
153 Static ohci_soft_td_t *ohci_hash_find_td(ohci_softc_t *,
156 Static usbd_status ohci_setup_isoc(usbd_pipe_handle pipe);
157 Static void ohci_device_isoc_enter(usbd_xfer_handle);
159 Static usbd_status ohci_allocm(struct usbd_bus *, usb_dma_t *,
161 Static void ohci_freem(struct usbd_bus *, usb_dma_t *);
163 Static usbd_xfer_handle ohci_allocx(struct usbd_bus *);
164 Static void ohci_freex(struct usbd_bus *, usbd_xfer_handle);
166 Static usbd_status ohci_root_ctrl_transfer(usbd_xfer_handle);
167 Static usbd_status ohci_root_ctrl_start(usbd_xfer_handle);
168 Static void ohci_root_ctrl_abort(usbd_xfer_handle);
169 Static void ohci_root_ctrl_close(usbd_pipe_handle);
171 Static usbd_status ohci_root_intr_transfer(usbd_xfer_handle);
172 Static usbd_status ohci_root_intr_start(usbd_xfer_handle);
173 Static void ohci_root_intr_abort(usbd_xfer_handle);
174 Static void ohci_root_intr_close(usbd_pipe_handle);
175 Static void ohci_root_intr_done (usbd_xfer_handle);
177 Static usbd_status ohci_device_ctrl_transfer(usbd_xfer_handle);
178 Static usbd_status ohci_device_ctrl_start(usbd_xfer_handle);
179 Static void ohci_device_ctrl_abort(usbd_xfer_handle);
180 Static void ohci_device_ctrl_close(usbd_pipe_handle);
181 Static void ohci_device_ctrl_done (usbd_xfer_handle);
183 Static usbd_status ohci_device_bulk_transfer(usbd_xfer_handle);
184 Static usbd_status ohci_device_bulk_start(usbd_xfer_handle);
185 Static void ohci_device_bulk_abort(usbd_xfer_handle);
186 Static void ohci_device_bulk_close(usbd_pipe_handle);
187 Static void ohci_device_bulk_done (usbd_xfer_handle);
189 Static usbd_status ohci_device_intr_transfer(usbd_xfer_handle);
190 Static usbd_status ohci_device_intr_start(usbd_xfer_handle);
191 Static void ohci_device_intr_abort(usbd_xfer_handle);
192 Static void ohci_device_intr_close(usbd_pipe_handle);
193 Static void ohci_device_intr_done (usbd_xfer_handle);
195 Static usbd_status ohci_device_isoc_transfer(usbd_xfer_handle);
196 Static usbd_status ohci_device_isoc_start(usbd_xfer_handle);
197 Static void ohci_device_isoc_abort(usbd_xfer_handle);
198 Static void ohci_device_isoc_close(usbd_pipe_handle);
199 Static void ohci_device_isoc_done (usbd_xfer_handle);
201 Static usbd_status ohci_device_setintr(ohci_softc_t *sc,
202 struct ohci_pipe *pipe, int ival);
204 Static int ohci_str(usb_string_descriptor_t *, int, char *);
206 Static void ohci_timeout(void *);
207 Static void ohci_rhsc_able(ohci_softc_t *, int);
209 Static void ohci_close_pipe(usbd_pipe_handle pipe,
210 ohci_soft_ed_t *head);
211 Static void ohci_abort_xfer(usbd_xfer_handle xfer,
213 Static void ohci_abort_xfer_end(void *);
215 Static void ohci_device_clear_toggle(usbd_pipe_handle pipe);
216 Static void ohci_noop(usbd_pipe_handle pipe);
219 Static void ohci_dumpregs(ohci_softc_t *);
220 Static void ohci_dump_tds(ohci_soft_td_t *);
221 Static void ohci_dump_td(ohci_soft_td_t *);
222 Static void ohci_dump_ed(ohci_soft_ed_t *);
225 #define OWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
226 #define OREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
227 #define OREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
229 /* Reverse the bits in a value 0 .. 31 */
230 Static u_int8_t revbits[OHCI_NO_INTRS] =
231 { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
232 0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
233 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
234 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
237 struct usbd_pipe pipe;
241 ohci_soft_itd_t *itd;
243 /* Info needed for different pipe kinds. */
249 ohci_soft_td_t *setup, *data, *stat;
268 #define OHCI_INTR_ENDPT 1
270 Static struct usbd_bus_methods ohci_bus_methods = {
279 Static struct usbd_pipe_methods ohci_root_ctrl_methods = {
280 ohci_root_ctrl_transfer,
281 ohci_root_ctrl_start,
282 ohci_root_ctrl_abort,
283 ohci_root_ctrl_close,
288 Static struct usbd_pipe_methods ohci_root_intr_methods = {
289 ohci_root_intr_transfer,
290 ohci_root_intr_start,
291 ohci_root_intr_abort,
292 ohci_root_intr_close,
297 Static struct usbd_pipe_methods ohci_device_ctrl_methods = {
298 ohci_device_ctrl_transfer,
299 ohci_device_ctrl_start,
300 ohci_device_ctrl_abort,
301 ohci_device_ctrl_close,
303 ohci_device_ctrl_done,
306 Static struct usbd_pipe_methods ohci_device_intr_methods = {
307 ohci_device_intr_transfer,
308 ohci_device_intr_start,
309 ohci_device_intr_abort,
310 ohci_device_intr_close,
311 ohci_device_clear_toggle,
312 ohci_device_intr_done,
315 Static struct usbd_pipe_methods ohci_device_bulk_methods = {
316 ohci_device_bulk_transfer,
317 ohci_device_bulk_start,
318 ohci_device_bulk_abort,
319 ohci_device_bulk_close,
320 ohci_device_clear_toggle,
321 ohci_device_bulk_done,
324 Static struct usbd_pipe_methods ohci_device_isoc_methods = {
325 ohci_device_isoc_transfer,
326 ohci_device_isoc_start,
327 ohci_device_isoc_abort,
328 ohci_device_isoc_close,
330 ohci_device_isoc_done,
333 #if defined(__NetBSD__) || defined(__OpenBSD__)
335 ohci_activate(device_ptr_t self, enum devact act)
337 struct ohci_softc *sc = (struct ohci_softc *)self;
345 case DVACT_DEACTIVATE:
346 if (sc->sc_child != NULL)
347 rv = config_deactivate(sc->sc_child);
354 ohci_detach(struct ohci_softc *sc, int flags)
358 if (sc->sc_child != NULL)
359 rv = config_detach(sc->sc_child, flags);
364 #if defined(__NetBSD__)
365 powerhook_disestablish(sc->sc_powerhook);
366 shutdownhook_disestablish(sc->sc_shutdownhook);
368 /* free data structures XXX */
375 ohci_alloc_sed(ohci_softc_t *sc)
382 if (sc->sc_freeeds == NULL) {
383 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
384 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
385 OHCI_ED_ALIGN, &dma);
388 for(i = 0; i < OHCI_SED_CHUNK; i++) {
389 offs = i * OHCI_SED_SIZE;
390 sed = (ohci_soft_ed_t *)((char *)KERNADDR(&dma, offs));
391 sed->physaddr = DMAADDR(&dma, offs);
392 sed->next = sc->sc_freeeds;
393 sc->sc_freeeds = sed;
396 sed = sc->sc_freeeds;
397 sc->sc_freeeds = sed->next;
398 memset(&sed->ed, 0, sizeof(ohci_ed_t));
404 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
406 sed->next = sc->sc_freeeds;
407 sc->sc_freeeds = sed;
411 ohci_alloc_std(ohci_softc_t *sc)
419 if (sc->sc_freetds == NULL) {
420 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
421 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
422 OHCI_TD_ALIGN, &dma);
425 for(i = 0; i < OHCI_STD_CHUNK; i++) {
426 offs = i * OHCI_STD_SIZE;
427 std = (ohci_soft_td_t *)((char *)KERNADDR(&dma, offs));
428 std->physaddr = DMAADDR(&dma, offs);
429 std->nexttd = sc->sc_freetds;
430 sc->sc_freetds = std;
433 std = sc->sc_freetds;
434 sc->sc_freetds = std->nexttd;
435 memset(&std->td, 0, sizeof(ohci_td_t));
439 ohci_hash_add_td(sc, std);
446 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
451 ohci_hash_rem_td(sc, std);
454 std->nexttd = sc->sc_freetds;
455 sc->sc_freetds = std;
459 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
460 int len, int rd, u_int16_t flags, usb_dma_t *dma,
461 ohci_soft_td_t *std, ohci_soft_td_t **rstd)
463 ohci_soft_td_t *next, *cur;
464 ohci_physaddr_t dataphys, dataphysend;
465 u_int32_t intr, tdflags;
469 DPRINTFN(len < 4096,("ohci_alloc_std_chain: start len=%d\n", len));
473 dataphysend = OHCI_PAGE(DMAADDR(dma, len - 1));
475 (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
476 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY |
477 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0);
480 next = ohci_alloc_std(sc);
484 dataphys = DMAADDR(dma, offset);
486 /* The OHCI hardware can handle at most one page crossing. */
487 #if defined(__NetBSD__) || defined(__OpenBSD__)
488 if (OHCI_PAGE(dataphys) == dataphysend ||
489 OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == dataphysend)
490 #elif defined(__FreeBSD__)
491 /* XXX This is pretty broken: Because we do not allocate
492 * a contiguous buffer (contiguous in physical pages) we
493 * can only transfer one page in one go.
494 * So check whether the start and end of the buffer are on
497 if (OHCI_PAGE(dataphys) == dataphysend)
500 /* we can handle it in this TD */
503 /* XXX The calculation below is wrong and could
504 * result in a packet that is not a multiple of the
505 * MaxPacketSize in the case where the buffer does not
506 * start on an appropriate address (like for example in
507 * the case of an mbuf cluster). You'll get an early
510 #if defined(__NetBSD__) || defined(__OpenBSD__)
511 /* must use multiple TDs, fill as much as possible. */
512 curlen = 2 * OHCI_PAGE_SIZE -
513 OHCI_PAGE_MASK(dataphys);
514 if (curlen > len) /* may have fit in one page */
516 #elif defined(__FreeBSD__)
517 /* See comment above (XXX) */
518 curlen = OHCI_PAGE_SIZE -
519 OHCI_PAGE_MASK(dataphys);
522 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
523 "dataphysend=0x%08x len=%d curlen=%d\n",
524 dataphys, dataphysend,
528 intr = len == 0 ? OHCI_TD_SET_DI(1) : OHCI_TD_NOINTR;
529 cur->td.td_flags = LE(tdflags | intr);
530 cur->td.td_cbp = LE(dataphys);
532 cur->td.td_nexttd = LE(next->physaddr);
533 cur->td.td_be = LE(dataphys + curlen - 1);
535 cur->flags = OHCI_ADD_LEN;
536 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
537 dataphys, dataphys + curlen - 1));
540 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
544 if ((flags & USBD_FORCE_SHORT_XFER) &&
545 len % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
546 /* Force a 0 length transfer at the end. */
548 cur->td.td_flags = LE(tdflags | OHCI_TD_NOINTR);
551 next = ohci_alloc_std(sc);
555 cur->td.td_flags = LE(tdflags | OHCI_TD_SET_DI(1));
556 cur->td.td_cbp = 0; /* indicate 0 length packet */
558 cur->td.td_nexttd = LE(next->physaddr);
562 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
564 cur->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
567 return (USBD_NORMAL_COMPLETION);
576 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
577 ohci_soft_td_t *stdend)
581 for (; std != stdend; std = p) {
583 ohci_free_std(sc, std);
589 ohci_alloc_sitd(ohci_softc_t *sc)
591 ohci_soft_itd_t *sitd;
596 if (sc->sc_freeitds == NULL) {
597 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
598 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
599 OHCI_TD_ALIGN, &dma);
602 for(i = 0; i < OHCI_STD_CHUNK; i++) {
603 offs = i * OHCI_STD_SIZE;
604 sitd = (ohci_soft_itd_t *)((char*)KERNADDR(&dma, offs));
605 sitd->physaddr = DMAADDR(&dma, offs);
606 sitd->nextitd = sc->sc_freeitds;
607 sc->sc_freeitds = sitd;
610 sitd = sc->sc_freeitds;
611 sc->sc_freeitds = sitd->nextitd;
612 memset(&sitd->itd, 0, sizeof(ohci_itd_t));
618 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
620 sitd->nextitd = sc->sc_freeitds;
621 sc->sc_freeitds = sitd;
625 ohci_init(ohci_softc_t *sc)
627 ohci_soft_ed_t *sed, *psed;
630 u_int32_t s, ctl, ival, hcr, fm, per, rev;
632 DPRINTF(("ohci_init: start\n"));
633 #if defined(__OpenBSD__)
636 printf("%s:", USBDEVNAME(sc->sc_bus.bdev));
638 rev = OREAD4(sc, OHCI_REVISION);
639 printf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
640 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
642 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
643 printf("%s: unsupported OHCI revision\n",
644 USBDEVNAME(sc->sc_bus.bdev));
645 sc->sc_bus.usbrev = USBREV_UNKNOWN;
648 sc->sc_bus.usbrev = USBREV_1_0;
650 for (i = 0; i < OHCI_HASH_SIZE; i++)
651 LIST_INIT(&sc->sc_hash_tds[i]);
653 SIMPLEQ_INIT(&sc->sc_free_xfers);
655 /* Allocate the HCCA area. */
656 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
657 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
660 sc->sc_hcca = (struct ohci_hcca *)KERNADDR(&sc->sc_hccadma, 0);
661 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
663 sc->sc_eintrs = OHCI_NORMAL_INTRS;
665 /* Allocate dummy ED that starts the control list. */
666 sc->sc_ctrl_head = ohci_alloc_sed(sc);
667 if (sc->sc_ctrl_head == NULL) {
671 sc->sc_ctrl_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
673 /* Allocate dummy ED that starts the bulk list. */
674 sc->sc_bulk_head = ohci_alloc_sed(sc);
675 if (sc->sc_bulk_head == NULL) {
679 sc->sc_bulk_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
681 /* Allocate dummy ED that starts the isochronous list. */
682 sc->sc_isoc_head = ohci_alloc_sed(sc);
683 if (sc->sc_isoc_head == NULL) {
687 sc->sc_isoc_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
689 /* Allocate all the dummy EDs that make up the interrupt tree. */
690 for (i = 0; i < OHCI_NO_EDS; i++) {
691 sed = ohci_alloc_sed(sc);
694 ohci_free_sed(sc, sc->sc_eds[i]);
698 /* All ED fields are set to 0. */
700 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
702 psed = sc->sc_eds[(i-1) / 2];
704 psed= sc->sc_isoc_head;
706 sed->ed.ed_nexted = LE(psed->physaddr);
709 * Fill HCCA interrupt table. The bit reversal is to get
710 * the tree set up properly to spread the interrupts.
712 for (i = 0; i < OHCI_NO_INTRS; i++)
713 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
714 LE(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
716 /* Determine in what context we are running. */
717 ctl = OREAD4(sc, OHCI_CONTROL);
719 /* SMM active, request change */
720 DPRINTF(("ohci_init: SMM active, request owner change\n"));
721 s = OREAD4(sc, OHCI_COMMAND_STATUS);
722 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
723 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
724 usb_delay_ms(&sc->sc_bus, 1);
725 ctl = OREAD4(sc, OHCI_CONTROL);
727 if ((ctl & OHCI_IR) == 0) {
728 printf("%s: SMM does not respond, resetting\n",
729 USBDEVNAME(sc->sc_bus.bdev));
730 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
733 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
734 /* BIOS started controller. */
735 DPRINTF(("ohci_init: BIOS active\n"));
736 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
737 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
738 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
741 DPRINTF(("ohci_init: cold started\n"));
743 /* Controller was cold started. */
744 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
748 * This reset should not be necessary according to the OHCI spec, but
749 * without it some controllers do not start.
751 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
752 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
753 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
755 /* We now own the host controller and the bus has been reset. */
756 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
758 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
759 /* Nominal time for a reset is 10 us. */
760 for (i = 0; i < 10; i++) {
762 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
767 printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
776 /* The controller is now in SUSPEND state, we have 2ms to finish. */
778 /* Set up HC registers. */
779 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
780 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
781 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
782 /* disable all interrupts and then switch on all desired interrupts */
783 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
784 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
785 /* switch on desired functional features */
786 ctl = OREAD4(sc, OHCI_CONTROL);
787 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
788 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
789 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
790 /* And finally start it! */
791 OWRITE4(sc, OHCI_CONTROL, ctl);
794 * The controller is now OPERATIONAL. Set a some final
795 * registers that should be set earlier, but that the
796 * controller ignores when in the SUSPEND state.
798 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
799 fm |= OHCI_FSMPS(ival) | ival;
800 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
801 per = OHCI_PERIODIC(ival); /* 90% periodic */
802 OWRITE4(sc, OHCI_PERIODIC_START, per);
804 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
806 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
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__)
818 sc->sc_powerhook = powerhook_establish(ohci_power, sc);
819 sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc);
822 return (USBD_NORMAL_COMPLETION);
825 for (i = 0; i < OHCI_NO_EDS; i++)
826 ohci_free_sed(sc, sc->sc_eds[i]);
828 ohci_free_sed(sc, sc->sc_isoc_head);
830 ohci_free_sed(sc, sc->sc_ctrl_head);
832 ohci_free_sed(sc, sc->sc_bulk_head);
834 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
839 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
841 #if defined(__NetBSD__) || defined(__OpenBSD__)
842 struct ohci_softc *sc = (struct ohci_softc *)bus;
845 return (usb_allocmem(&sc->sc_bus, size, 0, dma));
849 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
851 #if defined(__NetBSD__) || defined(__OpenBSD__)
852 struct ohci_softc *sc = (struct ohci_softc *)bus;
855 usb_freemem(&sc->sc_bus, dma);
859 ohci_allocx(struct usbd_bus *bus)
861 struct ohci_softc *sc = (struct ohci_softc *)bus;
862 usbd_xfer_handle xfer;
864 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
866 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
868 xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
870 memset(xfer, 0, sizeof *xfer);
875 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
877 struct ohci_softc *sc = (struct ohci_softc *)bus;
879 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
883 * Shut down the controller when the system is going down.
885 #if defined(__NetBSD__) || defined(__OpenBSD__)
887 ohci_shutdown(void *v)
889 ohci_softc_t *sc = v;
891 DPRINTF(("ohci_shutdown: stopping the HC\n"));
892 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
896 * Handle suspend/resume.
898 * We need to switch to polling mode here, because this routine is
899 * called from an intterupt context. This is all right since we
900 * are almost suspended anyway.
903 ohci_power(int why, void *v)
906 ohci_softc_t *sc = v;
908 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
909 /* XXX should suspend/resume */
917 ohci_dumpregs(ohci_softc_t *sc)
919 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
920 OREAD4(sc, OHCI_REVISION),
921 OREAD4(sc, OHCI_CONTROL),
922 OREAD4(sc, OHCI_COMMAND_STATUS)));
923 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
924 OREAD4(sc, OHCI_INTERRUPT_STATUS),
925 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
926 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
927 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
928 OREAD4(sc, OHCI_HCCA),
929 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
930 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
931 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
932 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
933 OREAD4(sc, OHCI_BULK_HEAD_ED),
934 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
935 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
936 OREAD4(sc, OHCI_DONE_HEAD),
937 OREAD4(sc, OHCI_FM_INTERVAL),
938 OREAD4(sc, OHCI_FM_REMAINING)));
939 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
940 OREAD4(sc, OHCI_FM_NUMBER),
941 OREAD4(sc, OHCI_PERIODIC_START),
942 OREAD4(sc, OHCI_LS_THRESHOLD)));
943 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n",
944 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
945 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
946 OREAD4(sc, OHCI_RH_STATUS)));
947 DPRINTF((" port1=0x%08x port2=0x%08x\n",
948 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
949 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
950 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n",
951 LE(sc->sc_hcca->hcca_frame_number),
952 LE(sc->sc_hcca->hcca_done_head)));
956 Static int ohci_intr1(ohci_softc_t *);
961 ohci_softc_t *sc = p;
963 /* If we get an interrupt while polling, then just ignore it. */
964 if (sc->sc_bus.use_polling) {
966 printf("ohci_intr: ignored interrupt while polling\n");
971 return (ohci_intr1(sc));
975 ohci_intr1(ohci_softc_t *sc)
977 u_int32_t intrs, eintrs;
978 ohci_physaddr_t done;
980 /* In case the interrupt occurs before initialization has completed. */
981 if (sc == NULL || sc->sc_hcca == NULL) {
983 printf("ohci_intr: sc->sc_hcca == NULL\n");
989 done = LE(sc->sc_hcca->hcca_done_head);
991 /* The LSb of done is used to inform the HC Driver that an interrupt
992 * condition exists for both the Done list and for another event
993 * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
994 * Driver checks the HccaDoneHead Value. If this value is 0, then the
995 * interrupt was caused by other than the HccaDoneHead update and the
996 * HcInterruptStatus register needs to be accessed to determine that
997 * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
998 * update interrupt is indicated and if the LSb of done is nonzero,
999 * then an additional interrupt event is indicated and
1000 * HcInterruptStatus should be checked to determine its cause.
1003 sc->sc_hcca->hcca_done_head = 0;
1004 if (done & ~OHCI_DONE_INTRS)
1006 if (done & OHCI_DONE_INTRS) {
1007 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1008 done &= ~OHCI_DONE_INTRS;
1011 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1015 /* nothing to be done ?! */
1019 intrs &= ~OHCI_MIE; /* mask out Master Interrupt Enable */
1021 /* Acknowledge any interrupts that have happened */
1022 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs);
1024 /* Any interrupts we had enabled? */
1025 eintrs = intrs & sc->sc_eintrs;
1029 sc->sc_bus.intr_context++;
1030 sc->sc_bus.no_intrs++;
1031 DPRINTFN(7, ("ohci_intr: sc=%p intrs=%x(%x) eintr=%x\n",
1032 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1035 if (eintrs & OHCI_SO) {
1036 printf("%s: scheduling overrun\n",USBDEVNAME(sc->sc_bus.bdev));
1040 if (eintrs & OHCI_WDH) {
1041 ohci_process_done(sc, done);
1044 if (eintrs & OHCI_RD) {
1045 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1046 /* XXX process resume detect */
1048 if (eintrs & OHCI_UE) {
1049 printf("%s: unrecoverable error, controller halted\n",
1050 USBDEVNAME(sc->sc_bus.bdev));
1051 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1054 if (eintrs & OHCI_RHSC) {
1055 ohci_rhsc(sc, sc->sc_intrxfer);
1056 intrs &= ~OHCI_RHSC;
1059 * Disable RHSC interrupt for now, because it will be
1060 * on until the port has been reset.
1062 ohci_rhsc_able(sc, 0);
1065 sc->sc_bus.intr_context--;
1067 /* Block unprocessed interrupts. XXX */
1068 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, intrs);
1069 sc->sc_eintrs &= ~intrs;
1075 ohci_rhsc_able(ohci_softc_t *sc, int on)
1077 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1079 sc->sc_eintrs |= OHCI_RHSC;
1080 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1082 sc->sc_eintrs &= ~OHCI_RHSC;
1083 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1088 char *ohci_cc_strs[] = {
1092 "DATA_TOGGLE_MISMATCH",
1094 "DEVICE_NOT_RESPONDING",
1095 "PID_CHECK_FAILURE",
1109 ohci_process_done(ohci_softc_t *sc, ohci_physaddr_t done)
1111 ohci_soft_td_t *std, *sdone, *stdnext;
1112 usbd_xfer_handle xfer;
1115 DPRINTFN(10,("ohci_process_done: done=0x%08lx\n", (u_long)done));
1117 /* Reverse the done list and store the reversed list in sdone */
1119 for (; done; done = LE(std->td.td_nexttd)) {
1120 std = ohci_hash_find_td(sc, done & LE(OHCI_TAILMASK));
1123 DPRINTF(("%s: Invalid done queue 0x%08x",
1124 USBDEVNAME(sc->sc_bus.bdev), done));
1127 /* XXX Should we compare the list of active TDs with
1128 * the list of TDs queued at EDs to handle the ones that
1129 * are not listed on any of the ED queues and therefore
1140 if (ohcidebug > 10) {
1141 DPRINTF(("ohci_process_done: TD done:\n"));
1142 for (std = sdone; std; std = std->dnext)
1143 ohci_dump_td(sdone);
1147 for (std = sdone; std; std = stdnext) {
1149 stdnext = std->dnext;
1150 DPRINTFN(5, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1151 std, xfer, (xfer? xfer->hcpriv:NULL)));
1152 if (xfer == NULL || (std->flags & OHCI_TD_HANDLED)) {
1153 /* xfer == NULL: There seems to be no xfer associated
1154 * with this TD. It is tailp that happened to end up on
1156 * flags & OHCI_TD_HANDLED: The TD has already been
1157 * handled by process_done and should not be done again.
1161 cc = OHCI_TD_GET_CC(LE(std->td.td_flags));
1162 usb_untimeout(ohci_timeout, xfer, xfer->timo_handle);
1163 if (xfer->status == USBD_CANCELLED ||
1164 xfer->status == USBD_TIMEOUT) {
1165 DPRINTF(("ohci_process_done: cancel/timeout, xfer=%p\n",
1167 /* Handled by abort routine. */
1168 } else if (cc == OHCI_CC_NO_ERROR) {
1169 DPRINTFN(15, ("ohci_process_done: no error, xfer=%p\n",
1172 if (std->td.td_cbp != 0)
1173 len -= LE(std->td.td_be) -
1174 LE(std->td.td_cbp) + 1;
1175 if (std->flags & OHCI_ADD_LEN)
1176 xfer->actlen += len;
1177 if (std->flags & OHCI_CALL_DONE) {
1178 xfer->status = USBD_NORMAL_COMPLETION;
1179 usb_transfer_complete(xfer);
1181 ohci_free_std(sc, std);
1184 * Endpoint is halted. First unlink all the TDs
1185 * belonging to the failed transfer, and then restart
1188 ohci_soft_td_t *p, *n;
1189 struct ohci_pipe *opipe =
1190 (struct ohci_pipe *)xfer->pipe;
1192 DPRINTF(("ohci_process_done: err cc=%d (%s), xfer=%p\n",
1193 OHCI_TD_GET_CC(LE(std->td.td_flags)),
1194 ohci_cc_strs[OHCI_TD_GET_CC(LE(std->td.td_flags))],
1197 /* Mark all the TDs in the done queue for the current
1200 for (p = stdnext; p; p = p->dnext) {
1201 if (p->xfer == xfer)
1202 p->flags |= OHCI_TD_HANDLED;
1205 /* remove TDs for the current xfer from the ED */
1206 for (p = std; p->xfer == xfer; p = n) {
1208 ohci_free_std(sc, p);
1210 opipe->sed->ed.ed_headp = LE(p->physaddr);
1212 /* XXX why is this being done? Why not OHCI_BLF too */
1213 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1215 if (cc == OHCI_CC_STALL)
1216 xfer->status = USBD_STALLED;
1218 xfer->status = USBD_IOERROR;
1220 usb_transfer_complete(xfer);
1226 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1228 DPRINTFN(10,("ohci_ctrl_done: xfer=%p\n", xfer));
1231 if (!(xfer->rqflags & URQ_REQUEST)) {
1232 panic("ohci_ctrl_done: not a request\n");
1235 xfer->hcpriv = NULL;
1239 ohci_device_intr_done(usbd_xfer_handle xfer)
1241 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1242 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1243 ohci_soft_ed_t *sed = opipe->sed;
1244 ohci_soft_td_t *data, *tail;
1247 DPRINTFN(10,("ohci_intr_done: xfer=%p, actlen=%d\n",
1248 xfer, xfer->actlen));
1250 xfer->hcpriv = NULL;
1252 if (xfer->pipe->repeat) {
1253 data = opipe->tail.td;
1254 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1256 xfer->status = USBD_NOMEM;
1261 data->td.td_flags = LE(
1262 OHCI_TD_IN | OHCI_TD_NOCC |
1263 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1264 if (xfer->flags & USBD_SHORT_XFER_OK)
1265 data->td.td_flags |= LE(OHCI_TD_R);
1266 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
1267 data->nexttd = tail;
1268 data->td.td_nexttd = LE(tail->physaddr);
1269 data->td.td_be = LE(LE(data->td.td_cbp) + xfer->length - 1);
1270 data->len = xfer->length;
1272 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1273 xfer->hcpriv = data;
1276 ohci_hash_add_td(sc, data);
1277 sed->ed.ed_tailp = LE(tail->physaddr);
1278 opipe->tail.td = tail;
1283 ohci_device_bulk_done(usbd_xfer_handle xfer)
1285 DPRINTFN(10,("ohci_bulk_done: xfer=%p, actlen=%d\n",
1286 xfer, xfer->actlen));
1288 xfer->hcpriv = NULL;
1292 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1294 usbd_pipe_handle pipe;
1295 struct ohci_pipe *opipe;
1300 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1301 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1302 sc, xfer, hstatus));
1305 /* Just ignore the change. */
1310 opipe = (struct ohci_pipe *)pipe;
1312 p = KERNADDR(&xfer->dmabuf, 0);
1313 m = min(sc->sc_noport, xfer->length * 8 - 1);
1314 memset(p, 0, xfer->length);
1315 for (i = 1; i <= m; i++) {
1316 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1317 p[i/8] |= 1 << (i%8);
1319 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1320 xfer->actlen = xfer->length;
1321 xfer->status = USBD_NORMAL_COMPLETION;
1323 usb_transfer_complete(xfer);
1327 ohci_root_intr_done(usbd_xfer_handle xfer)
1329 xfer->hcpriv = NULL;
1333 * Wait here until controller claims to have an interrupt.
1334 * Then call ohci_intr and return. Use timeout to avoid waiting
1338 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1340 int timo = xfer->timeout;
1344 xfer->status = USBD_IN_PROGRESS;
1345 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1346 usb_delay_ms(&sc->sc_bus, 1);
1347 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1348 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1355 if (xfer->status != USBD_IN_PROGRESS)
1361 DPRINTF(("ohci_waitintr: timeout\n"));
1365 xfer->status = USBD_TIMEOUT;
1366 usb_transfer_complete(xfer);
1367 /* XXX should free TD */
1371 ohci_poll(struct usbd_bus *bus)
1373 ohci_softc_t *sc = (ohci_softc_t *)bus;
1375 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1380 ohci_device_request(usbd_xfer_handle xfer)
1382 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1383 usb_device_request_t *req = &xfer->request;
1384 usbd_device_handle dev = opipe->pipe.device;
1385 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1386 int addr = dev->address;
1387 ohci_soft_td_t *setup, *data = 0, *stat, *next, *tail;
1388 ohci_soft_ed_t *sed;
1394 isread = req->bmRequestType & UT_READ;
1395 len = UGETW(req->wLength);
1397 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1398 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1399 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1400 UGETW(req->wIndex), len, addr,
1401 opipe->pipe.endpoint->edesc->bEndpointAddress));
1403 setup = opipe->tail.td;
1404 stat = ohci_alloc_std(sc);
1409 tail = ohci_alloc_std(sc);
1417 opipe->u.ctl.length = len;
1419 /* Update device address and length since they may have changed. */
1420 /* XXX This only needs to be done once, but it's too early in open. */
1421 sed->ed.ed_flags = LE(
1422 (LE(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1423 OHCI_ED_SET_FA(addr) |
1424 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1426 /* Set up data transaction */
1428 data = ohci_alloc_std(sc);
1433 data->td.td_flags = LE(
1434 (isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
1435 OHCI_TD_TOGGLE_1 | OHCI_TD_NOINTR |
1436 (xfer->flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0));
1437 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
1438 data->nexttd = stat;
1439 data->td.td_nexttd = LE(stat->physaddr);
1440 data->td.td_be = LE(LE(data->td.td_cbp) + len - 1);
1443 data->flags = OHCI_ADD_LEN;
1446 stat->flags = OHCI_CALL_DONE;
1450 stat->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1453 memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1455 setup->td.td_flags = LE(OHCI_TD_SETUP | OHCI_TD_NOCC |
1456 OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1457 setup->td.td_cbp = LE(DMAADDR(&opipe->u.ctl.reqdma, 0));
1458 setup->nexttd = next;
1459 setup->td.td_nexttd = LE(next->physaddr);
1460 setup->td.td_be = LE(LE(setup->td.td_cbp) + sizeof *req - 1);
1461 setup->len = 0; /* XXX The number of byte we count */
1464 xfer->hcpriv = setup;
1466 stat->td.td_flags = LE(
1467 (isread ? OHCI_TD_OUT : OHCI_TD_IN) | OHCI_TD_NOCC |
1468 OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1469 stat->td.td_cbp = 0;
1470 stat->nexttd = tail;
1471 stat->td.td_nexttd = LE(tail->physaddr);
1477 if (ohcidebug > 5) {
1478 DPRINTF(("ohci_device_request:\n"));
1480 ohci_dump_tds(setup);
1484 /* Insert ED in schedule */
1486 sed->ed.ed_tailp = LE(tail->physaddr);
1487 opipe->tail.td = tail;
1488 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1489 if (xfer->timeout && !sc->sc_bus.use_polling) {
1490 usb_timeout(ohci_timeout, xfer,
1491 MS_TO_TICKS(xfer->timeout), xfer->timo_handle);
1496 if (ohcidebug > 25) {
1497 usb_delay_ms(&sc->sc_bus, 5);
1498 DPRINTF(("ohci_device_request: status=%x\n",
1499 OREAD4(sc, OHCI_COMMAND_STATUS)));
1501 ohci_dump_tds(setup);
1505 return (USBD_NORMAL_COMPLETION);
1508 ohci_free_std(sc, tail);
1510 ohci_free_std(sc, stat);
1516 * Add an ED to the schedule. Called at splusb().
1519 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1522 sed->next = head->next;
1523 sed->ed.ed_nexted = head->ed.ed_nexted;
1525 head->ed.ed_nexted = LE(sed->physaddr);
1529 * Remove an ED from the schedule. Called at splusb().
1532 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1539 for (p = head; p == NULL && p->next != sed; p = p->next)
1542 panic("ohci_rem_ed: ED not found\n");
1543 p->next = sed->next;
1544 p->ed.ed_nexted = sed->ed.ed_nexted;
1548 * When a transfer is completed the TD is added to the done queue by
1549 * the host controller. This queue is the processed by software.
1550 * Unfortunately the queue contains the physical address of the TD
1551 * and we have no simple way to translate this back to a kernel address.
1552 * To make the translation possible (and fast) we use a hash table of
1553 * TDs currently in the schedule. The physical address is used as the
1557 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1558 /* Called at splusb() */
1560 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1562 int h = HASH(std->physaddr);
1566 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1569 /* Called at splusb() */
1571 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1575 LIST_REMOVE(std, hnext);
1579 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1582 ohci_soft_td_t *std;
1584 /* if these are present they should be masked out at an earlier
1587 KASSERT((a&~OHCI_TAILMASK) == 0, ("%s: 0x%b has lower bits set\n",
1588 USBDEVNAME(sc->sc_bus.bdev),
1589 (int) a, "\20\1HALT\2TOGGLE"));
1591 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1593 std = LIST_NEXT(std, hnext))
1594 if (std->physaddr == a)
1597 DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1598 USBDEVNAME(sc->sc_bus.bdev), (u_long) a));
1603 ohci_timeout(void *addr)
1605 usbd_xfer_handle xfer = addr;
1608 DPRINTF(("ohci_timeout: xfer=%p\n", xfer));
1611 xfer->device->bus->intr_context++;
1612 ohci_abort_xfer(xfer, USBD_TIMEOUT);
1613 xfer->device->bus->intr_context--;
1619 ohci_dump_tds(ohci_soft_td_t *std)
1621 for (; std; std = std->nexttd)
1626 ohci_dump_td(ohci_soft_td_t *std)
1628 DPRINTF(("TD(%p) at %08lx: %b delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1629 "nexttd=0x%08lx be=0x%08lx\n",
1630 std, (u_long)std->physaddr,
1631 (int)LE(std->td.td_flags),
1632 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1633 OHCI_TD_GET_DI(LE(std->td.td_flags)),
1634 OHCI_TD_GET_EC(LE(std->td.td_flags)),
1635 OHCI_TD_GET_CC(LE(std->td.td_flags)),
1636 (u_long)LE(std->td.td_cbp),
1637 (u_long)LE(std->td.td_nexttd), (u_long)LE(std->td.td_be)));
1641 ohci_dump_ed(ohci_soft_ed_t *sed)
1643 DPRINTF(("ED(%p) at %08lx: addr=%d endpt=%d maxp=%d %b\n"
1644 "tailp=0x%8b headp=0x%8b nexted=0x%08lx\n",
1645 sed, (u_long)sed->physaddr,
1646 OHCI_ED_GET_FA(LE(sed->ed.ed_flags)),
1647 OHCI_ED_GET_EN(LE(sed->ed.ed_flags)),
1648 OHCI_ED_GET_MAXP(LE(sed->ed.ed_flags)),
1649 (int)LE(sed->ed.ed_flags),
1650 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
1651 (int)(uintptr_t)LE(sed->ed.ed_tailp),
1653 (int)(uintptr_t)LE(sed->ed.ed_headp),
1655 (u_long)LE(sed->ed.ed_nexted)));
1660 ohci_open(usbd_pipe_handle pipe)
1662 usbd_device_handle dev = pipe->device;
1663 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1664 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1665 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1666 u_int8_t addr = dev->address;
1667 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1668 ohci_soft_ed_t *sed;
1669 ohci_soft_td_t *std = NULL;
1670 ohci_soft_itd_t *sitd;
1671 ohci_physaddr_t tdphys;
1677 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1678 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1679 if (addr == sc->sc_addr) {
1680 switch (ed->bEndpointAddress) {
1681 case USB_CONTROL_ENDPOINT:
1682 pipe->methods = &ohci_root_ctrl_methods;
1684 case UE_DIR_IN | OHCI_INTR_ENDPT:
1685 pipe->methods = &ohci_root_intr_methods;
1688 return (USBD_INVAL);
1691 sed = ohci_alloc_sed(sc);
1695 if (xfertype == UE_ISOCHRONOUS) {
1696 sitd = ohci_alloc_sitd(sc);
1698 ohci_free_sitd(sc, sitd);
1701 opipe->tail.itd = sitd;
1702 tdphys = LE(sitd->physaddr);
1703 fmt = OHCI_ED_FORMAT_ISO;
1705 std = ohci_alloc_std(sc);
1707 ohci_free_std(sc, std);
1710 opipe->tail.td = std;
1711 tdphys = LE(std->physaddr);
1712 fmt = OHCI_ED_FORMAT_GEN;
1714 sed->ed.ed_flags = LE(
1715 OHCI_ED_SET_FA(addr) |
1716 OHCI_ED_SET_EN(ed->bEndpointAddress) |
1718 (dev->lowspeed ? OHCI_ED_SPEED : 0) | fmt |
1719 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
1720 sed->ed.ed_headp = sed->ed.ed_tailp = tdphys;
1724 pipe->methods = &ohci_device_ctrl_methods;
1725 err = usb_allocmem(&sc->sc_bus,
1726 sizeof(usb_device_request_t),
1727 0, &opipe->u.ctl.reqdma);
1731 ohci_add_ed(sed, sc->sc_ctrl_head);
1735 pipe->methods = &ohci_device_intr_methods;
1736 ival = pipe->interval;
1737 if (ival == USBD_DEFAULT_INTERVAL)
1738 ival = ed->bInterval;
1739 return (ohci_device_setintr(sc, opipe, ival));
1740 case UE_ISOCHRONOUS:
1741 pipe->methods = &ohci_device_isoc_methods;
1742 return (ohci_setup_isoc(pipe));
1744 pipe->methods = &ohci_device_bulk_methods;
1746 ohci_add_ed(sed, sc->sc_bulk_head);
1751 return (USBD_NORMAL_COMPLETION);
1754 ohci_free_std(sc, std);
1756 ohci_free_sed(sc, sed);
1758 return (USBD_NOMEM);
1763 * Close a reqular pipe.
1764 * Assumes that there are no pending transactions.
1767 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
1769 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1770 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
1771 ohci_soft_ed_t *sed = opipe->sed;
1776 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
1777 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
1778 != (sed->ed.ed_headp & LE(OHCI_HEADMASK))) {
1779 ohci_physaddr_t td = sed->ed.ed_headp;
1780 ohci_soft_td_t *std;
1781 for (std = LIST_FIRST(&sc->sc_hash_tds[HASH(td)]);
1783 std = LIST_NEXT(std, hnext))
1784 if (std->physaddr == td)
1786 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
1787 "tl=0x%x pipe=%p, std=%p\n", sed,
1788 (int)LE(sed->ed.ed_headp), (int)LE(sed->ed.ed_tailp),
1790 usb_delay_ms(&sc->sc_bus, 2);
1791 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
1792 != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
1793 printf("ohci_close_pipe: pipe still not empty\n");
1796 ohci_rem_ed(sed, head);
1798 ohci_free_sed(sc, opipe->sed);
1802 * Abort a device request.
1803 * If this routine is called at splusb() it guarantees that the request
1804 * will be removed from the hardware scheduling and that the callback
1805 * for it will be called with USBD_CANCELLED status.
1806 * It's impossible to guarantee that the requested transfer will not
1807 * have happened since the hardware runs concurrently.
1808 * If the transaction has already happened we rely on the ordinary
1809 * interrupt processing to process it.
1812 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1814 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1815 ohci_soft_ed_t *sed;
1817 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p\n", xfer, opipe));
1819 xfer->status = status;
1821 usb_untimeout(ohci_timeout, xfer, xfer->timo_handle);
1824 sed->ed.ed_flags |= LE(OHCI_ED_SKIP); /* force hardware skip */
1826 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
1831 if (xfer->device->bus->intr_context) {
1832 /* We have no process context, so we can't use tsleep(). */
1833 timeout(ohci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
1835 #if defined(DIAGNOSTIC) && defined(__FreeBSD__)
1836 KASSERT(mycpu->gd_intr_nesting_level == 0,
1837 ("ohci_abort_req in interrupt context"));
1839 usb_delay_ms(opipe->pipe.device->bus, 1);
1840 ohci_abort_xfer_end(xfer);
1844 ohci_abort_xfer_end(xfer);
1849 ohci_abort_xfer_end(void *v)
1851 usbd_xfer_handle xfer = v;
1852 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1853 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1854 ohci_soft_ed_t *sed;
1855 ohci_soft_td_t *p, *n;
1863 printf("ohci_abort_xfer: hcpriv==0\n");
1868 for (; p->xfer == xfer; p = n) {
1870 ohci_free_std(sc, p);
1874 DPRINTFN(2,("ohci_abort_xfer: set hd=%x, tl=%x\n",
1875 (int)LE(p->physaddr), (int)LE(sed->ed.ed_tailp)));
1876 sed->ed.ed_headp = p->physaddr; /* unlink TDs */
1877 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP); /* remove hardware skip */
1879 usb_transfer_complete(xfer);
1885 * Data structures and routines to emulate the root hub.
1887 Static usb_device_descriptor_t ohci_devd = {
1888 USB_DEVICE_DESCRIPTOR_SIZE,
1889 UDESC_DEVICE, /* type */
1890 {0x00, 0x01}, /* USB version */
1891 UDCLASS_HUB, /* class */
1892 UDSUBCLASS_HUB, /* subclass */
1893 UDPROTO_FSHUB, /* protocol */
1894 64, /* max packet */
1895 {0},{0},{0x00,0x01}, /* device id */
1896 1,2,0, /* string indicies */
1897 1 /* # of configurations */
1900 Static usb_config_descriptor_t ohci_confd = {
1901 USB_CONFIG_DESCRIPTOR_SIZE,
1903 {USB_CONFIG_DESCRIPTOR_SIZE +
1904 USB_INTERFACE_DESCRIPTOR_SIZE +
1905 USB_ENDPOINT_DESCRIPTOR_SIZE},
1913 Static usb_interface_descriptor_t ohci_ifcd = {
1914 USB_INTERFACE_DESCRIPTOR_SIZE,
1925 Static usb_endpoint_descriptor_t ohci_endpd = {
1926 USB_ENDPOINT_DESCRIPTOR_SIZE,
1928 UE_DIR_IN | OHCI_INTR_ENDPT,
1930 {8, 0}, /* max packet */
1934 Static usb_hub_descriptor_t ohci_hubd = {
1935 USB_HUB_DESCRIPTOR_SIZE,
1945 ohci_str(usb_string_descriptor_t *p, int l, char *s)
1951 p->bLength = 2 * strlen(s) + 2;
1954 p->bDescriptorType = UDESC_STRING;
1956 for (i = 0; s[i] && l > 1; i++, l -= 2)
1957 USETW2(p->bString[i], 0, s[i]);
1962 * Simulate a hardware hub by handling all the necessary requests.
1965 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
1969 /* Insert last in queue. */
1970 err = usb_insert_transfer(xfer);
1974 /* Pipe isn't running, start first */
1975 return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1979 ohci_root_ctrl_start(usbd_xfer_handle xfer)
1981 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
1982 usb_device_request_t *req;
1985 int s, len, value, index, l, totlen = 0;
1986 usb_port_status_t ps;
1987 usb_hub_descriptor_t hubd;
1992 if (!(xfer->rqflags & URQ_REQUEST))
1994 return (USBD_INVAL);
1996 req = &xfer->request;
1998 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
1999 req->bmRequestType, req->bRequest));
2001 len = UGETW(req->wLength);
2002 value = UGETW(req->wValue);
2003 index = UGETW(req->wIndex);
2006 buf = KERNADDR(&xfer->dmabuf, 0);
2008 #define C(x,y) ((x) | ((y) << 8))
2009 switch(C(req->bRequest, req->bmRequestType)) {
2010 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2011 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2012 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2014 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2015 * for the integrated root hub.
2018 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2020 *(u_int8_t *)buf = sc->sc_conf;
2024 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2025 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2026 switch(value >> 8) {
2028 if ((value & 0xff) != 0) {
2032 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2033 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2034 memcpy(buf, &ohci_devd, l);
2037 if ((value & 0xff) != 0) {
2041 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2042 memcpy(buf, &ohci_confd, l);
2043 buf = (char *)buf + l;
2045 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2047 memcpy(buf, &ohci_ifcd, l);
2048 buf = (char *)buf + l;
2050 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2052 memcpy(buf, &ohci_endpd, l);
2057 *(u_int8_t *)buf = 0;
2059 switch (value & 0xff) {
2060 case 1: /* Vendor */
2061 totlen = ohci_str(buf, len, sc->sc_vendor);
2063 case 2: /* Product */
2064 totlen = ohci_str(buf, len, "OHCI root hub");
2073 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2075 *(u_int8_t *)buf = 0;
2079 case C(UR_GET_STATUS, UT_READ_DEVICE):
2081 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2085 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2086 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2088 USETW(((usb_status_t *)buf)->wStatus, 0);
2092 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2093 if (value >= USB_MAX_DEVICES) {
2097 sc->sc_addr = value;
2099 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2100 if (value != 0 && value != 1) {
2104 sc->sc_conf = value;
2106 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2108 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2109 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2110 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2113 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2115 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2118 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2120 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2121 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2122 "port=%d feature=%d\n",
2124 if (index < 1 || index > sc->sc_noport) {
2128 port = OHCI_RH_PORT_STATUS(index);
2130 case UHF_PORT_ENABLE:
2131 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2133 case UHF_PORT_SUSPEND:
2134 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2136 case UHF_PORT_POWER:
2137 OWRITE4(sc, port, UPS_LOW_SPEED);
2139 case UHF_C_PORT_CONNECTION:
2140 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2142 case UHF_C_PORT_ENABLE:
2143 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2145 case UHF_C_PORT_SUSPEND:
2146 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2148 case UHF_C_PORT_OVER_CURRENT:
2149 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2151 case UHF_C_PORT_RESET:
2152 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2159 case UHF_C_PORT_CONNECTION:
2160 case UHF_C_PORT_ENABLE:
2161 case UHF_C_PORT_SUSPEND:
2162 case UHF_C_PORT_OVER_CURRENT:
2163 case UHF_C_PORT_RESET:
2164 /* Enable RHSC interrupt if condition is cleared. */
2165 if ((OREAD4(sc, port) >> 16) == 0)
2166 ohci_rhsc_able(sc, 1);
2172 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2177 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2179 hubd.bNbrPorts = sc->sc_noport;
2180 USETW(hubd.wHubCharacteristics,
2181 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2182 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2183 /* XXX overcurrent */
2185 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2186 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2187 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2188 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2189 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2190 l = min(len, hubd.bDescLength);
2192 memcpy(buf, &hubd, l);
2194 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2199 memset(buf, 0, len); /* ? XXX */
2202 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2203 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2205 if (index < 1 || index > sc->sc_noport) {
2213 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2214 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2216 USETW(ps.wPortStatus, v);
2217 USETW(ps.wPortChange, v >> 16);
2218 l = min(len, sizeof ps);
2219 memcpy(buf, &ps, l);
2222 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2225 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2227 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2228 if (index < 1 || index > sc->sc_noport) {
2232 port = OHCI_RH_PORT_STATUS(index);
2234 case UHF_PORT_ENABLE:
2235 OWRITE4(sc, port, UPS_PORT_ENABLED);
2237 case UHF_PORT_SUSPEND:
2238 OWRITE4(sc, port, UPS_SUSPEND);
2240 case UHF_PORT_RESET:
2241 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2243 OWRITE4(sc, port, UPS_RESET);
2244 for (i = 0; i < 10; i++) {
2245 usb_delay_ms(&sc->sc_bus, 10);
2246 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2249 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2250 index, OREAD4(sc, port)));
2252 case UHF_PORT_POWER:
2253 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2255 OWRITE4(sc, port, UPS_PORT_POWER);
2266 xfer->actlen = totlen;
2267 err = USBD_NORMAL_COMPLETION;
2271 usb_transfer_complete(xfer);
2273 return (USBD_IN_PROGRESS);
2276 /* Abort a root control request. */
2278 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2280 /* Nothing to do, all transfers are synchronous. */
2283 /* Close the root pipe. */
2285 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2287 DPRINTF(("ohci_root_ctrl_close\n"));
2288 /* Nothing to do. */
2292 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2296 /* Insert last in queue. */
2297 err = usb_insert_transfer(xfer);
2301 /* Pipe isn't running, start first */
2302 return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2306 ohci_root_intr_start(usbd_xfer_handle xfer)
2308 usbd_pipe_handle pipe = xfer->pipe;
2309 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2311 sc->sc_intrxfer = xfer;
2313 return (USBD_IN_PROGRESS);
2316 /* Abort a root interrupt request. */
2318 ohci_root_intr_abort(usbd_xfer_handle xfer)
2322 if (xfer->pipe->intrxfer == xfer) {
2323 DPRINTF(("ohci_root_intr_abort: remove\n"));
2324 xfer->pipe->intrxfer = NULL;
2326 xfer->status = USBD_CANCELLED;
2328 usb_transfer_complete(xfer);
2332 /* Close the root pipe. */
2334 ohci_root_intr_close(usbd_pipe_handle pipe)
2336 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2338 DPRINTF(("ohci_root_intr_close\n"));
2340 sc->sc_intrxfer = NULL;
2343 /************************/
2346 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2350 /* Insert last in queue. */
2351 err = usb_insert_transfer(xfer);
2355 /* Pipe isn't running, start first */
2356 return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2360 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2362 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2366 if (!(xfer->rqflags & URQ_REQUEST)) {
2368 printf("ohci_device_ctrl_transfer: not a request\n");
2369 return (USBD_INVAL);
2373 err = ohci_device_request(xfer);
2377 if (sc->sc_bus.use_polling)
2378 ohci_waitintr(sc, xfer);
2379 return (USBD_IN_PROGRESS);
2382 /* Abort a device control request. */
2384 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2386 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2387 ohci_abort_xfer(xfer, USBD_CANCELLED);
2390 /* Close a device control pipe. */
2392 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2394 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2395 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2397 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2398 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2399 ohci_free_std(sc, opipe->tail.td);
2402 /************************/
2405 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2407 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2409 opipe->sed->ed.ed_headp &= LE(~OHCI_TOGGLECARRY);
2413 ohci_noop(usbd_pipe_handle pipe)
2418 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2422 /* Insert last in queue. */
2423 err = usb_insert_transfer(xfer);
2427 /* Pipe isn't running, start first */
2428 return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2432 ohci_device_bulk_start(usbd_xfer_handle xfer)
2434 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2435 usbd_device_handle dev = opipe->pipe.device;
2436 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2437 int addr = dev->address;
2438 ohci_soft_td_t *data, *tail, *tdp;
2439 ohci_soft_ed_t *sed;
2440 int s, len, isread, endpt;
2444 if (xfer->rqflags & URQ_REQUEST) {
2446 printf("ohci_device_bulk_start: a request\n");
2447 return (USBD_INVAL);
2452 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2453 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2456 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2457 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2460 opipe->u.bulk.isread = isread;
2461 opipe->u.bulk.length = len;
2463 /* Update device address */
2464 sed->ed.ed_flags = LE(
2465 (LE(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2466 OHCI_ED_SET_FA(addr));
2468 /* Allocate a chain of new TDs (including a new tail). */
2469 data = opipe->tail.td;
2470 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer->flags,
2471 &xfer->dmabuf, data, &tail);
2476 xfer->hcpriv = data;
2478 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2479 "td_cbp=0x%08x td_be=0x%08x\n",
2480 (int)LE(sed->ed.ed_flags), (int)LE(data->td.td_flags),
2481 (int)LE(data->td.td_cbp), (int)LE(data->td.td_be)));
2484 if (ohcidebug > 4) {
2486 ohci_dump_tds(data);
2490 /* Insert ED in schedule */
2492 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2494 ohci_hash_add_td(sc, tdp);
2496 sed->ed.ed_tailp = LE(tail->physaddr);
2497 opipe->tail.td = tail;
2498 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP);
2499 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2500 if (xfer->timeout && !sc->sc_bus.use_polling) {
2501 usb_timeout(ohci_timeout, xfer,
2502 MS_TO_TICKS(xfer->timeout), xfer->timo_handle);
2506 /* This goes wrong if we are too slow. */
2507 if (ohcidebug > 5) {
2508 usb_delay_ms(&sc->sc_bus, 5);
2509 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2510 OREAD4(sc, OHCI_COMMAND_STATUS)));
2512 ohci_dump_tds(data);
2518 return (USBD_IN_PROGRESS);
2522 ohci_device_bulk_abort(usbd_xfer_handle xfer)
2524 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
2525 ohci_abort_xfer(xfer, USBD_CANCELLED);
2529 * Close a device bulk pipe.
2532 ohci_device_bulk_close(usbd_pipe_handle pipe)
2534 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2535 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2537 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
2538 ohci_close_pipe(pipe, sc->sc_bulk_head);
2539 ohci_free_std(sc, opipe->tail.td);
2542 /************************/
2545 ohci_device_intr_transfer(usbd_xfer_handle xfer)
2549 /* Insert last in queue. */
2550 err = usb_insert_transfer(xfer);
2554 /* Pipe isn't running, start first */
2555 return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2559 ohci_device_intr_start(usbd_xfer_handle xfer)
2561 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2562 usbd_device_handle dev = opipe->pipe.device;
2563 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2564 ohci_soft_ed_t *sed = opipe->sed;
2565 ohci_soft_td_t *data, *tail;
2569 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
2570 "flags=%d priv=%p\n",
2571 xfer, xfer->length, xfer->flags, xfer->priv));
2574 if (xfer->rqflags & URQ_REQUEST)
2575 panic("ohci_device_intr_transfer: a request\n");
2580 data = opipe->tail.td;
2581 tail = ohci_alloc_std(sc);
2583 return (USBD_NOMEM);
2586 data->td.td_flags = LE(
2587 OHCI_TD_IN | OHCI_TD_NOCC |
2588 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
2589 if (xfer->flags & USBD_SHORT_XFER_OK)
2590 data->td.td_flags |= LE(OHCI_TD_R);
2591 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
2592 data->nexttd = tail;
2593 data->td.td_nexttd = LE(tail->physaddr);
2594 data->td.td_be = LE(LE(data->td.td_cbp) + len - 1);
2597 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
2598 xfer->hcpriv = data;
2601 if (ohcidebug > 5) {
2602 DPRINTF(("ohci_device_intr_transfer:\n"));
2604 ohci_dump_tds(data);
2608 /* Insert ED in schedule */
2610 ohci_hash_add_td(sc, data);
2611 sed->ed.ed_tailp = LE(tail->physaddr);
2612 opipe->tail.td = tail;
2613 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP);
2617 * This goes horribly wrong, printing thousands of descriptors,
2618 * because false references are followed due to the fact that the
2621 if (ohcidebug > 5) {
2622 usb_delay_ms(&sc->sc_bus, 5);
2623 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2624 OREAD4(sc, OHCI_COMMAND_STATUS)));
2626 ohci_dump_tds(data);
2631 return (USBD_IN_PROGRESS);
2634 /* Abort a device control request. */
2636 ohci_device_intr_abort(usbd_xfer_handle xfer)
2638 if (xfer->pipe->intrxfer == xfer) {
2639 DPRINTF(("ohci_device_intr_abort: remove\n"));
2640 xfer->pipe->intrxfer = NULL;
2642 ohci_abort_xfer(xfer, USBD_CANCELLED);
2645 /* Close a device interrupt pipe. */
2647 ohci_device_intr_close(usbd_pipe_handle pipe)
2649 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2650 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2651 int nslots = opipe->u.intr.nslots;
2652 int pos = opipe->u.intr.pos;
2654 ohci_soft_ed_t *p, *sed = opipe->sed;
2657 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
2658 pipe, nslots, pos));
2660 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
2661 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
2662 != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
2663 usb_delay_ms(&sc->sc_bus, 2);
2665 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
2666 != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
2667 panic("%s: Intr pipe %p still has TDs queued\n",
2668 USBDEVNAME(sc->sc_bus.bdev), pipe);
2671 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
2675 panic("ohci_device_intr_close: ED not found\n");
2677 p->next = sed->next;
2678 p->ed.ed_nexted = sed->ed.ed_nexted;
2681 for (j = 0; j < nslots; j++)
2682 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
2684 ohci_free_std(sc, opipe->tail.td);
2685 ohci_free_sed(sc, opipe->sed);
2689 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
2692 u_int npoll, slow, shigh, nslots;
2694 ohci_soft_ed_t *hsed, *sed = opipe->sed;
2696 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
2698 printf("ohci_setintr: 0 interval\n");
2699 return (USBD_INVAL);
2702 npoll = OHCI_NO_INTRS;
2703 while (npoll > ival)
2705 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
2708 * We now know which level in the tree the ED must go into.
2709 * Figure out which slot has most bandwidth left over.
2715 * 8 7 8 9 10 11 12 13 14
2716 * N (N-1) .. (N-1+N-1)
2719 shigh = slow + npoll;
2720 nslots = OHCI_NO_INTRS / npoll;
2721 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
2723 for (j = 0; j < nslots; j++)
2724 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
2730 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
2731 best, slow, shigh, bestbw));
2734 hsed = sc->sc_eds[best];
2735 sed->next = hsed->next;
2736 sed->ed.ed_nexted = hsed->ed.ed_nexted;
2738 hsed->ed.ed_nexted = LE(sed->physaddr);
2741 for (j = 0; j < nslots; j++)
2742 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
2743 opipe->u.intr.nslots = nslots;
2744 opipe->u.intr.pos = best;
2746 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
2747 return (USBD_NORMAL_COMPLETION);
2750 /***********************/
2753 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
2757 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
2759 /* Put it on our queue, */
2760 err = usb_insert_transfer(xfer);
2762 /* bail out on error, */
2763 if (err && err != USBD_IN_PROGRESS)
2766 /* XXX should check inuse here */
2768 /* insert into schedule, */
2769 ohci_device_isoc_enter(xfer);
2771 /* and put on interrupt list if the pipe wasn't running */
2773 ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2779 ohci_device_isoc_enter(usbd_xfer_handle xfer)
2781 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2782 usbd_device_handle dev = opipe->pipe.device;
2783 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2784 ohci_soft_ed_t *sed = opipe->sed;
2785 struct iso *iso = &opipe->u.iso;
2786 ohci_soft_itd_t *sitd, *nsitd;
2787 ohci_physaddr_t buf, offs;
2788 int i, ncur, nframes;
2793 sitd = opipe->tail.itd;
2794 buf = DMAADDR(&xfer->dmabuf, 0);
2795 sitd->itd.itd_bp0 = LE(buf & OHCI_ITD_PAGE_MASK);
2796 nframes = xfer->nframes;
2797 offs = buf & OHCI_ITD_OFFSET_MASK;
2798 for (i = ncur = 0; i < nframes; i++, ncur++) {
2799 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
2800 ncross > 1) { /* too many page crossings */
2802 nsitd = ohci_alloc_sitd(sc);
2803 if (nsitd == NULL) {
2808 sitd->nextitd = nsitd;
2809 sitd->itd.itd_nextitd = LE(nsitd->physaddr);
2810 sitd->itd.itd_flags = LE(
2812 OHCI_ITD_SET_SF(iso->next) |
2814 OHCI_ITD_SET_FC(OHCI_ITD_NOFFSET));
2815 sitd->itd.itd_be = LE(LE(sitd->itd.itd_bp0) + offs - 1);
2816 nsitd->itd.itd_bp0 = LE((buf + offs) & OHCI_ITD_PAGE_MASK);
2818 iso->next = iso->next + ncur;
2822 /* XXX byte order */
2823 sitd->itd.itd_offset[i] =
2824 offs | (ncross == 1 ? OHCI_ITD_PAGE_SELECT : 0);
2825 offs += xfer->frlengths[i];
2826 /* XXX update ncross */
2828 nsitd = ohci_alloc_sitd(sc);
2829 if (nsitd == NULL) {
2834 sitd->nextitd = nsitd;
2835 sitd->itd.itd_nextitd = LE(nsitd->physaddr);
2836 sitd->itd.itd_flags = LE(
2838 OHCI_ITD_SET_SF(iso->next) |
2839 OHCI_ITD_SET_DI(0) |
2840 OHCI_ITD_SET_FC(ncur));
2841 sitd->itd.itd_be = LE(LE(sitd->itd.itd_bp0) + offs - 1);
2842 iso->next = iso->next + ncur;
2844 opipe->tail.itd = nsitd;
2845 sed->ed.ed_tailp = LE(nsitd->physaddr);
2851 ohci_device_isoc_start(usbd_xfer_handle xfer)
2853 printf("ohci_device_isoc_start: not implemented\n");
2854 return (USBD_INVAL);
2858 ohci_device_isoc_abort(usbd_xfer_handle xfer)
2863 ohci_device_isoc_done(usbd_xfer_handle xfer)
2865 printf("ohci_device_isoc_done: not implemented\n");
2869 ohci_setup_isoc(usbd_pipe_handle pipe)
2871 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2872 struct iso *iso = &opipe->u.iso;
2877 return (USBD_NORMAL_COMPLETION);
2881 ohci_device_isoc_close(usbd_pipe_handle pipe)
2883 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2884 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2886 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
2887 ohci_close_pipe(pipe, sc->sc_isoc_head);
2888 ohci_free_sitd(sc, opipe->tail.itd);