2 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * USB Universal Host Controller driver.
33 * Handles e.g. PIIX3 and PIIX4.
35 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
36 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
37 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
38 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
41 #include <sys/stdint.h>
42 #include <sys/stddef.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
63 #define USB_DEBUG_VAR uhcidebug
65 #include <dev/usb/usb_core.h>
66 #include <dev/usb/usb_debug.h>
67 #include <dev/usb/usb_busdma.h>
68 #include <dev/usb/usb_process.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_device.h>
71 #include <dev/usb/usb_hub.h>
72 #include <dev/usb/usb_util.h>
74 #include <dev/usb/usb_controller.h>
75 #include <dev/usb/usb_bus.h>
76 #include <dev/usb/controller/uhci.h>
77 #include <dev/usb/controller/uhcireg.h>
80 #define UHCI_BUS2SC(bus) \
81 ((uhci_softc_t *)(((uint8_t *)(bus)) - \
82 ((uint8_t *)&(((uhci_softc_t *)0)->sc_bus))))
85 static int uhcidebug = 0;
86 static int uhcinoloop = 0;
88 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
89 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
90 &uhcidebug, 0, "uhci debug level");
91 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
92 &uhcinoloop, 0, "uhci noloop");
94 TUNABLE_INT("hw.usb.uhci.debug", &uhcidebug);
95 TUNABLE_INT("hw.usb.uhci.loop", &uhcinoloop);
97 static void uhci_dumpregs(uhci_softc_t *sc);
98 static void uhci_dump_tds(uhci_td_t *td);
102 #define UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
103 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
104 #define UWRITE1(sc, r, x) \
105 do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
106 } while (/*CONSTCOND*/0)
107 #define UWRITE2(sc, r, x) \
108 do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
109 } while (/*CONSTCOND*/0)
110 #define UWRITE4(sc, r, x) \
111 do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
112 } while (/*CONSTCOND*/0)
113 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
114 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
115 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
117 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
118 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
120 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
122 #define UHCI_INTR_ENDPT 1
124 struct uhci_mem_layout {
126 struct usb_page_search buf_res;
127 struct usb_page_search fix_res;
129 struct usb_page_cache *buf_pc;
130 struct usb_page_cache *fix_pc;
134 uint16_t max_frame_size;
137 struct uhci_std_temp {
139 struct uhci_mem_layout ml;
146 uint16_t max_frame_size;
148 uint8_t setup_alt_next;
152 extern struct usb_bus_methods uhci_bus_methods;
153 extern struct usb_pipe_methods uhci_device_bulk_methods;
154 extern struct usb_pipe_methods uhci_device_ctrl_methods;
155 extern struct usb_pipe_methods uhci_device_intr_methods;
156 extern 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) {
221 /* need to do a fixup */
223 usbd_get_page(ml->fix_pc, 0, &ml->fix_res);
225 td->td_buffer = htole32(ml->fix_res.physaddr);
228 * The UHCI driver cannot handle
229 * page crossings, so a fixup is
242 if ((td->td_token & htole32(UHCI_TD_PID)) ==
243 htole32(UHCI_TD_PID_IN)) {
244 td->fix_pc = ml->fix_pc;
245 usb_pc_cpu_invalidate(ml->fix_pc);
250 /* copy data to fixup location */
252 usbd_copy_out(ml->buf_pc, ml->buf_offset,
253 ml->fix_res.buffer, td->len);
255 usb_pc_cpu_flush(ml->fix_pc);
258 /* prepare next fixup */
264 td->td_buffer = htole32(ml->buf_res.physaddr);
268 /* prepare next data location */
270 ml->buf_offset += td->len;
279 uhci_restart(uhci_softc_t *sc)
281 struct usb_page_search buf_res;
283 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
285 if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) {
286 DPRINTFN(2, "Already started\n");
290 DPRINTFN(2, "Restarting\n");
292 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
294 /* Reload fresh base address */
295 UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
298 * Assume 64 byte packets at frame end and start HC controller:
300 UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
302 /* wait 10 milliseconds */
304 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
306 /* check that controller has started */
308 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
309 DPRINTFN(2, "Failed\n");
316 uhci_reset(uhci_softc_t *sc)
320 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
322 DPRINTF("resetting the HC\n");
324 /* disable interrupts */
326 UWRITE2(sc, UHCI_INTR, 0);
330 UHCICMD(sc, UHCI_CMD_GRESET);
334 usb_pause_mtx(&sc->sc_bus.bus_mtx,
335 USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
337 /* terminate all transfers */
339 UHCICMD(sc, UHCI_CMD_HCRESET);
341 /* the reset bit goes low when the controller is done */
343 n = UHCI_RESET_TIMEOUT;
345 /* wait one millisecond */
347 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
349 if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
354 device_printf(sc->sc_bus.bdev,
355 "controller did not reset\n");
361 /* wait one millisecond */
363 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
365 /* check if HC is stopped */
366 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
371 device_printf(sc->sc_bus.bdev,
372 "controller did not stop\n");
376 /* reload the configuration */
377 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
378 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
380 USB_BUS_UNLOCK(&sc->sc_bus);
382 /* stop root interrupt */
383 usb_callout_drain(&sc->sc_root_intr);
385 USB_BUS_LOCK(&sc->sc_bus);
389 uhci_start(uhci_softc_t *sc)
391 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
393 DPRINTFN(2, "enabling\n");
395 /* enable interrupts */
397 UWRITE2(sc, UHCI_INTR,
403 if (uhci_restart(sc)) {
404 device_printf(sc->sc_bus.bdev,
405 "cannot start HC controller\n");
408 /* start root interrupt */
412 static struct uhci_qh *
413 uhci_init_qh(struct usb_page_cache *pc)
415 struct usb_page_search buf_res;
418 usbd_get_page(pc, 0, &buf_res);
423 htole32(buf_res.physaddr) |
424 htole32(UHCI_PTR_QH);
431 static struct uhci_td *
432 uhci_init_td(struct usb_page_cache *pc)
434 struct usb_page_search buf_res;
437 usbd_get_page(pc, 0, &buf_res);
442 htole32(buf_res.physaddr) |
443 htole32(UHCI_PTR_TD);
451 uhci_init(uhci_softc_t *sc)
459 usb_callout_init_mtx(&sc->sc_root_intr, &sc->sc_bus.bus_mtx, 0);
466 sc->sc_saved_sof = 0x40; /* default value */
467 sc->sc_saved_frnum = 0; /* default frame number */
472 sc->sc_ls_ctl_p_last =
473 uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc);
475 sc->sc_fs_ctl_p_last =
476 uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc);
479 uhci_init_qh(&sc->sc_hw.bulk_start_pc);
481 sc->sc_reclaim_qh_p =
482 sc->sc_fs_ctl_p_last;
484 /* setup reclaim looping point */
485 sc->sc_reclaim_qh_p =
490 uhci_init_qh(&sc->sc_hw.last_qh_pc);
493 uhci_init_td(&sc->sc_hw.last_td_pc);
495 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
496 sc->sc_isoc_p_last[x] =
497 uhci_init_td(sc->sc_hw.isoc_start_pc + x);
500 for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) {
501 sc->sc_intr_p_last[x] =
502 uhci_init_qh(sc->sc_hw.intr_start_pc + x);
506 * the QHs are arranged to give poll intervals that are
507 * powers of 2 times 1ms
509 bit = UHCI_IFRAMELIST_COUNT / 2;
516 y = (x ^ bit) | (bit / 2);
519 * the next QH has half the poll interval
521 qh_x = sc->sc_intr_p_last[x];
522 qh_y = sc->sc_intr_p_last[y];
525 qh_x->qh_h_next = qh_y->qh_self;
527 qh_x->qh_e_next = htole32(UHCI_PTR_T);
537 qh_ls = sc->sc_ls_ctl_p_last;
538 qh_intr = sc->sc_intr_p_last[0];
540 /* start QH for interrupt traffic */
541 qh_intr->h_next = qh_ls;
542 qh_intr->qh_h_next = qh_ls->qh_self;
544 qh_intr->qh_e_next = htole32(UHCI_PTR_T);
546 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
551 td_x = sc->sc_isoc_p_last[x];
552 qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)];
554 /* start TD for isochronous traffic */
556 td_x->td_next = qh_intr->qh_self;
557 td_x->td_status = htole32(UHCI_TD_IOS);
558 td_x->td_token = htole32(0);
559 td_x->td_buffer = htole32(0);
566 qh_ls = sc->sc_ls_ctl_p_last;
567 qh_fs = sc->sc_fs_ctl_p_last;
569 /* start QH where low speed control traffic will be queued */
570 qh_ls->h_next = qh_fs;
571 qh_ls->qh_h_next = qh_fs->qh_self;
573 qh_ls->qh_e_next = htole32(UHCI_PTR_T);
581 qh_ctl = sc->sc_fs_ctl_p_last;
582 qh_blk = sc->sc_bulk_p_last;
584 /* start QH where full speed control traffic will be queued */
585 qh_ctl->h_next = qh_blk;
586 qh_ctl->qh_h_next = qh_blk->qh_self;
588 qh_ctl->qh_e_next = htole32(UHCI_PTR_T);
590 qh_lst = sc->sc_last_qh_p;
592 /* start QH where bulk traffic will be queued */
593 qh_blk->h_next = qh_lst;
594 qh_blk->qh_h_next = qh_lst->qh_self;
596 qh_blk->qh_e_next = htole32(UHCI_PTR_T);
598 td_lst = sc->sc_last_td_p;
600 /* end QH which is used for looping the QHs */
602 qh_lst->qh_h_next = htole32(UHCI_PTR_T); /* end of QH chain */
603 qh_lst->e_next = td_lst;
604 qh_lst->qh_e_next = td_lst->td_self;
607 * end TD which hangs from the last QH, to avoid a bug in the PIIX
608 * that makes it run berserk otherwise
611 td_lst->td_next = htole32(UHCI_PTR_T);
612 td_lst->td_status = htole32(0); /* inactive */
613 td_lst->td_token = htole32(0);
614 td_lst->td_buffer = htole32(0);
617 struct usb_page_search buf_res;
620 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
622 pframes = buf_res.buffer;
626 * Setup UHCI framelist
630 * pframes -> full speed isochronous -> interrupt QH's -> low
631 * speed control -> full speed control -> bulk transfers
635 for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
637 sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
640 /* flush all cache into memory */
642 usb_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
644 /* set up the bus struct */
645 sc->sc_bus.methods = &uhci_bus_methods;
647 USB_BUS_LOCK(&sc->sc_bus);
648 /* reset the controller */
651 /* start the controller */
653 USB_BUS_UNLOCK(&sc->sc_bus);
655 /* catch lost interrupts */
656 uhci_do_poll(&sc->sc_bus);
661 /* NOTE: suspend/resume is called from
662 * interrupt context and cannot sleep!
666 uhci_suspend(uhci_softc_t *sc)
668 USB_BUS_LOCK(&sc->sc_bus);
675 /* save some state if BIOS doesn't */
677 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
678 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
680 /* stop the controller */
684 /* enter global suspend */
686 UHCICMD(sc, UHCI_CMD_EGSM);
688 usb_pause_mtx(&sc->sc_bus.bus_mtx,
689 USB_MS_TO_TICKS(USB_RESUME_WAIT));
691 USB_BUS_UNLOCK(&sc->sc_bus);
695 uhci_resume(uhci_softc_t *sc)
697 USB_BUS_LOCK(&sc->sc_bus);
699 /* reset the controller */
703 /* force global resume */
705 UHCICMD(sc, UHCI_CMD_FGR);
707 usb_pause_mtx(&sc->sc_bus.bus_mtx,
708 USB_MS_TO_TICKS(USB_RESUME_DELAY));
710 /* and start traffic again */
720 USB_BUS_UNLOCK(&sc->sc_bus);
722 /* catch lost interrupts */
723 uhci_do_poll(&sc->sc_bus);
728 uhci_dumpregs(uhci_softc_t *sc)
730 DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
731 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
732 device_get_nameunit(sc->sc_bus.bdev),
733 UREAD2(sc, UHCI_CMD),
734 UREAD2(sc, UHCI_STS),
735 UREAD2(sc, UHCI_INTR),
736 UREAD2(sc, UHCI_FRNUM),
737 UREAD4(sc, UHCI_FLBASEADDR),
738 UREAD1(sc, UHCI_SOF),
739 UREAD2(sc, UHCI_PORTSC1),
740 UREAD2(sc, UHCI_PORTSC2));
744 uhci_dump_td(uhci_td_t *p)
751 usb_pc_cpu_invalidate(p->page_cache);
753 td_next = le32toh(p->td_next);
754 td_status = le32toh(p->td_status);
755 td_token = le32toh(p->td_token);
758 * Check whether the link pointer in this TD marks the link pointer
761 temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
763 printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
764 "token=0x%08x buffer=0x%08x\n",
770 le32toh(p->td_buffer));
772 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,"
773 "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
775 (td_next & 1) ? "-T" : "",
776 (td_next & 2) ? "-Q" : "",
777 (td_next & 4) ? "-VF" : "",
778 (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
779 (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
780 (td_status & UHCI_TD_NAK) ? "-NAK" : "",
781 (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
782 (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
783 (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
784 (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
785 (td_status & UHCI_TD_IOC) ? "-IOC" : "",
786 (td_status & UHCI_TD_IOS) ? "-IOS" : "",
787 (td_status & UHCI_TD_LS) ? "-LS" : "",
788 (td_status & UHCI_TD_SPD) ? "-SPD" : "",
789 UHCI_TD_GET_ERRCNT(td_status),
790 UHCI_TD_GET_ACTLEN(td_status),
791 UHCI_TD_GET_PID(td_token),
792 UHCI_TD_GET_DEVADDR(td_token),
793 UHCI_TD_GET_ENDPT(td_token),
794 UHCI_TD_GET_DT(td_token),
795 UHCI_TD_GET_MAXLEN(td_token));
801 uhci_dump_qh(uhci_qh_t *sqh)
807 usb_pc_cpu_invalidate(sqh->page_cache);
809 qh_h_next = le32toh(sqh->qh_h_next);
810 qh_e_next = le32toh(sqh->qh_e_next);
812 DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
813 le32toh(sqh->qh_self), qh_h_next, qh_e_next);
815 temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
816 (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
822 uhci_dump_all(uhci_softc_t *sc)
825 uhci_dump_qh(sc->sc_ls_ctl_p_last);
826 uhci_dump_qh(sc->sc_fs_ctl_p_last);
827 uhci_dump_qh(sc->sc_bulk_p_last);
828 uhci_dump_qh(sc->sc_last_qh_p);
832 uhci_dump_tds(uhci_td_t *td)
837 if (uhci_dump_td(td)) {
846 * Let the last QH loop back to the full speed control transfer QH.
847 * This is what intel calls "bandwidth reclamation" and improves
848 * USB performance a lot for some devices.
849 * If we are already looping, just count it.
852 uhci_add_loop(uhci_softc_t *sc)
854 struct uhci_qh *qh_lst;
855 struct uhci_qh *qh_rec;
862 if (++(sc->sc_loops) == 1) {
863 DPRINTFN(6, "add\n");
865 qh_lst = sc->sc_last_qh_p;
866 qh_rec = sc->sc_reclaim_qh_p;
868 /* NOTE: we don't loop back the soft pointer */
870 qh_lst->qh_h_next = qh_rec->qh_self;
871 usb_pc_cpu_flush(qh_lst->page_cache);
876 uhci_rem_loop(uhci_softc_t *sc)
878 struct uhci_qh *qh_lst;
885 if (--(sc->sc_loops) == 0) {
886 DPRINTFN(6, "remove\n");
888 qh_lst = sc->sc_last_qh_p;
889 qh_lst->qh_h_next = htole32(UHCI_PTR_T);
890 usb_pc_cpu_flush(qh_lst->page_cache);
895 uhci_transfer_intr_enqueue(struct usb_xfer *xfer)
897 /* check for early completion */
898 if (uhci_check_transfer(xfer)) {
901 /* put transfer on interrupt queue */
902 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
904 /* start timeout, if any */
905 if (xfer->timeout != 0) {
906 usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
910 #define UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
912 _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
914 DPRINTFN(11, "%p to %p\n", std, last);
916 /* (sc->sc_bus.mtx) must be locked */
918 std->next = last->next;
919 std->td_next = last->td_next;
923 usb_pc_cpu_flush(std->page_cache);
926 * the last->next->prev is never followed: std->next->prev = std;
929 last->td_next = std->td_self;
931 usb_pc_cpu_flush(last->page_cache);
936 #define UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
938 _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
940 DPRINTFN(11, "%p to %p\n", sqh, last);
942 if (sqh->h_prev != NULL) {
943 /* should not happen */
944 DPRINTFN(0, "QH already linked!\n");
947 /* (sc->sc_bus.mtx) must be locked */
949 sqh->h_next = last->h_next;
950 sqh->qh_h_next = last->qh_h_next;
954 usb_pc_cpu_flush(sqh->page_cache);
957 * The "last->h_next->h_prev" is never followed:
959 * "sqh->h_next->h_prev" = sqh;
963 last->qh_h_next = sqh->qh_self;
965 usb_pc_cpu_flush(last->page_cache);
972 #define UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
974 _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
976 DPRINTFN(11, "%p from %p\n", std, last);
978 /* (sc->sc_bus.mtx) must be locked */
980 std->prev->next = std->next;
981 std->prev->td_next = std->td_next;
983 usb_pc_cpu_flush(std->prev->page_cache);
986 std->next->prev = std->prev;
987 usb_pc_cpu_flush(std->next->page_cache);
989 return ((last == std) ? std->prev : last);
992 #define UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
994 _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
996 DPRINTFN(11, "%p from %p\n", sqh, last);
998 /* (sc->sc_bus.mtx) must be locked */
1000 /* only remove if not removed from a queue */
1003 sqh->h_prev->h_next = sqh->h_next;
1004 sqh->h_prev->qh_h_next = sqh->qh_h_next;
1006 usb_pc_cpu_flush(sqh->h_prev->page_cache);
1009 sqh->h_next->h_prev = sqh->h_prev;
1010 usb_pc_cpu_flush(sqh->h_next->page_cache);
1012 last = ((last == sqh) ? sqh->h_prev : last);
1016 usb_pc_cpu_flush(sqh->page_cache);
1022 uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer)
1024 struct usb_page_search res;
1025 uint32_t nframes = xfer->nframes;
1027 uint32_t offset = 0;
1028 uint32_t *plen = xfer->frlengths;
1030 uhci_td_t *td = xfer->td_transfer_first;
1031 uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
1033 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1034 xfer, xfer->endpoint);
1036 /* sync any DMA memory before doing fixups */
1038 usb_bdma_post_sync(xfer);
1042 panic("%s:%d: out of TD's\n",
1043 __FUNCTION__, __LINE__);
1045 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
1046 pp_last = &sc->sc_isoc_p_last[0];
1049 if (uhcidebug > 5) {
1050 DPRINTF("isoc TD\n");
1054 usb_pc_cpu_invalidate(td->page_cache);
1055 status = le32toh(td->td_status);
1057 len = UHCI_TD_GET_ACTLEN(status);
1064 usbd_get_page(td->fix_pc, 0, &res);
1066 /* copy data from fixup location to real location */
1068 usb_pc_cpu_invalidate(td->fix_pc);
1070 usbd_copy_in(xfer->frbuffers, offset,
1077 /* remove TD from schedule */
1078 UHCI_REMOVE_TD(td, *pp_last);
1085 xfer->aframes = xfer->nframes;
1089 uhci_non_isoc_done_sub(struct usb_xfer *xfer)
1091 struct usb_page_search res;
1093 uhci_td_t *td_alt_next;
1098 td = xfer->td_transfer_cache;
1099 td_alt_next = td->alt_next;
1101 if (xfer->aframes != xfer->nframes) {
1102 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1106 usb_pc_cpu_invalidate(td->page_cache);
1107 status = le32toh(td->td_status);
1108 token = le32toh(td->td_token);
1111 * Verify the status and add
1112 * up the actual length:
1115 len = UHCI_TD_GET_ACTLEN(status);
1116 if (len > td->len) {
1117 /* should not happen */
1118 DPRINTF("Invalid status length, "
1119 "0x%04x/0x%04x bytes\n", len, td->len);
1120 status |= UHCI_TD_STALLED;
1122 } else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
1126 usbd_get_page(td->fix_pc, 0, &res);
1129 * copy data from fixup location to real
1133 usb_pc_cpu_invalidate(td->fix_pc);
1135 usbd_copy_in(xfer->frbuffers + xfer->aframes,
1136 xfer->frlengths[xfer->aframes], res.buffer, len);
1138 /* update actual length */
1140 xfer->frlengths[xfer->aframes] += len;
1142 /* Check for last transfer */
1143 if (((void *)td) == xfer->td_transfer_last) {
1147 if (status & UHCI_TD_STALLED) {
1148 /* the transfer is finished */
1152 /* Check for short transfer */
1153 if (len != td->len) {
1154 if (xfer->flags_int.short_frames_ok) {
1155 /* follow alt next */
1158 /* the transfer is finished */
1165 if (td->alt_next != td_alt_next) {
1166 /* this USB frame is complete */
1171 /* update transfer cache */
1173 xfer->td_transfer_cache = td;
1175 /* update data toggle */
1177 xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
1180 if (status & UHCI_TD_ERROR) {
1181 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
1182 "status=%s%s%s%s%s%s%s%s%s%s%s\n",
1183 xfer->address, xfer->endpointno, xfer->aframes,
1184 (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
1185 (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
1186 (status & UHCI_TD_NAK) ? "[NAK]" : "",
1187 (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
1188 (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
1189 (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
1190 (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1191 (status & UHCI_TD_IOC) ? "[IOC]" : "",
1192 (status & UHCI_TD_IOS) ? "[IOS]" : "",
1193 (status & UHCI_TD_LS) ? "[LS]" : "",
1194 (status & UHCI_TD_SPD) ? "[SPD]" : "");
1197 return (status & UHCI_TD_STALLED) ?
1198 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION;
1202 uhci_non_isoc_done(struct usb_xfer *xfer)
1204 usb_error_t err = 0;
1206 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1207 xfer, xfer->endpoint);
1210 if (uhcidebug > 10) {
1211 uhci_dump_tds(xfer->td_transfer_first);
1215 /* sync any DMA memory before doing fixups */
1217 usb_bdma_post_sync(xfer);
1221 xfer->td_transfer_cache = xfer->td_transfer_first;
1223 if (xfer->flags_int.control_xfr) {
1224 if (xfer->flags_int.control_hdr) {
1226 err = uhci_non_isoc_done_sub(xfer);
1230 if (xfer->td_transfer_cache == NULL) {
1234 while (xfer->aframes != xfer->nframes) {
1236 err = uhci_non_isoc_done_sub(xfer);
1239 if (xfer->td_transfer_cache == NULL) {
1244 if (xfer->flags_int.control_xfr &&
1245 !xfer->flags_int.control_act) {
1247 err = uhci_non_isoc_done_sub(xfer);
1250 uhci_device_done(xfer, err);
1253 /*------------------------------------------------------------------------*
1254 * uhci_check_transfer_sub
1256 * The main purpose of this function is to update the data-toggle
1257 * in case it is wrong.
1258 *------------------------------------------------------------------------*/
1260 uhci_check_transfer_sub(struct usb_xfer *xfer)
1264 uhci_td_t *td_alt_next;
1269 td = xfer->td_transfer_cache;
1270 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1272 td_token = td->obj_next->td_token;
1274 xfer->td_transfer_cache = td;
1275 td_self = td->td_self;
1276 td_alt_next = td->alt_next;
1278 if (xfer->flags_int.control_xfr)
1279 goto skip; /* don't touch the DT value! */
1281 if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))))
1282 goto skip; /* data toggle has correct value */
1285 * The data toggle is wrong and we need to toggle it !
1289 td->td_token ^= htole32(UHCI_TD_SET_DT(1));
1290 usb_pc_cpu_flush(td->page_cache);
1292 if (td == xfer->td_transfer_last) {
1298 if (td->alt_next != td_alt_next) {
1306 qh->qh_e_next = td_self;
1307 usb_pc_cpu_flush(qh->page_cache);
1309 DPRINTFN(13, "xfer=%p following alt next\n", xfer);
1312 /*------------------------------------------------------------------------*
1313 * uhci_check_transfer
1316 * 0: USB transfer is not finished
1317 * Else: USB transfer is finished
1318 *------------------------------------------------------------------------*/
1320 uhci_check_transfer(struct usb_xfer *xfer)
1326 DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
1328 if (xfer->endpoint->methods == &uhci_device_isoc_methods) {
1329 /* isochronous transfer */
1331 td = xfer->td_transfer_last;
1333 usb_pc_cpu_invalidate(td->page_cache);
1334 status = le32toh(td->td_status);
1336 /* check also if the first is complete */
1338 td = xfer->td_transfer_first;
1340 usb_pc_cpu_invalidate(td->page_cache);
1341 status |= le32toh(td->td_status);
1343 if (!(status & UHCI_TD_ACTIVE)) {
1344 uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1348 /* non-isochronous transfer */
1351 * check whether there is an error somewhere
1352 * in the middle, or whether there was a short
1353 * packet (SPD and not ACTIVE)
1355 td = xfer->td_transfer_cache;
1358 usb_pc_cpu_invalidate(td->page_cache);
1359 status = le32toh(td->td_status);
1360 token = le32toh(td->td_token);
1363 * if there is an active TD the transfer isn't done
1365 if (status & UHCI_TD_ACTIVE) {
1367 xfer->td_transfer_cache = td;
1371 * last transfer descriptor makes the transfer done
1373 if (((void *)td) == xfer->td_transfer_last) {
1377 * any kind of error makes the transfer done
1379 if (status & UHCI_TD_STALLED) {
1383 * check if we reached the last packet
1384 * or if there is a short packet:
1386 if ((td->td_next == htole32(UHCI_PTR_T)) ||
1387 (UHCI_TD_GET_ACTLEN(status) < td->len)) {
1389 if (xfer->flags_int.short_frames_ok) {
1390 /* follow alt next */
1393 xfer->td_transfer_cache = td;
1394 uhci_check_transfer_sub(xfer);
1398 /* transfer is done */
1403 uhci_non_isoc_done(xfer);
1408 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1416 uhci_interrupt_poll(uhci_softc_t *sc)
1418 struct usb_xfer *xfer;
1421 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1423 * check if transfer is transferred
1425 if (uhci_check_transfer(xfer)) {
1426 /* queue has been modified */
1432 /*------------------------------------------------------------------------*
1433 * uhci_interrupt - UHCI interrupt handler
1435 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1436 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1438 *------------------------------------------------------------------------*/
1440 uhci_interrupt(uhci_softc_t *sc)
1444 USB_BUS_LOCK(&sc->sc_bus);
1446 DPRINTFN(16, "real interrupt\n");
1449 if (uhcidebug > 15) {
1453 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1455 /* the interrupt was not for us */
1458 if (status & (UHCI_STS_RD | UHCI_STS_HSE |
1459 UHCI_STS_HCPE | UHCI_STS_HCH)) {
1461 if (status & UHCI_STS_RD) {
1463 printf("%s: resume detect\n",
1467 if (status & UHCI_STS_HSE) {
1468 printf("%s: host system error\n",
1471 if (status & UHCI_STS_HCPE) {
1472 printf("%s: host controller process error\n",
1475 if (status & UHCI_STS_HCH) {
1476 /* no acknowledge needed */
1477 DPRINTF("%s: host controller halted\n",
1480 if (uhcidebug > 0) {
1486 /* get acknowledge bits */
1487 status &= (UHCI_STS_USBINT |
1494 /* nothing to acknowledge */
1497 /* acknowledge interrupts */
1498 UWRITE2(sc, UHCI_STS, status);
1500 /* poll all the USB transfers */
1501 uhci_interrupt_poll(sc);
1504 USB_BUS_UNLOCK(&sc->sc_bus);
1508 * called when a request does not complete
1511 uhci_timeout(void *arg)
1513 struct usb_xfer *xfer = arg;
1515 DPRINTF("xfer=%p\n", xfer);
1517 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1519 /* transfer is transferred */
1520 uhci_device_done(xfer, USB_ERR_TIMEOUT);
1524 uhci_do_poll(struct usb_bus *bus)
1526 struct uhci_softc *sc = UHCI_BUS2SC(bus);
1528 USB_BUS_LOCK(&sc->sc_bus);
1529 uhci_interrupt_poll(sc);
1530 USB_BUS_UNLOCK(&sc->sc_bus);
1534 uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
1538 uhci_td_t *td_alt_next;
1541 uint8_t shortpkt_old;
1545 shortpkt_old = temp->shortpkt;
1546 len_old = temp->len;
1549 /* software is used to detect short incoming transfers */
1551 if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
1552 temp->td_status |= htole32(UHCI_TD_SPD);
1554 temp->td_status &= ~htole32(UHCI_TD_SPD);
1557 temp->ml.buf_offset = 0;
1561 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1562 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
1565 td_next = temp->td_next;
1569 if (temp->len == 0) {
1571 if (temp->shortpkt) {
1574 /* send a Zero Length Packet, ZLP, last */
1577 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
1582 average = temp->average;
1584 if (temp->len < average) {
1586 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1587 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
1588 average = temp->len;
1592 if (td_next == NULL) {
1593 panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
1598 td_next = td->obj_next;
1600 /* check if we are pre-computing */
1604 /* update remaining length */
1606 temp->len -= average;
1610 /* fill out current TD */
1612 td->td_status = temp->td_status;
1613 td->td_token = temp->td_token;
1615 /* update data toggle */
1617 temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
1627 /* update remaining length */
1629 temp->len -= average;
1633 /* fill out buffer pointer and do fixup, if any */
1635 uhci_mem_layout_fixup(&temp->ml, td);
1638 td->alt_next = td_alt_next;
1640 if ((td_next == td_alt_next) && temp->setup_alt_next) {
1641 /* we need to receive these frames one by one ! */
1642 td->td_status |= htole32(UHCI_TD_IOC);
1643 td->td_next = htole32(UHCI_PTR_T);
1646 /* link the current TD with the next one */
1647 td->td_next = td_next->td_self;
1651 usb_pc_cpu_flush(td->page_cache);
1657 /* setup alt next pointer, if any */
1658 if (temp->last_frame) {
1661 /* we use this field internally */
1662 td_alt_next = td_next;
1666 temp->shortpkt = shortpkt_old;
1667 temp->len = len_old;
1671 temp->td_next = td_next;
1675 uhci_setup_standard_chain(struct usb_xfer *xfer)
1677 struct uhci_std_temp temp;
1681 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1682 xfer->address, UE_GET_ADDR(xfer->endpointno),
1683 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1685 temp.average = xfer->max_frame_size;
1686 temp.max_frame_size = xfer->max_frame_size;
1688 /* toggle the DMA set we are using */
1689 xfer->flags_int.curr_dma_set ^= 1;
1691 /* get next DMA set */
1692 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1693 xfer->td_transfer_first = td;
1694 xfer->td_transfer_cache = td;
1698 temp.last_frame = 0;
1699 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1701 uhci_mem_layout_init(&temp.ml, xfer);
1704 htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
1707 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1708 temp.td_status |= htole32(UHCI_TD_LS);
1711 htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) |
1712 UHCI_TD_SET_DEVADDR(xfer->address));
1714 if (xfer->endpoint->toggle_next) {
1716 temp.td_token |= htole32(UHCI_TD_SET_DT(1));
1718 /* check if we should prepend a setup message */
1720 if (xfer->flags_int.control_xfr) {
1722 if (xfer->flags_int.control_hdr) {
1724 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1725 UHCI_TD_SET_ENDPT(0xF));
1726 temp.td_token |= htole32(UHCI_TD_PID_SETUP |
1729 temp.len = xfer->frlengths[0];
1730 temp.ml.buf_pc = xfer->frbuffers + 0;
1731 temp.shortpkt = temp.len ? 1 : 0;
1732 /* check for last frame */
1733 if (xfer->nframes == 1) {
1734 /* no STATUS stage yet, SETUP is last */
1735 if (xfer->flags_int.control_act) {
1736 temp.last_frame = 1;
1737 temp.setup_alt_next = 0;
1740 uhci_setup_standard_chain_sub(&temp);
1747 while (x != xfer->nframes) {
1749 /* DATA0 / DATA1 message */
1751 temp.len = xfer->frlengths[x];
1752 temp.ml.buf_pc = xfer->frbuffers + x;
1756 if (x == xfer->nframes) {
1757 if (xfer->flags_int.control_xfr) {
1758 /* no STATUS stage yet, DATA is last */
1759 if (xfer->flags_int.control_act) {
1760 temp.last_frame = 1;
1761 temp.setup_alt_next = 0;
1764 temp.last_frame = 1;
1765 temp.setup_alt_next = 0;
1769 * Keep previous data toggle,
1770 * device address and endpoint number:
1773 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1774 UHCI_TD_SET_ENDPT(0xF) |
1777 if (temp.len == 0) {
1779 /* make sure that we send an USB packet */
1785 /* regular data transfer */
1787 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1790 /* set endpoint direction */
1793 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1794 htole32(UHCI_TD_PID_IN) :
1795 htole32(UHCI_TD_PID_OUT);
1797 uhci_setup_standard_chain_sub(&temp);
1800 /* check if we should append a status stage */
1802 if (xfer->flags_int.control_xfr &&
1803 !xfer->flags_int.control_act) {
1806 * send a DATA1 message and reverse the current endpoint
1810 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1811 UHCI_TD_SET_ENDPT(0xF) |
1814 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1815 htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
1816 htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
1819 temp.ml.buf_pc = NULL;
1821 temp.last_frame = 1;
1822 temp.setup_alt_next = 0;
1824 uhci_setup_standard_chain_sub(&temp);
1828 /* Ensure that last TD is terminating: */
1829 td->td_next = htole32(UHCI_PTR_T);
1831 /* set interrupt bit */
1833 td->td_status |= htole32(UHCI_TD_IOC);
1835 usb_pc_cpu_flush(td->page_cache);
1837 /* must have at least one frame! */
1839 xfer->td_transfer_last = td;
1842 if (uhcidebug > 8) {
1843 DPRINTF("nexttog=%d; data before transfer:\n",
1844 xfer->endpoint->toggle_next);
1845 uhci_dump_tds(xfer->td_transfer_first);
1848 return (xfer->td_transfer_first);
1851 /* NOTE: "done" can be run two times in a row,
1852 * from close and from interrupt
1856 uhci_device_done(struct usb_xfer *xfer, usb_error_t error)
1858 struct usb_pipe_methods *methods = xfer->endpoint->methods;
1859 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1862 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1864 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1865 xfer, xfer->endpoint, error);
1867 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1869 usb_pc_cpu_invalidate(qh->page_cache);
1871 if (xfer->flags_int.bandwidth_reclaimed) {
1872 xfer->flags_int.bandwidth_reclaimed = 0;
1875 if (methods == &uhci_device_bulk_methods) {
1876 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
1878 if (methods == &uhci_device_ctrl_methods) {
1879 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1880 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
1882 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
1885 if (methods == &uhci_device_intr_methods) {
1886 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
1889 * Only finish isochronous transfers once
1890 * which will update "xfer->frlengths".
1892 if (xfer->td_transfer_first &&
1893 xfer->td_transfer_last) {
1894 if (methods == &uhci_device_isoc_methods) {
1895 uhci_isoc_done(sc, xfer);
1897 xfer->td_transfer_first = NULL;
1898 xfer->td_transfer_last = NULL;
1900 /* dequeue transfer and start next transfer */
1901 usbd_transfer_done(xfer, error);
1904 /*------------------------------------------------------------------------*
1906 *------------------------------------------------------------------------*/
1908 uhci_device_bulk_open(struct usb_xfer *xfer)
1914 uhci_device_bulk_close(struct usb_xfer *xfer)
1916 uhci_device_done(xfer, USB_ERR_CANCELLED);
1920 uhci_device_bulk_enter(struct usb_xfer *xfer)
1926 uhci_device_bulk_start(struct usb_xfer *xfer)
1928 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1933 td = uhci_setup_standard_chain(xfer);
1936 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1939 qh->qh_e_next = td->td_self;
1941 if (xfer->xroot->udev->flags.self_suspended == 0) {
1942 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
1944 xfer->flags_int.bandwidth_reclaimed = 1;
1946 usb_pc_cpu_flush(qh->page_cache);
1949 /* put transfer on interrupt queue */
1950 uhci_transfer_intr_enqueue(xfer);
1953 struct usb_pipe_methods uhci_device_bulk_methods =
1955 .open = uhci_device_bulk_open,
1956 .close = uhci_device_bulk_close,
1957 .enter = uhci_device_bulk_enter,
1958 .start = uhci_device_bulk_start,
1961 /*------------------------------------------------------------------------*
1962 * uhci control support
1963 *------------------------------------------------------------------------*/
1965 uhci_device_ctrl_open(struct usb_xfer *xfer)
1971 uhci_device_ctrl_close(struct usb_xfer *xfer)
1973 uhci_device_done(xfer, USB_ERR_CANCELLED);
1977 uhci_device_ctrl_enter(struct usb_xfer *xfer)
1983 uhci_device_ctrl_start(struct usb_xfer *xfer)
1985 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1990 td = uhci_setup_standard_chain(xfer);
1993 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1996 qh->qh_e_next = td->td_self;
1999 * NOTE: some devices choke on bandwidth- reclamation for control
2002 if (xfer->xroot->udev->flags.self_suspended == 0) {
2003 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
2004 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
2006 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
2009 usb_pc_cpu_flush(qh->page_cache);
2011 /* put transfer on interrupt queue */
2012 uhci_transfer_intr_enqueue(xfer);
2015 struct usb_pipe_methods uhci_device_ctrl_methods =
2017 .open = uhci_device_ctrl_open,
2018 .close = uhci_device_ctrl_close,
2019 .enter = uhci_device_ctrl_enter,
2020 .start = uhci_device_ctrl_start,
2023 /*------------------------------------------------------------------------*
2024 * uhci interrupt support
2025 *------------------------------------------------------------------------*/
2027 uhci_device_intr_open(struct usb_xfer *xfer)
2029 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2035 bit = UHCI_IFRAMELIST_COUNT / 2;
2037 if (xfer->interval >= bit) {
2041 if (sc->sc_intr_stat[x] <
2042 sc->sc_intr_stat[best]) {
2052 sc->sc_intr_stat[best]++;
2053 xfer->qh_pos = best;
2055 DPRINTFN(3, "best=%d interval=%d\n",
2056 best, xfer->interval);
2060 uhci_device_intr_close(struct usb_xfer *xfer)
2062 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2064 sc->sc_intr_stat[xfer->qh_pos]--;
2066 uhci_device_done(xfer, USB_ERR_CANCELLED);
2070 uhci_device_intr_enter(struct usb_xfer *xfer)
2076 uhci_device_intr_start(struct usb_xfer *xfer)
2078 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2083 td = uhci_setup_standard_chain(xfer);
2086 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2089 qh->qh_e_next = td->td_self;
2091 if (xfer->xroot->udev->flags.self_suspended == 0) {
2092 /* enter QHs into the controller data structures */
2093 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
2095 usb_pc_cpu_flush(qh->page_cache);
2098 /* put transfer on interrupt queue */
2099 uhci_transfer_intr_enqueue(xfer);
2102 struct usb_pipe_methods uhci_device_intr_methods =
2104 .open = uhci_device_intr_open,
2105 .close = uhci_device_intr_close,
2106 .enter = uhci_device_intr_enter,
2107 .start = uhci_device_intr_start,
2110 /*------------------------------------------------------------------------*
2111 * uhci isochronous support
2112 *------------------------------------------------------------------------*/
2114 uhci_device_isoc_open(struct usb_xfer *xfer)
2121 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
2122 UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) :
2123 UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0);
2125 td_token = htole32(td_token);
2127 /* initialize all TD's */
2129 for (ds = 0; ds != 2; ds++) {
2131 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2133 /* mark TD as inactive */
2134 td->td_status = htole32(UHCI_TD_IOS);
2135 td->td_token = td_token;
2137 usb_pc_cpu_flush(td->page_cache);
2143 uhci_device_isoc_close(struct usb_xfer *xfer)
2145 uhci_device_done(xfer, USB_ERR_CANCELLED);
2149 uhci_device_isoc_enter(struct usb_xfer *xfer)
2151 struct uhci_mem_layout ml;
2152 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2162 uhci_td_t *td_last = NULL;
2163 uhci_td_t **pp_last;
2165 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2166 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2168 nframes = UREAD2(sc, UHCI_FRNUM);
2170 temp = (nframes - xfer->endpoint->isoc_next) &
2171 (UHCI_VFRAMELIST_COUNT - 1);
2173 if ((xfer->endpoint->is_synced == 0) ||
2174 (temp < xfer->nframes)) {
2176 * If there is data underflow or the pipe queue is empty we
2177 * schedule the transfer a few frames ahead of the current
2178 * frame position. Else two isochronous transfers might
2181 xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
2182 xfer->endpoint->is_synced = 1;
2183 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2186 * compute how many milliseconds the insertion is ahead of the
2187 * current frame position:
2189 temp = (xfer->endpoint->isoc_next - nframes) &
2190 (UHCI_VFRAMELIST_COUNT - 1);
2193 * pre-compute when the isochronous transfer will be finished:
2195 xfer->isoc_time_complete =
2196 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2199 /* get the real number of frames */
2201 nframes = xfer->nframes;
2203 uhci_mem_layout_init(&ml, xfer);
2205 plen = xfer->frlengths;
2207 /* toggle the DMA set we are using */
2208 xfer->flags_int.curr_dma_set ^= 1;
2210 /* get next DMA set */
2211 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2212 xfer->td_transfer_first = td;
2214 pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next];
2216 /* store starting position */
2218 xfer->qh_pos = xfer->endpoint->isoc_next;
2222 panic("%s:%d: out of TD's\n",
2223 __FUNCTION__, __LINE__);
2225 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
2226 pp_last = &sc->sc_isoc_p_last[0];
2228 if (*plen > xfer->max_frame_size) {
2232 printf("%s: frame length(%d) exceeds %d "
2233 "bytes (frame truncated)\n",
2234 __FUNCTION__, *plen,
2235 xfer->max_frame_size);
2238 *plen = xfer->max_frame_size;
2240 /* reuse td_token from last transfer */
2242 td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2243 td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
2249 * Do not call "uhci_mem_layout_fixup()" when the
2257 /* fill out buffer pointer and do fixup, if any */
2259 uhci_mem_layout_fixup(&ml, td);
2265 td->td_status = htole32
2266 (UHCI_TD_ZERO_ACTLEN
2267 (UHCI_TD_SET_ERRCNT(0) |
2272 td->td_status = htole32
2273 (UHCI_TD_ZERO_ACTLEN
2274 (UHCI_TD_SET_ERRCNT(0) |
2279 usb_pc_cpu_flush(td->page_cache);
2282 if (uhcidebug > 5) {
2283 DPRINTF("TD %d\n", nframes);
2287 /* insert TD into schedule */
2288 UHCI_APPEND_TD(td, *pp_last);
2296 xfer->td_transfer_last = td_last;
2298 /* update isoc_next */
2299 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
2300 (UHCI_VFRAMELIST_COUNT - 1);
2304 uhci_device_isoc_start(struct usb_xfer *xfer)
2306 /* put transfer on interrupt queue */
2307 uhci_transfer_intr_enqueue(xfer);
2310 struct usb_pipe_methods uhci_device_isoc_methods =
2312 .open = uhci_device_isoc_open,
2313 .close = uhci_device_isoc_close,
2314 .enter = uhci_device_isoc_enter,
2315 .start = uhci_device_isoc_start,
2318 /*------------------------------------------------------------------------*
2319 * uhci root control support
2320 *------------------------------------------------------------------------*
2321 * Simulate a hardware hub by handling all the necessary requests.
2322 *------------------------------------------------------------------------*/
2325 struct usb_device_descriptor uhci_devd =
2327 sizeof(struct usb_device_descriptor),
2328 UDESC_DEVICE, /* type */
2329 {0x00, 0x01}, /* USB version */
2330 UDCLASS_HUB, /* class */
2331 UDSUBCLASS_HUB, /* subclass */
2332 UDPROTO_FSHUB, /* protocol */
2333 64, /* max packet */
2334 {0}, {0}, {0x00, 0x01}, /* device id */
2335 1, 2, 0, /* string indicies */
2336 1 /* # of configurations */
2339 static const struct uhci_config_desc uhci_confd = {
2341 .bLength = sizeof(struct usb_config_descriptor),
2342 .bDescriptorType = UDESC_CONFIG,
2343 .wTotalLength[0] = sizeof(uhci_confd),
2345 .bConfigurationValue = 1,
2346 .iConfiguration = 0,
2347 .bmAttributes = UC_SELF_POWERED,
2348 .bMaxPower = 0 /* max power */
2351 .bLength = sizeof(struct usb_interface_descriptor),
2352 .bDescriptorType = UDESC_INTERFACE,
2354 .bInterfaceClass = UICLASS_HUB,
2355 .bInterfaceSubClass = UISUBCLASS_HUB,
2356 .bInterfaceProtocol = UIPROTO_FSHUB,
2359 .bLength = sizeof(struct usb_endpoint_descriptor),
2360 .bDescriptorType = UDESC_ENDPOINT,
2361 .bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
2362 .bmAttributes = UE_INTERRUPT,
2363 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
2369 struct usb_hub_descriptor_min uhci_hubd_piix =
2371 sizeof(uhci_hubd_piix),
2374 {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
2375 50, /* power on to power good */
2377 {0x00}, /* both ports are removable */
2381 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2382 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2383 * should not be used by the USB subsystem. As we cannot issue a
2384 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2385 * will be enabled as part of the reset.
2387 * On the VT83C572, the port cannot be successfully enabled until the
2388 * outstanding "port enable change" and "connection status change"
2389 * events have been reset.
2392 uhci_portreset(uhci_softc_t *sc, uint16_t index)
2399 port = UHCI_PORTSC1;
2400 else if (index == 2)
2401 port = UHCI_PORTSC2;
2403 return (USB_ERR_IOERROR);
2406 * Before we do anything, turn on SOF messages on the USB
2407 * BUS. Some USB devices do not cope without them!
2411 x = URWMASK(UREAD2(sc, port));
2412 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2414 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2415 USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
2417 DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
2418 index, UREAD2(sc, port));
2420 x = URWMASK(UREAD2(sc, port));
2421 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2424 mtx_unlock(&sc->sc_bus.bus_mtx);
2427 * This delay needs to be exactly 100us, else some USB devices
2432 mtx_lock(&sc->sc_bus.bus_mtx);
2434 DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
2435 index, UREAD2(sc, port));
2437 x = URWMASK(UREAD2(sc, port));
2438 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2440 for (lim = 0; lim < 12; lim++) {
2442 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2443 USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
2445 x = UREAD2(sc, port);
2447 DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
2450 if (!(x & UHCI_PORTSC_CCS)) {
2452 * No device is connected (or was disconnected
2453 * during reset). Consider the port reset.
2454 * The delay must be long enough to ensure on
2455 * the initial iteration that the device
2456 * connection will have been registered. 50ms
2457 * appears to be sufficient, but 20ms is not.
2459 DPRINTFN(4, "uhci port %d loop %u, device detached\n",
2463 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
2465 * Port enabled changed and/or connection
2466 * status changed were set. Reset either or
2467 * both raised flags (by writing a 1 to that
2468 * bit), and wait again for state to settle.
2470 UWRITE2(sc, port, URWMASK(x) |
2471 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
2474 if (x & UHCI_PORTSC_PE) {
2475 /* port is enabled */
2478 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
2481 DPRINTFN(2, "uhci port %d reset timed out\n", index);
2482 return (USB_ERR_TIMEOUT);
2485 DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
2486 index, UREAD2(sc, port));
2489 return (USB_ERR_NORMAL_COMPLETION);
2493 uhci_roothub_exec(struct usb_device *udev,
2494 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2496 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
2498 const char *str_ptr;
2508 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2511 ptr = (const void *)&sc->sc_hub_desc.temp;
2515 value = UGETW(req->wValue);
2516 index = UGETW(req->wIndex);
2518 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2519 "wValue=0x%04x wIndex=0x%04x\n",
2520 req->bmRequestType, req->bRequest,
2521 UGETW(req->wLength), value, index);
2523 #define C(x,y) ((x) | ((y) << 8))
2524 switch (C(req->bRequest, req->bmRequestType)) {
2525 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2526 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2527 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2529 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2530 * for the integrated root hub.
2533 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2535 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2537 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2538 switch (value >> 8) {
2540 if ((value & 0xff) != 0) {
2541 err = USB_ERR_IOERROR;
2544 len = sizeof(uhci_devd);
2545 ptr = (const void *)&uhci_devd;
2549 if ((value & 0xff) != 0) {
2550 err = USB_ERR_IOERROR;
2553 len = sizeof(uhci_confd);
2554 ptr = (const void *)&uhci_confd;
2558 switch (value & 0xff) {
2559 case 0: /* Language table */
2563 case 1: /* Vendor */
2564 str_ptr = sc->sc_vendor;
2567 case 2: /* Product */
2568 str_ptr = "UHCI root HUB";
2576 len = usb_make_str_desc
2577 (sc->sc_hub_desc.temp,
2578 sizeof(sc->sc_hub_desc.temp),
2583 err = USB_ERR_IOERROR;
2587 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2589 sc->sc_hub_desc.temp[0] = 0;
2591 case C(UR_GET_STATUS, UT_READ_DEVICE):
2593 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
2595 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2596 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2598 USETW(sc->sc_hub_desc.stat.wStatus, 0);
2600 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2601 if (value >= UHCI_MAX_DEVICES) {
2602 err = USB_ERR_IOERROR;
2605 sc->sc_addr = value;
2607 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2608 if ((value != 0) && (value != 1)) {
2609 err = USB_ERR_IOERROR;
2612 sc->sc_conf = value;
2614 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2616 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2617 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2618 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2619 err = USB_ERR_IOERROR;
2621 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2623 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2626 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2628 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2629 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
2630 "port=%d feature=%d\n",
2633 port = UHCI_PORTSC1;
2634 else if (index == 2)
2635 port = UHCI_PORTSC2;
2637 err = USB_ERR_IOERROR;
2641 case UHF_PORT_ENABLE:
2642 x = URWMASK(UREAD2(sc, port));
2643 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2645 case UHF_PORT_SUSPEND:
2646 x = URWMASK(UREAD2(sc, port));
2647 UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
2649 case UHF_PORT_RESET:
2650 x = URWMASK(UREAD2(sc, port));
2651 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2653 case UHF_C_PORT_CONNECTION:
2654 x = URWMASK(UREAD2(sc, port));
2655 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2657 case UHF_C_PORT_ENABLE:
2658 x = URWMASK(UREAD2(sc, port));
2659 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2661 case UHF_C_PORT_OVER_CURRENT:
2662 x = URWMASK(UREAD2(sc, port));
2663 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2665 case UHF_C_PORT_RESET:
2667 err = USB_ERR_NORMAL_COMPLETION;
2669 case UHF_C_PORT_SUSPEND:
2670 sc->sc_isresumed &= ~(1 << index);
2672 case UHF_PORT_CONNECTION:
2673 case UHF_PORT_OVER_CURRENT:
2674 case UHF_PORT_POWER:
2675 case UHF_PORT_LOW_SPEED:
2677 err = USB_ERR_IOERROR;
2681 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2683 port = UHCI_PORTSC1;
2684 else if (index == 2)
2685 port = UHCI_PORTSC2;
2687 err = USB_ERR_IOERROR;
2691 sc->sc_hub_desc.temp[0] =
2692 ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2693 UHCI_PORTSC_LS_SHIFT);
2695 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2696 if ((value & 0xff) != 0) {
2697 err = USB_ERR_IOERROR;
2700 len = sizeof(uhci_hubd_piix);
2701 ptr = (const void *)&uhci_hubd_piix;
2703 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2705 bzero(sc->sc_hub_desc.temp, 16);
2707 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2709 port = UHCI_PORTSC1;
2710 else if (index == 2)
2711 port = UHCI_PORTSC2;
2713 err = USB_ERR_IOERROR;
2716 x = UREAD2(sc, port);
2717 status = change = 0;
2718 if (x & UHCI_PORTSC_CCS)
2719 status |= UPS_CURRENT_CONNECT_STATUS;
2720 if (x & UHCI_PORTSC_CSC)
2721 change |= UPS_C_CONNECT_STATUS;
2722 if (x & UHCI_PORTSC_PE)
2723 status |= UPS_PORT_ENABLED;
2724 if (x & UHCI_PORTSC_POEDC)
2725 change |= UPS_C_PORT_ENABLED;
2726 if (x & UHCI_PORTSC_OCI)
2727 status |= UPS_OVERCURRENT_INDICATOR;
2728 if (x & UHCI_PORTSC_OCIC)
2729 change |= UPS_C_OVERCURRENT_INDICATOR;
2730 if (x & UHCI_PORTSC_LSDA)
2731 status |= UPS_LOW_SPEED;
2732 if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
2733 /* need to do a write back */
2734 UWRITE2(sc, port, URWMASK(x));
2736 /* wait 20ms for resume sequence to complete */
2737 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
2739 /* clear suspend and resume detect */
2740 UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
2743 /* wait a little bit */
2744 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
2746 sc->sc_isresumed |= (1 << index);
2748 } else if (x & UHCI_PORTSC_SUSP) {
2749 status |= UPS_SUSPEND;
2751 status |= UPS_PORT_POWER;
2752 if (sc->sc_isresumed & (1 << index))
2753 change |= UPS_C_SUSPEND;
2755 change |= UPS_C_PORT_RESET;
2756 USETW(sc->sc_hub_desc.ps.wPortStatus, status);
2757 USETW(sc->sc_hub_desc.ps.wPortChange, change);
2758 len = sizeof(sc->sc_hub_desc.ps);
2760 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2761 err = USB_ERR_IOERROR;
2763 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2765 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2767 port = UHCI_PORTSC1;
2768 else if (index == 2)
2769 port = UHCI_PORTSC2;
2771 err = USB_ERR_IOERROR;
2775 case UHF_PORT_ENABLE:
2776 x = URWMASK(UREAD2(sc, port));
2777 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2779 case UHF_PORT_SUSPEND:
2780 x = URWMASK(UREAD2(sc, port));
2781 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2783 case UHF_PORT_RESET:
2784 err = uhci_portreset(sc, index);
2786 case UHF_PORT_POWER:
2787 /* pretend we turned on power */
2788 err = USB_ERR_NORMAL_COMPLETION;
2790 case UHF_C_PORT_CONNECTION:
2791 case UHF_C_PORT_ENABLE:
2792 case UHF_C_PORT_OVER_CURRENT:
2793 case UHF_PORT_CONNECTION:
2794 case UHF_PORT_OVER_CURRENT:
2795 case UHF_PORT_LOW_SPEED:
2796 case UHF_C_PORT_SUSPEND:
2797 case UHF_C_PORT_RESET:
2799 err = USB_ERR_IOERROR;
2804 err = USB_ERR_IOERROR;
2814 * This routine is executed periodically and simulates interrupts from
2815 * the root controller interrupt pipe for port status change:
2818 uhci_root_intr(uhci_softc_t *sc)
2822 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2824 sc->sc_hub_idata[0] = 0;
2826 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
2827 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2828 sc->sc_hub_idata[0] |= 1 << 1;
2830 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
2831 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2832 sc->sc_hub_idata[0] |= 1 << 2;
2836 usb_callout_reset(&sc->sc_root_intr, hz,
2837 (void *)&uhci_root_intr, sc);
2839 if (sc->sc_hub_idata[0] != 0) {
2840 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2841 sizeof(sc->sc_hub_idata));
2846 uhci_xfer_setup(struct usb_setup_params *parm)
2848 struct usb_page_search page_info;
2849 struct usb_page_cache *pc;
2851 struct usb_xfer *xfer;
2859 sc = UHCI_BUS2SC(parm->udev->bus);
2860 xfer = parm->curr_xfer;
2862 parm->hc_max_packet_size = 0x500;
2863 parm->hc_max_packet_count = 1;
2864 parm->hc_max_frame_size = 0x500;
2867 * compute ntd and nqh
2869 if (parm->methods == &uhci_device_ctrl_methods) {
2870 xfer->flags_int.bdma_enable = 1;
2871 xfer->flags_int.bdma_no_post_sync = 1;
2873 usbd_transfer_setup_sub(parm);
2875 /* see EHCI HC driver for proof of "ntd" formula */
2878 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
2879 + (xfer->max_data_length / xfer->max_frame_size));
2881 } else if (parm->methods == &uhci_device_bulk_methods) {
2882 xfer->flags_int.bdma_enable = 1;
2883 xfer->flags_int.bdma_no_post_sync = 1;
2885 usbd_transfer_setup_sub(parm);
2888 ntd = ((2 * xfer->nframes)
2889 + (xfer->max_data_length / xfer->max_frame_size));
2891 } else if (parm->methods == &uhci_device_intr_methods) {
2892 xfer->flags_int.bdma_enable = 1;
2893 xfer->flags_int.bdma_no_post_sync = 1;
2895 usbd_transfer_setup_sub(parm);
2898 ntd = ((2 * xfer->nframes)
2899 + (xfer->max_data_length / xfer->max_frame_size));
2901 } else if (parm->methods == &uhci_device_isoc_methods) {
2902 xfer->flags_int.bdma_enable = 1;
2903 xfer->flags_int.bdma_no_post_sync = 1;
2905 usbd_transfer_setup_sub(parm);
2908 ntd = xfer->nframes;
2912 usbd_transfer_setup_sub(parm);
2922 * NOTE: the UHCI controller requires that
2923 * every packet must be contiguous on
2924 * the same USB memory page !
2926 nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
2929 * Compute a suitable power of two alignment
2930 * for our "max_frame_size" fixup buffer(s):
2932 align = xfer->max_frame_size;
2939 /* check for power of two */
2940 if (!(xfer->max_frame_size &
2941 (xfer->max_frame_size - 1))) {
2945 * We don't allow alignments of
2946 * less than 8 bytes:
2948 * NOTE: Allocating using an aligment
2949 * of 1 byte has special meaning!
2956 if (usbd_transfer_setup_sub_malloc(
2957 parm, &pc, xfer->max_frame_size,
2959 parm->err = USB_ERR_NOMEM;
2962 xfer->buf_fixup = pc;
2971 if (usbd_transfer_setup_sub_malloc(
2972 parm, &pc, sizeof(uhci_td_t),
2973 UHCI_TD_ALIGN, ntd)) {
2974 parm->err = USB_ERR_NOMEM;
2978 for (n = 0; n != ntd; n++) {
2981 usbd_get_page(pc + n, 0, &page_info);
2983 td = page_info.buffer;
2986 if ((parm->methods == &uhci_device_bulk_methods) ||
2987 (parm->methods == &uhci_device_ctrl_methods) ||
2988 (parm->methods == &uhci_device_intr_methods)) {
2989 /* set depth first bit */
2990 td->td_self = htole32(page_info.physaddr |
2991 UHCI_PTR_TD | UHCI_PTR_VF);
2993 td->td_self = htole32(page_info.physaddr |
2997 td->obj_next = last_obj;
2998 td->page_cache = pc + n;
3002 usb_pc_cpu_flush(pc + n);
3005 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3009 if (usbd_transfer_setup_sub_malloc(
3010 parm, &pc, sizeof(uhci_qh_t),
3011 UHCI_QH_ALIGN, nqh)) {
3012 parm->err = USB_ERR_NOMEM;
3016 for (n = 0; n != nqh; n++) {
3019 usbd_get_page(pc + n, 0, &page_info);
3021 qh = page_info.buffer;
3024 qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
3025 qh->obj_next = last_obj;
3026 qh->page_cache = pc + n;
3030 usb_pc_cpu_flush(pc + n);
3033 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3035 if (!xfer->flags_int.curr_dma_set) {
3036 xfer->flags_int.curr_dma_set = 1;
3042 uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3043 struct usb_endpoint *ep)
3045 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
3047 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3049 edesc->bEndpointAddress, udev->flags.usb_mode,
3052 if (udev->flags.usb_mode != USB_MODE_HOST) {
3056 if (udev->device_index != sc->sc_addr) {
3057 switch (edesc->bmAttributes & UE_XFERTYPE) {
3059 ep->methods = &uhci_device_ctrl_methods;
3062 ep->methods = &uhci_device_intr_methods;
3064 case UE_ISOCHRONOUS:
3065 if (udev->speed == USB_SPEED_FULL) {
3066 ep->methods = &uhci_device_isoc_methods;
3070 ep->methods = &uhci_device_bulk_methods;
3080 uhci_xfer_unsetup(struct usb_xfer *xfer)
3086 uhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3089 * Wait until hardware has finished any possible use of the
3090 * transfer descriptor(s) and QH
3092 *pus = (1125); /* microseconds */
3096 uhci_device_resume(struct usb_device *udev)
3098 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3099 struct usb_xfer *xfer;
3100 struct usb_pipe_methods *methods;
3105 USB_BUS_LOCK(udev->bus);
3107 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3109 if (xfer->xroot->udev == udev) {
3111 methods = xfer->endpoint->methods;
3112 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3114 if (methods == &uhci_device_bulk_methods) {
3115 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
3117 xfer->flags_int.bandwidth_reclaimed = 1;
3119 if (methods == &uhci_device_ctrl_methods) {
3120 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3121 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
3123 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
3126 if (methods == &uhci_device_intr_methods) {
3127 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3132 USB_BUS_UNLOCK(udev->bus);
3138 uhci_device_suspend(struct usb_device *udev)
3140 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3141 struct usb_xfer *xfer;
3142 struct usb_pipe_methods *methods;
3147 USB_BUS_LOCK(udev->bus);
3149 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3151 if (xfer->xroot->udev == udev) {
3153 methods = xfer->endpoint->methods;
3154 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3156 if (xfer->flags_int.bandwidth_reclaimed) {
3157 xfer->flags_int.bandwidth_reclaimed = 0;
3160 if (methods == &uhci_device_bulk_methods) {
3161 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
3163 if (methods == &uhci_device_ctrl_methods) {
3164 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3165 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
3167 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
3170 if (methods == &uhci_device_intr_methods) {
3171 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3176 USB_BUS_UNLOCK(udev->bus);
3182 uhci_set_hw_power(struct usb_bus *bus)
3184 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3191 flags = bus->hw_power_state;
3194 * WARNING: Some FULL speed USB devices require periodic SOF
3195 * messages! If any USB devices are connected through the
3196 * UHCI, power save will be disabled!
3198 if (flags & (USB_HW_POWER_CONTROL |
3199 USB_HW_POWER_NON_ROOT_HUB |
3201 USB_HW_POWER_INTERRUPT |
3202 USB_HW_POWER_ISOC)) {
3203 DPRINTF("Some USB transfer is "
3204 "active on unit %u.\n",
3205 device_get_unit(sc->sc_bus.bdev));
3208 DPRINTF("Power save on unit %u.\n",
3209 device_get_unit(sc->sc_bus.bdev));
3210 UHCICMD(sc, UHCI_CMD_MAXP);
3213 USB_BUS_UNLOCK(bus);
3219 struct usb_bus_methods uhci_bus_methods =
3221 .endpoint_init = uhci_ep_init,
3222 .xfer_setup = uhci_xfer_setup,
3223 .xfer_unsetup = uhci_xfer_unsetup,
3224 .get_dma_delay = uhci_get_dma_delay,
3225 .device_resume = uhci_device_resume,
3226 .device_suspend = uhci_device_suspend,
3227 .set_hw_power = uhci_set_hw_power,
3228 .roothub_exec = uhci_roothub_exec,
3229 .xfer_poll = uhci_do_poll,