1 /* $NetBSD: uhci.c,v 1.170 2003/02/19 01:35:04 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uhci.c,v 1.162.2.1 2006/03/01 01:59:04 iedowse Exp $ */
4 /* Also already incorporated from NetBSD:
5 * $NetBSD: uhci.c,v 1.172 2003/02/23 04:19:26 simonb Exp $
6 * $NetBSD: uhci.c,v 1.173 2003/05/13 04:41:59 gson Exp $
7 * $NetBSD: uhci.c,v 1.175 2003/09/12 16:18:08 mycroft Exp $
8 * $NetBSD: uhci.c,v 1.176 2003/11/04 19:11:21 mycroft Exp $
9 * $NetBSD: uhci.c,v 1.177 2003/12/29 08:17:10 toshii Exp $
10 * $NetBSD: uhci.c,v 1.178 2004/03/02 16:32:05 martin Exp $
11 * $NetBSD: uhci.c,v 1.180 2004/07/17 20:12:03 mycroft Exp $
15 * Copyright (c) 1998 The NetBSD Foundation, Inc.
16 * All rights reserved.
18 * This code is derived from software contributed to The NetBSD Foundation
19 * by Lennart Augustsson (lennart@augustsson.net) at
20 * Carlstedt Research & Technology.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * This product includes software developed by the NetBSD
33 * Foundation, Inc. and its contributors.
34 * 4. Neither the name of The NetBSD Foundation nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
39 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
40 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
42 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48 * POSSIBILITY OF SUCH DAMAGE.
52 * USB Universal Host Controller driver.
53 * Handles e.g. PIIX3 and PIIX4.
55 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
56 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
57 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
58 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #include <sys/endian.h>
66 #include <sys/module.h>
68 #if defined(DIAGNOSTIC) && defined(__i386__)
69 #include <machine/cpu.h>
72 #include <sys/queue.h>
73 #include <sys/sysctl.h>
74 #include <sys/thread2.h>
76 #include <machine/endian.h>
78 #include <bus/usb/usb.h>
79 #include <bus/usb/usbdi.h>
80 #include <bus/usb/usbdivar.h>
81 #include <bus/usb/usb_mem.h>
82 #include <bus/usb/usb_quirks.h>
84 #include <bus/usb/uhcireg.h>
85 #include <bus/usb/uhcivar.h>
87 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
88 /*#define UHCI_CTL_LOOP */
90 #define delay(d) DELAY(d)
92 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
96 #define DPRINTF(x) if (uhcidebug) kprintf x
97 #define DPRINTFN(n,x) if (uhcidebug>(n)) kprintf x
100 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
101 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
102 &uhcidebug, 0, "uhci debug level");
103 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
104 &uhcinoloop, 0, "uhci noloop");
105 #define bitmask_snprintf(q,f,b,l) ksnprintf((b), (l), "%b", (q), (f))
108 #define DPRINTFN(n,x)
112 struct usbd_pipe pipe;
116 usbd_xfer_handle abortstart, abortend;
118 /* Info needed for different pipe kinds. */
124 uhci_soft_td_t *setup, *stat;
131 uhci_soft_qh_t **qhs;
141 uhci_soft_td_t **stds;
147 static void uhci_globalreset(uhci_softc_t *);
148 static usbd_status uhci_portreset(uhci_softc_t*, int);
149 static void uhci_reset(uhci_softc_t *);
150 static usbd_status uhci_run(uhci_softc_t *, int run);
151 static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
152 static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
153 static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
154 static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
156 static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
158 static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
161 static void uhci_free_std_chain(uhci_softc_t *,
162 uhci_soft_td_t *, uhci_soft_td_t *);
163 static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
164 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
165 uhci_soft_td_t **, uhci_soft_td_t **);
166 static void uhci_poll_hub(void *);
167 static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
168 static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
169 static void uhci_idone(uhci_intr_info_t *);
171 static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
173 static void uhci_timeout(void *);
174 static void uhci_timeout_task(void *);
175 static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
176 static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
177 static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
178 static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
179 static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
180 static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
181 static int uhci_str(usb_string_descriptor_t *, int, char *);
182 static void uhci_add_loop(uhci_softc_t *sc);
183 static void uhci_rem_loop(uhci_softc_t *sc);
185 static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
186 static void uhci_device_isoc_enter(usbd_xfer_handle);
188 static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
189 static void uhci_freem(struct usbd_bus *, usb_dma_t *);
191 static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
192 static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
194 static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
195 static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
196 static void uhci_device_ctrl_abort(usbd_xfer_handle);
197 static void uhci_device_ctrl_close(usbd_pipe_handle);
198 static void uhci_device_ctrl_done(usbd_xfer_handle);
200 static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
201 static usbd_status uhci_device_intr_start(usbd_xfer_handle);
202 static void uhci_device_intr_abort(usbd_xfer_handle);
203 static void uhci_device_intr_close(usbd_pipe_handle);
204 static void uhci_device_intr_done(usbd_xfer_handle);
206 static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
207 static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
208 static void uhci_device_bulk_abort(usbd_xfer_handle);
209 static void uhci_device_bulk_close(usbd_pipe_handle);
210 static void uhci_device_bulk_done(usbd_xfer_handle);
212 static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
213 static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
214 static void uhci_device_isoc_abort(usbd_xfer_handle);
215 static void uhci_device_isoc_close(usbd_pipe_handle);
216 static void uhci_device_isoc_done(usbd_xfer_handle);
218 static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
219 static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
220 static void uhci_root_ctrl_abort(usbd_xfer_handle);
221 static void uhci_root_ctrl_close(usbd_pipe_handle);
222 static void uhci_root_ctrl_done(usbd_xfer_handle);
224 static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
225 static usbd_status uhci_root_intr_start(usbd_xfer_handle);
226 static void uhci_root_intr_abort(usbd_xfer_handle);
227 static void uhci_root_intr_close(usbd_pipe_handle);
228 static void uhci_root_intr_done(usbd_xfer_handle);
230 static usbd_status uhci_open(usbd_pipe_handle);
231 static void uhci_poll(struct usbd_bus *);
232 static void uhci_softintr(void *);
234 static usbd_status uhci_device_request(usbd_xfer_handle xfer);
236 static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
237 static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
238 static usbd_status uhci_device_setintr(uhci_softc_t *sc,
239 struct uhci_pipe *pipe, int ival);
241 static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
242 static void uhci_noop(usbd_pipe_handle pipe);
244 static __inline uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
248 static void uhci_dump_all(uhci_softc_t *);
249 static void uhci_dumpregs(uhci_softc_t *);
251 static void uhci_dump_qhs(uhci_soft_qh_t *);
253 static void uhci_dump_qh(uhci_soft_qh_t *);
254 static void uhci_dump_tds(uhci_soft_td_t *);
255 static void uhci_dump_td(uhci_soft_td_t *);
256 static void uhci_dump_ii(uhci_intr_info_t *ii);
257 void uhci_dump(void);
260 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
261 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
262 #define UWRITE1(sc, r, x) \
263 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
264 } while (/*CONSTCOND*/0)
265 #define UWRITE2(sc, r, x) \
266 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
267 } while (/*CONSTCOND*/0)
268 #define UWRITE4(sc, r, x) \
269 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
270 } while (/*CONSTCOND*/0)
271 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
272 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
273 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
275 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
276 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
278 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
280 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
282 #define UHCI_INTR_ENDPT 1
284 struct usbd_bus_methods uhci_bus_methods = {
294 struct usbd_pipe_methods uhci_root_ctrl_methods = {
295 uhci_root_ctrl_transfer,
296 uhci_root_ctrl_start,
297 uhci_root_ctrl_abort,
298 uhci_root_ctrl_close,
303 struct usbd_pipe_methods uhci_root_intr_methods = {
304 uhci_root_intr_transfer,
305 uhci_root_intr_start,
306 uhci_root_intr_abort,
307 uhci_root_intr_close,
312 struct usbd_pipe_methods uhci_device_ctrl_methods = {
313 uhci_device_ctrl_transfer,
314 uhci_device_ctrl_start,
315 uhci_device_ctrl_abort,
316 uhci_device_ctrl_close,
318 uhci_device_ctrl_done,
321 struct usbd_pipe_methods uhci_device_intr_methods = {
322 uhci_device_intr_transfer,
323 uhci_device_intr_start,
324 uhci_device_intr_abort,
325 uhci_device_intr_close,
326 uhci_device_clear_toggle,
327 uhci_device_intr_done,
330 struct usbd_pipe_methods uhci_device_bulk_methods = {
331 uhci_device_bulk_transfer,
332 uhci_device_bulk_start,
333 uhci_device_bulk_abort,
334 uhci_device_bulk_close,
335 uhci_device_clear_toggle,
336 uhci_device_bulk_done,
339 struct usbd_pipe_methods uhci_device_isoc_methods = {
340 uhci_device_isoc_transfer,
341 uhci_device_isoc_start,
342 uhci_device_isoc_abort,
343 uhci_device_isoc_close,
345 uhci_device_isoc_done,
348 #define uhci_add_intr_info(sc, ii) \
349 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
350 #define uhci_del_intr_info(ii) \
352 LIST_REMOVE((ii), list); \
353 (ii)->list.le_prev = NULL; \
355 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
357 static __inline uhci_soft_qh_t *
358 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
360 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
362 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
363 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
364 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
365 kprintf("uhci_find_prev_qh: QH not found\n");
374 uhci_globalreset(uhci_softc_t *sc)
376 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
377 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
378 UHCICMD(sc, 0); /* do nothing */
382 uhci_init(uhci_softc_t *sc)
386 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
389 DPRINTFN(1,("uhci_init: start\n"));
398 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
399 uhci_globalreset(sc); /* reset the controller */
402 /* Allocate and initialize real frame array. */
403 err = usb_allocmem(&sc->sc_bus,
404 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
405 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
408 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
409 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
410 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
413 * Allocate a TD, inactive, that hangs from the last QH.
414 * This is to avoid a bug in the PIIX that makes it run berserk
417 std = uhci_alloc_std(sc);
420 std->link.std = NULL;
421 std->td.td_link = htole32(UHCI_PTR_T);
422 std->td.td_status = htole32(0); /* inactive */
423 std->td.td_token = htole32(0);
424 std->td.td_buffer = htole32(0);
426 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
427 lsqh = uhci_alloc_sqh(sc);
431 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
433 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
434 sc->sc_last_qh = lsqh;
436 /* Allocate the dummy QH where bulk traffic will be queued. */
437 bsqh = uhci_alloc_sqh(sc);
441 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
443 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
444 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
446 /* Allocate dummy QH where high speed control traffic will be queued. */
447 chsqh = uhci_alloc_sqh(sc);
451 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
453 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
454 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
456 /* Allocate dummy QH where control traffic will be queued. */
457 clsqh = uhci_alloc_sqh(sc);
460 clsqh->hlink = chsqh;
461 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
463 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
464 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
467 * Make all (virtual) frame list pointers point to the interrupt
468 * queue heads and the interrupt queue heads at the control
469 * queue head and point the physical frame list to the virtual.
471 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
472 std = uhci_alloc_std(sc);
473 sqh = uhci_alloc_sqh(sc);
474 if (std == NULL || sqh == NULL)
478 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
479 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
480 std->td.td_token = htole32(0);
481 std->td.td_buffer = htole32(0);
483 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
485 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
486 sc->sc_vframes[i].htd = std;
487 sc->sc_vframes[i].etd = std;
488 sc->sc_vframes[i].hqh = sqh;
489 sc->sc_vframes[i].eqh = sqh;
491 j < UHCI_FRAMELIST_COUNT;
492 j += UHCI_VFRAMELIST_COUNT) {
493 sc->sc_pframes[j] = htole32(std->physaddr);
497 LIST_INIT(&sc->sc_intrhead);
499 STAILQ_INIT(&sc->sc_free_xfers);
501 callout_init(&sc->sc_poll_handle);
503 /* Set up the bus struct. */
504 sc->sc_bus.methods = &uhci_bus_methods;
505 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
507 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
509 DPRINTFN(1,("uhci_init: enabling\n"));
511 err = uhci_run(sc, 1); /* and here we go... */
515 sc->sc_flags |= UHCI_SCFLG_DONEINIT;
516 UWRITE2(sc, UHCI_INTR,
517 UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
518 UHCI_INTR_IOCE | UHCI_INTR_SPIE);
526 uhci_detach(struct uhci_softc *sc, int flags)
528 usbd_xfer_handle xfer;
531 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
534 /* Free all xfers associated with this HC. */
536 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
539 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
543 /* XXX free other data structures XXX */
544 usb_freemem(&sc->sc_bus, &sc->sc_dma);
550 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
552 return (usb_allocmem(bus, size, 0, dma));
556 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
558 usb_freemem(bus, dma);
562 uhci_allocx(struct usbd_bus *bus)
564 struct uhci_softc *sc = (struct uhci_softc *)bus;
565 usbd_xfer_handle xfer;
567 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
569 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
571 if (xfer->busy_free != XFER_FREE) {
572 kprintf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
577 xfer = kmalloc(sizeof(struct uhci_xfer), M_USB, M_INTWAIT);
580 memset(xfer, 0, sizeof (struct uhci_xfer));
581 UXFER(xfer)->iinfo.sc = sc;
582 usb_init_task(&UXFER(xfer)->abort_task, uhci_timeout_task,
584 UXFER(xfer)->uhci_xfer_flags = 0;
586 UXFER(xfer)->iinfo.isdone = 1;
587 xfer->busy_free = XFER_BUSY;
594 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
596 struct uhci_softc *sc = (struct uhci_softc *)bus;
599 if (xfer->busy_free != XFER_BUSY) {
600 kprintf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
604 xfer->busy_free = XFER_FREE;
605 if (!UXFER(xfer)->iinfo.isdone) {
606 kprintf("uhci_freex: !isdone\n");
610 STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
614 * Shut down the controller when the system is going down.
617 uhci_shutdown(void *v)
619 uhci_softc_t *sc = v;
621 DPRINTF(("uhci_shutdown: stopping the HC\n"));
622 uhci_run(sc, 0); /* stop the controller */
626 * Handle suspend/resume.
628 * We need to switch to polling mode here, because this routine is
629 * called from an interrupt context. This is all right since we
630 * are almost suspended anyway.
633 uhci_power(int why, void *v)
635 uhci_softc_t *sc = v;
639 cmd = UREAD2(sc, UHCI_CMD);
641 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
642 sc, why, sc->sc_suspend, cmd));
644 if (why != PWR_RESUME) {
649 if (sc->sc_intr_xfer != NULL)
650 callout_stop(&sc->sc_poll_handle);
651 sc->sc_bus.use_polling++;
652 uhci_run(sc, 0); /* stop the controller */
654 /* save some state if BIOS doesn't */
655 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
656 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
658 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
660 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
661 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
662 sc->sc_suspend = why;
663 sc->sc_bus.use_polling--;
664 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
667 if (sc->sc_suspend == PWR_RESUME)
668 kprintf("uhci_power: weird, resume without suspend.\n");
670 sc->sc_bus.use_polling++;
671 sc->sc_suspend = why;
672 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
673 uhci_globalreset(sc); /* reset the controller */
675 if (cmd & UHCI_CMD_RS)
676 uhci_run(sc, 0); /* in case BIOS has started it */
678 uhci_globalreset(sc);
681 /* restore saved state */
682 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
683 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
684 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
686 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
687 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
688 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
690 /* re-enable intrs */
691 UWRITE2(sc, UHCI_INTR,
692 UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
693 UHCI_INTR_IOCE | UHCI_INTR_SPIE);
694 UHCICMD(sc, UHCI_CMD_MAXP);
695 uhci_run(sc, 1); /* and start traffic again */
696 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
697 sc->sc_bus.use_polling--;
698 if (sc->sc_intr_xfer != NULL)
699 callout_reset(&sc->sc_poll_handle, sc->sc_ival,
700 uhci_poll_hub, sc->sc_intr_xfer);
711 uhci_dumpregs(uhci_softc_t *sc)
713 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
714 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
715 device_get_nameunit(sc->sc_bus.bdev),
716 UREAD2(sc, UHCI_CMD),
717 UREAD2(sc, UHCI_STS),
718 UREAD2(sc, UHCI_INTR),
719 UREAD2(sc, UHCI_FRNUM),
720 UREAD4(sc, UHCI_FLBASEADDR),
721 UREAD1(sc, UHCI_SOF),
722 UREAD2(sc, UHCI_PORTSC1),
723 UREAD2(sc, UHCI_PORTSC2)));
727 uhci_dump_td(uhci_soft_td_t *p)
729 char sbuf[128], sbuf2[128];
731 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
732 "token=0x%08lx buffer=0x%08lx\n",
733 p, (long)p->physaddr,
734 (long)le32toh(p->td.td_link),
735 (long)le32toh(p->td.td_status),
736 (long)le32toh(p->td.td_token),
737 (long)le32toh(p->td.td_buffer)));
739 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
741 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
742 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
743 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
744 sbuf2, sizeof(sbuf2));
746 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
747 "D=%d,maxlen=%d\n", sbuf, sbuf2,
748 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
749 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
750 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
751 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
752 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
753 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
754 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
758 uhci_dump_qh(uhci_soft_qh_t *sqh)
760 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
761 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
762 le32toh(sqh->qh.qh_elink)));
769 uhci_dump_all(thesc);
774 uhci_dump_all(uhci_softc_t *sc)
777 kprintf("intrs=%d\n", sc->sc_bus.no_intrs);
778 /*kprintf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
779 uhci_dump_qh(sc->sc_lctl_start);
784 uhci_dump_qhs(uhci_soft_qh_t *sqh)
788 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
789 * Traverses sideways first, then down.
799 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
803 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
804 uhci_dump_qhs(sqh->hlink);
806 DPRINTF(("No QH\n"));
808 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
809 uhci_dump_tds(sqh->elink);
811 DPRINTF(("No TD\n"));
816 uhci_dump_tds(uhci_soft_td_t *std)
820 for(td = std; td != NULL; td = td->link.std) {
823 /* Check whether the link pointer in this TD marks
824 * the link pointer as end of queue. This avoids
825 * printing the free list in case the queue/TD has
826 * already been moved there (seatbelt).
828 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
829 le32toh(td->td.td_link) == 0)
835 uhci_dump_ii(uhci_intr_info_t *ii)
837 usbd_pipe_handle pipe;
838 usb_endpoint_descriptor_t *ed;
839 usbd_device_handle dev;
842 #define DONE ii->isdone
847 kprintf("ii NULL\n");
850 if (ii->xfer == NULL) {
851 kprintf("ii %p: done=%d xfer=NULL\n",
855 pipe = ii->xfer->pipe;
857 kprintf("ii %p: done=%d xfer=%p pipe=NULL\n",
861 if (pipe->endpoint == NULL) {
862 kprintf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
863 ii, DONE, ii->xfer, pipe);
866 if (pipe->device == NULL) {
867 kprintf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
868 ii, DONE, ii->xfer, pipe);
871 ed = pipe->endpoint->edesc;
873 kprintf("ii %p: done=%d xfer=%p dev=%p vid=0x%04x pid=0x%04x addr=%d pipe=%p ep=0x%02x attr=0x%02x\n",
874 ii, DONE, ii->xfer, dev,
875 UGETW(dev->ddesc.idVendor),
876 UGETW(dev->ddesc.idProduct),
878 ed->bEndpointAddress, ed->bmAttributes);
882 void uhci_dump_iis(struct uhci_softc *sc);
884 uhci_dump_iis(struct uhci_softc *sc)
886 uhci_intr_info_t *ii;
888 kprintf("intr_info list:\n");
889 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
894 void iidump(void) { uhci_dump_iis(thesc); }
899 * This routine is executed periodically and simulates interrupts
900 * from the root controller interrupt pipe for port status change.
903 uhci_poll_hub(void *addr)
905 usbd_xfer_handle xfer = addr;
906 usbd_pipe_handle pipe = xfer->pipe;
907 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
910 DPRINTFN(20, ("uhci_poll_hub\n"));
912 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
914 p = KERNADDR(&xfer->dmabuf, 0);
916 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
918 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
921 /* No change, try again in a while */
925 xfer->status = USBD_NORMAL_COMPLETION;
927 xfer->device->bus->intr_context++;
928 usb_transfer_complete(xfer);
929 xfer->device->bus->intr_context--;
934 uhci_root_intr_done(usbd_xfer_handle xfer)
939 uhci_root_ctrl_done(usbd_xfer_handle xfer)
944 * Let the last QH loop back to the high speed control transfer QH.
945 * This is what intel calls "bandwidth reclamation" and improves
946 * USB performance a lot for some devices.
947 * If we are already looping, just count it.
950 uhci_add_loop(uhci_softc_t *sc) {
955 if (++sc->sc_loops == 1) {
956 DPRINTFN(5,("uhci_start_loop: add\n"));
957 /* Note, we don't loop back the soft pointer. */
958 sc->sc_last_qh->qh.qh_hlink =
959 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
964 uhci_rem_loop(uhci_softc_t *sc) {
969 if (--sc->sc_loops == 0) {
970 DPRINTFN(5,("uhci_end_loop: remove\n"));
971 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
975 /* Add high speed control QH, called from a critical section. */
977 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
981 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
982 eqh = sc->sc_hctl_end;
983 sqh->hlink = eqh->hlink;
984 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
986 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
987 sc->sc_hctl_end = sqh;
993 /* Remove high speed control QH, called from a critical section. */
995 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
999 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1000 #ifdef UHCI_CTL_LOOP
1004 * The T bit should be set in the elink of the QH so that the HC
1005 * doesn't follow the pointer. This condition may fail if the
1006 * the transferred packet was short so that the QH still points
1007 * at the last used TD.
1008 * In this case we set the T bit and wait a little for the HC
1009 * to stop looking at the TD.
1011 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1012 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1013 delay(UHCI_QH_REMOVE_DELAY);
1016 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1017 pqh->hlink = sqh->hlink;
1018 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1019 delay(UHCI_QH_REMOVE_DELAY);
1020 if (sc->sc_hctl_end == sqh)
1021 sc->sc_hctl_end = pqh;
1024 /* Add low speed control QH, called from a critical section. */
1026 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1028 uhci_soft_qh_t *eqh;
1030 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1031 eqh = sc->sc_lctl_end;
1032 sqh->hlink = eqh->hlink;
1033 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1035 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1036 sc->sc_lctl_end = sqh;
1039 /* Remove low speed control QH, called from a critical section. */
1041 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1043 uhci_soft_qh_t *pqh;
1045 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1046 /* See comment in uhci_remove_hs_ctrl() */
1047 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1048 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1049 delay(UHCI_QH_REMOVE_DELAY);
1051 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1052 pqh->hlink = sqh->hlink;
1053 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1054 delay(UHCI_QH_REMOVE_DELAY);
1055 if (sc->sc_lctl_end == sqh)
1056 sc->sc_lctl_end = pqh;
1059 /* Add bulk QH, called from a critical section. */
1061 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1063 uhci_soft_qh_t *eqh;
1065 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1066 eqh = sc->sc_bulk_end;
1067 sqh->hlink = eqh->hlink;
1068 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1070 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1071 sc->sc_bulk_end = sqh;
1075 /* Remove bulk QH, called from a critical section. */
1077 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1079 uhci_soft_qh_t *pqh;
1081 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1083 /* See comment in uhci_remove_hs_ctrl() */
1084 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1085 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1086 delay(UHCI_QH_REMOVE_DELAY);
1088 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1089 pqh->hlink = sqh->hlink;
1090 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1091 delay(UHCI_QH_REMOVE_DELAY);
1092 if (sc->sc_bulk_end == sqh)
1093 sc->sc_bulk_end = pqh;
1096 static int uhci_intr1(uhci_softc_t *);
1099 uhci_intr(void *arg)
1101 uhci_softc_t *sc = arg;
1103 if (sc->sc_dying || (sc->sc_flags & UHCI_SCFLG_DONEINIT) == 0)
1106 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1107 if (sc->sc_bus.use_polling) {
1109 DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n"));
1114 return (uhci_intr1(sc));
1118 uhci_intr1(uhci_softc_t *sc)
1125 * It can happen that an interrupt will be delivered to
1126 * us before the device has been fully attached and the
1127 * softc struct has been configured. Usually this happens
1128 * when kldloading the USB support as a module after the
1129 * system has been booted. If we detect this condition,
1130 * we need to squelch the unwanted interrupts until we're
1133 if (sc->sc_bus.bdev == NULL) {
1134 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1135 uhci_run(sc, 0); /* stop the controller */
1136 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1141 if (uhcidebug > 15) {
1142 DPRINTF(("%s: uhci_intr1\n", device_get_nameunit(sc->sc_bus.bdev)));
1146 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1147 if (status == 0) /* The interrupt was not for us. */
1150 if (sc->sc_suspend != PWR_RESUME) {
1151 device_printf(sc->sc_bus.bdev,
1152 "interrupt while not operating ignored\n");
1153 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1158 if (status & UHCI_STS_USBINT)
1159 ack |= UHCI_STS_USBINT;
1160 if (status & UHCI_STS_USBEI)
1161 ack |= UHCI_STS_USBEI;
1162 if (status & UHCI_STS_RD) {
1165 device_printf(sc->sc_bus.bdev, "resume detect\n");
1168 if (status & UHCI_STS_HSE) {
1169 ack |= UHCI_STS_HSE;
1170 device_printf(sc->sc_bus.bdev, "host system error\n");
1172 if (status & UHCI_STS_HCPE) {
1173 ack |= UHCI_STS_HCPE;
1174 device_printf(sc->sc_bus.bdev,
1175 "host controller process error\n");
1177 if (status & UHCI_STS_HCH) {
1178 /* no acknowledge needed */
1179 if (!sc->sc_dying) {
1180 device_printf(sc->sc_bus.bdev,
1181 "host controller halted\n");
1190 return (0); /* nothing to acknowledge */
1191 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1193 sc->sc_bus.no_intrs++;
1194 usb_schedsoftintr(&sc->sc_bus);
1196 DPRINTFN(15, ("%s: uhci_intr: exit\n", device_get_nameunit(sc->sc_bus.bdev)));
1202 uhci_softintr(void *v)
1204 uhci_softc_t *sc = v;
1205 uhci_intr_info_t *ii, *nextii;
1207 DPRINTFN(10,("%s: uhci_softintr (%d)\n", device_get_nameunit(sc->sc_bus.bdev),
1208 sc->sc_bus.intr_context));
1210 sc->sc_bus.intr_context++;
1213 * Interrupts on UHCI really suck. When the host controller
1214 * interrupts because a transfer is completed there is no
1215 * way of knowing which transfer it was. You can scan down
1216 * the TDs and QHs of the previous frame to limit the search,
1217 * but that assumes that the interrupt was not delayed by more
1218 * than 1 ms, which may not always be true (e.g. after debug
1219 * output on a slow console).
1220 * We scan all interrupt descriptors to see if any have
1223 LIST_FOREACH_MUTABLE(ii, &sc->sc_intrhead, list, nextii)
1224 uhci_check_intr(sc, ii);
1226 #ifdef USB_USE_SOFTINTR
1227 if (sc->sc_softwake) {
1228 sc->sc_softwake = 0;
1229 wakeup(&sc->sc_softwake);
1231 #endif /* USB_USE_SOFTINTR */
1233 sc->sc_bus.intr_context--;
1236 /* Check for an interrupt. */
1238 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1240 uhci_soft_td_t *std, *lstd;
1243 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1246 kprintf("uhci_check_intr: no ii? %p\n", ii);
1250 if (ii->xfer->status == USBD_CANCELLED ||
1251 ii->xfer->status == USBD_TIMEOUT) {
1252 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1256 if (ii->stdstart == NULL)
1261 kprintf("uhci_check_intr: std==0\n");
1266 * If the last TD is still active we need to check whether there
1267 * is an error somewhere in the middle, or whether there was a
1268 * short packet (SPD and not ACTIVE).
1270 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1271 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1272 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1273 status = le32toh(std->td.td_status);
1274 /* If there's an active TD the xfer isn't done. */
1275 if (status & UHCI_TD_ACTIVE)
1277 /* Any kind of error makes the xfer done. */
1278 if (status & UHCI_TD_STALLED)
1280 /* We want short packets, and it is short: it's done */
1281 if ((status & UHCI_TD_SPD) &&
1282 UHCI_TD_GET_ACTLEN(status) <
1283 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) {
1287 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1292 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1293 callout_stop(&ii->xfer->timeout_handle);
1294 usb_rem_task(ii->xfer->pipe->device, &UXFER(ii->xfer)->abort_task);
1298 /* Called from a critical section. */
1300 uhci_idone(uhci_intr_info_t *ii)
1302 usbd_xfer_handle xfer = ii->xfer;
1303 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1304 uhci_soft_td_t *std;
1305 u_int32_t status = 0, nstatus;
1308 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1315 kprintf("uhci_idone: ii is done!\n ");
1318 kprintf("uhci_idone: ii=%p is done!\n", ii);
1327 if (xfer->nframes != 0) {
1328 /* Isoc transfer, do things differently. */
1329 uhci_soft_td_t **stds = upipe->u.iso.stds;
1330 int i, n, nframes, len;
1332 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1334 nframes = xfer->nframes;
1336 n = UXFER(xfer)->curframe;
1337 for (i = 0; i < nframes; i++) {
1340 if (uhcidebug > 5) {
1341 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1345 if (++n >= UHCI_VFRAMELIST_COUNT)
1347 status = le32toh(std->td.td_status);
1348 len = UHCI_TD_GET_ACTLEN(status);
1349 xfer->frlengths[i] = len;
1352 upipe->u.iso.inuse -= nframes;
1353 xfer->actlen = actlen;
1354 xfer->status = USBD_NORMAL_COMPLETION;
1359 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1362 uhci_dump_tds(ii->stdstart);
1365 /* The transfer is done, compute actual length and status. */
1367 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1368 nstatus = le32toh(std->td.td_status);
1369 if (nstatus & UHCI_TD_ACTIVE)
1373 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1374 UHCI_TD_PID_SETUP) {
1375 actlen += UHCI_TD_GET_ACTLEN(status);
1378 * UHCI will report CRCTO in addition to a STALL or NAK
1379 * for a SETUP transaction. See section 3.2.2, "TD
1380 * CONTROL AND STATUS".
1382 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1383 status &= ~UHCI_TD_CRCTO;
1387 /* If there are left over TDs we need to update the toggle. */
1389 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1391 status &= UHCI_TD_ERROR;
1392 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1394 xfer->actlen = actlen;
1399 bitmask_snprintf((u_int32_t)status,
1400 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1401 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1402 sbuf, sizeof(sbuf));
1404 DPRINTFN((status == UHCI_TD_STALLED)*10,
1405 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1407 xfer->pipe->device->address,
1408 xfer->pipe->endpoint->edesc->bEndpointAddress,
1411 if (status == UHCI_TD_STALLED)
1412 xfer->status = USBD_STALLED;
1414 xfer->status = USBD_IOERROR; /* more info XXX */
1416 xfer->status = USBD_NORMAL_COMPLETION;
1420 usb_transfer_complete(xfer);
1421 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1425 * Called when a request does not complete.
1428 uhci_timeout(void *addr)
1430 uhci_intr_info_t *ii = addr;
1431 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1432 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1433 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1435 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1438 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1442 /* Execute the abort in a process context. */
1443 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task,
1448 uhci_timeout_task(void *addr)
1450 usbd_xfer_handle xfer = addr;
1452 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1455 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1460 * Wait here until controller claims to have an interrupt.
1461 * Then call uhci_intr and return. Use timeout to avoid waiting
1463 * Only used during boot when interrupts are not enabled yet.
1466 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1468 int timo = xfer->timeout;
1469 uhci_intr_info_t *ii;
1471 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1473 xfer->status = USBD_IN_PROGRESS;
1474 for (; timo >= 0; timo--) {
1475 usb_delay_ms(&sc->sc_bus, 1);
1476 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1477 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1479 if (xfer->status != USBD_IN_PROGRESS)
1484 DPRINTF(("uhci_waitintr: timeout\n"));
1485 for (ii = LIST_FIRST(&sc->sc_intrhead);
1486 ii != NULL && ii->xfer != xfer;
1487 ii = LIST_NEXT(ii, list))
1491 panic("uhci_waitintr: lost intr_info");
1497 uhci_poll(struct usbd_bus *bus)
1499 uhci_softc_t *sc = (uhci_softc_t *)bus;
1508 uhci_reset(uhci_softc_t *sc)
1512 UHCICMD(sc, UHCI_CMD_HCRESET);
1513 /* The reset bit goes low when the controller is done. */
1514 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1515 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1516 usb_delay_ms(&sc->sc_bus, 1);
1517 if (n >= UHCI_RESET_TIMEOUT)
1518 device_printf(sc->sc_bus.bdev, "controller did not reset\n");
1522 uhci_run(uhci_softc_t *sc, int run)
1529 DPRINTF(("uhci_run: setting run=%d\n", run));
1530 cmd = UREAD2(sc, UHCI_CMD);
1534 cmd &= ~UHCI_CMD_RS;
1536 for(n = 0; n < 10; n++) {
1537 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1538 /* return when we've entered the state we want */
1539 if (run == running) {
1541 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1542 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1543 return (USBD_NORMAL_COMPLETION);
1545 usb_delay_ms(&sc->sc_bus, 1);
1548 device_printf(sc->sc_bus.bdev, "cannot %s\n", run ? "start" : "stop");
1549 return (USBD_IOERROR);
1553 * Memory management routines.
1554 * uhci_alloc_std allocates TDs
1555 * uhci_alloc_sqh allocates QHs
1556 * These two routines do their own free list management,
1557 * partly for speed, partly because allocating DMAable memory
1558 * has page size granularaity so much memory would be wasted if
1559 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1563 uhci_alloc_std(uhci_softc_t *sc)
1565 uhci_soft_td_t *std;
1570 if (sc->sc_freetds == NULL) {
1571 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1572 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1573 UHCI_TD_ALIGN, &dma);
1576 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1577 offs = i * UHCI_STD_SIZE;
1578 std = KERNADDR(&dma, offs);
1579 std->physaddr = DMAADDR(&dma, offs);
1580 std->link.std = sc->sc_freetds;
1581 sc->sc_freetds = std;
1584 std = sc->sc_freetds;
1585 sc->sc_freetds = std->link.std;
1586 memset(&std->td, 0, sizeof(uhci_td_t));
1591 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1594 #define TD_IS_FREE 0x12345678
1595 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1596 kprintf("uhci_free_std: freeing free TD %p\n", std);
1599 std->td.td_token = htole32(TD_IS_FREE);
1601 std->link.std = sc->sc_freetds;
1602 sc->sc_freetds = std;
1606 uhci_alloc_sqh(uhci_softc_t *sc)
1608 uhci_soft_qh_t *sqh;
1613 if (sc->sc_freeqhs == NULL) {
1614 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1615 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1616 UHCI_QH_ALIGN, &dma);
1619 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1620 offs = i * UHCI_SQH_SIZE;
1621 sqh = KERNADDR(&dma, offs);
1622 sqh->physaddr = DMAADDR(&dma, offs);
1623 sqh->hlink = sc->sc_freeqhs;
1624 sc->sc_freeqhs = sqh;
1627 sqh = sc->sc_freeqhs;
1628 sc->sc_freeqhs = sqh->hlink;
1629 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1634 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1636 sqh->hlink = sc->sc_freeqhs;
1637 sc->sc_freeqhs = sqh;
1641 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1642 uhci_soft_td_t *stdend)
1646 for (; std != stdend; std = p) {
1648 uhci_free_std(sc, std);
1653 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1654 int rd, u_int16_t flags, usb_dma_t *dma,
1655 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1657 uhci_soft_td_t *p, *lastp;
1658 uhci_physaddr_t lastlink;
1659 int i, ntd, l, tog, maxp;
1661 int addr = upipe->pipe.device->address;
1662 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1664 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1665 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1666 upipe->pipe.device->speed, flags));
1667 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1669 kprintf("uhci_alloc_std_chain: maxp=0\n");
1670 return (USBD_INVAL);
1672 ntd = (len + maxp - 1) / maxp;
1673 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1675 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1678 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1679 return (USBD_NORMAL_COMPLETION);
1681 tog = upipe->nexttoggle;
1684 upipe->nexttoggle = tog ^ 1;
1686 lastlink = UHCI_PTR_T;
1688 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1689 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1690 status |= UHCI_TD_LS;
1691 if (flags & USBD_SHORT_XFER_OK)
1692 status |= UHCI_TD_SPD;
1693 for (i = ntd; i >= 0; i--) {
1694 p = uhci_alloc_std(sc);
1696 uhci_free_std_chain(sc, lastp, NULL);
1697 return (USBD_NOMEM);
1699 p->link.std = lastp;
1700 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1702 lastlink = p->physaddr;
1703 p->td.td_status = htole32(status);
1707 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1713 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1714 UHCI_TD_OUT(l, endpt, addr, tog));
1715 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1719 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1720 upipe->nexttoggle));
1721 return (USBD_NORMAL_COMPLETION);
1725 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1727 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1728 upipe->nexttoggle = 0;
1732 uhci_noop(usbd_pipe_handle pipe)
1737 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1741 /* Insert last in queue. */
1742 err = usb_insert_transfer(xfer);
1747 * Pipe isn't running (otherwise err would be USBD_INPROG),
1748 * so start it first.
1750 return (uhci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
1754 uhci_device_bulk_start(usbd_xfer_handle xfer)
1756 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1757 usbd_device_handle dev = upipe->pipe.device;
1758 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1759 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1760 uhci_soft_td_t *data, *dataend;
1761 uhci_soft_qh_t *sqh;
1763 int len, isread, endpt;
1765 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1766 xfer, xfer->length, xfer->flags, ii));
1769 return (USBD_IOERROR);
1772 if (xfer->rqflags & URQ_REQUEST)
1773 panic("uhci_device_bulk_transfer: a request");
1777 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1778 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1779 sqh = upipe->u.bulk.sqh;
1781 upipe->u.bulk.isread = isread;
1782 upipe->u.bulk.length = len;
1784 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1785 &xfer->dmabuf, &data, &dataend);
1788 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1791 if (uhcidebug > 8) {
1792 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1793 uhci_dump_tds(data);
1797 /* Set up interrupt info. */
1799 ii->stdstart = data;
1800 ii->stdend = dataend;
1803 kprintf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1809 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1812 uhci_add_bulk(sc, sqh);
1813 uhci_add_intr_info(sc, ii);
1815 if (xfer->timeout && !sc->sc_bus.use_polling) {
1816 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1819 xfer->status = USBD_IN_PROGRESS;
1823 if (uhcidebug > 10) {
1824 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1825 uhci_dump_tds(data);
1829 if (sc->sc_bus.use_polling)
1830 uhci_waitintr(sc, xfer);
1832 return (USBD_IN_PROGRESS);
1835 /* Abort a device bulk request. */
1837 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1839 DPRINTF(("uhci_device_bulk_abort:\n"));
1840 uhci_abort_xfer(xfer, USBD_CANCELLED);
1844 * Abort a device request.
1845 * If this routine is called from a critical section. It guarantees that
1846 * the request will be removed from the hardware scheduling and that
1847 * the callback for it will be called with USBD_CANCELLED status.
1848 * It's impossible to guarantee that the requested transfer will not
1849 * have happened since the hardware runs concurrently.
1850 * If the transaction has already happened we rely on the ordinary
1851 * interrupt processing to process it.
1854 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1856 struct uhci_xfer *uxfer = UXFER(xfer);
1857 uhci_intr_info_t *ii = &uxfer->iinfo;
1858 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1859 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1860 uhci_soft_td_t *std;
1862 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1865 /* If we're dying, just do the software part. */
1867 xfer->status = status; /* make software ignore it */
1868 callout_stop(&xfer->timeout_handle);
1869 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1870 usb_transfer_complete(xfer);
1876 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
1877 panic("uhci_abort_xfer: not in process context");
1881 * If an abort is already in progress then just wait for it to
1882 * complete and return.
1885 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING) {
1887 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n"));
1888 /* No need to wait if we're aborting from a timeout. */
1889 if (status == USBD_TIMEOUT)
1891 /* Override the status which might be USBD_TIMEOUT. */
1892 xfer->status = status;
1893 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n"));
1894 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTWAIT;
1896 while (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING)
1897 tsleep(&uxfer->uhci_xfer_flags, 0, "uhciaw", 0);
1903 * Step 1: Make interrupt routine and hardware ignore xfer.
1905 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTING;
1906 xfer->status = status; /* make software ignore it */
1907 callout_stop(&xfer->timeout_handle);
1908 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1909 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1910 for (std = ii->stdstart; std != NULL; std = std->link.std)
1911 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1915 * Step 2: Wait until we know hardware has finished any possible
1916 * use of the xfer. Also make sure the soft interrupt routine
1919 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1921 #ifdef USB_USE_SOFTINTR
1922 sc->sc_softwake = 1;
1923 #endif /* USB_USE_SOFTINTR */
1924 usb_schedsoftintr(&sc->sc_bus);
1925 #ifdef USB_USE_SOFTINTR
1926 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1927 tsleep(&sc->sc_softwake, 0, "uhciab", 0);
1928 #endif /* USB_USE_SOFTINTR */
1931 * Step 3: Execute callback.
1933 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1937 /* Do the wakeup first to avoid touching the xfer after the callback. */
1938 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTING;
1939 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTWAIT) {
1940 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTWAIT;
1941 wakeup(&uxfer->uhci_xfer_flags);
1943 usb_transfer_complete(xfer);
1947 /* Close a device bulk pipe. */
1949 uhci_device_bulk_close(usbd_pipe_handle pipe)
1951 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1952 usbd_device_handle dev = upipe->pipe.device;
1953 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1955 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1956 pipe->endpoint->savedtoggle = upipe->nexttoggle;
1960 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1964 /* Insert last in queue. */
1965 err = usb_insert_transfer(xfer);
1970 * Pipe isn't running (otherwise err would be USBD_INPROG),
1971 * so start it first.
1973 return (uhci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
1977 uhci_device_ctrl_start(usbd_xfer_handle xfer)
1979 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1983 return (USBD_IOERROR);
1986 if (!(xfer->rqflags & URQ_REQUEST))
1987 panic("uhci_device_ctrl_transfer: not a request");
1990 err = uhci_device_request(xfer);
1994 if (sc->sc_bus.use_polling)
1995 uhci_waitintr(sc, xfer);
1996 return (USBD_IN_PROGRESS);
2000 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2004 /* Insert last in queue. */
2005 err = usb_insert_transfer(xfer);
2010 * Pipe isn't running (otherwise err would be USBD_INPROG),
2011 * so start it first.
2013 return (uhci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
2017 uhci_device_intr_start(usbd_xfer_handle xfer)
2019 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2020 usbd_device_handle dev = upipe->pipe.device;
2021 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2022 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2023 uhci_soft_td_t *data, *dataend;
2024 uhci_soft_qh_t *sqh;
2030 return (USBD_IOERROR);
2032 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2033 xfer, xfer->length, xfer->flags));
2036 if (xfer->rqflags & URQ_REQUEST)
2037 panic("uhci_device_intr_transfer: a request");
2040 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2041 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2042 sqh = upipe->u.bulk.sqh;
2044 upipe->u.intr.isread = isread;
2046 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread,
2047 xfer->flags, &xfer->dmabuf, &data,
2051 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2054 if (uhcidebug > 10) {
2055 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2056 uhci_dump_tds(data);
2057 uhci_dump_qh(upipe->u.intr.qhs[0]);
2062 /* Set up interrupt info. */
2064 ii->stdstart = data;
2065 ii->stdend = dataend;
2068 kprintf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2073 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2074 upipe->u.intr.qhs[0]));
2075 for (i = 0; i < upipe->u.intr.npoll; i++) {
2076 sqh = upipe->u.intr.qhs[i];
2078 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2080 uhci_add_intr_info(sc, ii);
2081 xfer->status = USBD_IN_PROGRESS;
2085 if (uhcidebug > 10) {
2086 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2087 uhci_dump_tds(data);
2088 uhci_dump_qh(upipe->u.intr.qhs[0]);
2092 return (USBD_IN_PROGRESS);
2095 /* Abort a device control request. */
2097 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2099 DPRINTF(("uhci_device_ctrl_abort:\n"));
2100 uhci_abort_xfer(xfer, USBD_CANCELLED);
2103 /* Close a device control pipe. */
2105 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2109 /* Abort a device interrupt request. */
2111 uhci_device_intr_abort(usbd_xfer_handle xfer)
2113 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2114 if (xfer->pipe->intrxfer == xfer) {
2115 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2116 xfer->pipe->intrxfer = NULL;
2118 uhci_abort_xfer(xfer, USBD_CANCELLED);
2121 /* Close a device interrupt pipe. */
2123 uhci_device_intr_close(usbd_pipe_handle pipe)
2125 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2126 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2129 /* Unlink descriptors from controller data structures. */
2130 npoll = upipe->u.intr.npoll;
2132 for (i = 0; i < npoll; i++)
2133 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2137 * We now have to wait for any activity on the physical
2138 * descriptors to stop.
2140 usb_delay_ms(&sc->sc_bus, 2);
2142 for(i = 0; i < npoll; i++)
2143 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2144 kfree(upipe->u.intr.qhs, M_USBHC);
2146 /* XXX free other resources */
2150 uhci_device_request(usbd_xfer_handle xfer)
2152 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2153 usb_device_request_t *req = &xfer->request;
2154 usbd_device_handle dev = upipe->pipe.device;
2155 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2156 int addr = dev->address;
2157 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2158 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2159 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2160 uhci_soft_qh_t *sqh;
2166 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2167 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2168 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2169 UGETW(req->wIndex), UGETW(req->wLength),
2172 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2173 isread = req->bmRequestType & UT_READ;
2174 len = UGETW(req->wLength);
2176 setup = upipe->u.ctl.setup;
2177 stat = upipe->u.ctl.stat;
2178 sqh = upipe->u.ctl.sqh;
2180 /* Set up data transaction */
2182 upipe->nexttoggle = 1;
2183 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2184 &xfer->dmabuf, &data, &dataend);
2188 dataend->link.std = stat;
2189 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2193 upipe->u.ctl.length = len;
2195 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2197 setup->link.std = next;
2198 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2199 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2201 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2202 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2203 stat->link.std = NULL;
2204 stat->td.td_link = htole32(UHCI_PTR_T);
2205 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2206 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2209 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2210 UHCI_TD_IN (0, endpt, addr, 1));
2211 stat->td.td_buffer = htole32(0);
2214 if (uhcidebug > 10) {
2215 DPRINTF(("uhci_device_request: before transfer\n"));
2216 uhci_dump_tds(setup);
2220 /* Set up interrupt info. */
2222 ii->stdstart = setup;
2226 kprintf("uhci_device_request: not done, ii=%p\n", ii);
2232 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2235 if (dev->speed == USB_SPEED_LOW)
2236 uhci_add_ls_ctrl(sc, sqh);
2238 uhci_add_hs_ctrl(sc, sqh);
2239 uhci_add_intr_info(sc, ii);
2241 if (uhcidebug > 12) {
2242 uhci_soft_td_t *std;
2243 uhci_soft_qh_t *xqh;
2244 uhci_soft_qh_t *sxqh;
2246 uhci_physaddr_t link;
2247 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2248 for (std = sc->sc_vframes[0].htd, link = 0;
2249 (link & UHCI_PTR_QH) == 0;
2250 std = std->link.std) {
2251 link = le32toh(std->td.td_link);
2254 sxqh = (uhci_soft_qh_t *)std;
2258 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2259 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2262 DPRINTF(("Enqueued QH:\n"));
2264 uhci_dump_tds(sqh->elink);
2267 if (xfer->timeout && !sc->sc_bus.use_polling) {
2268 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2271 xfer->status = USBD_IN_PROGRESS;
2274 return (USBD_NORMAL_COMPLETION);
2278 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2282 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2284 /* Put it on our queue, */
2285 err = usb_insert_transfer(xfer);
2287 /* bail out on error, */
2288 if (err && err != USBD_IN_PROGRESS)
2291 /* XXX should check inuse here */
2293 /* insert into schedule, */
2294 uhci_device_isoc_enter(xfer);
2296 /* and start if the pipe wasn't running */
2298 uhci_device_isoc_start(STAILQ_FIRST(&xfer->pipe->queue));
2304 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2306 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2307 usbd_device_handle dev = upipe->pipe.device;
2308 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2309 struct iso *iso = &upipe->u.iso;
2310 uhci_soft_td_t *std;
2311 u_int32_t buf, len, status;
2312 int i, next, nframes;
2314 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2316 iso->inuse, iso->next, xfer, xfer->nframes));
2321 if (xfer->status == USBD_IN_PROGRESS) {
2322 /* This request has already been entered into the frame list */
2323 kprintf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2328 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2329 kprintf("uhci_device_isoc_enter: overflow!\n");
2334 /* Not in use yet, schedule it a few frames ahead. */
2335 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2336 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2339 xfer->status = USBD_IN_PROGRESS;
2340 UXFER(xfer)->curframe = next;
2342 buf = DMAADDR(&xfer->dmabuf, 0);
2343 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2346 nframes = xfer->nframes;
2348 for (i = 0; i < nframes; i++) {
2349 std = iso->stds[next];
2350 if (++next >= UHCI_VFRAMELIST_COUNT)
2352 len = xfer->frlengths[i];
2353 std->td.td_buffer = htole32(buf);
2354 if (i == nframes - 1)
2355 status |= UHCI_TD_IOC;
2356 std->td.td_status = htole32(status);
2357 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2358 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2360 if (uhcidebug > 5) {
2361 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2368 iso->inuse += xfer->nframes;
2374 uhci_device_isoc_start(usbd_xfer_handle xfer)
2376 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2377 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2378 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2379 uhci_soft_td_t *end;
2382 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2385 return (USBD_IOERROR);
2388 if (xfer->status != USBD_IN_PROGRESS)
2389 kprintf("uhci_device_isoc_start: not in progress %p\n", xfer);
2392 /* Find the last TD */
2393 i = UXFER(xfer)->curframe + xfer->nframes;
2394 if (i >= UHCI_VFRAMELIST_COUNT)
2395 i -= UHCI_VFRAMELIST_COUNT;
2396 end = upipe->u.iso.stds[i];
2400 kprintf("uhci_device_isoc_start: end == NULL\n");
2401 return (USBD_INVAL);
2407 /* Set up interrupt info. */
2413 kprintf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2416 uhci_add_intr_info(sc, ii);
2420 return (USBD_IN_PROGRESS);
2424 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2426 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2427 uhci_soft_td_t **stds = upipe->u.iso.stds;
2428 uhci_soft_td_t *std;
2429 int i, n, nframes, maxlen, len;
2433 /* Transfer is already done. */
2434 if (xfer->status != USBD_NOT_STARTED &&
2435 xfer->status != USBD_IN_PROGRESS) {
2440 /* Give xfer the requested abort code. */
2441 xfer->status = USBD_CANCELLED;
2443 /* make hardware ignore it, */
2444 nframes = xfer->nframes;
2445 n = UXFER(xfer)->curframe;
2447 for (i = 0; i < nframes; i++) {
2449 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2450 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2453 if (++n >= UHCI_VFRAMELIST_COUNT)
2457 /* and wait until we are sure the hardware has finished. */
2461 UXFER(xfer)->iinfo.isdone = 1;
2463 /* Run callback and remove from interrupt list. */
2464 usb_transfer_complete(xfer);
2470 uhci_device_isoc_close(usbd_pipe_handle pipe)
2472 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2473 usbd_device_handle dev = upipe->pipe.device;
2474 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2475 uhci_soft_td_t *std, *vstd;
2480 * Make sure all TDs are marked as inactive.
2481 * Wait for completion.
2485 iso = &upipe->u.iso;
2487 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2488 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2489 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2492 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2494 for (vstd = sc->sc_vframes[i].htd;
2495 vstd != NULL && vstd->link.std != std;
2496 vstd = vstd->link.std)
2500 kprintf("uhci_device_isoc_close: %p not found\n", std);
2504 vstd->link = std->link;
2505 vstd->td.td_link = std->td.td_link;
2506 uhci_free_std(sc, std);
2510 kfree(iso->stds, M_USBHC);
2514 uhci_setup_isoc(usbd_pipe_handle pipe)
2516 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2517 usbd_device_handle dev = upipe->pipe.device;
2518 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2519 int addr = upipe->pipe.device->address;
2520 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2521 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2522 uhci_soft_td_t *std, *vstd;
2527 iso = &upipe->u.iso;
2528 iso->stds = kmalloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2529 M_USBHC, M_INTWAIT);
2531 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2532 UHCI_TD_OUT(0, endpt, addr, 0);
2534 /* Allocate the TDs and mark as inactive; */
2535 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2536 std = uhci_alloc_std(sc);
2539 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2540 std->td.td_token = htole32(token);
2544 /* Insert TDs into schedule. */
2546 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2548 vstd = sc->sc_vframes[i].htd;
2549 std->link = vstd->link;
2550 std->td.td_link = vstd->td.td_link;
2551 vstd->link.std = std;
2552 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2559 return (USBD_NORMAL_COMPLETION);
2563 uhci_free_std(sc, iso->stds[i]);
2564 kfree(iso->stds, M_USBHC);
2565 return (USBD_NOMEM);
2569 uhci_device_isoc_done(usbd_xfer_handle xfer)
2571 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2573 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2575 if (ii->xfer != xfer)
2576 /* Not on interrupt list, ignore it. */
2579 if (!uhci_active_intr_info(ii))
2583 if (xfer->busy_free != XFER_BUSY) {
2584 kprintf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2585 xfer, xfer->busy_free);
2589 if (ii->stdend == NULL) {
2590 kprintf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2598 /* Turn off the interrupt since it is active even if the TD is not. */
2599 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2601 uhci_del_intr_info(ii); /* remove from active list */
2604 if (ii->stdend == NULL) {
2605 kprintf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2615 uhci_device_intr_done(usbd_xfer_handle xfer)
2617 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2618 uhci_softc_t *sc = ii->sc;
2619 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2620 uhci_soft_qh_t *sqh;
2623 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2625 npoll = upipe->u.intr.npoll;
2626 for(i = 0; i < npoll; i++) {
2627 sqh = upipe->u.intr.qhs[i];
2629 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2631 uhci_free_std_chain(sc, ii->stdstart, NULL);
2634 if (xfer->pipe->repeat) {
2635 uhci_soft_td_t *data, *dataend;
2637 DPRINTFN(5,("uhci_device_intr_done: requeueing\n"));
2639 /* This alloc cannot fail since we freed the chain above. */
2640 uhci_alloc_std_chain(upipe, sc, xfer->length,
2641 upipe->u.intr.isread, xfer->flags,
2642 &xfer->dmabuf, &data, &dataend);
2643 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2646 if (uhcidebug > 10) {
2647 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2648 uhci_dump_tds(data);
2649 uhci_dump_qh(upipe->u.intr.qhs[0]);
2653 ii->stdstart = data;
2654 ii->stdend = dataend;
2657 kprintf("uhci_device_intr_done: not done, ii=%p\n", ii);
2661 for (i = 0; i < npoll; i++) {
2662 sqh = upipe->u.intr.qhs[i];
2664 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2666 xfer->status = USBD_IN_PROGRESS;
2667 /* The ii is already on the examined list, just leave it. */
2669 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2670 if (uhci_active_intr_info(ii))
2671 uhci_del_intr_info(ii);
2675 /* Deallocate request data structures */
2677 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2679 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2680 uhci_softc_t *sc = ii->sc;
2681 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2684 if (!(xfer->rqflags & URQ_REQUEST))
2685 panic("uhci_device_ctrl_done: not a request");
2688 if (!uhci_active_intr_info(ii))
2691 uhci_del_intr_info(ii); /* remove from active list */
2693 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2694 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2696 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2698 if (upipe->u.ctl.length != 0)
2699 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2701 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2704 /* Deallocate request data structures */
2706 uhci_device_bulk_done(usbd_xfer_handle xfer)
2708 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2709 uhci_softc_t *sc = ii->sc;
2710 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2712 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2713 xfer, ii, sc, upipe));
2715 if (!uhci_active_intr_info(ii))
2718 uhci_del_intr_info(ii); /* remove from active list */
2720 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2722 uhci_free_std_chain(sc, ii->stdstart, NULL);
2724 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2727 /* Add interrupt QH, called with vflock. */
2729 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2731 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2732 uhci_soft_qh_t *eqh;
2734 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2737 sqh->hlink = eqh->hlink;
2738 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2740 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2745 /* Remove interrupt QH. */
2747 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2749 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2750 uhci_soft_qh_t *pqh;
2752 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2754 /* See comment in uhci_remove_ctrl() */
2755 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2756 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2757 delay(UHCI_QH_REMOVE_DELAY);
2760 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2761 pqh->hlink = sqh->hlink;
2762 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2763 delay(UHCI_QH_REMOVE_DELAY);
2770 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2772 uhci_soft_qh_t *sqh;
2774 u_int bestbw, bw, bestoffs, offs;
2776 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2778 kprintf("uhci_setintr: 0 interval\n");
2779 return (USBD_INVAL);
2782 if (ival > UHCI_VFRAMELIST_COUNT)
2783 ival = UHCI_VFRAMELIST_COUNT;
2784 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2785 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2787 upipe->u.intr.npoll = npoll;
2789 kmalloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_INTWAIT);
2792 * Figure out which offset in the schedule that has most
2793 * bandwidth left over.
2795 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2796 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2797 for (bw = i = 0; i < npoll; i++)
2798 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2804 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2806 for(i = 0; i < npoll; i++) {
2807 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2809 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2810 sqh->pos = MOD(i * ival + bestoffs);
2815 /* Enter QHs into the controller data structures. */
2816 for(i = 0; i < npoll; i++)
2817 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2820 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2821 return (USBD_NORMAL_COMPLETION);
2824 /* Open a new pipe. */
2826 uhci_open(usbd_pipe_handle pipe)
2828 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2829 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2830 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2834 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2835 pipe, pipe->device->address,
2836 ed->bEndpointAddress, sc->sc_addr));
2838 upipe->aborting = 0;
2839 upipe->nexttoggle = pipe->endpoint->savedtoggle;
2841 if (pipe->device->address == sc->sc_addr) {
2842 switch (ed->bEndpointAddress) {
2843 case USB_CONTROL_ENDPOINT:
2844 pipe->methods = &uhci_root_ctrl_methods;
2846 case UE_DIR_IN | UHCI_INTR_ENDPT:
2847 pipe->methods = &uhci_root_intr_methods;
2850 return (USBD_INVAL);
2853 switch (ed->bmAttributes & UE_XFERTYPE) {
2855 pipe->methods = &uhci_device_ctrl_methods;
2856 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2857 if (upipe->u.ctl.sqh == NULL)
2859 upipe->u.ctl.setup = uhci_alloc_std(sc);
2860 if (upipe->u.ctl.setup == NULL) {
2861 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2864 upipe->u.ctl.stat = uhci_alloc_std(sc);
2865 if (upipe->u.ctl.stat == NULL) {
2866 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2867 uhci_free_std(sc, upipe->u.ctl.setup);
2870 err = usb_allocmem(&sc->sc_bus,
2871 sizeof(usb_device_request_t),
2872 0, &upipe->u.ctl.reqdma);
2874 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2875 uhci_free_std(sc, upipe->u.ctl.setup);
2876 uhci_free_std(sc, upipe->u.ctl.stat);
2881 pipe->methods = &uhci_device_intr_methods;
2882 ival = pipe->interval;
2883 if (ival == USBD_DEFAULT_INTERVAL)
2884 ival = ed->bInterval;
2885 return (uhci_device_setintr(sc, upipe, ival));
2886 case UE_ISOCHRONOUS:
2887 pipe->methods = &uhci_device_isoc_methods;
2888 return (uhci_setup_isoc(pipe));
2890 pipe->methods = &uhci_device_bulk_methods;
2891 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2892 if (upipe->u.bulk.sqh == NULL)
2897 return (USBD_NORMAL_COMPLETION);
2900 return (USBD_NOMEM);
2904 * Data structures and routines to emulate the root hub.
2906 usb_device_descriptor_t uhci_devd = {
2907 USB_DEVICE_DESCRIPTOR_SIZE,
2908 UDESC_DEVICE, /* type */
2909 {0x00, 0x01}, /* USB version */
2910 UDCLASS_HUB, /* class */
2911 UDSUBCLASS_HUB, /* subclass */
2912 UDPROTO_FSHUB, /* protocol */
2913 64, /* max packet */
2914 {0},{0},{0x00,0x01}, /* device id */
2915 1,2,0, /* string indicies */
2916 1 /* # of configurations */
2919 usb_config_descriptor_t uhci_confd = {
2920 USB_CONFIG_DESCRIPTOR_SIZE,
2922 {USB_CONFIG_DESCRIPTOR_SIZE +
2923 USB_INTERFACE_DESCRIPTOR_SIZE +
2924 USB_ENDPOINT_DESCRIPTOR_SIZE},
2932 usb_interface_descriptor_t uhci_ifcd = {
2933 USB_INTERFACE_DESCRIPTOR_SIZE,
2944 usb_endpoint_descriptor_t uhci_endpd = {
2945 USB_ENDPOINT_DESCRIPTOR_SIZE,
2947 UE_DIR_IN | UHCI_INTR_ENDPT,
2953 usb_hub_descriptor_t uhci_hubd_piix = {
2954 USB_HUB_DESCRIPTOR_SIZE,
2957 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2958 50, /* power on to power good */
2960 { 0x00 }, /* both ports are removable */
2964 uhci_str(usb_string_descriptor_t *p, int l, char *s)
2970 p->bLength = 2 * strlen(s) + 2;
2973 p->bDescriptorType = UDESC_STRING;
2975 for (i = 0; s[i] && l > 1; i++, l -= 2)
2976 USETW2(p->bString[i], 0, s[i]);
2981 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2982 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2983 * should not be used by the USB subsystem. As we cannot issue a
2984 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2985 * will be enabled as part of the reset.
2987 * On the VT83C572, the port cannot be successfully enabled until the
2988 * outstanding "port enable change" and "connection status change"
2989 * events have been reset.
2992 uhci_portreset(uhci_softc_t *sc, int index)
2997 port = UHCI_PORTSC1;
2998 else if (index == 2)
2999 port = UHCI_PORTSC2;
3001 return (USBD_IOERROR);
3003 x = URWMASK(UREAD2(sc, port));
3004 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3006 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3008 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3009 index, UREAD2(sc, port)));
3011 x = URWMASK(UREAD2(sc, port));
3012 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3016 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3017 index, UREAD2(sc, port)));
3019 x = URWMASK(UREAD2(sc, port));
3020 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3022 for (lim = 10; --lim > 0;) {
3023 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3025 x = UREAD2(sc, port);
3027 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3030 if (!(x & UHCI_PORTSC_CCS)) {
3032 * No device is connected (or was disconnected
3033 * during reset). Consider the port reset.
3034 * The delay must be long enough to ensure on
3035 * the initial iteration that the device
3036 * connection will have been registered. 50ms
3037 * appears to be sufficient, but 20ms is not.
3039 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3044 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3046 * Port enabled changed and/or connection
3047 * status changed were set. Reset either or
3048 * both raised flags (by writing a 1 to that
3049 * bit), and wait again for state to settle.
3051 UWRITE2(sc, port, URWMASK(x) |
3052 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3056 if (x & UHCI_PORTSC_PE)
3057 /* Port is enabled */
3060 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3063 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3064 index, UREAD2(sc, port)));
3067 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3068 return (USBD_TIMEOUT);
3072 return (USBD_NORMAL_COMPLETION);
3076 * Simulate a hardware hub by handling all the necessary requests.
3079 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3083 /* Insert last in queue. */
3084 err = usb_insert_transfer(xfer);
3089 * Pipe isn't running (otherwise err would be USBD_INPROG),
3090 * so start it first.
3092 return (uhci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
3096 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3098 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3099 usb_device_request_t *req;
3102 int len, value, index, status, change, l, totlen = 0;
3103 usb_port_status_t ps;
3107 return (USBD_IOERROR);
3110 if (!(xfer->rqflags & URQ_REQUEST))
3111 panic("uhci_root_ctrl_transfer: not a request");
3113 req = &xfer->request;
3115 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3116 req->bmRequestType, req->bRequest));
3118 len = UGETW(req->wLength);
3119 value = UGETW(req->wValue);
3120 index = UGETW(req->wIndex);
3123 buf = KERNADDR(&xfer->dmabuf, 0);
3125 #define C(x,y) ((x) | ((y) << 8))
3126 switch(C(req->bRequest, req->bmRequestType)) {
3127 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3128 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3129 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3131 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3132 * for the integrated root hub.
3135 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3137 *(u_int8_t *)buf = sc->sc_conf;
3141 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3142 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3143 switch(value >> 8) {
3145 if ((value & 0xff) != 0) {
3149 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3150 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3151 memcpy(buf, &uhci_devd, l);
3154 if ((value & 0xff) != 0) {
3158 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3159 memcpy(buf, &uhci_confd, l);
3160 buf = (char *)buf + l;
3162 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3164 memcpy(buf, &uhci_ifcd, l);
3165 buf = (char *)buf + l;
3167 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3169 memcpy(buf, &uhci_endpd, l);
3174 *(u_int8_t *)buf = 0;
3176 switch (value & 0xff) {
3177 case 1: /* Vendor */
3178 totlen = uhci_str(buf, len, sc->sc_vendor);
3180 case 2: /* Product */
3181 totlen = uhci_str(buf, len, "UHCI root hub");
3190 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3192 *(u_int8_t *)buf = 0;
3196 case C(UR_GET_STATUS, UT_READ_DEVICE):
3198 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3202 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3203 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3205 USETW(((usb_status_t *)buf)->wStatus, 0);
3209 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3210 if (value >= USB_MAX_DEVICES) {
3214 sc->sc_addr = value;
3216 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3217 if (value != 0 && value != 1) {
3221 sc->sc_conf = value;
3223 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3225 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3226 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3227 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3230 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3232 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3235 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3237 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3238 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3239 "port=%d feature=%d\n",
3242 port = UHCI_PORTSC1;
3243 else if (index == 2)
3244 port = UHCI_PORTSC2;
3250 case UHF_PORT_ENABLE:
3251 x = URWMASK(UREAD2(sc, port));
3252 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3254 case UHF_PORT_SUSPEND:
3255 x = URWMASK(UREAD2(sc, port));
3256 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3258 case UHF_PORT_RESET:
3259 x = URWMASK(UREAD2(sc, port));
3260 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3262 case UHF_C_PORT_CONNECTION:
3263 x = URWMASK(UREAD2(sc, port));
3264 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3266 case UHF_C_PORT_ENABLE:
3267 x = URWMASK(UREAD2(sc, port));
3268 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3270 case UHF_C_PORT_OVER_CURRENT:
3271 x = URWMASK(UREAD2(sc, port));
3272 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3274 case UHF_C_PORT_RESET:
3276 err = USBD_NORMAL_COMPLETION;
3278 case UHF_PORT_CONNECTION:
3279 case UHF_PORT_OVER_CURRENT:
3280 case UHF_PORT_POWER:
3281 case UHF_PORT_LOW_SPEED:
3282 case UHF_C_PORT_SUSPEND:
3288 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3290 port = UHCI_PORTSC1;
3291 else if (index == 2)
3292 port = UHCI_PORTSC2;
3299 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3300 UHCI_PORTSC_LS_SHIFT;
3304 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3305 if ((value & 0xff) != 0) {
3309 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3311 memcpy(buf, &uhci_hubd_piix, l);
3313 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3318 memset(buf, 0, len);
3321 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3323 port = UHCI_PORTSC1;
3324 else if (index == 2)
3325 port = UHCI_PORTSC2;
3334 x = UREAD2(sc, port);
3335 status = change = 0;
3336 if (x & UHCI_PORTSC_CCS)
3337 status |= UPS_CURRENT_CONNECT_STATUS;
3338 if (x & UHCI_PORTSC_CSC)
3339 change |= UPS_C_CONNECT_STATUS;
3340 if (x & UHCI_PORTSC_PE)
3341 status |= UPS_PORT_ENABLED;
3342 if (x & UHCI_PORTSC_POEDC)
3343 change |= UPS_C_PORT_ENABLED;
3344 if (x & UHCI_PORTSC_OCI)
3345 status |= UPS_OVERCURRENT_INDICATOR;
3346 if (x & UHCI_PORTSC_OCIC)
3347 change |= UPS_C_OVERCURRENT_INDICATOR;
3348 if (x & UHCI_PORTSC_SUSP)
3349 status |= UPS_SUSPEND;
3350 if (x & UHCI_PORTSC_LSDA)
3351 status |= UPS_LOW_SPEED;
3352 status |= UPS_PORT_POWER;
3354 change |= UPS_C_PORT_RESET;
3355 USETW(ps.wPortStatus, status);
3356 USETW(ps.wPortChange, change);
3357 l = min(len, sizeof ps);
3358 memcpy(buf, &ps, l);
3361 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3364 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3366 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3368 port = UHCI_PORTSC1;
3369 else if (index == 2)
3370 port = UHCI_PORTSC2;
3376 case UHF_PORT_ENABLE:
3377 x = URWMASK(UREAD2(sc, port));
3378 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3380 case UHF_PORT_SUSPEND:
3381 x = URWMASK(UREAD2(sc, port));
3382 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3384 case UHF_PORT_RESET:
3385 err = uhci_portreset(sc, index);
3387 case UHF_PORT_POWER:
3388 /* Pretend we turned on power */
3389 err = USBD_NORMAL_COMPLETION;
3391 case UHF_C_PORT_CONNECTION:
3392 case UHF_C_PORT_ENABLE:
3393 case UHF_C_PORT_OVER_CURRENT:
3394 case UHF_PORT_CONNECTION:
3395 case UHF_PORT_OVER_CURRENT:
3396 case UHF_PORT_LOW_SPEED:
3397 case UHF_C_PORT_SUSPEND:
3398 case UHF_C_PORT_RESET:
3408 xfer->actlen = totlen;
3409 err = USBD_NORMAL_COMPLETION;
3413 usb_transfer_complete(xfer);
3415 return (USBD_IN_PROGRESS);
3418 /* Abort a root control request. */
3420 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3422 /* Nothing to do, all transfers are synchronous. */
3425 /* Close the root pipe. */
3427 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3429 DPRINTF(("uhci_root_ctrl_close\n"));
3432 /* Abort a root interrupt request. */
3434 uhci_root_intr_abort(usbd_xfer_handle xfer)
3436 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3438 callout_stop(&sc->sc_poll_handle);
3439 sc->sc_intr_xfer = NULL;
3441 if (xfer->pipe->intrxfer == xfer) {
3442 DPRINTF(("uhci_root_intr_abort: remove\n"));
3443 xfer->pipe->intrxfer = 0;
3445 xfer->status = USBD_CANCELLED;
3447 UXFER(xfer)->iinfo.isdone = 1;
3449 usb_transfer_complete(xfer);
3453 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3457 /* Insert last in queue. */
3458 err = usb_insert_transfer(xfer);
3463 * Pipe isn't running (otherwise err would be USBD_INPROG),
3464 * so start it first.
3466 return (uhci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3469 /* Start a transfer on the root interrupt pipe */
3471 uhci_root_intr_start(usbd_xfer_handle xfer)
3473 usbd_pipe_handle pipe = xfer->pipe;
3474 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3476 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3477 xfer, xfer->length, xfer->flags));
3480 return (USBD_IOERROR);
3482 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3483 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3484 sc->sc_intr_xfer = xfer;
3485 return (USBD_IN_PROGRESS);
3488 /* Close the root interrupt pipe. */
3490 uhci_root_intr_close(usbd_pipe_handle pipe)
3492 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3494 callout_stop(&sc->sc_poll_handle);
3495 sc->sc_intr_xfer = NULL;
3496 DPRINTF(("uhci_root_intr_close\n"));