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.17 2006/10/25 20:55:52 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 #if defined(DIAGNOSTIC) && defined(__i386__)
75 #include <machine/cpu.h>
79 #include <sys/queue.h>
80 #include <sys/sysctl.h>
81 #include <sys/thread2.h>
83 #include <machine/endian.h>
89 #include "usb_quirks.h"
94 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
95 /*#define UHCI_CTL_LOOP */
97 #if defined(__FreeBSD__) || defined(__DragonFly__)
98 #include <machine/clock.h>
100 #define delay(d) DELAY(d)
103 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
105 #if defined(__OpenBSD__)
106 struct cfdriver uhci_cd = {
107 NULL, "uhci", DV_DULL
113 #define DPRINTF(x) if (uhcidebug) printf x
114 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
117 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
118 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
119 &uhcidebug, 0, "uhci debug level");
120 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
121 &uhcinoloop, 0, "uhci noloop");
123 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
127 #define DPRINTFN(n,x)
131 * The UHCI controller is little endian, so on big endian machines
132 * the data strored in memory needs to be swapped.
134 #if defined(__OpenBSD__)
135 #if BYTE_ORDER == BIG_ENDIAN
136 #define htole32(x) (bswap32(x))
137 #define le32toh(x) (bswap32(x))
139 #define htole32(x) (x)
140 #define le32toh(x) (x)
145 struct usbd_pipe pipe;
149 usbd_xfer_handle abortstart, abortend;
151 /* Info needed for different pipe kinds. */
157 uhci_soft_td_t *setup, *stat;
164 uhci_soft_qh_t **qhs;
174 uhci_soft_td_t **stds;
180 Static void uhci_globalreset(uhci_softc_t *);
181 Static usbd_status uhci_portreset(uhci_softc_t*, int);
182 Static void uhci_reset(uhci_softc_t *);
183 #if defined(__NetBSD__) || defined(__OpenBSD__)
184 Static void uhci_shutdown(void *v);
185 Static void uhci_power(int, void *);
187 Static usbd_status uhci_run(uhci_softc_t *, int run);
188 Static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
189 Static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
190 Static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
191 Static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
193 Static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
195 Static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
198 Static void uhci_free_std_chain(uhci_softc_t *,
199 uhci_soft_td_t *, uhci_soft_td_t *);
200 Static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
201 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
202 uhci_soft_td_t **, uhci_soft_td_t **);
203 Static void uhci_poll_hub(void *);
204 Static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
205 Static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
206 Static void uhci_idone(uhci_intr_info_t *);
208 Static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
210 Static void uhci_timeout(void *);
211 Static void uhci_timeout_task(void *);
212 Static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
213 Static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
214 Static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
215 Static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
216 Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
217 Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
218 Static int uhci_str(usb_string_descriptor_t *, int, char *);
219 Static void uhci_add_loop(uhci_softc_t *sc);
220 Static void uhci_rem_loop(uhci_softc_t *sc);
222 Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
223 Static void uhci_device_isoc_enter(usbd_xfer_handle);
225 Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
226 Static void uhci_freem(struct usbd_bus *, usb_dma_t *);
228 Static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
229 Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
231 Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
232 Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
233 Static void uhci_device_ctrl_abort(usbd_xfer_handle);
234 Static void uhci_device_ctrl_close(usbd_pipe_handle);
235 Static void uhci_device_ctrl_done(usbd_xfer_handle);
237 Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
238 Static usbd_status uhci_device_intr_start(usbd_xfer_handle);
239 Static void uhci_device_intr_abort(usbd_xfer_handle);
240 Static void uhci_device_intr_close(usbd_pipe_handle);
241 Static void uhci_device_intr_done(usbd_xfer_handle);
243 Static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
244 Static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
245 Static void uhci_device_bulk_abort(usbd_xfer_handle);
246 Static void uhci_device_bulk_close(usbd_pipe_handle);
247 Static void uhci_device_bulk_done(usbd_xfer_handle);
249 Static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
250 Static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
251 Static void uhci_device_isoc_abort(usbd_xfer_handle);
252 Static void uhci_device_isoc_close(usbd_pipe_handle);
253 Static void uhci_device_isoc_done(usbd_xfer_handle);
255 Static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
256 Static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
257 Static void uhci_root_ctrl_abort(usbd_xfer_handle);
258 Static void uhci_root_ctrl_close(usbd_pipe_handle);
259 Static void uhci_root_ctrl_done(usbd_xfer_handle);
261 Static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
262 Static usbd_status uhci_root_intr_start(usbd_xfer_handle);
263 Static void uhci_root_intr_abort(usbd_xfer_handle);
264 Static void uhci_root_intr_close(usbd_pipe_handle);
265 Static void uhci_root_intr_done(usbd_xfer_handle);
267 Static usbd_status uhci_open(usbd_pipe_handle);
268 Static void uhci_poll(struct usbd_bus *);
269 Static void uhci_softintr(void *);
271 Static usbd_status uhci_device_request(usbd_xfer_handle xfer);
273 Static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
274 Static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
275 Static usbd_status uhci_device_setintr(uhci_softc_t *sc,
276 struct uhci_pipe *pipe, int ival);
278 Static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
279 Static void uhci_noop(usbd_pipe_handle pipe);
281 Static __inline__ uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
285 Static void uhci_dump_all(uhci_softc_t *);
286 Static void uhci_dumpregs(uhci_softc_t *);
287 Static void uhci_dump_qhs(uhci_soft_qh_t *);
288 Static void uhci_dump_qh(uhci_soft_qh_t *);
289 Static void uhci_dump_tds(uhci_soft_td_t *);
290 Static void uhci_dump_td(uhci_soft_td_t *);
291 Static void uhci_dump_ii(uhci_intr_info_t *ii);
292 void uhci_dump(void);
295 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
296 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
297 #define UWRITE1(sc, r, x) \
298 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
299 } while (/*CONSTCOND*/0)
300 #define UWRITE2(sc, r, x) \
301 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
302 } while (/*CONSTCOND*/0)
303 #define UWRITE4(sc, r, x) \
304 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
305 } while (/*CONSTCOND*/0)
306 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
307 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
308 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
310 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
311 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
313 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
315 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
317 #define UHCI_INTR_ENDPT 1
319 struct usbd_bus_methods uhci_bus_methods = {
329 struct usbd_pipe_methods uhci_root_ctrl_methods = {
330 uhci_root_ctrl_transfer,
331 uhci_root_ctrl_start,
332 uhci_root_ctrl_abort,
333 uhci_root_ctrl_close,
338 struct usbd_pipe_methods uhci_root_intr_methods = {
339 uhci_root_intr_transfer,
340 uhci_root_intr_start,
341 uhci_root_intr_abort,
342 uhci_root_intr_close,
347 struct usbd_pipe_methods uhci_device_ctrl_methods = {
348 uhci_device_ctrl_transfer,
349 uhci_device_ctrl_start,
350 uhci_device_ctrl_abort,
351 uhci_device_ctrl_close,
353 uhci_device_ctrl_done,
356 struct usbd_pipe_methods uhci_device_intr_methods = {
357 uhci_device_intr_transfer,
358 uhci_device_intr_start,
359 uhci_device_intr_abort,
360 uhci_device_intr_close,
361 uhci_device_clear_toggle,
362 uhci_device_intr_done,
365 struct usbd_pipe_methods uhci_device_bulk_methods = {
366 uhci_device_bulk_transfer,
367 uhci_device_bulk_start,
368 uhci_device_bulk_abort,
369 uhci_device_bulk_close,
370 uhci_device_clear_toggle,
371 uhci_device_bulk_done,
374 struct usbd_pipe_methods uhci_device_isoc_methods = {
375 uhci_device_isoc_transfer,
376 uhci_device_isoc_start,
377 uhci_device_isoc_abort,
378 uhci_device_isoc_close,
380 uhci_device_isoc_done,
383 #define uhci_add_intr_info(sc, ii) \
384 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
385 #define uhci_del_intr_info(ii) \
387 ++ii->sc->sc_intrhead_deletion_counter; \
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 = kmalloc(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;
695 cmd = UREAD2(sc, UHCI_CMD);
697 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
698 sc, why, sc->sc_suspend, cmd));
700 if (why != PWR_RESUME) {
705 if (sc->sc_intr_xfer != NULL)
706 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub,
708 sc->sc_bus.use_polling++;
709 uhci_run(sc, 0); /* stop the controller */
711 /* save some state if BIOS doesn't */
712 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
713 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
715 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
717 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
718 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
719 sc->sc_suspend = why;
720 sc->sc_bus.use_polling--;
721 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
724 if (sc->sc_suspend == PWR_RESUME)
725 printf("uhci_power: weird, resume without suspend.\n");
727 sc->sc_bus.use_polling++;
728 sc->sc_suspend = why;
729 if (cmd & UHCI_CMD_RS)
730 uhci_run(sc, 0); /* in case BIOS has started it */
732 /* restore saved state */
733 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
734 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
735 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
737 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
738 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
739 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
740 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
741 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
742 UHCICMD(sc, UHCI_CMD_MAXP);
743 uhci_run(sc, 1); /* and start traffic again */
744 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
745 sc->sc_bus.use_polling--;
746 if (sc->sc_intr_xfer != NULL)
747 usb_callout(sc->sc_poll_handle, sc->sc_ival,
748 uhci_poll_hub, sc->sc_intr_xfer);
759 uhci_dumpregs(uhci_softc_t *sc)
761 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
762 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
763 USBDEVNAME(sc->sc_bus.bdev),
764 UREAD2(sc, UHCI_CMD),
765 UREAD2(sc, UHCI_STS),
766 UREAD2(sc, UHCI_INTR),
767 UREAD2(sc, UHCI_FRNUM),
768 UREAD4(sc, UHCI_FLBASEADDR),
769 UREAD1(sc, UHCI_SOF),
770 UREAD2(sc, UHCI_PORTSC1),
771 UREAD2(sc, UHCI_PORTSC2)));
775 uhci_dump_td(uhci_soft_td_t *p)
777 char sbuf[128], sbuf2[128];
779 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
780 "token=0x%08lx buffer=0x%08lx\n",
781 p, (long)p->physaddr,
782 (long)le32toh(p->td.td_link),
783 (long)le32toh(p->td.td_status),
784 (long)le32toh(p->td.td_token),
785 (long)le32toh(p->td.td_buffer)));
787 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
789 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
790 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
791 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
792 sbuf2, sizeof(sbuf2));
794 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
795 "D=%d,maxlen=%d\n", sbuf, sbuf2,
796 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
797 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
798 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
799 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
800 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
801 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
802 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
806 uhci_dump_qh(uhci_soft_qh_t *sqh)
808 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
809 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
810 le32toh(sqh->qh.qh_elink)));
817 uhci_dump_all(thesc);
822 uhci_dump_all(uhci_softc_t *sc)
825 printf("intrs=%d\n", sc->sc_bus.no_intrs);
826 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
827 uhci_dump_qh(sc->sc_lctl_start);
831 uhci_dump_qhs(uhci_soft_qh_t *sqh)
835 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
836 * Traverses sideways first, then down.
846 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
850 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
851 uhci_dump_qhs(sqh->hlink);
853 DPRINTF(("No QH\n"));
855 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
856 uhci_dump_tds(sqh->elink);
858 DPRINTF(("No TD\n"));
862 uhci_dump_tds(uhci_soft_td_t *std)
866 for(td = std; td != NULL; td = td->link.std) {
869 /* Check whether the link pointer in this TD marks
870 * the link pointer as end of queue. This avoids
871 * printing the free list in case the queue/TD has
872 * already been moved there (seatbelt).
874 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
875 le32toh(td->td.td_link) == 0)
881 uhci_dump_ii(uhci_intr_info_t *ii)
883 usbd_pipe_handle pipe;
884 usb_endpoint_descriptor_t *ed;
885 usbd_device_handle dev;
888 #define DONE ii->isdone
896 if (ii->xfer == NULL) {
897 printf("ii %p: done=%d xfer=NULL\n",
901 pipe = ii->xfer->pipe;
903 printf("ii %p: done=%d xfer=%p pipe=NULL\n",
907 if (pipe->endpoint == NULL) {
908 printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
909 ii, DONE, ii->xfer, pipe);
912 if (pipe->device == NULL) {
913 printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
914 ii, DONE, ii->xfer, pipe);
917 ed = pipe->endpoint->edesc;
919 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",
920 ii, DONE, ii->xfer, dev,
921 UGETW(dev->ddesc.idVendor),
922 UGETW(dev->ddesc.idProduct),
924 ed->bEndpointAddress, ed->bmAttributes);
928 void uhci_dump_iis(struct uhci_softc *sc);
930 uhci_dump_iis(struct uhci_softc *sc)
932 uhci_intr_info_t *ii;
934 printf("intr_info list:\n");
935 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
940 void iidump(void) { uhci_dump_iis(thesc); }
945 * This routine is executed periodically and simulates interrupts
946 * from the root controller interrupt pipe for port status change.
949 uhci_poll_hub(void *addr)
951 usbd_xfer_handle xfer = addr;
952 usbd_pipe_handle pipe = xfer->pipe;
953 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
956 DPRINTFN(20, ("uhci_poll_hub\n"));
958 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
960 p = KERNADDR(&xfer->dmabuf, 0);
962 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
964 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
967 /* No change, try again in a while */
971 xfer->status = USBD_NORMAL_COMPLETION;
973 xfer->device->bus->intr_context++;
974 usb_transfer_complete(xfer);
975 xfer->device->bus->intr_context--;
980 uhci_root_intr_done(usbd_xfer_handle xfer)
985 uhci_root_ctrl_done(usbd_xfer_handle xfer)
990 * Let the last QH loop back to the high speed control transfer QH.
991 * This is what intel calls "bandwidth reclamation" and improves
992 * USB performance a lot for some devices.
993 * If we are already looping, just count it.
996 uhci_add_loop(uhci_softc_t *sc) {
1001 if (++sc->sc_loops == 1) {
1002 DPRINTFN(5,("uhci_start_loop: add\n"));
1003 /* Note, we don't loop back the soft pointer. */
1004 sc->sc_last_qh->qh.qh_hlink =
1005 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
1010 uhci_rem_loop(uhci_softc_t *sc) {
1015 if (--sc->sc_loops == 0) {
1016 DPRINTFN(5,("uhci_end_loop: remove\n"));
1017 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1021 /* Add high speed control QH, called from a critical section. */
1023 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1025 uhci_soft_qh_t *eqh;
1027 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
1028 eqh = sc->sc_hctl_end;
1029 sqh->hlink = eqh->hlink;
1030 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1032 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1033 sc->sc_hctl_end = sqh;
1034 #ifdef UHCI_CTL_LOOP
1039 /* Remove high speed control QH, called from a critical section. */
1041 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1043 uhci_soft_qh_t *pqh;
1045 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1046 #ifdef UHCI_CTL_LOOP
1050 * The T bit should be set in the elink of the QH so that the HC
1051 * doesn't follow the pointer. This condition may fail if the
1052 * the transferred packet was short so that the QH still points
1053 * at the last used TD.
1054 * In this case we set the T bit and wait a little for the HC
1055 * to stop looking at the TD.
1057 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1058 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1059 delay(UHCI_QH_REMOVE_DELAY);
1062 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1063 pqh->hlink = sqh->hlink;
1064 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1065 delay(UHCI_QH_REMOVE_DELAY);
1066 if (sc->sc_hctl_end == sqh)
1067 sc->sc_hctl_end = pqh;
1070 /* Add low speed control QH, called from a critical section. */
1072 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1074 uhci_soft_qh_t *eqh;
1076 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1077 eqh = sc->sc_lctl_end;
1078 sqh->hlink = eqh->hlink;
1079 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1081 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1082 sc->sc_lctl_end = sqh;
1085 /* Remove low speed control QH, called from a critical section. */
1087 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1089 uhci_soft_qh_t *pqh;
1091 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1092 /* See comment in uhci_remove_hs_ctrl() */
1093 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1094 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1095 delay(UHCI_QH_REMOVE_DELAY);
1097 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1098 pqh->hlink = sqh->hlink;
1099 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1100 delay(UHCI_QH_REMOVE_DELAY);
1101 if (sc->sc_lctl_end == sqh)
1102 sc->sc_lctl_end = pqh;
1105 /* Add bulk QH, called from a critical section. */
1107 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1109 uhci_soft_qh_t *eqh;
1111 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1112 eqh = sc->sc_bulk_end;
1113 sqh->hlink = eqh->hlink;
1114 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1116 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1117 sc->sc_bulk_end = sqh;
1121 /* Remove bulk QH, called from a critical section. */
1123 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1125 uhci_soft_qh_t *pqh;
1127 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1129 /* See comment in uhci_remove_hs_ctrl() */
1130 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1131 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1132 delay(UHCI_QH_REMOVE_DELAY);
1134 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1135 pqh->hlink = sqh->hlink;
1136 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1137 delay(UHCI_QH_REMOVE_DELAY);
1138 if (sc->sc_bulk_end == sqh)
1139 sc->sc_bulk_end = pqh;
1142 Static int uhci_intr1(uhci_softc_t *);
1145 uhci_intr(void *arg)
1147 uhci_softc_t *sc = arg;
1152 if (sc->sc_bus.use_polling) {
1154 DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n"));
1159 return (uhci_intr1(sc));
1163 uhci_intr1(uhci_softc_t *sc)
1170 * It can happen that an interrupt will be delivered to
1171 * us before the device has been fully attached and the
1172 * softc struct has been configured. Usually this happens
1173 * when kldloading the USB support as a module after the
1174 * system has been booted. If we detect this condition,
1175 * we need to squelch the unwanted interrupts until we're
1178 if (sc->sc_bus.bdev == NULL) {
1179 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1180 uhci_run(sc, 0); /* stop the controller */
1181 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1186 if (uhcidebug > 15) {
1187 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev)));
1191 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1192 if (status == 0) /* The interrupt was not for us. */
1195 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1196 if (sc->sc_suspend != PWR_RESUME)
1197 printf("uhci_intr: suspended sts=0x%x\n", status);
1200 if (sc->sc_suspend != PWR_RESUME) {
1201 printf("%s: interrupt while not operating ignored\n",
1202 USBDEVNAME(sc->sc_bus.bdev));
1203 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1208 if (status & UHCI_STS_USBINT)
1209 ack |= UHCI_STS_USBINT;
1210 if (status & UHCI_STS_USBEI)
1211 ack |= UHCI_STS_USBEI;
1212 if (status & UHCI_STS_RD) {
1215 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1218 if (status & UHCI_STS_HSE) {
1219 ack |= UHCI_STS_HSE;
1220 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
1222 if (status & UHCI_STS_HCPE) {
1223 ack |= UHCI_STS_HCPE;
1224 printf("%s: host controller process error\n",
1225 USBDEVNAME(sc->sc_bus.bdev));
1227 if (status & UHCI_STS_HCH) {
1228 /* no acknowledge needed */
1229 if (!sc->sc_dying) {
1230 printf("%s: host controller halted\n",
1231 USBDEVNAME(sc->sc_bus.bdev));
1240 return (0); /* nothing to acknowledge */
1241 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1243 sc->sc_bus.no_intrs++;
1244 usb_schedsoftintr(&sc->sc_bus);
1246 DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
1252 uhci_softintr(void *v)
1254 uhci_softc_t *sc = v;
1255 uhci_intr_info_t *ii;
1256 int last_deletion_counter;
1258 DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
1259 sc->sc_bus.intr_context));
1261 sc->sc_bus.intr_context++;
1264 * Interrupts on UHCI really suck. When the host controller
1265 * interrupts because a transfer is completed there is no
1266 * way of knowing which transfer it was. You can scan down
1267 * the TDs and QHs of the previous frame to limit the search,
1268 * but that assumes that the interrupt was not delayed by more
1269 * than 1 ms, which may not always be true (e.g. after debug
1270 * output on a slow console).
1271 * We scan all interrupt descriptors to see if any have
1274 * XXX horrible hack - use a counter to detect if the list is
1275 * modified out from under us and rescan if it is.
1278 last_deletion_counter = sc->sc_intrhead_deletion_counter;
1279 LIST_FOREACH(ii, &sc->sc_intrhead, list) {
1280 uhci_check_intr(sc, ii);
1281 if (sc->sc_intrhead_deletion_counter != last_deletion_counter)
1285 #ifdef USB_USE_SOFTINTR
1286 if (sc->sc_softwake) {
1287 sc->sc_softwake = 0;
1288 wakeup(&sc->sc_softwake);
1290 #endif /* USB_USE_SOFTINTR */
1292 sc->sc_bus.intr_context--;
1295 /* Check for an interrupt. */
1297 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1299 uhci_soft_td_t *std, *lstd;
1302 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1305 printf("uhci_check_intr: no ii? %p\n", ii);
1309 if (ii->xfer->status == USBD_CANCELLED ||
1310 ii->xfer->status == USBD_TIMEOUT) {
1311 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1315 if (ii->stdstart == NULL)
1320 printf("uhci_check_intr: std==0\n");
1325 * If the last TD is still active we need to check whether there
1326 * is an error somewhere in the middle, or whether there was a
1327 * short packet (SPD and not ACTIVE).
1329 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1330 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1331 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1332 status = le32toh(std->td.td_status);
1333 /* If there's an active TD the xfer isn't done. */
1334 if (status & UHCI_TD_ACTIVE)
1336 /* Any kind of error makes the xfer done. */
1337 if (status & UHCI_TD_STALLED)
1339 /* We want short packets, and it is short: it's done */
1340 if ((status & UHCI_TD_SPD) &&
1341 UHCI_TD_GET_ACTLEN(status) <
1342 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) {
1346 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1351 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1352 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1356 /* Called from a critical section. */
1358 uhci_idone(uhci_intr_info_t *ii)
1360 usbd_xfer_handle xfer = ii->xfer;
1361 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1362 uhci_soft_td_t *std;
1363 u_int32_t status = 0, nstatus;
1366 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1373 printf("uhci_idone: ii is done!\n ");
1376 printf("uhci_idone: ii=%p is done!\n", ii);
1385 if (xfer->nframes != 0) {
1386 /* Isoc transfer, do things differently. */
1387 uhci_soft_td_t **stds = upipe->u.iso.stds;
1388 int i, n, nframes, len;
1390 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1392 nframes = xfer->nframes;
1394 n = UXFER(xfer)->curframe;
1395 for (i = 0; i < nframes; i++) {
1398 if (uhcidebug > 5) {
1399 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1403 if (++n >= UHCI_VFRAMELIST_COUNT)
1405 status = le32toh(std->td.td_status);
1406 len = UHCI_TD_GET_ACTLEN(status);
1407 xfer->frlengths[i] = len;
1410 upipe->u.iso.inuse -= nframes;
1411 xfer->actlen = actlen;
1412 xfer->status = USBD_NORMAL_COMPLETION;
1417 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1420 uhci_dump_tds(ii->stdstart);
1423 /* The transfer is done, compute actual length and status. */
1425 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1426 nstatus = le32toh(std->td.td_status);
1427 if (nstatus & UHCI_TD_ACTIVE)
1431 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1432 UHCI_TD_PID_SETUP) {
1433 actlen += UHCI_TD_GET_ACTLEN(status);
1436 * UHCI will report CRCTO in addition to a STALL or NAK
1437 * for a SETUP transaction. See section 3.2.2, "TD
1438 * CONTROL AND STATUS".
1440 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1441 status &= ~UHCI_TD_CRCTO;
1445 /* If there are left over TDs we need to update the toggle. */
1447 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1449 status &= UHCI_TD_ERROR;
1450 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1452 xfer->actlen = actlen;
1457 bitmask_snprintf((u_int32_t)status,
1458 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1459 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1460 sbuf, sizeof(sbuf));
1462 DPRINTFN((status == UHCI_TD_STALLED)*10,
1463 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1465 xfer->pipe->device->address,
1466 xfer->pipe->endpoint->edesc->bEndpointAddress,
1469 if (status == UHCI_TD_STALLED)
1470 xfer->status = USBD_STALLED;
1472 xfer->status = USBD_IOERROR; /* more info XXX */
1474 xfer->status = USBD_NORMAL_COMPLETION;
1478 usb_transfer_complete(xfer);
1479 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1483 * Called when a request does not complete.
1486 uhci_timeout(void *addr)
1488 uhci_intr_info_t *ii = addr;
1489 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1490 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1491 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1493 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1496 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1500 /* Execute the abort in a process context. */
1501 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer);
1502 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1506 uhci_timeout_task(void *addr)
1508 usbd_xfer_handle xfer = addr;
1510 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1513 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1518 * Wait here until controller claims to have an interrupt.
1519 * Then call uhci_intr and return. Use timeout to avoid waiting
1521 * Only used during boot when interrupts are not enabled yet.
1524 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1526 int timo = xfer->timeout;
1527 uhci_intr_info_t *ii;
1529 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1531 xfer->status = USBD_IN_PROGRESS;
1532 for (; timo >= 0; timo--) {
1533 usb_delay_ms(&sc->sc_bus, 1);
1534 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1535 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1537 if (xfer->status != USBD_IN_PROGRESS)
1542 DPRINTF(("uhci_waitintr: timeout\n"));
1543 for (ii = LIST_FIRST(&sc->sc_intrhead);
1544 ii != NULL && ii->xfer != xfer;
1545 ii = LIST_NEXT(ii, list))
1549 panic("uhci_waitintr: lost intr_info");
1555 uhci_poll(struct usbd_bus *bus)
1557 uhci_softc_t *sc = (uhci_softc_t *)bus;
1559 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1564 uhci_reset(uhci_softc_t *sc)
1568 UHCICMD(sc, UHCI_CMD_HCRESET);
1569 /* The reset bit goes low when the controller is done. */
1570 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1571 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1572 usb_delay_ms(&sc->sc_bus, 1);
1573 if (n >= UHCI_RESET_TIMEOUT)
1574 printf("%s: controller did not reset\n",
1575 USBDEVNAME(sc->sc_bus.bdev));
1579 uhci_run(uhci_softc_t *sc, int run)
1586 DPRINTF(("uhci_run: setting run=%d\n", run));
1587 cmd = UREAD2(sc, UHCI_CMD);
1591 cmd &= ~UHCI_CMD_RS;
1593 for(n = 0; n < 10; n++) {
1594 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1595 /* return when we've entered the state we want */
1596 if (run == running) {
1598 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1599 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1600 return (USBD_NORMAL_COMPLETION);
1602 usb_delay_ms(&sc->sc_bus, 1);
1605 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1606 run ? "start" : "stop");
1607 return (USBD_IOERROR);
1611 * Memory management routines.
1612 * uhci_alloc_std allocates TDs
1613 * uhci_alloc_sqh allocates QHs
1614 * These two routines do their own free list management,
1615 * partly for speed, partly because allocating DMAable memory
1616 * has page size granularaity so much memory would be wasted if
1617 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1621 uhci_alloc_std(uhci_softc_t *sc)
1623 uhci_soft_td_t *std;
1628 if (sc->sc_freetds == NULL) {
1629 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1630 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1631 UHCI_TD_ALIGN, &dma);
1634 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1635 offs = i * UHCI_STD_SIZE;
1636 std = KERNADDR(&dma, offs);
1637 std->physaddr = DMAADDR(&dma, offs);
1638 std->link.std = sc->sc_freetds;
1639 sc->sc_freetds = std;
1642 std = sc->sc_freetds;
1643 sc->sc_freetds = std->link.std;
1644 memset(&std->td, 0, sizeof(uhci_td_t));
1649 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1652 #define TD_IS_FREE 0x12345678
1653 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1654 printf("uhci_free_std: freeing free TD %p\n", std);
1657 std->td.td_token = htole32(TD_IS_FREE);
1659 std->link.std = sc->sc_freetds;
1660 sc->sc_freetds = std;
1664 uhci_alloc_sqh(uhci_softc_t *sc)
1666 uhci_soft_qh_t *sqh;
1671 if (sc->sc_freeqhs == NULL) {
1672 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1673 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1674 UHCI_QH_ALIGN, &dma);
1677 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1678 offs = i * UHCI_SQH_SIZE;
1679 sqh = KERNADDR(&dma, offs);
1680 sqh->physaddr = DMAADDR(&dma, offs);
1681 sqh->hlink = sc->sc_freeqhs;
1682 sc->sc_freeqhs = sqh;
1685 sqh = sc->sc_freeqhs;
1686 sc->sc_freeqhs = sqh->hlink;
1687 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1692 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1694 sqh->hlink = sc->sc_freeqhs;
1695 sc->sc_freeqhs = sqh;
1699 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1700 uhci_soft_td_t *stdend)
1704 for (; std != stdend; std = p) {
1706 uhci_free_std(sc, std);
1711 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1712 int rd, u_int16_t flags, usb_dma_t *dma,
1713 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1715 uhci_soft_td_t *p, *lastp;
1716 uhci_physaddr_t lastlink;
1717 int i, ntd, l, tog, maxp;
1719 int addr = upipe->pipe.device->address;
1720 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1722 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1723 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1724 upipe->pipe.device->speed, flags));
1725 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1727 printf("uhci_alloc_std_chain: maxp=0\n");
1728 return (USBD_INVAL);
1730 ntd = (len + maxp - 1) / maxp;
1731 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1733 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1736 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1737 return (USBD_NORMAL_COMPLETION);
1739 tog = upipe->nexttoggle;
1742 upipe->nexttoggle = tog ^ 1;
1744 lastlink = UHCI_PTR_T;
1746 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1747 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1748 status |= UHCI_TD_LS;
1749 if (flags & USBD_SHORT_XFER_OK)
1750 status |= UHCI_TD_SPD;
1751 for (i = ntd; i >= 0; i--) {
1752 p = uhci_alloc_std(sc);
1754 uhci_free_std_chain(sc, lastp, NULL);
1755 return (USBD_NOMEM);
1757 p->link.std = lastp;
1758 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1760 lastlink = p->physaddr;
1761 p->td.td_status = htole32(status);
1765 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1771 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1772 UHCI_TD_OUT(l, endpt, addr, tog));
1773 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1777 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1778 upipe->nexttoggle));
1779 return (USBD_NORMAL_COMPLETION);
1783 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1785 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1786 upipe->nexttoggle = 0;
1790 uhci_noop(usbd_pipe_handle pipe)
1795 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1799 /* Insert last in queue. */
1800 err = usb_insert_transfer(xfer);
1805 * Pipe isn't running (otherwise err would be USBD_INPROG),
1806 * so start it first.
1808 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1812 uhci_device_bulk_start(usbd_xfer_handle xfer)
1814 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1815 usbd_device_handle dev = upipe->pipe.device;
1816 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1817 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1818 uhci_soft_td_t *data, *dataend;
1819 uhci_soft_qh_t *sqh;
1821 int len, isread, endpt;
1823 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1824 xfer, xfer->length, xfer->flags, ii));
1827 return (USBD_IOERROR);
1830 if (xfer->rqflags & URQ_REQUEST)
1831 panic("uhci_device_bulk_transfer: a request");
1835 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1836 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1837 sqh = upipe->u.bulk.sqh;
1839 upipe->u.bulk.isread = isread;
1840 upipe->u.bulk.length = len;
1842 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1843 &xfer->dmabuf, &data, &dataend);
1846 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1849 if (uhcidebug > 8) {
1850 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1851 uhci_dump_tds(data);
1855 /* Set up interrupt info. */
1857 ii->stdstart = data;
1858 ii->stdend = dataend;
1861 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1867 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1870 uhci_add_bulk(sc, sqh);
1871 uhci_add_intr_info(sc, ii);
1873 if (xfer->timeout && !sc->sc_bus.use_polling) {
1874 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1877 xfer->status = USBD_IN_PROGRESS;
1881 if (uhcidebug > 10) {
1882 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1883 uhci_dump_tds(data);
1887 if (sc->sc_bus.use_polling)
1888 uhci_waitintr(sc, xfer);
1890 return (USBD_IN_PROGRESS);
1893 /* Abort a device bulk request. */
1895 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1897 DPRINTF(("uhci_device_bulk_abort:\n"));
1898 uhci_abort_xfer(xfer, USBD_CANCELLED);
1902 * Abort a device request.
1903 * If this routine is called from a critical section. It guarentees that
1904 * the request will be removed from the hardware scheduling and that
1905 * the callback for it will be called with USBD_CANCELLED status.
1906 * It's impossible to guarantee that the requested transfer will not
1907 * have happened since the hardware runs concurrently.
1908 * If the transaction has already happened we rely on the ordinary
1909 * interrupt processing to process it.
1912 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1914 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1915 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1916 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1917 uhci_soft_td_t *std;
1919 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1922 /* If we're dying, just do the software part. */
1924 xfer->status = status; /* make software ignore it */
1925 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1926 usb_transfer_complete(xfer);
1931 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
1932 panic("uhci_abort_xfer: not in process context");
1935 * Step 1: Make interrupt routine and hardware ignore xfer.
1938 xfer->status = status; /* make software ignore it */
1939 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1940 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1941 for (std = ii->stdstart; std != NULL; std = std->link.std)
1942 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1946 * Step 2: Wait until we know hardware has finished any possible
1947 * use of the xfer. Also make sure the soft interrupt routine
1950 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1952 #ifdef USB_USE_SOFTINTR
1953 sc->sc_softwake = 1;
1954 #endif /* USB_USE_SOFTINTR */
1955 usb_schedsoftintr(&sc->sc_bus);
1956 #ifdef USB_USE_SOFTINTR
1957 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1958 tsleep(&sc->sc_softwake, 0, "uhciab", 0);
1959 #endif /* USB_USE_SOFTINTR */
1963 * Step 3: Execute callback.
1967 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1973 usb_transfer_complete(xfer);
1978 /* Close a device bulk pipe. */
1980 uhci_device_bulk_close(usbd_pipe_handle pipe)
1982 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1983 usbd_device_handle dev = upipe->pipe.device;
1984 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1986 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1990 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1994 /* Insert last in queue. */
1995 err = usb_insert_transfer(xfer);
2000 * Pipe isn't running (otherwise err would be USBD_INPROG),
2001 * so start it first.
2003 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2007 uhci_device_ctrl_start(usbd_xfer_handle xfer)
2009 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2013 return (USBD_IOERROR);
2016 if (!(xfer->rqflags & URQ_REQUEST))
2017 panic("uhci_device_ctrl_transfer: not a request");
2020 err = uhci_device_request(xfer);
2024 if (sc->sc_bus.use_polling)
2025 uhci_waitintr(sc, xfer);
2026 return (USBD_IN_PROGRESS);
2030 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2034 /* Insert last in queue. */
2035 err = usb_insert_transfer(xfer);
2040 * Pipe isn't running (otherwise err would be USBD_INPROG),
2041 * so start it first.
2043 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2047 uhci_device_intr_start(usbd_xfer_handle xfer)
2049 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2050 usbd_device_handle dev = upipe->pipe.device;
2051 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2052 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2053 uhci_soft_td_t *data, *dataend;
2054 uhci_soft_qh_t *sqh;
2060 return (USBD_IOERROR);
2062 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2063 xfer, xfer->length, xfer->flags));
2066 if (xfer->rqflags & URQ_REQUEST)
2067 panic("uhci_device_intr_transfer: a request");
2070 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2071 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2072 sqh = upipe->u.bulk.sqh;
2074 upipe->u.intr.isread = isread;
2076 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread,
2077 xfer->flags, &xfer->dmabuf, &data,
2081 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2084 if (uhcidebug > 10) {
2085 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2086 uhci_dump_tds(data);
2087 uhci_dump_qh(upipe->u.intr.qhs[0]);
2092 /* Set up interrupt info. */
2094 ii->stdstart = data;
2095 ii->stdend = dataend;
2098 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2103 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2104 upipe->u.intr.qhs[0]));
2105 for (i = 0; i < upipe->u.intr.npoll; i++) {
2106 sqh = upipe->u.intr.qhs[i];
2108 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2110 uhci_add_intr_info(sc, ii);
2111 xfer->status = USBD_IN_PROGRESS;
2115 if (uhcidebug > 10) {
2116 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2117 uhci_dump_tds(data);
2118 uhci_dump_qh(upipe->u.intr.qhs[0]);
2122 return (USBD_IN_PROGRESS);
2125 /* Abort a device control request. */
2127 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2129 DPRINTF(("uhci_device_ctrl_abort:\n"));
2130 uhci_abort_xfer(xfer, USBD_CANCELLED);
2133 /* Close a device control pipe. */
2135 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2139 /* Abort a device interrupt request. */
2141 uhci_device_intr_abort(usbd_xfer_handle xfer)
2143 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2144 if (xfer->pipe->intrxfer == xfer) {
2145 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2146 xfer->pipe->intrxfer = NULL;
2148 uhci_abort_xfer(xfer, USBD_CANCELLED);
2151 /* Close a device interrupt pipe. */
2153 uhci_device_intr_close(usbd_pipe_handle pipe)
2155 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2156 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2159 /* Unlink descriptors from controller data structures. */
2160 npoll = upipe->u.intr.npoll;
2162 for (i = 0; i < npoll; i++)
2163 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2167 * We now have to wait for any activity on the physical
2168 * descriptors to stop.
2170 usb_delay_ms(&sc->sc_bus, 2);
2172 for(i = 0; i < npoll; i++)
2173 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2174 kfree(upipe->u.intr.qhs, M_USBHC);
2176 /* XXX free other resources */
2180 uhci_device_request(usbd_xfer_handle xfer)
2182 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2183 usb_device_request_t *req = &xfer->request;
2184 usbd_device_handle dev = upipe->pipe.device;
2185 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2186 int addr = dev->address;
2187 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2188 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2189 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2190 uhci_soft_qh_t *sqh;
2196 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2197 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2198 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2199 UGETW(req->wIndex), UGETW(req->wLength),
2202 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2203 isread = req->bmRequestType & UT_READ;
2204 len = UGETW(req->wLength);
2206 setup = upipe->u.ctl.setup;
2207 stat = upipe->u.ctl.stat;
2208 sqh = upipe->u.ctl.sqh;
2210 /* Set up data transaction */
2212 upipe->nexttoggle = 1;
2213 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2214 &xfer->dmabuf, &data, &dataend);
2218 dataend->link.std = stat;
2219 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2223 upipe->u.ctl.length = len;
2225 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2227 setup->link.std = next;
2228 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2229 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2231 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2232 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2233 stat->link.std = NULL;
2234 stat->td.td_link = htole32(UHCI_PTR_T);
2235 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2236 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2239 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2240 UHCI_TD_IN (0, endpt, addr, 1));
2241 stat->td.td_buffer = htole32(0);
2244 if (uhcidebug > 10) {
2245 DPRINTF(("uhci_device_request: before transfer\n"));
2246 uhci_dump_tds(setup);
2250 /* Set up interrupt info. */
2252 ii->stdstart = setup;
2256 printf("uhci_device_request: not done, ii=%p\n", ii);
2262 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2265 if (dev->speed == USB_SPEED_LOW)
2266 uhci_add_ls_ctrl(sc, sqh);
2268 uhci_add_hs_ctrl(sc, sqh);
2269 uhci_add_intr_info(sc, ii);
2271 if (uhcidebug > 12) {
2272 uhci_soft_td_t *std;
2273 uhci_soft_qh_t *xqh;
2274 uhci_soft_qh_t *sxqh;
2276 uhci_physaddr_t link;
2277 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2278 for (std = sc->sc_vframes[0].htd, link = 0;
2279 (link & UHCI_PTR_QH) == 0;
2280 std = std->link.std) {
2281 link = le32toh(std->td.td_link);
2284 sxqh = (uhci_soft_qh_t *)std;
2288 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2289 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2292 DPRINTF(("Enqueued QH:\n"));
2294 uhci_dump_tds(sqh->elink);
2297 if (xfer->timeout && !sc->sc_bus.use_polling) {
2298 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2301 xfer->status = USBD_IN_PROGRESS;
2304 return (USBD_NORMAL_COMPLETION);
2308 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2312 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2314 /* Put it on our queue, */
2315 err = usb_insert_transfer(xfer);
2317 /* bail out on error, */
2318 if (err && err != USBD_IN_PROGRESS)
2321 /* XXX should check inuse here */
2323 /* insert into schedule, */
2324 uhci_device_isoc_enter(xfer);
2326 /* and start if the pipe wasn't running */
2328 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2334 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2336 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2337 usbd_device_handle dev = upipe->pipe.device;
2338 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2339 struct iso *iso = &upipe->u.iso;
2340 uhci_soft_td_t *std;
2341 u_int32_t buf, len, status;
2342 int i, next, nframes;
2344 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2346 iso->inuse, iso->next, xfer, xfer->nframes));
2351 if (xfer->status == USBD_IN_PROGRESS) {
2352 /* This request has already been entered into the frame list */
2353 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2358 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2359 printf("uhci_device_isoc_enter: overflow!\n");
2364 /* Not in use yet, schedule it a few frames ahead. */
2365 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2366 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2369 xfer->status = USBD_IN_PROGRESS;
2370 UXFER(xfer)->curframe = next;
2372 buf = DMAADDR(&xfer->dmabuf, 0);
2373 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2376 nframes = xfer->nframes;
2378 for (i = 0; i < nframes; i++) {
2379 std = iso->stds[next];
2380 if (++next >= UHCI_VFRAMELIST_COUNT)
2382 len = xfer->frlengths[i];
2383 std->td.td_buffer = htole32(buf);
2384 if (i == nframes - 1)
2385 status |= UHCI_TD_IOC;
2386 std->td.td_status = htole32(status);
2387 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2388 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2390 if (uhcidebug > 5) {
2391 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2398 iso->inuse += xfer->nframes;
2404 uhci_device_isoc_start(usbd_xfer_handle xfer)
2406 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2407 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2408 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2409 uhci_soft_td_t *end;
2412 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2415 return (USBD_IOERROR);
2418 if (xfer->status != USBD_IN_PROGRESS)
2419 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2422 /* Find the last TD */
2423 i = UXFER(xfer)->curframe + xfer->nframes;
2424 if (i >= UHCI_VFRAMELIST_COUNT)
2425 i -= UHCI_VFRAMELIST_COUNT;
2426 end = upipe->u.iso.stds[i];
2430 printf("uhci_device_isoc_start: end == NULL\n");
2431 return (USBD_INVAL);
2437 /* Set up interrupt info. */
2443 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2446 uhci_add_intr_info(sc, ii);
2450 return (USBD_IN_PROGRESS);
2454 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2456 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2457 uhci_soft_td_t **stds = upipe->u.iso.stds;
2458 uhci_soft_td_t *std;
2459 int i, n, nframes, maxlen, len;
2463 /* Transfer is already done. */
2464 if (xfer->status != USBD_NOT_STARTED &&
2465 xfer->status != USBD_IN_PROGRESS) {
2470 /* Give xfer the requested abort code. */
2471 xfer->status = USBD_CANCELLED;
2473 /* make hardware ignore it, */
2474 nframes = xfer->nframes;
2475 n = UXFER(xfer)->curframe;
2477 for (i = 0; i < nframes; i++) {
2479 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2480 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2483 if (++n >= UHCI_VFRAMELIST_COUNT)
2487 /* and wait until we are sure the hardware has finished. */
2491 UXFER(xfer)->iinfo.isdone = 1;
2493 /* Run callback and remove from interrupt list. */
2494 usb_transfer_complete(xfer);
2500 uhci_device_isoc_close(usbd_pipe_handle pipe)
2502 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2503 usbd_device_handle dev = upipe->pipe.device;
2504 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2505 uhci_soft_td_t *std, *vstd;
2510 * Make sure all TDs are marked as inactive.
2511 * Wait for completion.
2515 iso = &upipe->u.iso;
2517 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2518 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2519 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2522 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2524 for (vstd = sc->sc_vframes[i].htd;
2525 vstd != NULL && vstd->link.std != std;
2526 vstd = vstd->link.std)
2530 printf("uhci_device_isoc_close: %p not found\n", std);
2534 vstd->link = std->link;
2535 vstd->td.td_link = std->td.td_link;
2536 uhci_free_std(sc, std);
2540 kfree(iso->stds, M_USBHC);
2544 uhci_setup_isoc(usbd_pipe_handle pipe)
2546 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2547 usbd_device_handle dev = upipe->pipe.device;
2548 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2549 int addr = upipe->pipe.device->address;
2550 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2551 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2552 uhci_soft_td_t *std, *vstd;
2557 iso = &upipe->u.iso;
2558 iso->stds = kmalloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2559 M_USBHC, M_INTWAIT);
2561 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2562 UHCI_TD_OUT(0, endpt, addr, 0);
2564 /* Allocate the TDs and mark as inactive; */
2565 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2566 std = uhci_alloc_std(sc);
2569 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2570 std->td.td_token = htole32(token);
2574 /* Insert TDs into schedule. */
2576 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2578 vstd = sc->sc_vframes[i].htd;
2579 std->link = vstd->link;
2580 std->td.td_link = vstd->td.td_link;
2581 vstd->link.std = std;
2582 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2589 return (USBD_NORMAL_COMPLETION);
2593 uhci_free_std(sc, iso->stds[i]);
2594 kfree(iso->stds, M_USBHC);
2595 return (USBD_NOMEM);
2599 uhci_device_isoc_done(usbd_xfer_handle xfer)
2601 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2603 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2605 if (ii->xfer != xfer)
2606 /* Not on interrupt list, ignore it. */
2609 if (!uhci_active_intr_info(ii))
2613 if (xfer->busy_free != XFER_BUSY) {
2614 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2615 xfer, xfer->busy_free);
2619 if (ii->stdend == NULL) {
2620 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2628 /* Turn off the interrupt since it is active even if the TD is not. */
2629 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2631 uhci_del_intr_info(ii); /* remove from active list */
2634 if (ii->stdend == NULL) {
2635 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2645 uhci_device_intr_done(usbd_xfer_handle xfer)
2647 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2648 uhci_softc_t *sc = ii->sc;
2649 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2650 uhci_soft_qh_t *sqh;
2653 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2655 npoll = upipe->u.intr.npoll;
2656 for(i = 0; i < npoll; i++) {
2657 sqh = upipe->u.intr.qhs[i];
2659 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2661 uhci_free_std_chain(sc, ii->stdstart, NULL);
2664 if (xfer->pipe->repeat) {
2665 uhci_soft_td_t *data, *dataend;
2667 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2669 /* This alloc cannot fail since we freed the chain above. */
2670 uhci_alloc_std_chain(upipe, sc, xfer->length,
2671 upipe->u.intr.isread, xfer->flags,
2672 &xfer->dmabuf, &data, &dataend);
2673 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2676 if (uhcidebug > 10) {
2677 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2678 uhci_dump_tds(data);
2679 uhci_dump_qh(upipe->u.intr.qhs[0]);
2683 ii->stdstart = data;
2684 ii->stdend = dataend;
2687 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2691 for (i = 0; i < npoll; i++) {
2692 sqh = upipe->u.intr.qhs[i];
2694 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2696 xfer->status = USBD_IN_PROGRESS;
2697 /* The ii is already on the examined list, just leave it. */
2699 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2700 if (uhci_active_intr_info(ii))
2701 uhci_del_intr_info(ii);
2705 /* Deallocate request data structures */
2707 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2709 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2710 uhci_softc_t *sc = ii->sc;
2711 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2714 if (!(xfer->rqflags & URQ_REQUEST))
2715 panic("uhci_device_ctrl_done: not a request");
2718 if (!uhci_active_intr_info(ii))
2721 uhci_del_intr_info(ii); /* remove from active list */
2723 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2724 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2726 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2728 if (upipe->u.ctl.length != 0)
2729 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2731 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2734 /* Deallocate request data structures */
2736 uhci_device_bulk_done(usbd_xfer_handle xfer)
2738 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2739 uhci_softc_t *sc = ii->sc;
2740 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2742 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2743 xfer, ii, sc, upipe));
2745 if (!uhci_active_intr_info(ii))
2748 uhci_del_intr_info(ii); /* remove from active list */
2750 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2752 uhci_free_std_chain(sc, ii->stdstart, NULL);
2754 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2757 /* Add interrupt QH, called with vflock. */
2759 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2761 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2762 uhci_soft_qh_t *eqh;
2764 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2767 sqh->hlink = eqh->hlink;
2768 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2770 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2775 /* Remove interrupt QH. */
2777 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2779 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2780 uhci_soft_qh_t *pqh;
2782 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2784 /* See comment in uhci_remove_ctrl() */
2785 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2786 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2787 delay(UHCI_QH_REMOVE_DELAY);
2790 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2791 pqh->hlink = sqh->hlink;
2792 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2793 delay(UHCI_QH_REMOVE_DELAY);
2800 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2802 uhci_soft_qh_t *sqh;
2804 u_int bestbw, bw, bestoffs, offs;
2806 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2808 printf("uhci_setintr: 0 interval\n");
2809 return (USBD_INVAL);
2812 if (ival > UHCI_VFRAMELIST_COUNT)
2813 ival = UHCI_VFRAMELIST_COUNT;
2814 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2815 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2817 upipe->u.intr.npoll = npoll;
2819 kmalloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_INTWAIT);
2822 * Figure out which offset in the schedule that has most
2823 * bandwidth left over.
2825 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2826 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2827 for (bw = i = 0; i < npoll; i++)
2828 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2834 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2836 for(i = 0; i < npoll; i++) {
2837 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2839 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2840 sqh->pos = MOD(i * ival + bestoffs);
2845 /* Enter QHs into the controller data structures. */
2846 for(i = 0; i < npoll; i++)
2847 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2850 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2851 return (USBD_NORMAL_COMPLETION);
2854 /* Open a new pipe. */
2856 uhci_open(usbd_pipe_handle pipe)
2858 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2859 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2860 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2864 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2865 pipe, pipe->device->address,
2866 ed->bEndpointAddress, sc->sc_addr));
2868 upipe->aborting = 0;
2869 upipe->nexttoggle = 0;
2871 if (pipe->device->address == sc->sc_addr) {
2872 switch (ed->bEndpointAddress) {
2873 case USB_CONTROL_ENDPOINT:
2874 pipe->methods = &uhci_root_ctrl_methods;
2876 case UE_DIR_IN | UHCI_INTR_ENDPT:
2877 pipe->methods = &uhci_root_intr_methods;
2880 return (USBD_INVAL);
2883 switch (ed->bmAttributes & UE_XFERTYPE) {
2885 pipe->methods = &uhci_device_ctrl_methods;
2886 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2887 if (upipe->u.ctl.sqh == NULL)
2889 upipe->u.ctl.setup = uhci_alloc_std(sc);
2890 if (upipe->u.ctl.setup == NULL) {
2891 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2894 upipe->u.ctl.stat = uhci_alloc_std(sc);
2895 if (upipe->u.ctl.stat == NULL) {
2896 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2897 uhci_free_std(sc, upipe->u.ctl.setup);
2900 err = usb_allocmem(&sc->sc_bus,
2901 sizeof(usb_device_request_t),
2902 0, &upipe->u.ctl.reqdma);
2904 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2905 uhci_free_std(sc, upipe->u.ctl.setup);
2906 uhci_free_std(sc, upipe->u.ctl.stat);
2911 pipe->methods = &uhci_device_intr_methods;
2912 ival = pipe->interval;
2913 if (ival == USBD_DEFAULT_INTERVAL)
2914 ival = ed->bInterval;
2915 return (uhci_device_setintr(sc, upipe, ival));
2916 case UE_ISOCHRONOUS:
2917 pipe->methods = &uhci_device_isoc_methods;
2918 return (uhci_setup_isoc(pipe));
2920 pipe->methods = &uhci_device_bulk_methods;
2921 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2922 if (upipe->u.bulk.sqh == NULL)
2927 return (USBD_NORMAL_COMPLETION);
2930 return (USBD_NOMEM);
2934 * Data structures and routines to emulate the root hub.
2936 usb_device_descriptor_t uhci_devd = {
2937 USB_DEVICE_DESCRIPTOR_SIZE,
2938 UDESC_DEVICE, /* type */
2939 {0x00, 0x01}, /* USB version */
2940 UDCLASS_HUB, /* class */
2941 UDSUBCLASS_HUB, /* subclass */
2942 UDPROTO_FSHUB, /* protocol */
2943 64, /* max packet */
2944 {0},{0},{0x00,0x01}, /* device id */
2945 1,2,0, /* string indicies */
2946 1 /* # of configurations */
2949 usb_config_descriptor_t uhci_confd = {
2950 USB_CONFIG_DESCRIPTOR_SIZE,
2952 {USB_CONFIG_DESCRIPTOR_SIZE +
2953 USB_INTERFACE_DESCRIPTOR_SIZE +
2954 USB_ENDPOINT_DESCRIPTOR_SIZE},
2962 usb_interface_descriptor_t uhci_ifcd = {
2963 USB_INTERFACE_DESCRIPTOR_SIZE,
2974 usb_endpoint_descriptor_t uhci_endpd = {
2975 USB_ENDPOINT_DESCRIPTOR_SIZE,
2977 UE_DIR_IN | UHCI_INTR_ENDPT,
2983 usb_hub_descriptor_t uhci_hubd_piix = {
2984 USB_HUB_DESCRIPTOR_SIZE,
2987 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2988 50, /* power on to power good */
2990 { 0x00 }, /* both ports are removable */
2994 uhci_str(usb_string_descriptor_t *p, int l, char *s)
3000 p->bLength = 2 * strlen(s) + 2;
3003 p->bDescriptorType = UDESC_STRING;
3005 for (i = 0; s[i] && l > 1; i++, l -= 2)
3006 USETW2(p->bString[i], 0, s[i]);
3011 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3012 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3013 * should not be used by the USB subsystem. As we cannot issue a
3014 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3015 * will be enabled as part of the reset.
3017 * On the VT83C572, the port cannot be successfully enabled until the
3018 * outstanding "port enable change" and "connection status change"
3019 * events have been reset.
3022 uhci_portreset(uhci_softc_t *sc, int index)
3027 port = UHCI_PORTSC1;
3028 else if (index == 2)
3029 port = UHCI_PORTSC2;
3031 return (USBD_IOERROR);
3033 x = URWMASK(UREAD2(sc, port));
3034 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3036 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3038 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3039 index, UREAD2(sc, port)));
3041 x = URWMASK(UREAD2(sc, port));
3042 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3046 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3047 index, UREAD2(sc, port)));
3049 x = URWMASK(UREAD2(sc, port));
3050 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3052 for (lim = 10; --lim > 0;) {
3053 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3055 x = UREAD2(sc, port);
3057 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3060 if (!(x & UHCI_PORTSC_CCS)) {
3062 * No device is connected (or was disconnected
3063 * during reset). Consider the port reset.
3064 * The delay must be long enough to ensure on
3065 * the initial iteration that the device
3066 * connection will have been registered. 50ms
3067 * appears to be sufficient, but 20ms is not.
3069 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3074 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3076 * Port enabled changed and/or connection
3077 * status changed were set. Reset either or
3078 * both raised flags (by writing a 1 to that
3079 * bit), and wait again for state to settle.
3081 UWRITE2(sc, port, URWMASK(x) |
3082 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3086 if (x & UHCI_PORTSC_PE)
3087 /* Port is enabled */
3090 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3093 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3094 index, UREAD2(sc, port)));
3097 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3098 return (USBD_TIMEOUT);
3102 return (USBD_NORMAL_COMPLETION);
3106 * Simulate a hardware hub by handling all the necessary requests.
3109 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3113 /* Insert last in queue. */
3114 err = usb_insert_transfer(xfer);
3119 * Pipe isn't running (otherwise err would be USBD_INPROG),
3120 * so start it first.
3122 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3126 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3128 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3129 usb_device_request_t *req;
3132 int len, value, index, status, change, l, totlen = 0;
3133 usb_port_status_t ps;
3137 return (USBD_IOERROR);
3140 if (!(xfer->rqflags & URQ_REQUEST))
3141 panic("uhci_root_ctrl_transfer: not a request");
3143 req = &xfer->request;
3145 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3146 req->bmRequestType, req->bRequest));
3148 len = UGETW(req->wLength);
3149 value = UGETW(req->wValue);
3150 index = UGETW(req->wIndex);
3153 buf = KERNADDR(&xfer->dmabuf, 0);
3155 #define C(x,y) ((x) | ((y) << 8))
3156 switch(C(req->bRequest, req->bmRequestType)) {
3157 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3158 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3159 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3161 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3162 * for the integrated root hub.
3165 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3167 *(u_int8_t *)buf = sc->sc_conf;
3171 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3172 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3175 switch(value >> 8) {
3177 if ((value & 0xff) != 0) {
3181 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3182 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3183 memcpy(buf, &uhci_devd, l);
3186 if ((value & 0xff) != 0) {
3190 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3191 memcpy(buf, &uhci_confd, l);
3192 buf = (char *)buf + l;
3194 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3196 memcpy(buf, &uhci_ifcd, l);
3197 buf = (char *)buf + l;
3199 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3201 memcpy(buf, &uhci_endpd, l);
3204 *(u_int8_t *)buf = 0;
3206 switch (value & 0xff) {
3207 case 1: /* Vendor */
3208 totlen = uhci_str(buf, len, sc->sc_vendor);
3210 case 2: /* Product */
3211 totlen = uhci_str(buf, len, "UHCI root hub");
3220 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3222 *(u_int8_t *)buf = 0;
3226 case C(UR_GET_STATUS, UT_READ_DEVICE):
3228 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3232 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3233 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3235 USETW(((usb_status_t *)buf)->wStatus, 0);
3239 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3240 if (value >= USB_MAX_DEVICES) {
3244 sc->sc_addr = value;
3246 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3247 if (value != 0 && value != 1) {
3251 sc->sc_conf = value;
3253 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3255 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3256 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3257 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3260 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3262 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3265 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3267 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3268 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3269 "port=%d feature=%d\n",
3272 port = UHCI_PORTSC1;
3273 else if (index == 2)
3274 port = UHCI_PORTSC2;
3280 case UHF_PORT_ENABLE:
3281 x = URWMASK(UREAD2(sc, port));
3282 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3284 case UHF_PORT_SUSPEND:
3285 x = URWMASK(UREAD2(sc, port));
3286 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3288 case UHF_PORT_RESET:
3289 x = URWMASK(UREAD2(sc, port));
3290 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3292 case UHF_C_PORT_CONNECTION:
3293 x = URWMASK(UREAD2(sc, port));
3294 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3296 case UHF_C_PORT_ENABLE:
3297 x = URWMASK(UREAD2(sc, port));
3298 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3300 case UHF_C_PORT_OVER_CURRENT:
3301 x = URWMASK(UREAD2(sc, port));
3302 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3304 case UHF_C_PORT_RESET:
3306 err = USBD_NORMAL_COMPLETION;
3308 case UHF_PORT_CONNECTION:
3309 case UHF_PORT_OVER_CURRENT:
3310 case UHF_PORT_POWER:
3311 case UHF_PORT_LOW_SPEED:
3312 case UHF_C_PORT_SUSPEND:
3318 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3320 port = UHCI_PORTSC1;
3321 else if (index == 2)
3322 port = UHCI_PORTSC2;
3329 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3330 UHCI_PORTSC_LS_SHIFT;
3334 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3341 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3343 memcpy(buf, &uhci_hubd_piix, l);
3345 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3350 memset(buf, 0, len);
3353 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3355 port = UHCI_PORTSC1;
3356 else if (index == 2)
3357 port = UHCI_PORTSC2;
3366 x = UREAD2(sc, port);
3367 status = change = 0;
3368 if (x & UHCI_PORTSC_CCS)
3369 status |= UPS_CURRENT_CONNECT_STATUS;
3370 if (x & UHCI_PORTSC_CSC)
3371 change |= UPS_C_CONNECT_STATUS;
3372 if (x & UHCI_PORTSC_PE)
3373 status |= UPS_PORT_ENABLED;
3374 if (x & UHCI_PORTSC_POEDC)
3375 change |= UPS_C_PORT_ENABLED;
3376 if (x & UHCI_PORTSC_OCI)
3377 status |= UPS_OVERCURRENT_INDICATOR;
3378 if (x & UHCI_PORTSC_OCIC)
3379 change |= UPS_C_OVERCURRENT_INDICATOR;
3380 if (x & UHCI_PORTSC_SUSP)
3381 status |= UPS_SUSPEND;
3382 if (x & UHCI_PORTSC_LSDA)
3383 status |= UPS_LOW_SPEED;
3384 status |= UPS_PORT_POWER;
3386 change |= UPS_C_PORT_RESET;
3387 USETW(ps.wPortStatus, status);
3388 USETW(ps.wPortChange, change);
3389 l = min(len, sizeof ps);
3390 memcpy(buf, &ps, l);
3393 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3396 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3398 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3400 port = UHCI_PORTSC1;
3401 else if (index == 2)
3402 port = UHCI_PORTSC2;
3408 case UHF_PORT_ENABLE:
3409 x = URWMASK(UREAD2(sc, port));
3410 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3412 case UHF_PORT_SUSPEND:
3413 x = URWMASK(UREAD2(sc, port));
3414 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3416 case UHF_PORT_RESET:
3417 err = uhci_portreset(sc, index);
3419 case UHF_PORT_POWER:
3420 /* Pretend we turned on power */
3421 err = USBD_NORMAL_COMPLETION;
3423 case UHF_C_PORT_CONNECTION:
3424 case UHF_C_PORT_ENABLE:
3425 case UHF_C_PORT_OVER_CURRENT:
3426 case UHF_PORT_CONNECTION:
3427 case UHF_PORT_OVER_CURRENT:
3428 case UHF_PORT_LOW_SPEED:
3429 case UHF_C_PORT_SUSPEND:
3430 case UHF_C_PORT_RESET:
3440 xfer->actlen = totlen;
3441 err = USBD_NORMAL_COMPLETION;
3445 usb_transfer_complete(xfer);
3447 return (USBD_IN_PROGRESS);
3450 /* Abort a root control request. */
3452 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3454 /* Nothing to do, all transfers are synchronous. */
3457 /* Close the root pipe. */
3459 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3461 DPRINTF(("uhci_root_ctrl_close\n"));
3464 /* Abort a root interrupt request. */
3466 uhci_root_intr_abort(usbd_xfer_handle xfer)
3468 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3470 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3471 sc->sc_intr_xfer = NULL;
3473 if (xfer->pipe->intrxfer == xfer) {
3474 DPRINTF(("uhci_root_intr_abort: remove\n"));
3475 xfer->pipe->intrxfer = 0;
3477 xfer->status = USBD_CANCELLED;
3479 UXFER(xfer)->iinfo.isdone = 1;
3481 usb_transfer_complete(xfer);
3485 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3489 /* Insert last in queue. */
3490 err = usb_insert_transfer(xfer);
3495 * Pipe isn't running (otherwise err would be USBD_INPROG),
3496 * so start it first.
3498 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3501 /* Start a transfer on the root interrupt pipe */
3503 uhci_root_intr_start(usbd_xfer_handle xfer)
3505 usbd_pipe_handle pipe = xfer->pipe;
3506 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3508 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3509 xfer, xfer->length, xfer->flags));
3512 return (USBD_IOERROR);
3514 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3515 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3516 sc->sc_intr_xfer = xfer;
3517 return (USBD_IN_PROGRESS);
3520 /* Close the root interrupt pipe. */
3522 uhci_root_intr_close(usbd_pipe_handle pipe)
3524 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3526 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3527 sc->sc_intr_xfer = NULL;
3528 DPRINTF(("uhci_root_intr_close\n"));