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 | CTLFLAG_TUN,
90 &uhcidebug, 0, "uhci debug level");
91 TUNABLE_INT("hw.usb.uhci.debug", &uhcidebug);
92 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
93 &uhcinoloop, 0, "uhci noloop");
94 TUNABLE_INT("hw.usb.uhci.loop", &uhcinoloop);
96 static void uhci_dumpregs(uhci_softc_t *sc);
97 static void uhci_dump_tds(uhci_td_t *td);
101 #define UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
102 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
103 #define UWRITE1(sc, r, x) \
104 do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
105 } while (/*CONSTCOND*/0)
106 #define UWRITE2(sc, r, x) \
107 do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
108 } while (/*CONSTCOND*/0)
109 #define UWRITE4(sc, r, x) \
110 do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
111 } while (/*CONSTCOND*/0)
112 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
113 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
114 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
116 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
117 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
119 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
121 #define UHCI_INTR_ENDPT 1
123 struct uhci_mem_layout {
125 struct usb_page_search buf_res;
126 struct usb_page_search fix_res;
128 struct usb_page_cache *buf_pc;
129 struct usb_page_cache *fix_pc;
133 uint16_t max_frame_size;
136 struct uhci_std_temp {
138 struct uhci_mem_layout ml;
145 uint16_t max_frame_size;
147 uint8_t setup_alt_next;
151 extern struct usb_bus_methods uhci_bus_methods;
152 extern struct usb_pipe_methods uhci_device_bulk_methods;
153 extern struct usb_pipe_methods uhci_device_ctrl_methods;
154 extern struct usb_pipe_methods uhci_device_intr_methods;
155 extern struct usb_pipe_methods uhci_device_isoc_methods;
157 static uint8_t uhci_restart(uhci_softc_t *sc);
158 static void uhci_do_poll(struct usb_bus *);
159 static void uhci_device_done(struct usb_xfer *, usb_error_t);
160 static void uhci_transfer_intr_enqueue(struct usb_xfer *);
161 static void uhci_timeout(void *);
162 static uint8_t uhci_check_transfer(struct usb_xfer *);
163 static void uhci_root_intr(uhci_softc_t *sc);
166 uhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
168 struct uhci_softc *sc = UHCI_BUS2SC(bus);
171 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
172 sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
174 cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
175 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
177 cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
178 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
180 cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
181 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
183 cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
184 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
186 cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
187 sizeof(uhci_td_t), UHCI_TD_ALIGN);
189 for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
190 cb(bus, sc->sc_hw.isoc_start_pc + i,
191 sc->sc_hw.isoc_start_pg + i,
192 sizeof(uhci_td_t), UHCI_TD_ALIGN);
195 for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
196 cb(bus, sc->sc_hw.intr_start_pc + i,
197 sc->sc_hw.intr_start_pg + i,
198 sizeof(uhci_qh_t), UHCI_QH_ALIGN);
203 uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb_xfer *xfer)
205 ml->buf_pc = xfer->frbuffers + 0;
206 ml->fix_pc = xfer->buf_fixup;
210 ml->max_frame_size = xfer->max_frame_size;
214 uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
216 usbd_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
218 if (ml->buf_res.length < td->len) {
220 /* need to do a fixup */
222 usbd_get_page(ml->fix_pc, 0, &ml->fix_res);
224 td->td_buffer = htole32(ml->fix_res.physaddr);
227 * The UHCI driver cannot handle
228 * page crossings, so a fixup is
241 if ((td->td_token & htole32(UHCI_TD_PID)) ==
242 htole32(UHCI_TD_PID_IN)) {
243 td->fix_pc = ml->fix_pc;
244 usb_pc_cpu_invalidate(ml->fix_pc);
249 /* copy data to fixup location */
251 usbd_copy_out(ml->buf_pc, ml->buf_offset,
252 ml->fix_res.buffer, td->len);
254 usb_pc_cpu_flush(ml->fix_pc);
257 /* prepare next fixup */
263 td->td_buffer = htole32(ml->buf_res.physaddr);
267 /* prepare next data location */
269 ml->buf_offset += td->len;
278 uhci_restart(uhci_softc_t *sc)
280 struct usb_page_search buf_res;
282 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
284 if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) {
285 DPRINTFN(2, "Already started\n");
289 DPRINTFN(2, "Restarting\n");
291 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
293 /* Reload fresh base address */
294 UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
297 * Assume 64 byte packets at frame end and start HC controller:
299 UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
301 /* wait 10 milliseconds */
303 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
305 /* check that controller has started */
307 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
308 DPRINTFN(2, "Failed\n");
315 uhci_reset(uhci_softc_t *sc)
319 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
321 DPRINTF("resetting the HC\n");
323 /* disable interrupts */
325 UWRITE2(sc, UHCI_INTR, 0);
329 UHCICMD(sc, UHCI_CMD_GRESET);
333 usb_pause_mtx(&sc->sc_bus.bus_mtx,
334 USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
336 /* terminate all transfers */
338 UHCICMD(sc, UHCI_CMD_HCRESET);
340 /* the reset bit goes low when the controller is done */
342 n = UHCI_RESET_TIMEOUT;
344 /* wait one millisecond */
346 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
348 if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
353 device_printf(sc->sc_bus.bdev,
354 "controller did not reset\n");
360 /* wait one millisecond */
362 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
364 /* check if HC is stopped */
365 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
370 device_printf(sc->sc_bus.bdev,
371 "controller did not stop\n");
375 /* reset frame number */
376 UWRITE2(sc, UHCI_FRNUM, 0);
377 /* set default SOF value */
378 UWRITE1(sc, UHCI_SOF, 0x40);
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);
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);
659 uhci_suspend(uhci_softc_t *sc)
667 USB_BUS_LOCK(&sc->sc_bus);
669 /* stop the controller */
673 /* enter global suspend */
675 UHCICMD(sc, UHCI_CMD_EGSM);
677 USB_BUS_UNLOCK(&sc->sc_bus);
681 uhci_resume(uhci_softc_t *sc)
683 USB_BUS_LOCK(&sc->sc_bus);
685 /* reset the controller */
689 /* force global resume */
691 UHCICMD(sc, UHCI_CMD_FGR);
693 /* and start traffic again */
697 USB_BUS_UNLOCK(&sc->sc_bus);
704 /* catch lost interrupts */
705 uhci_do_poll(&sc->sc_bus);
710 uhci_dumpregs(uhci_softc_t *sc)
712 DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
713 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
714 device_get_nameunit(sc->sc_bus.bdev),
715 UREAD2(sc, UHCI_CMD),
716 UREAD2(sc, UHCI_STS),
717 UREAD2(sc, UHCI_INTR),
718 UREAD2(sc, UHCI_FRNUM),
719 UREAD4(sc, UHCI_FLBASEADDR),
720 UREAD1(sc, UHCI_SOF),
721 UREAD2(sc, UHCI_PORTSC1),
722 UREAD2(sc, UHCI_PORTSC2));
726 uhci_dump_td(uhci_td_t *p)
733 usb_pc_cpu_invalidate(p->page_cache);
735 td_next = le32toh(p->td_next);
736 td_status = le32toh(p->td_status);
737 td_token = le32toh(p->td_token);
740 * Check whether the link pointer in this TD marks the link pointer
743 temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
745 printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
746 "token=0x%08x buffer=0x%08x\n",
752 le32toh(p->td_buffer));
754 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,"
755 "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
757 (td_next & 1) ? "-T" : "",
758 (td_next & 2) ? "-Q" : "",
759 (td_next & 4) ? "-VF" : "",
760 (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
761 (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
762 (td_status & UHCI_TD_NAK) ? "-NAK" : "",
763 (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
764 (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
765 (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
766 (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
767 (td_status & UHCI_TD_IOC) ? "-IOC" : "",
768 (td_status & UHCI_TD_IOS) ? "-IOS" : "",
769 (td_status & UHCI_TD_LS) ? "-LS" : "",
770 (td_status & UHCI_TD_SPD) ? "-SPD" : "",
771 UHCI_TD_GET_ERRCNT(td_status),
772 UHCI_TD_GET_ACTLEN(td_status),
773 UHCI_TD_GET_PID(td_token),
774 UHCI_TD_GET_DEVADDR(td_token),
775 UHCI_TD_GET_ENDPT(td_token),
776 UHCI_TD_GET_DT(td_token),
777 UHCI_TD_GET_MAXLEN(td_token));
783 uhci_dump_qh(uhci_qh_t *sqh)
789 usb_pc_cpu_invalidate(sqh->page_cache);
791 qh_h_next = le32toh(sqh->qh_h_next);
792 qh_e_next = le32toh(sqh->qh_e_next);
794 DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
795 le32toh(sqh->qh_self), qh_h_next, qh_e_next);
797 temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
798 (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
804 uhci_dump_all(uhci_softc_t *sc)
807 uhci_dump_qh(sc->sc_ls_ctl_p_last);
808 uhci_dump_qh(sc->sc_fs_ctl_p_last);
809 uhci_dump_qh(sc->sc_bulk_p_last);
810 uhci_dump_qh(sc->sc_last_qh_p);
814 uhci_dump_tds(uhci_td_t *td)
819 if (uhci_dump_td(td)) {
828 * Let the last QH loop back to the full speed control transfer QH.
829 * This is what intel calls "bandwidth reclamation" and improves
830 * USB performance a lot for some devices.
831 * If we are already looping, just count it.
834 uhci_add_loop(uhci_softc_t *sc)
836 struct uhci_qh *qh_lst;
837 struct uhci_qh *qh_rec;
844 if (++(sc->sc_loops) == 1) {
845 DPRINTFN(6, "add\n");
847 qh_lst = sc->sc_last_qh_p;
848 qh_rec = sc->sc_reclaim_qh_p;
850 /* NOTE: we don't loop back the soft pointer */
852 qh_lst->qh_h_next = qh_rec->qh_self;
853 usb_pc_cpu_flush(qh_lst->page_cache);
858 uhci_rem_loop(uhci_softc_t *sc)
860 struct uhci_qh *qh_lst;
867 if (--(sc->sc_loops) == 0) {
868 DPRINTFN(6, "remove\n");
870 qh_lst = sc->sc_last_qh_p;
871 qh_lst->qh_h_next = htole32(UHCI_PTR_T);
872 usb_pc_cpu_flush(qh_lst->page_cache);
877 uhci_transfer_intr_enqueue(struct usb_xfer *xfer)
879 /* check for early completion */
880 if (uhci_check_transfer(xfer)) {
883 /* put transfer on interrupt queue */
884 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
886 /* start timeout, if any */
887 if (xfer->timeout != 0) {
888 usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
892 #define UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
894 _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
896 DPRINTFN(11, "%p to %p\n", std, last);
898 /* (sc->sc_bus.mtx) must be locked */
900 std->next = last->next;
901 std->td_next = last->td_next;
905 usb_pc_cpu_flush(std->page_cache);
908 * the last->next->prev is never followed: std->next->prev = std;
911 last->td_next = std->td_self;
913 usb_pc_cpu_flush(last->page_cache);
918 #define UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
920 _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
922 DPRINTFN(11, "%p to %p\n", sqh, last);
924 if (sqh->h_prev != NULL) {
925 /* should not happen */
926 DPRINTFN(0, "QH already linked!\n");
929 /* (sc->sc_bus.mtx) must be locked */
931 sqh->h_next = last->h_next;
932 sqh->qh_h_next = last->qh_h_next;
936 usb_pc_cpu_flush(sqh->page_cache);
939 * The "last->h_next->h_prev" is never followed:
941 * "sqh->h_next->h_prev" = sqh;
945 last->qh_h_next = sqh->qh_self;
947 usb_pc_cpu_flush(last->page_cache);
954 #define UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
956 _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
958 DPRINTFN(11, "%p from %p\n", std, last);
960 /* (sc->sc_bus.mtx) must be locked */
962 std->prev->next = std->next;
963 std->prev->td_next = std->td_next;
965 usb_pc_cpu_flush(std->prev->page_cache);
968 std->next->prev = std->prev;
969 usb_pc_cpu_flush(std->next->page_cache);
971 return ((last == std) ? std->prev : last);
974 #define UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
976 _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
978 DPRINTFN(11, "%p from %p\n", sqh, last);
980 /* (sc->sc_bus.mtx) must be locked */
982 /* only remove if not removed from a queue */
985 sqh->h_prev->h_next = sqh->h_next;
986 sqh->h_prev->qh_h_next = sqh->qh_h_next;
988 usb_pc_cpu_flush(sqh->h_prev->page_cache);
991 sqh->h_next->h_prev = sqh->h_prev;
992 usb_pc_cpu_flush(sqh->h_next->page_cache);
994 last = ((last == sqh) ? sqh->h_prev : last);
998 usb_pc_cpu_flush(sqh->page_cache);
1004 uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer)
1006 struct usb_page_search res;
1007 uint32_t nframes = xfer->nframes;
1009 uint32_t offset = 0;
1010 uint32_t *plen = xfer->frlengths;
1012 uhci_td_t *td = xfer->td_transfer_first;
1013 uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
1015 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1016 xfer, xfer->endpoint);
1018 /* sync any DMA memory before doing fixups */
1020 usb_bdma_post_sync(xfer);
1024 panic("%s:%d: out of TD's\n",
1025 __FUNCTION__, __LINE__);
1027 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
1028 pp_last = &sc->sc_isoc_p_last[0];
1031 if (uhcidebug > 5) {
1032 DPRINTF("isoc TD\n");
1036 usb_pc_cpu_invalidate(td->page_cache);
1037 status = le32toh(td->td_status);
1039 len = UHCI_TD_GET_ACTLEN(status);
1046 usbd_get_page(td->fix_pc, 0, &res);
1048 /* copy data from fixup location to real location */
1050 usb_pc_cpu_invalidate(td->fix_pc);
1052 usbd_copy_in(xfer->frbuffers, offset,
1059 /* remove TD from schedule */
1060 UHCI_REMOVE_TD(td, *pp_last);
1067 xfer->aframes = xfer->nframes;
1071 uhci_non_isoc_done_sub(struct usb_xfer *xfer)
1073 struct usb_page_search res;
1075 uhci_td_t *td_alt_next;
1080 td = xfer->td_transfer_cache;
1081 td_alt_next = td->alt_next;
1083 if (xfer->aframes != xfer->nframes) {
1084 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
1088 usb_pc_cpu_invalidate(td->page_cache);
1089 status = le32toh(td->td_status);
1090 token = le32toh(td->td_token);
1093 * Verify the status and add
1094 * up the actual length:
1097 len = UHCI_TD_GET_ACTLEN(status);
1098 if (len > td->len) {
1099 /* should not happen */
1100 DPRINTF("Invalid status length, "
1101 "0x%04x/0x%04x bytes\n", len, td->len);
1102 status |= UHCI_TD_STALLED;
1104 } else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
1108 usbd_get_page(td->fix_pc, 0, &res);
1111 * copy data from fixup location to real
1115 usb_pc_cpu_invalidate(td->fix_pc);
1117 usbd_copy_in(xfer->frbuffers + xfer->aframes,
1118 xfer->frlengths[xfer->aframes], res.buffer, len);
1120 /* update actual length */
1122 xfer->frlengths[xfer->aframes] += len;
1124 /* Check for last transfer */
1125 if (((void *)td) == xfer->td_transfer_last) {
1129 if (status & UHCI_TD_STALLED) {
1130 /* the transfer is finished */
1134 /* Check for short transfer */
1135 if (len != td->len) {
1136 if (xfer->flags_int.short_frames_ok) {
1137 /* follow alt next */
1140 /* the transfer is finished */
1147 if (td->alt_next != td_alt_next) {
1148 /* this USB frame is complete */
1153 /* update transfer cache */
1155 xfer->td_transfer_cache = td;
1157 /* update data toggle */
1159 xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
1162 if (status & UHCI_TD_ERROR) {
1163 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
1164 "status=%s%s%s%s%s%s%s%s%s%s%s\n",
1165 xfer->address, xfer->endpointno, xfer->aframes,
1166 (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
1167 (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
1168 (status & UHCI_TD_NAK) ? "[NAK]" : "",
1169 (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
1170 (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
1171 (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
1172 (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1173 (status & UHCI_TD_IOC) ? "[IOC]" : "",
1174 (status & UHCI_TD_IOS) ? "[IOS]" : "",
1175 (status & UHCI_TD_LS) ? "[LS]" : "",
1176 (status & UHCI_TD_SPD) ? "[SPD]" : "");
1179 if (status & UHCI_TD_STALLED) {
1180 /* try to separate I/O errors from STALL */
1181 if (UHCI_TD_GET_ERRCNT(status) == 0)
1182 return (USB_ERR_IOERROR);
1183 return (USB_ERR_STALLED);
1185 return (USB_ERR_NORMAL_COMPLETION);
1189 uhci_non_isoc_done(struct usb_xfer *xfer)
1191 usb_error_t err = 0;
1193 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1194 xfer, xfer->endpoint);
1197 if (uhcidebug > 10) {
1198 uhci_dump_tds(xfer->td_transfer_first);
1202 /* sync any DMA memory before doing fixups */
1204 usb_bdma_post_sync(xfer);
1208 xfer->td_transfer_cache = xfer->td_transfer_first;
1210 if (xfer->flags_int.control_xfr) {
1211 if (xfer->flags_int.control_hdr) {
1213 err = uhci_non_isoc_done_sub(xfer);
1217 if (xfer->td_transfer_cache == NULL) {
1221 while (xfer->aframes != xfer->nframes) {
1223 err = uhci_non_isoc_done_sub(xfer);
1226 if (xfer->td_transfer_cache == NULL) {
1231 if (xfer->flags_int.control_xfr &&
1232 !xfer->flags_int.control_act) {
1234 err = uhci_non_isoc_done_sub(xfer);
1237 uhci_device_done(xfer, err);
1240 /*------------------------------------------------------------------------*
1241 * uhci_check_transfer_sub
1243 * The main purpose of this function is to update the data-toggle
1244 * in case it is wrong.
1245 *------------------------------------------------------------------------*/
1247 uhci_check_transfer_sub(struct usb_xfer *xfer)
1251 uhci_td_t *td_alt_next;
1256 td = xfer->td_transfer_cache;
1257 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1259 td_token = td->obj_next->td_token;
1261 xfer->td_transfer_cache = td;
1262 td_self = td->td_self;
1263 td_alt_next = td->alt_next;
1265 if (xfer->flags_int.control_xfr)
1266 goto skip; /* don't touch the DT value! */
1268 if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))))
1269 goto skip; /* data toggle has correct value */
1272 * The data toggle is wrong and we need to toggle it !
1276 td->td_token ^= htole32(UHCI_TD_SET_DT(1));
1277 usb_pc_cpu_flush(td->page_cache);
1279 if (td == xfer->td_transfer_last) {
1285 if (td->alt_next != td_alt_next) {
1293 qh->qh_e_next = td_self;
1294 usb_pc_cpu_flush(qh->page_cache);
1296 DPRINTFN(13, "xfer=%p following alt next\n", xfer);
1299 /*------------------------------------------------------------------------*
1300 * uhci_check_transfer
1303 * 0: USB transfer is not finished
1304 * Else: USB transfer is finished
1305 *------------------------------------------------------------------------*/
1307 uhci_check_transfer(struct usb_xfer *xfer)
1313 DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
1315 if (xfer->endpoint->methods == &uhci_device_isoc_methods) {
1316 /* isochronous transfer */
1318 td = xfer->td_transfer_last;
1320 usb_pc_cpu_invalidate(td->page_cache);
1321 status = le32toh(td->td_status);
1323 /* check also if the first is complete */
1325 td = xfer->td_transfer_first;
1327 usb_pc_cpu_invalidate(td->page_cache);
1328 status |= le32toh(td->td_status);
1330 if (!(status & UHCI_TD_ACTIVE)) {
1331 uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1335 /* non-isochronous transfer */
1338 * check whether there is an error somewhere
1339 * in the middle, or whether there was a short
1340 * packet (SPD and not ACTIVE)
1342 td = xfer->td_transfer_cache;
1345 usb_pc_cpu_invalidate(td->page_cache);
1346 status = le32toh(td->td_status);
1347 token = le32toh(td->td_token);
1350 * if there is an active TD the transfer isn't done
1352 if (status & UHCI_TD_ACTIVE) {
1354 xfer->td_transfer_cache = td;
1358 * last transfer descriptor makes the transfer done
1360 if (((void *)td) == xfer->td_transfer_last) {
1364 * any kind of error makes the transfer done
1366 if (status & UHCI_TD_STALLED) {
1370 * check if we reached the last packet
1371 * or if there is a short packet:
1373 if ((td->td_next == htole32(UHCI_PTR_T)) ||
1374 (UHCI_TD_GET_ACTLEN(status) < td->len)) {
1376 if (xfer->flags_int.short_frames_ok) {
1377 /* follow alt next */
1380 xfer->td_transfer_cache = td;
1381 uhci_check_transfer_sub(xfer);
1385 /* transfer is done */
1390 uhci_non_isoc_done(xfer);
1395 DPRINTFN(13, "xfer=%p is still active\n", xfer);
1403 uhci_interrupt_poll(uhci_softc_t *sc)
1405 struct usb_xfer *xfer;
1408 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1410 * check if transfer is transferred
1412 if (uhci_check_transfer(xfer)) {
1413 /* queue has been modified */
1419 /*------------------------------------------------------------------------*
1420 * uhci_interrupt - UHCI interrupt handler
1422 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1423 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1425 *------------------------------------------------------------------------*/
1427 uhci_interrupt(uhci_softc_t *sc)
1431 USB_BUS_LOCK(&sc->sc_bus);
1433 DPRINTFN(16, "real interrupt\n");
1436 if (uhcidebug > 15) {
1440 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1442 /* the interrupt was not for us */
1445 if (status & (UHCI_STS_RD | UHCI_STS_HSE |
1446 UHCI_STS_HCPE | UHCI_STS_HCH)) {
1448 if (status & UHCI_STS_RD) {
1450 printf("%s: resume detect\n",
1454 if (status & UHCI_STS_HSE) {
1455 printf("%s: host system error\n",
1458 if (status & UHCI_STS_HCPE) {
1459 printf("%s: host controller process error\n",
1462 if (status & UHCI_STS_HCH) {
1463 /* no acknowledge needed */
1464 DPRINTF("%s: host controller halted\n",
1467 if (uhcidebug > 0) {
1473 /* get acknowledge bits */
1474 status &= (UHCI_STS_USBINT |
1481 /* nothing to acknowledge */
1484 /* acknowledge interrupts */
1485 UWRITE2(sc, UHCI_STS, status);
1487 /* poll all the USB transfers */
1488 uhci_interrupt_poll(sc);
1491 USB_BUS_UNLOCK(&sc->sc_bus);
1495 * called when a request does not complete
1498 uhci_timeout(void *arg)
1500 struct usb_xfer *xfer = arg;
1502 DPRINTF("xfer=%p\n", xfer);
1504 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1506 /* transfer is transferred */
1507 uhci_device_done(xfer, USB_ERR_TIMEOUT);
1511 uhci_do_poll(struct usb_bus *bus)
1513 struct uhci_softc *sc = UHCI_BUS2SC(bus);
1515 USB_BUS_LOCK(&sc->sc_bus);
1516 uhci_interrupt_poll(sc);
1517 USB_BUS_UNLOCK(&sc->sc_bus);
1521 uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
1525 uhci_td_t *td_alt_next;
1528 uint8_t shortpkt_old;
1532 shortpkt_old = temp->shortpkt;
1533 len_old = temp->len;
1536 /* software is used to detect short incoming transfers */
1538 if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
1539 temp->td_status |= htole32(UHCI_TD_SPD);
1541 temp->td_status &= ~htole32(UHCI_TD_SPD);
1544 temp->ml.buf_offset = 0;
1548 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1549 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
1552 td_next = temp->td_next;
1556 if (temp->len == 0) {
1558 if (temp->shortpkt) {
1561 /* send a Zero Length Packet, ZLP, last */
1564 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
1569 average = temp->average;
1571 if (temp->len < average) {
1573 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1574 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
1575 average = temp->len;
1579 if (td_next == NULL) {
1580 panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
1585 td_next = td->obj_next;
1587 /* check if we are pre-computing */
1591 /* update remaining length */
1593 temp->len -= average;
1597 /* fill out current TD */
1599 td->td_status = temp->td_status;
1600 td->td_token = temp->td_token;
1602 /* update data toggle */
1604 temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
1614 /* update remaining length */
1616 temp->len -= average;
1620 /* fill out buffer pointer and do fixup, if any */
1622 uhci_mem_layout_fixup(&temp->ml, td);
1625 td->alt_next = td_alt_next;
1627 if ((td_next == td_alt_next) && temp->setup_alt_next) {
1628 /* we need to receive these frames one by one ! */
1629 td->td_status |= htole32(UHCI_TD_IOC);
1630 td->td_next = htole32(UHCI_PTR_T);
1633 /* link the current TD with the next one */
1634 td->td_next = td_next->td_self;
1638 usb_pc_cpu_flush(td->page_cache);
1644 /* setup alt next pointer, if any */
1645 if (temp->last_frame) {
1648 /* we use this field internally */
1649 td_alt_next = td_next;
1653 temp->shortpkt = shortpkt_old;
1654 temp->len = len_old;
1658 temp->td_next = td_next;
1662 uhci_setup_standard_chain(struct usb_xfer *xfer)
1664 struct uhci_std_temp temp;
1668 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1669 xfer->address, UE_GET_ADDR(xfer->endpointno),
1670 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1672 temp.average = xfer->max_frame_size;
1673 temp.max_frame_size = xfer->max_frame_size;
1675 /* toggle the DMA set we are using */
1676 xfer->flags_int.curr_dma_set ^= 1;
1678 /* get next DMA set */
1679 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1680 xfer->td_transfer_first = td;
1681 xfer->td_transfer_cache = td;
1685 temp.last_frame = 0;
1686 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1688 uhci_mem_layout_init(&temp.ml, xfer);
1691 htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
1694 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1695 temp.td_status |= htole32(UHCI_TD_LS);
1698 htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) |
1699 UHCI_TD_SET_DEVADDR(xfer->address));
1701 if (xfer->endpoint->toggle_next) {
1703 temp.td_token |= htole32(UHCI_TD_SET_DT(1));
1705 /* check if we should prepend a setup message */
1707 if (xfer->flags_int.control_xfr) {
1709 if (xfer->flags_int.control_hdr) {
1711 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1712 UHCI_TD_SET_ENDPT(0xF));
1713 temp.td_token |= htole32(UHCI_TD_PID_SETUP |
1716 temp.len = xfer->frlengths[0];
1717 temp.ml.buf_pc = xfer->frbuffers + 0;
1718 temp.shortpkt = temp.len ? 1 : 0;
1719 /* check for last frame */
1720 if (xfer->nframes == 1) {
1721 /* no STATUS stage yet, SETUP is last */
1722 if (xfer->flags_int.control_act) {
1723 temp.last_frame = 1;
1724 temp.setup_alt_next = 0;
1727 uhci_setup_standard_chain_sub(&temp);
1734 while (x != xfer->nframes) {
1736 /* DATA0 / DATA1 message */
1738 temp.len = xfer->frlengths[x];
1739 temp.ml.buf_pc = xfer->frbuffers + x;
1743 if (x == xfer->nframes) {
1744 if (xfer->flags_int.control_xfr) {
1745 /* no STATUS stage yet, DATA is last */
1746 if (xfer->flags_int.control_act) {
1747 temp.last_frame = 1;
1748 temp.setup_alt_next = 0;
1751 temp.last_frame = 1;
1752 temp.setup_alt_next = 0;
1756 * Keep previous data toggle,
1757 * device address and endpoint number:
1760 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1761 UHCI_TD_SET_ENDPT(0xF) |
1764 if (temp.len == 0) {
1766 /* make sure that we send an USB packet */
1772 /* regular data transfer */
1774 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1777 /* set endpoint direction */
1780 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1781 htole32(UHCI_TD_PID_IN) :
1782 htole32(UHCI_TD_PID_OUT);
1784 uhci_setup_standard_chain_sub(&temp);
1787 /* check if we should append a status stage */
1789 if (xfer->flags_int.control_xfr &&
1790 !xfer->flags_int.control_act) {
1793 * send a DATA1 message and reverse the current endpoint
1797 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1798 UHCI_TD_SET_ENDPT(0xF) |
1801 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1802 htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
1803 htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
1806 temp.ml.buf_pc = NULL;
1808 temp.last_frame = 1;
1809 temp.setup_alt_next = 0;
1811 uhci_setup_standard_chain_sub(&temp);
1815 /* Ensure that last TD is terminating: */
1816 td->td_next = htole32(UHCI_PTR_T);
1818 /* set interrupt bit */
1820 td->td_status |= htole32(UHCI_TD_IOC);
1822 usb_pc_cpu_flush(td->page_cache);
1824 /* must have at least one frame! */
1826 xfer->td_transfer_last = td;
1829 if (uhcidebug > 8) {
1830 DPRINTF("nexttog=%d; data before transfer:\n",
1831 xfer->endpoint->toggle_next);
1832 uhci_dump_tds(xfer->td_transfer_first);
1835 return (xfer->td_transfer_first);
1838 /* NOTE: "done" can be run two times in a row,
1839 * from close and from interrupt
1843 uhci_device_done(struct usb_xfer *xfer, usb_error_t error)
1845 struct usb_pipe_methods *methods = xfer->endpoint->methods;
1846 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1849 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1851 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1852 xfer, xfer->endpoint, error);
1854 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1856 usb_pc_cpu_invalidate(qh->page_cache);
1858 if (xfer->flags_int.bandwidth_reclaimed) {
1859 xfer->flags_int.bandwidth_reclaimed = 0;
1862 if (methods == &uhci_device_bulk_methods) {
1863 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
1865 if (methods == &uhci_device_ctrl_methods) {
1866 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1867 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
1869 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
1872 if (methods == &uhci_device_intr_methods) {
1873 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
1876 * Only finish isochronous transfers once
1877 * which will update "xfer->frlengths".
1879 if (xfer->td_transfer_first &&
1880 xfer->td_transfer_last) {
1881 if (methods == &uhci_device_isoc_methods) {
1882 uhci_isoc_done(sc, xfer);
1884 xfer->td_transfer_first = NULL;
1885 xfer->td_transfer_last = NULL;
1887 /* dequeue transfer and start next transfer */
1888 usbd_transfer_done(xfer, error);
1891 /*------------------------------------------------------------------------*
1893 *------------------------------------------------------------------------*/
1895 uhci_device_bulk_open(struct usb_xfer *xfer)
1901 uhci_device_bulk_close(struct usb_xfer *xfer)
1903 uhci_device_done(xfer, USB_ERR_CANCELLED);
1907 uhci_device_bulk_enter(struct usb_xfer *xfer)
1913 uhci_device_bulk_start(struct usb_xfer *xfer)
1915 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1920 td = uhci_setup_standard_chain(xfer);
1923 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1926 qh->qh_e_next = td->td_self;
1928 if (xfer->xroot->udev->flags.self_suspended == 0) {
1929 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
1931 xfer->flags_int.bandwidth_reclaimed = 1;
1933 usb_pc_cpu_flush(qh->page_cache);
1936 /* put transfer on interrupt queue */
1937 uhci_transfer_intr_enqueue(xfer);
1940 struct usb_pipe_methods uhci_device_bulk_methods =
1942 .open = uhci_device_bulk_open,
1943 .close = uhci_device_bulk_close,
1944 .enter = uhci_device_bulk_enter,
1945 .start = uhci_device_bulk_start,
1948 /*------------------------------------------------------------------------*
1949 * uhci control support
1950 *------------------------------------------------------------------------*/
1952 uhci_device_ctrl_open(struct usb_xfer *xfer)
1958 uhci_device_ctrl_close(struct usb_xfer *xfer)
1960 uhci_device_done(xfer, USB_ERR_CANCELLED);
1964 uhci_device_ctrl_enter(struct usb_xfer *xfer)
1970 uhci_device_ctrl_start(struct usb_xfer *xfer)
1972 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1977 td = uhci_setup_standard_chain(xfer);
1980 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1983 qh->qh_e_next = td->td_self;
1986 * NOTE: some devices choke on bandwidth- reclamation for control
1989 if (xfer->xroot->udev->flags.self_suspended == 0) {
1990 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1991 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
1993 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
1996 usb_pc_cpu_flush(qh->page_cache);
1998 /* put transfer on interrupt queue */
1999 uhci_transfer_intr_enqueue(xfer);
2002 struct usb_pipe_methods uhci_device_ctrl_methods =
2004 .open = uhci_device_ctrl_open,
2005 .close = uhci_device_ctrl_close,
2006 .enter = uhci_device_ctrl_enter,
2007 .start = uhci_device_ctrl_start,
2010 /*------------------------------------------------------------------------*
2011 * uhci interrupt support
2012 *------------------------------------------------------------------------*/
2014 uhci_device_intr_open(struct usb_xfer *xfer)
2016 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2022 bit = UHCI_IFRAMELIST_COUNT / 2;
2024 if (xfer->interval >= bit) {
2028 if (sc->sc_intr_stat[x] <
2029 sc->sc_intr_stat[best]) {
2039 sc->sc_intr_stat[best]++;
2040 xfer->qh_pos = best;
2042 DPRINTFN(3, "best=%d interval=%d\n",
2043 best, xfer->interval);
2047 uhci_device_intr_close(struct usb_xfer *xfer)
2049 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2051 sc->sc_intr_stat[xfer->qh_pos]--;
2053 uhci_device_done(xfer, USB_ERR_CANCELLED);
2057 uhci_device_intr_enter(struct usb_xfer *xfer)
2063 uhci_device_intr_start(struct usb_xfer *xfer)
2065 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2070 td = uhci_setup_standard_chain(xfer);
2073 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2076 qh->qh_e_next = td->td_self;
2078 if (xfer->xroot->udev->flags.self_suspended == 0) {
2079 /* enter QHs into the controller data structures */
2080 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
2082 usb_pc_cpu_flush(qh->page_cache);
2085 /* put transfer on interrupt queue */
2086 uhci_transfer_intr_enqueue(xfer);
2089 struct usb_pipe_methods uhci_device_intr_methods =
2091 .open = uhci_device_intr_open,
2092 .close = uhci_device_intr_close,
2093 .enter = uhci_device_intr_enter,
2094 .start = uhci_device_intr_start,
2097 /*------------------------------------------------------------------------*
2098 * uhci isochronous support
2099 *------------------------------------------------------------------------*/
2101 uhci_device_isoc_open(struct usb_xfer *xfer)
2108 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
2109 UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) :
2110 UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0);
2112 td_token = htole32(td_token);
2114 /* initialize all TD's */
2116 for (ds = 0; ds != 2; ds++) {
2118 for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2120 /* mark TD as inactive */
2121 td->td_status = htole32(UHCI_TD_IOS);
2122 td->td_token = td_token;
2124 usb_pc_cpu_flush(td->page_cache);
2130 uhci_device_isoc_close(struct usb_xfer *xfer)
2132 uhci_device_done(xfer, USB_ERR_CANCELLED);
2136 uhci_device_isoc_enter(struct usb_xfer *xfer)
2138 struct uhci_mem_layout ml;
2139 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2149 uhci_td_t *td_last = NULL;
2150 uhci_td_t **pp_last;
2152 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2153 xfer, xfer->endpoint->isoc_next, xfer->nframes);
2155 nframes = UREAD2(sc, UHCI_FRNUM);
2157 temp = (nframes - xfer->endpoint->isoc_next) &
2158 (UHCI_VFRAMELIST_COUNT - 1);
2160 if ((xfer->endpoint->is_synced == 0) ||
2161 (temp < xfer->nframes)) {
2163 * If there is data underflow or the pipe queue is empty we
2164 * schedule the transfer a few frames ahead of the current
2165 * frame position. Else two isochronous transfers might
2168 xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
2169 xfer->endpoint->is_synced = 1;
2170 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2173 * compute how many milliseconds the insertion is ahead of the
2174 * current frame position:
2176 temp = (xfer->endpoint->isoc_next - nframes) &
2177 (UHCI_VFRAMELIST_COUNT - 1);
2180 * pre-compute when the isochronous transfer will be finished:
2182 xfer->isoc_time_complete =
2183 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2186 /* get the real number of frames */
2188 nframes = xfer->nframes;
2190 uhci_mem_layout_init(&ml, xfer);
2192 plen = xfer->frlengths;
2194 /* toggle the DMA set we are using */
2195 xfer->flags_int.curr_dma_set ^= 1;
2197 /* get next DMA set */
2198 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2199 xfer->td_transfer_first = td;
2201 pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next];
2203 /* store starting position */
2205 xfer->qh_pos = xfer->endpoint->isoc_next;
2209 panic("%s:%d: out of TD's\n",
2210 __FUNCTION__, __LINE__);
2212 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
2213 pp_last = &sc->sc_isoc_p_last[0];
2215 if (*plen > xfer->max_frame_size) {
2219 printf("%s: frame length(%d) exceeds %d "
2220 "bytes (frame truncated)\n",
2221 __FUNCTION__, *plen,
2222 xfer->max_frame_size);
2225 *plen = xfer->max_frame_size;
2227 /* reuse td_token from last transfer */
2229 td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2230 td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
2236 * Do not call "uhci_mem_layout_fixup()" when the
2244 /* fill out buffer pointer and do fixup, if any */
2246 uhci_mem_layout_fixup(&ml, td);
2252 td->td_status = htole32
2253 (UHCI_TD_ZERO_ACTLEN
2254 (UHCI_TD_SET_ERRCNT(0) |
2259 td->td_status = htole32
2260 (UHCI_TD_ZERO_ACTLEN
2261 (UHCI_TD_SET_ERRCNT(0) |
2266 usb_pc_cpu_flush(td->page_cache);
2269 if (uhcidebug > 5) {
2270 DPRINTF("TD %d\n", nframes);
2274 /* insert TD into schedule */
2275 UHCI_APPEND_TD(td, *pp_last);
2283 xfer->td_transfer_last = td_last;
2285 /* update isoc_next */
2286 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
2287 (UHCI_VFRAMELIST_COUNT - 1);
2291 uhci_device_isoc_start(struct usb_xfer *xfer)
2293 /* put transfer on interrupt queue */
2294 uhci_transfer_intr_enqueue(xfer);
2297 struct usb_pipe_methods uhci_device_isoc_methods =
2299 .open = uhci_device_isoc_open,
2300 .close = uhci_device_isoc_close,
2301 .enter = uhci_device_isoc_enter,
2302 .start = uhci_device_isoc_start,
2305 /*------------------------------------------------------------------------*
2306 * uhci root control support
2307 *------------------------------------------------------------------------*
2308 * Simulate a hardware hub by handling all the necessary requests.
2309 *------------------------------------------------------------------------*/
2312 struct usb_device_descriptor uhci_devd =
2314 sizeof(struct usb_device_descriptor),
2315 UDESC_DEVICE, /* type */
2316 {0x00, 0x01}, /* USB version */
2317 UDCLASS_HUB, /* class */
2318 UDSUBCLASS_HUB, /* subclass */
2319 UDPROTO_FSHUB, /* protocol */
2320 64, /* max packet */
2321 {0}, {0}, {0x00, 0x01}, /* device id */
2322 1, 2, 0, /* string indicies */
2323 1 /* # of configurations */
2326 static const struct uhci_config_desc uhci_confd = {
2328 .bLength = sizeof(struct usb_config_descriptor),
2329 .bDescriptorType = UDESC_CONFIG,
2330 .wTotalLength[0] = sizeof(uhci_confd),
2332 .bConfigurationValue = 1,
2333 .iConfiguration = 0,
2334 .bmAttributes = UC_SELF_POWERED,
2335 .bMaxPower = 0 /* max power */
2338 .bLength = sizeof(struct usb_interface_descriptor),
2339 .bDescriptorType = UDESC_INTERFACE,
2341 .bInterfaceClass = UICLASS_HUB,
2342 .bInterfaceSubClass = UISUBCLASS_HUB,
2343 .bInterfaceProtocol = UIPROTO_FSHUB,
2346 .bLength = sizeof(struct usb_endpoint_descriptor),
2347 .bDescriptorType = UDESC_ENDPOINT,
2348 .bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
2349 .bmAttributes = UE_INTERRUPT,
2350 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */
2356 struct usb_hub_descriptor_min uhci_hubd_piix =
2358 .bDescLength = sizeof(uhci_hubd_piix),
2359 .bDescriptorType = UDESC_HUB,
2361 .wHubCharacteristics = {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
2362 .bPwrOn2PwrGood = 50,
2366 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2367 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2368 * should not be used by the USB subsystem. As we cannot issue a
2369 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2370 * will be enabled as part of the reset.
2372 * On the VT83C572, the port cannot be successfully enabled until the
2373 * outstanding "port enable change" and "connection status change"
2374 * events have been reset.
2377 uhci_portreset(uhci_softc_t *sc, uint16_t index)
2384 port = UHCI_PORTSC1;
2385 else if (index == 2)
2386 port = UHCI_PORTSC2;
2388 return (USB_ERR_IOERROR);
2391 * Before we do anything, turn on SOF messages on the USB
2392 * BUS. Some USB devices do not cope without them!
2396 x = URWMASK(UREAD2(sc, port));
2397 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2399 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2400 USB_MS_TO_TICKS(usb_port_root_reset_delay));
2402 DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
2403 index, UREAD2(sc, port));
2405 x = URWMASK(UREAD2(sc, port));
2406 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2409 mtx_unlock(&sc->sc_bus.bus_mtx);
2412 * This delay needs to be exactly 100us, else some USB devices
2417 mtx_lock(&sc->sc_bus.bus_mtx);
2419 DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
2420 index, UREAD2(sc, port));
2422 x = URWMASK(UREAD2(sc, port));
2423 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2425 for (lim = 0; lim < 12; lim++) {
2427 usb_pause_mtx(&sc->sc_bus.bus_mtx,
2428 USB_MS_TO_TICKS(usb_port_reset_delay));
2430 x = UREAD2(sc, port);
2432 DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
2435 if (!(x & UHCI_PORTSC_CCS)) {
2437 * No device is connected (or was disconnected
2438 * during reset). Consider the port reset.
2439 * The delay must be long enough to ensure on
2440 * the initial iteration that the device
2441 * connection will have been registered. 50ms
2442 * appears to be sufficient, but 20ms is not.
2444 DPRINTFN(4, "uhci port %d loop %u, device detached\n",
2448 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
2450 * Port enabled changed and/or connection
2451 * status changed were set. Reset either or
2452 * both raised flags (by writing a 1 to that
2453 * bit), and wait again for state to settle.
2455 UWRITE2(sc, port, URWMASK(x) |
2456 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
2459 if (x & UHCI_PORTSC_PE) {
2460 /* port is enabled */
2463 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
2466 DPRINTFN(2, "uhci port %d reset timed out\n", index);
2467 return (USB_ERR_TIMEOUT);
2470 DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
2471 index, UREAD2(sc, port));
2474 return (USB_ERR_NORMAL_COMPLETION);
2478 uhci_roothub_exec(struct usb_device *udev,
2479 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2481 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
2483 const char *str_ptr;
2493 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2496 ptr = (const void *)&sc->sc_hub_desc.temp;
2500 value = UGETW(req->wValue);
2501 index = UGETW(req->wIndex);
2503 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2504 "wValue=0x%04x wIndex=0x%04x\n",
2505 req->bmRequestType, req->bRequest,
2506 UGETW(req->wLength), value, index);
2508 #define C(x,y) ((x) | ((y) << 8))
2509 switch (C(req->bRequest, req->bmRequestType)) {
2510 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2511 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2512 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2514 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2515 * for the integrated root hub.
2518 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2520 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2522 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2523 switch (value >> 8) {
2525 if ((value & 0xff) != 0) {
2526 err = USB_ERR_IOERROR;
2529 len = sizeof(uhci_devd);
2530 ptr = (const void *)&uhci_devd;
2534 if ((value & 0xff) != 0) {
2535 err = USB_ERR_IOERROR;
2538 len = sizeof(uhci_confd);
2539 ptr = (const void *)&uhci_confd;
2543 switch (value & 0xff) {
2544 case 0: /* Language table */
2548 case 1: /* Vendor */
2549 str_ptr = sc->sc_vendor;
2552 case 2: /* Product */
2553 str_ptr = "UHCI root HUB";
2561 len = usb_make_str_desc
2562 (sc->sc_hub_desc.temp,
2563 sizeof(sc->sc_hub_desc.temp),
2568 err = USB_ERR_IOERROR;
2572 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2574 sc->sc_hub_desc.temp[0] = 0;
2576 case C(UR_GET_STATUS, UT_READ_DEVICE):
2578 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
2580 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2581 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2583 USETW(sc->sc_hub_desc.stat.wStatus, 0);
2585 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2586 if (value >= UHCI_MAX_DEVICES) {
2587 err = USB_ERR_IOERROR;
2590 sc->sc_addr = value;
2592 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2593 if ((value != 0) && (value != 1)) {
2594 err = USB_ERR_IOERROR;
2597 sc->sc_conf = value;
2599 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2601 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2602 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2603 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2604 err = USB_ERR_IOERROR;
2606 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2608 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2611 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2613 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2614 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
2615 "port=%d feature=%d\n",
2618 port = UHCI_PORTSC1;
2619 else if (index == 2)
2620 port = UHCI_PORTSC2;
2622 err = USB_ERR_IOERROR;
2626 case UHF_PORT_ENABLE:
2627 x = URWMASK(UREAD2(sc, port));
2628 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2630 case UHF_PORT_SUSPEND:
2631 x = URWMASK(UREAD2(sc, port));
2632 UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
2634 case UHF_PORT_RESET:
2635 x = URWMASK(UREAD2(sc, port));
2636 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2638 case UHF_C_PORT_CONNECTION:
2639 x = URWMASK(UREAD2(sc, port));
2640 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2642 case UHF_C_PORT_ENABLE:
2643 x = URWMASK(UREAD2(sc, port));
2644 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2646 case UHF_C_PORT_OVER_CURRENT:
2647 x = URWMASK(UREAD2(sc, port));
2648 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2650 case UHF_C_PORT_RESET:
2652 err = USB_ERR_NORMAL_COMPLETION;
2654 case UHF_C_PORT_SUSPEND:
2655 sc->sc_isresumed &= ~(1 << index);
2657 case UHF_PORT_CONNECTION:
2658 case UHF_PORT_OVER_CURRENT:
2659 case UHF_PORT_POWER:
2660 case UHF_PORT_LOW_SPEED:
2662 err = USB_ERR_IOERROR;
2666 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2668 port = UHCI_PORTSC1;
2669 else if (index == 2)
2670 port = UHCI_PORTSC2;
2672 err = USB_ERR_IOERROR;
2676 sc->sc_hub_desc.temp[0] =
2677 ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2678 UHCI_PORTSC_LS_SHIFT);
2680 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2681 if ((value & 0xff) != 0) {
2682 err = USB_ERR_IOERROR;
2685 len = sizeof(uhci_hubd_piix);
2686 ptr = (const void *)&uhci_hubd_piix;
2688 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2690 memset(sc->sc_hub_desc.temp, 0, 16);
2692 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2694 port = UHCI_PORTSC1;
2695 else if (index == 2)
2696 port = UHCI_PORTSC2;
2698 err = USB_ERR_IOERROR;
2701 x = UREAD2(sc, port);
2702 status = change = 0;
2703 if (x & UHCI_PORTSC_CCS)
2704 status |= UPS_CURRENT_CONNECT_STATUS;
2705 if (x & UHCI_PORTSC_CSC)
2706 change |= UPS_C_CONNECT_STATUS;
2707 if (x & UHCI_PORTSC_PE)
2708 status |= UPS_PORT_ENABLED;
2709 if (x & UHCI_PORTSC_POEDC)
2710 change |= UPS_C_PORT_ENABLED;
2711 if (x & UHCI_PORTSC_OCI)
2712 status |= UPS_OVERCURRENT_INDICATOR;
2713 if (x & UHCI_PORTSC_OCIC)
2714 change |= UPS_C_OVERCURRENT_INDICATOR;
2715 if (x & UHCI_PORTSC_LSDA)
2716 status |= UPS_LOW_SPEED;
2717 if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
2718 /* need to do a write back */
2719 UWRITE2(sc, port, URWMASK(x));
2721 /* wait 20ms for resume sequence to complete */
2722 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
2724 /* clear suspend and resume detect */
2725 UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
2728 /* wait a little bit */
2729 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
2731 sc->sc_isresumed |= (1 << index);
2733 } else if (x & UHCI_PORTSC_SUSP) {
2734 status |= UPS_SUSPEND;
2736 status |= UPS_PORT_POWER;
2737 if (sc->sc_isresumed & (1 << index))
2738 change |= UPS_C_SUSPEND;
2740 change |= UPS_C_PORT_RESET;
2741 USETW(sc->sc_hub_desc.ps.wPortStatus, status);
2742 USETW(sc->sc_hub_desc.ps.wPortChange, change);
2743 len = sizeof(sc->sc_hub_desc.ps);
2745 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2746 err = USB_ERR_IOERROR;
2748 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2750 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2752 port = UHCI_PORTSC1;
2753 else if (index == 2)
2754 port = UHCI_PORTSC2;
2756 err = USB_ERR_IOERROR;
2760 case UHF_PORT_ENABLE:
2761 x = URWMASK(UREAD2(sc, port));
2762 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2764 case UHF_PORT_SUSPEND:
2765 x = URWMASK(UREAD2(sc, port));
2766 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2768 case UHF_PORT_RESET:
2769 err = uhci_portreset(sc, index);
2771 case UHF_PORT_POWER:
2772 /* pretend we turned on power */
2773 err = USB_ERR_NORMAL_COMPLETION;
2775 case UHF_C_PORT_CONNECTION:
2776 case UHF_C_PORT_ENABLE:
2777 case UHF_C_PORT_OVER_CURRENT:
2778 case UHF_PORT_CONNECTION:
2779 case UHF_PORT_OVER_CURRENT:
2780 case UHF_PORT_LOW_SPEED:
2781 case UHF_C_PORT_SUSPEND:
2782 case UHF_C_PORT_RESET:
2784 err = USB_ERR_IOERROR;
2789 err = USB_ERR_IOERROR;
2799 * This routine is executed periodically and simulates interrupts from
2800 * the root controller interrupt pipe for port status change:
2803 uhci_root_intr(uhci_softc_t *sc)
2807 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2809 sc->sc_hub_idata[0] = 0;
2811 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
2812 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2813 sc->sc_hub_idata[0] |= 1 << 1;
2815 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
2816 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2817 sc->sc_hub_idata[0] |= 1 << 2;
2821 usb_callout_reset(&sc->sc_root_intr, hz,
2822 (void *)&uhci_root_intr, sc);
2824 if (sc->sc_hub_idata[0] != 0) {
2825 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2826 sizeof(sc->sc_hub_idata));
2831 uhci_xfer_setup(struct usb_setup_params *parm)
2833 struct usb_page_search page_info;
2834 struct usb_page_cache *pc;
2836 struct usb_xfer *xfer;
2844 sc = UHCI_BUS2SC(parm->udev->bus);
2845 xfer = parm->curr_xfer;
2847 parm->hc_max_packet_size = 0x500;
2848 parm->hc_max_packet_count = 1;
2849 parm->hc_max_frame_size = 0x500;
2852 * compute ntd and nqh
2854 if (parm->methods == &uhci_device_ctrl_methods) {
2855 xfer->flags_int.bdma_enable = 1;
2856 xfer->flags_int.bdma_no_post_sync = 1;
2858 usbd_transfer_setup_sub(parm);
2860 /* see EHCI HC driver for proof of "ntd" formula */
2863 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
2864 + (xfer->max_data_length / xfer->max_frame_size));
2866 } else if (parm->methods == &uhci_device_bulk_methods) {
2867 xfer->flags_int.bdma_enable = 1;
2868 xfer->flags_int.bdma_no_post_sync = 1;
2870 usbd_transfer_setup_sub(parm);
2873 ntd = ((2 * xfer->nframes)
2874 + (xfer->max_data_length / xfer->max_frame_size));
2876 } else if (parm->methods == &uhci_device_intr_methods) {
2877 xfer->flags_int.bdma_enable = 1;
2878 xfer->flags_int.bdma_no_post_sync = 1;
2880 usbd_transfer_setup_sub(parm);
2883 ntd = ((2 * xfer->nframes)
2884 + (xfer->max_data_length / xfer->max_frame_size));
2886 } else if (parm->methods == &uhci_device_isoc_methods) {
2887 xfer->flags_int.bdma_enable = 1;
2888 xfer->flags_int.bdma_no_post_sync = 1;
2890 usbd_transfer_setup_sub(parm);
2893 ntd = xfer->nframes;
2897 usbd_transfer_setup_sub(parm);
2907 * NOTE: the UHCI controller requires that
2908 * every packet must be contiguous on
2909 * the same USB memory page !
2911 nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
2914 * Compute a suitable power of two alignment
2915 * for our "max_frame_size" fixup buffer(s):
2917 align = xfer->max_frame_size;
2924 /* check for power of two */
2925 if (!(xfer->max_frame_size &
2926 (xfer->max_frame_size - 1))) {
2930 * We don't allow alignments of
2931 * less than 8 bytes:
2933 * NOTE: Allocating using an aligment
2934 * of 1 byte has special meaning!
2941 if (usbd_transfer_setup_sub_malloc(
2942 parm, &pc, xfer->max_frame_size,
2944 parm->err = USB_ERR_NOMEM;
2947 xfer->buf_fixup = pc;
2956 if (usbd_transfer_setup_sub_malloc(
2957 parm, &pc, sizeof(uhci_td_t),
2958 UHCI_TD_ALIGN, ntd)) {
2959 parm->err = USB_ERR_NOMEM;
2963 for (n = 0; n != ntd; n++) {
2966 usbd_get_page(pc + n, 0, &page_info);
2968 td = page_info.buffer;
2971 if ((parm->methods == &uhci_device_bulk_methods) ||
2972 (parm->methods == &uhci_device_ctrl_methods) ||
2973 (parm->methods == &uhci_device_intr_methods)) {
2974 /* set depth first bit */
2975 td->td_self = htole32(page_info.physaddr |
2976 UHCI_PTR_TD | UHCI_PTR_VF);
2978 td->td_self = htole32(page_info.physaddr |
2982 td->obj_next = last_obj;
2983 td->page_cache = pc + n;
2987 usb_pc_cpu_flush(pc + n);
2990 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
2994 if (usbd_transfer_setup_sub_malloc(
2995 parm, &pc, sizeof(uhci_qh_t),
2996 UHCI_QH_ALIGN, nqh)) {
2997 parm->err = USB_ERR_NOMEM;
3001 for (n = 0; n != nqh; n++) {
3004 usbd_get_page(pc + n, 0, &page_info);
3006 qh = page_info.buffer;
3009 qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
3010 qh->obj_next = last_obj;
3011 qh->page_cache = pc + n;
3015 usb_pc_cpu_flush(pc + n);
3018 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3020 if (!xfer->flags_int.curr_dma_set) {
3021 xfer->flags_int.curr_dma_set = 1;
3027 uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3028 struct usb_endpoint *ep)
3030 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
3032 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3034 edesc->bEndpointAddress, udev->flags.usb_mode,
3037 if (udev->flags.usb_mode != USB_MODE_HOST) {
3041 if (udev->device_index != sc->sc_addr) {
3042 switch (edesc->bmAttributes & UE_XFERTYPE) {
3044 ep->methods = &uhci_device_ctrl_methods;
3047 ep->methods = &uhci_device_intr_methods;
3049 case UE_ISOCHRONOUS:
3050 if (udev->speed == USB_SPEED_FULL) {
3051 ep->methods = &uhci_device_isoc_methods;
3055 ep->methods = &uhci_device_bulk_methods;
3065 uhci_xfer_unsetup(struct usb_xfer *xfer)
3071 uhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3074 * Wait until hardware has finished any possible use of the
3075 * transfer descriptor(s) and QH
3077 *pus = (1125); /* microseconds */
3081 uhci_device_resume(struct usb_device *udev)
3083 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3084 struct usb_xfer *xfer;
3085 struct usb_pipe_methods *methods;
3090 USB_BUS_LOCK(udev->bus);
3092 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3094 if (xfer->xroot->udev == udev) {
3096 methods = xfer->endpoint->methods;
3097 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3099 if (methods == &uhci_device_bulk_methods) {
3100 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
3102 xfer->flags_int.bandwidth_reclaimed = 1;
3104 if (methods == &uhci_device_ctrl_methods) {
3105 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3106 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
3108 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
3111 if (methods == &uhci_device_intr_methods) {
3112 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3117 USB_BUS_UNLOCK(udev->bus);
3123 uhci_device_suspend(struct usb_device *udev)
3125 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3126 struct usb_xfer *xfer;
3127 struct usb_pipe_methods *methods;
3132 USB_BUS_LOCK(udev->bus);
3134 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3136 if (xfer->xroot->udev == udev) {
3138 methods = xfer->endpoint->methods;
3139 qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3141 if (xfer->flags_int.bandwidth_reclaimed) {
3142 xfer->flags_int.bandwidth_reclaimed = 0;
3145 if (methods == &uhci_device_bulk_methods) {
3146 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
3148 if (methods == &uhci_device_ctrl_methods) {
3149 if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3150 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
3152 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
3155 if (methods == &uhci_device_intr_methods) {
3156 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3161 USB_BUS_UNLOCK(udev->bus);
3167 uhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
3169 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3172 case USB_HW_POWER_SUSPEND:
3173 case USB_HW_POWER_SHUTDOWN:
3176 case USB_HW_POWER_RESUME:
3185 uhci_set_hw_power(struct usb_bus *bus)
3187 struct uhci_softc *sc = UHCI_BUS2SC(bus);
3194 flags = bus->hw_power_state;
3197 * WARNING: Some FULL speed USB devices require periodic SOF
3198 * messages! If any USB devices are connected through the
3199 * UHCI, power save will be disabled!
3201 if (flags & (USB_HW_POWER_CONTROL |
3202 USB_HW_POWER_NON_ROOT_HUB |
3204 USB_HW_POWER_INTERRUPT |
3205 USB_HW_POWER_ISOC)) {
3206 DPRINTF("Some USB transfer is "
3207 "active on unit %u.\n",
3208 device_get_unit(sc->sc_bus.bdev));
3211 DPRINTF("Power save on unit %u.\n",
3212 device_get_unit(sc->sc_bus.bdev));
3213 UHCICMD(sc, UHCI_CMD_MAXP);
3216 USB_BUS_UNLOCK(bus);
3222 struct usb_bus_methods uhci_bus_methods =
3224 .endpoint_init = uhci_ep_init,
3225 .xfer_setup = uhci_xfer_setup,
3226 .xfer_unsetup = uhci_xfer_unsetup,
3227 .get_dma_delay = uhci_get_dma_delay,
3228 .device_resume = uhci_device_resume,
3229 .device_suspend = uhci_device_suspend,
3230 .set_hw_power = uhci_set_hw_power,
3231 .set_hw_power_sleep = uhci_set_hw_power_sleep,
3232 .roothub_exec = uhci_roothub_exec,
3233 .xfer_poll = uhci_do_poll,