3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
7 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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 #ifdef USB_GLOBAL_INCLUDE_FILE
42 #include USB_GLOBAL_INCLUDE_FILE
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
66 #define USB_DEBUG_VAR uhcidebug
68 #include <dev/usb/usb_core.h>
69 #include <dev/usb/usb_debug.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_process.h>
72 #include <dev/usb/usb_transfer.h>
73 #include <dev/usb/usb_device.h>
74 #include <dev/usb/usb_hub.h>
75 #include <dev/usb/usb_util.h>
77 #include <dev/usb/usb_controller.h>
78 #include <dev/usb/usb_bus.h>
79 #endif /* USB_GLOBAL_INCLUDE_FILE */
81 #include <dev/usb/controller/uhci.h>
82 #include <dev/usb/controller/uhcireg.h>
85 #define UHCI_BUS2SC(bus) \
86 __containerof(bus, uhci_softc_t, sc_bus)
89 static int uhcidebug = 0;
90 static int uhcinoloop = 0;
92 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
94 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RWTUN,
95 &uhcidebug, 0, "uhci debug level");
96 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RWTUN,
97 &uhcinoloop, 0, "uhci noloop");
99 static void uhci_dumpregs(uhci_softc_t *sc);
100 static void uhci_dump_tds(uhci_td_t *td);
104 #define UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
105 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
106 #define UWRITE1(sc, r, x) \
107 do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
108 } while (/*CONSTCOND*/0)
109 #define UWRITE2(sc, r, x) \
110 do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
111 } while (/*CONSTCOND*/0)
112 #define UWRITE4(sc, r, x) \
113 do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
114 } while (/*CONSTCOND*/0)
115 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
116 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
117 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
119 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
120 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
122 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
124 #define UHCI_INTR_ENDPT 1
126 struct uhci_mem_layout {
127 struct usb_page_search buf_res;
128 struct usb_page_search fix_res;
130 struct usb_page_cache *buf_pc;
131 struct usb_page_cache *fix_pc;
135 uint16_t max_frame_size;
138 struct uhci_std_temp {
139 struct uhci_mem_layout ml;
146 uint16_t max_frame_size;
148 uint8_t setup_alt_next;
152 static const struct usb_bus_methods uhci_bus_methods;
153 static const struct usb_pipe_methods uhci_device_bulk_methods;
154 static const struct usb_pipe_methods uhci_device_ctrl_methods;
155 static const struct usb_pipe_methods uhci_device_intr_methods;
156 static const struct usb_pipe_methods uhci_device_isoc_methods;
158 static uint8_t uhci_restart(uhci_softc_t *sc);
159 static void uhci_do_poll(struct usb_bus *);
160 static void uhci_device_done(struct usb_xfer *, usb_error_t);
161 static void uhci_transfer_intr_enqueue(struct usb_xfer *);
162 static void uhci_timeout(void *);
163 static uint8_t uhci_check_transfer(struct usb_xfer *);
164 static void uhci_root_intr(uhci_softc_t *sc);
167 uhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
169 struct uhci_softc *sc = UHCI_BUS2SC(bus);
172 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
173 sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
175 cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
176 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
178 cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
179 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
181 cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
182 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
184 cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
185 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
187 cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
188 sizeof(uhci_td_t), UHCI_TD_ALIGN);
190 for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
191 cb(bus, sc->sc_hw.isoc_start_pc + i,
192 sc->sc_hw.isoc_start_pg + i,
193 sizeof(uhci_td_t), UHCI_TD_ALIGN);
196 for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
197 cb(bus, sc->sc_hw.intr_start_pc + i,
198 sc->sc_hw.intr_start_pg + i,
199 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
204 uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb_xfer *xfer)
206 ml->buf_pc = xfer->frbuffers + 0;
207 ml->fix_pc = xfer->buf_fixup;
211 ml->max_frame_size = xfer->max_frame_size;
215 uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
217 usbd_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
219 if (ml->buf_res.length < td->len) {
220 /* need to do a fixup */
222 usbd_get_page(ml->fix_pc, 0, &ml->fix_res);
224 td->td_buffer = htole32(ml->fix_res.physaddr);
227 * The UHCI driver cannot handle
228 * page crossings, so a fixup is
241 if ((td->td_token & htole32(UHCI_TD_PID)) ==
242 htole32(UHCI_TD_PID_IN)) {
243 td->fix_pc = ml->fix_pc;
244 usb_pc_cpu_invalidate(ml->fix_pc);
249 /* copy data to fixup location */
251 usbd_copy_out(ml->buf_pc, ml->buf_offset,
252 ml->fix_res.buffer, td->len);
254 usb_pc_cpu_flush(ml->fix_pc);
257 /* prepare next fixup */
262 td->td_buffer = htole32(ml->buf_res.physaddr);
266 /* prepare next data location */
268 ml->buf_offset += td->len;
277 uhci_restart(uhci_softc_t *sc)
279 struct usb_page_search buf_res;
281 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
283 if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) {
284 DPRINTFN(2, "Already started\n");
288 DPRINTFN(2, "Restarting\n");
290 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
292 /* Reload fresh base address */
293 UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
296 * Assume 64 byte packets at frame end and start HC controller:
298 UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
300 /* wait 10 milliseconds */
302 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
304 /* check that controller has started */
306 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
307 DPRINTFN(2, "Failed\n");
314 uhci_reset(uhci_softc_t *sc)
318 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
320 DPRINTF("resetting the HC\n");
322 /* disable interrupts */
324 UWRITE2(sc, UHCI_INTR, 0);
328 UHCICMD(sc, UHCI_CMD_GRESET);
332 usb_pause_mtx(&sc->sc_bus.bus_mtx,
333 USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
335 /* terminate all transfers */
337 UHCICMD(sc, UHCI_CMD_HCRESET);
339 /* the reset bit goes low when the controller is done */
341 n = UHCI_RESET_TIMEOUT;
343 /* wait one millisecond */
345 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
347 if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
352 device_printf(sc->sc_bus.bdev,
353 "controller did not reset\n");
359 /* wait one millisecond */
361 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
363 /* check if HC is stopped */
364 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
369 device_printf(sc->sc_bus.bdev,
370 "controller did not stop\n");
374 /* reset frame number */
375 UWRITE2(sc, UHCI_FRNUM, 0);
376 /* set default SOF value */
377 UWRITE1(sc, UHCI_SOF, 0x40);
379 USB_BUS_UNLOCK(&sc->sc_bus);
381 /* stop root interrupt */
382 usb_callout_drain(&sc->sc_root_intr);
384 USB_BUS_LOCK(&sc->sc_bus);
388 uhci_start(uhci_softc_t *sc)
390 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
392 DPRINTFN(2, "enabling\n");
394 /* enable interrupts */
396 UWRITE2(sc, UHCI_INTR,
402 if (uhci_restart(sc)) {
403 device_printf(sc->sc_bus.bdev,
404 "cannot start HC controller\n");
407 /* start root interrupt */
411 static struct uhci_qh *
412 uhci_init_qh(struct usb_page_cache *pc)
414 struct usb_page_search buf_res;
417 usbd_get_page(pc, 0, &buf_res);
422 htole32(buf_res.physaddr) |
423 htole32(UHCI_PTR_QH);
430 static struct uhci_td *
431 uhci_init_td(struct usb_page_cache *pc)
433 struct usb_page_search buf_res;
436 usbd_get_page(pc, 0, &buf_res);
441 htole32(buf_res.physaddr) |
442 htole32(UHCI_PTR_TD);
450 uhci_init(uhci_softc_t *sc)
458 usb_callout_init_mtx(&sc->sc_root_intr, &sc->sc_bus.bus_mtx, 0);
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++) {
546 td_x = sc->sc_isoc_p_last[x];
547 qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)];
549 /* start TD for isochronous traffic */
551 td_x->td_next = qh_intr->qh_self;
552 td_x->td_status = htole32(UHCI_TD_IOS);
553 td_x->td_token = htole32(0);
554 td_x->td_buffer = htole32(0);
561 qh_ls = sc->sc_ls_ctl_p_last;
562 qh_fs = sc->sc_fs_ctl_p_last;
564 /* start QH where low speed control traffic will be queued */
565 qh_ls->h_next = qh_fs;
566 qh_ls->qh_h_next = qh_fs->qh_self;
568 qh_ls->qh_e_next = htole32(UHCI_PTR_T);
576 qh_ctl = sc->sc_fs_ctl_p_last;
577 qh_blk = sc->sc_bulk_p_last;
579 /* start QH where full speed control traffic will be queued */
580 qh_ctl->h_next = qh_blk;
581 qh_ctl->qh_h_next = qh_blk->qh_self;
583 qh_ctl->qh_e_next = htole32(UHCI_PTR_T);
585 qh_lst = sc->sc_last_qh_p;
587 /* start QH where bulk traffic will be queued */
588 qh_blk->h_next = qh_lst;
589 qh_blk->qh_h_next = qh_lst->qh_self;
591 qh_blk->qh_e_next = htole32(UHCI_PTR_T);
593 td_lst = sc->sc_last_td_p;
595 /* end QH which is used for looping the QHs */
597 qh_lst->qh_h_next = htole32(UHCI_PTR_T); /* end of QH chain */
598 qh_lst->e_next = td_lst;
599 qh_lst->qh_e_next = td_lst->td_self;
602 * end TD which hangs from the last QH, to avoid a bug in the PIIX
603 * that makes it run berserk otherwise
606 td_lst->td_next = htole32(UHCI_PTR_T);
607 td_lst->td_status = htole32(0); /* inactive */
608 td_lst->td_token = htole32(0);
609 td_lst->td_buffer = htole32(0);
612 struct usb_page_search buf_res;
615 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
617 pframes = buf_res.buffer;
620 * Setup UHCI framelist
624 * pframes -> full speed isochronous -> interrupt QH's -> low
625 * speed control -> full speed control -> bulk transfers
629 for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
631 sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
634 /* flush all cache into memory */
636 usb_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
638 /* set up the bus struct */
639 sc->sc_bus.methods = &uhci_bus_methods;
641 USB_BUS_LOCK(&sc->sc_bus);
642 /* reset the controller */
645 /* start the controller */
647 USB_BUS_UNLOCK(&sc->sc_bus);
649 /* catch lost interrupts */
650 uhci_do_poll(&sc->sc_bus);
656 uhci_suspend(uhci_softc_t *sc)
664 USB_BUS_LOCK(&sc->sc_bus);
666 /* stop the controller */
670 /* enter global suspend */
672 UHCICMD(sc, UHCI_CMD_EGSM);
674 USB_BUS_UNLOCK(&sc->sc_bus);
678 uhci_resume(uhci_softc_t *sc)
680 USB_BUS_LOCK(&sc->sc_bus);
682 /* reset the controller */
686 /* force global resume */
688 UHCICMD(sc, UHCI_CMD_FGR);
690 /* and start traffic again */
694 USB_BUS_UNLOCK(&sc->sc_bus);
701 /* catch lost interrupts */
702 uhci_do_poll(&sc->sc_bus);
707 uhci_dumpregs(uhci_softc_t *sc)
709 DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
710 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
711 device_get_nameunit(sc->sc_bus.bdev),
712 UREAD2(sc, UHCI_CMD),
713 UREAD2(sc, UHCI_STS),
714 UREAD2(sc, UHCI_INTR),
715 UREAD2(sc, UHCI_FRNUM),
716 UREAD4(sc, UHCI_FLBASEADDR),
717 UREAD1(sc, UHCI_SOF),
718 UREAD2(sc, UHCI_PORTSC1),
719 UREAD2(sc, UHCI_PORTSC2));
723 uhci_dump_td(uhci_td_t *p)
730 usb_pc_cpu_invalidate(p->page_cache);
732 td_next = le32toh(p->td_next);
733 td_status = le32toh(p->td_status);
734 td_token = le32toh(p->td_token);
737 * Check whether the link pointer in this TD marks the link pointer
740 temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
742 printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
743 "token=0x%08x buffer=0x%08x\n",
749 le32toh(p->td_buffer));
751 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,"
752 "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
754 (td_next & 1) ? "-T" : "",
755 (td_next & 2) ? "-Q" : "",
756 (td_next & 4) ? "-VF" : "",
757 (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
758 (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
759 (td_status & UHCI_TD_NAK) ? "-NAK" : "",
760 (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
761 (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
762 (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
763 (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
764 (td_status & UHCI_TD_IOC) ? "-IOC" : "",
765 (td_status & UHCI_TD_IOS) ? "-IOS" : "",
766 (td_status & UHCI_TD_LS) ? "-LS" : "",
767 (td_status & UHCI_TD_SPD) ? "-SPD" : "",
768 UHCI_TD_GET_ERRCNT(td_status),
769 UHCI_TD_GET_ACTLEN(td_status),
770 UHCI_TD_GET_PID(td_token),
771 UHCI_TD_GET_DEVADDR(td_token),
772 UHCI_TD_GET_ENDPT(td_token),
773 UHCI_TD_GET_DT(td_token),
774 UHCI_TD_GET_MAXLEN(td_token));
780 uhci_dump_qh(uhci_qh_t *sqh)
786 usb_pc_cpu_invalidate(sqh->page_cache);
788 qh_h_next = le32toh(sqh->qh_h_next);
789 qh_e_next = le32toh(sqh->qh_e_next);
791 DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
792 le32toh(sqh->qh_self), qh_h_next, qh_e_next);
794 temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
795 (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
801 uhci_dump_all(uhci_softc_t *sc)
804 uhci_dump_qh(sc->sc_ls_ctl_p_last);
805 uhci_dump_qh(sc->sc_fs_ctl_p_last);
806 uhci_dump_qh(sc->sc_bulk_p_last);
807 uhci_dump_qh(sc->sc_last_qh_p);
811 uhci_dump_tds(uhci_td_t *td)
816 if (uhci_dump_td(td)) {
825 * Let the last QH loop back to the full speed control transfer QH.
826 * This is what intel calls "bandwidth reclamation" and improves
827 * USB performance a lot for some devices.
828 * If we are already looping, just count it.
831 uhci_add_loop(uhci_softc_t *sc)
833 struct uhci_qh *qh_lst;
834 struct uhci_qh *qh_rec;
841 if (++(sc->sc_loops) == 1) {
842 DPRINTFN(6, "add\n");
844 qh_lst = sc->sc_last_qh_p;
845 qh_rec = sc->sc_reclaim_qh_p;
847 /* NOTE: we don't loop back the soft pointer */
849 qh_lst->qh_h_next = qh_rec->qh_self;
850 usb_pc_cpu_flush(qh_lst->page_cache);
855 uhci_rem_loop(uhci_softc_t *sc)
857 struct uhci_qh *qh_lst;
864 if (--(sc->sc_loops) == 0) {
865 DPRINTFN(6, "remove\n");
867 qh_lst = sc->sc_last_qh_p;
868 qh_lst->qh_h_next = htole32(UHCI_PTR_T);
869 usb_pc_cpu_flush(qh_lst->page_cache);
874 uhci_transfer_intr_enqueue(struct usb_xfer *xfer)
876 /* check for early completion */
877 if (uhci_check_transfer(xfer)) {
880 /* put transfer on interrupt queue */
881 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
883 /* start timeout, if any */
884 if (xfer->timeout != 0) {
885 usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
889 #define UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
891 _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
893 DPRINTFN(11, "%p to %p\n", std, last);
895 /* (sc->sc_bus.mtx) must be locked */
897 std->next = last->next;
898 std->td_next = last->td_next;
902 usb_pc_cpu_flush(std->page_cache);
905 * the last->next->prev is never followed: std->next->prev = std;
908 last->td_next = std->td_self;
910 usb_pc_cpu_flush(last->page_cache);
915 #define UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
917 _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
919 DPRINTFN(11, "%p to %p\n", sqh, last);
921 if (sqh->h_prev != NULL) {
922 /* should not happen */
923 DPRINTFN(0, "QH already linked!\n");
926 /* (sc->sc_bus.mtx) must be locked */
928 sqh->h_next = last->h_next;
929 sqh->qh_h_next = last->qh_h_next;
933 usb_pc_cpu_flush(sqh->page_cache);
936 * The "last->h_next->h_prev" is never followed:
938 * "sqh->h_next->h_prev" = sqh;
942 last->qh_h_next = sqh->qh_self;
944 usb_pc_cpu_flush(last->page_cache);
951 #define UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
953 _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
955 DPRINTFN(11, "%p from %p\n", std, last);
957 /* (sc->sc_bus.mtx) must be locked */
959 std->prev->next = std->next;
960 std->prev->td_next = std->td_next;
962 usb_pc_cpu_flush(std->prev->page_cache);
965 std->next->prev = std->prev;
966 usb_pc_cpu_flush(std->next->page_cache);
968 return ((last == std) ? std->prev : last);
971 #define UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
973 _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
975 DPRINTFN(11, "%p from %p\n", sqh, last);
977 /* (sc->sc_bus.mtx) must be locked */
979 /* only remove if not removed from a queue */
981 sqh->h_prev->h_next = sqh->h_next;
982 sqh->h_prev->qh_h_next = sqh->qh_h_next;
984 usb_pc_cpu_flush(sqh->h_prev->page_cache);
987 sqh->h_next->h_prev = sqh->h_prev;
988 usb_pc_cpu_flush(sqh->h_next->page_cache);
990 last = ((last == sqh) ? sqh->h_prev : last);
994 usb_pc_cpu_flush(sqh->page_cache);
1000 uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer)
1002 struct usb_page_search res;
1003 uint32_t nframes = xfer->nframes;
1005 uint32_t offset = 0;
1006 uint32_t *plen = xfer->frlengths;
1008 uhci_td_t *td = xfer->td_transfer_first;
1009 uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
1011 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1012 xfer, xfer->endpoint);
1014 /* sync any DMA memory before doing fixups */
1016 usb_bdma_post_sync(xfer);
1020 panic("%s:%d: out of TD's\n",
1021 __FUNCTION__, __LINE__);
1023 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
1024 pp_last = &sc->sc_isoc_p_last[0];
1027 if (uhcidebug > 5) {
1028 DPRINTF("isoc TD\n");
1032 usb_pc_cpu_invalidate(td->page_cache);
1033 status = le32toh(td->td_status);
1035 len = UHCI_TD_GET_ACTLEN(status);
1041 usbd_get_page(td->fix_pc, 0, &res);
1043 /* copy data from fixup location to real location */
1045 usb_pc_cpu_invalidate(td->fix_pc);
1047 usbd_copy_in(xfer->frbuffers, offset,
1054 /* remove TD from schedule */
1055 UHCI_REMOVE_TD(td, *pp_last);
1062 xfer->aframes = xfer->nframes;
1066 uhci_non_isoc_done_sub(struct usb_xfer *xfer)
1068 struct usb_page_search res;
1070 uhci_td_t *td_alt_next;
1075 td = xfer->td_transfer_cache;
1076 td_alt_next = td->alt_next;
1078 if (xfer->aframes != xfer->nframes) {
1079 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1082 usb_pc_cpu_invalidate(td->page_cache);
1083 status = le32toh(td->td_status);
1084 token = le32toh(td->td_token);
1087 * Verify the status and add
1088 * up the actual length:
1091 len = UHCI_TD_GET_ACTLEN(status);
1092 if (len > td->len) {
1093 /* should not happen */
1094 DPRINTF("Invalid status length, "
1095 "0x%04x/0x%04x bytes\n", len, td->len);
1096 status |= UHCI_TD_STALLED;
1098 } else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
1100 usbd_get_page(td->fix_pc, 0, &res);
1103 * copy data from fixup location to real
1107 usb_pc_cpu_invalidate(td->fix_pc);
1109 usbd_copy_in(xfer->frbuffers + xfer->aframes,
1110 xfer->frlengths[xfer->aframes], res.buffer, len);
1112 /* update actual length */
1114 xfer->frlengths[xfer->aframes] += len;
1116 /* Check for last transfer */
1117 if (((void *)td) == xfer->td_transfer_last) {
1121 if (status & UHCI_TD_STALLED) {
1122 /* the transfer is finished */
1126 /* Check for short transfer */
1127 if (len != td->len) {
1128 if (xfer->flags_int.short_frames_ok) {
1129 /* follow alt next */
1132 /* the transfer is finished */
1139 if (td->alt_next != td_alt_next) {
1140 /* this USB frame is complete */
1145 /* update transfer cache */
1147 xfer->td_transfer_cache = td;
1149 /* update data toggle */
1151 xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
1154 if (status & UHCI_TD_ERROR) {
1155 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
1156 "status=%s%s%s%s%s%s%s%s%s%s%s\n",
1157 xfer->address, xfer->endpointno, xfer->aframes,
1158 (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
1159 (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
1160 (status & UHCI_TD_NAK) ? "[NAK]" : "",
1161 (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
1162 (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
1163 (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
1164 (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1165 (status & UHCI_TD_IOC) ? "[IOC]" : "",
1166 (status & UHCI_TD_IOS) ? "[IOS]" : "",
1167 (status & UHCI_TD_LS) ? "[LS]" : "",
1168 (status & UHCI_TD_SPD) ? "[SPD]" : "");
1171 if (status & UHCI_TD_STALLED) {
1172 /* try to separate I/O errors from STALL */
1173 if (UHCI_TD_GET_ERRCNT(status) == 0)
1174 return (USB_ERR_IOERROR);
1175 return (USB_ERR_STALLED);
1177 return (USB_ERR_NORMAL_COMPLETION);
1181 uhci_non_isoc_done(struct usb_xfer *xfer)
1183 usb_error_t err = 0;
1185 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1186 xfer, xfer->endpoint);
1189 if (uhcidebug > 10) {
1190 uhci_dump_tds(xfer->td_transfer_first);
1194 /* sync any DMA memory before doing fixups */
1196 usb_bdma_post_sync(xfer);
1200 xfer->td_transfer_cache = xfer->td_transfer_first;
1202 if (xfer->flags_int.control_xfr) {
1203 if (xfer->flags_int.control_hdr) {
1204 err = uhci_non_isoc_done_sub(xfer);
1208 if (xfer->td_transfer_cache == NULL) {
1212 while (xfer->aframes != xfer->nframes) {
1213 err = uhci_non_isoc_done_sub(xfer);
1216 if (xfer->td_transfer_cache == NULL) {
1221 if (xfer->flags_int.control_xfr &&
1222 !xfer->flags_int.control_act) {
1223 err = uhci_non_isoc_done_sub(xfer);
1226 uhci_device_done(xfer, err);
1229 /*------------------------------------------------------------------------*
1230 * uhci_check_transfer_sub
1232 * The main purpose of this function is to update the data-toggle
1233 * in case it is wrong.
1234 *------------------------------------------------------------------------*/
1236 uhci_check_transfer_sub(struct usb_xfer *xfer)
1240 uhci_td_t *td_alt_next;
1245 td = xfer->td_transfer_cache;
1246 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1248 td_token = td->obj_next->td_token;
1250 xfer->td_transfer_cache = td;
1251 td_self = td->td_self;
1252 td_alt_next = td->alt_next;
1254 if (xfer->flags_int.control_xfr)
1255 goto skip; /* don't touch the DT value! */
1257 if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))))
1258 goto skip; /* data toggle has correct value */
1261 * The data toggle is wrong and we need to toggle it !
1264 td->td_token ^= htole32(UHCI_TD_SET_DT(1));
1265 usb_pc_cpu_flush(td->page_cache);
1267 if (td == xfer->td_transfer_last) {
1273 if (td->alt_next != td_alt_next) {
1281 qh->qh_e_next = td_self;
1282 usb_pc_cpu_flush(qh->page_cache);
1284 DPRINTFN(13, "xfer=%p following alt next\n", xfer);
1287 /*------------------------------------------------------------------------*
1288 * uhci_check_transfer
1291 * 0: USB transfer is not finished
1292 * Else: USB transfer is finished
1293 *------------------------------------------------------------------------*/
1295 uhci_check_transfer(struct usb_xfer *xfer)
1301 DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
1303 if (xfer->endpoint->methods == &uhci_device_isoc_methods) {
1304 /* isochronous transfer */
1306 td = xfer->td_transfer_last;
1308 usb_pc_cpu_invalidate(td->page_cache);
1309 status = le32toh(td->td_status);
1311 /* check also if the first is complete */
1313 td = xfer->td_transfer_first;
1315 usb_pc_cpu_invalidate(td->page_cache);
1316 status |= le32toh(td->td_status);
1318 if (!(status & UHCI_TD_ACTIVE)) {
1319 uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1323 /* non-isochronous transfer */
1326 * check whether there is an error somewhere
1327 * in the middle, or whether there was a short
1328 * packet (SPD and not ACTIVE)
1330 td = xfer->td_transfer_cache;
1333 usb_pc_cpu_invalidate(td->page_cache);
1334 status = le32toh(td->td_status);
1335 token = le32toh(td->td_token);
1338 * if there is an active TD the transfer isn't done
1340 if (status & UHCI_TD_ACTIVE) {
1342 xfer->td_transfer_cache = td;
1346 * last transfer descriptor makes the transfer done
1348 if (((void *)td) == xfer->td_transfer_last) {
1352 * any kind of error makes the transfer done
1354 if (status & UHCI_TD_STALLED) {
1358 * check if we reached the last packet
1359 * or if there is a short packet:
1361 if ((td->td_next == htole32(UHCI_PTR_T)) ||
1362 (UHCI_TD_GET_ACTLEN(status) < td->len)) {
1363 if (xfer->flags_int.short_frames_ok) {
1364 /* follow alt next */
1367 xfer->td_transfer_cache = td;
1368 uhci_check_transfer_sub(xfer);
1372 /* transfer is done */
1377 uhci_non_isoc_done(xfer);
1382 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1390 uhci_interrupt_poll(uhci_softc_t *sc)
1392 struct usb_xfer *xfer;
1395 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1397 * check if transfer is transferred
1399 if (uhci_check_transfer(xfer)) {
1400 /* queue has been modified */
1406 /*------------------------------------------------------------------------*
1407 * uhci_interrupt - UHCI interrupt handler
1409 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1410 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1412 *------------------------------------------------------------------------*/
1414 uhci_interrupt(uhci_softc_t *sc)
1418 USB_BUS_LOCK(&sc->sc_bus);
1420 DPRINTFN(16, "real interrupt\n");
1423 if (uhcidebug > 15) {
1427 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1429 /* the interrupt was not for us */
1432 if (status & (UHCI_STS_RD | UHCI_STS_HSE |
1433 UHCI_STS_HCPE | UHCI_STS_HCH)) {
1434 if (status & UHCI_STS_RD) {
1436 printf("%s: resume detect\n",
1440 if (status & UHCI_STS_HSE) {
1441 printf("%s: host system error\n",
1444 if (status & UHCI_STS_HCPE) {
1445 printf("%s: host controller process error\n",
1448 if (status & UHCI_STS_HCH) {
1449 /* no acknowledge needed */
1450 DPRINTF("%s: host controller halted\n",
1453 if (uhcidebug > 0) {
1459 /* get acknowledge bits */
1460 status &= (UHCI_STS_USBINT |
1468 /* nothing to acknowledge */
1471 /* acknowledge interrupts */
1472 UWRITE2(sc, UHCI_STS, status);
1474 /* poll all the USB transfers */
1475 uhci_interrupt_poll(sc);
1478 USB_BUS_UNLOCK(&sc->sc_bus);
1482 * called when a request does not complete
1485 uhci_timeout(void *arg)
1487 struct usb_xfer *xfer = arg;
1489 DPRINTF("xfer=%p\n", xfer);
1491 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1493 /* transfer is transferred */
1494 uhci_device_done(xfer, USB_ERR_TIMEOUT);
1498 uhci_do_poll(struct usb_bus *bus)
1500 struct uhci_softc *sc = UHCI_BUS2SC(bus);
1502 USB_BUS_LOCK(&sc->sc_bus);
1503 uhci_interrupt_poll(sc);
1504 USB_BUS_UNLOCK(&sc->sc_bus);
1508 uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
1512 uhci_td_t *td_alt_next;
1515 uint8_t shortpkt_old;
1519 shortpkt_old = temp->shortpkt;
1520 len_old = temp->len;
1523 /* software is used to detect short incoming transfers */
1525 if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
1526 temp->td_status |= htole32(UHCI_TD_SPD);
1528 temp->td_status &= ~htole32(UHCI_TD_SPD);
1531 temp->ml.buf_offset = 0;
1535 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1536 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
1539 td_next = temp->td_next;
1542 if (temp->len == 0) {
1543 if (temp->shortpkt) {
1546 /* send a Zero Length Packet, ZLP, last */
1549 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
1553 average = temp->average;
1555 if (temp->len < average) {
1557 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1558 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
1559 average = temp->len;
1563 if (td_next == NULL) {
1564 panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
1569 td_next = td->obj_next;
1571 /* check if we are pre-computing */
1574 /* update remaining length */
1576 temp->len -= average;
1580 /* fill out current TD */
1582 td->td_status = temp->td_status;
1583 td->td_token = temp->td_token;
1585 /* update data toggle */
1587 temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
1595 /* update remaining length */
1597 temp->len -= average;
1601 /* fill out buffer pointer and do fixup, if any */
1603 uhci_mem_layout_fixup(&temp->ml, td);
1606 td->alt_next = td_alt_next;
1608 if ((td_next == td_alt_next) && temp->setup_alt_next) {
1609 /* we need to receive these frames one by one ! */
1610 td->td_status |= htole32(UHCI_TD_IOC);
1611 td->td_next = htole32(UHCI_PTR_T);
1614 /* link the current TD with the next one */
1615 td->td_next = td_next->td_self;
1619 usb_pc_cpu_flush(td->page_cache);
1625 /* setup alt next pointer, if any */
1626 if (temp->last_frame) {
1629 /* we use this field internally */
1630 td_alt_next = td_next;
1634 temp->shortpkt = shortpkt_old;
1635 temp->len = len_old;
1639 temp->td_next = td_next;
1643 uhci_setup_standard_chain(struct usb_xfer *xfer)
1645 struct uhci_std_temp temp;
1649 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1650 xfer->address, UE_GET_ADDR(xfer->endpointno),
1651 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1653 temp.average = xfer->max_frame_size;
1654 temp.max_frame_size = xfer->max_frame_size;
1656 /* toggle the DMA set we are using */
1657 xfer->flags_int.curr_dma_set ^= 1;
1659 /* get next DMA set */
1660 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1661 xfer->td_transfer_first = td;
1662 xfer->td_transfer_cache = td;
1666 temp.last_frame = 0;
1667 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1669 uhci_mem_layout_init(&temp.ml, xfer);
1672 htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
1675 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1676 temp.td_status |= htole32(UHCI_TD_LS);
1679 htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) |
1680 UHCI_TD_SET_DEVADDR(xfer->address));
1682 if (xfer->endpoint->toggle_next) {
1684 temp.td_token |= htole32(UHCI_TD_SET_DT(1));
1686 /* check if we should prepend a setup message */
1688 if (xfer->flags_int.control_xfr) {
1689 if (xfer->flags_int.control_hdr) {
1690 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1691 UHCI_TD_SET_ENDPT(0xF));
1692 temp.td_token |= htole32(UHCI_TD_PID_SETUP |
1695 temp.len = xfer->frlengths[0];
1696 temp.ml.buf_pc = xfer->frbuffers + 0;
1697 temp.shortpkt = temp.len ? 1 : 0;
1698 /* check for last frame */
1699 if (xfer->nframes == 1) {
1700 /* no STATUS stage yet, SETUP is last */
1701 if (xfer->flags_int.control_act) {
1702 temp.last_frame = 1;
1703 temp.setup_alt_next = 0;
1706 uhci_setup_standard_chain_sub(&temp);
1713 while (x != xfer->nframes) {
1714 /* DATA0 / DATA1 message */
1716 temp.len = xfer->frlengths[x];
1717 temp.ml.buf_pc = xfer->frbuffers + x;
1721 if (x == xfer->nframes) {
1722 if (xfer->flags_int.control_xfr) {
1723 /* no STATUS stage yet, DATA is last */
1724 if (xfer->flags_int.control_act) {
1725 temp.last_frame = 1;
1726 temp.setup_alt_next = 0;
1729 temp.last_frame = 1;
1730 temp.setup_alt_next = 0;
1734 * Keep previous data toggle,
1735 * device address and endpoint number:
1738 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1739 UHCI_TD_SET_ENDPT(0xF) |
1742 if (temp.len == 0) {
1743 /* make sure that we send an USB packet */
1748 /* regular data transfer */
1750 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1753 /* set endpoint direction */
1756 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1757 htole32(UHCI_TD_PID_IN) :
1758 htole32(UHCI_TD_PID_OUT);
1760 uhci_setup_standard_chain_sub(&temp);
1763 /* check if we should append a status stage */
1765 if (xfer->flags_int.control_xfr &&
1766 !xfer->flags_int.control_act) {
1768 * send a DATA1 message and reverse the current endpoint
1772 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1773 UHCI_TD_SET_ENDPT(0xF) |
1776 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1777 htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
1778 htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
1781 temp.ml.buf_pc = NULL;
1783 temp.last_frame = 1;
1784 temp.setup_alt_next = 0;
1786 uhci_setup_standard_chain_sub(&temp);
1790 /* Ensure that last TD is terminating: */
1791 td->td_next = htole32(UHCI_PTR_T);
1793 /* set interrupt bit */
1795 td->td_status |= htole32(UHCI_TD_IOC);
1797 usb_pc_cpu_flush(td->page_cache);
1799 /* must have at least one frame! */
1801 xfer->td_transfer_last = td;
1804 if (uhcidebug > 8) {
1805 DPRINTF("nexttog=%d; data before transfer:\n",
1806 xfer->endpoint->toggle_next);
1807 uhci_dump_tds(xfer->td_transfer_first);
1810 return (xfer->td_transfer_first);
1813 /* NOTE: "done" can be run two times in a row,
1814 * from close and from interrupt
1818 uhci_device_done(struct usb_xfer *xfer, usb_error_t error)
1820 const struct usb_pipe_methods *methods = xfer->endpoint->methods;
1821 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1824 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1826 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1827 xfer, xfer->endpoint, error);
1829 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1831 usb_pc_cpu_invalidate(qh->page_cache);
1833 if (xfer->flags_int.bandwidth_reclaimed) {
1834 xfer->flags_int.bandwidth_reclaimed = 0;
1837 if (methods == &uhci_device_bulk_methods) {
1838 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
1840 if (methods == &uhci_device_ctrl_methods) {
1841 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1842 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
1844 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
1847 if (methods == &uhci_device_intr_methods) {
1848 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
1851 * Only finish isochronous transfers once
1852 * which will update "xfer->frlengths".
1854 if (xfer->td_transfer_first &&
1855 xfer->td_transfer_last) {
1856 if (methods == &uhci_device_isoc_methods) {
1857 uhci_isoc_done(sc, xfer);
1859 xfer->td_transfer_first = NULL;
1860 xfer->td_transfer_last = NULL;
1862 /* dequeue transfer and start next transfer */
1863 usbd_transfer_done(xfer, error);
1866 /*------------------------------------------------------------------------*
1868 *------------------------------------------------------------------------*/
1870 uhci_device_bulk_open(struct usb_xfer *xfer)
1876 uhci_device_bulk_close(struct usb_xfer *xfer)
1878 uhci_device_done(xfer, USB_ERR_CANCELLED);
1882 uhci_device_bulk_enter(struct usb_xfer *xfer)
1888 uhci_device_bulk_start(struct usb_xfer *xfer)
1890 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1895 td = uhci_setup_standard_chain(xfer);
1898 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1901 qh->qh_e_next = td->td_self;
1903 if (xfer->xroot->udev->flags.self_suspended == 0) {
1904 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
1906 xfer->flags_int.bandwidth_reclaimed = 1;
1908 usb_pc_cpu_flush(qh->page_cache);
1911 /* put transfer on interrupt queue */
1912 uhci_transfer_intr_enqueue(xfer);
1915 static const struct usb_pipe_methods uhci_device_bulk_methods =
1917 .open = uhci_device_bulk_open,
1918 .close = uhci_device_bulk_close,
1919 .enter = uhci_device_bulk_enter,
1920 .start = uhci_device_bulk_start,
1923 /*------------------------------------------------------------------------*
1924 * uhci control support
1925 *------------------------------------------------------------------------*/
1927 uhci_device_ctrl_open(struct usb_xfer *xfer)
1933 uhci_device_ctrl_close(struct usb_xfer *xfer)
1935 uhci_device_done(xfer, USB_ERR_CANCELLED);
1939 uhci_device_ctrl_enter(struct usb_xfer *xfer)
1945 uhci_device_ctrl_start(struct usb_xfer *xfer)
1947 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1952 td = uhci_setup_standard_chain(xfer);
1955 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1958 qh->qh_e_next = td->td_self;
1961 * NOTE: some devices choke on bandwidth- reclamation for control
1964 if (xfer->xroot->udev->flags.self_suspended == 0) {
1965 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1966 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
1968 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
1971 usb_pc_cpu_flush(qh->page_cache);
1973 /* put transfer on interrupt queue */
1974 uhci_transfer_intr_enqueue(xfer);
1977 static const struct usb_pipe_methods uhci_device_ctrl_methods =
1979 .open = uhci_device_ctrl_open,
1980 .close = uhci_device_ctrl_close,
1981 .enter = uhci_device_ctrl_enter,
1982 .start = uhci_device_ctrl_start,
1985 /*------------------------------------------------------------------------*
1986 * uhci interrupt support
1987 *------------------------------------------------------------------------*/
1989 uhci_device_intr_open(struct usb_xfer *xfer)
1991 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1997 bit = UHCI_IFRAMELIST_COUNT / 2;
1999 if (xfer->interval >= bit) {
2003 if (sc->sc_intr_stat[x] <
2004 sc->sc_intr_stat[best]) {
2014 sc->sc_intr_stat[best]++;
2015 xfer->qh_pos = best;
2017 DPRINTFN(3, "best=%d interval=%d\n",
2018 best, xfer->interval);
2022 uhci_device_intr_close(struct usb_xfer *xfer)
2024 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2026 sc->sc_intr_stat[xfer->qh_pos]--;
2028 uhci_device_done(xfer, USB_ERR_CANCELLED);
2032 uhci_device_intr_enter(struct usb_xfer *xfer)
2038 uhci_device_intr_start(struct usb_xfer *xfer)
2040 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2045 td = uhci_setup_standard_chain(xfer);
2048 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2051 qh->qh_e_next = td->td_self;
2053 if (xfer->xroot->udev->flags.self_suspended == 0) {
2054 /* enter QHs into the controller data structures */
2055 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
2057 usb_pc_cpu_flush(qh->page_cache);
2060 /* put transfer on interrupt queue */
2061 uhci_transfer_intr_enqueue(xfer);
2064 static const struct usb_pipe_methods uhci_device_intr_methods =
2066 .open = uhci_device_intr_open,
2067 .close = uhci_device_intr_close,
2068 .enter = uhci_device_intr_enter,
2069 .start = uhci_device_intr_start,
2072 /*------------------------------------------------------------------------*
2073 * uhci isochronous support
2074 *------------------------------------------------------------------------*/
2076 uhci_device_isoc_open(struct usb_xfer *xfer)
2083 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
2084 UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) :
2085 UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0);
2087 td_token = htole32(td_token);
2089 /* initialize all TD's */
2091 for (ds = 0; ds != 2; ds++) {
2092 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2093 /* mark TD as inactive */
2094 td->td_status = htole32(UHCI_TD_IOS);
2095 td->td_token = td_token;
2097 usb_pc_cpu_flush(td->page_cache);
2103 uhci_device_isoc_close(struct usb_xfer *xfer)
2105 uhci_device_done(xfer, USB_ERR_CANCELLED);
2109 uhci_device_isoc_enter(struct usb_xfer *xfer)
2111 struct uhci_mem_layout ml;
2112 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2122 uhci_td_t *td_last = NULL;
2123 uhci_td_t **pp_last;
2125 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2126 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2128 nframes = UREAD2(sc, UHCI_FRNUM);
2130 temp = (nframes - xfer->endpoint->isoc_next) &
2131 (UHCI_VFRAMELIST_COUNT - 1);
2133 if ((xfer->endpoint->is_synced == 0) ||
2134 (temp < xfer->nframes)) {
2136 * If there is data underflow or the pipe queue is empty we
2137 * schedule the transfer a few frames ahead of the current
2138 * frame position. Else two isochronous transfers might
2141 xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
2142 xfer->endpoint->is_synced = 1;
2143 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2146 * compute how many milliseconds the insertion is ahead of the
2147 * current frame position:
2149 temp = (xfer->endpoint->isoc_next - nframes) &
2150 (UHCI_VFRAMELIST_COUNT - 1);
2153 * pre-compute when the isochronous transfer will be finished:
2155 xfer->isoc_time_complete =
2156 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2159 /* get the real number of frames */
2161 nframes = xfer->nframes;
2163 uhci_mem_layout_init(&ml, xfer);
2165 plen = xfer->frlengths;
2167 /* toggle the DMA set we are using */
2168 xfer->flags_int.curr_dma_set ^= 1;
2170 /* get next DMA set */
2171 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2172 xfer->td_transfer_first = td;
2174 pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next];
2176 /* store starting position */
2178 xfer->qh_pos = xfer->endpoint->isoc_next;
2182 panic("%s:%d: out of TD's\n",
2183 __FUNCTION__, __LINE__);
2185 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
2186 pp_last = &sc->sc_isoc_p_last[0];
2188 if (*plen > xfer->max_frame_size) {
2192 printf("%s: frame length(%d) exceeds %d "
2193 "bytes (frame truncated)\n",
2194 __FUNCTION__, *plen,
2195 xfer->max_frame_size);
2198 *plen = xfer->max_frame_size;
2200 /* reuse td_token from last transfer */
2202 td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2203 td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
2209 * Do not call "uhci_mem_layout_fixup()" when the
2216 /* fill out buffer pointer and do fixup, if any */
2218 uhci_mem_layout_fixup(&ml, td);
2223 td->td_status = htole32
2224 (UHCI_TD_ZERO_ACTLEN
2225 (UHCI_TD_SET_ERRCNT(0) |
2230 td->td_status = htole32
2231 (UHCI_TD_ZERO_ACTLEN
2232 (UHCI_TD_SET_ERRCNT(0) |
2237 usb_pc_cpu_flush(td->page_cache);
2240 if (uhcidebug > 5) {
2241 DPRINTF("TD %d\n", nframes);
2245 /* insert TD into schedule */
2246 UHCI_APPEND_TD(td, *pp_last);
2254 xfer->td_transfer_last = td_last;
2256 /* update isoc_next */
2257 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
2258 (UHCI_VFRAMELIST_COUNT - 1);
2262 uhci_device_isoc_start(struct usb_xfer *xfer)
2264 /* put transfer on interrupt queue */
2265 uhci_transfer_intr_enqueue(xfer);
2268 static const struct usb_pipe_methods uhci_device_isoc_methods =
2270 .open = uhci_device_isoc_open,
2271 .close = uhci_device_isoc_close,
2272 .enter = uhci_device_isoc_enter,
2273 .start = uhci_device_isoc_start,
2276 /*------------------------------------------------------------------------*
2277 * uhci root control support
2278 *------------------------------------------------------------------------*
2279 * Simulate a hardware hub by handling all the necessary requests.
2280 *------------------------------------------------------------------------*/
2283 struct usb_device_descriptor uhci_devd =
2285 sizeof(struct usb_device_descriptor),
2286 UDESC_DEVICE, /* type */
2287 {0x00, 0x01}, /* USB version */
2288 UDCLASS_HUB, /* class */
2289 UDSUBCLASS_HUB, /* subclass */
2290 UDPROTO_FSHUB, /* protocol */
2291 64, /* max packet */
2292 {0}, {0}, {0x00, 0x01}, /* device id */
2293 1, 2, 0, /* string indexes */
2294 1 /* # of configurations */
2297 static const struct uhci_config_desc uhci_confd = {
2299 .bLength = sizeof(struct usb_config_descriptor),
2300 .bDescriptorType = UDESC_CONFIG,
2301 .wTotalLength[0] = sizeof(uhci_confd),
2303 .bConfigurationValue = 1,
2304 .iConfiguration = 0,
2305 .bmAttributes = UC_SELF_POWERED,
2306 .bMaxPower = 0 /* max power */
2309 .bLength = sizeof(struct usb_interface_descriptor),
2310 .bDescriptorType = UDESC_INTERFACE,
2312 .bInterfaceClass = UICLASS_HUB,
2313 .bInterfaceSubClass = UISUBCLASS_HUB,
2314 .bInterfaceProtocol = UIPROTO_FSHUB,
2317 .bLength = sizeof(struct usb_endpoint_descriptor),
2318 .bDescriptorType = UDESC_ENDPOINT,
2319 .bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
2320 .bmAttributes = UE_INTERRUPT,
2321 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
2327 struct usb_hub_descriptor_min uhci_hubd_piix =
2329 .bDescLength = sizeof(uhci_hubd_piix),
2330 .bDescriptorType = UDESC_HUB,
2332 .wHubCharacteristics = {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
2333 .bPwrOn2PwrGood = 50,
2337 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2338 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2339 * should not be used by the USB subsystem. As we cannot issue a
2340 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2341 * will be enabled as part of the reset.
2343 * On the VT83C572, the port cannot be successfully enabled until the
2344 * outstanding "port enable change" and "connection status change"
2345 * events have been reset.
2348 uhci_portreset(uhci_softc_t *sc, uint16_t index)
2355 port = UHCI_PORTSC1;
2356 else if (index == 2)
2357 port = UHCI_PORTSC2;
2359 return (USB_ERR_IOERROR);
2362 * Before we do anything, turn on SOF messages on the USB
2363 * BUS. Some USB devices do not cope without them!
2367 x = URWMASK(UREAD2(sc, port));
2368 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2370 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2371 USB_MS_TO_TICKS(usb_port_root_reset_delay));
2373 DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
2374 index, UREAD2(sc, port));
2376 x = URWMASK(UREAD2(sc, port));
2377 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2379 mtx_unlock(&sc->sc_bus.bus_mtx);
2382 * This delay needs to be exactly 100us, else some USB devices
2387 mtx_lock(&sc->sc_bus.bus_mtx);
2389 DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
2390 index, UREAD2(sc, port));
2392 x = URWMASK(UREAD2(sc, port));
2393 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2395 for (lim = 0; lim < 12; lim++) {
2396 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2397 USB_MS_TO_TICKS(usb_port_reset_delay));
2399 x = UREAD2(sc, port);
2401 DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
2404 if (!(x & UHCI_PORTSC_CCS)) {
2406 * No device is connected (or was disconnected
2407 * during reset). Consider the port reset.
2408 * The delay must be long enough to ensure on
2409 * the initial iteration that the device
2410 * connection will have been registered. 50ms
2411 * appears to be sufficient, but 20ms is not.
2413 DPRINTFN(4, "uhci port %d loop %u, device detached\n",
2417 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
2419 * Port enabled changed and/or connection
2420 * status changed were set. Reset either or
2421 * both raised flags (by writing a 1 to that
2422 * bit), and wait again for state to settle.
2424 UWRITE2(sc, port, URWMASK(x) |
2425 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
2428 if (x & UHCI_PORTSC_PE) {
2429 /* port is enabled */
2432 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
2435 DPRINTFN(2, "uhci port %d reset timed out\n", index);
2436 return (USB_ERR_TIMEOUT);
2439 DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
2440 index, UREAD2(sc, port));
2443 return (USB_ERR_NORMAL_COMPLETION);
2447 uhci_roothub_exec(struct usb_device *udev,
2448 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2450 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
2452 const char *str_ptr;
2462 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2465 ptr = (const void *)&sc->sc_hub_desc.temp;
2469 value = UGETW(req->wValue);
2470 index = UGETW(req->wIndex);
2472 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2473 "wValue=0x%04x wIndex=0x%04x\n",
2474 req->bmRequestType, req->bRequest,
2475 UGETW(req->wLength), value, index);
2477 #define C(x,y) ((x) | ((y) << 8))
2478 switch (C(req->bRequest, req->bmRequestType)) {
2479 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2480 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2481 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2483 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2484 * for the integrated root hub.
2487 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2489 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2491 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2492 switch (value >> 8) {
2494 if ((value & 0xff) != 0) {
2495 err = USB_ERR_IOERROR;
2498 len = sizeof(uhci_devd);
2499 ptr = (const void *)&uhci_devd;
2503 if ((value & 0xff) != 0) {
2504 err = USB_ERR_IOERROR;
2507 len = sizeof(uhci_confd);
2508 ptr = (const void *)&uhci_confd;
2512 switch (value & 0xff) {
2513 case 0: /* Language table */
2517 case 1: /* Vendor */
2518 str_ptr = sc->sc_vendor;
2521 case 2: /* Product */
2522 str_ptr = "UHCI root HUB";
2530 len = usb_make_str_desc
2531 (sc->sc_hub_desc.temp,
2532 sizeof(sc->sc_hub_desc.temp),
2537 err = USB_ERR_IOERROR;
2541 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2543 sc->sc_hub_desc.temp[0] = 0;
2545 case C(UR_GET_STATUS, UT_READ_DEVICE):
2547 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
2549 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2550 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2552 USETW(sc->sc_hub_desc.stat.wStatus, 0);
2554 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2555 if (value >= UHCI_MAX_DEVICES) {
2556 err = USB_ERR_IOERROR;
2559 sc->sc_addr = value;
2561 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2562 if ((value != 0) && (value != 1)) {
2563 err = USB_ERR_IOERROR;
2566 sc->sc_conf = value;
2568 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2570 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2571 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2572 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2573 err = USB_ERR_IOERROR;
2575 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2577 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2580 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2582 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2583 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
2584 "port=%d feature=%d\n",
2587 port = UHCI_PORTSC1;
2588 else if (index == 2)
2589 port = UHCI_PORTSC2;
2591 err = USB_ERR_IOERROR;
2595 case UHF_PORT_ENABLE:
2596 x = URWMASK(UREAD2(sc, port));
2597 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2599 case UHF_PORT_SUSPEND:
2600 x = URWMASK(UREAD2(sc, port));
2601 UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
2603 case UHF_PORT_RESET:
2604 x = URWMASK(UREAD2(sc, port));
2605 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2607 case UHF_C_PORT_CONNECTION:
2608 x = URWMASK(UREAD2(sc, port));
2609 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2611 case UHF_C_PORT_ENABLE:
2612 x = URWMASK(UREAD2(sc, port));
2613 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2615 case UHF_C_PORT_OVER_CURRENT:
2616 x = URWMASK(UREAD2(sc, port));
2617 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2619 case UHF_C_PORT_RESET:
2621 err = USB_ERR_NORMAL_COMPLETION;
2623 case UHF_C_PORT_SUSPEND:
2624 sc->sc_isresumed &= ~(1 << index);
2626 case UHF_PORT_CONNECTION:
2627 case UHF_PORT_OVER_CURRENT:
2628 case UHF_PORT_POWER:
2629 case UHF_PORT_LOW_SPEED:
2631 err = USB_ERR_IOERROR;
2635 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2637 port = UHCI_PORTSC1;
2638 else if (index == 2)
2639 port = UHCI_PORTSC2;
2641 err = USB_ERR_IOERROR;
2645 sc->sc_hub_desc.temp[0] =
2646 ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2647 UHCI_PORTSC_LS_SHIFT);
2649 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2650 if ((value & 0xff) != 0) {
2651 err = USB_ERR_IOERROR;
2654 len = sizeof(uhci_hubd_piix);
2655 ptr = (const void *)&uhci_hubd_piix;
2657 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2659 memset(sc->sc_hub_desc.temp, 0, 16);
2661 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2663 port = UHCI_PORTSC1;
2664 else if (index == 2)
2665 port = UHCI_PORTSC2;
2667 err = USB_ERR_IOERROR;
2670 x = UREAD2(sc, port);
2671 status = change = 0;
2672 if (x & UHCI_PORTSC_CCS)
2673 status |= UPS_CURRENT_CONNECT_STATUS;
2674 if (x & UHCI_PORTSC_CSC)
2675 change |= UPS_C_CONNECT_STATUS;
2676 if (x & UHCI_PORTSC_PE)
2677 status |= UPS_PORT_ENABLED;
2678 if (x & UHCI_PORTSC_POEDC)
2679 change |= UPS_C_PORT_ENABLED;
2680 if (x & UHCI_PORTSC_OCI)
2681 status |= UPS_OVERCURRENT_INDICATOR;
2682 if (x & UHCI_PORTSC_OCIC)
2683 change |= UPS_C_OVERCURRENT_INDICATOR;
2684 if (x & UHCI_PORTSC_LSDA)
2685 status |= UPS_LOW_SPEED;
2686 if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
2687 /* need to do a write back */
2688 UWRITE2(sc, port, URWMASK(x));
2690 /* wait 20ms for resume sequence to complete */
2691 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
2693 /* clear suspend and resume detect */
2694 UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
2697 /* wait a little bit */
2698 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
2700 sc->sc_isresumed |= (1 << index);
2702 } else if (x & UHCI_PORTSC_SUSP) {
2703 status |= UPS_SUSPEND;
2705 status |= UPS_PORT_POWER;
2706 if (sc->sc_isresumed & (1 << index))
2707 change |= UPS_C_SUSPEND;
2709 change |= UPS_C_PORT_RESET;
2710 USETW(sc->sc_hub_desc.ps.wPortStatus, status);
2711 USETW(sc->sc_hub_desc.ps.wPortChange, change);
2712 len = sizeof(sc->sc_hub_desc.ps);
2714 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2715 err = USB_ERR_IOERROR;
2717 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2719 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2721 port = UHCI_PORTSC1;
2722 else if (index == 2)
2723 port = UHCI_PORTSC2;
2725 err = USB_ERR_IOERROR;
2729 case UHF_PORT_ENABLE:
2730 x = URWMASK(UREAD2(sc, port));
2731 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2733 case UHF_PORT_SUSPEND:
2734 x = URWMASK(UREAD2(sc, port));
2735 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2737 case UHF_PORT_RESET:
2738 err = uhci_portreset(sc, index);
2740 case UHF_PORT_POWER:
2741 /* pretend we turned on power */
2742 err = USB_ERR_NORMAL_COMPLETION;
2744 case UHF_C_PORT_CONNECTION:
2745 case UHF_C_PORT_ENABLE:
2746 case UHF_C_PORT_OVER_CURRENT:
2747 case UHF_PORT_CONNECTION:
2748 case UHF_PORT_OVER_CURRENT:
2749 case UHF_PORT_LOW_SPEED:
2750 case UHF_C_PORT_SUSPEND:
2751 case UHF_C_PORT_RESET:
2753 err = USB_ERR_IOERROR;
2758 err = USB_ERR_IOERROR;
2768 * This routine is executed periodically and simulates interrupts from
2769 * the root controller interrupt pipe for port status change:
2772 uhci_root_intr(uhci_softc_t *sc)
2776 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2778 sc->sc_hub_idata[0] = 0;
2780 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
2781 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2782 sc->sc_hub_idata[0] |= 1 << 1;
2784 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
2785 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2786 sc->sc_hub_idata[0] |= 1 << 2;
2790 usb_callout_reset(&sc->sc_root_intr, hz,
2791 (void *)&uhci_root_intr, sc);
2793 if (sc->sc_hub_idata[0] != 0) {
2794 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2795 sizeof(sc->sc_hub_idata));
2800 uhci_xfer_setup(struct usb_setup_params *parm)
2802 struct usb_page_search page_info;
2803 struct usb_page_cache *pc;
2805 struct usb_xfer *xfer;
2813 sc = UHCI_BUS2SC(parm->udev->bus);
2814 xfer = parm->curr_xfer;
2816 parm->hc_max_packet_size = 0x500;
2817 parm->hc_max_packet_count = 1;
2818 parm->hc_max_frame_size = 0x500;
2821 * compute ntd and nqh
2823 if (parm->methods == &uhci_device_ctrl_methods) {
2824 xfer->flags_int.bdma_enable = 1;
2825 xfer->flags_int.bdma_no_post_sync = 1;
2827 usbd_transfer_setup_sub(parm);
2829 /* see EHCI HC driver for proof of "ntd" formula */
2832 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
2833 + (xfer->max_data_length / xfer->max_frame_size));
2835 } else if (parm->methods == &uhci_device_bulk_methods) {
2836 xfer->flags_int.bdma_enable = 1;
2837 xfer->flags_int.bdma_no_post_sync = 1;
2839 usbd_transfer_setup_sub(parm);
2842 ntd = ((2 * xfer->nframes)
2843 + (xfer->max_data_length / xfer->max_frame_size));
2845 } else if (parm->methods == &uhci_device_intr_methods) {
2846 xfer->flags_int.bdma_enable = 1;
2847 xfer->flags_int.bdma_no_post_sync = 1;
2849 usbd_transfer_setup_sub(parm);
2852 ntd = ((2 * xfer->nframes)
2853 + (xfer->max_data_length / xfer->max_frame_size));
2855 } else if (parm->methods == &uhci_device_isoc_methods) {
2856 xfer->flags_int.bdma_enable = 1;
2857 xfer->flags_int.bdma_no_post_sync = 1;
2859 usbd_transfer_setup_sub(parm);
2862 ntd = xfer->nframes;
2865 usbd_transfer_setup_sub(parm);
2875 * NOTE: the UHCI controller requires that
2876 * every packet must be contiguous on
2877 * the same USB memory page !
2879 nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
2882 * Compute a suitable power of two alignment
2883 * for our "max_frame_size" fixup buffer(s):
2885 align = xfer->max_frame_size;
2892 /* check for power of two */
2893 if (!(xfer->max_frame_size &
2894 (xfer->max_frame_size - 1))) {
2898 * We don't allow alignments of
2899 * less than 8 bytes:
2901 * NOTE: Allocating using an aligment
2902 * of 1 byte has special meaning!
2909 if (usbd_transfer_setup_sub_malloc(
2910 parm, &pc, xfer->max_frame_size,
2912 parm->err = USB_ERR_NOMEM;
2915 xfer->buf_fixup = pc;
2924 if (usbd_transfer_setup_sub_malloc(
2925 parm, &pc, sizeof(uhci_td_t),
2926 UHCI_TD_ALIGN, ntd)) {
2927 parm->err = USB_ERR_NOMEM;
2931 for (n = 0; n != ntd; n++) {
2934 usbd_get_page(pc + n, 0, &page_info);
2936 td = page_info.buffer;
2939 if ((parm->methods == &uhci_device_bulk_methods) ||
2940 (parm->methods == &uhci_device_ctrl_methods) ||
2941 (parm->methods == &uhci_device_intr_methods)) {
2942 /* set depth first bit */
2943 td->td_self = htole32(page_info.physaddr |
2944 UHCI_PTR_TD | UHCI_PTR_VF);
2946 td->td_self = htole32(page_info.physaddr |
2950 td->obj_next = last_obj;
2951 td->page_cache = pc + n;
2955 usb_pc_cpu_flush(pc + n);
2958 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
2962 if (usbd_transfer_setup_sub_malloc(
2963 parm, &pc, sizeof(uhci_qh_t),
2964 UHCI_QH_ALIGN, nqh)) {
2965 parm->err = USB_ERR_NOMEM;
2969 for (n = 0; n != nqh; n++) {
2972 usbd_get_page(pc + n, 0, &page_info);
2974 qh = page_info.buffer;
2977 qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
2978 qh->obj_next = last_obj;
2979 qh->page_cache = pc + n;
2983 usb_pc_cpu_flush(pc + n);
2986 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
2988 if (!xfer->flags_int.curr_dma_set) {
2989 xfer->flags_int.curr_dma_set = 1;
2995 uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2996 struct usb_endpoint *ep)
2998 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
3000 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3002 edesc->bEndpointAddress, udev->flags.usb_mode,
3005 if (udev->device_index != sc->sc_addr) {
3006 switch (edesc->bmAttributes & UE_XFERTYPE) {
3008 ep->methods = &uhci_device_ctrl_methods;
3011 ep->methods = &uhci_device_intr_methods;
3013 case UE_ISOCHRONOUS:
3014 if (udev->speed == USB_SPEED_FULL) {
3015 ep->methods = &uhci_device_isoc_methods;
3019 ep->methods = &uhci_device_bulk_methods;
3029 uhci_xfer_unsetup(struct usb_xfer *xfer)
3035 uhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3038 * Wait until hardware has finished any possible use of the
3039 * transfer descriptor(s) and QH
3041 *pus = (1125); /* microseconds */
3045 uhci_device_resume(struct usb_device *udev)
3047 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3048 struct usb_xfer *xfer;
3049 const struct usb_pipe_methods *methods;
3054 USB_BUS_LOCK(udev->bus);
3056 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3057 if (xfer->xroot->udev == udev) {
3058 methods = xfer->endpoint->methods;
3059 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3061 if (methods == &uhci_device_bulk_methods) {
3062 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
3064 xfer->flags_int.bandwidth_reclaimed = 1;
3066 if (methods == &uhci_device_ctrl_methods) {
3067 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3068 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
3070 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
3073 if (methods == &uhci_device_intr_methods) {
3074 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3079 USB_BUS_UNLOCK(udev->bus);
3085 uhci_device_suspend(struct usb_device *udev)
3087 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3088 struct usb_xfer *xfer;
3089 const struct usb_pipe_methods *methods;
3094 USB_BUS_LOCK(udev->bus);
3096 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3097 if (xfer->xroot->udev == udev) {
3098 methods = xfer->endpoint->methods;
3099 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3101 if (xfer->flags_int.bandwidth_reclaimed) {
3102 xfer->flags_int.bandwidth_reclaimed = 0;
3105 if (methods == &uhci_device_bulk_methods) {
3106 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
3108 if (methods == &uhci_device_ctrl_methods) {
3109 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3110 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
3112 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
3115 if (methods == &uhci_device_intr_methods) {
3116 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3121 USB_BUS_UNLOCK(udev->bus);
3127 uhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3129 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3132 case USB_HW_POWER_SUSPEND:
3133 case USB_HW_POWER_SHUTDOWN:
3136 case USB_HW_POWER_RESUME:
3145 uhci_set_hw_power(struct usb_bus *bus)
3147 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3154 flags = bus->hw_power_state;
3157 * WARNING: Some FULL speed USB devices require periodic SOF
3158 * messages! If any USB devices are connected through the
3159 * UHCI, power save will be disabled!
3161 if (flags & (USB_HW_POWER_CONTROL |
3162 USB_HW_POWER_NON_ROOT_HUB |
3164 USB_HW_POWER_INTERRUPT |
3165 USB_HW_POWER_ISOC)) {
3166 DPRINTF("Some USB transfer is "
3167 "active on unit %u.\n",
3168 device_get_unit(sc->sc_bus.bdev));
3171 DPRINTF("Power save on unit %u.\n",
3172 device_get_unit(sc->sc_bus.bdev));
3173 UHCICMD(sc, UHCI_CMD_MAXP);
3176 USB_BUS_UNLOCK(bus);
3181 static const struct usb_bus_methods uhci_bus_methods =
3183 .endpoint_init = uhci_ep_init,
3184 .xfer_setup = uhci_xfer_setup,
3185 .xfer_unsetup = uhci_xfer_unsetup,
3186 .get_dma_delay = uhci_get_dma_delay,
3187 .device_resume = uhci_device_resume,
3188 .device_suspend = uhci_device_suspend,
3189 .set_hw_power = uhci_set_hw_power,
3190 .set_hw_power_sleep = uhci_set_hw_power_sleep,
3191 .roothub_exec = uhci_roothub_exec,
3192 .xfer_poll = uhci_do_poll,