2 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * USB Universal Host Controller driver.
33 * Handles e.g. PIIX3 and PIIX4.
35 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
36 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
37 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
38 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
41 #include <sys/stdint.h>
42 #include <sys/stddef.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/linker_set.h>
50 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
64 #define USB_DEBUG_VAR uhcidebug
66 #include <dev/usb/usb_core.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_process.h>
70 #include <dev/usb/usb_transfer.h>
71 #include <dev/usb/usb_device.h>
72 #include <dev/usb/usb_hub.h>
73 #include <dev/usb/usb_util.h>
75 #include <dev/usb/usb_controller.h>
76 #include <dev/usb/usb_bus.h>
77 #include <dev/usb/controller/uhci.h>
78 #include <dev/usb/controller/uhcireg.h>
81 #define UHCI_BUS2SC(bus) \
82 ((uhci_softc_t *)(((uint8_t *)(bus)) - \
83 ((uint8_t *)&(((uhci_softc_t *)0)->sc_bus))))
86 static int uhcidebug = 0;
87 static int uhcinoloop = 0;
89 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
90 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
91 &uhcidebug, 0, "uhci debug level");
92 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
93 &uhcinoloop, 0, "uhci noloop");
94 static void uhci_dumpregs(uhci_softc_t *sc);
95 static void uhci_dump_tds(uhci_td_t *td);
99 #define UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
100 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
101 #define UWRITE1(sc, r, x) \
102 do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
103 } while (/*CONSTCOND*/0)
104 #define UWRITE2(sc, r, x) \
105 do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
106 } while (/*CONSTCOND*/0)
107 #define UWRITE4(sc, r, x) \
108 do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
109 } while (/*CONSTCOND*/0)
110 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
111 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
112 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
114 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
115 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
117 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
119 #define UHCI_INTR_ENDPT 1
121 struct uhci_mem_layout {
123 struct usb_page_search buf_res;
124 struct usb_page_search fix_res;
126 struct usb_page_cache *buf_pc;
127 struct usb_page_cache *fix_pc;
131 uint16_t max_frame_size;
134 struct uhci_std_temp {
136 struct uhci_mem_layout ml;
143 uint16_t max_frame_size;
145 uint8_t setup_alt_next;
149 extern struct usb_bus_methods uhci_bus_methods;
150 extern struct usb_pipe_methods uhci_device_bulk_methods;
151 extern struct usb_pipe_methods uhci_device_ctrl_methods;
152 extern struct usb_pipe_methods uhci_device_intr_methods;
153 extern struct usb_pipe_methods uhci_device_isoc_methods;
155 static uint8_t uhci_restart(uhci_softc_t *sc);
156 static void uhci_do_poll(struct usb_bus *);
157 static void uhci_device_done(struct usb_xfer *, usb_error_t);
158 static void uhci_transfer_intr_enqueue(struct usb_xfer *);
159 static void uhci_timeout(void *);
160 static uint8_t uhci_check_transfer(struct usb_xfer *);
161 static void uhci_root_intr(uhci_softc_t *sc);
164 uhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
166 struct uhci_softc *sc = UHCI_BUS2SC(bus);
169 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
170 sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
172 cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
173 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
175 cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
176 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
178 cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
179 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
181 cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
182 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
184 cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
185 sizeof(uhci_td_t), UHCI_TD_ALIGN);
187 for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
188 cb(bus, sc->sc_hw.isoc_start_pc + i,
189 sc->sc_hw.isoc_start_pg + i,
190 sizeof(uhci_td_t), UHCI_TD_ALIGN);
193 for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
194 cb(bus, sc->sc_hw.intr_start_pc + i,
195 sc->sc_hw.intr_start_pg + i,
196 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
201 uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb_xfer *xfer)
203 ml->buf_pc = xfer->frbuffers + 0;
204 ml->fix_pc = xfer->buf_fixup;
208 ml->max_frame_size = xfer->max_frame_size;
212 uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
214 usbd_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
216 if (ml->buf_res.length < td->len) {
218 /* need to do a fixup */
220 usbd_get_page(ml->fix_pc, 0, &ml->fix_res);
222 td->td_buffer = htole32(ml->fix_res.physaddr);
225 * The UHCI driver cannot handle
226 * page crossings, so a fixup is
239 if ((td->td_token & htole32(UHCI_TD_PID)) ==
240 htole32(UHCI_TD_PID_IN)) {
241 td->fix_pc = ml->fix_pc;
242 usb_pc_cpu_invalidate(ml->fix_pc);
247 /* copy data to fixup location */
249 usbd_copy_out(ml->buf_pc, ml->buf_offset,
250 ml->fix_res.buffer, td->len);
252 usb_pc_cpu_flush(ml->fix_pc);
255 /* prepare next fixup */
261 td->td_buffer = htole32(ml->buf_res.physaddr);
265 /* prepare next data location */
267 ml->buf_offset += td->len;
276 uhci_restart(uhci_softc_t *sc)
278 struct usb_page_search buf_res;
280 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
282 if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) {
283 DPRINTFN(2, "Already started\n");
287 DPRINTFN(2, "Restarting\n");
289 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
291 /* Reload fresh base address */
292 UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
295 * Assume 64 byte packets at frame end and start HC controller:
297 UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
299 /* wait 10 milliseconds */
301 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
303 /* check that controller has started */
305 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
306 DPRINTFN(2, "Failed\n");
313 uhci_reset(uhci_softc_t *sc)
317 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
319 DPRINTF("resetting the HC\n");
321 /* disable interrupts */
323 UWRITE2(sc, UHCI_INTR, 0);
327 UHCICMD(sc, UHCI_CMD_GRESET);
331 usb_pause_mtx(&sc->sc_bus.bus_mtx,
332 USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
334 /* terminate all transfers */
336 UHCICMD(sc, UHCI_CMD_HCRESET);
338 /* the reset bit goes low when the controller is done */
340 n = UHCI_RESET_TIMEOUT;
342 /* wait one millisecond */
344 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
346 if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
351 device_printf(sc->sc_bus.bdev,
352 "controller did not reset\n");
358 /* wait one millisecond */
360 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
362 /* check if HC is stopped */
363 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
368 device_printf(sc->sc_bus.bdev,
369 "controller did not stop\n");
373 /* reload the configuration */
374 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
375 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
377 USB_BUS_UNLOCK(&sc->sc_bus);
379 /* 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, MA_OWNED);
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)
456 usb_callout_init_mtx(&sc->sc_root_intr, &sc->sc_bus.bus_mtx, 0);
463 sc->sc_saved_sof = 0x40; /* default value */
464 sc->sc_saved_frnum = 0; /* default frame number */
469 sc->sc_ls_ctl_p_last =
470 uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc);
472 sc->sc_fs_ctl_p_last =
473 uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc);
476 uhci_init_qh(&sc->sc_hw.bulk_start_pc);
478 sc->sc_reclaim_qh_p =
479 sc->sc_fs_ctl_p_last;
481 /* setup reclaim looping point */
482 sc->sc_reclaim_qh_p =
487 uhci_init_qh(&sc->sc_hw.last_qh_pc);
490 uhci_init_td(&sc->sc_hw.last_td_pc);
492 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
493 sc->sc_isoc_p_last[x] =
494 uhci_init_td(sc->sc_hw.isoc_start_pc + x);
497 for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) {
498 sc->sc_intr_p_last[x] =
499 uhci_init_qh(sc->sc_hw.intr_start_pc + x);
503 * the QHs are arranged to give poll intervals that are
504 * powers of 2 times 1ms
506 bit = UHCI_IFRAMELIST_COUNT / 2;
513 y = (x ^ bit) | (bit / 2);
516 * the next QH has half the poll interval
518 qh_x = sc->sc_intr_p_last[x];
519 qh_y = sc->sc_intr_p_last[y];
522 qh_x->qh_h_next = qh_y->qh_self;
524 qh_x->qh_e_next = htole32(UHCI_PTR_T);
534 qh_ls = sc->sc_ls_ctl_p_last;
535 qh_intr = sc->sc_intr_p_last[0];
537 /* start QH for interrupt traffic */
538 qh_intr->h_next = qh_ls;
539 qh_intr->qh_h_next = qh_ls->qh_self;
541 qh_intr->qh_e_next = htole32(UHCI_PTR_T);
543 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
548 td_x = sc->sc_isoc_p_last[x];
549 qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)];
551 /* start TD for isochronous traffic */
553 td_x->td_next = qh_intr->qh_self;
554 td_x->td_status = htole32(UHCI_TD_IOS);
555 td_x->td_token = htole32(0);
556 td_x->td_buffer = htole32(0);
563 qh_ls = sc->sc_ls_ctl_p_last;
564 qh_fs = sc->sc_fs_ctl_p_last;
566 /* start QH where low speed control traffic will be queued */
567 qh_ls->h_next = qh_fs;
568 qh_ls->qh_h_next = qh_fs->qh_self;
570 qh_ls->qh_e_next = htole32(UHCI_PTR_T);
578 qh_ctl = sc->sc_fs_ctl_p_last;
579 qh_blk = sc->sc_bulk_p_last;
581 /* start QH where full speed control traffic will be queued */
582 qh_ctl->h_next = qh_blk;
583 qh_ctl->qh_h_next = qh_blk->qh_self;
585 qh_ctl->qh_e_next = htole32(UHCI_PTR_T);
587 qh_lst = sc->sc_last_qh_p;
589 /* start QH where bulk traffic will be queued */
590 qh_blk->h_next = qh_lst;
591 qh_blk->qh_h_next = qh_lst->qh_self;
593 qh_blk->qh_e_next = htole32(UHCI_PTR_T);
595 td_lst = sc->sc_last_td_p;
597 /* end QH which is used for looping the QHs */
599 qh_lst->qh_h_next = htole32(UHCI_PTR_T); /* end of QH chain */
600 qh_lst->e_next = td_lst;
601 qh_lst->qh_e_next = td_lst->td_self;
604 * end TD which hangs from the last QH, to avoid a bug in the PIIX
605 * that makes it run berserk otherwise
608 td_lst->td_next = htole32(UHCI_PTR_T);
609 td_lst->td_status = htole32(0); /* inactive */
610 td_lst->td_token = htole32(0);
611 td_lst->td_buffer = htole32(0);
614 struct usb_page_search buf_res;
617 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
619 pframes = buf_res.buffer;
623 * Setup UHCI framelist
627 * pframes -> full speed isochronous -> interrupt QH's -> low
628 * speed control -> full speed control -> bulk transfers
632 for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
634 sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
637 /* flush all cache into memory */
639 usb_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
641 /* set up the bus struct */
642 sc->sc_bus.methods = &uhci_bus_methods;
644 USB_BUS_LOCK(&sc->sc_bus);
645 /* reset the controller */
648 /* start the controller */
650 USB_BUS_UNLOCK(&sc->sc_bus);
652 /* catch lost interrupts */
653 uhci_do_poll(&sc->sc_bus);
658 /* NOTE: suspend/resume is called from
659 * interrupt context and cannot sleep!
663 uhci_suspend(uhci_softc_t *sc)
665 USB_BUS_LOCK(&sc->sc_bus);
672 /* save some state if BIOS doesn't */
674 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
675 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
677 /* stop the controller */
681 /* enter global suspend */
683 UHCICMD(sc, UHCI_CMD_EGSM);
685 usb_pause_mtx(&sc->sc_bus.bus_mtx,
686 USB_MS_TO_TICKS(USB_RESUME_WAIT));
688 USB_BUS_UNLOCK(&sc->sc_bus);
692 uhci_resume(uhci_softc_t *sc)
694 USB_BUS_LOCK(&sc->sc_bus);
696 /* reset the controller */
700 /* force global resume */
702 UHCICMD(sc, UHCI_CMD_FGR);
704 usb_pause_mtx(&sc->sc_bus.bus_mtx,
705 USB_MS_TO_TICKS(USB_RESUME_DELAY));
707 /* and start traffic again */
717 USB_BUS_UNLOCK(&sc->sc_bus);
719 /* catch lost interrupts */
720 uhci_do_poll(&sc->sc_bus);
725 uhci_dumpregs(uhci_softc_t *sc)
727 DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
728 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
729 device_get_nameunit(sc->sc_bus.bdev),
730 UREAD2(sc, UHCI_CMD),
731 UREAD2(sc, UHCI_STS),
732 UREAD2(sc, UHCI_INTR),
733 UREAD2(sc, UHCI_FRNUM),
734 UREAD4(sc, UHCI_FLBASEADDR),
735 UREAD1(sc, UHCI_SOF),
736 UREAD2(sc, UHCI_PORTSC1),
737 UREAD2(sc, UHCI_PORTSC2));
741 uhci_dump_td(uhci_td_t *p)
748 usb_pc_cpu_invalidate(p->page_cache);
750 td_next = le32toh(p->td_next);
751 td_status = le32toh(p->td_status);
752 td_token = le32toh(p->td_token);
755 * Check whether the link pointer in this TD marks the link pointer
758 temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
760 printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
761 "token=0x%08x buffer=0x%08x\n",
767 le32toh(p->td_buffer));
769 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,"
770 "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
772 (td_next & 1) ? "-T" : "",
773 (td_next & 2) ? "-Q" : "",
774 (td_next & 4) ? "-VF" : "",
775 (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
776 (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
777 (td_status & UHCI_TD_NAK) ? "-NAK" : "",
778 (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
779 (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
780 (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
781 (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
782 (td_status & UHCI_TD_IOC) ? "-IOC" : "",
783 (td_status & UHCI_TD_IOS) ? "-IOS" : "",
784 (td_status & UHCI_TD_LS) ? "-LS" : "",
785 (td_status & UHCI_TD_SPD) ? "-SPD" : "",
786 UHCI_TD_GET_ERRCNT(td_status),
787 UHCI_TD_GET_ACTLEN(td_status),
788 UHCI_TD_GET_PID(td_token),
789 UHCI_TD_GET_DEVADDR(td_token),
790 UHCI_TD_GET_ENDPT(td_token),
791 UHCI_TD_GET_DT(td_token),
792 UHCI_TD_GET_MAXLEN(td_token));
798 uhci_dump_qh(uhci_qh_t *sqh)
804 usb_pc_cpu_invalidate(sqh->page_cache);
806 qh_h_next = le32toh(sqh->qh_h_next);
807 qh_e_next = le32toh(sqh->qh_e_next);
809 DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
810 le32toh(sqh->qh_self), qh_h_next, qh_e_next);
812 temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
813 (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
819 uhci_dump_all(uhci_softc_t *sc)
822 uhci_dump_qh(sc->sc_ls_ctl_p_last);
823 uhci_dump_qh(sc->sc_fs_ctl_p_last);
824 uhci_dump_qh(sc->sc_bulk_p_last);
825 uhci_dump_qh(sc->sc_last_qh_p);
829 uhci_dump_qhs(uhci_qh_t *sqh)
833 temp = uhci_dump_qh(sqh);
836 * uhci_dump_qhs displays all the QHs and TDs from the given QH
837 * onwards Traverses sideways first, then down.
839 * QH1 QH2 No QH TD2.1 TD2.2 TD1.1 etc.
841 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
845 uhci_dump_qhs(sqh->h_next);
850 uhci_dump_tds(sqh->e_next);
856 uhci_dump_tds(uhci_td_t *td)
861 if (uhci_dump_td(td)) {
870 * Let the last QH loop back to the full speed control transfer QH.
871 * This is what intel calls "bandwidth reclamation" and improves
872 * USB performance a lot for some devices.
873 * If we are already looping, just count it.
876 uhci_add_loop(uhci_softc_t *sc)
878 struct uhci_qh *qh_lst;
879 struct uhci_qh *qh_rec;
886 if (++(sc->sc_loops) == 1) {
887 DPRINTFN(6, "add\n");
889 qh_lst = sc->sc_last_qh_p;
890 qh_rec = sc->sc_reclaim_qh_p;
892 /* NOTE: we don't loop back the soft pointer */
894 qh_lst->qh_h_next = qh_rec->qh_self;
895 usb_pc_cpu_flush(qh_lst->page_cache);
900 uhci_rem_loop(uhci_softc_t *sc)
902 struct uhci_qh *qh_lst;
909 if (--(sc->sc_loops) == 0) {
910 DPRINTFN(6, "remove\n");
912 qh_lst = sc->sc_last_qh_p;
913 qh_lst->qh_h_next = htole32(UHCI_PTR_T);
914 usb_pc_cpu_flush(qh_lst->page_cache);
919 uhci_transfer_intr_enqueue(struct usb_xfer *xfer)
921 /* check for early completion */
922 if (uhci_check_transfer(xfer)) {
925 /* put transfer on interrupt queue */
926 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
928 /* start timeout, if any */
929 if (xfer->timeout != 0) {
930 usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
934 #define UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
936 _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
938 DPRINTFN(11, "%p to %p\n", std, last);
940 /* (sc->sc_bus.mtx) must be locked */
942 std->next = last->next;
943 std->td_next = last->td_next;
947 usb_pc_cpu_flush(std->page_cache);
950 * the last->next->prev is never followed: std->next->prev = std;
953 last->td_next = std->td_self;
955 usb_pc_cpu_flush(last->page_cache);
960 #define UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
962 _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
964 DPRINTFN(11, "%p to %p\n", sqh, last);
966 if (sqh->h_prev != NULL) {
967 /* should not happen */
968 DPRINTFN(0, "QH already linked!\n");
971 /* (sc->sc_bus.mtx) must be locked */
973 sqh->h_next = last->h_next;
974 sqh->qh_h_next = last->qh_h_next;
978 usb_pc_cpu_flush(sqh->page_cache);
981 * The "last->h_next->h_prev" is never followed:
983 * "sqh->h_next->h_prev" = sqh;
987 last->qh_h_next = sqh->qh_self;
989 usb_pc_cpu_flush(last->page_cache);
996 #define UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
998 _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
1000 DPRINTFN(11, "%p from %p\n", std, last);
1002 /* (sc->sc_bus.mtx) must be locked */
1004 std->prev->next = std->next;
1005 std->prev->td_next = std->td_next;
1007 usb_pc_cpu_flush(std->prev->page_cache);
1010 std->next->prev = std->prev;
1011 usb_pc_cpu_flush(std->next->page_cache);
1013 return ((last == std) ? std->prev : last);
1016 #define UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
1018 _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
1020 DPRINTFN(11, "%p from %p\n", sqh, last);
1022 /* (sc->sc_bus.mtx) must be locked */
1024 /* only remove if not removed from a queue */
1027 sqh->h_prev->h_next = sqh->h_next;
1028 sqh->h_prev->qh_h_next = sqh->qh_h_next;
1030 usb_pc_cpu_flush(sqh->h_prev->page_cache);
1033 sqh->h_next->h_prev = sqh->h_prev;
1034 usb_pc_cpu_flush(sqh->h_next->page_cache);
1036 last = ((last == sqh) ? sqh->h_prev : last);
1040 usb_pc_cpu_flush(sqh->page_cache);
1046 uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer)
1048 struct usb_page_search res;
1049 uint32_t nframes = xfer->nframes;
1051 uint32_t offset = 0;
1052 uint32_t *plen = xfer->frlengths;
1054 uhci_td_t *td = xfer->td_transfer_first;
1055 uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
1057 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1058 xfer, xfer->endpoint);
1060 /* sync any DMA memory before doing fixups */
1062 usb_bdma_post_sync(xfer);
1066 panic("%s:%d: out of TD's\n",
1067 __FUNCTION__, __LINE__);
1069 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
1070 pp_last = &sc->sc_isoc_p_last[0];
1073 if (uhcidebug > 5) {
1074 DPRINTF("isoc TD\n");
1078 usb_pc_cpu_invalidate(td->page_cache);
1079 status = le32toh(td->td_status);
1081 len = UHCI_TD_GET_ACTLEN(status);
1088 usbd_get_page(td->fix_pc, 0, &res);
1090 /* copy data from fixup location to real location */
1092 usb_pc_cpu_invalidate(td->fix_pc);
1094 usbd_copy_in(xfer->frbuffers, offset,
1101 /* remove TD from schedule */
1102 UHCI_REMOVE_TD(td, *pp_last);
1109 xfer->aframes = xfer->nframes;
1113 uhci_non_isoc_done_sub(struct usb_xfer *xfer)
1115 struct usb_page_search res;
1117 uhci_td_t *td_alt_next;
1122 td = xfer->td_transfer_cache;
1123 td_alt_next = td->alt_next;
1125 if (xfer->aframes != xfer->nframes) {
1126 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1130 usb_pc_cpu_invalidate(td->page_cache);
1131 status = le32toh(td->td_status);
1132 token = le32toh(td->td_token);
1135 * Verify the status and add
1136 * up the actual length:
1139 len = UHCI_TD_GET_ACTLEN(status);
1140 if (len > td->len) {
1141 /* should not happen */
1142 DPRINTF("Invalid status length, "
1143 "0x%04x/0x%04x bytes\n", len, td->len);
1144 status |= UHCI_TD_STALLED;
1146 } else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
1150 usbd_get_page(td->fix_pc, 0, &res);
1153 * copy data from fixup location to real
1157 usb_pc_cpu_invalidate(td->fix_pc);
1159 usbd_copy_in(xfer->frbuffers + xfer->aframes,
1160 xfer->frlengths[xfer->aframes], res.buffer, len);
1162 /* update actual length */
1164 xfer->frlengths[xfer->aframes] += len;
1166 /* Check for last transfer */
1167 if (((void *)td) == xfer->td_transfer_last) {
1171 if (status & UHCI_TD_STALLED) {
1172 /* the transfer is finished */
1176 /* Check for short transfer */
1177 if (len != td->len) {
1178 if (xfer->flags_int.short_frames_ok) {
1179 /* follow alt next */
1182 /* the transfer is finished */
1189 if (td->alt_next != td_alt_next) {
1190 /* this USB frame is complete */
1195 /* update transfer cache */
1197 xfer->td_transfer_cache = td;
1199 /* update data toggle */
1201 xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
1204 if (status & UHCI_TD_ERROR) {
1205 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
1206 "status=%s%s%s%s%s%s%s%s%s%s%s\n",
1207 xfer->address, xfer->endpointno, xfer->aframes,
1208 (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
1209 (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
1210 (status & UHCI_TD_NAK) ? "[NAK]" : "",
1211 (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
1212 (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
1213 (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
1214 (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1215 (status & UHCI_TD_IOC) ? "[IOC]" : "",
1216 (status & UHCI_TD_IOS) ? "[IOS]" : "",
1217 (status & UHCI_TD_LS) ? "[LS]" : "",
1218 (status & UHCI_TD_SPD) ? "[SPD]" : "");
1221 return (status & UHCI_TD_STALLED) ?
1222 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION;
1226 uhci_non_isoc_done(struct usb_xfer *xfer)
1228 usb_error_t err = 0;
1230 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1231 xfer, xfer->endpoint);
1234 if (uhcidebug > 10) {
1235 uhci_dump_tds(xfer->td_transfer_first);
1239 /* sync any DMA memory before doing fixups */
1241 usb_bdma_post_sync(xfer);
1245 xfer->td_transfer_cache = xfer->td_transfer_first;
1247 if (xfer->flags_int.control_xfr) {
1248 if (xfer->flags_int.control_hdr) {
1250 err = uhci_non_isoc_done_sub(xfer);
1254 if (xfer->td_transfer_cache == NULL) {
1258 while (xfer->aframes != xfer->nframes) {
1260 err = uhci_non_isoc_done_sub(xfer);
1263 if (xfer->td_transfer_cache == NULL) {
1268 if (xfer->flags_int.control_xfr &&
1269 !xfer->flags_int.control_act) {
1271 err = uhci_non_isoc_done_sub(xfer);
1274 uhci_device_done(xfer, err);
1277 /*------------------------------------------------------------------------*
1278 * uhci_check_transfer_sub
1280 * The main purpose of this function is to update the data-toggle
1281 * in case it is wrong.
1282 *------------------------------------------------------------------------*/
1284 uhci_check_transfer_sub(struct usb_xfer *xfer)
1288 uhci_td_t *td_alt_next;
1293 td = xfer->td_transfer_cache;
1294 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1296 td_token = td->obj_next->td_token;
1298 xfer->td_transfer_cache = td;
1299 td_self = td->td_self;
1300 td_alt_next = td->alt_next;
1302 if (xfer->flags_int.control_xfr)
1303 goto skip; /* don't touch the DT value! */
1305 if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))))
1306 goto skip; /* data toggle has correct value */
1309 * The data toggle is wrong and we need to toggle it !
1313 td->td_token ^= htole32(UHCI_TD_SET_DT(1));
1314 usb_pc_cpu_flush(td->page_cache);
1316 if (td == xfer->td_transfer_last) {
1322 if (td->alt_next != td_alt_next) {
1330 qh->qh_e_next = td_self;
1331 usb_pc_cpu_flush(qh->page_cache);
1333 DPRINTFN(13, "xfer=%p following alt next\n", xfer);
1336 /*------------------------------------------------------------------------*
1337 * uhci_check_transfer
1340 * 0: USB transfer is not finished
1341 * Else: USB transfer is finished
1342 *------------------------------------------------------------------------*/
1344 uhci_check_transfer(struct usb_xfer *xfer)
1350 DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
1352 if (xfer->endpoint->methods == &uhci_device_isoc_methods) {
1353 /* isochronous transfer */
1355 td = xfer->td_transfer_last;
1357 usb_pc_cpu_invalidate(td->page_cache);
1358 status = le32toh(td->td_status);
1360 /* check also if the first is complete */
1362 td = xfer->td_transfer_first;
1364 usb_pc_cpu_invalidate(td->page_cache);
1365 status |= le32toh(td->td_status);
1367 if (!(status & UHCI_TD_ACTIVE)) {
1368 uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1372 /* non-isochronous transfer */
1375 * check whether there is an error somewhere
1376 * in the middle, or whether there was a short
1377 * packet (SPD and not ACTIVE)
1379 td = xfer->td_transfer_cache;
1382 usb_pc_cpu_invalidate(td->page_cache);
1383 status = le32toh(td->td_status);
1384 token = le32toh(td->td_token);
1387 * if there is an active TD the transfer isn't done
1389 if (status & UHCI_TD_ACTIVE) {
1391 xfer->td_transfer_cache = td;
1395 * last transfer descriptor makes the transfer done
1397 if (((void *)td) == xfer->td_transfer_last) {
1401 * any kind of error makes the transfer done
1403 if (status & UHCI_TD_STALLED) {
1407 * check if we reached the last packet
1408 * or if there is a short packet:
1410 if ((td->td_next == htole32(UHCI_PTR_T)) ||
1411 (UHCI_TD_GET_ACTLEN(status) < td->len)) {
1413 if (xfer->flags_int.short_frames_ok) {
1414 /* follow alt next */
1417 xfer->td_transfer_cache = td;
1418 uhci_check_transfer_sub(xfer);
1422 /* transfer is done */
1427 uhci_non_isoc_done(xfer);
1432 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1440 uhci_interrupt_poll(uhci_softc_t *sc)
1442 struct usb_xfer *xfer;
1445 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1447 * check if transfer is transferred
1449 if (uhci_check_transfer(xfer)) {
1450 /* queue has been modified */
1456 /*------------------------------------------------------------------------*
1457 * uhci_interrupt - UHCI interrupt handler
1459 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1460 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1462 *------------------------------------------------------------------------*/
1464 uhci_interrupt(uhci_softc_t *sc)
1468 USB_BUS_LOCK(&sc->sc_bus);
1470 DPRINTFN(16, "real interrupt\n");
1473 if (uhcidebug > 15) {
1477 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1479 /* the interrupt was not for us */
1482 if (status & (UHCI_STS_RD | UHCI_STS_HSE |
1483 UHCI_STS_HCPE | UHCI_STS_HCH)) {
1485 if (status & UHCI_STS_RD) {
1487 printf("%s: resume detect\n",
1491 if (status & UHCI_STS_HSE) {
1492 printf("%s: host system error\n",
1495 if (status & UHCI_STS_HCPE) {
1496 printf("%s: host controller process error\n",
1499 if (status & UHCI_STS_HCH) {
1500 /* no acknowledge needed */
1501 DPRINTF("%s: host controller halted\n",
1504 if (uhcidebug > 0) {
1510 /* get acknowledge bits */
1511 status &= (UHCI_STS_USBINT |
1518 /* nothing to acknowledge */
1521 /* acknowledge interrupts */
1522 UWRITE2(sc, UHCI_STS, status);
1524 /* poll all the USB transfers */
1525 uhci_interrupt_poll(sc);
1528 USB_BUS_UNLOCK(&sc->sc_bus);
1532 * called when a request does not complete
1535 uhci_timeout(void *arg)
1537 struct usb_xfer *xfer = arg;
1539 DPRINTF("xfer=%p\n", xfer);
1541 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1543 /* transfer is transferred */
1544 uhci_device_done(xfer, USB_ERR_TIMEOUT);
1548 uhci_do_poll(struct usb_bus *bus)
1550 struct uhci_softc *sc = UHCI_BUS2SC(bus);
1552 USB_BUS_LOCK(&sc->sc_bus);
1553 uhci_interrupt_poll(sc);
1554 USB_BUS_UNLOCK(&sc->sc_bus);
1558 uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
1562 uhci_td_t *td_alt_next;
1565 uint8_t shortpkt_old;
1569 shortpkt_old = temp->shortpkt;
1570 len_old = temp->len;
1573 /* software is used to detect short incoming transfers */
1575 if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
1576 temp->td_status |= htole32(UHCI_TD_SPD);
1578 temp->td_status &= ~htole32(UHCI_TD_SPD);
1581 temp->ml.buf_offset = 0;
1585 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1586 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
1589 td_next = temp->td_next;
1593 if (temp->len == 0) {
1595 if (temp->shortpkt) {
1598 /* send a Zero Length Packet, ZLP, last */
1601 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
1606 average = temp->average;
1608 if (temp->len < average) {
1610 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1611 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
1612 average = temp->len;
1616 if (td_next == NULL) {
1617 panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
1622 td_next = td->obj_next;
1624 /* check if we are pre-computing */
1628 /* update remaining length */
1630 temp->len -= average;
1634 /* fill out current TD */
1636 td->td_status = temp->td_status;
1637 td->td_token = temp->td_token;
1639 /* update data toggle */
1641 temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
1651 /* update remaining length */
1653 temp->len -= average;
1657 /* fill out buffer pointer and do fixup, if any */
1659 uhci_mem_layout_fixup(&temp->ml, td);
1662 td->alt_next = td_alt_next;
1664 if ((td_next == td_alt_next) && temp->setup_alt_next) {
1665 /* we need to receive these frames one by one ! */
1666 td->td_status |= htole32(UHCI_TD_IOC);
1667 td->td_next = htole32(UHCI_PTR_T);
1670 /* link the current TD with the next one */
1671 td->td_next = td_next->td_self;
1675 usb_pc_cpu_flush(td->page_cache);
1681 /* setup alt next pointer, if any */
1682 if (temp->last_frame) {
1685 /* we use this field internally */
1686 td_alt_next = td_next;
1690 temp->shortpkt = shortpkt_old;
1691 temp->len = len_old;
1695 temp->td_next = td_next;
1699 uhci_setup_standard_chain(struct usb_xfer *xfer)
1701 struct uhci_std_temp temp;
1705 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1706 xfer->address, UE_GET_ADDR(xfer->endpointno),
1707 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1709 temp.average = xfer->max_frame_size;
1710 temp.max_frame_size = xfer->max_frame_size;
1712 /* toggle the DMA set we are using */
1713 xfer->flags_int.curr_dma_set ^= 1;
1715 /* get next DMA set */
1716 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1717 xfer->td_transfer_first = td;
1718 xfer->td_transfer_cache = td;
1722 temp.last_frame = 0;
1723 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1725 uhci_mem_layout_init(&temp.ml, xfer);
1728 htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
1731 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1732 temp.td_status |= htole32(UHCI_TD_LS);
1735 htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) |
1736 UHCI_TD_SET_DEVADDR(xfer->address));
1738 if (xfer->endpoint->toggle_next) {
1740 temp.td_token |= htole32(UHCI_TD_SET_DT(1));
1742 /* check if we should prepend a setup message */
1744 if (xfer->flags_int.control_xfr) {
1746 if (xfer->flags_int.control_hdr) {
1748 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1749 UHCI_TD_SET_ENDPT(0xF));
1750 temp.td_token |= htole32(UHCI_TD_PID_SETUP |
1753 temp.len = xfer->frlengths[0];
1754 temp.ml.buf_pc = xfer->frbuffers + 0;
1755 temp.shortpkt = temp.len ? 1 : 0;
1756 /* check for last frame */
1757 if (xfer->nframes == 1) {
1758 /* no STATUS stage yet, SETUP is last */
1759 if (xfer->flags_int.control_act) {
1760 temp.last_frame = 1;
1761 temp.setup_alt_next = 0;
1764 uhci_setup_standard_chain_sub(&temp);
1771 while (x != xfer->nframes) {
1773 /* DATA0 / DATA1 message */
1775 temp.len = xfer->frlengths[x];
1776 temp.ml.buf_pc = xfer->frbuffers + x;
1780 if (x == xfer->nframes) {
1781 if (xfer->flags_int.control_xfr) {
1782 /* no STATUS stage yet, DATA is last */
1783 if (xfer->flags_int.control_act) {
1784 temp.last_frame = 1;
1785 temp.setup_alt_next = 0;
1788 temp.last_frame = 1;
1789 temp.setup_alt_next = 0;
1793 * Keep previous data toggle,
1794 * device address and endpoint number:
1797 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1798 UHCI_TD_SET_ENDPT(0xF) |
1801 if (temp.len == 0) {
1803 /* make sure that we send an USB packet */
1809 /* regular data transfer */
1811 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1814 /* set endpoint direction */
1817 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1818 htole32(UHCI_TD_PID_IN) :
1819 htole32(UHCI_TD_PID_OUT);
1821 uhci_setup_standard_chain_sub(&temp);
1824 /* check if we should append a status stage */
1826 if (xfer->flags_int.control_xfr &&
1827 !xfer->flags_int.control_act) {
1830 * send a DATA1 message and reverse the current endpoint
1834 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1835 UHCI_TD_SET_ENDPT(0xF) |
1838 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1839 htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
1840 htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
1843 temp.ml.buf_pc = NULL;
1845 temp.last_frame = 1;
1846 temp.setup_alt_next = 0;
1848 uhci_setup_standard_chain_sub(&temp);
1852 /* Ensure that last TD is terminating: */
1853 td->td_next = htole32(UHCI_PTR_T);
1855 /* set interrupt bit */
1857 td->td_status |= htole32(UHCI_TD_IOC);
1859 usb_pc_cpu_flush(td->page_cache);
1861 /* must have at least one frame! */
1863 xfer->td_transfer_last = td;
1866 if (uhcidebug > 8) {
1867 DPRINTF("nexttog=%d; data before transfer:\n",
1868 xfer->endpoint->toggle_next);
1869 uhci_dump_tds(xfer->td_transfer_first);
1872 return (xfer->td_transfer_first);
1875 /* NOTE: "done" can be run two times in a row,
1876 * from close and from interrupt
1880 uhci_device_done(struct usb_xfer *xfer, usb_error_t error)
1882 struct usb_pipe_methods *methods = xfer->endpoint->methods;
1883 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1886 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1888 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1889 xfer, xfer->endpoint, error);
1891 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1893 usb_pc_cpu_invalidate(qh->page_cache);
1895 if (xfer->flags_int.bandwidth_reclaimed) {
1896 xfer->flags_int.bandwidth_reclaimed = 0;
1899 if (methods == &uhci_device_bulk_methods) {
1900 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
1902 if (methods == &uhci_device_ctrl_methods) {
1903 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1904 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
1906 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
1909 if (methods == &uhci_device_intr_methods) {
1910 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
1913 * Only finish isochronous transfers once
1914 * which will update "xfer->frlengths".
1916 if (xfer->td_transfer_first &&
1917 xfer->td_transfer_last) {
1918 if (methods == &uhci_device_isoc_methods) {
1919 uhci_isoc_done(sc, xfer);
1921 xfer->td_transfer_first = NULL;
1922 xfer->td_transfer_last = NULL;
1924 /* dequeue transfer and start next transfer */
1925 usbd_transfer_done(xfer, error);
1928 /*------------------------------------------------------------------------*
1930 *------------------------------------------------------------------------*/
1932 uhci_device_bulk_open(struct usb_xfer *xfer)
1938 uhci_device_bulk_close(struct usb_xfer *xfer)
1940 uhci_device_done(xfer, USB_ERR_CANCELLED);
1944 uhci_device_bulk_enter(struct usb_xfer *xfer)
1950 uhci_device_bulk_start(struct usb_xfer *xfer)
1952 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1957 td = uhci_setup_standard_chain(xfer);
1960 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1963 qh->qh_e_next = td->td_self;
1965 if (xfer->xroot->udev->flags.self_suspended == 0) {
1966 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
1968 xfer->flags_int.bandwidth_reclaimed = 1;
1970 usb_pc_cpu_flush(qh->page_cache);
1973 /* put transfer on interrupt queue */
1974 uhci_transfer_intr_enqueue(xfer);
1977 struct usb_pipe_methods uhci_device_bulk_methods =
1979 .open = uhci_device_bulk_open,
1980 .close = uhci_device_bulk_close,
1981 .enter = uhci_device_bulk_enter,
1982 .start = uhci_device_bulk_start,
1985 /*------------------------------------------------------------------------*
1986 * uhci control support
1987 *------------------------------------------------------------------------*/
1989 uhci_device_ctrl_open(struct usb_xfer *xfer)
1995 uhci_device_ctrl_close(struct usb_xfer *xfer)
1997 uhci_device_done(xfer, USB_ERR_CANCELLED);
2001 uhci_device_ctrl_enter(struct usb_xfer *xfer)
2007 uhci_device_ctrl_start(struct usb_xfer *xfer)
2009 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2014 td = uhci_setup_standard_chain(xfer);
2017 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2020 qh->qh_e_next = td->td_self;
2023 * NOTE: some devices choke on bandwidth- reclamation for control
2026 if (xfer->xroot->udev->flags.self_suspended == 0) {
2027 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
2028 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
2030 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
2033 usb_pc_cpu_flush(qh->page_cache);
2035 /* put transfer on interrupt queue */
2036 uhci_transfer_intr_enqueue(xfer);
2039 struct usb_pipe_methods uhci_device_ctrl_methods =
2041 .open = uhci_device_ctrl_open,
2042 .close = uhci_device_ctrl_close,
2043 .enter = uhci_device_ctrl_enter,
2044 .start = uhci_device_ctrl_start,
2047 /*------------------------------------------------------------------------*
2048 * uhci interrupt support
2049 *------------------------------------------------------------------------*/
2051 uhci_device_intr_open(struct usb_xfer *xfer)
2053 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2059 bit = UHCI_IFRAMELIST_COUNT / 2;
2061 if (xfer->interval >= bit) {
2065 if (sc->sc_intr_stat[x] <
2066 sc->sc_intr_stat[best]) {
2076 sc->sc_intr_stat[best]++;
2077 xfer->qh_pos = best;
2079 DPRINTFN(3, "best=%d interval=%d\n",
2080 best, xfer->interval);
2084 uhci_device_intr_close(struct usb_xfer *xfer)
2086 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2088 sc->sc_intr_stat[xfer->qh_pos]--;
2090 uhci_device_done(xfer, USB_ERR_CANCELLED);
2094 uhci_device_intr_enter(struct usb_xfer *xfer)
2100 uhci_device_intr_start(struct usb_xfer *xfer)
2102 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2107 td = uhci_setup_standard_chain(xfer);
2110 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2113 qh->qh_e_next = td->td_self;
2115 if (xfer->xroot->udev->flags.self_suspended == 0) {
2116 /* enter QHs into the controller data structures */
2117 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
2119 usb_pc_cpu_flush(qh->page_cache);
2122 /* put transfer on interrupt queue */
2123 uhci_transfer_intr_enqueue(xfer);
2126 struct usb_pipe_methods uhci_device_intr_methods =
2128 .open = uhci_device_intr_open,
2129 .close = uhci_device_intr_close,
2130 .enter = uhci_device_intr_enter,
2131 .start = uhci_device_intr_start,
2134 /*------------------------------------------------------------------------*
2135 * uhci isochronous support
2136 *------------------------------------------------------------------------*/
2138 uhci_device_isoc_open(struct usb_xfer *xfer)
2145 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
2146 UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) :
2147 UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0);
2149 td_token = htole32(td_token);
2151 /* initialize all TD's */
2153 for (ds = 0; ds != 2; ds++) {
2155 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2157 /* mark TD as inactive */
2158 td->td_status = htole32(UHCI_TD_IOS);
2159 td->td_token = td_token;
2161 usb_pc_cpu_flush(td->page_cache);
2167 uhci_device_isoc_close(struct usb_xfer *xfer)
2169 uhci_device_done(xfer, USB_ERR_CANCELLED);
2173 uhci_device_isoc_enter(struct usb_xfer *xfer)
2175 struct uhci_mem_layout ml;
2176 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2186 uhci_td_t *td_last = NULL;
2187 uhci_td_t **pp_last;
2189 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2190 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2192 nframes = UREAD2(sc, UHCI_FRNUM);
2194 temp = (nframes - xfer->endpoint->isoc_next) &
2195 (UHCI_VFRAMELIST_COUNT - 1);
2197 if ((xfer->endpoint->is_synced == 0) ||
2198 (temp < xfer->nframes)) {
2200 * If there is data underflow or the pipe queue is empty we
2201 * schedule the transfer a few frames ahead of the current
2202 * frame position. Else two isochronous transfers might
2205 xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
2206 xfer->endpoint->is_synced = 1;
2207 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2210 * compute how many milliseconds the insertion is ahead of the
2211 * current frame position:
2213 temp = (xfer->endpoint->isoc_next - nframes) &
2214 (UHCI_VFRAMELIST_COUNT - 1);
2217 * pre-compute when the isochronous transfer will be finished:
2219 xfer->isoc_time_complete =
2220 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2223 /* get the real number of frames */
2225 nframes = xfer->nframes;
2227 uhci_mem_layout_init(&ml, xfer);
2229 plen = xfer->frlengths;
2231 /* toggle the DMA set we are using */
2232 xfer->flags_int.curr_dma_set ^= 1;
2234 /* get next DMA set */
2235 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2236 xfer->td_transfer_first = td;
2238 pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next];
2240 /* store starting position */
2242 xfer->qh_pos = xfer->endpoint->isoc_next;
2246 panic("%s:%d: out of TD's\n",
2247 __FUNCTION__, __LINE__);
2249 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
2250 pp_last = &sc->sc_isoc_p_last[0];
2252 if (*plen > xfer->max_frame_size) {
2256 printf("%s: frame length(%d) exceeds %d "
2257 "bytes (frame truncated)\n",
2258 __FUNCTION__, *plen,
2259 xfer->max_frame_size);
2262 *plen = xfer->max_frame_size;
2264 /* reuse td_token from last transfer */
2266 td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2267 td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
2273 * Do not call "uhci_mem_layout_fixup()" when the
2281 /* fill out buffer pointer and do fixup, if any */
2283 uhci_mem_layout_fixup(&ml, td);
2289 td->td_status = htole32
2290 (UHCI_TD_ZERO_ACTLEN
2291 (UHCI_TD_SET_ERRCNT(0) |
2296 td->td_status = htole32
2297 (UHCI_TD_ZERO_ACTLEN
2298 (UHCI_TD_SET_ERRCNT(0) |
2303 usb_pc_cpu_flush(td->page_cache);
2306 if (uhcidebug > 5) {
2307 DPRINTF("TD %d\n", nframes);
2311 /* insert TD into schedule */
2312 UHCI_APPEND_TD(td, *pp_last);
2320 xfer->td_transfer_last = td_last;
2322 /* update isoc_next */
2323 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
2324 (UHCI_VFRAMELIST_COUNT - 1);
2328 uhci_device_isoc_start(struct usb_xfer *xfer)
2330 /* put transfer on interrupt queue */
2331 uhci_transfer_intr_enqueue(xfer);
2334 struct usb_pipe_methods uhci_device_isoc_methods =
2336 .open = uhci_device_isoc_open,
2337 .close = uhci_device_isoc_close,
2338 .enter = uhci_device_isoc_enter,
2339 .start = uhci_device_isoc_start,
2342 /*------------------------------------------------------------------------*
2343 * uhci root control support
2344 *------------------------------------------------------------------------*
2345 * Simulate a hardware hub by handling all the necessary requests.
2346 *------------------------------------------------------------------------*/
2349 struct usb_device_descriptor uhci_devd =
2351 sizeof(struct usb_device_descriptor),
2352 UDESC_DEVICE, /* type */
2353 {0x00, 0x01}, /* USB version */
2354 UDCLASS_HUB, /* class */
2355 UDSUBCLASS_HUB, /* subclass */
2356 UDPROTO_FSHUB, /* protocol */
2357 64, /* max packet */
2358 {0}, {0}, {0x00, 0x01}, /* device id */
2359 1, 2, 0, /* string indicies */
2360 1 /* # of configurations */
2363 static const struct uhci_config_desc uhci_confd = {
2365 .bLength = sizeof(struct usb_config_descriptor),
2366 .bDescriptorType = UDESC_CONFIG,
2367 .wTotalLength[0] = sizeof(uhci_confd),
2369 .bConfigurationValue = 1,
2370 .iConfiguration = 0,
2371 .bmAttributes = UC_SELF_POWERED,
2372 .bMaxPower = 0 /* max power */
2375 .bLength = sizeof(struct usb_interface_descriptor),
2376 .bDescriptorType = UDESC_INTERFACE,
2378 .bInterfaceClass = UICLASS_HUB,
2379 .bInterfaceSubClass = UISUBCLASS_HUB,
2380 .bInterfaceProtocol = UIPROTO_FSHUB,
2383 .bLength = sizeof(struct usb_endpoint_descriptor),
2384 .bDescriptorType = UDESC_ENDPOINT,
2385 .bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
2386 .bmAttributes = UE_INTERRUPT,
2387 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
2393 struct usb_hub_descriptor_min uhci_hubd_piix =
2395 sizeof(uhci_hubd_piix),
2398 {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
2399 50, /* power on to power good */
2401 {0x00}, /* both ports are removable */
2405 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2406 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2407 * should not be used by the USB subsystem. As we cannot issue a
2408 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2409 * will be enabled as part of the reset.
2411 * On the VT83C572, the port cannot be successfully enabled until the
2412 * outstanding "port enable change" and "connection status change"
2413 * events have been reset.
2416 uhci_portreset(uhci_softc_t *sc, uint16_t index)
2423 port = UHCI_PORTSC1;
2424 else if (index == 2)
2425 port = UHCI_PORTSC2;
2427 return (USB_ERR_IOERROR);
2430 * Before we do anything, turn on SOF messages on the USB
2431 * BUS. Some USB devices do not cope without them!
2435 x = URWMASK(UREAD2(sc, port));
2436 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2438 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2439 USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
2441 DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
2442 index, UREAD2(sc, port));
2444 x = URWMASK(UREAD2(sc, port));
2445 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2448 mtx_unlock(&sc->sc_bus.bus_mtx);
2451 * This delay needs to be exactly 100us, else some USB devices
2456 mtx_lock(&sc->sc_bus.bus_mtx);
2458 DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
2459 index, UREAD2(sc, port));
2461 x = URWMASK(UREAD2(sc, port));
2462 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2464 for (lim = 0; lim < 12; lim++) {
2466 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2467 USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
2469 x = UREAD2(sc, port);
2471 DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
2474 if (!(x & UHCI_PORTSC_CCS)) {
2476 * No device is connected (or was disconnected
2477 * during reset). Consider the port reset.
2478 * The delay must be long enough to ensure on
2479 * the initial iteration that the device
2480 * connection will have been registered. 50ms
2481 * appears to be sufficient, but 20ms is not.
2483 DPRINTFN(4, "uhci port %d loop %u, device detached\n",
2487 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
2489 * Port enabled changed and/or connection
2490 * status changed were set. Reset either or
2491 * both raised flags (by writing a 1 to that
2492 * bit), and wait again for state to settle.
2494 UWRITE2(sc, port, URWMASK(x) |
2495 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
2498 if (x & UHCI_PORTSC_PE) {
2499 /* port is enabled */
2502 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
2505 DPRINTFN(2, "uhci port %d reset timed out\n", index);
2506 return (USB_ERR_TIMEOUT);
2509 DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
2510 index, UREAD2(sc, port));
2513 return (USB_ERR_NORMAL_COMPLETION);
2517 uhci_roothub_exec(struct usb_device *udev,
2518 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2520 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
2522 const char *str_ptr;
2532 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2535 ptr = (const void *)&sc->sc_hub_desc.temp;
2539 value = UGETW(req->wValue);
2540 index = UGETW(req->wIndex);
2542 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2543 "wValue=0x%04x wIndex=0x%04x\n",
2544 req->bmRequestType, req->bRequest,
2545 UGETW(req->wLength), value, index);
2547 #define C(x,y) ((x) | ((y) << 8))
2548 switch (C(req->bRequest, req->bmRequestType)) {
2549 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2550 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2551 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2553 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2554 * for the integrated root hub.
2557 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2559 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2561 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2562 switch (value >> 8) {
2564 if ((value & 0xff) != 0) {
2565 err = USB_ERR_IOERROR;
2568 len = sizeof(uhci_devd);
2569 ptr = (const void *)&uhci_devd;
2573 if ((value & 0xff) != 0) {
2574 err = USB_ERR_IOERROR;
2577 len = sizeof(uhci_confd);
2578 ptr = (const void *)&uhci_confd;
2582 switch (value & 0xff) {
2583 case 0: /* Language table */
2587 case 1: /* Vendor */
2588 str_ptr = sc->sc_vendor;
2591 case 2: /* Product */
2592 str_ptr = "UHCI root HUB";
2600 len = usb_make_str_desc
2601 (sc->sc_hub_desc.temp,
2602 sizeof(sc->sc_hub_desc.temp),
2607 err = USB_ERR_IOERROR;
2611 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2613 sc->sc_hub_desc.temp[0] = 0;
2615 case C(UR_GET_STATUS, UT_READ_DEVICE):
2617 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
2619 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2620 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2622 USETW(sc->sc_hub_desc.stat.wStatus, 0);
2624 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2625 if (value >= UHCI_MAX_DEVICES) {
2626 err = USB_ERR_IOERROR;
2629 sc->sc_addr = value;
2631 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2632 if ((value != 0) && (value != 1)) {
2633 err = USB_ERR_IOERROR;
2636 sc->sc_conf = value;
2638 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2640 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2641 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2642 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2643 err = USB_ERR_IOERROR;
2645 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2647 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2650 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2652 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2653 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
2654 "port=%d feature=%d\n",
2657 port = UHCI_PORTSC1;
2658 else if (index == 2)
2659 port = UHCI_PORTSC2;
2661 err = USB_ERR_IOERROR;
2665 case UHF_PORT_ENABLE:
2666 x = URWMASK(UREAD2(sc, port));
2667 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2669 case UHF_PORT_SUSPEND:
2670 x = URWMASK(UREAD2(sc, port));
2671 UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
2673 case UHF_PORT_RESET:
2674 x = URWMASK(UREAD2(sc, port));
2675 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2677 case UHF_C_PORT_CONNECTION:
2678 x = URWMASK(UREAD2(sc, port));
2679 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2681 case UHF_C_PORT_ENABLE:
2682 x = URWMASK(UREAD2(sc, port));
2683 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2685 case UHF_C_PORT_OVER_CURRENT:
2686 x = URWMASK(UREAD2(sc, port));
2687 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2689 case UHF_C_PORT_RESET:
2691 err = USB_ERR_NORMAL_COMPLETION;
2693 case UHF_C_PORT_SUSPEND:
2694 sc->sc_isresumed &= ~(1 << index);
2696 case UHF_PORT_CONNECTION:
2697 case UHF_PORT_OVER_CURRENT:
2698 case UHF_PORT_POWER:
2699 case UHF_PORT_LOW_SPEED:
2701 err = USB_ERR_IOERROR;
2705 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2707 port = UHCI_PORTSC1;
2708 else if (index == 2)
2709 port = UHCI_PORTSC2;
2711 err = USB_ERR_IOERROR;
2715 sc->sc_hub_desc.temp[0] =
2716 ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2717 UHCI_PORTSC_LS_SHIFT);
2719 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2720 if ((value & 0xff) != 0) {
2721 err = USB_ERR_IOERROR;
2724 len = sizeof(uhci_hubd_piix);
2725 ptr = (const void *)&uhci_hubd_piix;
2727 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2729 bzero(sc->sc_hub_desc.temp, 16);
2731 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2733 port = UHCI_PORTSC1;
2734 else if (index == 2)
2735 port = UHCI_PORTSC2;
2737 err = USB_ERR_IOERROR;
2740 x = UREAD2(sc, port);
2741 status = change = 0;
2742 if (x & UHCI_PORTSC_CCS)
2743 status |= UPS_CURRENT_CONNECT_STATUS;
2744 if (x & UHCI_PORTSC_CSC)
2745 change |= UPS_C_CONNECT_STATUS;
2746 if (x & UHCI_PORTSC_PE)
2747 status |= UPS_PORT_ENABLED;
2748 if (x & UHCI_PORTSC_POEDC)
2749 change |= UPS_C_PORT_ENABLED;
2750 if (x & UHCI_PORTSC_OCI)
2751 status |= UPS_OVERCURRENT_INDICATOR;
2752 if (x & UHCI_PORTSC_OCIC)
2753 change |= UPS_C_OVERCURRENT_INDICATOR;
2754 if (x & UHCI_PORTSC_LSDA)
2755 status |= UPS_LOW_SPEED;
2756 if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
2757 /* need to do a write back */
2758 UWRITE2(sc, port, URWMASK(x));
2760 /* wait 20ms for resume sequence to complete */
2761 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
2763 /* clear suspend and resume detect */
2764 UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
2767 /* wait a little bit */
2768 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
2770 sc->sc_isresumed |= (1 << index);
2772 } else if (x & UHCI_PORTSC_SUSP) {
2773 status |= UPS_SUSPEND;
2775 status |= UPS_PORT_POWER;
2776 if (sc->sc_isresumed & (1 << index))
2777 change |= UPS_C_SUSPEND;
2779 change |= UPS_C_PORT_RESET;
2780 USETW(sc->sc_hub_desc.ps.wPortStatus, status);
2781 USETW(sc->sc_hub_desc.ps.wPortChange, change);
2782 len = sizeof(sc->sc_hub_desc.ps);
2784 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2785 err = USB_ERR_IOERROR;
2787 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2789 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2791 port = UHCI_PORTSC1;
2792 else if (index == 2)
2793 port = UHCI_PORTSC2;
2795 err = USB_ERR_IOERROR;
2799 case UHF_PORT_ENABLE:
2800 x = URWMASK(UREAD2(sc, port));
2801 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2803 case UHF_PORT_SUSPEND:
2804 x = URWMASK(UREAD2(sc, port));
2805 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2807 case UHF_PORT_RESET:
2808 err = uhci_portreset(sc, index);
2810 case UHF_PORT_POWER:
2811 /* pretend we turned on power */
2812 err = USB_ERR_NORMAL_COMPLETION;
2814 case UHF_C_PORT_CONNECTION:
2815 case UHF_C_PORT_ENABLE:
2816 case UHF_C_PORT_OVER_CURRENT:
2817 case UHF_PORT_CONNECTION:
2818 case UHF_PORT_OVER_CURRENT:
2819 case UHF_PORT_LOW_SPEED:
2820 case UHF_C_PORT_SUSPEND:
2821 case UHF_C_PORT_RESET:
2823 err = USB_ERR_IOERROR;
2828 err = USB_ERR_IOERROR;
2838 * This routine is executed periodically and simulates interrupts from
2839 * the root controller interrupt pipe for port status change:
2842 uhci_root_intr(uhci_softc_t *sc)
2846 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2848 sc->sc_hub_idata[0] = 0;
2850 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
2851 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2852 sc->sc_hub_idata[0] |= 1 << 1;
2854 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
2855 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2856 sc->sc_hub_idata[0] |= 1 << 2;
2860 usb_callout_reset(&sc->sc_root_intr, hz,
2861 (void *)&uhci_root_intr, sc);
2863 if (sc->sc_hub_idata[0] != 0) {
2864 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2865 sizeof(sc->sc_hub_idata));
2870 uhci_xfer_setup(struct usb_setup_params *parm)
2872 struct usb_page_search page_info;
2873 struct usb_page_cache *pc;
2875 struct usb_xfer *xfer;
2883 sc = UHCI_BUS2SC(parm->udev->bus);
2884 xfer = parm->curr_xfer;
2886 parm->hc_max_packet_size = 0x500;
2887 parm->hc_max_packet_count = 1;
2888 parm->hc_max_frame_size = 0x500;
2891 * compute ntd and nqh
2893 if (parm->methods == &uhci_device_ctrl_methods) {
2894 xfer->flags_int.bdma_enable = 1;
2895 xfer->flags_int.bdma_no_post_sync = 1;
2897 usbd_transfer_setup_sub(parm);
2899 /* see EHCI HC driver for proof of "ntd" formula */
2902 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
2903 + (xfer->max_data_length / xfer->max_frame_size));
2905 } else if (parm->methods == &uhci_device_bulk_methods) {
2906 xfer->flags_int.bdma_enable = 1;
2907 xfer->flags_int.bdma_no_post_sync = 1;
2909 usbd_transfer_setup_sub(parm);
2912 ntd = ((2 * xfer->nframes)
2913 + (xfer->max_data_length / xfer->max_frame_size));
2915 } else if (parm->methods == &uhci_device_intr_methods) {
2916 xfer->flags_int.bdma_enable = 1;
2917 xfer->flags_int.bdma_no_post_sync = 1;
2919 usbd_transfer_setup_sub(parm);
2922 ntd = ((2 * xfer->nframes)
2923 + (xfer->max_data_length / xfer->max_frame_size));
2925 } else if (parm->methods == &uhci_device_isoc_methods) {
2926 xfer->flags_int.bdma_enable = 1;
2927 xfer->flags_int.bdma_no_post_sync = 1;
2929 usbd_transfer_setup_sub(parm);
2932 ntd = xfer->nframes;
2936 usbd_transfer_setup_sub(parm);
2946 * NOTE: the UHCI controller requires that
2947 * every packet must be contiguous on
2948 * the same USB memory page !
2950 nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
2953 * Compute a suitable power of two alignment
2954 * for our "max_frame_size" fixup buffer(s):
2956 align = xfer->max_frame_size;
2963 /* check for power of two */
2964 if (!(xfer->max_frame_size &
2965 (xfer->max_frame_size - 1))) {
2969 * We don't allow alignments of
2970 * less than 8 bytes:
2972 * NOTE: Allocating using an aligment
2973 * of 1 byte has special meaning!
2980 if (usbd_transfer_setup_sub_malloc(
2981 parm, &pc, xfer->max_frame_size,
2983 parm->err = USB_ERR_NOMEM;
2986 xfer->buf_fixup = pc;
2995 if (usbd_transfer_setup_sub_malloc(
2996 parm, &pc, sizeof(uhci_td_t),
2997 UHCI_TD_ALIGN, ntd)) {
2998 parm->err = USB_ERR_NOMEM;
3002 for (n = 0; n != ntd; n++) {
3005 usbd_get_page(pc + n, 0, &page_info);
3007 td = page_info.buffer;
3010 if ((parm->methods == &uhci_device_bulk_methods) ||
3011 (parm->methods == &uhci_device_ctrl_methods) ||
3012 (parm->methods == &uhci_device_intr_methods)) {
3013 /* set depth first bit */
3014 td->td_self = htole32(page_info.physaddr |
3015 UHCI_PTR_TD | UHCI_PTR_VF);
3017 td->td_self = htole32(page_info.physaddr |
3021 td->obj_next = last_obj;
3022 td->page_cache = pc + n;
3026 usb_pc_cpu_flush(pc + n);
3029 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3033 if (usbd_transfer_setup_sub_malloc(
3034 parm, &pc, sizeof(uhci_qh_t),
3035 UHCI_QH_ALIGN, nqh)) {
3036 parm->err = USB_ERR_NOMEM;
3040 for (n = 0; n != nqh; n++) {
3043 usbd_get_page(pc + n, 0, &page_info);
3045 qh = page_info.buffer;
3048 qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
3049 qh->obj_next = last_obj;
3050 qh->page_cache = pc + n;
3054 usb_pc_cpu_flush(pc + n);
3057 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3059 if (!xfer->flags_int.curr_dma_set) {
3060 xfer->flags_int.curr_dma_set = 1;
3066 uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3067 struct usb_endpoint *ep)
3069 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
3071 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3073 edesc->bEndpointAddress, udev->flags.usb_mode,
3076 if (udev->flags.usb_mode != USB_MODE_HOST) {
3080 if (udev->device_index != sc->sc_addr) {
3081 switch (edesc->bmAttributes & UE_XFERTYPE) {
3083 ep->methods = &uhci_device_ctrl_methods;
3086 ep->methods = &uhci_device_intr_methods;
3088 case UE_ISOCHRONOUS:
3089 if (udev->speed == USB_SPEED_FULL) {
3090 ep->methods = &uhci_device_isoc_methods;
3094 if (udev->speed != USB_SPEED_LOW) {
3095 ep->methods = &uhci_device_bulk_methods;
3106 uhci_xfer_unsetup(struct usb_xfer *xfer)
3112 uhci_get_dma_delay(struct usb_bus *bus, uint32_t *pus)
3115 * Wait until hardware has finished any possible use of the
3116 * transfer descriptor(s) and QH
3118 *pus = (1125); /* microseconds */
3122 uhci_device_resume(struct usb_device *udev)
3124 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3125 struct usb_xfer *xfer;
3126 struct usb_pipe_methods *methods;
3131 USB_BUS_LOCK(udev->bus);
3133 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3135 if (xfer->xroot->udev == udev) {
3137 methods = xfer->endpoint->methods;
3138 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3140 if (methods == &uhci_device_bulk_methods) {
3141 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
3143 xfer->flags_int.bandwidth_reclaimed = 1;
3145 if (methods == &uhci_device_ctrl_methods) {
3146 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3147 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
3149 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
3152 if (methods == &uhci_device_intr_methods) {
3153 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3158 USB_BUS_UNLOCK(udev->bus);
3164 uhci_device_suspend(struct usb_device *udev)
3166 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3167 struct usb_xfer *xfer;
3168 struct usb_pipe_methods *methods;
3173 USB_BUS_LOCK(udev->bus);
3175 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3177 if (xfer->xroot->udev == udev) {
3179 methods = xfer->endpoint->methods;
3180 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3182 if (xfer->flags_int.bandwidth_reclaimed) {
3183 xfer->flags_int.bandwidth_reclaimed = 0;
3186 if (methods == &uhci_device_bulk_methods) {
3187 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
3189 if (methods == &uhci_device_ctrl_methods) {
3190 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3191 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
3193 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
3196 if (methods == &uhci_device_intr_methods) {
3197 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3202 USB_BUS_UNLOCK(udev->bus);
3208 uhci_set_hw_power(struct usb_bus *bus)
3210 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3217 flags = bus->hw_power_state;
3220 * WARNING: Some FULL speed USB devices require periodic SOF
3221 * messages! If any USB devices are connected through the
3222 * UHCI, power save will be disabled!
3224 if (flags & (USB_HW_POWER_CONTROL |
3225 USB_HW_POWER_NON_ROOT_HUB |
3227 USB_HW_POWER_INTERRUPT |
3228 USB_HW_POWER_ISOC)) {
3229 DPRINTF("Some USB transfer is "
3230 "active on unit %u.\n",
3231 device_get_unit(sc->sc_bus.bdev));
3234 DPRINTF("Power save on unit %u.\n",
3235 device_get_unit(sc->sc_bus.bdev));
3236 UHCICMD(sc, UHCI_CMD_MAXP);
3239 USB_BUS_UNLOCK(bus);
3245 struct usb_bus_methods uhci_bus_methods =
3247 .endpoint_init = uhci_ep_init,
3248 .xfer_setup = uhci_xfer_setup,
3249 .xfer_unsetup = uhci_xfer_unsetup,
3250 .get_dma_delay = uhci_get_dma_delay,
3251 .device_resume = uhci_device_resume,
3252 .device_suspend = uhci_device_suspend,
3253 .set_hw_power = uhci_set_hw_power,
3254 .roothub_exec = uhci_roothub_exec,
3255 .xfer_poll = uhci_do_poll,