1 /* $FreeBSD: head/sys/dev/usb/controller/uhci.c 278883 2015-02-17 07:52:50Z hselasky $ */
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
5 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB Universal Host Controller driver.
31 * Handles e.g. PIIX3 and PIIX4.
33 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
34 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
35 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
36 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
39 #include <sys/stdint.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <bus/u4b/usb.h>
56 #include <bus/u4b/usbdi.h>
58 #define USB_DEBUG_VAR uhcidebug
60 #include <bus/u4b/usb_core.h>
61 #include <bus/u4b/usb_debug.h>
62 #include <bus/u4b/usb_busdma.h>
63 #include <bus/u4b/usb_process.h>
64 #include <bus/u4b/usb_transfer.h>
65 #include <bus/u4b/usb_device.h>
66 #include <bus/u4b/usb_hub.h>
67 #include <bus/u4b/usb_util.h>
69 #include <bus/u4b/usb_controller.h>
70 #include <bus/u4b/usb_bus.h>
71 #include <bus/u4b/controller/uhci.h>
72 #include <bus/u4b/controller/uhcireg.h>
75 #define UHCI_BUS2SC(bus) \
76 ((uhci_softc_t *)(((uint8_t *)(bus)) - \
77 ((uint8_t *)&(((uhci_softc_t *)0)->sc_bus))))
80 static int uhcidebug = 0;
81 static int uhcinoloop = 0;
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
84 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
85 &uhcidebug, 0, "uhci debug level");
86 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
87 &uhcinoloop, 0, "uhci noloop");
89 TUNABLE_INT("hw.usb.uhci.debug", &uhcidebug);
90 TUNABLE_INT("hw.usb.uhci.loop", &uhcinoloop);
92 static void uhci_dumpregs(uhci_softc_t *sc);
93 static void uhci_dump_tds(uhci_td_t *td);
97 #define UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
98 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
99 #define UWRITE1(sc, r, x) \
100 do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
101 } while (/*CONSTCOND*/0)
102 #define UWRITE2(sc, r, x) \
103 do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
104 } while (/*CONSTCOND*/0)
105 #define UWRITE4(sc, r, x) \
106 do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
107 } while (/*CONSTCOND*/0)
108 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
109 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
110 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
112 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
113 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
115 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
117 #define UHCI_INTR_ENDPT 1
119 struct uhci_mem_layout {
121 struct usb_page_search buf_res;
122 struct usb_page_search fix_res;
124 struct usb_page_cache *buf_pc;
125 struct usb_page_cache *fix_pc;
129 uint16_t max_frame_size;
132 struct uhci_std_temp {
134 struct uhci_mem_layout ml;
141 uint16_t max_frame_size;
143 uint8_t setup_alt_next;
147 static const struct usb_bus_methods uhci_bus_methods;
148 static const struct usb_pipe_methods uhci_device_bulk_methods;
149 static const struct usb_pipe_methods uhci_device_ctrl_methods;
150 static const struct usb_pipe_methods uhci_device_intr_methods;
151 static const struct usb_pipe_methods uhci_device_isoc_methods;
153 static uint8_t uhci_restart(uhci_softc_t *sc);
154 static void uhci_do_poll(struct usb_bus *);
155 static void uhci_device_done(struct usb_xfer *, usb_error_t);
156 static void uhci_transfer_intr_enqueue(struct usb_xfer *);
157 static void uhci_timeout(void *);
158 static uint8_t uhci_check_transfer(struct usb_xfer *);
159 static void uhci_root_intr(uhci_softc_t *sc);
162 uhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
164 struct uhci_softc *sc = UHCI_BUS2SC(bus);
167 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
168 sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
170 cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
171 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
173 cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
174 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
176 cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
177 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
179 cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
180 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
182 cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
183 sizeof(uhci_td_t), UHCI_TD_ALIGN);
185 for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
186 cb(bus, sc->sc_hw.isoc_start_pc + i,
187 sc->sc_hw.isoc_start_pg + i,
188 sizeof(uhci_td_t), UHCI_TD_ALIGN);
191 for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
192 cb(bus, sc->sc_hw.intr_start_pc + i,
193 sc->sc_hw.intr_start_pg + i,
194 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
199 uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb_xfer *xfer)
201 ml->buf_pc = xfer->frbuffers + 0;
202 ml->fix_pc = xfer->buf_fixup;
206 ml->max_frame_size = xfer->max_frame_size;
210 uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
212 usbd_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
214 if (ml->buf_res.length < td->len) {
216 /* need to do a fixup */
218 usbd_get_page(ml->fix_pc, 0, &ml->fix_res);
220 td->td_buffer = htole32(ml->fix_res.physaddr);
223 * The UHCI driver cannot handle
224 * page crossings, so a fixup is
237 if ((td->td_token & htole32(UHCI_TD_PID)) ==
238 htole32(UHCI_TD_PID_IN)) {
239 td->fix_pc = ml->fix_pc;
240 usb_pc_cpu_invalidate(ml->fix_pc);
245 /* copy data to fixup location */
247 usbd_copy_out(ml->buf_pc, ml->buf_offset,
248 ml->fix_res.buffer, td->len);
250 usb_pc_cpu_flush(ml->fix_pc);
253 /* prepare next fixup */
259 td->td_buffer = htole32(ml->buf_res.physaddr);
263 /* prepare next data location */
265 ml->buf_offset += td->len;
274 uhci_restart(uhci_softc_t *sc)
276 struct usb_page_search buf_res;
278 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
280 if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) {
281 DPRINTFN(2, "Already started\n");
285 DPRINTFN(2, "Restarting\n");
287 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
289 /* Reload fresh base address */
290 UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
293 * Assume 64 byte packets at frame end and start HC controller:
295 UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
297 /* wait 10 milliseconds */
299 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 100);
301 /* check that controller has started */
303 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
304 DPRINTFN(2, "Failed\n");
311 uhci_reset(uhci_softc_t *sc)
315 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
317 DPRINTF("resetting the HC\n");
319 /* disable interrupts */
321 UWRITE2(sc, UHCI_INTR, 0);
325 UHCICMD(sc, UHCI_CMD_GRESET);
329 usb_pause_mtx(&sc->sc_bus.bus_lock,
330 USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
332 /* terminate all transfers */
334 UHCICMD(sc, UHCI_CMD_HCRESET);
336 /* the reset bit goes low when the controller is done */
338 n = UHCI_RESET_TIMEOUT;
340 /* wait one millisecond */
342 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 1000);
344 if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
349 device_printf(sc->sc_bus.bdev,
350 "controller did not reset\n");
356 /* wait one millisecond */
358 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 1000);
360 /* check if HC is stopped */
361 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
366 device_printf(sc->sc_bus.bdev,
367 "controller did not stop\n");
371 /* reset frame number */
372 UWRITE2(sc, UHCI_FRNUM, 0);
373 /* set default SOF value */
374 UWRITE1(sc, UHCI_SOF, 0x40);
376 USB_BUS_UNLOCK(&sc->sc_bus);
378 /* stop root interrupt */
380 usb_callout_drain(&sc->sc_root_intr);
382 USB_BUS_LOCK(&sc->sc_bus);
386 uhci_start(uhci_softc_t *sc)
388 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
390 DPRINTFN(2, "enabling\n");
392 /* enable interrupts */
394 UWRITE2(sc, UHCI_INTR,
400 if (uhci_restart(sc)) {
401 device_printf(sc->sc_bus.bdev,
402 "cannot start HC controller\n");
405 /* start root interrupt */
409 static struct uhci_qh *
410 uhci_init_qh(struct usb_page_cache *pc)
412 struct usb_page_search buf_res;
415 usbd_get_page(pc, 0, &buf_res);
420 htole32(buf_res.physaddr) |
421 htole32(UHCI_PTR_QH);
428 static struct uhci_td *
429 uhci_init_td(struct usb_page_cache *pc)
431 struct usb_page_search buf_res;
434 usbd_get_page(pc, 0, &buf_res);
439 htole32(buf_res.physaddr) |
440 htole32(UHCI_PTR_TD);
448 uhci_init(uhci_softc_t *sc)
457 usb_callout_init_mtx(&sc->sc_root_intr, &sc->sc_bus.bus_lock, 0);
467 sc->sc_ls_ctl_p_last =
468 uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc);
470 sc->sc_fs_ctl_p_last =
471 uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc);
474 uhci_init_qh(&sc->sc_hw.bulk_start_pc);
476 sc->sc_reclaim_qh_p =
477 sc->sc_fs_ctl_p_last;
479 /* setup reclaim looping point */
480 sc->sc_reclaim_qh_p =
485 uhci_init_qh(&sc->sc_hw.last_qh_pc);
488 uhci_init_td(&sc->sc_hw.last_td_pc);
490 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
491 sc->sc_isoc_p_last[x] =
492 uhci_init_td(sc->sc_hw.isoc_start_pc + x);
495 for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) {
496 sc->sc_intr_p_last[x] =
497 uhci_init_qh(sc->sc_hw.intr_start_pc + x);
501 * the QHs are arranged to give poll intervals that are
502 * powers of 2 times 1ms
504 bit = UHCI_IFRAMELIST_COUNT / 2;
511 y = (x ^ bit) | (bit / 2);
514 * the next QH has half the poll interval
516 qh_x = sc->sc_intr_p_last[x];
517 qh_y = sc->sc_intr_p_last[y];
520 qh_x->qh_h_next = qh_y->qh_self;
522 qh_x->qh_e_next = htole32(UHCI_PTR_T);
532 qh_ls = sc->sc_ls_ctl_p_last;
533 qh_intr = sc->sc_intr_p_last[0];
535 /* start QH for interrupt traffic */
536 qh_intr->h_next = qh_ls;
537 qh_intr->qh_h_next = qh_ls->qh_self;
539 qh_intr->qh_e_next = htole32(UHCI_PTR_T);
541 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;
621 * Setup UHCI framelist
625 * pframes -> full speed isochronous -> interrupt QH's -> low
626 * speed control -> full speed control -> bulk transfers
630 for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
632 sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
635 /* flush all cache into memory */
637 usb_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
639 /* set up the bus struct */
640 sc->sc_bus.methods = &uhci_bus_methods;
642 USB_BUS_LOCK(&sc->sc_bus);
643 /* reset the controller */
646 /* start the controller */
648 USB_BUS_UNLOCK(&sc->sc_bus);
650 /* catch lost interrupts */
651 uhci_do_poll(&sc->sc_bus);
657 uhci_suspend(uhci_softc_t *sc)
665 USB_BUS_LOCK(&sc->sc_bus);
667 /* stop the controller */
671 /* enter global suspend */
673 UHCICMD(sc, UHCI_CMD_EGSM);
675 USB_BUS_UNLOCK(&sc->sc_bus);
679 uhci_resume(uhci_softc_t *sc)
681 USB_BUS_LOCK(&sc->sc_bus);
683 /* reset the controller */
687 /* force global resume */
689 UHCICMD(sc, UHCI_CMD_FGR);
691 /* and start traffic again */
695 USB_BUS_UNLOCK(&sc->sc_bus);
702 /* catch lost interrupts */
703 uhci_do_poll(&sc->sc_bus);
708 uhci_dumpregs(uhci_softc_t *sc)
710 DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
711 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
712 device_get_nameunit(sc->sc_bus.bdev),
713 UREAD2(sc, UHCI_CMD),
714 UREAD2(sc, UHCI_STS),
715 UREAD2(sc, UHCI_INTR),
716 UREAD2(sc, UHCI_FRNUM),
717 UREAD4(sc, UHCI_FLBASEADDR),
718 UREAD1(sc, UHCI_SOF),
719 UREAD2(sc, UHCI_PORTSC1),
720 UREAD2(sc, UHCI_PORTSC2));
724 uhci_dump_td(uhci_td_t *p)
731 usb_pc_cpu_invalidate(p->page_cache);
733 td_next = le32toh(p->td_next);
734 td_status = le32toh(p->td_status);
735 td_token = le32toh(p->td_token);
738 * Check whether the link pointer in this TD marks the link pointer
741 temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
743 kprintf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
744 "token=0x%08x buffer=0x%08x\n",
750 le32toh(p->td_buffer));
752 kprintf("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,"
753 "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
755 (td_next & 1) ? "-T" : "",
756 (td_next & 2) ? "-Q" : "",
757 (td_next & 4) ? "-VF" : "",
758 (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
759 (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
760 (td_status & UHCI_TD_NAK) ? "-NAK" : "",
761 (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
762 (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
763 (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
764 (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
765 (td_status & UHCI_TD_IOC) ? "-IOC" : "",
766 (td_status & UHCI_TD_IOS) ? "-IOS" : "",
767 (td_status & UHCI_TD_LS) ? "-LS" : "",
768 (td_status & UHCI_TD_SPD) ? "-SPD" : "",
769 UHCI_TD_GET_ERRCNT(td_status),
770 UHCI_TD_GET_ACTLEN(td_status),
771 UHCI_TD_GET_PID(td_token),
772 UHCI_TD_GET_DEVADDR(td_token),
773 UHCI_TD_GET_ENDPT(td_token),
774 UHCI_TD_GET_DT(td_token),
775 UHCI_TD_GET_MAXLEN(td_token));
781 uhci_dump_qh(uhci_qh_t *sqh)
787 usb_pc_cpu_invalidate(sqh->page_cache);
789 qh_h_next = le32toh(sqh->qh_h_next);
790 qh_e_next = le32toh(sqh->qh_e_next);
792 DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
793 le32toh(sqh->qh_self), qh_h_next, qh_e_next);
795 temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
796 (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
802 uhci_dump_all(uhci_softc_t *sc)
805 uhci_dump_qh(sc->sc_ls_ctl_p_last);
806 uhci_dump_qh(sc->sc_fs_ctl_p_last);
807 uhci_dump_qh(sc->sc_bulk_p_last);
808 uhci_dump_qh(sc->sc_last_qh_p);
812 uhci_dump_tds(uhci_td_t *td)
817 if (uhci_dump_td(td)) {
826 * Let the last QH loop back to the full speed control transfer QH.
827 * This is what intel calls "bandwidth reclamation" and improves
828 * USB performance a lot for some devices.
829 * If we are already looping, just count it.
832 uhci_add_loop(uhci_softc_t *sc)
834 struct uhci_qh *qh_lst;
835 struct uhci_qh *qh_rec;
842 if (++(sc->sc_loops) == 1) {
843 DPRINTFN(6, "add\n");
845 qh_lst = sc->sc_last_qh_p;
846 qh_rec = sc->sc_reclaim_qh_p;
848 /* NOTE: we don't loop back the soft pointer */
850 qh_lst->qh_h_next = qh_rec->qh_self;
851 usb_pc_cpu_flush(qh_lst->page_cache);
856 uhci_rem_loop(uhci_softc_t *sc)
858 struct uhci_qh *qh_lst;
865 if (--(sc->sc_loops) == 0) {
866 DPRINTFN(6, "remove\n");
868 qh_lst = sc->sc_last_qh_p;
869 qh_lst->qh_h_next = htole32(UHCI_PTR_T);
870 usb_pc_cpu_flush(qh_lst->page_cache);
875 uhci_transfer_intr_enqueue(struct usb_xfer *xfer)
877 /* check for early completion */
878 if (uhci_check_transfer(xfer)) {
881 /* put transfer on interrupt queue */
882 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
884 /* start timeout, if any */
885 if (xfer->timeout != 0) {
886 usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
890 #define UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
892 _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
894 DPRINTFN(11, "%p to %p\n", std, last);
896 /* (sc->sc_bus.lock) must be locked */
898 std->next = last->next;
899 std->td_next = last->td_next;
903 usb_pc_cpu_flush(std->page_cache);
906 * the last->next->prev is never followed: std->next->prev = std;
909 last->td_next = std->td_self;
911 usb_pc_cpu_flush(last->page_cache);
916 #define UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
918 _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
920 DPRINTFN(11, "%p to %p\n", sqh, last);
922 if (sqh->h_prev != NULL) {
923 /* should not happen */
924 DPRINTFN(0, "QH already linked!\n");
927 /* (sc->sc_bus.lock) must be locked */
929 sqh->h_next = last->h_next;
930 sqh->qh_h_next = last->qh_h_next;
934 usb_pc_cpu_flush(sqh->page_cache);
937 * The "last->h_next->h_prev" is never followed:
939 * "sqh->h_next->h_prev" = sqh;
943 last->qh_h_next = sqh->qh_self;
945 usb_pc_cpu_flush(last->page_cache);
952 #define UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
954 _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
956 DPRINTFN(11, "%p from %p\n", std, last);
958 /* (sc->sc_bus.lock) must be locked */
960 std->prev->next = std->next;
961 std->prev->td_next = std->td_next;
963 usb_pc_cpu_flush(std->prev->page_cache);
966 std->next->prev = std->prev;
967 usb_pc_cpu_flush(std->next->page_cache);
969 return ((last == std) ? std->prev : last);
972 #define UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
974 _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
976 DPRINTFN(11, "%p from %p\n", sqh, last);
978 /* (sc->sc_bus.lock) must be locked */
980 /* only remove if not removed from a queue */
983 sqh->h_prev->h_next = sqh->h_next;
984 sqh->h_prev->qh_h_next = sqh->qh_h_next;
986 usb_pc_cpu_flush(sqh->h_prev->page_cache);
989 sqh->h_next->h_prev = sqh->h_prev;
990 usb_pc_cpu_flush(sqh->h_next->page_cache);
992 last = ((last == sqh) ? sqh->h_prev : last);
996 usb_pc_cpu_flush(sqh->page_cache);
1002 uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer)
1004 struct usb_page_search res;
1005 uint32_t nframes = xfer->nframes;
1007 uint32_t offset = 0;
1008 uint32_t *plen = xfer->frlengths;
1010 uhci_td_t *td = xfer->td_transfer_first;
1011 uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
1013 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1014 xfer, xfer->endpoint);
1016 /* sync any DMA memory before doing fixups */
1018 usb_bdma_post_sync(xfer);
1022 panic("%s:%d: out of TD's\n",
1023 __func__, __LINE__);
1025 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
1026 pp_last = &sc->sc_isoc_p_last[0];
1029 if (uhcidebug > 5) {
1030 DPRINTF("isoc TD\n");
1034 usb_pc_cpu_invalidate(td->page_cache);
1035 status = le32toh(td->td_status);
1037 len = UHCI_TD_GET_ACTLEN(status);
1044 usbd_get_page(td->fix_pc, 0, &res);
1046 /* copy data from fixup location to real location */
1048 usb_pc_cpu_invalidate(td->fix_pc);
1050 usbd_copy_in(xfer->frbuffers, offset,
1057 /* remove TD from schedule */
1058 UHCI_REMOVE_TD(td, *pp_last);
1065 xfer->aframes = xfer->nframes;
1069 uhci_non_isoc_done_sub(struct usb_xfer *xfer)
1071 struct usb_page_search res;
1073 uhci_td_t *td_alt_next;
1078 td = xfer->td_transfer_cache;
1079 td_alt_next = td->alt_next;
1081 if (xfer->aframes != xfer->nframes) {
1082 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1086 usb_pc_cpu_invalidate(td->page_cache);
1087 status = le32toh(td->td_status);
1088 token = le32toh(td->td_token);
1091 * Verify the status and add
1092 * up the actual length:
1095 len = UHCI_TD_GET_ACTLEN(status);
1096 if (len > td->len) {
1097 /* should not happen */
1098 DPRINTF("Invalid status length, "
1099 "0x%04x/0x%04x bytes\n", len, td->len);
1100 status |= UHCI_TD_STALLED;
1102 } else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
1106 usbd_get_page(td->fix_pc, 0, &res);
1109 * copy data from fixup location to real
1113 usb_pc_cpu_invalidate(td->fix_pc);
1115 usbd_copy_in(xfer->frbuffers + xfer->aframes,
1116 xfer->frlengths[xfer->aframes], res.buffer, len);
1118 /* update actual length */
1120 xfer->frlengths[xfer->aframes] += len;
1122 /* Check for last transfer */
1123 if (((void *)td) == xfer->td_transfer_last) {
1127 if (status & UHCI_TD_STALLED) {
1128 /* the transfer is finished */
1132 /* Check for short transfer */
1133 if (len != td->len) {
1134 if (xfer->flags_int.short_frames_ok) {
1135 /* follow alt next */
1138 /* the transfer is finished */
1145 if (td->alt_next != td_alt_next) {
1146 /* this USB frame is complete */
1151 /* update transfer cache */
1153 xfer->td_transfer_cache = td;
1155 /* update data toggle */
1157 xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
1160 if (status & UHCI_TD_ERROR) {
1161 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
1162 "status=%s%s%s%s%s%s%s%s%s%s%s\n",
1163 xfer->address, xfer->endpointno, xfer->aframes,
1164 (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
1165 (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
1166 (status & UHCI_TD_NAK) ? "[NAK]" : "",
1167 (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
1168 (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
1169 (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
1170 (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1171 (status & UHCI_TD_IOC) ? "[IOC]" : "",
1172 (status & UHCI_TD_IOS) ? "[IOS]" : "",
1173 (status & UHCI_TD_LS) ? "[LS]" : "",
1174 (status & UHCI_TD_SPD) ? "[SPD]" : "");
1177 if (status & UHCI_TD_STALLED) {
1178 /* try to separate I/O errors from STALL */
1179 if (UHCI_TD_GET_ERRCNT(status) == 0)
1180 return (USB_ERR_IOERROR);
1181 return (USB_ERR_STALLED);
1183 return (USB_ERR_NORMAL_COMPLETION);
1187 uhci_non_isoc_done(struct usb_xfer *xfer)
1189 usb_error_t err = 0;
1191 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1192 xfer, xfer->endpoint);
1195 if (uhcidebug > 10) {
1196 uhci_dump_tds(xfer->td_transfer_first);
1200 /* sync any DMA memory before doing fixups */
1202 usb_bdma_post_sync(xfer);
1206 xfer->td_transfer_cache = xfer->td_transfer_first;
1208 if (xfer->flags_int.control_xfr) {
1209 if (xfer->flags_int.control_hdr) {
1211 err = uhci_non_isoc_done_sub(xfer);
1215 if (xfer->td_transfer_cache == NULL) {
1219 while (xfer->aframes != xfer->nframes) {
1221 err = uhci_non_isoc_done_sub(xfer);
1224 if (xfer->td_transfer_cache == NULL) {
1229 if (xfer->flags_int.control_xfr &&
1230 !xfer->flags_int.control_act) {
1232 err = uhci_non_isoc_done_sub(xfer);
1235 uhci_device_done(xfer, err);
1238 /*------------------------------------------------------------------------*
1239 * uhci_check_transfer_sub
1241 * The main purpose of this function is to update the data-toggle
1242 * in case it is wrong.
1243 *------------------------------------------------------------------------*/
1245 uhci_check_transfer_sub(struct usb_xfer *xfer)
1249 uhci_td_t *td_alt_next;
1254 td = xfer->td_transfer_cache;
1255 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1257 td_token = td->obj_next->td_token;
1259 xfer->td_transfer_cache = td;
1260 td_self = td->td_self;
1261 td_alt_next = td->alt_next;
1263 if (xfer->flags_int.control_xfr)
1264 goto skip; /* don't touch the DT value! */
1266 if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))))
1267 goto skip; /* data toggle has correct value */
1270 * The data toggle is wrong and we need to toggle it !
1274 td->td_token ^= htole32(UHCI_TD_SET_DT(1));
1275 usb_pc_cpu_flush(td->page_cache);
1277 if (td == xfer->td_transfer_last) {
1283 if (td->alt_next != td_alt_next) {
1291 qh->qh_e_next = td_self;
1292 usb_pc_cpu_flush(qh->page_cache);
1294 DPRINTFN(13, "xfer=%p following alt next\n", xfer);
1297 /*------------------------------------------------------------------------*
1298 * uhci_check_transfer
1301 * 0: USB transfer is not finished
1302 * Else: USB transfer is finished
1303 *------------------------------------------------------------------------*/
1305 uhci_check_transfer(struct usb_xfer *xfer)
1311 DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
1313 if (xfer->endpoint->methods == &uhci_device_isoc_methods) {
1314 /* isochronous transfer */
1316 td = xfer->td_transfer_last;
1318 usb_pc_cpu_invalidate(td->page_cache);
1319 status = le32toh(td->td_status);
1321 /* check also if the first is complete */
1323 td = xfer->td_transfer_first;
1325 usb_pc_cpu_invalidate(td->page_cache);
1326 status |= le32toh(td->td_status);
1328 if (!(status & UHCI_TD_ACTIVE)) {
1329 uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1333 /* non-isochronous transfer */
1336 * check whether there is an error somewhere
1337 * in the middle, or whether there was a short
1338 * packet (SPD and not ACTIVE)
1340 td = xfer->td_transfer_cache;
1343 usb_pc_cpu_invalidate(td->page_cache);
1344 status = le32toh(td->td_status);
1345 token = le32toh(td->td_token);
1348 * if there is an active TD the transfer isn't done
1350 if (status & UHCI_TD_ACTIVE) {
1352 xfer->td_transfer_cache = td;
1356 * last transfer descriptor makes the transfer done
1358 if (((void *)td) == xfer->td_transfer_last) {
1362 * any kind of error makes the transfer done
1364 if (status & UHCI_TD_STALLED) {
1368 * check if we reached the last packet
1369 * or if there is a short packet:
1371 if ((td->td_next == htole32(UHCI_PTR_T)) ||
1372 (UHCI_TD_GET_ACTLEN(status) < td->len)) {
1374 if (xfer->flags_int.short_frames_ok) {
1375 /* follow alt next */
1378 xfer->td_transfer_cache = td;
1379 uhci_check_transfer_sub(xfer);
1383 /* transfer is done */
1388 uhci_non_isoc_done(xfer);
1393 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1401 uhci_interrupt_poll(uhci_softc_t *sc)
1403 struct usb_xfer *xfer;
1406 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1408 * check if transfer is transferred
1410 if (uhci_check_transfer(xfer)) {
1411 /* queue has been modified */
1417 /*------------------------------------------------------------------------*
1418 * uhci_interrupt - UHCI interrupt handler
1420 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1421 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1423 *------------------------------------------------------------------------*/
1425 uhci_interrupt(uhci_softc_t *sc)
1429 USB_BUS_LOCK(&sc->sc_bus);
1431 DPRINTFN(16, "real interrupt\n");
1434 if (uhcidebug > 15) {
1438 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1440 /* the interrupt was not for us */
1443 if (status & (UHCI_STS_RD | UHCI_STS_HSE |
1444 UHCI_STS_HCPE | UHCI_STS_HCH)) {
1446 if (status & UHCI_STS_RD) {
1448 kprintf("%s: resume detect\n",
1452 if (status & UHCI_STS_HSE) {
1453 kprintf("%s: host system error\n",
1456 if (status & UHCI_STS_HCPE) {
1457 kprintf("%s: host controller process error\n",
1460 if (status & UHCI_STS_HCH) {
1461 /* no acknowledge needed */
1462 DPRINTF("%s: host controller halted\n",
1465 if (uhcidebug > 0) {
1471 /* get acknowledge bits */
1472 status &= (UHCI_STS_USBINT |
1480 /* nothing to acknowledge */
1483 /* acknowledge interrupts */
1484 UWRITE2(sc, UHCI_STS, status);
1486 /* poll all the USB transfers */
1487 uhci_interrupt_poll(sc);
1490 USB_BUS_UNLOCK(&sc->sc_bus);
1494 * called when a request does not complete
1497 uhci_timeout(void *arg)
1499 struct usb_xfer *xfer = arg;
1501 DPRINTF("xfer=%p\n", xfer);
1503 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
1505 /* transfer is transferred */
1506 uhci_device_done(xfer, USB_ERR_TIMEOUT);
1510 uhci_do_poll(struct usb_bus *bus)
1512 struct uhci_softc *sc = UHCI_BUS2SC(bus);
1514 USB_BUS_LOCK(&sc->sc_bus);
1515 uhci_interrupt_poll(sc);
1516 USB_BUS_UNLOCK(&sc->sc_bus);
1520 uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
1524 uhci_td_t *td_alt_next;
1527 uint8_t shortpkt_old;
1531 shortpkt_old = temp->shortpkt;
1532 len_old = temp->len;
1535 /* software is used to detect short incoming transfers */
1537 if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
1538 temp->td_status |= htole32(UHCI_TD_SPD);
1540 temp->td_status &= ~htole32(UHCI_TD_SPD);
1543 temp->ml.buf_offset = 0;
1547 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1548 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
1551 td_next = temp->td_next;
1555 if (temp->len == 0) {
1557 if (temp->shortpkt) {
1560 /* send a Zero Length Packet, ZLP, last */
1563 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
1568 average = temp->average;
1570 if (temp->len < average) {
1572 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1573 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
1574 average = temp->len;
1578 if (td_next == NULL) {
1579 panic("%s: out of UHCI transfer descriptors!", __func__);
1584 td_next = td->obj_next;
1586 /* check if we are pre-computing */
1590 /* update remaining length */
1592 temp->len -= average;
1596 /* fill out current TD */
1598 td->td_status = temp->td_status;
1599 td->td_token = temp->td_token;
1601 /* update data toggle */
1603 temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
1613 /* update remaining length */
1615 temp->len -= average;
1619 /* fill out buffer pointer and do fixup, if any */
1621 uhci_mem_layout_fixup(&temp->ml, td);
1624 td->alt_next = td_alt_next;
1626 if ((td_next == td_alt_next) && temp->setup_alt_next) {
1627 /* we need to receive these frames one by one ! */
1628 td->td_status |= htole32(UHCI_TD_IOC);
1629 td->td_next = htole32(UHCI_PTR_T);
1632 /* link the current TD with the next one */
1633 td->td_next = td_next->td_self;
1637 usb_pc_cpu_flush(td->page_cache);
1643 /* setup alt next pointer, if any */
1644 if (temp->last_frame) {
1647 /* we use this field internally */
1648 td_alt_next = td_next;
1652 temp->shortpkt = shortpkt_old;
1653 temp->len = len_old;
1657 temp->td_next = td_next;
1661 uhci_setup_standard_chain(struct usb_xfer *xfer)
1663 struct uhci_std_temp temp;
1667 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1668 xfer->address, UE_GET_ADDR(xfer->endpointno),
1669 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1671 temp.average = xfer->max_frame_size;
1672 temp.max_frame_size = xfer->max_frame_size;
1674 /* toggle the DMA set we are using */
1675 xfer->flags_int.curr_dma_set ^= 1;
1677 /* get next DMA set */
1678 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1679 xfer->td_transfer_first = td;
1680 xfer->td_transfer_cache = td;
1684 temp.last_frame = 0;
1685 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1687 uhci_mem_layout_init(&temp.ml, xfer);
1690 htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
1693 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1694 temp.td_status |= htole32(UHCI_TD_LS);
1697 htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) |
1698 UHCI_TD_SET_DEVADDR(xfer->address));
1700 if (xfer->endpoint->toggle_next) {
1702 temp.td_token |= htole32(UHCI_TD_SET_DT(1));
1704 /* check if we should prepend a setup message */
1706 if (xfer->flags_int.control_xfr) {
1708 if (xfer->flags_int.control_hdr) {
1710 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1711 UHCI_TD_SET_ENDPT(0xF));
1712 temp.td_token |= htole32(UHCI_TD_PID_SETUP |
1715 temp.len = xfer->frlengths[0];
1716 temp.ml.buf_pc = xfer->frbuffers + 0;
1717 temp.shortpkt = temp.len ? 1 : 0;
1718 /* check for last frame */
1719 if (xfer->nframes == 1) {
1720 /* no STATUS stage yet, SETUP is last */
1721 if (xfer->flags_int.control_act) {
1722 temp.last_frame = 1;
1723 temp.setup_alt_next = 0;
1726 uhci_setup_standard_chain_sub(&temp);
1733 while (x != xfer->nframes) {
1735 /* DATA0 / DATA1 message */
1737 temp.len = xfer->frlengths[x];
1738 temp.ml.buf_pc = xfer->frbuffers + x;
1742 if (x == xfer->nframes) {
1743 if (xfer->flags_int.control_xfr) {
1744 /* no STATUS stage yet, DATA is last */
1745 if (xfer->flags_int.control_act) {
1746 temp.last_frame = 1;
1747 temp.setup_alt_next = 0;
1750 temp.last_frame = 1;
1751 temp.setup_alt_next = 0;
1755 * Keep previous data toggle,
1756 * device address and endpoint number:
1759 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1760 UHCI_TD_SET_ENDPT(0xF) |
1763 if (temp.len == 0) {
1765 /* make sure that we send an USB packet */
1771 /* regular data transfer */
1773 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1776 /* set endpoint direction */
1779 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1780 htole32(UHCI_TD_PID_IN) :
1781 htole32(UHCI_TD_PID_OUT);
1783 uhci_setup_standard_chain_sub(&temp);
1786 /* check if we should append a status stage */
1788 if (xfer->flags_int.control_xfr &&
1789 !xfer->flags_int.control_act) {
1792 * send a DATA1 message and reverse the current endpoint
1796 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1797 UHCI_TD_SET_ENDPT(0xF) |
1800 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1801 htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
1802 htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
1805 temp.ml.buf_pc = NULL;
1807 temp.last_frame = 1;
1808 temp.setup_alt_next = 0;
1810 uhci_setup_standard_chain_sub(&temp);
1814 /* Ensure that last TD is terminating: */
1815 td->td_next = htole32(UHCI_PTR_T);
1817 /* set interrupt bit */
1819 td->td_status |= htole32(UHCI_TD_IOC);
1821 usb_pc_cpu_flush(td->page_cache);
1823 /* must have at least one frame! */
1825 xfer->td_transfer_last = td;
1828 if (uhcidebug > 8) {
1829 DPRINTF("nexttog=%d; data before transfer:\n",
1830 xfer->endpoint->toggle_next);
1831 uhci_dump_tds(xfer->td_transfer_first);
1834 return (xfer->td_transfer_first);
1837 /* NOTE: "done" can be run two times in a row,
1838 * from close and from interrupt
1842 uhci_device_done(struct usb_xfer *xfer, usb_error_t error)
1844 const struct usb_pipe_methods *methods = xfer->endpoint->methods;
1845 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1848 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
1850 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1851 xfer, xfer->endpoint, error);
1853 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1855 usb_pc_cpu_invalidate(qh->page_cache);
1857 if (xfer->flags_int.bandwidth_reclaimed) {
1858 xfer->flags_int.bandwidth_reclaimed = 0;
1861 if (methods == &uhci_device_bulk_methods) {
1862 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
1864 if (methods == &uhci_device_ctrl_methods) {
1865 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1866 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
1868 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
1871 if (methods == &uhci_device_intr_methods) {
1872 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
1875 * Only finish isochronous transfers once
1876 * which will update "xfer->frlengths".
1878 if (xfer->td_transfer_first &&
1879 xfer->td_transfer_last) {
1880 if (methods == &uhci_device_isoc_methods) {
1881 uhci_isoc_done(sc, xfer);
1883 xfer->td_transfer_first = NULL;
1884 xfer->td_transfer_last = NULL;
1886 /* dequeue transfer and start next transfer */
1887 usbd_transfer_done(xfer, error);
1890 /*------------------------------------------------------------------------*
1892 *------------------------------------------------------------------------*/
1894 uhci_device_bulk_open(struct usb_xfer *xfer)
1900 uhci_device_bulk_close(struct usb_xfer *xfer)
1902 uhci_device_done(xfer, USB_ERR_CANCELLED);
1906 uhci_device_bulk_enter(struct usb_xfer *xfer)
1912 uhci_device_bulk_start(struct usb_xfer *xfer)
1914 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1919 td = uhci_setup_standard_chain(xfer);
1922 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1925 qh->qh_e_next = td->td_self;
1927 if (xfer->xroot->udev->flags.self_suspended == 0) {
1928 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
1930 xfer->flags_int.bandwidth_reclaimed = 1;
1932 usb_pc_cpu_flush(qh->page_cache);
1935 /* put transfer on interrupt queue */
1936 uhci_transfer_intr_enqueue(xfer);
1939 static const struct usb_pipe_methods uhci_device_bulk_methods =
1941 .open = uhci_device_bulk_open,
1942 .close = uhci_device_bulk_close,
1943 .enter = uhci_device_bulk_enter,
1944 .start = uhci_device_bulk_start,
1947 /*------------------------------------------------------------------------*
1948 * uhci control support
1949 *------------------------------------------------------------------------*/
1951 uhci_device_ctrl_open(struct usb_xfer *xfer)
1957 uhci_device_ctrl_close(struct usb_xfer *xfer)
1959 uhci_device_done(xfer, USB_ERR_CANCELLED);
1963 uhci_device_ctrl_enter(struct usb_xfer *xfer)
1969 uhci_device_ctrl_start(struct usb_xfer *xfer)
1971 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1976 td = uhci_setup_standard_chain(xfer);
1979 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1982 qh->qh_e_next = td->td_self;
1985 * NOTE: some devices choke on bandwidth- reclamation for control
1988 if (xfer->xroot->udev->flags.self_suspended == 0) {
1989 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1990 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
1992 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
1995 usb_pc_cpu_flush(qh->page_cache);
1997 /* put transfer on interrupt queue */
1998 uhci_transfer_intr_enqueue(xfer);
2001 static const struct usb_pipe_methods uhci_device_ctrl_methods =
2003 .open = uhci_device_ctrl_open,
2004 .close = uhci_device_ctrl_close,
2005 .enter = uhci_device_ctrl_enter,
2006 .start = uhci_device_ctrl_start,
2009 /*------------------------------------------------------------------------*
2010 * uhci interrupt support
2011 *------------------------------------------------------------------------*/
2013 uhci_device_intr_open(struct usb_xfer *xfer)
2015 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2021 bit = UHCI_IFRAMELIST_COUNT / 2;
2023 if (xfer->interval >= bit) {
2027 if (sc->sc_intr_stat[x] <
2028 sc->sc_intr_stat[best]) {
2038 sc->sc_intr_stat[best]++;
2039 xfer->qh_pos = best;
2041 DPRINTFN(3, "best=%d interval=%d\n",
2042 best, xfer->interval);
2046 uhci_device_intr_close(struct usb_xfer *xfer)
2048 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2050 sc->sc_intr_stat[xfer->qh_pos]--;
2052 uhci_device_done(xfer, USB_ERR_CANCELLED);
2056 uhci_device_intr_enter(struct usb_xfer *xfer)
2062 uhci_device_intr_start(struct usb_xfer *xfer)
2064 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2069 td = uhci_setup_standard_chain(xfer);
2072 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2075 qh->qh_e_next = td->td_self;
2077 if (xfer->xroot->udev->flags.self_suspended == 0) {
2078 /* enter QHs into the controller data structures */
2079 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
2081 usb_pc_cpu_flush(qh->page_cache);
2084 /* put transfer on interrupt queue */
2085 uhci_transfer_intr_enqueue(xfer);
2088 static const struct usb_pipe_methods uhci_device_intr_methods =
2090 .open = uhci_device_intr_open,
2091 .close = uhci_device_intr_close,
2092 .enter = uhci_device_intr_enter,
2093 .start = uhci_device_intr_start,
2096 /*------------------------------------------------------------------------*
2097 * uhci isochronous support
2098 *------------------------------------------------------------------------*/
2100 uhci_device_isoc_open(struct usb_xfer *xfer)
2107 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
2108 UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) :
2109 UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0);
2111 td_token = htole32(td_token);
2113 /* initialize all TD's */
2115 for (ds = 0; ds != 2; ds++) {
2117 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2119 /* mark TD as inactive */
2120 td->td_status = htole32(UHCI_TD_IOS);
2121 td->td_token = td_token;
2123 usb_pc_cpu_flush(td->page_cache);
2129 uhci_device_isoc_close(struct usb_xfer *xfer)
2131 uhci_device_done(xfer, USB_ERR_CANCELLED);
2135 uhci_device_isoc_enter(struct usb_xfer *xfer)
2137 struct uhci_mem_layout ml;
2138 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2148 uhci_td_t *td_last = NULL;
2149 uhci_td_t **pp_last;
2151 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2152 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2154 nframes = UREAD2(sc, UHCI_FRNUM);
2156 temp = (nframes - xfer->endpoint->isoc_next) &
2157 (UHCI_VFRAMELIST_COUNT - 1);
2159 if ((xfer->endpoint->is_synced == 0) ||
2160 (temp < xfer->nframes)) {
2162 * If there is data underflow or the pipe queue is empty we
2163 * schedule the transfer a few frames ahead of the current
2164 * frame position. Else two isochronous transfers might
2167 xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
2168 xfer->endpoint->is_synced = 1;
2169 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2172 * compute how many milliseconds the insertion is ahead of the
2173 * current frame position:
2175 temp = (xfer->endpoint->isoc_next - nframes) &
2176 (UHCI_VFRAMELIST_COUNT - 1);
2179 * pre-compute when the isochronous transfer will be finished:
2181 xfer->isoc_time_complete =
2182 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2185 /* get the real number of frames */
2187 nframes = xfer->nframes;
2189 uhci_mem_layout_init(&ml, xfer);
2191 plen = xfer->frlengths;
2193 /* toggle the DMA set we are using */
2194 xfer->flags_int.curr_dma_set ^= 1;
2196 /* get next DMA set */
2197 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2198 xfer->td_transfer_first = td;
2200 pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next];
2202 /* store starting position */
2204 xfer->qh_pos = xfer->endpoint->isoc_next;
2208 panic("%s:%d: out of TD's\n",
2209 __func__, __LINE__);
2211 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
2212 pp_last = &sc->sc_isoc_p_last[0];
2214 if (*plen > xfer->max_frame_size) {
2218 kprintf("%s: frame length(%d) exceeds %d "
2219 "bytes (frame truncated)\n",
2221 xfer->max_frame_size);
2224 *plen = xfer->max_frame_size;
2226 /* reuse td_token from last transfer */
2228 td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2229 td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
2235 * Do not call "uhci_mem_layout_fixup()" when the
2243 /* fill out buffer pointer and do fixup, if any */
2245 uhci_mem_layout_fixup(&ml, td);
2251 td->td_status = htole32
2252 (UHCI_TD_ZERO_ACTLEN
2253 (UHCI_TD_SET_ERRCNT(0) |
2258 td->td_status = htole32
2259 (UHCI_TD_ZERO_ACTLEN
2260 (UHCI_TD_SET_ERRCNT(0) |
2265 usb_pc_cpu_flush(td->page_cache);
2268 if (uhcidebug > 5) {
2269 DPRINTF("TD %d\n", nframes);
2273 /* insert TD into schedule */
2274 UHCI_APPEND_TD(td, *pp_last);
2282 xfer->td_transfer_last = td_last;
2284 /* update isoc_next */
2285 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
2286 (UHCI_VFRAMELIST_COUNT - 1);
2290 uhci_device_isoc_start(struct usb_xfer *xfer)
2292 /* put transfer on interrupt queue */
2293 uhci_transfer_intr_enqueue(xfer);
2296 static const struct usb_pipe_methods uhci_device_isoc_methods =
2298 .open = uhci_device_isoc_open,
2299 .close = uhci_device_isoc_close,
2300 .enter = uhci_device_isoc_enter,
2301 .start = uhci_device_isoc_start,
2304 /*------------------------------------------------------------------------*
2305 * uhci root control support
2306 *------------------------------------------------------------------------*
2307 * Simulate a hardware hub by handling all the necessary requests.
2308 *------------------------------------------------------------------------*/
2311 struct usb_device_descriptor uhci_devd =
2313 sizeof(struct usb_device_descriptor),
2314 UDESC_DEVICE, /* type */
2315 {0x00, 0x01}, /* USB version */
2316 UDCLASS_HUB, /* class */
2317 UDSUBCLASS_HUB, /* subclass */
2318 UDPROTO_FSHUB, /* protocol */
2319 64, /* max packet */
2320 {0}, {0}, {0x00, 0x01}, /* device id */
2321 1, 2, 0, /* string indicies */
2322 1 /* # of configurations */
2325 static const struct uhci_config_desc uhci_confd = {
2327 .bLength = sizeof(struct usb_config_descriptor),
2328 .bDescriptorType = UDESC_CONFIG,
2329 .wTotalLength[0] = sizeof(uhci_confd),
2331 .bConfigurationValue = 1,
2332 .iConfiguration = 0,
2333 .bmAttributes = UC_SELF_POWERED,
2334 .bMaxPower = 0 /* max power */
2337 .bLength = sizeof(struct usb_interface_descriptor),
2338 .bDescriptorType = UDESC_INTERFACE,
2340 .bInterfaceClass = UICLASS_HUB,
2341 .bInterfaceSubClass = UISUBCLASS_HUB,
2342 .bInterfaceProtocol = UIPROTO_FSHUB,
2345 .bLength = sizeof(struct usb_endpoint_descriptor),
2346 .bDescriptorType = UDESC_ENDPOINT,
2347 .bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
2348 .bmAttributes = UE_INTERRUPT,
2349 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
2355 struct usb_hub_descriptor_min uhci_hubd_piix =
2357 .bDescLength = sizeof(uhci_hubd_piix),
2358 .bDescriptorType = UDESC_HUB,
2360 .wHubCharacteristics = {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
2361 .bPwrOn2PwrGood = 50,
2365 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2366 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2367 * should not be used by the USB subsystem. As we cannot issue a
2368 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2369 * will be enabled as part of the reset.
2371 * On the VT83C572, the port cannot be successfully enabled until the
2372 * outstanding "port enable change" and "connection status change"
2373 * events have been reset.
2376 uhci_portreset(uhci_softc_t *sc, uint16_t index)
2383 port = UHCI_PORTSC1;
2384 else if (index == 2)
2385 port = UHCI_PORTSC2;
2387 return (USB_ERR_IOERROR);
2390 * Before we do anything, turn on SOF messages on the USB
2391 * BUS. Some USB devices do not cope without them!
2395 x = URWMASK(UREAD2(sc, port));
2396 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2398 usb_pause_mtx(&sc->sc_bus.bus_lock,
2399 USB_MS_TO_TICKS(usb_port_root_reset_delay));
2401 DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
2402 index, UREAD2(sc, port));
2404 x = URWMASK(UREAD2(sc, port));
2405 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2408 lockmgr(&sc->sc_bus.bus_lock, LK_RELEASE);
2411 * This delay needs to be exactly 100us, else some USB devices
2416 lockmgr(&sc->sc_bus.bus_lock, LK_EXCLUSIVE);
2418 DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
2419 index, UREAD2(sc, port));
2421 x = URWMASK(UREAD2(sc, port));
2422 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2424 for (lim = 0; lim < 12; lim++) {
2426 usb_pause_mtx(&sc->sc_bus.bus_lock,
2427 USB_MS_TO_TICKS(usb_port_reset_delay));
2429 x = UREAD2(sc, port);
2431 DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
2434 if (!(x & UHCI_PORTSC_CCS)) {
2436 * No device is connected (or was disconnected
2437 * during reset). Consider the port reset.
2438 * The delay must be long enough to ensure on
2439 * the initial iteration that the device
2440 * connection will have been registered. 50ms
2441 * appears to be sufficient, but 20ms is not.
2443 DPRINTFN(4, "uhci port %d loop %u, device detached\n",
2447 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
2449 * Port enabled changed and/or connection
2450 * status changed were set. Reset either or
2451 * both raised flags (by writing a 1 to that
2452 * bit), and wait again for state to settle.
2454 UWRITE2(sc, port, URWMASK(x) |
2455 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
2458 if (x & UHCI_PORTSC_PE) {
2459 /* port is enabled */
2462 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
2465 DPRINTFN(2, "uhci port %d reset timed out\n", index);
2466 return (USB_ERR_TIMEOUT);
2469 DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
2470 index, UREAD2(sc, port));
2473 return (USB_ERR_NORMAL_COMPLETION);
2477 uhci_roothub_exec(struct usb_device *udev,
2478 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2480 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
2482 const char *str_ptr;
2492 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2495 ptr = (const void *)&sc->sc_hub_desc.temp;
2499 value = UGETW(req->wValue);
2500 index = UGETW(req->wIndex);
2502 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2503 "wValue=0x%04x wIndex=0x%04x\n",
2504 req->bmRequestType, req->bRequest,
2505 UGETW(req->wLength), value, index);
2507 #define C(x,y) ((x) | ((y) << 8))
2508 switch (C(req->bRequest, req->bmRequestType)) {
2509 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2510 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2511 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2513 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2514 * for the integrated root hub.
2517 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2519 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2521 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2522 switch (value >> 8) {
2524 if ((value & 0xff) != 0) {
2525 err = USB_ERR_IOERROR;
2528 len = sizeof(uhci_devd);
2529 ptr = (const void *)&uhci_devd;
2533 if ((value & 0xff) != 0) {
2534 err = USB_ERR_IOERROR;
2537 len = sizeof(uhci_confd);
2538 ptr = (const void *)&uhci_confd;
2542 switch (value & 0xff) {
2543 case 0: /* Language table */
2547 case 1: /* Vendor */
2548 str_ptr = sc->sc_vendor;
2551 case 2: /* Product */
2552 str_ptr = "UHCI root HUB";
2560 len = usb_make_str_desc
2561 (sc->sc_hub_desc.temp,
2562 sizeof(sc->sc_hub_desc.temp),
2567 err = USB_ERR_IOERROR;
2571 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2573 sc->sc_hub_desc.temp[0] = 0;
2575 case C(UR_GET_STATUS, UT_READ_DEVICE):
2577 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
2579 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2580 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2582 USETW(sc->sc_hub_desc.stat.wStatus, 0);
2584 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2585 if (value >= UHCI_MAX_DEVICES) {
2586 err = USB_ERR_IOERROR;
2589 sc->sc_addr = value;
2591 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2592 if ((value != 0) && (value != 1)) {
2593 err = USB_ERR_IOERROR;
2596 sc->sc_conf = value;
2598 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2600 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2601 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2602 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2603 err = USB_ERR_IOERROR;
2605 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2607 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2610 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2612 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2613 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
2614 "port=%d feature=%d\n",
2617 port = UHCI_PORTSC1;
2618 else if (index == 2)
2619 port = UHCI_PORTSC2;
2621 err = USB_ERR_IOERROR;
2625 case UHF_PORT_ENABLE:
2626 x = URWMASK(UREAD2(sc, port));
2627 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2629 case UHF_PORT_SUSPEND:
2630 x = URWMASK(UREAD2(sc, port));
2631 UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
2633 case UHF_PORT_RESET:
2634 x = URWMASK(UREAD2(sc, port));
2635 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2637 case UHF_C_PORT_CONNECTION:
2638 x = URWMASK(UREAD2(sc, port));
2639 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2641 case UHF_C_PORT_ENABLE:
2642 x = URWMASK(UREAD2(sc, port));
2643 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2645 case UHF_C_PORT_OVER_CURRENT:
2646 x = URWMASK(UREAD2(sc, port));
2647 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2649 case UHF_C_PORT_RESET:
2651 err = USB_ERR_NORMAL_COMPLETION;
2653 case UHF_C_PORT_SUSPEND:
2654 sc->sc_isresumed &= ~(1 << index);
2656 case UHF_PORT_CONNECTION:
2657 case UHF_PORT_OVER_CURRENT:
2658 case UHF_PORT_POWER:
2659 case UHF_PORT_LOW_SPEED:
2661 err = USB_ERR_IOERROR;
2665 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2667 port = UHCI_PORTSC1;
2668 else if (index == 2)
2669 port = UHCI_PORTSC2;
2671 err = USB_ERR_IOERROR;
2675 sc->sc_hub_desc.temp[0] =
2676 ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2677 UHCI_PORTSC_LS_SHIFT);
2679 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2680 if ((value & 0xff) != 0) {
2681 err = USB_ERR_IOERROR;
2684 len = sizeof(uhci_hubd_piix);
2685 ptr = (const void *)&uhci_hubd_piix;
2687 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2689 memset(sc->sc_hub_desc.temp, 0, 16);
2691 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2693 port = UHCI_PORTSC1;
2694 else if (index == 2)
2695 port = UHCI_PORTSC2;
2697 err = USB_ERR_IOERROR;
2700 x = UREAD2(sc, port);
2701 status = change = 0;
2702 if (x & UHCI_PORTSC_CCS)
2703 status |= UPS_CURRENT_CONNECT_STATUS;
2704 if (x & UHCI_PORTSC_CSC)
2705 change |= UPS_C_CONNECT_STATUS;
2706 if (x & UHCI_PORTSC_PE)
2707 status |= UPS_PORT_ENABLED;
2708 if (x & UHCI_PORTSC_POEDC)
2709 change |= UPS_C_PORT_ENABLED;
2710 if (x & UHCI_PORTSC_OCI)
2711 status |= UPS_OVERCURRENT_INDICATOR;
2712 if (x & UHCI_PORTSC_OCIC)
2713 change |= UPS_C_OVERCURRENT_INDICATOR;
2714 if (x & UHCI_PORTSC_LSDA)
2715 status |= UPS_LOW_SPEED;
2716 if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
2717 /* need to do a write back */
2718 UWRITE2(sc, port, URWMASK(x));
2720 /* wait 20ms for resume sequence to complete */
2721 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 50);
2723 /* clear suspend and resume detect */
2724 UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
2727 /* wait a little bit */
2728 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 500);
2730 sc->sc_isresumed |= (1 << index);
2732 } else if (x & UHCI_PORTSC_SUSP) {
2733 status |= UPS_SUSPEND;
2735 status |= UPS_PORT_POWER;
2736 if (sc->sc_isresumed & (1 << index))
2737 change |= UPS_C_SUSPEND;
2739 change |= UPS_C_PORT_RESET;
2740 USETW(sc->sc_hub_desc.ps.wPortStatus, status);
2741 USETW(sc->sc_hub_desc.ps.wPortChange, change);
2742 len = sizeof(sc->sc_hub_desc.ps);
2744 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2745 err = USB_ERR_IOERROR;
2747 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2749 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2751 port = UHCI_PORTSC1;
2752 else if (index == 2)
2753 port = UHCI_PORTSC2;
2755 err = USB_ERR_IOERROR;
2759 case UHF_PORT_ENABLE:
2760 x = URWMASK(UREAD2(sc, port));
2761 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2763 case UHF_PORT_SUSPEND:
2764 x = URWMASK(UREAD2(sc, port));
2765 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2767 case UHF_PORT_RESET:
2768 err = uhci_portreset(sc, index);
2770 case UHF_PORT_POWER:
2771 /* pretend we turned on power */
2772 err = USB_ERR_NORMAL_COMPLETION;
2774 case UHF_C_PORT_CONNECTION:
2775 case UHF_C_PORT_ENABLE:
2776 case UHF_C_PORT_OVER_CURRENT:
2777 case UHF_PORT_CONNECTION:
2778 case UHF_PORT_OVER_CURRENT:
2779 case UHF_PORT_LOW_SPEED:
2780 case UHF_C_PORT_SUSPEND:
2781 case UHF_C_PORT_RESET:
2783 err = USB_ERR_IOERROR;
2788 err = USB_ERR_IOERROR;
2798 * This routine is executed periodically and simulates interrupts from
2799 * the root controller interrupt pipe for port status change:
2802 uhci_root_intr(uhci_softc_t *sc)
2806 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2808 sc->sc_hub_idata[0] = 0;
2810 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
2811 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2812 sc->sc_hub_idata[0] |= 1 << 1;
2814 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
2815 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2816 sc->sc_hub_idata[0] |= 1 << 2;
2820 usb_callout_reset(&sc->sc_root_intr, hz,
2821 (void *)&uhci_root_intr, sc);
2823 if (sc->sc_hub_idata[0] != 0) {
2824 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2825 sizeof(sc->sc_hub_idata));
2830 uhci_xfer_setup(struct usb_setup_params *parm)
2832 struct usb_page_search page_info;
2833 struct usb_page_cache *pc;
2835 struct usb_xfer *xfer;
2843 sc = UHCI_BUS2SC(parm->udev->bus);
2844 xfer = parm->curr_xfer;
2846 parm->hc_max_packet_size = 0x500;
2847 parm->hc_max_packet_count = 1;
2848 parm->hc_max_frame_size = 0x500;
2851 * compute ntd and nqh
2853 if (parm->methods == &uhci_device_ctrl_methods) {
2854 xfer->flags_int.bdma_enable = 1;
2855 xfer->flags_int.bdma_no_post_sync = 1;
2857 usbd_transfer_setup_sub(parm);
2859 /* see EHCI HC driver for proof of "ntd" formula */
2862 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
2863 + (xfer->max_data_length / xfer->max_frame_size));
2865 } else if (parm->methods == &uhci_device_bulk_methods) {
2866 xfer->flags_int.bdma_enable = 1;
2867 xfer->flags_int.bdma_no_post_sync = 1;
2869 usbd_transfer_setup_sub(parm);
2872 ntd = ((2 * xfer->nframes)
2873 + (xfer->max_data_length / xfer->max_frame_size));
2875 } else if (parm->methods == &uhci_device_intr_methods) {
2876 xfer->flags_int.bdma_enable = 1;
2877 xfer->flags_int.bdma_no_post_sync = 1;
2879 usbd_transfer_setup_sub(parm);
2882 ntd = ((2 * xfer->nframes)
2883 + (xfer->max_data_length / xfer->max_frame_size));
2885 } else if (parm->methods == &uhci_device_isoc_methods) {
2886 xfer->flags_int.bdma_enable = 1;
2887 xfer->flags_int.bdma_no_post_sync = 1;
2889 usbd_transfer_setup_sub(parm);
2892 ntd = xfer->nframes;
2896 usbd_transfer_setup_sub(parm);
2906 * NOTE: the UHCI controller requires that
2907 * every packet must be contiguous on
2908 * the same USB memory page !
2910 nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
2913 * Compute a suitable power of two alignment
2914 * for our "max_frame_size" fixup buffer(s):
2916 align = xfer->max_frame_size;
2923 /* check for power of two */
2924 if (!(xfer->max_frame_size &
2925 (xfer->max_frame_size - 1))) {
2929 * We don't allow alignments of
2930 * less than 8 bytes:
2932 * NOTE: Allocating using an aligment
2933 * of 1 byte has special meaning!
2940 if (usbd_transfer_setup_sub_malloc(
2941 parm, &pc, xfer->max_frame_size,
2943 parm->err = USB_ERR_NOMEM;
2946 xfer->buf_fixup = pc;
2955 if (usbd_transfer_setup_sub_malloc(
2956 parm, &pc, sizeof(uhci_td_t),
2957 UHCI_TD_ALIGN, ntd)) {
2958 parm->err = USB_ERR_NOMEM;
2962 for (n = 0; n != ntd; n++) {
2965 usbd_get_page(pc + n, 0, &page_info);
2967 td = page_info.buffer;
2970 if ((parm->methods == &uhci_device_bulk_methods) ||
2971 (parm->methods == &uhci_device_ctrl_methods) ||
2972 (parm->methods == &uhci_device_intr_methods)) {
2973 /* set depth first bit */
2974 td->td_self = htole32(page_info.physaddr |
2975 UHCI_PTR_TD | UHCI_PTR_VF);
2977 td->td_self = htole32(page_info.physaddr |
2981 td->obj_next = last_obj;
2982 td->page_cache = pc + n;
2986 usb_pc_cpu_flush(pc + n);
2989 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
2993 if (usbd_transfer_setup_sub_malloc(
2994 parm, &pc, sizeof(uhci_qh_t),
2995 UHCI_QH_ALIGN, nqh)) {
2996 parm->err = USB_ERR_NOMEM;
3000 for (n = 0; n != nqh; n++) {
3003 usbd_get_page(pc + n, 0, &page_info);
3005 qh = page_info.buffer;
3008 qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
3009 qh->obj_next = last_obj;
3010 qh->page_cache = pc + n;
3014 usb_pc_cpu_flush(pc + n);
3017 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3019 if (!xfer->flags_int.curr_dma_set) {
3020 xfer->flags_int.curr_dma_set = 1;
3026 uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3027 struct usb_endpoint *ep)
3029 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
3031 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3033 edesc->bEndpointAddress, udev->flags.usb_mode,
3036 if (udev->device_index != sc->sc_addr) {
3037 switch (edesc->bmAttributes & UE_XFERTYPE) {
3039 ep->methods = &uhci_device_ctrl_methods;
3042 ep->methods = &uhci_device_intr_methods;
3044 case UE_ISOCHRONOUS:
3045 if (udev->speed == USB_SPEED_FULL) {
3046 ep->methods = &uhci_device_isoc_methods;
3050 ep->methods = &uhci_device_bulk_methods;
3060 uhci_xfer_unsetup(struct usb_xfer *xfer)
3066 uhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3069 * Wait until hardware has finished any possible use of the
3070 * transfer descriptor(s) and QH
3072 *pus = (1125); /* microseconds */
3076 uhci_device_resume(struct usb_device *udev)
3078 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3079 struct usb_xfer *xfer;
3080 const struct usb_pipe_methods *methods;
3085 USB_BUS_LOCK(udev->bus);
3087 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3089 if (xfer->xroot->udev == udev) {
3091 methods = xfer->endpoint->methods;
3092 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3094 if (methods == &uhci_device_bulk_methods) {
3095 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
3097 xfer->flags_int.bandwidth_reclaimed = 1;
3099 if (methods == &uhci_device_ctrl_methods) {
3100 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3101 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
3103 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
3106 if (methods == &uhci_device_intr_methods) {
3107 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3112 USB_BUS_UNLOCK(udev->bus);
3118 uhci_device_suspend(struct usb_device *udev)
3120 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3121 struct usb_xfer *xfer;
3122 const struct usb_pipe_methods *methods;
3127 USB_BUS_LOCK(udev->bus);
3129 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3131 if (xfer->xroot->udev == udev) {
3133 methods = xfer->endpoint->methods;
3134 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3136 if (xfer->flags_int.bandwidth_reclaimed) {
3137 xfer->flags_int.bandwidth_reclaimed = 0;
3140 if (methods == &uhci_device_bulk_methods) {
3141 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
3143 if (methods == &uhci_device_ctrl_methods) {
3144 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3145 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
3147 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
3150 if (methods == &uhci_device_intr_methods) {
3151 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3156 USB_BUS_UNLOCK(udev->bus);
3162 uhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3164 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3167 case USB_HW_POWER_SUSPEND:
3168 case USB_HW_POWER_SHUTDOWN:
3171 case USB_HW_POWER_RESUME:
3180 uhci_set_hw_power(struct usb_bus *bus)
3182 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3189 flags = bus->hw_power_state;
3192 * WARNING: Some FULL speed USB devices require periodic SOF
3193 * messages! If any USB devices are connected through the
3194 * UHCI, power save will be disabled!
3196 if (flags & (USB_HW_POWER_CONTROL |
3197 USB_HW_POWER_NON_ROOT_HUB |
3199 USB_HW_POWER_INTERRUPT |
3200 USB_HW_POWER_ISOC)) {
3201 DPRINTF("Some USB transfer is "
3202 "active on unit %u.\n",
3203 device_get_unit(sc->sc_bus.bdev));
3206 DPRINTF("Power save on unit %u.\n",
3207 device_get_unit(sc->sc_bus.bdev));
3208 UHCICMD(sc, UHCI_CMD_MAXP);
3211 USB_BUS_UNLOCK(bus);
3217 static const struct usb_bus_methods uhci_bus_methods =
3219 .endpoint_init = uhci_ep_init,
3220 .xfer_setup = uhci_xfer_setup,
3221 .xfer_unsetup = uhci_xfer_unsetup,
3222 .get_dma_delay = uhci_get_dma_delay,
3223 .device_resume = uhci_device_resume,
3224 .device_suspend = uhci_device_suspend,
3225 .set_hw_power = uhci_set_hw_power,
3226 .set_hw_power_sleep = uhci_set_hw_power_sleep,
3227 .roothub_exec = uhci_roothub_exec,
3228 .xfer_poll = uhci_do_poll,