2 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * USB Universal Host Controller driver.
33 * Handles e.g. PIIX3 and PIIX4.
35 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
36 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
37 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
38 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
41 #include <sys/stdint.h>
42 #include <sys/stddef.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/linker_set.h>
50 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
64 #define USB_DEBUG_VAR uhcidebug
66 #include <dev/usb/usb_core.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_process.h>
70 #include <dev/usb/usb_transfer.h>
71 #include <dev/usb/usb_device.h>
72 #include <dev/usb/usb_hub.h>
73 #include <dev/usb/usb_util.h>
75 #include <dev/usb/usb_controller.h>
76 #include <dev/usb/usb_bus.h>
77 #include <dev/usb/controller/uhci.h>
80 #define UHCI_BUS2SC(bus) \
81 ((uhci_softc_t *)(((uint8_t *)(bus)) - \
82 ((uint8_t *)&(((uhci_softc_t *)0)->sc_bus))))
85 static int uhcidebug = 0;
86 static int uhcinoloop = 0;
88 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
89 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
90 &uhcidebug, 0, "uhci debug level");
91 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
92 &uhcinoloop, 0, "uhci noloop");
93 static void uhci_dumpregs(uhci_softc_t *sc);
94 static void uhci_dump_tds(uhci_td_t *td);
98 #define UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
99 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
100 #define UWRITE1(sc, r, x) \
101 do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
102 } while (/*CONSTCOND*/0)
103 #define UWRITE2(sc, r, x) \
104 do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
105 } while (/*CONSTCOND*/0)
106 #define UWRITE4(sc, r, x) \
107 do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
108 } while (/*CONSTCOND*/0)
109 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
110 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
111 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
113 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
114 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
116 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
118 #define UHCI_INTR_ENDPT 1
120 struct uhci_mem_layout {
122 struct usb_page_search buf_res;
123 struct usb_page_search fix_res;
125 struct usb_page_cache *buf_pc;
126 struct usb_page_cache *fix_pc;
130 uint16_t max_frame_size;
133 struct uhci_std_temp {
135 struct uhci_mem_layout ml;
142 uint16_t max_frame_size;
144 uint8_t setup_alt_next;
148 extern struct usb_bus_methods uhci_bus_methods;
149 extern struct usb_pipe_methods uhci_device_bulk_methods;
150 extern struct usb_pipe_methods uhci_device_ctrl_methods;
151 extern struct usb_pipe_methods uhci_device_intr_methods;
152 extern struct usb_pipe_methods uhci_device_isoc_methods;
154 static uint8_t uhci_restart(uhci_softc_t *sc);
155 static void uhci_do_poll(struct usb_bus *);
156 static void uhci_device_done(struct usb_xfer *, usb_error_t);
157 static void uhci_transfer_intr_enqueue(struct usb_xfer *);
158 static void uhci_timeout(void *);
159 static uint8_t uhci_check_transfer(struct usb_xfer *);
160 static void uhci_root_intr(uhci_softc_t *sc);
163 uhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
165 struct uhci_softc *sc = UHCI_BUS2SC(bus);
168 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
169 sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
171 cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
172 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
174 cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
175 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
177 cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
178 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
180 cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
181 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
183 cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
184 sizeof(uhci_td_t), UHCI_TD_ALIGN);
186 for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
187 cb(bus, sc->sc_hw.isoc_start_pc + i,
188 sc->sc_hw.isoc_start_pg + i,
189 sizeof(uhci_td_t), UHCI_TD_ALIGN);
192 for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
193 cb(bus, sc->sc_hw.intr_start_pc + i,
194 sc->sc_hw.intr_start_pg + i,
195 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
200 uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb_xfer *xfer)
202 ml->buf_pc = xfer->frbuffers + 0;
203 ml->fix_pc = xfer->buf_fixup;
207 ml->max_frame_size = xfer->max_frame_size;
211 uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
213 usbd_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
215 if (ml->buf_res.length < td->len) {
217 /* need to do a fixup */
219 usbd_get_page(ml->fix_pc, 0, &ml->fix_res);
221 td->td_buffer = htole32(ml->fix_res.physaddr);
224 * The UHCI driver cannot handle
225 * page crossings, so a fixup is
238 if ((td->td_token & htole32(UHCI_TD_PID)) ==
239 htole32(UHCI_TD_PID_IN)) {
240 td->fix_pc = ml->fix_pc;
241 usb_pc_cpu_invalidate(ml->fix_pc);
246 /* copy data to fixup location */
248 usbd_copy_out(ml->buf_pc, ml->buf_offset,
249 ml->fix_res.buffer, td->len);
251 usb_pc_cpu_flush(ml->fix_pc);
254 /* prepare next fixup */
260 td->td_buffer = htole32(ml->buf_res.physaddr);
264 /* prepare next data location */
266 ml->buf_offset += td->len;
275 uhci_restart(uhci_softc_t *sc)
277 struct usb_page_search buf_res;
279 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
281 if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) {
282 DPRINTFN(2, "Already started\n");
286 DPRINTFN(2, "Restarting\n");
288 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
290 /* Reload fresh base address */
291 UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
294 * Assume 64 byte packets at frame end and start HC controller:
296 UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
298 /* wait 10 milliseconds */
300 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
302 /* check that controller has started */
304 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
305 DPRINTFN(2, "Failed\n");
312 uhci_reset(uhci_softc_t *sc)
316 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
318 DPRINTF("resetting the HC\n");
320 /* disable interrupts */
322 UWRITE2(sc, UHCI_INTR, 0);
326 UHCICMD(sc, UHCI_CMD_GRESET);
330 usb_pause_mtx(&sc->sc_bus.bus_mtx,
331 USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
333 /* terminate all transfers */
335 UHCICMD(sc, UHCI_CMD_HCRESET);
337 /* the reset bit goes low when the controller is done */
339 n = UHCI_RESET_TIMEOUT;
341 /* wait one millisecond */
343 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
345 if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
350 device_printf(sc->sc_bus.bdev,
351 "controller did not reset\n");
357 /* wait one millisecond */
359 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
361 /* check if HC is stopped */
362 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
367 device_printf(sc->sc_bus.bdev,
368 "controller did not stop\n");
372 /* reload the configuration */
373 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
374 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
376 USB_BUS_UNLOCK(&sc->sc_bus);
378 /* stop root interrupt */
379 usb_callout_drain(&sc->sc_root_intr);
381 USB_BUS_LOCK(&sc->sc_bus);
385 uhci_start(uhci_softc_t *sc)
387 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
389 DPRINTFN(2, "enabling\n");
391 /* enable interrupts */
393 UWRITE2(sc, UHCI_INTR,
399 if (uhci_restart(sc)) {
400 device_printf(sc->sc_bus.bdev,
401 "cannot start HC controller\n");
404 /* start root interrupt */
408 static struct uhci_qh *
409 uhci_init_qh(struct usb_page_cache *pc)
411 struct usb_page_search buf_res;
414 usbd_get_page(pc, 0, &buf_res);
419 htole32(buf_res.physaddr) |
420 htole32(UHCI_PTR_QH);
427 static struct uhci_td *
428 uhci_init_td(struct usb_page_cache *pc)
430 struct usb_page_search buf_res;
433 usbd_get_page(pc, 0, &buf_res);
438 htole32(buf_res.physaddr) |
439 htole32(UHCI_PTR_TD);
447 uhci_init(uhci_softc_t *sc)
455 usb_callout_init_mtx(&sc->sc_root_intr, &sc->sc_bus.bus_mtx, 0);
462 sc->sc_saved_sof = 0x40; /* default value */
463 sc->sc_saved_frnum = 0; /* default frame number */
468 sc->sc_ls_ctl_p_last =
469 uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc);
471 sc->sc_fs_ctl_p_last =
472 uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc);
475 uhci_init_qh(&sc->sc_hw.bulk_start_pc);
477 sc->sc_reclaim_qh_p =
478 sc->sc_fs_ctl_p_last;
480 /* setup reclaim looping point */
481 sc->sc_reclaim_qh_p =
486 uhci_init_qh(&sc->sc_hw.last_qh_pc);
489 uhci_init_td(&sc->sc_hw.last_td_pc);
491 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
492 sc->sc_isoc_p_last[x] =
493 uhci_init_td(sc->sc_hw.isoc_start_pc + x);
496 for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) {
497 sc->sc_intr_p_last[x] =
498 uhci_init_qh(sc->sc_hw.intr_start_pc + x);
502 * the QHs are arranged to give poll intervals that are
503 * powers of 2 times 1ms
505 bit = UHCI_IFRAMELIST_COUNT / 2;
512 y = (x ^ bit) | (bit / 2);
515 * the next QH has half the poll interval
517 qh_x = sc->sc_intr_p_last[x];
518 qh_y = sc->sc_intr_p_last[y];
521 qh_x->qh_h_next = qh_y->qh_self;
523 qh_x->qh_e_next = htole32(UHCI_PTR_T);
533 qh_ls = sc->sc_ls_ctl_p_last;
534 qh_intr = sc->sc_intr_p_last[0];
536 /* start QH for interrupt traffic */
537 qh_intr->h_next = qh_ls;
538 qh_intr->qh_h_next = qh_ls->qh_self;
540 qh_intr->qh_e_next = htole32(UHCI_PTR_T);
542 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
547 td_x = sc->sc_isoc_p_last[x];
548 qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)];
550 /* start TD for isochronous traffic */
552 td_x->td_next = qh_intr->qh_self;
553 td_x->td_status = htole32(UHCI_TD_IOS);
554 td_x->td_token = htole32(0);
555 td_x->td_buffer = htole32(0);
562 qh_ls = sc->sc_ls_ctl_p_last;
563 qh_fs = sc->sc_fs_ctl_p_last;
565 /* start QH where low speed control traffic will be queued */
566 qh_ls->h_next = qh_fs;
567 qh_ls->qh_h_next = qh_fs->qh_self;
569 qh_ls->qh_e_next = htole32(UHCI_PTR_T);
577 qh_ctl = sc->sc_fs_ctl_p_last;
578 qh_blk = sc->sc_bulk_p_last;
580 /* start QH where full speed control traffic will be queued */
581 qh_ctl->h_next = qh_blk;
582 qh_ctl->qh_h_next = qh_blk->qh_self;
584 qh_ctl->qh_e_next = htole32(UHCI_PTR_T);
586 qh_lst = sc->sc_last_qh_p;
588 /* start QH where bulk traffic will be queued */
589 qh_blk->h_next = qh_lst;
590 qh_blk->qh_h_next = qh_lst->qh_self;
592 qh_blk->qh_e_next = htole32(UHCI_PTR_T);
594 td_lst = sc->sc_last_td_p;
596 /* end QH which is used for looping the QHs */
598 qh_lst->qh_h_next = htole32(UHCI_PTR_T); /* end of QH chain */
599 qh_lst->e_next = td_lst;
600 qh_lst->qh_e_next = td_lst->td_self;
603 * end TD which hangs from the last QH, to avoid a bug in the PIIX
604 * that makes it run berserk otherwise
607 td_lst->td_next = htole32(UHCI_PTR_T);
608 td_lst->td_status = htole32(0); /* inactive */
609 td_lst->td_token = htole32(0);
610 td_lst->td_buffer = htole32(0);
613 struct usb_page_search buf_res;
616 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
618 pframes = buf_res.buffer;
622 * Setup UHCI framelist
626 * pframes -> full speed isochronous -> interrupt QH's -> low
627 * speed control -> full speed control -> bulk transfers
631 for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
633 sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
636 /* flush all cache into memory */
638 usb_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
640 /* set up the bus struct */
641 sc->sc_bus.methods = &uhci_bus_methods;
643 USB_BUS_LOCK(&sc->sc_bus);
644 /* reset the controller */
647 /* start the controller */
649 USB_BUS_UNLOCK(&sc->sc_bus);
651 /* catch lost interrupts */
652 uhci_do_poll(&sc->sc_bus);
657 /* NOTE: suspend/resume is called from
658 * interrupt context and cannot sleep!
662 uhci_suspend(uhci_softc_t *sc)
664 USB_BUS_LOCK(&sc->sc_bus);
671 /* save some state if BIOS doesn't */
673 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
674 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
676 /* stop the controller */
680 /* enter global suspend */
682 UHCICMD(sc, UHCI_CMD_EGSM);
684 usb_pause_mtx(&sc->sc_bus.bus_mtx,
685 USB_MS_TO_TICKS(USB_RESUME_WAIT));
687 USB_BUS_UNLOCK(&sc->sc_bus);
691 uhci_resume(uhci_softc_t *sc)
693 USB_BUS_LOCK(&sc->sc_bus);
695 /* reset the controller */
699 /* force global resume */
701 UHCICMD(sc, UHCI_CMD_FGR);
703 usb_pause_mtx(&sc->sc_bus.bus_mtx,
704 USB_MS_TO_TICKS(USB_RESUME_DELAY));
706 /* and start traffic again */
716 USB_BUS_UNLOCK(&sc->sc_bus);
718 /* catch lost interrupts */
719 uhci_do_poll(&sc->sc_bus);
724 uhci_dumpregs(uhci_softc_t *sc)
726 DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
727 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
728 device_get_nameunit(sc->sc_bus.bdev),
729 UREAD2(sc, UHCI_CMD),
730 UREAD2(sc, UHCI_STS),
731 UREAD2(sc, UHCI_INTR),
732 UREAD2(sc, UHCI_FRNUM),
733 UREAD4(sc, UHCI_FLBASEADDR),
734 UREAD1(sc, UHCI_SOF),
735 UREAD2(sc, UHCI_PORTSC1),
736 UREAD2(sc, UHCI_PORTSC2));
740 uhci_dump_td(uhci_td_t *p)
747 usb_pc_cpu_invalidate(p->page_cache);
749 td_next = le32toh(p->td_next);
750 td_status = le32toh(p->td_status);
751 td_token = le32toh(p->td_token);
754 * Check whether the link pointer in this TD marks the link pointer
757 temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
759 printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
760 "token=0x%08x buffer=0x%08x\n",
766 le32toh(p->td_buffer));
768 printf("TD(%p) td_next=%s%s%s td_status=%s%s%s%s%s%s%s%s%s%s%s, errcnt=%d, actlen=%d pid=%02x,"
769 "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
771 (td_next & 1) ? "-T" : "",
772 (td_next & 2) ? "-Q" : "",
773 (td_next & 4) ? "-VF" : "",
774 (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
775 (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
776 (td_status & UHCI_TD_NAK) ? "-NAK" : "",
777 (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
778 (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
779 (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
780 (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
781 (td_status & UHCI_TD_IOC) ? "-IOC" : "",
782 (td_status & UHCI_TD_IOS) ? "-IOS" : "",
783 (td_status & UHCI_TD_LS) ? "-LS" : "",
784 (td_status & UHCI_TD_SPD) ? "-SPD" : "",
785 UHCI_TD_GET_ERRCNT(td_status),
786 UHCI_TD_GET_ACTLEN(td_status),
787 UHCI_TD_GET_PID(td_token),
788 UHCI_TD_GET_DEVADDR(td_token),
789 UHCI_TD_GET_ENDPT(td_token),
790 UHCI_TD_GET_DT(td_token),
791 UHCI_TD_GET_MAXLEN(td_token));
797 uhci_dump_qh(uhci_qh_t *sqh)
803 usb_pc_cpu_invalidate(sqh->page_cache);
805 qh_h_next = le32toh(sqh->qh_h_next);
806 qh_e_next = le32toh(sqh->qh_e_next);
808 DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
809 le32toh(sqh->qh_self), qh_h_next, qh_e_next);
811 temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
812 (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
818 uhci_dump_all(uhci_softc_t *sc)
821 uhci_dump_qh(sc->sc_ls_ctl_p_last);
822 uhci_dump_qh(sc->sc_fs_ctl_p_last);
823 uhci_dump_qh(sc->sc_bulk_p_last);
824 uhci_dump_qh(sc->sc_last_qh_p);
828 uhci_dump_qhs(uhci_qh_t *sqh)
832 temp = uhci_dump_qh(sqh);
835 * uhci_dump_qhs displays all the QHs and TDs from the given QH
836 * onwards Traverses sideways first, then down.
838 * QH1 QH2 No QH TD2.1 TD2.2 TD1.1 etc.
840 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
844 uhci_dump_qhs(sqh->h_next);
849 uhci_dump_tds(sqh->e_next);
855 uhci_dump_tds(uhci_td_t *td)
860 if (uhci_dump_td(td)) {
869 * Let the last QH loop back to the full speed control transfer QH.
870 * This is what intel calls "bandwidth reclamation" and improves
871 * USB performance a lot for some devices.
872 * If we are already looping, just count it.
875 uhci_add_loop(uhci_softc_t *sc)
877 struct uhci_qh *qh_lst;
878 struct uhci_qh *qh_rec;
885 if (++(sc->sc_loops) == 1) {
886 DPRINTFN(6, "add\n");
888 qh_lst = sc->sc_last_qh_p;
889 qh_rec = sc->sc_reclaim_qh_p;
891 /* NOTE: we don't loop back the soft pointer */
893 qh_lst->qh_h_next = qh_rec->qh_self;
894 usb_pc_cpu_flush(qh_lst->page_cache);
899 uhci_rem_loop(uhci_softc_t *sc)
901 struct uhci_qh *qh_lst;
908 if (--(sc->sc_loops) == 0) {
909 DPRINTFN(6, "remove\n");
911 qh_lst = sc->sc_last_qh_p;
912 qh_lst->qh_h_next = htole32(UHCI_PTR_T);
913 usb_pc_cpu_flush(qh_lst->page_cache);
918 uhci_transfer_intr_enqueue(struct usb_xfer *xfer)
920 /* check for early completion */
921 if (uhci_check_transfer(xfer)) {
924 /* put transfer on interrupt queue */
925 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
927 /* start timeout, if any */
928 if (xfer->timeout != 0) {
929 usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
933 #define UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
935 _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
937 DPRINTFN(11, "%p to %p\n", std, last);
939 /* (sc->sc_bus.mtx) must be locked */
941 std->next = last->next;
942 std->td_next = last->td_next;
946 usb_pc_cpu_flush(std->page_cache);
949 * the last->next->prev is never followed: std->next->prev = std;
952 last->td_next = std->td_self;
954 usb_pc_cpu_flush(last->page_cache);
959 #define UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
961 _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
963 DPRINTFN(11, "%p to %p\n", sqh, last);
965 if (sqh->h_prev != NULL) {
966 /* should not happen */
967 DPRINTFN(0, "QH already linked!\n");
970 /* (sc->sc_bus.mtx) must be locked */
972 sqh->h_next = last->h_next;
973 sqh->qh_h_next = last->qh_h_next;
977 usb_pc_cpu_flush(sqh->page_cache);
980 * The "last->h_next->h_prev" is never followed:
982 * "sqh->h_next->h_prev" = sqh;
986 last->qh_h_next = sqh->qh_self;
988 usb_pc_cpu_flush(last->page_cache);
995 #define UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
997 _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
999 DPRINTFN(11, "%p from %p\n", std, last);
1001 /* (sc->sc_bus.mtx) must be locked */
1003 std->prev->next = std->next;
1004 std->prev->td_next = std->td_next;
1006 usb_pc_cpu_flush(std->prev->page_cache);
1009 std->next->prev = std->prev;
1010 usb_pc_cpu_flush(std->next->page_cache);
1012 return ((last == std) ? std->prev : last);
1015 #define UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
1017 _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
1019 DPRINTFN(11, "%p from %p\n", sqh, last);
1021 /* (sc->sc_bus.mtx) must be locked */
1023 /* only remove if not removed from a queue */
1026 sqh->h_prev->h_next = sqh->h_next;
1027 sqh->h_prev->qh_h_next = sqh->qh_h_next;
1029 usb_pc_cpu_flush(sqh->h_prev->page_cache);
1032 sqh->h_next->h_prev = sqh->h_prev;
1033 usb_pc_cpu_flush(sqh->h_next->page_cache);
1035 last = ((last == sqh) ? sqh->h_prev : last);
1039 usb_pc_cpu_flush(sqh->page_cache);
1045 uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer)
1047 struct usb_page_search res;
1048 uint32_t nframes = xfer->nframes;
1050 uint32_t offset = 0;
1051 uint32_t *plen = xfer->frlengths;
1053 uhci_td_t *td = xfer->td_transfer_first;
1054 uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
1056 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1057 xfer, xfer->endpoint);
1059 /* sync any DMA memory before doing fixups */
1061 usb_bdma_post_sync(xfer);
1065 panic("%s:%d: out of TD's\n",
1066 __FUNCTION__, __LINE__);
1068 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
1069 pp_last = &sc->sc_isoc_p_last[0];
1072 if (uhcidebug > 5) {
1073 DPRINTF("isoc TD\n");
1077 usb_pc_cpu_invalidate(td->page_cache);
1078 status = le32toh(td->td_status);
1080 len = UHCI_TD_GET_ACTLEN(status);
1087 usbd_get_page(td->fix_pc, 0, &res);
1089 /* copy data from fixup location to real location */
1091 usb_pc_cpu_invalidate(td->fix_pc);
1093 usbd_copy_in(xfer->frbuffers, offset,
1100 /* remove TD from schedule */
1101 UHCI_REMOVE_TD(td, *pp_last);
1108 xfer->aframes = xfer->nframes;
1112 uhci_non_isoc_done_sub(struct usb_xfer *xfer)
1114 struct usb_page_search res;
1116 uhci_td_t *td_alt_next;
1121 td = xfer->td_transfer_cache;
1122 td_alt_next = td->alt_next;
1124 if (xfer->aframes != xfer->nframes) {
1125 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1129 usb_pc_cpu_invalidate(td->page_cache);
1130 status = le32toh(td->td_status);
1131 token = le32toh(td->td_token);
1134 * Verify the status and add
1135 * up the actual length:
1138 len = UHCI_TD_GET_ACTLEN(status);
1139 if (len > td->len) {
1140 /* should not happen */
1141 DPRINTF("Invalid status length, "
1142 "0x%04x/0x%04x bytes\n", len, td->len);
1143 status |= UHCI_TD_STALLED;
1145 } else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
1149 usbd_get_page(td->fix_pc, 0, &res);
1152 * copy data from fixup location to real
1156 usb_pc_cpu_invalidate(td->fix_pc);
1158 usbd_copy_in(xfer->frbuffers + xfer->aframes,
1159 xfer->frlengths[xfer->aframes], res.buffer, len);
1161 /* update actual length */
1163 xfer->frlengths[xfer->aframes] += len;
1165 /* Check for last transfer */
1166 if (((void *)td) == xfer->td_transfer_last) {
1170 if (status & UHCI_TD_STALLED) {
1171 /* the transfer is finished */
1175 /* Check for short transfer */
1176 if (len != td->len) {
1177 if (xfer->flags_int.short_frames_ok) {
1178 /* follow alt next */
1181 /* the transfer is finished */
1188 if (td->alt_next != td_alt_next) {
1189 /* this USB frame is complete */
1194 /* update transfer cache */
1196 xfer->td_transfer_cache = td;
1198 /* update data toggle */
1200 xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
1203 if (status & UHCI_TD_ERROR) {
1204 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
1205 "status=%s%s%s%s%s%s%s%s%s%s%s\n",
1206 xfer->address, xfer->endpointno, xfer->aframes,
1207 (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
1208 (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
1209 (status & UHCI_TD_NAK) ? "[NAK]" : "",
1210 (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
1211 (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
1212 (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
1213 (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1214 (status & UHCI_TD_IOC) ? "[IOC]" : "",
1215 (status & UHCI_TD_IOS) ? "[IOS]" : "",
1216 (status & UHCI_TD_LS) ? "[LS]" : "",
1217 (status & UHCI_TD_SPD) ? "[SPD]" : "");
1220 return (status & UHCI_TD_STALLED) ?
1221 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION;
1225 uhci_non_isoc_done(struct usb_xfer *xfer)
1227 usb_error_t err = 0;
1229 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1230 xfer, xfer->endpoint);
1233 if (uhcidebug > 10) {
1234 uhci_dump_tds(xfer->td_transfer_first);
1238 /* sync any DMA memory before doing fixups */
1240 usb_bdma_post_sync(xfer);
1244 xfer->td_transfer_cache = xfer->td_transfer_first;
1246 if (xfer->flags_int.control_xfr) {
1247 if (xfer->flags_int.control_hdr) {
1249 err = uhci_non_isoc_done_sub(xfer);
1253 if (xfer->td_transfer_cache == NULL) {
1257 while (xfer->aframes != xfer->nframes) {
1259 err = uhci_non_isoc_done_sub(xfer);
1262 if (xfer->td_transfer_cache == NULL) {
1267 if (xfer->flags_int.control_xfr &&
1268 !xfer->flags_int.control_act) {
1270 err = uhci_non_isoc_done_sub(xfer);
1273 uhci_device_done(xfer, err);
1276 /*------------------------------------------------------------------------*
1277 * uhci_check_transfer_sub
1279 * The main purpose of this function is to update the data-toggle
1280 * in case it is wrong.
1281 *------------------------------------------------------------------------*/
1283 uhci_check_transfer_sub(struct usb_xfer *xfer)
1287 uhci_td_t *td_alt_next;
1292 td = xfer->td_transfer_cache;
1293 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1295 td_token = td->obj_next->td_token;
1297 xfer->td_transfer_cache = td;
1298 td_self = td->td_self;
1299 td_alt_next = td->alt_next;
1301 if (xfer->flags_int.control_xfr)
1302 goto skip; /* don't touch the DT value! */
1304 if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))))
1305 goto skip; /* data toggle has correct value */
1308 * The data toggle is wrong and we need to toggle it !
1312 td->td_token ^= htole32(UHCI_TD_SET_DT(1));
1313 usb_pc_cpu_flush(td->page_cache);
1315 if (td == xfer->td_transfer_last) {
1321 if (td->alt_next != td_alt_next) {
1329 qh->qh_e_next = td_self;
1330 usb_pc_cpu_flush(qh->page_cache);
1332 DPRINTFN(13, "xfer=%p following alt next\n", xfer);
1335 /*------------------------------------------------------------------------*
1336 * uhci_check_transfer
1339 * 0: USB transfer is not finished
1340 * Else: USB transfer is finished
1341 *------------------------------------------------------------------------*/
1343 uhci_check_transfer(struct usb_xfer *xfer)
1349 DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
1351 if (xfer->endpoint->methods == &uhci_device_isoc_methods) {
1352 /* isochronous transfer */
1354 td = xfer->td_transfer_last;
1356 usb_pc_cpu_invalidate(td->page_cache);
1357 status = le32toh(td->td_status);
1359 /* check also if the first is complete */
1361 td = xfer->td_transfer_first;
1363 usb_pc_cpu_invalidate(td->page_cache);
1364 status |= le32toh(td->td_status);
1366 if (!(status & UHCI_TD_ACTIVE)) {
1367 uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1371 /* non-isochronous transfer */
1374 * check whether there is an error somewhere
1375 * in the middle, or whether there was a short
1376 * packet (SPD and not ACTIVE)
1378 td = xfer->td_transfer_cache;
1381 usb_pc_cpu_invalidate(td->page_cache);
1382 status = le32toh(td->td_status);
1383 token = le32toh(td->td_token);
1386 * if there is an active TD the transfer isn't done
1388 if (status & UHCI_TD_ACTIVE) {
1390 xfer->td_transfer_cache = td;
1394 * last transfer descriptor makes the transfer done
1396 if (((void *)td) == xfer->td_transfer_last) {
1400 * any kind of error makes the transfer done
1402 if (status & UHCI_TD_STALLED) {
1406 * check if we reached the last packet
1407 * or if there is a short packet:
1409 if ((td->td_next == htole32(UHCI_PTR_T)) ||
1410 (UHCI_TD_GET_ACTLEN(status) < td->len)) {
1412 if (xfer->flags_int.short_frames_ok) {
1413 /* follow alt next */
1416 xfer->td_transfer_cache = td;
1417 uhci_check_transfer_sub(xfer);
1421 /* transfer is done */
1426 uhci_non_isoc_done(xfer);
1431 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1439 uhci_interrupt_poll(uhci_softc_t *sc)
1441 struct usb_xfer *xfer;
1444 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1446 * check if transfer is transferred
1448 if (uhci_check_transfer(xfer)) {
1449 /* queue has been modified */
1455 /*------------------------------------------------------------------------*
1456 * uhci_interrupt - UHCI interrupt handler
1458 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1459 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1461 *------------------------------------------------------------------------*/
1463 uhci_interrupt(uhci_softc_t *sc)
1467 USB_BUS_LOCK(&sc->sc_bus);
1469 DPRINTFN(16, "real interrupt\n");
1472 if (uhcidebug > 15) {
1476 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1478 /* the interrupt was not for us */
1481 if (status & (UHCI_STS_RD | UHCI_STS_HSE |
1482 UHCI_STS_HCPE | UHCI_STS_HCH)) {
1484 if (status & UHCI_STS_RD) {
1486 printf("%s: resume detect\n",
1490 if (status & UHCI_STS_HSE) {
1491 printf("%s: host system error\n",
1494 if (status & UHCI_STS_HCPE) {
1495 printf("%s: host controller process error\n",
1498 if (status & UHCI_STS_HCH) {
1499 /* no acknowledge needed */
1500 DPRINTF("%s: host controller halted\n",
1503 if (uhcidebug > 0) {
1509 /* get acknowledge bits */
1510 status &= (UHCI_STS_USBINT |
1517 /* nothing to acknowledge */
1520 /* acknowledge interrupts */
1521 UWRITE2(sc, UHCI_STS, status);
1523 /* poll all the USB transfers */
1524 uhci_interrupt_poll(sc);
1527 USB_BUS_UNLOCK(&sc->sc_bus);
1531 * called when a request does not complete
1534 uhci_timeout(void *arg)
1536 struct usb_xfer *xfer = arg;
1538 DPRINTF("xfer=%p\n", xfer);
1540 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1542 /* transfer is transferred */
1543 uhci_device_done(xfer, USB_ERR_TIMEOUT);
1547 uhci_do_poll(struct usb_bus *bus)
1549 struct uhci_softc *sc = UHCI_BUS2SC(bus);
1551 USB_BUS_LOCK(&sc->sc_bus);
1552 uhci_interrupt_poll(sc);
1553 USB_BUS_UNLOCK(&sc->sc_bus);
1557 uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
1561 uhci_td_t *td_alt_next;
1564 uint8_t shortpkt_old;
1568 shortpkt_old = temp->shortpkt;
1569 len_old = temp->len;
1572 /* software is used to detect short incoming transfers */
1574 if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
1575 temp->td_status |= htole32(UHCI_TD_SPD);
1577 temp->td_status &= ~htole32(UHCI_TD_SPD);
1580 temp->ml.buf_offset = 0;
1584 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1585 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
1588 td_next = temp->td_next;
1592 if (temp->len == 0) {
1594 if (temp->shortpkt) {
1597 /* send a Zero Length Packet, ZLP, last */
1600 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
1605 average = temp->average;
1607 if (temp->len < average) {
1609 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1610 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
1611 average = temp->len;
1615 if (td_next == NULL) {
1616 panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
1621 td_next = td->obj_next;
1623 /* check if we are pre-computing */
1627 /* update remaining length */
1629 temp->len -= average;
1633 /* fill out current TD */
1635 td->td_status = temp->td_status;
1636 td->td_token = temp->td_token;
1638 /* update data toggle */
1640 temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
1650 /* update remaining length */
1652 temp->len -= average;
1656 /* fill out buffer pointer and do fixup, if any */
1658 uhci_mem_layout_fixup(&temp->ml, td);
1661 td->alt_next = td_alt_next;
1663 if ((td_next == td_alt_next) && temp->setup_alt_next) {
1664 /* we need to receive these frames one by one ! */
1665 td->td_status |= htole32(UHCI_TD_IOC);
1666 td->td_next = htole32(UHCI_PTR_T);
1669 /* link the current TD with the next one */
1670 td->td_next = td_next->td_self;
1674 usb_pc_cpu_flush(td->page_cache);
1680 /* setup alt next pointer, if any */
1681 if (temp->last_frame) {
1684 /* we use this field internally */
1685 td_alt_next = td_next;
1689 temp->shortpkt = shortpkt_old;
1690 temp->len = len_old;
1694 temp->td_next = td_next;
1698 uhci_setup_standard_chain(struct usb_xfer *xfer)
1700 struct uhci_std_temp temp;
1704 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1705 xfer->address, UE_GET_ADDR(xfer->endpointno),
1706 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1708 temp.average = xfer->max_frame_size;
1709 temp.max_frame_size = xfer->max_frame_size;
1711 /* toggle the DMA set we are using */
1712 xfer->flags_int.curr_dma_set ^= 1;
1714 /* get next DMA set */
1715 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1716 xfer->td_transfer_first = td;
1717 xfer->td_transfer_cache = td;
1721 temp.last_frame = 0;
1722 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1724 uhci_mem_layout_init(&temp.ml, xfer);
1727 htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
1730 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1731 temp.td_status |= htole32(UHCI_TD_LS);
1734 htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) |
1735 UHCI_TD_SET_DEVADDR(xfer->address));
1737 if (xfer->endpoint->toggle_next) {
1739 temp.td_token |= htole32(UHCI_TD_SET_DT(1));
1741 /* check if we should prepend a setup message */
1743 if (xfer->flags_int.control_xfr) {
1745 if (xfer->flags_int.control_hdr) {
1747 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1748 UHCI_TD_SET_ENDPT(0xF));
1749 temp.td_token |= htole32(UHCI_TD_PID_SETUP |
1752 temp.len = xfer->frlengths[0];
1753 temp.ml.buf_pc = xfer->frbuffers + 0;
1754 temp.shortpkt = temp.len ? 1 : 0;
1755 /* check for last frame */
1756 if (xfer->nframes == 1) {
1757 /* no STATUS stage yet, SETUP is last */
1758 if (xfer->flags_int.control_act) {
1759 temp.last_frame = 1;
1760 temp.setup_alt_next = 0;
1763 uhci_setup_standard_chain_sub(&temp);
1770 while (x != xfer->nframes) {
1772 /* DATA0 / DATA1 message */
1774 temp.len = xfer->frlengths[x];
1775 temp.ml.buf_pc = xfer->frbuffers + x;
1779 if (x == xfer->nframes) {
1780 if (xfer->flags_int.control_xfr) {
1781 /* no STATUS stage yet, DATA is last */
1782 if (xfer->flags_int.control_act) {
1783 temp.last_frame = 1;
1784 temp.setup_alt_next = 0;
1787 temp.last_frame = 1;
1788 temp.setup_alt_next = 0;
1792 * Keep previous data toggle,
1793 * device address and endpoint number:
1796 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1797 UHCI_TD_SET_ENDPT(0xF) |
1800 if (temp.len == 0) {
1802 /* make sure that we send an USB packet */
1808 /* regular data transfer */
1810 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1813 /* set endpoint direction */
1816 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1817 htole32(UHCI_TD_PID_IN) :
1818 htole32(UHCI_TD_PID_OUT);
1820 uhci_setup_standard_chain_sub(&temp);
1823 /* check if we should append a status stage */
1825 if (xfer->flags_int.control_xfr &&
1826 !xfer->flags_int.control_act) {
1829 * send a DATA1 message and reverse the current endpoint
1833 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1834 UHCI_TD_SET_ENDPT(0xF) |
1837 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1838 htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
1839 htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
1842 temp.ml.buf_pc = NULL;
1844 temp.last_frame = 1;
1845 temp.setup_alt_next = 0;
1847 uhci_setup_standard_chain_sub(&temp);
1851 /* Ensure that last TD is terminating: */
1852 td->td_next = htole32(UHCI_PTR_T);
1854 /* set interrupt bit */
1856 td->td_status |= htole32(UHCI_TD_IOC);
1858 usb_pc_cpu_flush(td->page_cache);
1860 /* must have at least one frame! */
1862 xfer->td_transfer_last = td;
1865 if (uhcidebug > 8) {
1866 DPRINTF("nexttog=%d; data before transfer:\n",
1867 xfer->endpoint->toggle_next);
1868 uhci_dump_tds(xfer->td_transfer_first);
1871 return (xfer->td_transfer_first);
1874 /* NOTE: "done" can be run two times in a row,
1875 * from close and from interrupt
1879 uhci_device_done(struct usb_xfer *xfer, usb_error_t error)
1881 struct usb_pipe_methods *methods = xfer->endpoint->methods;
1882 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1885 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1887 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1888 xfer, xfer->endpoint, error);
1890 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1892 usb_pc_cpu_invalidate(qh->page_cache);
1894 if (xfer->flags_int.bandwidth_reclaimed) {
1895 xfer->flags_int.bandwidth_reclaimed = 0;
1898 if (methods == &uhci_device_bulk_methods) {
1899 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
1901 if (methods == &uhci_device_ctrl_methods) {
1902 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1903 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
1905 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
1908 if (methods == &uhci_device_intr_methods) {
1909 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
1912 * Only finish isochronous transfers once
1913 * which will update "xfer->frlengths".
1915 if (xfer->td_transfer_first &&
1916 xfer->td_transfer_last) {
1917 if (methods == &uhci_device_isoc_methods) {
1918 uhci_isoc_done(sc, xfer);
1920 xfer->td_transfer_first = NULL;
1921 xfer->td_transfer_last = NULL;
1923 /* dequeue transfer and start next transfer */
1924 usbd_transfer_done(xfer, error);
1927 /*------------------------------------------------------------------------*
1929 *------------------------------------------------------------------------*/
1931 uhci_device_bulk_open(struct usb_xfer *xfer)
1937 uhci_device_bulk_close(struct usb_xfer *xfer)
1939 uhci_device_done(xfer, USB_ERR_CANCELLED);
1943 uhci_device_bulk_enter(struct usb_xfer *xfer)
1949 uhci_device_bulk_start(struct usb_xfer *xfer)
1951 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1956 td = uhci_setup_standard_chain(xfer);
1959 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1962 qh->qh_e_next = td->td_self;
1964 if (xfer->xroot->udev->flags.self_suspended == 0) {
1965 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
1967 xfer->flags_int.bandwidth_reclaimed = 1;
1969 usb_pc_cpu_flush(qh->page_cache);
1972 /* put transfer on interrupt queue */
1973 uhci_transfer_intr_enqueue(xfer);
1976 struct usb_pipe_methods uhci_device_bulk_methods =
1978 .open = uhci_device_bulk_open,
1979 .close = uhci_device_bulk_close,
1980 .enter = uhci_device_bulk_enter,
1981 .start = uhci_device_bulk_start,
1984 /*------------------------------------------------------------------------*
1985 * uhci control support
1986 *------------------------------------------------------------------------*/
1988 uhci_device_ctrl_open(struct usb_xfer *xfer)
1994 uhci_device_ctrl_close(struct usb_xfer *xfer)
1996 uhci_device_done(xfer, USB_ERR_CANCELLED);
2000 uhci_device_ctrl_enter(struct usb_xfer *xfer)
2006 uhci_device_ctrl_start(struct usb_xfer *xfer)
2008 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2013 td = uhci_setup_standard_chain(xfer);
2016 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2019 qh->qh_e_next = td->td_self;
2022 * NOTE: some devices choke on bandwidth- reclamation for control
2025 if (xfer->xroot->udev->flags.self_suspended == 0) {
2026 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
2027 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
2029 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
2032 usb_pc_cpu_flush(qh->page_cache);
2034 /* put transfer on interrupt queue */
2035 uhci_transfer_intr_enqueue(xfer);
2038 struct usb_pipe_methods uhci_device_ctrl_methods =
2040 .open = uhci_device_ctrl_open,
2041 .close = uhci_device_ctrl_close,
2042 .enter = uhci_device_ctrl_enter,
2043 .start = uhci_device_ctrl_start,
2046 /*------------------------------------------------------------------------*
2047 * uhci interrupt support
2048 *------------------------------------------------------------------------*/
2050 uhci_device_intr_open(struct usb_xfer *xfer)
2052 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2058 bit = UHCI_IFRAMELIST_COUNT / 2;
2060 if (xfer->interval >= bit) {
2064 if (sc->sc_intr_stat[x] <
2065 sc->sc_intr_stat[best]) {
2075 sc->sc_intr_stat[best]++;
2076 xfer->qh_pos = best;
2078 DPRINTFN(3, "best=%d interval=%d\n",
2079 best, xfer->interval);
2083 uhci_device_intr_close(struct usb_xfer *xfer)
2085 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2087 sc->sc_intr_stat[xfer->qh_pos]--;
2089 uhci_device_done(xfer, USB_ERR_CANCELLED);
2093 uhci_device_intr_enter(struct usb_xfer *xfer)
2099 uhci_device_intr_start(struct usb_xfer *xfer)
2101 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2106 td = uhci_setup_standard_chain(xfer);
2109 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2112 qh->qh_e_next = td->td_self;
2114 if (xfer->xroot->udev->flags.self_suspended == 0) {
2115 /* enter QHs into the controller data structures */
2116 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
2118 usb_pc_cpu_flush(qh->page_cache);
2121 /* put transfer on interrupt queue */
2122 uhci_transfer_intr_enqueue(xfer);
2125 struct usb_pipe_methods uhci_device_intr_methods =
2127 .open = uhci_device_intr_open,
2128 .close = uhci_device_intr_close,
2129 .enter = uhci_device_intr_enter,
2130 .start = uhci_device_intr_start,
2133 /*------------------------------------------------------------------------*
2134 * uhci isochronous support
2135 *------------------------------------------------------------------------*/
2137 uhci_device_isoc_open(struct usb_xfer *xfer)
2144 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
2145 UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) :
2146 UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0);
2148 td_token = htole32(td_token);
2150 /* initialize all TD's */
2152 for (ds = 0; ds != 2; ds++) {
2154 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2156 /* mark TD as inactive */
2157 td->td_status = htole32(UHCI_TD_IOS);
2158 td->td_token = td_token;
2160 usb_pc_cpu_flush(td->page_cache);
2166 uhci_device_isoc_close(struct usb_xfer *xfer)
2168 uhci_device_done(xfer, USB_ERR_CANCELLED);
2172 uhci_device_isoc_enter(struct usb_xfer *xfer)
2174 struct uhci_mem_layout ml;
2175 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2185 uhci_td_t *td_last = NULL;
2186 uhci_td_t **pp_last;
2188 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2189 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2191 nframes = UREAD2(sc, UHCI_FRNUM);
2193 temp = (nframes - xfer->endpoint->isoc_next) &
2194 (UHCI_VFRAMELIST_COUNT - 1);
2196 if ((xfer->endpoint->is_synced == 0) ||
2197 (temp < xfer->nframes)) {
2199 * If there is data underflow or the pipe queue is empty we
2200 * schedule the transfer a few frames ahead of the current
2201 * frame position. Else two isochronous transfers might
2204 xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
2205 xfer->endpoint->is_synced = 1;
2206 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2209 * compute how many milliseconds the insertion is ahead of the
2210 * current frame position:
2212 temp = (xfer->endpoint->isoc_next - nframes) &
2213 (UHCI_VFRAMELIST_COUNT - 1);
2216 * pre-compute when the isochronous transfer will be finished:
2218 xfer->isoc_time_complete =
2219 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2222 /* get the real number of frames */
2224 nframes = xfer->nframes;
2226 uhci_mem_layout_init(&ml, xfer);
2228 plen = xfer->frlengths;
2230 /* toggle the DMA set we are using */
2231 xfer->flags_int.curr_dma_set ^= 1;
2233 /* get next DMA set */
2234 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2235 xfer->td_transfer_first = td;
2237 pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next];
2239 /* store starting position */
2241 xfer->qh_pos = xfer->endpoint->isoc_next;
2245 panic("%s:%d: out of TD's\n",
2246 __FUNCTION__, __LINE__);
2248 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
2249 pp_last = &sc->sc_isoc_p_last[0];
2251 if (*plen > xfer->max_frame_size) {
2255 printf("%s: frame length(%d) exceeds %d "
2256 "bytes (frame truncated)\n",
2257 __FUNCTION__, *plen,
2258 xfer->max_frame_size);
2261 *plen = xfer->max_frame_size;
2263 /* reuse td_token from last transfer */
2265 td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2266 td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
2272 * Do not call "uhci_mem_layout_fixup()" when the
2280 /* fill out buffer pointer and do fixup, if any */
2282 uhci_mem_layout_fixup(&ml, td);
2288 td->td_status = htole32
2289 (UHCI_TD_ZERO_ACTLEN
2290 (UHCI_TD_SET_ERRCNT(0) |
2295 td->td_status = htole32
2296 (UHCI_TD_ZERO_ACTLEN
2297 (UHCI_TD_SET_ERRCNT(0) |
2302 usb_pc_cpu_flush(td->page_cache);
2305 if (uhcidebug > 5) {
2306 DPRINTF("TD %d\n", nframes);
2310 /* insert TD into schedule */
2311 UHCI_APPEND_TD(td, *pp_last);
2319 xfer->td_transfer_last = td_last;
2321 /* update isoc_next */
2322 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
2323 (UHCI_VFRAMELIST_COUNT - 1);
2327 uhci_device_isoc_start(struct usb_xfer *xfer)
2329 /* put transfer on interrupt queue */
2330 uhci_transfer_intr_enqueue(xfer);
2333 struct usb_pipe_methods uhci_device_isoc_methods =
2335 .open = uhci_device_isoc_open,
2336 .close = uhci_device_isoc_close,
2337 .enter = uhci_device_isoc_enter,
2338 .start = uhci_device_isoc_start,
2341 /*------------------------------------------------------------------------*
2342 * uhci root control support
2343 *------------------------------------------------------------------------*
2344 * Simulate a hardware hub by handling all the necessary requests.
2345 *------------------------------------------------------------------------*/
2348 struct usb_device_descriptor uhci_devd =
2350 sizeof(struct usb_device_descriptor),
2351 UDESC_DEVICE, /* type */
2352 {0x00, 0x01}, /* USB version */
2353 UDCLASS_HUB, /* class */
2354 UDSUBCLASS_HUB, /* subclass */
2355 UDPROTO_FSHUB, /* protocol */
2356 64, /* max packet */
2357 {0}, {0}, {0x00, 0x01}, /* device id */
2358 1, 2, 0, /* string indicies */
2359 1 /* # of configurations */
2362 static const struct uhci_config_desc uhci_confd = {
2364 .bLength = sizeof(struct usb_config_descriptor),
2365 .bDescriptorType = UDESC_CONFIG,
2366 .wTotalLength[0] = sizeof(uhci_confd),
2368 .bConfigurationValue = 1,
2369 .iConfiguration = 0,
2370 .bmAttributes = UC_SELF_POWERED,
2371 .bMaxPower = 0 /* max power */
2374 .bLength = sizeof(struct usb_interface_descriptor),
2375 .bDescriptorType = UDESC_INTERFACE,
2377 .bInterfaceClass = UICLASS_HUB,
2378 .bInterfaceSubClass = UISUBCLASS_HUB,
2379 .bInterfaceProtocol = UIPROTO_FSHUB,
2382 .bLength = sizeof(struct usb_endpoint_descriptor),
2383 .bDescriptorType = UDESC_ENDPOINT,
2384 .bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
2385 .bmAttributes = UE_INTERRUPT,
2386 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
2392 struct usb_hub_descriptor_min uhci_hubd_piix =
2394 sizeof(uhci_hubd_piix),
2397 {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
2398 50, /* power on to power good */
2400 {0x00}, /* both ports are removable */
2404 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2405 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2406 * should not be used by the USB subsystem. As we cannot issue a
2407 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2408 * will be enabled as part of the reset.
2410 * On the VT83C572, the port cannot be successfully enabled until the
2411 * outstanding "port enable change" and "connection status change"
2412 * events have been reset.
2415 uhci_portreset(uhci_softc_t *sc, uint16_t index)
2422 port = UHCI_PORTSC1;
2423 else if (index == 2)
2424 port = UHCI_PORTSC2;
2426 return (USB_ERR_IOERROR);
2429 * Before we do anything, turn on SOF messages on the USB
2430 * BUS. Some USB devices do not cope without them!
2434 x = URWMASK(UREAD2(sc, port));
2435 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2437 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2438 USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
2440 DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
2441 index, UREAD2(sc, port));
2443 x = URWMASK(UREAD2(sc, port));
2444 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2447 mtx_unlock(&sc->sc_bus.bus_mtx);
2450 * This delay needs to be exactly 100us, else some USB devices
2455 mtx_lock(&sc->sc_bus.bus_mtx);
2457 DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
2458 index, UREAD2(sc, port));
2460 x = URWMASK(UREAD2(sc, port));
2461 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2463 for (lim = 0; lim < 12; lim++) {
2465 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2466 USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
2468 x = UREAD2(sc, port);
2470 DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
2473 if (!(x & UHCI_PORTSC_CCS)) {
2475 * No device is connected (or was disconnected
2476 * during reset). Consider the port reset.
2477 * The delay must be long enough to ensure on
2478 * the initial iteration that the device
2479 * connection will have been registered. 50ms
2480 * appears to be sufficient, but 20ms is not.
2482 DPRINTFN(4, "uhci port %d loop %u, device detached\n",
2486 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
2488 * Port enabled changed and/or connection
2489 * status changed were set. Reset either or
2490 * both raised flags (by writing a 1 to that
2491 * bit), and wait again for state to settle.
2493 UWRITE2(sc, port, URWMASK(x) |
2494 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
2497 if (x & UHCI_PORTSC_PE) {
2498 /* port is enabled */
2501 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
2504 DPRINTFN(2, "uhci port %d reset timed out\n", index);
2505 return (USB_ERR_TIMEOUT);
2508 DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
2509 index, UREAD2(sc, port));
2512 return (USB_ERR_NORMAL_COMPLETION);
2516 uhci_roothub_exec(struct usb_device *udev,
2517 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2519 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
2521 const char *str_ptr;
2531 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2534 ptr = (const void *)&sc->sc_hub_desc.temp;
2538 value = UGETW(req->wValue);
2539 index = UGETW(req->wIndex);
2541 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2542 "wValue=0x%04x wIndex=0x%04x\n",
2543 req->bmRequestType, req->bRequest,
2544 UGETW(req->wLength), value, index);
2546 #define C(x,y) ((x) | ((y) << 8))
2547 switch (C(req->bRequest, req->bmRequestType)) {
2548 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2549 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2550 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2552 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2553 * for the integrated root hub.
2556 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2558 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2560 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2561 switch (value >> 8) {
2563 if ((value & 0xff) != 0) {
2564 err = USB_ERR_IOERROR;
2567 len = sizeof(uhci_devd);
2568 ptr = (const void *)&uhci_devd;
2572 if ((value & 0xff) != 0) {
2573 err = USB_ERR_IOERROR;
2576 len = sizeof(uhci_confd);
2577 ptr = (const void *)&uhci_confd;
2581 switch (value & 0xff) {
2582 case 0: /* Language table */
2586 case 1: /* Vendor */
2587 str_ptr = sc->sc_vendor;
2590 case 2: /* Product */
2591 str_ptr = "UHCI root HUB";
2599 len = usb_make_str_desc
2600 (sc->sc_hub_desc.temp,
2601 sizeof(sc->sc_hub_desc.temp),
2606 err = USB_ERR_IOERROR;
2610 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2612 sc->sc_hub_desc.temp[0] = 0;
2614 case C(UR_GET_STATUS, UT_READ_DEVICE):
2616 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
2618 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2619 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2621 USETW(sc->sc_hub_desc.stat.wStatus, 0);
2623 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2624 if (value >= UHCI_MAX_DEVICES) {
2625 err = USB_ERR_IOERROR;
2628 sc->sc_addr = value;
2630 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2631 if ((value != 0) && (value != 1)) {
2632 err = USB_ERR_IOERROR;
2635 sc->sc_conf = value;
2637 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2639 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2640 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2641 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2642 err = USB_ERR_IOERROR;
2644 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2646 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2649 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2651 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2652 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
2653 "port=%d feature=%d\n",
2656 port = UHCI_PORTSC1;
2657 else if (index == 2)
2658 port = UHCI_PORTSC2;
2660 err = USB_ERR_IOERROR;
2664 case UHF_PORT_ENABLE:
2665 x = URWMASK(UREAD2(sc, port));
2666 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2668 case UHF_PORT_SUSPEND:
2669 x = URWMASK(UREAD2(sc, port));
2670 UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
2672 case UHF_PORT_RESET:
2673 x = URWMASK(UREAD2(sc, port));
2674 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2676 case UHF_C_PORT_CONNECTION:
2677 x = URWMASK(UREAD2(sc, port));
2678 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2680 case UHF_C_PORT_ENABLE:
2681 x = URWMASK(UREAD2(sc, port));
2682 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2684 case UHF_C_PORT_OVER_CURRENT:
2685 x = URWMASK(UREAD2(sc, port));
2686 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2688 case UHF_C_PORT_RESET:
2690 err = USB_ERR_NORMAL_COMPLETION;
2692 case UHF_C_PORT_SUSPEND:
2693 sc->sc_isresumed &= ~(1 << index);
2695 case UHF_PORT_CONNECTION:
2696 case UHF_PORT_OVER_CURRENT:
2697 case UHF_PORT_POWER:
2698 case UHF_PORT_LOW_SPEED:
2700 err = USB_ERR_IOERROR;
2704 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2706 port = UHCI_PORTSC1;
2707 else if (index == 2)
2708 port = UHCI_PORTSC2;
2710 err = USB_ERR_IOERROR;
2714 sc->sc_hub_desc.temp[0] =
2715 ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2716 UHCI_PORTSC_LS_SHIFT);
2718 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2719 if ((value & 0xff) != 0) {
2720 err = USB_ERR_IOERROR;
2723 len = sizeof(uhci_hubd_piix);
2724 ptr = (const void *)&uhci_hubd_piix;
2726 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2728 bzero(sc->sc_hub_desc.temp, 16);
2730 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2732 port = UHCI_PORTSC1;
2733 else if (index == 2)
2734 port = UHCI_PORTSC2;
2736 err = USB_ERR_IOERROR;
2739 x = UREAD2(sc, port);
2740 status = change = 0;
2741 if (x & UHCI_PORTSC_CCS)
2742 status |= UPS_CURRENT_CONNECT_STATUS;
2743 if (x & UHCI_PORTSC_CSC)
2744 change |= UPS_C_CONNECT_STATUS;
2745 if (x & UHCI_PORTSC_PE)
2746 status |= UPS_PORT_ENABLED;
2747 if (x & UHCI_PORTSC_POEDC)
2748 change |= UPS_C_PORT_ENABLED;
2749 if (x & UHCI_PORTSC_OCI)
2750 status |= UPS_OVERCURRENT_INDICATOR;
2751 if (x & UHCI_PORTSC_OCIC)
2752 change |= UPS_C_OVERCURRENT_INDICATOR;
2753 if (x & UHCI_PORTSC_LSDA)
2754 status |= UPS_LOW_SPEED;
2755 if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
2756 /* need to do a write back */
2757 UWRITE2(sc, port, URWMASK(x));
2759 /* wait 20ms for resume sequence to complete */
2760 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
2762 /* clear suspend and resume detect */
2763 UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
2766 /* wait a little bit */
2767 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
2769 sc->sc_isresumed |= (1 << index);
2771 } else if (x & UHCI_PORTSC_SUSP) {
2772 status |= UPS_SUSPEND;
2774 status |= UPS_PORT_POWER;
2775 if (sc->sc_isresumed & (1 << index))
2776 change |= UPS_C_SUSPEND;
2778 change |= UPS_C_PORT_RESET;
2779 USETW(sc->sc_hub_desc.ps.wPortStatus, status);
2780 USETW(sc->sc_hub_desc.ps.wPortChange, change);
2781 len = sizeof(sc->sc_hub_desc.ps);
2783 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2784 err = USB_ERR_IOERROR;
2786 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2788 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2790 port = UHCI_PORTSC1;
2791 else if (index == 2)
2792 port = UHCI_PORTSC2;
2794 err = USB_ERR_IOERROR;
2798 case UHF_PORT_ENABLE:
2799 x = URWMASK(UREAD2(sc, port));
2800 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2802 case UHF_PORT_SUSPEND:
2803 x = URWMASK(UREAD2(sc, port));
2804 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2806 case UHF_PORT_RESET:
2807 err = uhci_portreset(sc, index);
2809 case UHF_PORT_POWER:
2810 /* pretend we turned on power */
2811 err = USB_ERR_NORMAL_COMPLETION;
2813 case UHF_C_PORT_CONNECTION:
2814 case UHF_C_PORT_ENABLE:
2815 case UHF_C_PORT_OVER_CURRENT:
2816 case UHF_PORT_CONNECTION:
2817 case UHF_PORT_OVER_CURRENT:
2818 case UHF_PORT_LOW_SPEED:
2819 case UHF_C_PORT_SUSPEND:
2820 case UHF_C_PORT_RESET:
2822 err = USB_ERR_IOERROR;
2827 err = USB_ERR_IOERROR;
2837 * This routine is executed periodically and simulates interrupts from
2838 * the root controller interrupt pipe for port status change:
2841 uhci_root_intr(uhci_softc_t *sc)
2845 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2847 sc->sc_hub_idata[0] = 0;
2849 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
2850 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2851 sc->sc_hub_idata[0] |= 1 << 1;
2853 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
2854 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2855 sc->sc_hub_idata[0] |= 1 << 2;
2859 usb_callout_reset(&sc->sc_root_intr, hz,
2860 (void *)&uhci_root_intr, sc);
2862 if (sc->sc_hub_idata[0] != 0) {
2863 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2864 sizeof(sc->sc_hub_idata));
2869 uhci_xfer_setup(struct usb_setup_params *parm)
2871 struct usb_page_search page_info;
2872 struct usb_page_cache *pc;
2874 struct usb_xfer *xfer;
2882 sc = UHCI_BUS2SC(parm->udev->bus);
2883 xfer = parm->curr_xfer;
2885 parm->hc_max_packet_size = 0x500;
2886 parm->hc_max_packet_count = 1;
2887 parm->hc_max_frame_size = 0x500;
2890 * compute ntd and nqh
2892 if (parm->methods == &uhci_device_ctrl_methods) {
2893 xfer->flags_int.bdma_enable = 1;
2894 xfer->flags_int.bdma_no_post_sync = 1;
2896 usbd_transfer_setup_sub(parm);
2898 /* see EHCI HC driver for proof of "ntd" formula */
2901 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
2902 + (xfer->max_data_length / xfer->max_frame_size));
2904 } else if (parm->methods == &uhci_device_bulk_methods) {
2905 xfer->flags_int.bdma_enable = 1;
2906 xfer->flags_int.bdma_no_post_sync = 1;
2908 usbd_transfer_setup_sub(parm);
2911 ntd = ((2 * xfer->nframes)
2912 + (xfer->max_data_length / xfer->max_frame_size));
2914 } else if (parm->methods == &uhci_device_intr_methods) {
2915 xfer->flags_int.bdma_enable = 1;
2916 xfer->flags_int.bdma_no_post_sync = 1;
2918 usbd_transfer_setup_sub(parm);
2921 ntd = ((2 * xfer->nframes)
2922 + (xfer->max_data_length / xfer->max_frame_size));
2924 } else if (parm->methods == &uhci_device_isoc_methods) {
2925 xfer->flags_int.bdma_enable = 1;
2926 xfer->flags_int.bdma_no_post_sync = 1;
2928 usbd_transfer_setup_sub(parm);
2931 ntd = xfer->nframes;
2935 usbd_transfer_setup_sub(parm);
2945 * NOTE: the UHCI controller requires that
2946 * every packet must be contiguous on
2947 * the same USB memory page !
2949 nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
2952 * Compute a suitable power of two alignment
2953 * for our "max_frame_size" fixup buffer(s):
2955 align = xfer->max_frame_size;
2962 /* check for power of two */
2963 if (!(xfer->max_frame_size &
2964 (xfer->max_frame_size - 1))) {
2968 * We don't allow alignments of
2969 * less than 8 bytes:
2971 * NOTE: Allocating using an aligment
2972 * of 1 byte has special meaning!
2979 if (usbd_transfer_setup_sub_malloc(
2980 parm, &pc, xfer->max_frame_size,
2982 parm->err = USB_ERR_NOMEM;
2985 xfer->buf_fixup = pc;
2994 if (usbd_transfer_setup_sub_malloc(
2995 parm, &pc, sizeof(uhci_td_t),
2996 UHCI_TD_ALIGN, ntd)) {
2997 parm->err = USB_ERR_NOMEM;
3001 for (n = 0; n != ntd; n++) {
3004 usbd_get_page(pc + n, 0, &page_info);
3006 td = page_info.buffer;
3009 if ((parm->methods == &uhci_device_bulk_methods) ||
3010 (parm->methods == &uhci_device_ctrl_methods) ||
3011 (parm->methods == &uhci_device_intr_methods)) {
3012 /* set depth first bit */
3013 td->td_self = htole32(page_info.physaddr |
3014 UHCI_PTR_TD | UHCI_PTR_VF);
3016 td->td_self = htole32(page_info.physaddr |
3020 td->obj_next = last_obj;
3021 td->page_cache = pc + n;
3025 usb_pc_cpu_flush(pc + n);
3028 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3032 if (usbd_transfer_setup_sub_malloc(
3033 parm, &pc, sizeof(uhci_qh_t),
3034 UHCI_QH_ALIGN, nqh)) {
3035 parm->err = USB_ERR_NOMEM;
3039 for (n = 0; n != nqh; n++) {
3042 usbd_get_page(pc + n, 0, &page_info);
3044 qh = page_info.buffer;
3047 qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
3048 qh->obj_next = last_obj;
3049 qh->page_cache = pc + n;
3053 usb_pc_cpu_flush(pc + n);
3056 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3058 if (!xfer->flags_int.curr_dma_set) {
3059 xfer->flags_int.curr_dma_set = 1;
3065 uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3066 struct usb_endpoint *ep)
3068 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
3070 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3072 edesc->bEndpointAddress, udev->flags.usb_mode,
3075 if (udev->flags.usb_mode != USB_MODE_HOST) {
3079 if (udev->device_index != sc->sc_addr) {
3080 switch (edesc->bmAttributes & UE_XFERTYPE) {
3082 ep->methods = &uhci_device_ctrl_methods;
3085 ep->methods = &uhci_device_intr_methods;
3087 case UE_ISOCHRONOUS:
3088 if (udev->speed == USB_SPEED_FULL) {
3089 ep->methods = &uhci_device_isoc_methods;
3093 if (udev->speed != USB_SPEED_LOW) {
3094 ep->methods = &uhci_device_bulk_methods;
3105 uhci_xfer_unsetup(struct usb_xfer *xfer)
3111 uhci_get_dma_delay(struct usb_bus *bus, uint32_t *pus)
3114 * Wait until hardware has finished any possible use of the
3115 * transfer descriptor(s) and QH
3117 *pus = (1125); /* microseconds */
3121 uhci_device_resume(struct usb_device *udev)
3123 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3124 struct usb_xfer *xfer;
3125 struct usb_pipe_methods *methods;
3130 USB_BUS_LOCK(udev->bus);
3132 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3134 if (xfer->xroot->udev == udev) {
3136 methods = xfer->endpoint->methods;
3137 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3139 if (methods == &uhci_device_bulk_methods) {
3140 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
3142 xfer->flags_int.bandwidth_reclaimed = 1;
3144 if (methods == &uhci_device_ctrl_methods) {
3145 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3146 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
3148 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
3151 if (methods == &uhci_device_intr_methods) {
3152 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3157 USB_BUS_UNLOCK(udev->bus);
3163 uhci_device_suspend(struct usb_device *udev)
3165 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3166 struct usb_xfer *xfer;
3167 struct usb_pipe_methods *methods;
3172 USB_BUS_LOCK(udev->bus);
3174 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3176 if (xfer->xroot->udev == udev) {
3178 methods = xfer->endpoint->methods;
3179 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3181 if (xfer->flags_int.bandwidth_reclaimed) {
3182 xfer->flags_int.bandwidth_reclaimed = 0;
3185 if (methods == &uhci_device_bulk_methods) {
3186 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
3188 if (methods == &uhci_device_ctrl_methods) {
3189 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3190 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
3192 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
3195 if (methods == &uhci_device_intr_methods) {
3196 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3201 USB_BUS_UNLOCK(udev->bus);
3207 uhci_set_hw_power(struct usb_bus *bus)
3209 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3216 flags = bus->hw_power_state;
3219 * WARNING: Some FULL speed USB devices require periodic SOF
3220 * messages! If any USB devices are connected through the
3221 * UHCI, power save will be disabled!
3223 if (flags & (USB_HW_POWER_CONTROL |
3224 USB_HW_POWER_NON_ROOT_HUB |
3226 USB_HW_POWER_INTERRUPT |
3227 USB_HW_POWER_ISOC)) {
3228 DPRINTF("Some USB transfer is "
3229 "active on unit %u.\n",
3230 device_get_unit(sc->sc_bus.bdev));
3233 DPRINTF("Power save on unit %u.\n",
3234 device_get_unit(sc->sc_bus.bdev));
3235 UHCICMD(sc, UHCI_CMD_MAXP);
3238 USB_BUS_UNLOCK(bus);
3244 struct usb_bus_methods uhci_bus_methods =
3246 .endpoint_init = uhci_ep_init,
3247 .xfer_setup = uhci_xfer_setup,
3248 .xfer_unsetup = uhci_xfer_unsetup,
3249 .get_dma_delay = uhci_get_dma_delay,
3250 .device_resume = uhci_device_resume,
3251 .device_suspend = uhci_device_suspend,
3252 .set_hw_power = uhci_set_hw_power,
3253 .roothub_exec = uhci_roothub_exec,
3254 .xfer_poll = uhci_do_poll,