2 * $NetBSD: uhci.c,v 1.80 2000/01/19 01:16:38 augustss Exp $
3 * $NetBSD: uhci.c,v 1.170 2003/02/19 01:35:04 augustss Exp $
4 * $FreeBSD: src/sys/dev/usb/uhci.c,v 1.149 2003/11/10 00:08:41 joe Exp $
5 * $DragonFly: src/sys/bus/usb/uhci.c,v 1.10 2004/03/12 03:43:06 dillon Exp $
8 /* Also already incorporated from NetBSD:
9 * $NetBSD: uhci.c,v 1.172 2003/02/23 04:19:26 simonb Exp $
10 * $NetBSD: uhci.c,v 1.173 2003/05/13 04:41:59 gson Exp $
11 * $NetBSD: uhci.c,v 1.175 2003/09/12 16:18:08 mycroft Exp $
12 * $NetBSD: uhci.c,v 1.176 2003/11/04 19:11:21 mycroft Exp $
17 * Copyright (c) 1998 The NetBSD Foundation, Inc.
18 * All rights reserved.
20 * This code is derived from software contributed to The NetBSD Foundation
21 * by Lennart Augustsson (lennart@augustsson.net) at
22 * Carlstedt Research & Technology.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the NetBSD
35 * Foundation, Inc. and its contributors.
36 * 4. Neither the name of The NetBSD Foundation nor the names of its
37 * contributors may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
41 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
44 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50 * POSSIBILITY OF SUCH DAMAGE.
54 * USB Universal Host Controller driver.
55 * Handles e.g. PIIX3 and PIIX4.
57 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
58 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
59 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
60 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #if defined(__NetBSD__) || defined(__OpenBSD__)
68 #include <sys/device.h>
69 #include <sys/select.h>
70 #elif defined(__FreeBSD__) || defined(__DragonFly__)
71 #include <sys/endian.h>
72 #include <sys/module.h>
74 #include <machine/bus_pio.h>
75 #if defined(DIAGNOSTIC) && defined(__i386__)
76 #include <machine/cpu.h>
80 #include <sys/queue.h>
81 #include <sys/sysctl.h>
83 #include <machine/bus.h>
84 #include <machine/endian.h>
90 #include "usb_quirks.h"
95 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
96 /*#define UHCI_CTL_LOOP */
98 #if defined(__FreeBSD__) || defined(__DragonFly__)
99 #include <machine/clock.h>
101 #define delay(d) DELAY(d)
104 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
106 #if defined(__OpenBSD__)
107 struct cfdriver uhci_cd = {
108 NULL, "uhci", DV_DULL
114 #define DPRINTF(x) if (uhcidebug) printf x
115 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
118 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
119 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
120 &uhcidebug, 0, "uhci debug level");
121 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
122 &uhcinoloop, 0, "uhci noloop");
124 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
128 #define DPRINTFN(n,x)
132 * The UHCI controller is little endian, so on big endian machines
133 * the data strored in memory needs to be swapped.
135 #if defined(__OpenBSD__)
136 #if BYTE_ORDER == BIG_ENDIAN
137 #define htole32(x) (bswap32(x))
138 #define le32toh(x) (bswap32(x))
140 #define htole32(x) (x)
141 #define le32toh(x) (x)
146 struct usbd_pipe pipe;
150 usbd_xfer_handle abortstart, abortend;
152 /* Info needed for different pipe kinds. */
158 uhci_soft_td_t *setup, *stat;
165 uhci_soft_qh_t **qhs;
175 uhci_soft_td_t **stds;
181 Static void uhci_globalreset(uhci_softc_t *);
182 Static usbd_status uhci_portreset(uhci_softc_t*, int);
183 Static void uhci_reset(uhci_softc_t *);
184 #if defined(__NetBSD__) || defined(__OpenBSD__)
185 Static void uhci_shutdown(void *v);
186 Static void uhci_power(int, void *);
188 Static usbd_status uhci_run(uhci_softc_t *, int run);
189 Static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
190 Static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
191 Static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
192 Static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
194 Static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
196 Static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
199 Static void uhci_free_std_chain(uhci_softc_t *,
200 uhci_soft_td_t *, uhci_soft_td_t *);
201 Static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
202 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
203 uhci_soft_td_t **, uhci_soft_td_t **);
204 Static void uhci_poll_hub(void *);
205 Static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
206 Static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
207 Static void uhci_idone(uhci_intr_info_t *);
209 Static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
211 Static void uhci_timeout(void *);
212 Static void uhci_timeout_task(void *);
213 Static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
214 Static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
215 Static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
216 Static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
217 Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
218 Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
219 Static int uhci_str(usb_string_descriptor_t *, int, char *);
220 Static void uhci_add_loop(uhci_softc_t *sc);
221 Static void uhci_rem_loop(uhci_softc_t *sc);
223 Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
224 Static void uhci_device_isoc_enter(usbd_xfer_handle);
226 Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
227 Static void uhci_freem(struct usbd_bus *, usb_dma_t *);
229 Static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
230 Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
232 Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
233 Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
234 Static void uhci_device_ctrl_abort(usbd_xfer_handle);
235 Static void uhci_device_ctrl_close(usbd_pipe_handle);
236 Static void uhci_device_ctrl_done(usbd_xfer_handle);
238 Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
239 Static usbd_status uhci_device_intr_start(usbd_xfer_handle);
240 Static void uhci_device_intr_abort(usbd_xfer_handle);
241 Static void uhci_device_intr_close(usbd_pipe_handle);
242 Static void uhci_device_intr_done(usbd_xfer_handle);
244 Static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
245 Static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
246 Static void uhci_device_bulk_abort(usbd_xfer_handle);
247 Static void uhci_device_bulk_close(usbd_pipe_handle);
248 Static void uhci_device_bulk_done(usbd_xfer_handle);
250 Static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
251 Static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
252 Static void uhci_device_isoc_abort(usbd_xfer_handle);
253 Static void uhci_device_isoc_close(usbd_pipe_handle);
254 Static void uhci_device_isoc_done(usbd_xfer_handle);
256 Static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
257 Static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
258 Static void uhci_root_ctrl_abort(usbd_xfer_handle);
259 Static void uhci_root_ctrl_close(usbd_pipe_handle);
260 Static void uhci_root_ctrl_done(usbd_xfer_handle);
262 Static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
263 Static usbd_status uhci_root_intr_start(usbd_xfer_handle);
264 Static void uhci_root_intr_abort(usbd_xfer_handle);
265 Static void uhci_root_intr_close(usbd_pipe_handle);
266 Static void uhci_root_intr_done(usbd_xfer_handle);
268 Static usbd_status uhci_open(usbd_pipe_handle);
269 Static void uhci_poll(struct usbd_bus *);
270 Static void uhci_softintr(void *);
272 Static usbd_status uhci_device_request(usbd_xfer_handle xfer);
274 Static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
275 Static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
276 Static usbd_status uhci_device_setintr(uhci_softc_t *sc,
277 struct uhci_pipe *pipe, int ival);
279 Static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
280 Static void uhci_noop(usbd_pipe_handle pipe);
282 Static __inline__ uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
286 Static void uhci_dump_all(uhci_softc_t *);
287 Static void uhci_dumpregs(uhci_softc_t *);
288 Static void uhci_dump_qhs(uhci_soft_qh_t *);
289 Static void uhci_dump_qh(uhci_soft_qh_t *);
290 Static void uhci_dump_tds(uhci_soft_td_t *);
291 Static void uhci_dump_td(uhci_soft_td_t *);
292 Static void uhci_dump_ii(uhci_intr_info_t *ii);
293 void uhci_dump(void);
296 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
297 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
298 #define UWRITE1(sc, r, x) \
299 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
300 } while (/*CONSTCOND*/0)
301 #define UWRITE2(sc, r, x) \
302 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
303 } while (/*CONSTCOND*/0)
304 #define UWRITE4(sc, r, x) \
305 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
306 } while (/*CONSTCOND*/0)
307 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
308 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
309 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
311 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
312 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
314 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
316 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
318 #define UHCI_INTR_ENDPT 1
320 struct usbd_bus_methods uhci_bus_methods = {
330 struct usbd_pipe_methods uhci_root_ctrl_methods = {
331 uhci_root_ctrl_transfer,
332 uhci_root_ctrl_start,
333 uhci_root_ctrl_abort,
334 uhci_root_ctrl_close,
339 struct usbd_pipe_methods uhci_root_intr_methods = {
340 uhci_root_intr_transfer,
341 uhci_root_intr_start,
342 uhci_root_intr_abort,
343 uhci_root_intr_close,
348 struct usbd_pipe_methods uhci_device_ctrl_methods = {
349 uhci_device_ctrl_transfer,
350 uhci_device_ctrl_start,
351 uhci_device_ctrl_abort,
352 uhci_device_ctrl_close,
354 uhci_device_ctrl_done,
357 struct usbd_pipe_methods uhci_device_intr_methods = {
358 uhci_device_intr_transfer,
359 uhci_device_intr_start,
360 uhci_device_intr_abort,
361 uhci_device_intr_close,
362 uhci_device_clear_toggle,
363 uhci_device_intr_done,
366 struct usbd_pipe_methods uhci_device_bulk_methods = {
367 uhci_device_bulk_transfer,
368 uhci_device_bulk_start,
369 uhci_device_bulk_abort,
370 uhci_device_bulk_close,
371 uhci_device_clear_toggle,
372 uhci_device_bulk_done,
375 struct usbd_pipe_methods uhci_device_isoc_methods = {
376 uhci_device_isoc_transfer,
377 uhci_device_isoc_start,
378 uhci_device_isoc_abort,
379 uhci_device_isoc_close,
381 uhci_device_isoc_done,
384 #define uhci_add_intr_info(sc, ii) \
385 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
386 #define uhci_del_intr_info(ii) \
388 LIST_REMOVE((ii), list); \
389 (ii)->list.le_prev = NULL; \
391 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
393 Static __inline__ uhci_soft_qh_t *
394 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
396 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
398 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
399 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
400 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
401 printf("uhci_find_prev_qh: QH not found\n");
410 uhci_globalreset(uhci_softc_t *sc)
412 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
413 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
414 UHCICMD(sc, 0); /* do nothing */
418 uhci_init(uhci_softc_t *sc)
422 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
425 DPRINTFN(1,("uhci_init: start\n"));
434 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
435 uhci_globalreset(sc); /* reset the controller */
438 /* Allocate and initialize real frame array. */
439 err = usb_allocmem(&sc->sc_bus,
440 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
441 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
444 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
445 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
446 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
449 * Allocate a TD, inactive, that hangs from the last QH.
450 * This is to avoid a bug in the PIIX that makes it run berserk
453 std = uhci_alloc_std(sc);
456 std->link.std = NULL;
457 std->td.td_link = htole32(UHCI_PTR_T);
458 std->td.td_status = htole32(0); /* inactive */
459 std->td.td_token = htole32(0);
460 std->td.td_buffer = htole32(0);
462 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
463 lsqh = uhci_alloc_sqh(sc);
467 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
469 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
470 sc->sc_last_qh = lsqh;
472 /* Allocate the dummy QH where bulk traffic will be queued. */
473 bsqh = uhci_alloc_sqh(sc);
477 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
479 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
480 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
482 /* Allocate dummy QH where high speed control traffic will be queued. */
483 chsqh = uhci_alloc_sqh(sc);
487 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
489 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
490 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
492 /* Allocate dummy QH where control traffic will be queued. */
493 clsqh = uhci_alloc_sqh(sc);
497 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
499 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
500 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
503 * Make all (virtual) frame list pointers point to the interrupt
504 * queue heads and the interrupt queue heads at the control
505 * queue head and point the physical frame list to the virtual.
507 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
508 std = uhci_alloc_std(sc);
509 sqh = uhci_alloc_sqh(sc);
510 if (std == NULL || sqh == NULL)
514 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
515 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
516 std->td.td_token = htole32(0);
517 std->td.td_buffer = htole32(0);
519 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
521 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
522 sc->sc_vframes[i].htd = std;
523 sc->sc_vframes[i].etd = std;
524 sc->sc_vframes[i].hqh = sqh;
525 sc->sc_vframes[i].eqh = sqh;
527 j < UHCI_FRAMELIST_COUNT;
528 j += UHCI_VFRAMELIST_COUNT) {
529 sc->sc_pframes[j] = htole32(std->physaddr);
533 LIST_INIT(&sc->sc_intrhead);
535 SIMPLEQ_INIT(&sc->sc_free_xfers);
537 usb_callout_init(sc->sc_poll_handle);
539 /* Set up the bus struct. */
540 sc->sc_bus.methods = &uhci_bus_methods;
541 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
543 #if defined(__NetBSD__) || defined(__OpenBSD__)
544 sc->sc_suspend = PWR_RESUME;
545 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
546 sc->sc_shutdownhook = shutdownhook_establish(uhci_shutdown, sc);
548 DPRINTFN(1,("uhci_init: enabling\n"));
549 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
550 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
552 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
554 return (uhci_run(sc, 1)); /* and here we go... */
557 #if defined(__NetBSD__) || defined(__OpenBSD__)
559 uhci_activate(device_ptr_t self, enum devact act)
561 struct uhci_softc *sc = (struct uhci_softc *)self;
568 case DVACT_DEACTIVATE:
569 if (sc->sc_child != NULL)
570 rv = config_deactivate(sc->sc_child);
577 uhci_detach(struct uhci_softc *sc, int flags)
579 usbd_xfer_handle xfer;
582 if (sc->sc_child != NULL)
583 rv = config_detach(sc->sc_child, flags);
588 #if defined(__NetBSD__) || defined(__OpenBSD__)
589 powerhook_disestablish(sc->sc_powerhook);
590 shutdownhook_disestablish(sc->sc_shutdownhook);
593 /* Free all xfers associated with this HC. */
595 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
598 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
602 /* XXX free other data structures XXX */
609 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
611 return (usb_allocmem(bus, size, 0, dma));
615 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
617 usb_freemem(bus, dma);
621 uhci_allocx(struct usbd_bus *bus)
623 struct uhci_softc *sc = (struct uhci_softc *)bus;
624 usbd_xfer_handle xfer;
626 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
628 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
630 if (xfer->busy_free != XFER_FREE) {
631 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
636 xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_INTWAIT);
639 memset(xfer, 0, sizeof (struct uhci_xfer));
640 UXFER(xfer)->iinfo.sc = sc;
642 UXFER(xfer)->iinfo.isdone = 1;
643 xfer->busy_free = XFER_BUSY;
650 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
652 struct uhci_softc *sc = (struct uhci_softc *)bus;
655 if (xfer->busy_free != XFER_BUSY) {
656 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
660 xfer->busy_free = XFER_FREE;
661 if (!UXFER(xfer)->iinfo.isdone) {
662 printf("uhci_freex: !isdone\n");
666 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
670 * Shut down the controller when the system is going down.
673 uhci_shutdown(void *v)
675 uhci_softc_t *sc = v;
677 DPRINTF(("uhci_shutdown: stopping the HC\n"));
678 uhci_run(sc, 0); /* stop the controller */
682 * Handle suspend/resume.
684 * We need to switch to polling mode here, because this routine is
685 * called from an interrupt context. This is all right since we
686 * are almost suspended anyway.
689 uhci_power(int why, void *v)
691 uhci_softc_t *sc = v;
696 cmd = UREAD2(sc, UHCI_CMD);
698 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
699 sc, why, sc->sc_suspend, cmd));
701 if (why != PWR_RESUME) {
706 if (sc->sc_intr_xfer != NULL)
707 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub,
709 sc->sc_bus.use_polling++;
710 uhci_run(sc, 0); /* stop the controller */
712 /* save some state if BIOS doesn't */
713 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
714 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
716 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
718 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
719 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
720 sc->sc_suspend = why;
721 sc->sc_bus.use_polling--;
722 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
725 if (sc->sc_suspend == PWR_RESUME)
726 printf("uhci_power: weird, resume without suspend.\n");
728 sc->sc_bus.use_polling++;
729 sc->sc_suspend = why;
730 if (cmd & UHCI_CMD_RS)
731 uhci_run(sc, 0); /* in case BIOS has started it */
733 /* restore saved state */
734 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
735 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
736 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
738 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
739 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
740 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
741 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
742 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
743 UHCICMD(sc, UHCI_CMD_MAXP);
744 uhci_run(sc, 1); /* and start traffic again */
745 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
746 sc->sc_bus.use_polling--;
747 if (sc->sc_intr_xfer != NULL)
748 usb_callout(sc->sc_poll_handle, sc->sc_ival,
749 uhci_poll_hub, sc->sc_intr_xfer);
760 uhci_dumpregs(uhci_softc_t *sc)
762 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
763 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
764 USBDEVNAME(sc->sc_bus.bdev),
765 UREAD2(sc, UHCI_CMD),
766 UREAD2(sc, UHCI_STS),
767 UREAD2(sc, UHCI_INTR),
768 UREAD2(sc, UHCI_FRNUM),
769 UREAD4(sc, UHCI_FLBASEADDR),
770 UREAD1(sc, UHCI_SOF),
771 UREAD2(sc, UHCI_PORTSC1),
772 UREAD2(sc, UHCI_PORTSC2)));
776 uhci_dump_td(uhci_soft_td_t *p)
778 char sbuf[128], sbuf2[128];
780 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
781 "token=0x%08lx buffer=0x%08lx\n",
782 p, (long)p->physaddr,
783 (long)le32toh(p->td.td_link),
784 (long)le32toh(p->td.td_status),
785 (long)le32toh(p->td.td_token),
786 (long)le32toh(p->td.td_buffer)));
788 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
790 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
791 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
792 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
793 sbuf2, sizeof(sbuf2));
795 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
796 "D=%d,maxlen=%d\n", sbuf, sbuf2,
797 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
798 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
799 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
800 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
801 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
802 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
803 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
807 uhci_dump_qh(uhci_soft_qh_t *sqh)
809 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
810 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
811 le32toh(sqh->qh.qh_elink)));
818 uhci_dump_all(thesc);
823 uhci_dump_all(uhci_softc_t *sc)
826 printf("intrs=%d\n", sc->sc_bus.no_intrs);
827 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
828 uhci_dump_qh(sc->sc_lctl_start);
832 uhci_dump_qhs(uhci_soft_qh_t *sqh)
836 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
837 * Traverses sideways first, then down.
847 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
851 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
852 uhci_dump_qhs(sqh->hlink);
854 DPRINTF(("No QH\n"));
856 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
857 uhci_dump_tds(sqh->elink);
859 DPRINTF(("No TD\n"));
863 uhci_dump_tds(uhci_soft_td_t *std)
867 for(td = std; td != NULL; td = td->link.std) {
870 /* Check whether the link pointer in this TD marks
871 * the link pointer as end of queue. This avoids
872 * printing the free list in case the queue/TD has
873 * already been moved there (seatbelt).
875 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
876 le32toh(td->td.td_link) == 0)
882 uhci_dump_ii(uhci_intr_info_t *ii)
884 usbd_pipe_handle pipe;
885 usb_endpoint_descriptor_t *ed;
886 usbd_device_handle dev;
889 #define DONE ii->isdone
897 if (ii->xfer == NULL) {
898 printf("ii %p: done=%d xfer=NULL\n",
902 pipe = ii->xfer->pipe;
904 printf("ii %p: done=%d xfer=%p pipe=NULL\n",
908 if (pipe->endpoint == NULL) {
909 printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
910 ii, DONE, ii->xfer, pipe);
913 if (pipe->device == NULL) {
914 printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
915 ii, DONE, ii->xfer, pipe);
918 ed = pipe->endpoint->edesc;
920 printf("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",
921 ii, DONE, ii->xfer, dev,
922 UGETW(dev->ddesc.idVendor),
923 UGETW(dev->ddesc.idProduct),
925 ed->bEndpointAddress, ed->bmAttributes);
929 void uhci_dump_iis(struct uhci_softc *sc);
931 uhci_dump_iis(struct uhci_softc *sc)
933 uhci_intr_info_t *ii;
935 printf("intr_info list:\n");
936 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
941 void iidump(void) { uhci_dump_iis(thesc); }
946 * This routine is executed periodically and simulates interrupts
947 * from the root controller interrupt pipe for port status change.
950 uhci_poll_hub(void *addr)
952 usbd_xfer_handle xfer = addr;
953 usbd_pipe_handle pipe = xfer->pipe;
954 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
958 DPRINTFN(20, ("uhci_poll_hub\n"));
960 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
962 p = KERNADDR(&xfer->dmabuf, 0);
964 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
966 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
969 /* No change, try again in a while */
973 xfer->status = USBD_NORMAL_COMPLETION;
975 xfer->device->bus->intr_context++;
976 usb_transfer_complete(xfer);
977 xfer->device->bus->intr_context--;
982 uhci_root_intr_done(usbd_xfer_handle xfer)
987 uhci_root_ctrl_done(usbd_xfer_handle xfer)
992 * Let the last QH loop back to the high speed control transfer QH.
993 * This is what intel calls "bandwidth reclamation" and improves
994 * USB performance a lot for some devices.
995 * If we are already looping, just count it.
998 uhci_add_loop(uhci_softc_t *sc) {
1003 if (++sc->sc_loops == 1) {
1004 DPRINTFN(5,("uhci_start_loop: add\n"));
1005 /* Note, we don't loop back the soft pointer. */
1006 sc->sc_last_qh->qh.qh_hlink =
1007 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
1012 uhci_rem_loop(uhci_softc_t *sc) {
1017 if (--sc->sc_loops == 0) {
1018 DPRINTFN(5,("uhci_end_loop: remove\n"));
1019 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1023 /* Add high speed control QH, called at splusb(). */
1025 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1027 uhci_soft_qh_t *eqh;
1031 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
1032 eqh = sc->sc_hctl_end;
1033 sqh->hlink = eqh->hlink;
1034 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1036 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1037 sc->sc_hctl_end = sqh;
1038 #ifdef UHCI_CTL_LOOP
1043 /* Remove high speed control QH, called at splusb(). */
1045 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1047 uhci_soft_qh_t *pqh;
1051 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1052 #ifdef UHCI_CTL_LOOP
1056 * The T bit should be set in the elink of the QH so that the HC
1057 * doesn't follow the pointer. This condition may fail if the
1058 * the transferred packet was short so that the QH still points
1059 * at the last used TD.
1060 * In this case we set the T bit and wait a little for the HC
1061 * to stop looking at the TD.
1063 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1064 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1065 delay(UHCI_QH_REMOVE_DELAY);
1068 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1069 pqh->hlink = sqh->hlink;
1070 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1071 delay(UHCI_QH_REMOVE_DELAY);
1072 if (sc->sc_hctl_end == sqh)
1073 sc->sc_hctl_end = pqh;
1076 /* Add low speed control QH, called at splusb(). */
1078 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1080 uhci_soft_qh_t *eqh;
1084 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1085 eqh = sc->sc_lctl_end;
1086 sqh->hlink = eqh->hlink;
1087 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1089 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1090 sc->sc_lctl_end = sqh;
1093 /* Remove low speed control QH, called at splusb(). */
1095 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1097 uhci_soft_qh_t *pqh;
1101 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1102 /* See comment in uhci_remove_hs_ctrl() */
1103 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1104 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1105 delay(UHCI_QH_REMOVE_DELAY);
1107 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1108 pqh->hlink = sqh->hlink;
1109 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1110 delay(UHCI_QH_REMOVE_DELAY);
1111 if (sc->sc_lctl_end == sqh)
1112 sc->sc_lctl_end = pqh;
1115 /* Add bulk QH, called at splusb(). */
1117 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1119 uhci_soft_qh_t *eqh;
1123 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1124 eqh = sc->sc_bulk_end;
1125 sqh->hlink = eqh->hlink;
1126 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1128 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1129 sc->sc_bulk_end = sqh;
1133 /* Remove bulk QH, called at splusb(). */
1135 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1137 uhci_soft_qh_t *pqh;
1141 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1143 /* See comment in uhci_remove_hs_ctrl() */
1144 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1145 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1146 delay(UHCI_QH_REMOVE_DELAY);
1148 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1149 pqh->hlink = sqh->hlink;
1150 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1151 delay(UHCI_QH_REMOVE_DELAY);
1152 if (sc->sc_bulk_end == sqh)
1153 sc->sc_bulk_end = pqh;
1156 Static int uhci_intr1(uhci_softc_t *);
1159 uhci_intr(void *arg)
1161 uhci_softc_t *sc = arg;
1166 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1167 if (sc->sc_bus.use_polling) {
1169 printf("uhci_intr: ignored interrupt while polling\n");
1173 return (uhci_intr1(sc));
1177 uhci_intr1(uhci_softc_t *sc)
1184 * It can happen that an interrupt will be delivered to
1185 * us before the device has been fully attached and the
1186 * softc struct has been configured. Usually this happens
1187 * when kldloading the USB support as a module after the
1188 * system has been booted. If we detect this condition,
1189 * we need to squelch the unwanted interrupts until we're
1192 if (sc->sc_bus.bdev == NULL) {
1193 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1194 uhci_run(sc, 0); /* stop the controller */
1195 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1200 if (uhcidebug > 15) {
1201 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev)));
1205 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1206 if (status == 0) /* The interrupt was not for us. */
1209 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1210 if (sc->sc_suspend != PWR_RESUME)
1211 printf("uhci_intr: suspended sts=0x%x\n", status);
1214 if (sc->sc_suspend != PWR_RESUME) {
1215 printf("%s: interrupt while not operating ignored\n",
1216 USBDEVNAME(sc->sc_bus.bdev));
1217 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1222 if (status & UHCI_STS_USBINT)
1223 ack |= UHCI_STS_USBINT;
1224 if (status & UHCI_STS_USBEI)
1225 ack |= UHCI_STS_USBEI;
1226 if (status & UHCI_STS_RD) {
1229 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1232 if (status & UHCI_STS_HSE) {
1233 ack |= UHCI_STS_HSE;
1234 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
1236 if (status & UHCI_STS_HCPE) {
1237 ack |= UHCI_STS_HCPE;
1238 printf("%s: host controller process error\n",
1239 USBDEVNAME(sc->sc_bus.bdev));
1241 if (status & UHCI_STS_HCH) {
1242 /* no acknowledge needed */
1243 if (!sc->sc_dying) {
1244 printf("%s: host controller halted\n",
1245 USBDEVNAME(sc->sc_bus.bdev));
1254 return (0); /* nothing to acknowledge */
1255 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1257 sc->sc_bus.no_intrs++;
1258 usb_schedsoftintr(&sc->sc_bus);
1260 DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
1266 uhci_softintr(void *v)
1268 uhci_softc_t *sc = v;
1269 uhci_intr_info_t *ii;
1271 DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
1272 sc->sc_bus.intr_context));
1274 sc->sc_bus.intr_context++;
1277 * Interrupts on UHCI really suck. When the host controller
1278 * interrupts because a transfer is completed there is no
1279 * way of knowing which transfer it was. You can scan down
1280 * the TDs and QHs of the previous frame to limit the search,
1281 * but that assumes that the interrupt was not delayed by more
1282 * than 1 ms, which may not always be true (e.g. after debug
1283 * output on a slow console).
1284 * We scan all interrupt descriptors to see if any have
1287 LIST_FOREACH(ii, &sc->sc_intrhead, list)
1288 uhci_check_intr(sc, ii);
1290 #ifdef USB_USE_SOFTINTR
1291 if (sc->sc_softwake) {
1292 sc->sc_softwake = 0;
1293 wakeup(&sc->sc_softwake);
1295 #endif /* USB_USE_SOFTINTR */
1297 sc->sc_bus.intr_context--;
1300 /* Check for an interrupt. */
1302 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1304 uhci_soft_td_t *std, *lstd;
1307 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1310 printf("uhci_check_intr: no ii? %p\n", ii);
1314 if (ii->xfer->status == USBD_CANCELLED ||
1315 ii->xfer->status == USBD_TIMEOUT) {
1316 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1320 if (ii->stdstart == NULL)
1325 printf("uhci_check_intr: std==0\n");
1330 * If the last TD is still active we need to check whether there
1331 * is an error somewhere in the middle, or whether there was a
1332 * short packet (SPD and not ACTIVE).
1334 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1335 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1336 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1337 status = le32toh(std->td.td_status);
1338 /* If there's an active TD the xfer isn't done. */
1339 if (status & UHCI_TD_ACTIVE)
1341 /* Any kind of error makes the xfer done. */
1342 if (status & UHCI_TD_STALLED)
1344 /* We want short packets, and it is short: it's done */
1345 if ((status & UHCI_TD_SPD) &&
1346 UHCI_TD_GET_ACTLEN(status) <
1347 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) {
1351 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1356 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1357 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1361 /* Called at splusb() */
1363 uhci_idone(uhci_intr_info_t *ii)
1365 usbd_xfer_handle xfer = ii->xfer;
1366 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1367 uhci_soft_td_t *std;
1368 u_int32_t status = 0, nstatus;
1371 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1378 printf("uhci_idone: ii is done!\n ");
1381 printf("uhci_idone: ii=%p is done!\n", ii);
1390 if (xfer->nframes != 0) {
1391 /* Isoc transfer, do things differently. */
1392 uhci_soft_td_t **stds = upipe->u.iso.stds;
1393 int i, n, nframes, len;
1395 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1397 nframes = xfer->nframes;
1399 n = UXFER(xfer)->curframe;
1400 for (i = 0; i < nframes; i++) {
1403 if (uhcidebug > 5) {
1404 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1408 if (++n >= UHCI_VFRAMELIST_COUNT)
1410 status = le32toh(std->td.td_status);
1411 len = UHCI_TD_GET_ACTLEN(status);
1412 xfer->frlengths[i] = len;
1415 upipe->u.iso.inuse -= nframes;
1416 xfer->actlen = actlen;
1417 xfer->status = USBD_NORMAL_COMPLETION;
1422 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1425 uhci_dump_tds(ii->stdstart);
1428 /* The transfer is done, compute actual length and status. */
1430 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1431 nstatus = le32toh(std->td.td_status);
1432 if (nstatus & UHCI_TD_ACTIVE)
1436 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1437 UHCI_TD_PID_SETUP) {
1438 actlen += UHCI_TD_GET_ACTLEN(status);
1441 * UHCI will report CRCTO in addition to a STALL or NAK
1442 * for a SETUP transaction. See section 3.2.2, "TD
1443 * CONTROL AND STATUS".
1445 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1446 status &= ~UHCI_TD_CRCTO;
1450 /* If there are left over TDs we need to update the toggle. */
1452 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1454 status &= UHCI_TD_ERROR;
1455 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1457 xfer->actlen = actlen;
1462 bitmask_snprintf((u_int32_t)status,
1463 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1464 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1465 sbuf, sizeof(sbuf));
1467 DPRINTFN((status == UHCI_TD_STALLED)*10,
1468 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1470 xfer->pipe->device->address,
1471 xfer->pipe->endpoint->edesc->bEndpointAddress,
1474 if (status == UHCI_TD_STALLED)
1475 xfer->status = USBD_STALLED;
1477 xfer->status = USBD_IOERROR; /* more info XXX */
1479 xfer->status = USBD_NORMAL_COMPLETION;
1483 usb_transfer_complete(xfer);
1484 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1488 * Called when a request does not complete.
1491 uhci_timeout(void *addr)
1493 uhci_intr_info_t *ii = addr;
1494 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1495 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1496 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1498 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1501 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1505 /* Execute the abort in a process context. */
1506 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer);
1507 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1511 uhci_timeout_task(void *addr)
1513 usbd_xfer_handle xfer = addr;
1516 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1519 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1524 * Wait here until controller claims to have an interrupt.
1525 * Then call uhci_intr and return. Use timeout to avoid waiting
1527 * Only used during boot when interrupts are not enabled yet.
1530 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1532 int timo = xfer->timeout;
1533 uhci_intr_info_t *ii;
1535 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1537 xfer->status = USBD_IN_PROGRESS;
1538 for (; timo >= 0; timo--) {
1539 usb_delay_ms(&sc->sc_bus, 1);
1540 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1541 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
1543 if (xfer->status != USBD_IN_PROGRESS)
1548 DPRINTF(("uhci_waitintr: timeout\n"));
1549 for (ii = LIST_FIRST(&sc->sc_intrhead);
1550 ii != NULL && ii->xfer != xfer;
1551 ii = LIST_NEXT(ii, list))
1555 panic("uhci_waitintr: lost intr_info");
1561 uhci_poll(struct usbd_bus *bus)
1563 uhci_softc_t *sc = (uhci_softc_t *)bus;
1565 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
1570 uhci_reset(uhci_softc_t *sc)
1574 UHCICMD(sc, UHCI_CMD_HCRESET);
1575 /* The reset bit goes low when the controller is done. */
1576 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1577 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1578 usb_delay_ms(&sc->sc_bus, 1);
1579 if (n >= UHCI_RESET_TIMEOUT)
1580 printf("%s: controller did not reset\n",
1581 USBDEVNAME(sc->sc_bus.bdev));
1585 uhci_run(uhci_softc_t *sc, int run)
1592 DPRINTF(("uhci_run: setting run=%d\n", run));
1593 cmd = UREAD2(sc, UHCI_CMD);
1597 cmd &= ~UHCI_CMD_RS;
1599 for(n = 0; n < 10; n++) {
1600 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1601 /* return when we've entered the state we want */
1602 if (run == running) {
1604 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1605 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1606 return (USBD_NORMAL_COMPLETION);
1608 usb_delay_ms(&sc->sc_bus, 1);
1611 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1612 run ? "start" : "stop");
1613 return (USBD_IOERROR);
1617 * Memory management routines.
1618 * uhci_alloc_std allocates TDs
1619 * uhci_alloc_sqh allocates QHs
1620 * These two routines do their own free list management,
1621 * partly for speed, partly because allocating DMAable memory
1622 * has page size granularaity so much memory would be wasted if
1623 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1627 uhci_alloc_std(uhci_softc_t *sc)
1629 uhci_soft_td_t *std;
1634 if (sc->sc_freetds == NULL) {
1635 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1636 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1637 UHCI_TD_ALIGN, &dma);
1640 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1641 offs = i * UHCI_STD_SIZE;
1642 std = KERNADDR(&dma, offs);
1643 std->physaddr = DMAADDR(&dma, offs);
1644 std->link.std = sc->sc_freetds;
1645 sc->sc_freetds = std;
1648 std = sc->sc_freetds;
1649 sc->sc_freetds = std->link.std;
1650 memset(&std->td, 0, sizeof(uhci_td_t));
1655 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1658 #define TD_IS_FREE 0x12345678
1659 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1660 printf("uhci_free_std: freeing free TD %p\n", std);
1663 std->td.td_token = htole32(TD_IS_FREE);
1665 std->link.std = sc->sc_freetds;
1666 sc->sc_freetds = std;
1670 uhci_alloc_sqh(uhci_softc_t *sc)
1672 uhci_soft_qh_t *sqh;
1677 if (sc->sc_freeqhs == NULL) {
1678 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1679 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1680 UHCI_QH_ALIGN, &dma);
1683 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1684 offs = i * UHCI_SQH_SIZE;
1685 sqh = KERNADDR(&dma, offs);
1686 sqh->physaddr = DMAADDR(&dma, offs);
1687 sqh->hlink = sc->sc_freeqhs;
1688 sc->sc_freeqhs = sqh;
1691 sqh = sc->sc_freeqhs;
1692 sc->sc_freeqhs = sqh->hlink;
1693 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1698 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1700 sqh->hlink = sc->sc_freeqhs;
1701 sc->sc_freeqhs = sqh;
1705 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1706 uhci_soft_td_t *stdend)
1710 for (; std != stdend; std = p) {
1712 uhci_free_std(sc, std);
1717 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1718 int rd, u_int16_t flags, usb_dma_t *dma,
1719 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1721 uhci_soft_td_t *p, *lastp;
1722 uhci_physaddr_t lastlink;
1723 int i, ntd, l, tog, maxp;
1725 int addr = upipe->pipe.device->address;
1726 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1728 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1729 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1730 upipe->pipe.device->speed, flags));
1731 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1733 printf("uhci_alloc_std_chain: maxp=0\n");
1734 return (USBD_INVAL);
1736 ntd = (len + maxp - 1) / maxp;
1737 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1739 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1742 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1743 return (USBD_NORMAL_COMPLETION);
1745 tog = upipe->nexttoggle;
1748 upipe->nexttoggle = tog ^ 1;
1750 lastlink = UHCI_PTR_T;
1752 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1753 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1754 status |= UHCI_TD_LS;
1755 if (flags & USBD_SHORT_XFER_OK)
1756 status |= UHCI_TD_SPD;
1757 for (i = ntd; i >= 0; i--) {
1758 p = uhci_alloc_std(sc);
1760 uhci_free_std_chain(sc, lastp, NULL);
1761 return (USBD_NOMEM);
1763 p->link.std = lastp;
1764 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1766 lastlink = p->physaddr;
1767 p->td.td_status = htole32(status);
1771 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1777 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1778 UHCI_TD_OUT(l, endpt, addr, tog));
1779 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1783 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1784 upipe->nexttoggle));
1785 return (USBD_NORMAL_COMPLETION);
1789 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1791 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1792 upipe->nexttoggle = 0;
1796 uhci_noop(usbd_pipe_handle pipe)
1801 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1805 /* Insert last in queue. */
1806 err = usb_insert_transfer(xfer);
1811 * Pipe isn't running (otherwise err would be USBD_INPROG),
1812 * so start it first.
1814 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1818 uhci_device_bulk_start(usbd_xfer_handle xfer)
1820 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1821 usbd_device_handle dev = upipe->pipe.device;
1822 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1823 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1824 uhci_soft_td_t *data, *dataend;
1825 uhci_soft_qh_t *sqh;
1827 int len, isread, endpt;
1830 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1831 xfer, xfer->length, xfer->flags, ii));
1834 return (USBD_IOERROR);
1837 if (xfer->rqflags & URQ_REQUEST)
1838 panic("uhci_device_bulk_transfer: a request");
1842 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1843 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1844 sqh = upipe->u.bulk.sqh;
1846 upipe->u.bulk.isread = isread;
1847 upipe->u.bulk.length = len;
1849 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1850 &xfer->dmabuf, &data, &dataend);
1853 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1856 if (uhcidebug > 8) {
1857 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1858 uhci_dump_tds(data);
1862 /* Set up interrupt info. */
1864 ii->stdstart = data;
1865 ii->stdend = dataend;
1868 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1874 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1877 uhci_add_bulk(sc, sqh);
1878 uhci_add_intr_info(sc, ii);
1880 if (xfer->timeout && !sc->sc_bus.use_polling) {
1881 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1884 xfer->status = USBD_IN_PROGRESS;
1888 if (uhcidebug > 10) {
1889 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1890 uhci_dump_tds(data);
1894 if (sc->sc_bus.use_polling)
1895 uhci_waitintr(sc, xfer);
1897 return (USBD_IN_PROGRESS);
1900 /* Abort a device bulk request. */
1902 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1904 DPRINTF(("uhci_device_bulk_abort:\n"));
1905 uhci_abort_xfer(xfer, USBD_CANCELLED);
1909 * Abort a device request.
1910 * If this routine is called at splusb() it guarantees that the request
1911 * will be removed from the hardware scheduling and that the callback
1912 * for it will be called with USBD_CANCELLED status.
1913 * It's impossible to guarantee that the requested transfer will not
1914 * have happened since the hardware runs concurrently.
1915 * If the transaction has already happened we rely on the ordinary
1916 * interrupt processing to process it.
1919 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1921 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1922 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1923 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1924 uhci_soft_td_t *std;
1927 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1930 /* If we're dying, just do the software part. */
1932 xfer->status = status; /* make software ignore it */
1933 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1934 usb_transfer_complete(xfer);
1939 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
1940 panic("uhci_abort_xfer: not in process context");
1943 * Step 1: Make interrupt routine and hardware ignore xfer.
1946 xfer->status = status; /* make software ignore it */
1947 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1948 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1949 for (std = ii->stdstart; std != NULL; std = std->link.std)
1950 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1954 * Step 2: Wait until we know hardware has finished any possible
1955 * use of the xfer. Also make sure the soft interrupt routine
1958 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1960 #ifdef USB_USE_SOFTINTR
1961 sc->sc_softwake = 1;
1962 #endif /* USB_USE_SOFTINTR */
1963 usb_schedsoftintr(&sc->sc_bus);
1964 #ifdef USB_USE_SOFTINTR
1965 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1966 tsleep(&sc->sc_softwake, 0, "uhciab", 0);
1967 #endif /* USB_USE_SOFTINTR */
1971 * Step 3: Execute callback.
1975 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1981 usb_transfer_complete(xfer);
1985 /* Close a device bulk pipe. */
1987 uhci_device_bulk_close(usbd_pipe_handle pipe)
1989 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1990 usbd_device_handle dev = upipe->pipe.device;
1991 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1993 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1997 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
2001 /* Insert last in queue. */
2002 err = usb_insert_transfer(xfer);
2007 * Pipe isn't running (otherwise err would be USBD_INPROG),
2008 * so start it first.
2010 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2014 uhci_device_ctrl_start(usbd_xfer_handle xfer)
2016 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2020 return (USBD_IOERROR);
2023 if (!(xfer->rqflags & URQ_REQUEST))
2024 panic("uhci_device_ctrl_transfer: not a request");
2027 err = uhci_device_request(xfer);
2031 if (sc->sc_bus.use_polling)
2032 uhci_waitintr(sc, xfer);
2033 return (USBD_IN_PROGRESS);
2037 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2041 /* Insert last in queue. */
2042 err = usb_insert_transfer(xfer);
2047 * Pipe isn't running (otherwise err would be USBD_INPROG),
2048 * so start it first.
2050 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2054 uhci_device_intr_start(usbd_xfer_handle xfer)
2056 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2057 usbd_device_handle dev = upipe->pipe.device;
2058 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2059 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2060 uhci_soft_td_t *data, *dataend;
2061 uhci_soft_qh_t *sqh;
2067 return (USBD_IOERROR);
2069 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2070 xfer, xfer->length, xfer->flags));
2073 if (xfer->rqflags & URQ_REQUEST)
2074 panic("uhci_device_intr_transfer: a request");
2077 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2078 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2079 sqh = upipe->u.bulk.sqh;
2081 upipe->u.intr.isread = isread;
2083 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread,
2084 xfer->flags, &xfer->dmabuf, &data,
2088 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2091 if (uhcidebug > 10) {
2092 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2093 uhci_dump_tds(data);
2094 uhci_dump_qh(upipe->u.intr.qhs[0]);
2099 /* Set up interrupt info. */
2101 ii->stdstart = data;
2102 ii->stdend = dataend;
2105 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2110 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2111 upipe->u.intr.qhs[0]));
2112 for (i = 0; i < upipe->u.intr.npoll; i++) {
2113 sqh = upipe->u.intr.qhs[i];
2115 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2117 uhci_add_intr_info(sc, ii);
2118 xfer->status = USBD_IN_PROGRESS;
2122 if (uhcidebug > 10) {
2123 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2124 uhci_dump_tds(data);
2125 uhci_dump_qh(upipe->u.intr.qhs[0]);
2129 return (USBD_IN_PROGRESS);
2132 /* Abort a device control request. */
2134 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2136 DPRINTF(("uhci_device_ctrl_abort:\n"));
2137 uhci_abort_xfer(xfer, USBD_CANCELLED);
2140 /* Close a device control pipe. */
2142 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2146 /* Abort a device interrupt request. */
2148 uhci_device_intr_abort(usbd_xfer_handle xfer)
2150 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2151 if (xfer->pipe->intrxfer == xfer) {
2152 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2153 xfer->pipe->intrxfer = NULL;
2155 uhci_abort_xfer(xfer, USBD_CANCELLED);
2158 /* Close a device interrupt pipe. */
2160 uhci_device_intr_close(usbd_pipe_handle pipe)
2162 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2163 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2167 /* Unlink descriptors from controller data structures. */
2168 npoll = upipe->u.intr.npoll;
2170 for (i = 0; i < npoll; i++)
2171 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2175 * We now have to wait for any activity on the physical
2176 * descriptors to stop.
2178 usb_delay_ms(&sc->sc_bus, 2);
2180 for(i = 0; i < npoll; i++)
2181 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2182 free(upipe->u.intr.qhs, M_USBHC);
2184 /* XXX free other resources */
2188 uhci_device_request(usbd_xfer_handle xfer)
2190 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2191 usb_device_request_t *req = &xfer->request;
2192 usbd_device_handle dev = upipe->pipe.device;
2193 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2194 int addr = dev->address;
2195 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2196 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2197 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2198 uhci_soft_qh_t *sqh;
2205 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2206 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2207 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2208 UGETW(req->wIndex), UGETW(req->wLength),
2211 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2212 isread = req->bmRequestType & UT_READ;
2213 len = UGETW(req->wLength);
2215 setup = upipe->u.ctl.setup;
2216 stat = upipe->u.ctl.stat;
2217 sqh = upipe->u.ctl.sqh;
2219 /* Set up data transaction */
2221 upipe->nexttoggle = 1;
2222 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2223 &xfer->dmabuf, &data, &dataend);
2227 dataend->link.std = stat;
2228 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2232 upipe->u.ctl.length = len;
2234 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2236 setup->link.std = next;
2237 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2238 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2240 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2241 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2242 stat->link.std = NULL;
2243 stat->td.td_link = htole32(UHCI_PTR_T);
2244 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2245 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2248 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2249 UHCI_TD_IN (0, endpt, addr, 1));
2250 stat->td.td_buffer = htole32(0);
2253 if (uhcidebug > 10) {
2254 DPRINTF(("uhci_device_request: before transfer\n"));
2255 uhci_dump_tds(setup);
2259 /* Set up interrupt info. */
2261 ii->stdstart = setup;
2265 printf("uhci_device_request: not done, ii=%p\n", ii);
2271 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2274 if (dev->speed == USB_SPEED_LOW)
2275 uhci_add_ls_ctrl(sc, sqh);
2277 uhci_add_hs_ctrl(sc, sqh);
2278 uhci_add_intr_info(sc, ii);
2280 if (uhcidebug > 12) {
2281 uhci_soft_td_t *std;
2282 uhci_soft_qh_t *xqh;
2283 uhci_soft_qh_t *sxqh;
2285 uhci_physaddr_t link;
2286 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2287 for (std = sc->sc_vframes[0].htd, link = 0;
2288 (link & UHCI_PTR_QH) == 0;
2289 std = std->link.std) {
2290 link = le32toh(std->td.td_link);
2293 sxqh = (uhci_soft_qh_t *)std;
2297 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2298 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2301 DPRINTF(("Enqueued QH:\n"));
2303 uhci_dump_tds(sqh->elink);
2306 if (xfer->timeout && !sc->sc_bus.use_polling) {
2307 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2310 xfer->status = USBD_IN_PROGRESS;
2313 return (USBD_NORMAL_COMPLETION);
2317 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2321 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2323 /* Put it on our queue, */
2324 err = usb_insert_transfer(xfer);
2326 /* bail out on error, */
2327 if (err && err != USBD_IN_PROGRESS)
2330 /* XXX should check inuse here */
2332 /* insert into schedule, */
2333 uhci_device_isoc_enter(xfer);
2335 /* and start if the pipe wasn't running */
2337 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2343 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2345 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2346 usbd_device_handle dev = upipe->pipe.device;
2347 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2348 struct iso *iso = &upipe->u.iso;
2349 uhci_soft_td_t *std;
2350 u_int32_t buf, len, status;
2351 int s, i, next, nframes;
2353 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2355 iso->inuse, iso->next, xfer, xfer->nframes));
2360 if (xfer->status == USBD_IN_PROGRESS) {
2361 /* This request has already been entered into the frame list */
2362 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2367 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2368 printf("uhci_device_isoc_enter: overflow!\n");
2373 /* Not in use yet, schedule it a few frames ahead. */
2374 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2375 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2378 xfer->status = USBD_IN_PROGRESS;
2379 UXFER(xfer)->curframe = next;
2381 buf = DMAADDR(&xfer->dmabuf, 0);
2382 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2385 nframes = xfer->nframes;
2387 for (i = 0; i < nframes; i++) {
2388 std = iso->stds[next];
2389 if (++next >= UHCI_VFRAMELIST_COUNT)
2391 len = xfer->frlengths[i];
2392 std->td.td_buffer = htole32(buf);
2393 if (i == nframes - 1)
2394 status |= UHCI_TD_IOC;
2395 std->td.td_status = htole32(status);
2396 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2397 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2399 if (uhcidebug > 5) {
2400 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2407 iso->inuse += xfer->nframes;
2413 uhci_device_isoc_start(usbd_xfer_handle xfer)
2415 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2416 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2417 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2418 uhci_soft_td_t *end;
2421 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2424 return (USBD_IOERROR);
2427 if (xfer->status != USBD_IN_PROGRESS)
2428 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2431 /* Find the last TD */
2432 i = UXFER(xfer)->curframe + xfer->nframes;
2433 if (i >= UHCI_VFRAMELIST_COUNT)
2434 i -= UHCI_VFRAMELIST_COUNT;
2435 end = upipe->u.iso.stds[i];
2439 printf("uhci_device_isoc_start: end == NULL\n");
2440 return (USBD_INVAL);
2446 /* Set up interrupt info. */
2452 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2455 uhci_add_intr_info(sc, ii);
2459 return (USBD_IN_PROGRESS);
2463 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2465 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2466 uhci_soft_td_t **stds = upipe->u.iso.stds;
2467 uhci_soft_td_t *std;
2468 int i, n, s, nframes, maxlen, len;
2472 /* Transfer is already done. */
2473 if (xfer->status != USBD_NOT_STARTED &&
2474 xfer->status != USBD_IN_PROGRESS) {
2479 /* Give xfer the requested abort code. */
2480 xfer->status = USBD_CANCELLED;
2482 /* make hardware ignore it, */
2483 nframes = xfer->nframes;
2484 n = UXFER(xfer)->curframe;
2486 for (i = 0; i < nframes; i++) {
2488 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2489 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2492 if (++n >= UHCI_VFRAMELIST_COUNT)
2496 /* and wait until we are sure the hardware has finished. */
2500 UXFER(xfer)->iinfo.isdone = 1;
2502 /* Run callback and remove from interrupt list. */
2503 usb_transfer_complete(xfer);
2509 uhci_device_isoc_close(usbd_pipe_handle pipe)
2511 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2512 usbd_device_handle dev = upipe->pipe.device;
2513 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2514 uhci_soft_td_t *std, *vstd;
2519 * Make sure all TDs are marked as inactive.
2520 * Wait for completion.
2524 iso = &upipe->u.iso;
2526 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2527 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2528 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2531 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2533 for (vstd = sc->sc_vframes[i].htd;
2534 vstd != NULL && vstd->link.std != std;
2535 vstd = vstd->link.std)
2539 printf("uhci_device_isoc_close: %p not found\n", std);
2543 vstd->link = std->link;
2544 vstd->td.td_link = std->td.td_link;
2545 uhci_free_std(sc, std);
2549 free(iso->stds, M_USBHC);
2553 uhci_setup_isoc(usbd_pipe_handle pipe)
2555 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2556 usbd_device_handle dev = upipe->pipe.device;
2557 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2558 int addr = upipe->pipe.device->address;
2559 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2560 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2561 uhci_soft_td_t *std, *vstd;
2566 iso = &upipe->u.iso;
2567 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2568 M_USBHC, M_INTWAIT);
2570 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2571 UHCI_TD_OUT(0, endpt, addr, 0);
2573 /* Allocate the TDs and mark as inactive; */
2574 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2575 std = uhci_alloc_std(sc);
2578 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2579 std->td.td_token = htole32(token);
2583 /* Insert TDs into schedule. */
2585 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2587 vstd = sc->sc_vframes[i].htd;
2588 std->link = vstd->link;
2589 std->td.td_link = vstd->td.td_link;
2590 vstd->link.std = std;
2591 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2598 return (USBD_NORMAL_COMPLETION);
2602 uhci_free_std(sc, iso->stds[i]);
2603 free(iso->stds, M_USBHC);
2604 return (USBD_NOMEM);
2608 uhci_device_isoc_done(usbd_xfer_handle xfer)
2610 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2612 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2614 if (ii->xfer != xfer)
2615 /* Not on interrupt list, ignore it. */
2618 if (!uhci_active_intr_info(ii))
2622 if (xfer->busy_free != XFER_BUSY) {
2623 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2624 xfer, xfer->busy_free);
2628 if (ii->stdend == NULL) {
2629 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2637 /* Turn off the interrupt since it is active even if the TD is not. */
2638 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2640 uhci_del_intr_info(ii); /* remove from active list */
2643 if (ii->stdend == NULL) {
2644 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2654 uhci_device_intr_done(usbd_xfer_handle xfer)
2656 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2657 uhci_softc_t *sc = ii->sc;
2658 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2659 uhci_soft_qh_t *sqh;
2662 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2664 npoll = upipe->u.intr.npoll;
2665 for(i = 0; i < npoll; i++) {
2666 sqh = upipe->u.intr.qhs[i];
2668 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2670 uhci_free_std_chain(sc, ii->stdstart, NULL);
2673 if (xfer->pipe->repeat) {
2674 uhci_soft_td_t *data, *dataend;
2676 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2678 /* This alloc cannot fail since we freed the chain above. */
2679 uhci_alloc_std_chain(upipe, sc, xfer->length,
2680 upipe->u.intr.isread, xfer->flags,
2681 &xfer->dmabuf, &data, &dataend);
2682 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2685 if (uhcidebug > 10) {
2686 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2687 uhci_dump_tds(data);
2688 uhci_dump_qh(upipe->u.intr.qhs[0]);
2692 ii->stdstart = data;
2693 ii->stdend = dataend;
2696 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2700 for (i = 0; i < npoll; i++) {
2701 sqh = upipe->u.intr.qhs[i];
2703 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2705 xfer->status = USBD_IN_PROGRESS;
2706 /* The ii is already on the examined list, just leave it. */
2708 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2709 if (uhci_active_intr_info(ii))
2710 uhci_del_intr_info(ii);
2714 /* Deallocate request data structures */
2716 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2718 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2719 uhci_softc_t *sc = ii->sc;
2720 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2723 if (!(xfer->rqflags & URQ_REQUEST))
2724 panic("uhci_device_ctrl_done: not a request");
2727 if (!uhci_active_intr_info(ii))
2730 uhci_del_intr_info(ii); /* remove from active list */
2732 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2733 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2735 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2737 if (upipe->u.ctl.length != 0)
2738 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2740 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2743 /* Deallocate request data structures */
2745 uhci_device_bulk_done(usbd_xfer_handle xfer)
2747 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2748 uhci_softc_t *sc = ii->sc;
2749 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2751 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2752 xfer, ii, sc, upipe));
2754 if (!uhci_active_intr_info(ii))
2757 uhci_del_intr_info(ii); /* remove from active list */
2759 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2761 uhci_free_std_chain(sc, ii->stdstart, NULL);
2763 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2766 /* Add interrupt QH, called with vflock. */
2768 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2770 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2771 uhci_soft_qh_t *eqh;
2773 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2776 sqh->hlink = eqh->hlink;
2777 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2779 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2784 /* Remove interrupt QH. */
2786 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2788 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2789 uhci_soft_qh_t *pqh;
2791 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2793 /* See comment in uhci_remove_ctrl() */
2794 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2795 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2796 delay(UHCI_QH_REMOVE_DELAY);
2799 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2800 pqh->hlink = sqh->hlink;
2801 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2802 delay(UHCI_QH_REMOVE_DELAY);
2809 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2811 uhci_soft_qh_t *sqh;
2813 u_int bestbw, bw, bestoffs, offs;
2815 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2817 printf("uhci_setintr: 0 interval\n");
2818 return (USBD_INVAL);
2821 if (ival > UHCI_VFRAMELIST_COUNT)
2822 ival = UHCI_VFRAMELIST_COUNT;
2823 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2824 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2826 upipe->u.intr.npoll = npoll;
2828 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_INTWAIT);
2831 * Figure out which offset in the schedule that has most
2832 * bandwidth left over.
2834 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2835 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2836 for (bw = i = 0; i < npoll; i++)
2837 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2843 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2845 for(i = 0; i < npoll; i++) {
2846 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2848 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2849 sqh->pos = MOD(i * ival + bestoffs);
2854 /* Enter QHs into the controller data structures. */
2855 for(i = 0; i < npoll; i++)
2856 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2859 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2860 return (USBD_NORMAL_COMPLETION);
2863 /* Open a new pipe. */
2865 uhci_open(usbd_pipe_handle pipe)
2867 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2868 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2869 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2873 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2874 pipe, pipe->device->address,
2875 ed->bEndpointAddress, sc->sc_addr));
2877 upipe->aborting = 0;
2878 upipe->nexttoggle = 0;
2880 if (pipe->device->address == sc->sc_addr) {
2881 switch (ed->bEndpointAddress) {
2882 case USB_CONTROL_ENDPOINT:
2883 pipe->methods = &uhci_root_ctrl_methods;
2885 case UE_DIR_IN | UHCI_INTR_ENDPT:
2886 pipe->methods = &uhci_root_intr_methods;
2889 return (USBD_INVAL);
2892 switch (ed->bmAttributes & UE_XFERTYPE) {
2894 pipe->methods = &uhci_device_ctrl_methods;
2895 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2896 if (upipe->u.ctl.sqh == NULL)
2898 upipe->u.ctl.setup = uhci_alloc_std(sc);
2899 if (upipe->u.ctl.setup == NULL) {
2900 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2903 upipe->u.ctl.stat = uhci_alloc_std(sc);
2904 if (upipe->u.ctl.stat == NULL) {
2905 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2906 uhci_free_std(sc, upipe->u.ctl.setup);
2909 err = usb_allocmem(&sc->sc_bus,
2910 sizeof(usb_device_request_t),
2911 0, &upipe->u.ctl.reqdma);
2913 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2914 uhci_free_std(sc, upipe->u.ctl.setup);
2915 uhci_free_std(sc, upipe->u.ctl.stat);
2920 pipe->methods = &uhci_device_intr_methods;
2921 ival = pipe->interval;
2922 if (ival == USBD_DEFAULT_INTERVAL)
2923 ival = ed->bInterval;
2924 return (uhci_device_setintr(sc, upipe, ival));
2925 case UE_ISOCHRONOUS:
2926 pipe->methods = &uhci_device_isoc_methods;
2927 return (uhci_setup_isoc(pipe));
2929 pipe->methods = &uhci_device_bulk_methods;
2930 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2931 if (upipe->u.bulk.sqh == NULL)
2936 return (USBD_NORMAL_COMPLETION);
2939 return (USBD_NOMEM);
2943 * Data structures and routines to emulate the root hub.
2945 usb_device_descriptor_t uhci_devd = {
2946 USB_DEVICE_DESCRIPTOR_SIZE,
2947 UDESC_DEVICE, /* type */
2948 {0x00, 0x01}, /* USB version */
2949 UDCLASS_HUB, /* class */
2950 UDSUBCLASS_HUB, /* subclass */
2951 UDPROTO_FSHUB, /* protocol */
2952 64, /* max packet */
2953 {0},{0},{0x00,0x01}, /* device id */
2954 1,2,0, /* string indicies */
2955 1 /* # of configurations */
2958 usb_config_descriptor_t uhci_confd = {
2959 USB_CONFIG_DESCRIPTOR_SIZE,
2961 {USB_CONFIG_DESCRIPTOR_SIZE +
2962 USB_INTERFACE_DESCRIPTOR_SIZE +
2963 USB_ENDPOINT_DESCRIPTOR_SIZE},
2971 usb_interface_descriptor_t uhci_ifcd = {
2972 USB_INTERFACE_DESCRIPTOR_SIZE,
2983 usb_endpoint_descriptor_t uhci_endpd = {
2984 USB_ENDPOINT_DESCRIPTOR_SIZE,
2986 UE_DIR_IN | UHCI_INTR_ENDPT,
2992 usb_hub_descriptor_t uhci_hubd_piix = {
2993 USB_HUB_DESCRIPTOR_SIZE,
2996 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2997 50, /* power on to power good */
2999 { 0x00 }, /* both ports are removable */
3003 uhci_str(usb_string_descriptor_t *p, int l, char *s)
3009 p->bLength = 2 * strlen(s) + 2;
3012 p->bDescriptorType = UDESC_STRING;
3014 for (i = 0; s[i] && l > 1; i++, l -= 2)
3015 USETW2(p->bString[i], 0, s[i]);
3020 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3021 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3022 * should not be used by the USB subsystem. As we cannot issue a
3023 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3024 * will be enabled as part of the reset.
3026 * On the VT83C572, the port cannot be successfully enabled until the
3027 * outstanding "port enable change" and "connection status change"
3028 * events have been reset.
3031 uhci_portreset(uhci_softc_t *sc, int index)
3036 port = UHCI_PORTSC1;
3037 else if (index == 2)
3038 port = UHCI_PORTSC2;
3040 return (USBD_IOERROR);
3042 x = URWMASK(UREAD2(sc, port));
3043 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3045 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3047 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3048 index, UREAD2(sc, port)));
3050 x = URWMASK(UREAD2(sc, port));
3051 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3055 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3056 index, UREAD2(sc, port)));
3058 x = URWMASK(UREAD2(sc, port));
3059 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3061 for (lim = 10; --lim > 0;) {
3062 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3064 x = UREAD2(sc, port);
3066 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3069 if (!(x & UHCI_PORTSC_CCS)) {
3071 * No device is connected (or was disconnected
3072 * during reset). Consider the port reset.
3073 * The delay must be long enough to ensure on
3074 * the initial iteration that the device
3075 * connection will have been registered. 50ms
3076 * appears to be sufficient, but 20ms is not.
3078 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3083 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3085 * Port enabled changed and/or connection
3086 * status changed were set. Reset either or
3087 * both raised flags (by writing a 1 to that
3088 * bit), and wait again for state to settle.
3090 UWRITE2(sc, port, URWMASK(x) |
3091 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3095 if (x & UHCI_PORTSC_PE)
3096 /* Port is enabled */
3099 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3102 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3103 index, UREAD2(sc, port)));
3106 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3107 return (USBD_TIMEOUT);
3111 return (USBD_NORMAL_COMPLETION);
3115 * Simulate a hardware hub by handling all the necessary requests.
3118 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3122 /* Insert last in queue. */
3123 err = usb_insert_transfer(xfer);
3128 * Pipe isn't running (otherwise err would be USBD_INPROG),
3129 * so start it first.
3131 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3135 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3137 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3138 usb_device_request_t *req;
3141 int s, len, value, index, status, change, l, totlen = 0;
3142 usb_port_status_t ps;
3146 return (USBD_IOERROR);
3149 if (!(xfer->rqflags & URQ_REQUEST))
3150 panic("uhci_root_ctrl_transfer: not a request");
3152 req = &xfer->request;
3154 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3155 req->bmRequestType, req->bRequest));
3157 len = UGETW(req->wLength);
3158 value = UGETW(req->wValue);
3159 index = UGETW(req->wIndex);
3162 buf = KERNADDR(&xfer->dmabuf, 0);
3164 #define C(x,y) ((x) | ((y) << 8))
3165 switch(C(req->bRequest, req->bmRequestType)) {
3166 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3167 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3168 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3170 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3171 * for the integrated root hub.
3174 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3176 *(u_int8_t *)buf = sc->sc_conf;
3180 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3181 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3182 switch(value >> 8) {
3184 if ((value & 0xff) != 0) {
3188 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3189 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3190 memcpy(buf, &uhci_devd, l);
3193 if ((value & 0xff) != 0) {
3197 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3198 memcpy(buf, &uhci_confd, l);
3199 buf = (char *)buf + l;
3201 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3203 memcpy(buf, &uhci_ifcd, l);
3204 buf = (char *)buf + l;
3206 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3208 memcpy(buf, &uhci_endpd, l);
3213 *(u_int8_t *)buf = 0;
3215 switch (value & 0xff) {
3216 case 1: /* Vendor */
3217 totlen = uhci_str(buf, len, sc->sc_vendor);
3219 case 2: /* Product */
3220 totlen = uhci_str(buf, len, "UHCI root hub");
3229 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3231 *(u_int8_t *)buf = 0;
3235 case C(UR_GET_STATUS, UT_READ_DEVICE):
3237 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3241 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3242 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3244 USETW(((usb_status_t *)buf)->wStatus, 0);
3248 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3249 if (value >= USB_MAX_DEVICES) {
3253 sc->sc_addr = value;
3255 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3256 if (value != 0 && value != 1) {
3260 sc->sc_conf = value;
3262 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3264 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3265 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3266 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3269 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3271 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3274 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3276 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3277 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3278 "port=%d feature=%d\n",
3281 port = UHCI_PORTSC1;
3282 else if (index == 2)
3283 port = UHCI_PORTSC2;
3289 case UHF_PORT_ENABLE:
3290 x = URWMASK(UREAD2(sc, port));
3291 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3293 case UHF_PORT_SUSPEND:
3294 x = URWMASK(UREAD2(sc, port));
3295 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3297 case UHF_PORT_RESET:
3298 x = URWMASK(UREAD2(sc, port));
3299 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3301 case UHF_C_PORT_CONNECTION:
3302 x = URWMASK(UREAD2(sc, port));
3303 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3305 case UHF_C_PORT_ENABLE:
3306 x = URWMASK(UREAD2(sc, port));
3307 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3309 case UHF_C_PORT_OVER_CURRENT:
3310 x = URWMASK(UREAD2(sc, port));
3311 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3313 case UHF_C_PORT_RESET:
3315 err = USBD_NORMAL_COMPLETION;
3317 case UHF_PORT_CONNECTION:
3318 case UHF_PORT_OVER_CURRENT:
3319 case UHF_PORT_POWER:
3320 case UHF_PORT_LOW_SPEED:
3321 case UHF_C_PORT_SUSPEND:
3327 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3329 port = UHCI_PORTSC1;
3330 else if (index == 2)
3331 port = UHCI_PORTSC2;
3338 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3339 UHCI_PORTSC_LS_SHIFT;
3343 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3348 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3350 memcpy(buf, &uhci_hubd_piix, l);
3352 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3357 memset(buf, 0, len);
3360 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3362 port = UHCI_PORTSC1;
3363 else if (index == 2)
3364 port = UHCI_PORTSC2;
3373 x = UREAD2(sc, port);
3374 status = change = 0;
3375 if (x & UHCI_PORTSC_CCS)
3376 status |= UPS_CURRENT_CONNECT_STATUS;
3377 if (x & UHCI_PORTSC_CSC)
3378 change |= UPS_C_CONNECT_STATUS;
3379 if (x & UHCI_PORTSC_PE)
3380 status |= UPS_PORT_ENABLED;
3381 if (x & UHCI_PORTSC_POEDC)
3382 change |= UPS_C_PORT_ENABLED;
3383 if (x & UHCI_PORTSC_OCI)
3384 status |= UPS_OVERCURRENT_INDICATOR;
3385 if (x & UHCI_PORTSC_OCIC)
3386 change |= UPS_C_OVERCURRENT_INDICATOR;
3387 if (x & UHCI_PORTSC_SUSP)
3388 status |= UPS_SUSPEND;
3389 if (x & UHCI_PORTSC_LSDA)
3390 status |= UPS_LOW_SPEED;
3391 status |= UPS_PORT_POWER;
3393 change |= UPS_C_PORT_RESET;
3394 USETW(ps.wPortStatus, status);
3395 USETW(ps.wPortChange, change);
3396 l = min(len, sizeof ps);
3397 memcpy(buf, &ps, l);
3400 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3403 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3405 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3407 port = UHCI_PORTSC1;
3408 else if (index == 2)
3409 port = UHCI_PORTSC2;
3415 case UHF_PORT_ENABLE:
3416 x = URWMASK(UREAD2(sc, port));
3417 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3419 case UHF_PORT_SUSPEND:
3420 x = URWMASK(UREAD2(sc, port));
3421 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3423 case UHF_PORT_RESET:
3424 err = uhci_portreset(sc, index);
3426 case UHF_PORT_POWER:
3427 /* Pretend we turned on power */
3428 err = USBD_NORMAL_COMPLETION;
3430 case UHF_C_PORT_CONNECTION:
3431 case UHF_C_PORT_ENABLE:
3432 case UHF_C_PORT_OVER_CURRENT:
3433 case UHF_PORT_CONNECTION:
3434 case UHF_PORT_OVER_CURRENT:
3435 case UHF_PORT_LOW_SPEED:
3436 case UHF_C_PORT_SUSPEND:
3437 case UHF_C_PORT_RESET:
3447 xfer->actlen = totlen;
3448 err = USBD_NORMAL_COMPLETION;
3452 usb_transfer_complete(xfer);
3454 return (USBD_IN_PROGRESS);
3457 /* Abort a root control request. */
3459 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3461 /* Nothing to do, all transfers are synchronous. */
3464 /* Close the root pipe. */
3466 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3468 DPRINTF(("uhci_root_ctrl_close\n"));
3471 /* Abort a root interrupt request. */
3473 uhci_root_intr_abort(usbd_xfer_handle xfer)
3475 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3477 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3478 sc->sc_intr_xfer = NULL;
3480 if (xfer->pipe->intrxfer == xfer) {
3481 DPRINTF(("uhci_root_intr_abort: remove\n"));
3482 xfer->pipe->intrxfer = 0;
3484 xfer->status = USBD_CANCELLED;
3486 UXFER(xfer)->iinfo.isdone = 1;
3488 usb_transfer_complete(xfer);
3492 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3496 /* Insert last in queue. */
3497 err = usb_insert_transfer(xfer);
3502 * Pipe isn't running (otherwise err would be USBD_INPROG),
3503 * so start it first.
3505 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3508 /* Start a transfer on the root interrupt pipe */
3510 uhci_root_intr_start(usbd_xfer_handle xfer)
3512 usbd_pipe_handle pipe = xfer->pipe;
3513 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3515 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3516 xfer, xfer->length, xfer->flags));
3519 return (USBD_IOERROR);
3521 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3522 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3523 sc->sc_intr_xfer = xfer;
3524 return (USBD_IN_PROGRESS);
3527 /* Close the root interrupt pipe. */
3529 uhci_root_intr_close(usbd_pipe_handle pipe)
3531 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3533 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3534 sc->sc_intr_xfer = NULL;
3535 DPRINTF(("uhci_root_intr_close\n"));