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.12 2005/06/02 20:40:40 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>
82 #include <sys/thread2.h>
84 #include <machine/bus.h>
85 #include <machine/endian.h>
91 #include "usb_quirks.h"
96 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
97 /*#define UHCI_CTL_LOOP */
99 #if defined(__FreeBSD__) || defined(__DragonFly__)
100 #include <machine/clock.h>
102 #define delay(d) DELAY(d)
105 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
107 #if defined(__OpenBSD__)
108 struct cfdriver uhci_cd = {
109 NULL, "uhci", DV_DULL
115 #define DPRINTF(x) if (uhcidebug) printf x
116 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
119 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
120 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
121 &uhcidebug, 0, "uhci debug level");
122 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
123 &uhcinoloop, 0, "uhci noloop");
125 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
129 #define DPRINTFN(n,x)
133 * The UHCI controller is little endian, so on big endian machines
134 * the data strored in memory needs to be swapped.
136 #if defined(__OpenBSD__)
137 #if BYTE_ORDER == BIG_ENDIAN
138 #define htole32(x) (bswap32(x))
139 #define le32toh(x) (bswap32(x))
141 #define htole32(x) (x)
142 #define le32toh(x) (x)
147 struct usbd_pipe pipe;
151 usbd_xfer_handle abortstart, abortend;
153 /* Info needed for different pipe kinds. */
159 uhci_soft_td_t *setup, *stat;
166 uhci_soft_qh_t **qhs;
176 uhci_soft_td_t **stds;
182 Static void uhci_globalreset(uhci_softc_t *);
183 Static usbd_status uhci_portreset(uhci_softc_t*, int);
184 Static void uhci_reset(uhci_softc_t *);
185 #if defined(__NetBSD__) || defined(__OpenBSD__)
186 Static void uhci_shutdown(void *v);
187 Static void uhci_power(int, void *);
189 Static usbd_status uhci_run(uhci_softc_t *, int run);
190 Static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
191 Static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
192 Static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
193 Static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
195 Static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
197 Static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
200 Static void uhci_free_std_chain(uhci_softc_t *,
201 uhci_soft_td_t *, uhci_soft_td_t *);
202 Static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
203 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
204 uhci_soft_td_t **, uhci_soft_td_t **);
205 Static void uhci_poll_hub(void *);
206 Static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
207 Static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
208 Static void uhci_idone(uhci_intr_info_t *);
210 Static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
212 Static void uhci_timeout(void *);
213 Static void uhci_timeout_task(void *);
214 Static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
215 Static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
216 Static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
217 Static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
218 Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
219 Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
220 Static int uhci_str(usb_string_descriptor_t *, int, char *);
221 Static void uhci_add_loop(uhci_softc_t *sc);
222 Static void uhci_rem_loop(uhci_softc_t *sc);
224 Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
225 Static void uhci_device_isoc_enter(usbd_xfer_handle);
227 Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
228 Static void uhci_freem(struct usbd_bus *, usb_dma_t *);
230 Static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
231 Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
233 Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
234 Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
235 Static void uhci_device_ctrl_abort(usbd_xfer_handle);
236 Static void uhci_device_ctrl_close(usbd_pipe_handle);
237 Static void uhci_device_ctrl_done(usbd_xfer_handle);
239 Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
240 Static usbd_status uhci_device_intr_start(usbd_xfer_handle);
241 Static void uhci_device_intr_abort(usbd_xfer_handle);
242 Static void uhci_device_intr_close(usbd_pipe_handle);
243 Static void uhci_device_intr_done(usbd_xfer_handle);
245 Static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
246 Static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
247 Static void uhci_device_bulk_abort(usbd_xfer_handle);
248 Static void uhci_device_bulk_close(usbd_pipe_handle);
249 Static void uhci_device_bulk_done(usbd_xfer_handle);
251 Static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
252 Static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
253 Static void uhci_device_isoc_abort(usbd_xfer_handle);
254 Static void uhci_device_isoc_close(usbd_pipe_handle);
255 Static void uhci_device_isoc_done(usbd_xfer_handle);
257 Static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
258 Static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
259 Static void uhci_root_ctrl_abort(usbd_xfer_handle);
260 Static void uhci_root_ctrl_close(usbd_pipe_handle);
261 Static void uhci_root_ctrl_done(usbd_xfer_handle);
263 Static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
264 Static usbd_status uhci_root_intr_start(usbd_xfer_handle);
265 Static void uhci_root_intr_abort(usbd_xfer_handle);
266 Static void uhci_root_intr_close(usbd_pipe_handle);
267 Static void uhci_root_intr_done(usbd_xfer_handle);
269 Static usbd_status uhci_open(usbd_pipe_handle);
270 Static void uhci_poll(struct usbd_bus *);
271 Static void uhci_softintr(void *);
273 Static usbd_status uhci_device_request(usbd_xfer_handle xfer);
275 Static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
276 Static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
277 Static usbd_status uhci_device_setintr(uhci_softc_t *sc,
278 struct uhci_pipe *pipe, int ival);
280 Static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
281 Static void uhci_noop(usbd_pipe_handle pipe);
283 Static __inline__ uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
287 Static void uhci_dump_all(uhci_softc_t *);
288 Static void uhci_dumpregs(uhci_softc_t *);
289 Static void uhci_dump_qhs(uhci_soft_qh_t *);
290 Static void uhci_dump_qh(uhci_soft_qh_t *);
291 Static void uhci_dump_tds(uhci_soft_td_t *);
292 Static void uhci_dump_td(uhci_soft_td_t *);
293 Static void uhci_dump_ii(uhci_intr_info_t *ii);
294 void uhci_dump(void);
297 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
298 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
299 #define UWRITE1(sc, r, x) \
300 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
301 } while (/*CONSTCOND*/0)
302 #define UWRITE2(sc, r, x) \
303 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
304 } while (/*CONSTCOND*/0)
305 #define UWRITE4(sc, r, x) \
306 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
307 } while (/*CONSTCOND*/0)
308 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
309 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
310 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
312 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
313 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
315 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
317 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
319 #define UHCI_INTR_ENDPT 1
321 struct usbd_bus_methods uhci_bus_methods = {
331 struct usbd_pipe_methods uhci_root_ctrl_methods = {
332 uhci_root_ctrl_transfer,
333 uhci_root_ctrl_start,
334 uhci_root_ctrl_abort,
335 uhci_root_ctrl_close,
340 struct usbd_pipe_methods uhci_root_intr_methods = {
341 uhci_root_intr_transfer,
342 uhci_root_intr_start,
343 uhci_root_intr_abort,
344 uhci_root_intr_close,
349 struct usbd_pipe_methods uhci_device_ctrl_methods = {
350 uhci_device_ctrl_transfer,
351 uhci_device_ctrl_start,
352 uhci_device_ctrl_abort,
353 uhci_device_ctrl_close,
355 uhci_device_ctrl_done,
358 struct usbd_pipe_methods uhci_device_intr_methods = {
359 uhci_device_intr_transfer,
360 uhci_device_intr_start,
361 uhci_device_intr_abort,
362 uhci_device_intr_close,
363 uhci_device_clear_toggle,
364 uhci_device_intr_done,
367 struct usbd_pipe_methods uhci_device_bulk_methods = {
368 uhci_device_bulk_transfer,
369 uhci_device_bulk_start,
370 uhci_device_bulk_abort,
371 uhci_device_bulk_close,
372 uhci_device_clear_toggle,
373 uhci_device_bulk_done,
376 struct usbd_pipe_methods uhci_device_isoc_methods = {
377 uhci_device_isoc_transfer,
378 uhci_device_isoc_start,
379 uhci_device_isoc_abort,
380 uhci_device_isoc_close,
382 uhci_device_isoc_done,
385 #define uhci_add_intr_info(sc, ii) \
386 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
387 #define uhci_del_intr_info(ii) \
389 LIST_REMOVE((ii), list); \
390 (ii)->list.le_prev = NULL; \
392 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
394 Static __inline__ uhci_soft_qh_t *
395 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
397 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
399 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
400 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
401 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
402 printf("uhci_find_prev_qh: QH not found\n");
411 uhci_globalreset(uhci_softc_t *sc)
413 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
414 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
415 UHCICMD(sc, 0); /* do nothing */
419 uhci_init(uhci_softc_t *sc)
423 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
426 DPRINTFN(1,("uhci_init: start\n"));
435 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
436 uhci_globalreset(sc); /* reset the controller */
439 /* Allocate and initialize real frame array. */
440 err = usb_allocmem(&sc->sc_bus,
441 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
442 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
445 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
446 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
447 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
450 * Allocate a TD, inactive, that hangs from the last QH.
451 * This is to avoid a bug in the PIIX that makes it run berserk
454 std = uhci_alloc_std(sc);
457 std->link.std = NULL;
458 std->td.td_link = htole32(UHCI_PTR_T);
459 std->td.td_status = htole32(0); /* inactive */
460 std->td.td_token = htole32(0);
461 std->td.td_buffer = htole32(0);
463 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
464 lsqh = uhci_alloc_sqh(sc);
468 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
470 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
471 sc->sc_last_qh = lsqh;
473 /* Allocate the dummy QH where bulk traffic will be queued. */
474 bsqh = uhci_alloc_sqh(sc);
478 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
480 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
481 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
483 /* Allocate dummy QH where high speed control traffic will be queued. */
484 chsqh = uhci_alloc_sqh(sc);
488 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
490 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
491 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
493 /* Allocate dummy QH where control traffic will be queued. */
494 clsqh = uhci_alloc_sqh(sc);
498 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
500 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
501 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
504 * Make all (virtual) frame list pointers point to the interrupt
505 * queue heads and the interrupt queue heads at the control
506 * queue head and point the physical frame list to the virtual.
508 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
509 std = uhci_alloc_std(sc);
510 sqh = uhci_alloc_sqh(sc);
511 if (std == NULL || sqh == NULL)
515 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
516 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
517 std->td.td_token = htole32(0);
518 std->td.td_buffer = htole32(0);
520 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
522 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
523 sc->sc_vframes[i].htd = std;
524 sc->sc_vframes[i].etd = std;
525 sc->sc_vframes[i].hqh = sqh;
526 sc->sc_vframes[i].eqh = sqh;
528 j < UHCI_FRAMELIST_COUNT;
529 j += UHCI_VFRAMELIST_COUNT) {
530 sc->sc_pframes[j] = htole32(std->physaddr);
534 LIST_INIT(&sc->sc_intrhead);
536 SIMPLEQ_INIT(&sc->sc_free_xfers);
538 usb_callout_init(sc->sc_poll_handle);
540 /* Set up the bus struct. */
541 sc->sc_bus.methods = &uhci_bus_methods;
542 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
544 #if defined(__NetBSD__) || defined(__OpenBSD__)
545 sc->sc_suspend = PWR_RESUME;
546 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
547 sc->sc_shutdownhook = shutdownhook_establish(uhci_shutdown, sc);
549 DPRINTFN(1,("uhci_init: enabling\n"));
550 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
551 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
553 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
555 return (uhci_run(sc, 1)); /* and here we go... */
558 #if defined(__NetBSD__) || defined(__OpenBSD__)
560 uhci_activate(device_ptr_t self, enum devact act)
562 struct uhci_softc *sc = (struct uhci_softc *)self;
569 case DVACT_DEACTIVATE:
570 if (sc->sc_child != NULL)
571 rv = config_deactivate(sc->sc_child);
578 uhci_detach(struct uhci_softc *sc, int flags)
580 usbd_xfer_handle xfer;
583 if (sc->sc_child != NULL)
584 rv = config_detach(sc->sc_child, flags);
589 #if defined(__NetBSD__) || defined(__OpenBSD__)
590 powerhook_disestablish(sc->sc_powerhook);
591 shutdownhook_disestablish(sc->sc_shutdownhook);
594 /* Free all xfers associated with this HC. */
596 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
599 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
603 /* XXX free other data structures XXX */
610 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
612 return (usb_allocmem(bus, size, 0, dma));
616 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
618 usb_freemem(bus, dma);
622 uhci_allocx(struct usbd_bus *bus)
624 struct uhci_softc *sc = (struct uhci_softc *)bus;
625 usbd_xfer_handle xfer;
627 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
629 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
631 if (xfer->busy_free != XFER_FREE) {
632 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
637 xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_INTWAIT);
640 memset(xfer, 0, sizeof (struct uhci_xfer));
641 UXFER(xfer)->iinfo.sc = sc;
643 UXFER(xfer)->iinfo.isdone = 1;
644 xfer->busy_free = XFER_BUSY;
651 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
653 struct uhci_softc *sc = (struct uhci_softc *)bus;
656 if (xfer->busy_free != XFER_BUSY) {
657 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
661 xfer->busy_free = XFER_FREE;
662 if (!UXFER(xfer)->iinfo.isdone) {
663 printf("uhci_freex: !isdone\n");
667 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
671 * Shut down the controller when the system is going down.
674 uhci_shutdown(void *v)
676 uhci_softc_t *sc = v;
678 DPRINTF(("uhci_shutdown: stopping the HC\n"));
679 uhci_run(sc, 0); /* stop the controller */
683 * Handle suspend/resume.
685 * We need to switch to polling mode here, because this routine is
686 * called from an interrupt context. This is all right since we
687 * are almost suspended anyway.
690 uhci_power(int why, void *v)
692 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;
957 DPRINTFN(20, ("uhci_poll_hub\n"));
959 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
961 p = KERNADDR(&xfer->dmabuf, 0);
963 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
965 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
968 /* No change, try again in a while */
972 xfer->status = USBD_NORMAL_COMPLETION;
974 xfer->device->bus->intr_context++;
975 usb_transfer_complete(xfer);
976 xfer->device->bus->intr_context--;
981 uhci_root_intr_done(usbd_xfer_handle xfer)
986 uhci_root_ctrl_done(usbd_xfer_handle xfer)
991 * Let the last QH loop back to the high speed control transfer QH.
992 * This is what intel calls "bandwidth reclamation" and improves
993 * USB performance a lot for some devices.
994 * If we are already looping, just count it.
997 uhci_add_loop(uhci_softc_t *sc) {
1002 if (++sc->sc_loops == 1) {
1003 DPRINTFN(5,("uhci_start_loop: add\n"));
1004 /* Note, we don't loop back the soft pointer. */
1005 sc->sc_last_qh->qh.qh_hlink =
1006 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
1011 uhci_rem_loop(uhci_softc_t *sc) {
1016 if (--sc->sc_loops == 0) {
1017 DPRINTFN(5,("uhci_end_loop: remove\n"));
1018 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1022 /* Add high speed control QH, called from a critical section. */
1024 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1026 uhci_soft_qh_t *eqh;
1028 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
1029 eqh = sc->sc_hctl_end;
1030 sqh->hlink = eqh->hlink;
1031 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1033 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1034 sc->sc_hctl_end = sqh;
1035 #ifdef UHCI_CTL_LOOP
1040 /* Remove high speed control QH, called from a critical section. */
1042 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1044 uhci_soft_qh_t *pqh;
1046 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1047 #ifdef UHCI_CTL_LOOP
1051 * The T bit should be set in the elink of the QH so that the HC
1052 * doesn't follow the pointer. This condition may fail if the
1053 * the transferred packet was short so that the QH still points
1054 * at the last used TD.
1055 * In this case we set the T bit and wait a little for the HC
1056 * to stop looking at the TD.
1058 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1059 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1060 delay(UHCI_QH_REMOVE_DELAY);
1063 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1064 pqh->hlink = sqh->hlink;
1065 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1066 delay(UHCI_QH_REMOVE_DELAY);
1067 if (sc->sc_hctl_end == sqh)
1068 sc->sc_hctl_end = pqh;
1071 /* Add low speed control QH, called from a critical section. */
1073 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1075 uhci_soft_qh_t *eqh;
1077 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1078 eqh = sc->sc_lctl_end;
1079 sqh->hlink = eqh->hlink;
1080 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1082 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1083 sc->sc_lctl_end = sqh;
1086 /* Remove low speed control QH, called from a critical section. */
1088 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1090 uhci_soft_qh_t *pqh;
1092 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1093 /* See comment in uhci_remove_hs_ctrl() */
1094 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1095 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1096 delay(UHCI_QH_REMOVE_DELAY);
1098 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1099 pqh->hlink = sqh->hlink;
1100 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1101 delay(UHCI_QH_REMOVE_DELAY);
1102 if (sc->sc_lctl_end == sqh)
1103 sc->sc_lctl_end = pqh;
1106 /* Add bulk QH, called from a critical section. */
1108 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1110 uhci_soft_qh_t *eqh;
1112 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1113 eqh = sc->sc_bulk_end;
1114 sqh->hlink = eqh->hlink;
1115 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1117 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1118 sc->sc_bulk_end = sqh;
1122 /* Remove bulk QH, called from a critical section. */
1124 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1126 uhci_soft_qh_t *pqh;
1128 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1130 /* See comment in uhci_remove_hs_ctrl() */
1131 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1132 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1133 delay(UHCI_QH_REMOVE_DELAY);
1135 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1136 pqh->hlink = sqh->hlink;
1137 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1138 delay(UHCI_QH_REMOVE_DELAY);
1139 if (sc->sc_bulk_end == sqh)
1140 sc->sc_bulk_end = pqh;
1143 Static int uhci_intr1(uhci_softc_t *);
1146 uhci_intr(void *arg)
1148 uhci_softc_t *sc = arg;
1153 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1154 if (sc->sc_bus.use_polling) {
1156 printf("uhci_intr: ignored interrupt while polling\n");
1160 return (uhci_intr1(sc));
1164 uhci_intr1(uhci_softc_t *sc)
1171 * It can happen that an interrupt will be delivered to
1172 * us before the device has been fully attached and the
1173 * softc struct has been configured. Usually this happens
1174 * when kldloading the USB support as a module after the
1175 * system has been booted. If we detect this condition,
1176 * we need to squelch the unwanted interrupts until we're
1179 if (sc->sc_bus.bdev == NULL) {
1180 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1181 uhci_run(sc, 0); /* stop the controller */
1182 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1187 if (uhcidebug > 15) {
1188 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev)));
1192 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1193 if (status == 0) /* The interrupt was not for us. */
1196 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1197 if (sc->sc_suspend != PWR_RESUME)
1198 printf("uhci_intr: suspended sts=0x%x\n", status);
1201 if (sc->sc_suspend != PWR_RESUME) {
1202 printf("%s: interrupt while not operating ignored\n",
1203 USBDEVNAME(sc->sc_bus.bdev));
1204 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1209 if (status & UHCI_STS_USBINT)
1210 ack |= UHCI_STS_USBINT;
1211 if (status & UHCI_STS_USBEI)
1212 ack |= UHCI_STS_USBEI;
1213 if (status & UHCI_STS_RD) {
1216 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1219 if (status & UHCI_STS_HSE) {
1220 ack |= UHCI_STS_HSE;
1221 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
1223 if (status & UHCI_STS_HCPE) {
1224 ack |= UHCI_STS_HCPE;
1225 printf("%s: host controller process error\n",
1226 USBDEVNAME(sc->sc_bus.bdev));
1228 if (status & UHCI_STS_HCH) {
1229 /* no acknowledge needed */
1230 if (!sc->sc_dying) {
1231 printf("%s: host controller halted\n",
1232 USBDEVNAME(sc->sc_bus.bdev));
1241 return (0); /* nothing to acknowledge */
1242 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1244 sc->sc_bus.no_intrs++;
1245 usb_schedsoftintr(&sc->sc_bus);
1247 DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
1253 uhci_softintr(void *v)
1255 uhci_softc_t *sc = v;
1256 uhci_intr_info_t *ii;
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 LIST_FOREACH(ii, &sc->sc_intrhead, list)
1275 uhci_check_intr(sc, ii);
1277 #ifdef USB_USE_SOFTINTR
1278 if (sc->sc_softwake) {
1279 sc->sc_softwake = 0;
1280 wakeup(&sc->sc_softwake);
1282 #endif /* USB_USE_SOFTINTR */
1284 sc->sc_bus.intr_context--;
1287 /* Check for an interrupt. */
1289 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1291 uhci_soft_td_t *std, *lstd;
1294 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1297 printf("uhci_check_intr: no ii? %p\n", ii);
1301 if (ii->xfer->status == USBD_CANCELLED ||
1302 ii->xfer->status == USBD_TIMEOUT) {
1303 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1307 if (ii->stdstart == NULL)
1312 printf("uhci_check_intr: std==0\n");
1317 * If the last TD is still active we need to check whether there
1318 * is an error somewhere in the middle, or whether there was a
1319 * short packet (SPD and not ACTIVE).
1321 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1322 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1323 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1324 status = le32toh(std->td.td_status);
1325 /* If there's an active TD the xfer isn't done. */
1326 if (status & UHCI_TD_ACTIVE)
1328 /* Any kind of error makes the xfer done. */
1329 if (status & UHCI_TD_STALLED)
1331 /* We want short packets, and it is short: it's done */
1332 if ((status & UHCI_TD_SPD) &&
1333 UHCI_TD_GET_ACTLEN(status) <
1334 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) {
1338 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1343 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1344 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1348 /* Called from a critical section. */
1350 uhci_idone(uhci_intr_info_t *ii)
1352 usbd_xfer_handle xfer = ii->xfer;
1353 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1354 uhci_soft_td_t *std;
1355 u_int32_t status = 0, nstatus;
1358 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1365 printf("uhci_idone: ii is done!\n ");
1368 printf("uhci_idone: ii=%p is done!\n", ii);
1377 if (xfer->nframes != 0) {
1378 /* Isoc transfer, do things differently. */
1379 uhci_soft_td_t **stds = upipe->u.iso.stds;
1380 int i, n, nframes, len;
1382 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1384 nframes = xfer->nframes;
1386 n = UXFER(xfer)->curframe;
1387 for (i = 0; i < nframes; i++) {
1390 if (uhcidebug > 5) {
1391 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1395 if (++n >= UHCI_VFRAMELIST_COUNT)
1397 status = le32toh(std->td.td_status);
1398 len = UHCI_TD_GET_ACTLEN(status);
1399 xfer->frlengths[i] = len;
1402 upipe->u.iso.inuse -= nframes;
1403 xfer->actlen = actlen;
1404 xfer->status = USBD_NORMAL_COMPLETION;
1409 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1412 uhci_dump_tds(ii->stdstart);
1415 /* The transfer is done, compute actual length and status. */
1417 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1418 nstatus = le32toh(std->td.td_status);
1419 if (nstatus & UHCI_TD_ACTIVE)
1423 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1424 UHCI_TD_PID_SETUP) {
1425 actlen += UHCI_TD_GET_ACTLEN(status);
1428 * UHCI will report CRCTO in addition to a STALL or NAK
1429 * for a SETUP transaction. See section 3.2.2, "TD
1430 * CONTROL AND STATUS".
1432 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1433 status &= ~UHCI_TD_CRCTO;
1437 /* If there are left over TDs we need to update the toggle. */
1439 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1441 status &= UHCI_TD_ERROR;
1442 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1444 xfer->actlen = actlen;
1449 bitmask_snprintf((u_int32_t)status,
1450 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1451 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1452 sbuf, sizeof(sbuf));
1454 DPRINTFN((status == UHCI_TD_STALLED)*10,
1455 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1457 xfer->pipe->device->address,
1458 xfer->pipe->endpoint->edesc->bEndpointAddress,
1461 if (status == UHCI_TD_STALLED)
1462 xfer->status = USBD_STALLED;
1464 xfer->status = USBD_IOERROR; /* more info XXX */
1466 xfer->status = USBD_NORMAL_COMPLETION;
1470 usb_transfer_complete(xfer);
1471 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1475 * Called when a request does not complete.
1478 uhci_timeout(void *addr)
1480 uhci_intr_info_t *ii = addr;
1481 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1482 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1483 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1485 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1488 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1492 /* Execute the abort in a process context. */
1493 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer);
1494 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1498 uhci_timeout_task(void *addr)
1500 usbd_xfer_handle xfer = addr;
1502 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1505 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1510 * Wait here until controller claims to have an interrupt.
1511 * Then call uhci_intr and return. Use timeout to avoid waiting
1513 * Only used during boot when interrupts are not enabled yet.
1516 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1518 int timo = xfer->timeout;
1519 uhci_intr_info_t *ii;
1521 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1523 xfer->status = USBD_IN_PROGRESS;
1524 for (; timo >= 0; timo--) {
1525 usb_delay_ms(&sc->sc_bus, 1);
1526 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1527 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1529 if (xfer->status != USBD_IN_PROGRESS)
1534 DPRINTF(("uhci_waitintr: timeout\n"));
1535 for (ii = LIST_FIRST(&sc->sc_intrhead);
1536 ii != NULL && ii->xfer != xfer;
1537 ii = LIST_NEXT(ii, list))
1541 panic("uhci_waitintr: lost intr_info");
1547 uhci_poll(struct usbd_bus *bus)
1549 uhci_softc_t *sc = (uhci_softc_t *)bus;
1551 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1556 uhci_reset(uhci_softc_t *sc)
1560 UHCICMD(sc, UHCI_CMD_HCRESET);
1561 /* The reset bit goes low when the controller is done. */
1562 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1563 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1564 usb_delay_ms(&sc->sc_bus, 1);
1565 if (n >= UHCI_RESET_TIMEOUT)
1566 printf("%s: controller did not reset\n",
1567 USBDEVNAME(sc->sc_bus.bdev));
1571 uhci_run(uhci_softc_t *sc, int run)
1578 DPRINTF(("uhci_run: setting run=%d\n", run));
1579 cmd = UREAD2(sc, UHCI_CMD);
1583 cmd &= ~UHCI_CMD_RS;
1585 for(n = 0; n < 10; n++) {
1586 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1587 /* return when we've entered the state we want */
1588 if (run == running) {
1590 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1591 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1592 return (USBD_NORMAL_COMPLETION);
1594 usb_delay_ms(&sc->sc_bus, 1);
1597 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1598 run ? "start" : "stop");
1599 return (USBD_IOERROR);
1603 * Memory management routines.
1604 * uhci_alloc_std allocates TDs
1605 * uhci_alloc_sqh allocates QHs
1606 * These two routines do their own free list management,
1607 * partly for speed, partly because allocating DMAable memory
1608 * has page size granularaity so much memory would be wasted if
1609 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1613 uhci_alloc_std(uhci_softc_t *sc)
1615 uhci_soft_td_t *std;
1620 if (sc->sc_freetds == NULL) {
1621 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1622 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1623 UHCI_TD_ALIGN, &dma);
1626 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1627 offs = i * UHCI_STD_SIZE;
1628 std = KERNADDR(&dma, offs);
1629 std->physaddr = DMAADDR(&dma, offs);
1630 std->link.std = sc->sc_freetds;
1631 sc->sc_freetds = std;
1634 std = sc->sc_freetds;
1635 sc->sc_freetds = std->link.std;
1636 memset(&std->td, 0, sizeof(uhci_td_t));
1641 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1644 #define TD_IS_FREE 0x12345678
1645 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1646 printf("uhci_free_std: freeing free TD %p\n", std);
1649 std->td.td_token = htole32(TD_IS_FREE);
1651 std->link.std = sc->sc_freetds;
1652 sc->sc_freetds = std;
1656 uhci_alloc_sqh(uhci_softc_t *sc)
1658 uhci_soft_qh_t *sqh;
1663 if (sc->sc_freeqhs == NULL) {
1664 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1665 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1666 UHCI_QH_ALIGN, &dma);
1669 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1670 offs = i * UHCI_SQH_SIZE;
1671 sqh = KERNADDR(&dma, offs);
1672 sqh->physaddr = DMAADDR(&dma, offs);
1673 sqh->hlink = sc->sc_freeqhs;
1674 sc->sc_freeqhs = sqh;
1677 sqh = sc->sc_freeqhs;
1678 sc->sc_freeqhs = sqh->hlink;
1679 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1684 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1686 sqh->hlink = sc->sc_freeqhs;
1687 sc->sc_freeqhs = sqh;
1691 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1692 uhci_soft_td_t *stdend)
1696 for (; std != stdend; std = p) {
1698 uhci_free_std(sc, std);
1703 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1704 int rd, u_int16_t flags, usb_dma_t *dma,
1705 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1707 uhci_soft_td_t *p, *lastp;
1708 uhci_physaddr_t lastlink;
1709 int i, ntd, l, tog, maxp;
1711 int addr = upipe->pipe.device->address;
1712 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1714 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1715 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1716 upipe->pipe.device->speed, flags));
1717 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1719 printf("uhci_alloc_std_chain: maxp=0\n");
1720 return (USBD_INVAL);
1722 ntd = (len + maxp - 1) / maxp;
1723 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1725 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1728 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1729 return (USBD_NORMAL_COMPLETION);
1731 tog = upipe->nexttoggle;
1734 upipe->nexttoggle = tog ^ 1;
1736 lastlink = UHCI_PTR_T;
1738 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1739 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1740 status |= UHCI_TD_LS;
1741 if (flags & USBD_SHORT_XFER_OK)
1742 status |= UHCI_TD_SPD;
1743 for (i = ntd; i >= 0; i--) {
1744 p = uhci_alloc_std(sc);
1746 uhci_free_std_chain(sc, lastp, NULL);
1747 return (USBD_NOMEM);
1749 p->link.std = lastp;
1750 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1752 lastlink = p->physaddr;
1753 p->td.td_status = htole32(status);
1757 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1763 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1764 UHCI_TD_OUT(l, endpt, addr, tog));
1765 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1769 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1770 upipe->nexttoggle));
1771 return (USBD_NORMAL_COMPLETION);
1775 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1777 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1778 upipe->nexttoggle = 0;
1782 uhci_noop(usbd_pipe_handle pipe)
1787 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1791 /* Insert last in queue. */
1792 err = usb_insert_transfer(xfer);
1797 * Pipe isn't running (otherwise err would be USBD_INPROG),
1798 * so start it first.
1800 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1804 uhci_device_bulk_start(usbd_xfer_handle xfer)
1806 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1807 usbd_device_handle dev = upipe->pipe.device;
1808 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1809 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1810 uhci_soft_td_t *data, *dataend;
1811 uhci_soft_qh_t *sqh;
1813 int len, isread, endpt;
1815 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1816 xfer, xfer->length, xfer->flags, ii));
1819 return (USBD_IOERROR);
1822 if (xfer->rqflags & URQ_REQUEST)
1823 panic("uhci_device_bulk_transfer: a request");
1827 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1828 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1829 sqh = upipe->u.bulk.sqh;
1831 upipe->u.bulk.isread = isread;
1832 upipe->u.bulk.length = len;
1834 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1835 &xfer->dmabuf, &data, &dataend);
1838 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1841 if (uhcidebug > 8) {
1842 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1843 uhci_dump_tds(data);
1847 /* Set up interrupt info. */
1849 ii->stdstart = data;
1850 ii->stdend = dataend;
1853 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1859 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1862 uhci_add_bulk(sc, sqh);
1863 uhci_add_intr_info(sc, ii);
1865 if (xfer->timeout && !sc->sc_bus.use_polling) {
1866 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1869 xfer->status = USBD_IN_PROGRESS;
1873 if (uhcidebug > 10) {
1874 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1875 uhci_dump_tds(data);
1879 if (sc->sc_bus.use_polling)
1880 uhci_waitintr(sc, xfer);
1882 return (USBD_IN_PROGRESS);
1885 /* Abort a device bulk request. */
1887 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1889 DPRINTF(("uhci_device_bulk_abort:\n"));
1890 uhci_abort_xfer(xfer, USBD_CANCELLED);
1894 * Abort a device request.
1895 * If this routine is called from a critical section. It guarentees that
1896 * the request will be removed from the hardware scheduling and that
1897 * the callback for it will be called with USBD_CANCELLED status.
1898 * It's impossible to guarantee that the requested transfer will not
1899 * have happened since the hardware runs concurrently.
1900 * If the transaction has already happened we rely on the ordinary
1901 * interrupt processing to process it.
1904 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1906 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1907 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1908 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1909 uhci_soft_td_t *std;
1911 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1914 /* If we're dying, just do the software part. */
1916 xfer->status = status; /* make software ignore it */
1917 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1918 usb_transfer_complete(xfer);
1923 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
1924 panic("uhci_abort_xfer: not in process context");
1927 * Step 1: Make interrupt routine and hardware ignore xfer.
1930 xfer->status = status; /* make software ignore it */
1931 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1932 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1933 for (std = ii->stdstart; std != NULL; std = std->link.std)
1934 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1938 * Step 2: Wait until we know hardware has finished any possible
1939 * use of the xfer. Also make sure the soft interrupt routine
1942 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1944 #ifdef USB_USE_SOFTINTR
1945 sc->sc_softwake = 1;
1946 #endif /* USB_USE_SOFTINTR */
1947 usb_schedsoftintr(&sc->sc_bus);
1948 #ifdef USB_USE_SOFTINTR
1949 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1950 tsleep(&sc->sc_softwake, 0, "uhciab", 0);
1951 #endif /* USB_USE_SOFTINTR */
1955 * Step 3: Execute callback.
1959 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1965 usb_transfer_complete(xfer);
1969 /* Close a device bulk pipe. */
1971 uhci_device_bulk_close(usbd_pipe_handle pipe)
1973 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1974 usbd_device_handle dev = upipe->pipe.device;
1975 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1977 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1981 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1985 /* Insert last in queue. */
1986 err = usb_insert_transfer(xfer);
1991 * Pipe isn't running (otherwise err would be USBD_INPROG),
1992 * so start it first.
1994 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1998 uhci_device_ctrl_start(usbd_xfer_handle xfer)
2000 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2004 return (USBD_IOERROR);
2007 if (!(xfer->rqflags & URQ_REQUEST))
2008 panic("uhci_device_ctrl_transfer: not a request");
2011 err = uhci_device_request(xfer);
2015 if (sc->sc_bus.use_polling)
2016 uhci_waitintr(sc, xfer);
2017 return (USBD_IN_PROGRESS);
2021 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2025 /* Insert last in queue. */
2026 err = usb_insert_transfer(xfer);
2031 * Pipe isn't running (otherwise err would be USBD_INPROG),
2032 * so start it first.
2034 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2038 uhci_device_intr_start(usbd_xfer_handle xfer)
2040 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2041 usbd_device_handle dev = upipe->pipe.device;
2042 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2043 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2044 uhci_soft_td_t *data, *dataend;
2045 uhci_soft_qh_t *sqh;
2051 return (USBD_IOERROR);
2053 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2054 xfer, xfer->length, xfer->flags));
2057 if (xfer->rqflags & URQ_REQUEST)
2058 panic("uhci_device_intr_transfer: a request");
2061 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2062 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2063 sqh = upipe->u.bulk.sqh;
2065 upipe->u.intr.isread = isread;
2067 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread,
2068 xfer->flags, &xfer->dmabuf, &data,
2072 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2075 if (uhcidebug > 10) {
2076 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2077 uhci_dump_tds(data);
2078 uhci_dump_qh(upipe->u.intr.qhs[0]);
2083 /* Set up interrupt info. */
2085 ii->stdstart = data;
2086 ii->stdend = dataend;
2089 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2094 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2095 upipe->u.intr.qhs[0]));
2096 for (i = 0; i < upipe->u.intr.npoll; i++) {
2097 sqh = upipe->u.intr.qhs[i];
2099 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2101 uhci_add_intr_info(sc, ii);
2102 xfer->status = USBD_IN_PROGRESS;
2106 if (uhcidebug > 10) {
2107 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2108 uhci_dump_tds(data);
2109 uhci_dump_qh(upipe->u.intr.qhs[0]);
2113 return (USBD_IN_PROGRESS);
2116 /* Abort a device control request. */
2118 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2120 DPRINTF(("uhci_device_ctrl_abort:\n"));
2121 uhci_abort_xfer(xfer, USBD_CANCELLED);
2124 /* Close a device control pipe. */
2126 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2130 /* Abort a device interrupt request. */
2132 uhci_device_intr_abort(usbd_xfer_handle xfer)
2134 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2135 if (xfer->pipe->intrxfer == xfer) {
2136 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2137 xfer->pipe->intrxfer = NULL;
2139 uhci_abort_xfer(xfer, USBD_CANCELLED);
2142 /* Close a device interrupt pipe. */
2144 uhci_device_intr_close(usbd_pipe_handle pipe)
2146 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2147 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2150 /* Unlink descriptors from controller data structures. */
2151 npoll = upipe->u.intr.npoll;
2153 for (i = 0; i < npoll; i++)
2154 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2158 * We now have to wait for any activity on the physical
2159 * descriptors to stop.
2161 usb_delay_ms(&sc->sc_bus, 2);
2163 for(i = 0; i < npoll; i++)
2164 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2165 free(upipe->u.intr.qhs, M_USBHC);
2167 /* XXX free other resources */
2171 uhci_device_request(usbd_xfer_handle xfer)
2173 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2174 usb_device_request_t *req = &xfer->request;
2175 usbd_device_handle dev = upipe->pipe.device;
2176 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2177 int addr = dev->address;
2178 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2179 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2180 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2181 uhci_soft_qh_t *sqh;
2187 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2188 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2189 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2190 UGETW(req->wIndex), UGETW(req->wLength),
2193 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2194 isread = req->bmRequestType & UT_READ;
2195 len = UGETW(req->wLength);
2197 setup = upipe->u.ctl.setup;
2198 stat = upipe->u.ctl.stat;
2199 sqh = upipe->u.ctl.sqh;
2201 /* Set up data transaction */
2203 upipe->nexttoggle = 1;
2204 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2205 &xfer->dmabuf, &data, &dataend);
2209 dataend->link.std = stat;
2210 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2214 upipe->u.ctl.length = len;
2216 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2218 setup->link.std = next;
2219 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2220 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2222 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2223 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2224 stat->link.std = NULL;
2225 stat->td.td_link = htole32(UHCI_PTR_T);
2226 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2227 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2230 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2231 UHCI_TD_IN (0, endpt, addr, 1));
2232 stat->td.td_buffer = htole32(0);
2235 if (uhcidebug > 10) {
2236 DPRINTF(("uhci_device_request: before transfer\n"));
2237 uhci_dump_tds(setup);
2241 /* Set up interrupt info. */
2243 ii->stdstart = setup;
2247 printf("uhci_device_request: not done, ii=%p\n", ii);
2253 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2256 if (dev->speed == USB_SPEED_LOW)
2257 uhci_add_ls_ctrl(sc, sqh);
2259 uhci_add_hs_ctrl(sc, sqh);
2260 uhci_add_intr_info(sc, ii);
2262 if (uhcidebug > 12) {
2263 uhci_soft_td_t *std;
2264 uhci_soft_qh_t *xqh;
2265 uhci_soft_qh_t *sxqh;
2267 uhci_physaddr_t link;
2268 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2269 for (std = sc->sc_vframes[0].htd, link = 0;
2270 (link & UHCI_PTR_QH) == 0;
2271 std = std->link.std) {
2272 link = le32toh(std->td.td_link);
2275 sxqh = (uhci_soft_qh_t *)std;
2279 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2280 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2283 DPRINTF(("Enqueued QH:\n"));
2285 uhci_dump_tds(sqh->elink);
2288 if (xfer->timeout && !sc->sc_bus.use_polling) {
2289 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2292 xfer->status = USBD_IN_PROGRESS;
2295 return (USBD_NORMAL_COMPLETION);
2299 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2303 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2305 /* Put it on our queue, */
2306 err = usb_insert_transfer(xfer);
2308 /* bail out on error, */
2309 if (err && err != USBD_IN_PROGRESS)
2312 /* XXX should check inuse here */
2314 /* insert into schedule, */
2315 uhci_device_isoc_enter(xfer);
2317 /* and start if the pipe wasn't running */
2319 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2325 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2327 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2328 usbd_device_handle dev = upipe->pipe.device;
2329 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2330 struct iso *iso = &upipe->u.iso;
2331 uhci_soft_td_t *std;
2332 u_int32_t buf, len, status;
2333 int i, next, nframes;
2335 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2337 iso->inuse, iso->next, xfer, xfer->nframes));
2342 if (xfer->status == USBD_IN_PROGRESS) {
2343 /* This request has already been entered into the frame list */
2344 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2349 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2350 printf("uhci_device_isoc_enter: overflow!\n");
2355 /* Not in use yet, schedule it a few frames ahead. */
2356 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2357 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2360 xfer->status = USBD_IN_PROGRESS;
2361 UXFER(xfer)->curframe = next;
2363 buf = DMAADDR(&xfer->dmabuf, 0);
2364 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2367 nframes = xfer->nframes;
2369 for (i = 0; i < nframes; i++) {
2370 std = iso->stds[next];
2371 if (++next >= UHCI_VFRAMELIST_COUNT)
2373 len = xfer->frlengths[i];
2374 std->td.td_buffer = htole32(buf);
2375 if (i == nframes - 1)
2376 status |= UHCI_TD_IOC;
2377 std->td.td_status = htole32(status);
2378 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2379 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2381 if (uhcidebug > 5) {
2382 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2389 iso->inuse += xfer->nframes;
2395 uhci_device_isoc_start(usbd_xfer_handle xfer)
2397 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2398 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2399 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2400 uhci_soft_td_t *end;
2403 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2406 return (USBD_IOERROR);
2409 if (xfer->status != USBD_IN_PROGRESS)
2410 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2413 /* Find the last TD */
2414 i = UXFER(xfer)->curframe + xfer->nframes;
2415 if (i >= UHCI_VFRAMELIST_COUNT)
2416 i -= UHCI_VFRAMELIST_COUNT;
2417 end = upipe->u.iso.stds[i];
2421 printf("uhci_device_isoc_start: end == NULL\n");
2422 return (USBD_INVAL);
2428 /* Set up interrupt info. */
2434 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2437 uhci_add_intr_info(sc, ii);
2441 return (USBD_IN_PROGRESS);
2445 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2447 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2448 uhci_soft_td_t **stds = upipe->u.iso.stds;
2449 uhci_soft_td_t *std;
2450 int i, n, nframes, maxlen, len;
2454 /* Transfer is already done. */
2455 if (xfer->status != USBD_NOT_STARTED &&
2456 xfer->status != USBD_IN_PROGRESS) {
2461 /* Give xfer the requested abort code. */
2462 xfer->status = USBD_CANCELLED;
2464 /* make hardware ignore it, */
2465 nframes = xfer->nframes;
2466 n = UXFER(xfer)->curframe;
2468 for (i = 0; i < nframes; i++) {
2470 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2471 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2474 if (++n >= UHCI_VFRAMELIST_COUNT)
2478 /* and wait until we are sure the hardware has finished. */
2482 UXFER(xfer)->iinfo.isdone = 1;
2484 /* Run callback and remove from interrupt list. */
2485 usb_transfer_complete(xfer);
2491 uhci_device_isoc_close(usbd_pipe_handle pipe)
2493 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2494 usbd_device_handle dev = upipe->pipe.device;
2495 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2496 uhci_soft_td_t *std, *vstd;
2501 * Make sure all TDs are marked as inactive.
2502 * Wait for completion.
2506 iso = &upipe->u.iso;
2508 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2509 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2510 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2513 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2515 for (vstd = sc->sc_vframes[i].htd;
2516 vstd != NULL && vstd->link.std != std;
2517 vstd = vstd->link.std)
2521 printf("uhci_device_isoc_close: %p not found\n", std);
2525 vstd->link = std->link;
2526 vstd->td.td_link = std->td.td_link;
2527 uhci_free_std(sc, std);
2531 free(iso->stds, M_USBHC);
2535 uhci_setup_isoc(usbd_pipe_handle pipe)
2537 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2538 usbd_device_handle dev = upipe->pipe.device;
2539 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2540 int addr = upipe->pipe.device->address;
2541 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2542 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2543 uhci_soft_td_t *std, *vstd;
2548 iso = &upipe->u.iso;
2549 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2550 M_USBHC, M_INTWAIT);
2552 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2553 UHCI_TD_OUT(0, endpt, addr, 0);
2555 /* Allocate the TDs and mark as inactive; */
2556 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2557 std = uhci_alloc_std(sc);
2560 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2561 std->td.td_token = htole32(token);
2565 /* Insert TDs into schedule. */
2567 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2569 vstd = sc->sc_vframes[i].htd;
2570 std->link = vstd->link;
2571 std->td.td_link = vstd->td.td_link;
2572 vstd->link.std = std;
2573 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2580 return (USBD_NORMAL_COMPLETION);
2584 uhci_free_std(sc, iso->stds[i]);
2585 free(iso->stds, M_USBHC);
2586 return (USBD_NOMEM);
2590 uhci_device_isoc_done(usbd_xfer_handle xfer)
2592 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2594 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2596 if (ii->xfer != xfer)
2597 /* Not on interrupt list, ignore it. */
2600 if (!uhci_active_intr_info(ii))
2604 if (xfer->busy_free != XFER_BUSY) {
2605 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2606 xfer, xfer->busy_free);
2610 if (ii->stdend == NULL) {
2611 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2619 /* Turn off the interrupt since it is active even if the TD is not. */
2620 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2622 uhci_del_intr_info(ii); /* remove from active list */
2625 if (ii->stdend == NULL) {
2626 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2636 uhci_device_intr_done(usbd_xfer_handle xfer)
2638 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2639 uhci_softc_t *sc = ii->sc;
2640 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2641 uhci_soft_qh_t *sqh;
2644 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2646 npoll = upipe->u.intr.npoll;
2647 for(i = 0; i < npoll; i++) {
2648 sqh = upipe->u.intr.qhs[i];
2650 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2652 uhci_free_std_chain(sc, ii->stdstart, NULL);
2655 if (xfer->pipe->repeat) {
2656 uhci_soft_td_t *data, *dataend;
2658 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2660 /* This alloc cannot fail since we freed the chain above. */
2661 uhci_alloc_std_chain(upipe, sc, xfer->length,
2662 upipe->u.intr.isread, xfer->flags,
2663 &xfer->dmabuf, &data, &dataend);
2664 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2667 if (uhcidebug > 10) {
2668 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2669 uhci_dump_tds(data);
2670 uhci_dump_qh(upipe->u.intr.qhs[0]);
2674 ii->stdstart = data;
2675 ii->stdend = dataend;
2678 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2682 for (i = 0; i < npoll; i++) {
2683 sqh = upipe->u.intr.qhs[i];
2685 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2687 xfer->status = USBD_IN_PROGRESS;
2688 /* The ii is already on the examined list, just leave it. */
2690 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2691 if (uhci_active_intr_info(ii))
2692 uhci_del_intr_info(ii);
2696 /* Deallocate request data structures */
2698 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2700 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2701 uhci_softc_t *sc = ii->sc;
2702 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2705 if (!(xfer->rqflags & URQ_REQUEST))
2706 panic("uhci_device_ctrl_done: not a request");
2709 if (!uhci_active_intr_info(ii))
2712 uhci_del_intr_info(ii); /* remove from active list */
2714 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2715 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2717 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2719 if (upipe->u.ctl.length != 0)
2720 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2722 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2725 /* Deallocate request data structures */
2727 uhci_device_bulk_done(usbd_xfer_handle xfer)
2729 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2730 uhci_softc_t *sc = ii->sc;
2731 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2733 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2734 xfer, ii, sc, upipe));
2736 if (!uhci_active_intr_info(ii))
2739 uhci_del_intr_info(ii); /* remove from active list */
2741 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2743 uhci_free_std_chain(sc, ii->stdstart, NULL);
2745 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2748 /* Add interrupt QH, called with vflock. */
2750 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2752 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2753 uhci_soft_qh_t *eqh;
2755 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2758 sqh->hlink = eqh->hlink;
2759 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2761 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2766 /* Remove interrupt QH. */
2768 uhci_remove_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 *pqh;
2773 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2775 /* See comment in uhci_remove_ctrl() */
2776 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2777 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2778 delay(UHCI_QH_REMOVE_DELAY);
2781 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2782 pqh->hlink = sqh->hlink;
2783 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2784 delay(UHCI_QH_REMOVE_DELAY);
2791 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2793 uhci_soft_qh_t *sqh;
2795 u_int bestbw, bw, bestoffs, offs;
2797 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2799 printf("uhci_setintr: 0 interval\n");
2800 return (USBD_INVAL);
2803 if (ival > UHCI_VFRAMELIST_COUNT)
2804 ival = UHCI_VFRAMELIST_COUNT;
2805 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2806 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2808 upipe->u.intr.npoll = npoll;
2810 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_INTWAIT);
2813 * Figure out which offset in the schedule that has most
2814 * bandwidth left over.
2816 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2817 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2818 for (bw = i = 0; i < npoll; i++)
2819 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2825 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2827 for(i = 0; i < npoll; i++) {
2828 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2830 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2831 sqh->pos = MOD(i * ival + bestoffs);
2836 /* Enter QHs into the controller data structures. */
2837 for(i = 0; i < npoll; i++)
2838 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2841 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2842 return (USBD_NORMAL_COMPLETION);
2845 /* Open a new pipe. */
2847 uhci_open(usbd_pipe_handle pipe)
2849 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2850 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2851 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2855 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2856 pipe, pipe->device->address,
2857 ed->bEndpointAddress, sc->sc_addr));
2859 upipe->aborting = 0;
2860 upipe->nexttoggle = 0;
2862 if (pipe->device->address == sc->sc_addr) {
2863 switch (ed->bEndpointAddress) {
2864 case USB_CONTROL_ENDPOINT:
2865 pipe->methods = &uhci_root_ctrl_methods;
2867 case UE_DIR_IN | UHCI_INTR_ENDPT:
2868 pipe->methods = &uhci_root_intr_methods;
2871 return (USBD_INVAL);
2874 switch (ed->bmAttributes & UE_XFERTYPE) {
2876 pipe->methods = &uhci_device_ctrl_methods;
2877 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2878 if (upipe->u.ctl.sqh == NULL)
2880 upipe->u.ctl.setup = uhci_alloc_std(sc);
2881 if (upipe->u.ctl.setup == NULL) {
2882 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2885 upipe->u.ctl.stat = uhci_alloc_std(sc);
2886 if (upipe->u.ctl.stat == NULL) {
2887 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2888 uhci_free_std(sc, upipe->u.ctl.setup);
2891 err = usb_allocmem(&sc->sc_bus,
2892 sizeof(usb_device_request_t),
2893 0, &upipe->u.ctl.reqdma);
2895 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2896 uhci_free_std(sc, upipe->u.ctl.setup);
2897 uhci_free_std(sc, upipe->u.ctl.stat);
2902 pipe->methods = &uhci_device_intr_methods;
2903 ival = pipe->interval;
2904 if (ival == USBD_DEFAULT_INTERVAL)
2905 ival = ed->bInterval;
2906 return (uhci_device_setintr(sc, upipe, ival));
2907 case UE_ISOCHRONOUS:
2908 pipe->methods = &uhci_device_isoc_methods;
2909 return (uhci_setup_isoc(pipe));
2911 pipe->methods = &uhci_device_bulk_methods;
2912 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2913 if (upipe->u.bulk.sqh == NULL)
2918 return (USBD_NORMAL_COMPLETION);
2921 return (USBD_NOMEM);
2925 * Data structures and routines to emulate the root hub.
2927 usb_device_descriptor_t uhci_devd = {
2928 USB_DEVICE_DESCRIPTOR_SIZE,
2929 UDESC_DEVICE, /* type */
2930 {0x00, 0x01}, /* USB version */
2931 UDCLASS_HUB, /* class */
2932 UDSUBCLASS_HUB, /* subclass */
2933 UDPROTO_FSHUB, /* protocol */
2934 64, /* max packet */
2935 {0},{0},{0x00,0x01}, /* device id */
2936 1,2,0, /* string indicies */
2937 1 /* # of configurations */
2940 usb_config_descriptor_t uhci_confd = {
2941 USB_CONFIG_DESCRIPTOR_SIZE,
2943 {USB_CONFIG_DESCRIPTOR_SIZE +
2944 USB_INTERFACE_DESCRIPTOR_SIZE +
2945 USB_ENDPOINT_DESCRIPTOR_SIZE},
2953 usb_interface_descriptor_t uhci_ifcd = {
2954 USB_INTERFACE_DESCRIPTOR_SIZE,
2965 usb_endpoint_descriptor_t uhci_endpd = {
2966 USB_ENDPOINT_DESCRIPTOR_SIZE,
2968 UE_DIR_IN | UHCI_INTR_ENDPT,
2974 usb_hub_descriptor_t uhci_hubd_piix = {
2975 USB_HUB_DESCRIPTOR_SIZE,
2978 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2979 50, /* power on to power good */
2981 { 0x00 }, /* both ports are removable */
2985 uhci_str(usb_string_descriptor_t *p, int l, char *s)
2991 p->bLength = 2 * strlen(s) + 2;
2994 p->bDescriptorType = UDESC_STRING;
2996 for (i = 0; s[i] && l > 1; i++, l -= 2)
2997 USETW2(p->bString[i], 0, s[i]);
3002 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3003 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3004 * should not be used by the USB subsystem. As we cannot issue a
3005 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3006 * will be enabled as part of the reset.
3008 * On the VT83C572, the port cannot be successfully enabled until the
3009 * outstanding "port enable change" and "connection status change"
3010 * events have been reset.
3013 uhci_portreset(uhci_softc_t *sc, int index)
3018 port = UHCI_PORTSC1;
3019 else if (index == 2)
3020 port = UHCI_PORTSC2;
3022 return (USBD_IOERROR);
3024 x = URWMASK(UREAD2(sc, port));
3025 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3027 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3029 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3030 index, UREAD2(sc, port)));
3032 x = URWMASK(UREAD2(sc, port));
3033 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3037 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3038 index, UREAD2(sc, port)));
3040 x = URWMASK(UREAD2(sc, port));
3041 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3043 for (lim = 10; --lim > 0;) {
3044 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3046 x = UREAD2(sc, port);
3048 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3051 if (!(x & UHCI_PORTSC_CCS)) {
3053 * No device is connected (or was disconnected
3054 * during reset). Consider the port reset.
3055 * The delay must be long enough to ensure on
3056 * the initial iteration that the device
3057 * connection will have been registered. 50ms
3058 * appears to be sufficient, but 20ms is not.
3060 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3065 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3067 * Port enabled changed and/or connection
3068 * status changed were set. Reset either or
3069 * both raised flags (by writing a 1 to that
3070 * bit), and wait again for state to settle.
3072 UWRITE2(sc, port, URWMASK(x) |
3073 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3077 if (x & UHCI_PORTSC_PE)
3078 /* Port is enabled */
3081 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3084 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3085 index, UREAD2(sc, port)));
3088 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3089 return (USBD_TIMEOUT);
3093 return (USBD_NORMAL_COMPLETION);
3097 * Simulate a hardware hub by handling all the necessary requests.
3100 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3104 /* Insert last in queue. */
3105 err = usb_insert_transfer(xfer);
3110 * Pipe isn't running (otherwise err would be USBD_INPROG),
3111 * so start it first.
3113 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3117 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3119 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3120 usb_device_request_t *req;
3123 int len, value, index, status, change, l, totlen = 0;
3124 usb_port_status_t ps;
3128 return (USBD_IOERROR);
3131 if (!(xfer->rqflags & URQ_REQUEST))
3132 panic("uhci_root_ctrl_transfer: not a request");
3134 req = &xfer->request;
3136 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3137 req->bmRequestType, req->bRequest));
3139 len = UGETW(req->wLength);
3140 value = UGETW(req->wValue);
3141 index = UGETW(req->wIndex);
3144 buf = KERNADDR(&xfer->dmabuf, 0);
3146 #define C(x,y) ((x) | ((y) << 8))
3147 switch(C(req->bRequest, req->bmRequestType)) {
3148 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3149 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3150 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3152 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3153 * for the integrated root hub.
3156 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3158 *(u_int8_t *)buf = sc->sc_conf;
3162 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3163 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3164 switch(value >> 8) {
3166 if ((value & 0xff) != 0) {
3170 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3171 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3172 memcpy(buf, &uhci_devd, l);
3175 if ((value & 0xff) != 0) {
3179 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3180 memcpy(buf, &uhci_confd, l);
3181 buf = (char *)buf + l;
3183 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3185 memcpy(buf, &uhci_ifcd, l);
3186 buf = (char *)buf + l;
3188 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3190 memcpy(buf, &uhci_endpd, l);
3195 *(u_int8_t *)buf = 0;
3197 switch (value & 0xff) {
3198 case 1: /* Vendor */
3199 totlen = uhci_str(buf, len, sc->sc_vendor);
3201 case 2: /* Product */
3202 totlen = uhci_str(buf, len, "UHCI root hub");
3211 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3213 *(u_int8_t *)buf = 0;
3217 case C(UR_GET_STATUS, UT_READ_DEVICE):
3219 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3223 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3224 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3226 USETW(((usb_status_t *)buf)->wStatus, 0);
3230 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3231 if (value >= USB_MAX_DEVICES) {
3235 sc->sc_addr = value;
3237 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3238 if (value != 0 && value != 1) {
3242 sc->sc_conf = value;
3244 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3246 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3247 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3248 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3251 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3253 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3256 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3258 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3259 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3260 "port=%d feature=%d\n",
3263 port = UHCI_PORTSC1;
3264 else if (index == 2)
3265 port = UHCI_PORTSC2;
3271 case UHF_PORT_ENABLE:
3272 x = URWMASK(UREAD2(sc, port));
3273 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3275 case UHF_PORT_SUSPEND:
3276 x = URWMASK(UREAD2(sc, port));
3277 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3279 case UHF_PORT_RESET:
3280 x = URWMASK(UREAD2(sc, port));
3281 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3283 case UHF_C_PORT_CONNECTION:
3284 x = URWMASK(UREAD2(sc, port));
3285 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3287 case UHF_C_PORT_ENABLE:
3288 x = URWMASK(UREAD2(sc, port));
3289 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3291 case UHF_C_PORT_OVER_CURRENT:
3292 x = URWMASK(UREAD2(sc, port));
3293 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3295 case UHF_C_PORT_RESET:
3297 err = USBD_NORMAL_COMPLETION;
3299 case UHF_PORT_CONNECTION:
3300 case UHF_PORT_OVER_CURRENT:
3301 case UHF_PORT_POWER:
3302 case UHF_PORT_LOW_SPEED:
3303 case UHF_C_PORT_SUSPEND:
3309 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3311 port = UHCI_PORTSC1;
3312 else if (index == 2)
3313 port = UHCI_PORTSC2;
3320 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3321 UHCI_PORTSC_LS_SHIFT;
3325 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3330 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3332 memcpy(buf, &uhci_hubd_piix, l);
3334 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3339 memset(buf, 0, len);
3342 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3344 port = UHCI_PORTSC1;
3345 else if (index == 2)
3346 port = UHCI_PORTSC2;
3355 x = UREAD2(sc, port);
3356 status = change = 0;
3357 if (x & UHCI_PORTSC_CCS)
3358 status |= UPS_CURRENT_CONNECT_STATUS;
3359 if (x & UHCI_PORTSC_CSC)
3360 change |= UPS_C_CONNECT_STATUS;
3361 if (x & UHCI_PORTSC_PE)
3362 status |= UPS_PORT_ENABLED;
3363 if (x & UHCI_PORTSC_POEDC)
3364 change |= UPS_C_PORT_ENABLED;
3365 if (x & UHCI_PORTSC_OCI)
3366 status |= UPS_OVERCURRENT_INDICATOR;
3367 if (x & UHCI_PORTSC_OCIC)
3368 change |= UPS_C_OVERCURRENT_INDICATOR;
3369 if (x & UHCI_PORTSC_SUSP)
3370 status |= UPS_SUSPEND;
3371 if (x & UHCI_PORTSC_LSDA)
3372 status |= UPS_LOW_SPEED;
3373 status |= UPS_PORT_POWER;
3375 change |= UPS_C_PORT_RESET;
3376 USETW(ps.wPortStatus, status);
3377 USETW(ps.wPortChange, change);
3378 l = min(len, sizeof ps);
3379 memcpy(buf, &ps, l);
3382 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3385 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3387 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3389 port = UHCI_PORTSC1;
3390 else if (index == 2)
3391 port = UHCI_PORTSC2;
3397 case UHF_PORT_ENABLE:
3398 x = URWMASK(UREAD2(sc, port));
3399 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3401 case UHF_PORT_SUSPEND:
3402 x = URWMASK(UREAD2(sc, port));
3403 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3405 case UHF_PORT_RESET:
3406 err = uhci_portreset(sc, index);
3408 case UHF_PORT_POWER:
3409 /* Pretend we turned on power */
3410 err = USBD_NORMAL_COMPLETION;
3412 case UHF_C_PORT_CONNECTION:
3413 case UHF_C_PORT_ENABLE:
3414 case UHF_C_PORT_OVER_CURRENT:
3415 case UHF_PORT_CONNECTION:
3416 case UHF_PORT_OVER_CURRENT:
3417 case UHF_PORT_LOW_SPEED:
3418 case UHF_C_PORT_SUSPEND:
3419 case UHF_C_PORT_RESET:
3429 xfer->actlen = totlen;
3430 err = USBD_NORMAL_COMPLETION;
3434 usb_transfer_complete(xfer);
3436 return (USBD_IN_PROGRESS);
3439 /* Abort a root control request. */
3441 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3443 /* Nothing to do, all transfers are synchronous. */
3446 /* Close the root pipe. */
3448 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3450 DPRINTF(("uhci_root_ctrl_close\n"));
3453 /* Abort a root interrupt request. */
3455 uhci_root_intr_abort(usbd_xfer_handle xfer)
3457 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3459 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3460 sc->sc_intr_xfer = NULL;
3462 if (xfer->pipe->intrxfer == xfer) {
3463 DPRINTF(("uhci_root_intr_abort: remove\n"));
3464 xfer->pipe->intrxfer = 0;
3466 xfer->status = USBD_CANCELLED;
3468 UXFER(xfer)->iinfo.isdone = 1;
3470 usb_transfer_complete(xfer);
3474 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3478 /* Insert last in queue. */
3479 err = usb_insert_transfer(xfer);
3484 * Pipe isn't running (otherwise err would be USBD_INPROG),
3485 * so start it first.
3487 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3490 /* Start a transfer on the root interrupt pipe */
3492 uhci_root_intr_start(usbd_xfer_handle xfer)
3494 usbd_pipe_handle pipe = xfer->pipe;
3495 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3497 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3498 xfer, xfer->length, xfer->flags));
3501 return (USBD_IOERROR);
3503 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3504 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3505 sc->sc_intr_xfer = xfer;
3506 return (USBD_IN_PROGRESS);
3509 /* Close the root interrupt pipe. */
3511 uhci_root_intr_close(usbd_pipe_handle pipe)
3513 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3515 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3516 sc->sc_intr_xfer = NULL;
3517 DPRINTF(("uhci_root_intr_close\n"));