1 /* $NetBSD: uhci.c,v 1.170 2003/02/19 01:35:04 augustss Exp $ */
3 /* Also already incorporated from NetBSD:
4 * $NetBSD: uhci.c,v 1.172 2003/02/23 04:19:26 simonb Exp $
5 * $NetBSD: uhci.c,v 1.173 2003/05/13 04:41:59 gson Exp $
6 * $NetBSD: uhci.c,v 1.175 2003/09/12 16:18:08 mycroft Exp $
7 * $NetBSD: uhci.c,v 1.176 2003/11/04 19:11:21 mycroft Exp $
8 * $NetBSD: uhci.c,v 1.177 2003/12/29 08:17:10 toshii Exp $
9 * $NetBSD: uhci.c,v 1.178 2004/03/02 16:32:05 martin Exp $
10 * $NetBSD: uhci.c,v 1.180 2004/07/17 20:12:03 mycroft Exp $
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
18 * Copyright (c) 1998 The NetBSD Foundation, Inc.
19 * All rights reserved.
21 * This code is derived from software contributed to The NetBSD Foundation
22 * by Lennart Augustsson (lennart@augustsson.net) at
23 * Carlstedt Research & Technology.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
35 * This product includes software developed by the NetBSD
36 * Foundation, Inc. and its contributors.
37 * 4. Neither the name of The NetBSD Foundation nor the names of its
38 * contributors may be used to endorse or promote products derived
39 * from this software without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
42 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
43 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
45 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 * POSSIBILITY OF SUCH DAMAGE.
55 * USB Universal Host Controller driver.
56 * Handles e.g. PIIX3 and PIIX4.
58 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
59 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
60 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
61 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
68 #if defined(__NetBSD__) || defined(__OpenBSD__)
69 #include <sys/device.h>
70 #include <sys/select.h>
71 #elif defined(__FreeBSD__)
72 #include <sys/endian.h>
73 #include <sys/module.h>
75 #if defined(DIAGNOSTIC) && defined(__i386__)
76 #include <machine/cpu.h>
80 #include <sys/queue.h>
81 #include <sys/sysctl.h>
83 #include <machine/bus.h>
84 #include <machine/endian.h>
86 #include <dev/usb/usb.h>
87 #include <dev/usb/usbdi.h>
88 #include <dev/usb/usbdivar.h>
89 #include <dev/usb/usb_mem.h>
90 #include <dev/usb/usb_quirks.h>
92 #include <dev/usb/uhcireg.h>
93 #include <dev/usb/uhcivar.h>
95 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
96 /*#define UHCI_CTL_LOOP */
98 #if defined(__FreeBSD__)
99 #include <machine/clock.h>
101 #define delay(d) DELAY(d)
104 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
106 #if defined(__OpenBSD__)
107 struct cfdriver uhci_cd = {
108 NULL, "uhci", DV_DULL
114 #define DPRINTF(x) if (uhcidebug) printf x
115 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
118 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
119 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
120 &uhcidebug, 0, "uhci debug level");
121 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
122 &uhcinoloop, 0, "uhci noloop");
124 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
128 #define DPRINTFN(n,x)
132 * The UHCI controller is little endian, so on big endian machines
133 * the data strored in memory needs to be swapped.
135 #if defined(__OpenBSD__)
136 #if BYTE_ORDER == BIG_ENDIAN
137 #define htole32(x) (bswap32(x))
138 #define le32toh(x) (bswap32(x))
140 #define htole32(x) (x)
141 #define le32toh(x) (x)
146 struct usbd_pipe pipe;
150 usbd_xfer_handle abortstart, abortend;
152 /* Info needed for different pipe kinds. */
158 uhci_soft_td_t *setup, *stat;
165 uhci_soft_qh_t **qhs;
175 uhci_soft_td_t **stds;
181 Static void uhci_globalreset(uhci_softc_t *);
182 Static usbd_status uhci_portreset(uhci_softc_t*, int);
183 Static void uhci_reset(uhci_softc_t *);
184 #if defined(__NetBSD__) || defined(__OpenBSD__)
185 Static void uhci_shutdown(void *v);
186 Static void uhci_power(int, void *);
188 Static usbd_status uhci_run(uhci_softc_t *, int run);
189 Static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
190 Static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
191 Static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
192 Static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
194 Static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
196 Static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
199 Static void uhci_free_std_chain(uhci_softc_t *,
200 uhci_soft_td_t *, uhci_soft_td_t *);
201 Static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
202 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
203 uhci_soft_td_t **, uhci_soft_td_t **);
204 Static void uhci_poll_hub(void *);
205 Static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
206 Static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
207 Static void uhci_idone(uhci_intr_info_t *);
209 Static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
211 Static void uhci_timeout(void *);
212 Static void uhci_timeout_task(void *);
213 Static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
214 Static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
215 Static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
216 Static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
217 Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
218 Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
219 Static int uhci_str(usb_string_descriptor_t *, int, char *);
220 Static void uhci_add_loop(uhci_softc_t *sc);
221 Static void uhci_rem_loop(uhci_softc_t *sc);
223 Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
224 Static void uhci_device_isoc_enter(usbd_xfer_handle);
226 Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
227 Static void uhci_freem(struct usbd_bus *, usb_dma_t *);
229 Static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
230 Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
232 Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
233 Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
234 Static void uhci_device_ctrl_abort(usbd_xfer_handle);
235 Static void uhci_device_ctrl_close(usbd_pipe_handle);
236 Static void uhci_device_ctrl_done(usbd_xfer_handle);
238 Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
239 Static usbd_status uhci_device_intr_start(usbd_xfer_handle);
240 Static void uhci_device_intr_abort(usbd_xfer_handle);
241 Static void uhci_device_intr_close(usbd_pipe_handle);
242 Static void uhci_device_intr_done(usbd_xfer_handle);
244 Static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
245 Static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
246 Static void uhci_device_bulk_abort(usbd_xfer_handle);
247 Static void uhci_device_bulk_close(usbd_pipe_handle);
248 Static void uhci_device_bulk_done(usbd_xfer_handle);
250 Static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
251 Static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
252 Static void uhci_device_isoc_abort(usbd_xfer_handle);
253 Static void uhci_device_isoc_close(usbd_pipe_handle);
254 Static void uhci_device_isoc_done(usbd_xfer_handle);
256 Static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
257 Static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
258 Static void uhci_root_ctrl_abort(usbd_xfer_handle);
259 Static void uhci_root_ctrl_close(usbd_pipe_handle);
260 Static void uhci_root_ctrl_done(usbd_xfer_handle);
262 Static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
263 Static usbd_status uhci_root_intr_start(usbd_xfer_handle);
264 Static void uhci_root_intr_abort(usbd_xfer_handle);
265 Static void uhci_root_intr_close(usbd_pipe_handle);
266 Static void uhci_root_intr_done(usbd_xfer_handle);
268 Static usbd_status uhci_open(usbd_pipe_handle);
269 Static void uhci_poll(struct usbd_bus *);
270 Static void uhci_softintr(void *);
272 Static usbd_status uhci_device_request(usbd_xfer_handle xfer);
274 Static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
275 Static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
276 Static usbd_status uhci_device_setintr(uhci_softc_t *sc,
277 struct uhci_pipe *pipe, int ival);
279 Static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
280 Static void uhci_noop(usbd_pipe_handle pipe);
282 Static __inline uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
286 Static void uhci_dump_all(uhci_softc_t *);
287 Static void uhci_dumpregs(uhci_softc_t *);
288 Static void uhci_dump_qhs(uhci_soft_qh_t *);
289 Static void uhci_dump_qh(uhci_soft_qh_t *);
290 Static void uhci_dump_tds(uhci_soft_td_t *);
291 Static void uhci_dump_td(uhci_soft_td_t *);
292 Static void uhci_dump_ii(uhci_intr_info_t *ii);
293 void uhci_dump(void);
296 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
297 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
298 #define UWRITE1(sc, r, x) \
299 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
300 } while (/*CONSTCOND*/0)
301 #define UWRITE2(sc, r, x) \
302 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
303 } while (/*CONSTCOND*/0)
304 #define UWRITE4(sc, r, x) \
305 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
306 } while (/*CONSTCOND*/0)
307 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
308 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
309 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
311 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
312 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
314 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
316 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
318 #define UHCI_INTR_ENDPT 1
320 struct usbd_bus_methods uhci_bus_methods = {
330 struct usbd_pipe_methods uhci_root_ctrl_methods = {
331 uhci_root_ctrl_transfer,
332 uhci_root_ctrl_start,
333 uhci_root_ctrl_abort,
334 uhci_root_ctrl_close,
339 struct usbd_pipe_methods uhci_root_intr_methods = {
340 uhci_root_intr_transfer,
341 uhci_root_intr_start,
342 uhci_root_intr_abort,
343 uhci_root_intr_close,
348 struct usbd_pipe_methods uhci_device_ctrl_methods = {
349 uhci_device_ctrl_transfer,
350 uhci_device_ctrl_start,
351 uhci_device_ctrl_abort,
352 uhci_device_ctrl_close,
354 uhci_device_ctrl_done,
357 struct usbd_pipe_methods uhci_device_intr_methods = {
358 uhci_device_intr_transfer,
359 uhci_device_intr_start,
360 uhci_device_intr_abort,
361 uhci_device_intr_close,
362 uhci_device_clear_toggle,
363 uhci_device_intr_done,
366 struct usbd_pipe_methods uhci_device_bulk_methods = {
367 uhci_device_bulk_transfer,
368 uhci_device_bulk_start,
369 uhci_device_bulk_abort,
370 uhci_device_bulk_close,
371 uhci_device_clear_toggle,
372 uhci_device_bulk_done,
375 struct usbd_pipe_methods uhci_device_isoc_methods = {
376 uhci_device_isoc_transfer,
377 uhci_device_isoc_start,
378 uhci_device_isoc_abort,
379 uhci_device_isoc_close,
381 uhci_device_isoc_done,
384 #define uhci_add_intr_info(sc, ii) \
385 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
386 #define uhci_del_intr_info(ii) \
388 LIST_REMOVE((ii), list); \
389 (ii)->list.le_prev = NULL; \
391 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
393 Static __inline uhci_soft_qh_t *
394 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
396 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
398 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
399 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
400 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
401 printf("uhci_find_prev_qh: QH not found\n");
410 uhci_globalreset(uhci_softc_t *sc)
412 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
413 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
414 UHCICMD(sc, 0); /* do nothing */
418 uhci_init(uhci_softc_t *sc)
422 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
425 DPRINTFN(1,("uhci_init: start\n"));
434 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
435 uhci_globalreset(sc); /* reset the controller */
438 /* Allocate and initialize real frame array. */
439 err = usb_allocmem(&sc->sc_bus,
440 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
441 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
444 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
445 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
446 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
449 * Allocate a TD, inactive, that hangs from the last QH.
450 * This is to avoid a bug in the PIIX that makes it run berserk
453 std = uhci_alloc_std(sc);
456 std->link.std = NULL;
457 std->td.td_link = htole32(UHCI_PTR_T);
458 std->td.td_status = htole32(0); /* inactive */
459 std->td.td_token = htole32(0);
460 std->td.td_buffer = htole32(0);
462 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
463 lsqh = uhci_alloc_sqh(sc);
467 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
469 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
470 sc->sc_last_qh = lsqh;
472 /* Allocate the dummy QH where bulk traffic will be queued. */
473 bsqh = uhci_alloc_sqh(sc);
477 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
479 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
480 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
482 /* Allocate dummy QH where high speed control traffic will be queued. */
483 chsqh = uhci_alloc_sqh(sc);
487 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
489 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
490 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
492 /* Allocate dummy QH where control traffic will be queued. */
493 clsqh = uhci_alloc_sqh(sc);
497 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
499 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
500 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
503 * Make all (virtual) frame list pointers point to the interrupt
504 * queue heads and the interrupt queue heads at the control
505 * queue head and point the physical frame list to the virtual.
507 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
508 std = uhci_alloc_std(sc);
509 sqh = uhci_alloc_sqh(sc);
510 if (std == NULL || sqh == NULL)
513 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
514 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
515 std->td.td_token = htole32(0);
516 std->td.td_buffer = htole32(0);
518 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
520 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
521 sc->sc_vframes[i].htd = std;
522 sc->sc_vframes[i].etd = std;
523 sc->sc_vframes[i].hqh = sqh;
524 sc->sc_vframes[i].eqh = sqh;
526 j < UHCI_FRAMELIST_COUNT;
527 j += UHCI_VFRAMELIST_COUNT)
528 sc->sc_pframes[j] = htole32(std->physaddr);
531 LIST_INIT(&sc->sc_intrhead);
533 SIMPLEQ_INIT(&sc->sc_free_xfers);
535 usb_callout_init(sc->sc_poll_handle);
537 /* Set up the bus struct. */
538 sc->sc_bus.methods = &uhci_bus_methods;
539 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
541 #if defined(__NetBSD__) || defined(__OpenBSD__)
542 sc->sc_suspend = PWR_RESUME;
543 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
544 sc->sc_shutdownhook = shutdownhook_establish(uhci_shutdown, sc);
547 DPRINTFN(1,("uhci_init: enabling\n"));
548 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
549 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
551 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
553 return (uhci_run(sc, 1)); /* and here we go... */
556 #if defined(__NetBSD__) || defined(__OpenBSD__)
558 uhci_activate(device_ptr_t self, enum devact act)
560 struct uhci_softc *sc = (struct uhci_softc *)self;
567 case DVACT_DEACTIVATE:
568 if (sc->sc_child != NULL)
569 rv = config_deactivate(sc->sc_child);
577 uhci_detach(struct uhci_softc *sc, int flags)
579 usbd_xfer_handle xfer;
582 #if defined(__NetBSD__) || defined(__OpenBSD__)
583 if (sc->sc_child != NULL)
584 rv = config_detach(sc->sc_child, flags);
592 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
595 #if defined(__NetBSD__) || defined(__OpenBSD__)
596 powerhook_disestablish(sc->sc_powerhook);
597 shutdownhook_disestablish(sc->sc_shutdownhook);
600 /* Free all xfers associated with this HC. */
602 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
605 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
609 /* XXX free other data structures XXX */
610 usb_freemem(&sc->sc_bus, &sc->sc_dma);
616 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
618 return (usb_allocmem(bus, size, 0, dma));
622 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
624 usb_freemem(bus, dma);
628 uhci_allocx(struct usbd_bus *bus)
630 struct uhci_softc *sc = (struct uhci_softc *)bus;
631 usbd_xfer_handle xfer;
633 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
635 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
637 if (xfer->busy_free != XFER_FREE) {
638 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
643 xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_NOWAIT);
646 memset(xfer, 0, sizeof (struct uhci_xfer));
647 UXFER(xfer)->iinfo.sc = sc;
648 usb_init_task(&UXFER(xfer)->abort_task, uhci_timeout_task,
650 UXFER(xfer)->uhci_xfer_flags = 0;
652 UXFER(xfer)->iinfo.isdone = 1;
653 xfer->busy_free = XFER_BUSY;
660 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
662 struct uhci_softc *sc = (struct uhci_softc *)bus;
665 if (xfer->busy_free != XFER_BUSY) {
666 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
670 xfer->busy_free = XFER_FREE;
671 if (!UXFER(xfer)->iinfo.isdone) {
672 printf("uhci_freex: !isdone\n");
676 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
680 * Shut down the controller when the system is going down.
683 uhci_shutdown(void *v)
685 uhci_softc_t *sc = v;
687 DPRINTF(("uhci_shutdown: stopping the HC\n"));
688 uhci_run(sc, 0); /* stop the controller */
692 * Handle suspend/resume.
694 * We need to switch to polling mode here, because this routine is
695 * called from an interrupt context. This is all right since we
696 * are almost suspended anyway.
699 uhci_power(int why, void *v)
701 uhci_softc_t *sc = v;
706 cmd = UREAD2(sc, UHCI_CMD);
708 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
709 sc, why, sc->sc_suspend, cmd));
711 if (why != PWR_RESUME) {
716 if (sc->sc_intr_xfer != NULL)
717 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub,
719 sc->sc_bus.use_polling++;
720 uhci_run(sc, 0); /* stop the controller */
722 /* save some state if BIOS doesn't */
723 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
724 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
726 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
728 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
729 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
730 sc->sc_suspend = why;
731 sc->sc_bus.use_polling--;
732 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
735 if (sc->sc_suspend == PWR_RESUME)
736 printf("uhci_power: weird, resume without suspend.\n");
738 sc->sc_bus.use_polling++;
739 sc->sc_suspend = why;
740 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
741 uhci_globalreset(sc); /* reset the controller */
743 if (cmd & UHCI_CMD_RS)
744 uhci_run(sc, 0); /* in case BIOS has started it */
746 uhci_globalreset(sc);
749 /* restore saved state */
750 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
751 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
752 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
754 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
755 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
756 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
757 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
758 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
759 UHCICMD(sc, UHCI_CMD_MAXP);
760 uhci_run(sc, 1); /* and start traffic again */
761 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
762 sc->sc_bus.use_polling--;
763 if (sc->sc_intr_xfer != NULL)
764 usb_callout(sc->sc_poll_handle, sc->sc_ival,
765 uhci_poll_hub, sc->sc_intr_xfer);
776 uhci_dumpregs(uhci_softc_t *sc)
778 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
779 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
780 USBDEVNAME(sc->sc_bus.bdev),
781 UREAD2(sc, UHCI_CMD),
782 UREAD2(sc, UHCI_STS),
783 UREAD2(sc, UHCI_INTR),
784 UREAD2(sc, UHCI_FRNUM),
785 UREAD4(sc, UHCI_FLBASEADDR),
786 UREAD1(sc, UHCI_SOF),
787 UREAD2(sc, UHCI_PORTSC1),
788 UREAD2(sc, UHCI_PORTSC2)));
792 uhci_dump_td(uhci_soft_td_t *p)
794 char sbuf[128], sbuf2[128];
796 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
797 "token=0x%08lx buffer=0x%08lx\n",
798 p, (long)p->physaddr,
799 (long)le32toh(p->td.td_link),
800 (long)le32toh(p->td.td_status),
801 (long)le32toh(p->td.td_token),
802 (long)le32toh(p->td.td_buffer)));
804 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
806 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
807 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
808 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
809 sbuf2, sizeof(sbuf2));
811 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
812 "D=%d,maxlen=%d\n", sbuf, sbuf2,
813 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
814 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
815 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
816 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
817 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
818 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
819 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
823 uhci_dump_qh(uhci_soft_qh_t *sqh)
825 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
826 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
827 le32toh(sqh->qh.qh_elink)));
835 uhci_dump_all(thesc);
840 uhci_dump_all(uhci_softc_t *sc)
843 printf("intrs=%d\n", sc->sc_bus.no_intrs);
844 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
845 uhci_dump_qh(sc->sc_lctl_start);
850 uhci_dump_qhs(uhci_soft_qh_t *sqh)
854 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
855 * Traverses sideways first, then down.
865 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
869 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
870 uhci_dump_qhs(sqh->hlink);
872 DPRINTF(("No QH\n"));
874 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
875 uhci_dump_tds(sqh->elink);
877 DPRINTF(("No TD\n"));
881 uhci_dump_tds(uhci_soft_td_t *std)
885 for(td = std; td != NULL; td = td->link.std) {
888 /* Check whether the link pointer in this TD marks
889 * the link pointer as end of queue. This avoids
890 * printing the free list in case the queue/TD has
891 * already been moved there (seatbelt).
893 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
894 le32toh(td->td.td_link) == 0)
900 uhci_dump_ii(uhci_intr_info_t *ii)
902 usbd_pipe_handle pipe;
903 usb_endpoint_descriptor_t *ed;
904 usbd_device_handle dev;
907 #define DONE ii->isdone
915 if (ii->xfer == NULL) {
916 printf("ii %p: done=%d xfer=NULL\n",
920 pipe = ii->xfer->pipe;
922 printf("ii %p: done=%d xfer=%p pipe=NULL\n",
926 if (pipe->endpoint == NULL) {
927 printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
928 ii, DONE, ii->xfer, pipe);
931 if (pipe->device == NULL) {
932 printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
933 ii, DONE, ii->xfer, pipe);
936 ed = pipe->endpoint->edesc;
938 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",
939 ii, DONE, ii->xfer, dev,
940 UGETW(dev->ddesc.idVendor),
941 UGETW(dev->ddesc.idProduct),
943 ed->bEndpointAddress, ed->bmAttributes);
947 void uhci_dump_iis(struct uhci_softc *sc);
949 uhci_dump_iis(struct uhci_softc *sc)
951 uhci_intr_info_t *ii;
953 printf("intr_info list:\n");
954 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
959 void iidump(void) { uhci_dump_iis(thesc); }
964 * This routine is executed periodically and simulates interrupts
965 * from the root controller interrupt pipe for port status change.
968 uhci_poll_hub(void *addr)
970 usbd_xfer_handle xfer = addr;
971 usbd_pipe_handle pipe = xfer->pipe;
972 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
976 DPRINTFN(20, ("uhci_poll_hub\n"));
978 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
980 p = KERNADDR(&xfer->dmabuf, 0);
982 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
984 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
987 /* No change, try again in a while */
991 xfer->status = USBD_NORMAL_COMPLETION;
993 xfer->device->bus->intr_context++;
994 usb_transfer_complete(xfer);
995 xfer->device->bus->intr_context--;
1000 uhci_root_intr_done(usbd_xfer_handle xfer)
1005 uhci_root_ctrl_done(usbd_xfer_handle xfer)
1010 * Let the last QH loop back to the high speed control transfer QH.
1011 * This is what intel calls "bandwidth reclamation" and improves
1012 * USB performance a lot for some devices.
1013 * If we are already looping, just count it.
1016 uhci_add_loop(uhci_softc_t *sc) {
1021 if (++sc->sc_loops == 1) {
1022 DPRINTFN(5,("uhci_start_loop: add\n"));
1023 /* Note, we don't loop back the soft pointer. */
1024 sc->sc_last_qh->qh.qh_hlink =
1025 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
1030 uhci_rem_loop(uhci_softc_t *sc) {
1035 if (--sc->sc_loops == 0) {
1036 DPRINTFN(5,("uhci_end_loop: remove\n"));
1037 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1041 /* Add high speed control QH, called at splusb(). */
1043 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1045 uhci_soft_qh_t *eqh;
1049 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
1050 eqh = sc->sc_hctl_end;
1051 sqh->hlink = eqh->hlink;
1052 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1054 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1055 sc->sc_hctl_end = sqh;
1056 #ifdef UHCI_CTL_LOOP
1061 /* Remove high speed control QH, called at splusb(). */
1063 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1065 uhci_soft_qh_t *pqh;
1069 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1070 #ifdef UHCI_CTL_LOOP
1074 * The T bit should be set in the elink of the QH so that the HC
1075 * doesn't follow the pointer. This condition may fail if the
1076 * the transferred packet was short so that the QH still points
1077 * at the last used TD.
1078 * In this case we set the T bit and wait a little for the HC
1079 * to stop looking at the TD.
1081 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1082 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1083 delay(UHCI_QH_REMOVE_DELAY);
1086 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1087 pqh->hlink = sqh->hlink;
1088 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1089 delay(UHCI_QH_REMOVE_DELAY);
1090 if (sc->sc_hctl_end == sqh)
1091 sc->sc_hctl_end = pqh;
1094 /* Add low speed control QH, called at splusb(). */
1096 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1098 uhci_soft_qh_t *eqh;
1102 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1103 eqh = sc->sc_lctl_end;
1104 sqh->hlink = eqh->hlink;
1105 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1107 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1108 sc->sc_lctl_end = sqh;
1111 /* Remove low speed control QH, called at splusb(). */
1113 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1115 uhci_soft_qh_t *pqh;
1119 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1120 /* See comment in uhci_remove_hs_ctrl() */
1121 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1122 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1123 delay(UHCI_QH_REMOVE_DELAY);
1125 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1126 pqh->hlink = sqh->hlink;
1127 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1128 delay(UHCI_QH_REMOVE_DELAY);
1129 if (sc->sc_lctl_end == sqh)
1130 sc->sc_lctl_end = pqh;
1133 /* Add bulk QH, called at splusb(). */
1135 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1137 uhci_soft_qh_t *eqh;
1141 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1142 eqh = sc->sc_bulk_end;
1143 sqh->hlink = eqh->hlink;
1144 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1146 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1147 sc->sc_bulk_end = sqh;
1151 /* Remove bulk QH, called at splusb(). */
1153 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1155 uhci_soft_qh_t *pqh;
1159 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1161 /* See comment in uhci_remove_hs_ctrl() */
1162 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1163 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1164 delay(UHCI_QH_REMOVE_DELAY);
1166 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1167 pqh->hlink = sqh->hlink;
1168 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1169 delay(UHCI_QH_REMOVE_DELAY);
1170 if (sc->sc_bulk_end == sqh)
1171 sc->sc_bulk_end = pqh;
1174 Static int uhci_intr1(uhci_softc_t *);
1177 uhci_intr(void *arg)
1179 uhci_softc_t *sc = arg;
1184 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1185 if (sc->sc_bus.use_polling) {
1187 printf("uhci_intr: ignored interrupt while polling\n");
1191 return (uhci_intr1(sc));
1195 uhci_intr1(uhci_softc_t *sc)
1202 * It can happen that an interrupt will be delivered to
1203 * us before the device has been fully attached and the
1204 * softc struct has been configured. Usually this happens
1205 * when kldloading the USB support as a module after the
1206 * system has been booted. If we detect this condition,
1207 * we need to squelch the unwanted interrupts until we're
1210 if (sc->sc_bus.bdev == NULL) {
1211 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1212 uhci_run(sc, 0); /* stop the controller */
1213 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1218 if (uhcidebug > 15) {
1219 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev)));
1223 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1224 if (status == 0) /* The interrupt was not for us. */
1227 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1228 if (sc->sc_suspend != PWR_RESUME)
1229 printf("uhci_intr: suspended sts=0x%x\n", status);
1232 if (sc->sc_suspend != PWR_RESUME) {
1233 printf("%s: interrupt while not operating ignored\n",
1234 USBDEVNAME(sc->sc_bus.bdev));
1235 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1240 if (status & UHCI_STS_USBINT)
1241 ack |= UHCI_STS_USBINT;
1242 if (status & UHCI_STS_USBEI)
1243 ack |= UHCI_STS_USBEI;
1244 if (status & UHCI_STS_RD) {
1247 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1250 if (status & UHCI_STS_HSE) {
1251 ack |= UHCI_STS_HSE;
1252 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
1254 if (status & UHCI_STS_HCPE) {
1255 ack |= UHCI_STS_HCPE;
1256 printf("%s: host controller process error\n",
1257 USBDEVNAME(sc->sc_bus.bdev));
1259 if (status & UHCI_STS_HCH) {
1260 /* no acknowledge needed */
1261 if (!sc->sc_dying) {
1262 printf("%s: host controller halted\n",
1263 USBDEVNAME(sc->sc_bus.bdev));
1272 return (0); /* nothing to acknowledge */
1273 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1275 sc->sc_bus.no_intrs++;
1276 usb_schedsoftintr(&sc->sc_bus);
1278 DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
1284 uhci_softintr(void *v)
1286 uhci_softc_t *sc = v;
1287 uhci_intr_info_t *ii, *nextii;
1289 DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
1290 sc->sc_bus.intr_context));
1292 sc->sc_bus.intr_context++;
1295 * Interrupts on UHCI really suck. When the host controller
1296 * interrupts because a transfer is completed there is no
1297 * way of knowing which transfer it was. You can scan down
1298 * the TDs and QHs of the previous frame to limit the search,
1299 * but that assumes that the interrupt was not delayed by more
1300 * than 1 ms, which may not always be true (e.g. after debug
1301 * output on a slow console).
1302 * We scan all interrupt descriptors to see if any have
1305 LIST_FOREACH_SAFE(ii, &sc->sc_intrhead, list, nextii)
1306 uhci_check_intr(sc, ii);
1308 #ifdef USB_USE_SOFTINTR
1309 if (sc->sc_softwake) {
1310 sc->sc_softwake = 0;
1311 wakeup(&sc->sc_softwake);
1313 #endif /* USB_USE_SOFTINTR */
1315 sc->sc_bus.intr_context--;
1318 /* Check for an interrupt. */
1320 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1322 uhci_soft_td_t *std, *lstd;
1325 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1328 printf("uhci_check_intr: no ii? %p\n", ii);
1332 if (ii->xfer->status == USBD_CANCELLED ||
1333 ii->xfer->status == USBD_TIMEOUT) {
1334 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1338 if (ii->stdstart == NULL)
1343 printf("uhci_check_intr: std==0\n");
1348 * If the last TD is still active we need to check whether there
1349 * is an error somewhere in the middle, or whether there was a
1350 * short packet (SPD and not ACTIVE).
1352 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1353 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1354 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1355 status = le32toh(std->td.td_status);
1356 /* If there's an active TD the xfer isn't done. */
1357 if (status & UHCI_TD_ACTIVE)
1359 /* Any kind of error makes the xfer done. */
1360 if (status & UHCI_TD_STALLED)
1362 /* We want short packets, and it is short: it's done */
1363 if ((status & UHCI_TD_SPD) &&
1364 UHCI_TD_GET_ACTLEN(status) <
1365 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)))
1368 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1373 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1374 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1375 usb_rem_task(ii->xfer->pipe->device, &UXFER(ii->xfer)->abort_task);
1379 /* Called at splusb() */
1381 uhci_idone(uhci_intr_info_t *ii)
1383 usbd_xfer_handle xfer = ii->xfer;
1384 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1385 uhci_soft_td_t *std;
1386 u_int32_t status = 0, nstatus;
1389 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1396 printf("uhci_idone: ii is done!\n ");
1399 printf("uhci_idone: ii=%p is done!\n", ii);
1408 if (xfer->nframes != 0) {
1409 /* Isoc transfer, do things differently. */
1410 uhci_soft_td_t **stds = upipe->u.iso.stds;
1411 int i, n, nframes, len;
1413 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1415 nframes = xfer->nframes;
1417 n = UXFER(xfer)->curframe;
1418 for (i = 0; i < nframes; i++) {
1421 if (uhcidebug > 5) {
1422 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1426 if (++n >= UHCI_VFRAMELIST_COUNT)
1428 status = le32toh(std->td.td_status);
1429 len = UHCI_TD_GET_ACTLEN(status);
1430 xfer->frlengths[i] = len;
1433 upipe->u.iso.inuse -= nframes;
1434 xfer->actlen = actlen;
1435 xfer->status = USBD_NORMAL_COMPLETION;
1440 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1443 uhci_dump_tds(ii->stdstart);
1446 /* The transfer is done, compute actual length and status. */
1448 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1449 nstatus = le32toh(std->td.td_status);
1450 if (nstatus & UHCI_TD_ACTIVE)
1454 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1456 actlen += UHCI_TD_GET_ACTLEN(status);
1459 * UHCI will report CRCTO in addition to a STALL or NAK
1460 * for a SETUP transaction. See section 3.2.2, "TD
1461 * CONTROL AND STATUS".
1463 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1464 status &= ~UHCI_TD_CRCTO;
1467 /* If there are left over TDs we need to update the toggle. */
1469 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1471 status &= UHCI_TD_ERROR;
1472 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1474 xfer->actlen = actlen;
1479 bitmask_snprintf((u_int32_t)status,
1480 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1481 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1482 sbuf, sizeof(sbuf));
1484 DPRINTFN((status == UHCI_TD_STALLED)*10,
1485 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1487 xfer->pipe->device->address,
1488 xfer->pipe->endpoint->edesc->bEndpointAddress,
1492 if (status == UHCI_TD_STALLED)
1493 xfer->status = USBD_STALLED;
1495 xfer->status = USBD_IOERROR; /* more info XXX */
1497 xfer->status = USBD_NORMAL_COMPLETION;
1501 usb_transfer_complete(xfer);
1502 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1506 * Called when a request does not complete.
1509 uhci_timeout(void *addr)
1511 uhci_intr_info_t *ii = addr;
1512 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1513 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1514 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1516 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1519 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1523 /* Execute the abort in a process context. */
1524 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1528 uhci_timeout_task(void *addr)
1530 usbd_xfer_handle xfer = addr;
1533 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1536 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1541 * Wait here until controller claims to have an interrupt.
1542 * Then call uhci_intr and return. Use timeout to avoid waiting
1544 * Only used during boot when interrupts are not enabled yet.
1547 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1549 int timo = xfer->timeout;
1550 uhci_intr_info_t *ii;
1552 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1554 xfer->status = USBD_IN_PROGRESS;
1555 for (; timo >= 0; timo--) {
1556 usb_delay_ms(&sc->sc_bus, 1);
1557 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1558 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1560 if (xfer->status != USBD_IN_PROGRESS)
1565 DPRINTF(("uhci_waitintr: timeout\n"));
1566 for (ii = LIST_FIRST(&sc->sc_intrhead);
1567 ii != NULL && ii->xfer != xfer;
1568 ii = LIST_NEXT(ii, list))
1572 panic("uhci_waitintr: lost intr_info");
1578 uhci_poll(struct usbd_bus *bus)
1580 uhci_softc_t *sc = (uhci_softc_t *)bus;
1582 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1587 uhci_reset(uhci_softc_t *sc)
1591 UHCICMD(sc, UHCI_CMD_HCRESET);
1592 /* The reset bit goes low when the controller is done. */
1593 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1594 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1595 usb_delay_ms(&sc->sc_bus, 1);
1596 if (n >= UHCI_RESET_TIMEOUT)
1597 printf("%s: controller did not reset\n",
1598 USBDEVNAME(sc->sc_bus.bdev));
1602 uhci_run(uhci_softc_t *sc, int run)
1609 DPRINTF(("uhci_run: setting run=%d\n", run));
1610 cmd = UREAD2(sc, UHCI_CMD);
1614 cmd &= ~UHCI_CMD_RS;
1616 for(n = 0; n < 10; n++) {
1617 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1618 /* return when we've entered the state we want */
1619 if (run == running) {
1621 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1622 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1623 return (USBD_NORMAL_COMPLETION);
1625 usb_delay_ms(&sc->sc_bus, 1);
1628 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1629 run ? "start" : "stop");
1630 return (USBD_IOERROR);
1634 * Memory management routines.
1635 * uhci_alloc_std allocates TDs
1636 * uhci_alloc_sqh allocates QHs
1637 * These two routines do their own free list management,
1638 * partly for speed, partly because allocating DMAable memory
1639 * has page size granularaity so much memory would be wasted if
1640 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1644 uhci_alloc_std(uhci_softc_t *sc)
1646 uhci_soft_td_t *std;
1651 if (sc->sc_freetds == NULL) {
1652 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1653 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1654 UHCI_TD_ALIGN, &dma);
1657 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1658 offs = i * UHCI_STD_SIZE;
1659 std = KERNADDR(&dma, offs);
1660 std->physaddr = DMAADDR(&dma, offs);
1661 std->link.std = sc->sc_freetds;
1662 sc->sc_freetds = std;
1665 std = sc->sc_freetds;
1666 sc->sc_freetds = std->link.std;
1667 memset(&std->td, 0, sizeof(uhci_td_t));
1672 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1675 #define TD_IS_FREE 0x12345678
1676 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1677 printf("uhci_free_std: freeing free TD %p\n", std);
1680 std->td.td_token = htole32(TD_IS_FREE);
1682 std->link.std = sc->sc_freetds;
1683 sc->sc_freetds = std;
1687 uhci_alloc_sqh(uhci_softc_t *sc)
1689 uhci_soft_qh_t *sqh;
1694 if (sc->sc_freeqhs == NULL) {
1695 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1696 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1697 UHCI_QH_ALIGN, &dma);
1700 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1701 offs = i * UHCI_SQH_SIZE;
1702 sqh = KERNADDR(&dma, offs);
1703 sqh->physaddr = DMAADDR(&dma, offs);
1704 sqh->hlink = sc->sc_freeqhs;
1705 sc->sc_freeqhs = sqh;
1708 sqh = sc->sc_freeqhs;
1709 sc->sc_freeqhs = sqh->hlink;
1710 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1715 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1717 sqh->hlink = sc->sc_freeqhs;
1718 sc->sc_freeqhs = sqh;
1722 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1723 uhci_soft_td_t *stdend)
1727 for (; std != stdend; std = p) {
1729 uhci_free_std(sc, std);
1734 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1735 int rd, u_int16_t flags, usb_dma_t *dma,
1736 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1738 uhci_soft_td_t *p, *lastp;
1739 uhci_physaddr_t lastlink;
1740 int i, ntd, l, tog, maxp;
1742 int addr = upipe->pipe.device->address;
1743 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1745 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1746 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1747 upipe->pipe.device->speed, flags));
1748 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1750 printf("uhci_alloc_std_chain: maxp=0\n");
1751 return (USBD_INVAL);
1753 ntd = (len + maxp - 1) / maxp;
1754 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1756 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1759 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1760 return (USBD_NORMAL_COMPLETION);
1762 tog = upipe->nexttoggle;
1765 upipe->nexttoggle = tog ^ 1;
1767 lastlink = UHCI_PTR_T;
1769 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1770 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1771 status |= UHCI_TD_LS;
1772 if (flags & USBD_SHORT_XFER_OK)
1773 status |= UHCI_TD_SPD;
1774 for (i = ntd; i >= 0; i--) {
1775 p = uhci_alloc_std(sc);
1777 uhci_free_std_chain(sc, lastp, NULL);
1778 return (USBD_NOMEM);
1780 p->link.std = lastp;
1781 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1783 lastlink = p->physaddr;
1784 p->td.td_status = htole32(status);
1788 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1794 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1795 UHCI_TD_OUT(l, endpt, addr, tog));
1796 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1800 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1801 upipe->nexttoggle));
1802 return (USBD_NORMAL_COMPLETION);
1806 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1808 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1809 upipe->nexttoggle = 0;
1813 uhci_noop(usbd_pipe_handle pipe)
1818 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1822 /* Insert last in queue. */
1823 err = usb_insert_transfer(xfer);
1828 * Pipe isn't running (otherwise err would be USBD_INPROG),
1829 * so start it first.
1831 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1835 uhci_device_bulk_start(usbd_xfer_handle xfer)
1837 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1838 usbd_device_handle dev = upipe->pipe.device;
1839 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1840 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1841 uhci_soft_td_t *data, *dataend;
1842 uhci_soft_qh_t *sqh;
1844 int len, isread, endpt;
1847 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1848 xfer, xfer->length, xfer->flags, ii));
1851 return (USBD_IOERROR);
1854 if (xfer->rqflags & URQ_REQUEST)
1855 panic("uhci_device_bulk_transfer: a request");
1859 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1860 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1861 sqh = upipe->u.bulk.sqh;
1863 upipe->u.bulk.isread = isread;
1864 upipe->u.bulk.length = len;
1866 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1867 &xfer->dmabuf, &data, &dataend);
1870 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1873 if (uhcidebug > 8) {
1874 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1875 uhci_dump_tds(data);
1879 /* Set up interrupt info. */
1881 ii->stdstart = data;
1882 ii->stdend = dataend;
1885 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1891 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1894 uhci_add_bulk(sc, sqh);
1895 uhci_add_intr_info(sc, ii);
1897 if (xfer->timeout && !sc->sc_bus.use_polling) {
1898 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1901 xfer->status = USBD_IN_PROGRESS;
1905 if (uhcidebug > 10) {
1906 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1907 uhci_dump_tds(data);
1911 if (sc->sc_bus.use_polling)
1912 uhci_waitintr(sc, xfer);
1914 return (USBD_IN_PROGRESS);
1917 /* Abort a device bulk request. */
1919 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1921 DPRINTF(("uhci_device_bulk_abort:\n"));
1922 uhci_abort_xfer(xfer, USBD_CANCELLED);
1926 * Abort a device request.
1927 * If this routine is called at splusb() it guarantees that the request
1928 * will be removed from the hardware scheduling and that the callback
1929 * for it will be called with USBD_CANCELLED status.
1930 * It's impossible to guarantee that the requested transfer will not
1931 * have happened since the hardware runs concurrently.
1932 * If the transaction has already happened we rely on the ordinary
1933 * interrupt processing to process it.
1936 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1938 struct uhci_xfer *uxfer = UXFER(xfer);
1939 uhci_intr_info_t *ii = &uxfer->iinfo;
1940 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1941 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1942 uhci_soft_td_t *std;
1945 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1948 /* If we're dying, just do the software part. */
1950 xfer->status = status; /* make software ignore it */
1951 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1952 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1953 usb_transfer_complete(xfer);
1958 if (xfer->device->bus->intr_context || !curproc)
1959 panic("uhci_abort_xfer: not in process context");
1962 * If an abort is already in progress then just wait for it to
1963 * complete and return.
1965 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING) {
1966 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n"));
1967 /* No need to wait if we're aborting from a timeout. */
1968 if (status == USBD_TIMEOUT)
1970 /* Override the status which might be USBD_TIMEOUT. */
1971 xfer->status = status;
1972 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n"));
1973 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTWAIT;
1974 while (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING)
1975 tsleep(&uxfer->uhci_xfer_flags, PZERO, "uhciaw", 0);
1980 * Step 1: Make interrupt routine and hardware ignore xfer.
1983 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTING;
1984 xfer->status = status; /* make software ignore it */
1985 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1986 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1987 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1988 for (std = ii->stdstart; std != NULL; std = std->link.std)
1989 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1993 * Step 2: Wait until we know hardware has finished any possible
1994 * use of the xfer. Also make sure the soft interrupt routine
1997 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1999 #ifdef USB_USE_SOFTINTR
2000 sc->sc_softwake = 1;
2001 #endif /* USB_USE_SOFTINTR */
2002 usb_schedsoftintr(&sc->sc_bus);
2003 #ifdef USB_USE_SOFTINTR
2004 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
2005 tsleep(&sc->sc_softwake, PZERO, "uhciab", 0);
2006 #endif /* USB_USE_SOFTINTR */
2010 * Step 3: Execute callback.
2012 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
2017 /* Do the wakeup first to avoid touching the xfer after the callback. */
2018 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTING;
2019 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTWAIT) {
2020 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTWAIT;
2021 wakeup(&uxfer->uhci_xfer_flags);
2023 usb_transfer_complete(xfer);
2027 /* Close a device bulk pipe. */
2029 uhci_device_bulk_close(usbd_pipe_handle pipe)
2031 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2032 usbd_device_handle dev = upipe->pipe.device;
2033 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2035 uhci_free_sqh(sc, upipe->u.bulk.sqh);
2039 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
2043 /* Insert last in queue. */
2044 err = usb_insert_transfer(xfer);
2049 * Pipe isn't running (otherwise err would be USBD_INPROG),
2050 * so start it first.
2052 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2056 uhci_device_ctrl_start(usbd_xfer_handle xfer)
2058 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2062 return (USBD_IOERROR);
2065 if (!(xfer->rqflags & URQ_REQUEST))
2066 panic("uhci_device_ctrl_transfer: not a request");
2069 err = uhci_device_request(xfer);
2073 if (sc->sc_bus.use_polling)
2074 uhci_waitintr(sc, xfer);
2075 return (USBD_IN_PROGRESS);
2079 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2083 /* Insert last in queue. */
2084 err = usb_insert_transfer(xfer);
2089 * Pipe isn't running (otherwise err would be USBD_INPROG),
2090 * so start it first.
2092 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2096 uhci_device_intr_start(usbd_xfer_handle xfer)
2098 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2099 usbd_device_handle dev = upipe->pipe.device;
2100 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2101 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2102 uhci_soft_td_t *data, *dataend;
2103 uhci_soft_qh_t *sqh;
2109 return (USBD_IOERROR);
2111 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2112 xfer, xfer->length, xfer->flags));
2115 if (xfer->rqflags & URQ_REQUEST)
2116 panic("uhci_device_intr_transfer: a request");
2119 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2120 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2121 sqh = upipe->u.bulk.sqh;
2123 upipe->u.intr.isread = isread;
2125 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread,
2126 xfer->flags, &xfer->dmabuf, &data,
2130 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2133 if (uhcidebug > 10) {
2134 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2135 uhci_dump_tds(data);
2136 uhci_dump_qh(upipe->u.intr.qhs[0]);
2141 /* Set up interrupt info. */
2143 ii->stdstart = data;
2144 ii->stdend = dataend;
2147 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2152 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2153 upipe->u.intr.qhs[0]));
2154 for (i = 0; i < upipe->u.intr.npoll; i++) {
2155 sqh = upipe->u.intr.qhs[i];
2157 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2159 uhci_add_intr_info(sc, ii);
2160 xfer->status = USBD_IN_PROGRESS;
2164 if (uhcidebug > 10) {
2165 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2166 uhci_dump_tds(data);
2167 uhci_dump_qh(upipe->u.intr.qhs[0]);
2171 return (USBD_IN_PROGRESS);
2174 /* Abort a device control request. */
2176 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2178 DPRINTF(("uhci_device_ctrl_abort:\n"));
2179 uhci_abort_xfer(xfer, USBD_CANCELLED);
2182 /* Close a device control pipe. */
2184 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2188 /* Abort a device interrupt request. */
2190 uhci_device_intr_abort(usbd_xfer_handle xfer)
2192 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2193 if (xfer->pipe->intrxfer == xfer) {
2194 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2195 xfer->pipe->intrxfer = NULL;
2197 uhci_abort_xfer(xfer, USBD_CANCELLED);
2200 /* Close a device interrupt pipe. */
2202 uhci_device_intr_close(usbd_pipe_handle pipe)
2204 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2205 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2209 /* Unlink descriptors from controller data structures. */
2210 npoll = upipe->u.intr.npoll;
2212 for (i = 0; i < npoll; i++)
2213 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2217 * We now have to wait for any activity on the physical
2218 * descriptors to stop.
2220 usb_delay_ms(&sc->sc_bus, 2);
2222 for(i = 0; i < npoll; i++)
2223 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2224 free(upipe->u.intr.qhs, M_USBHC);
2226 /* XXX free other resources */
2230 uhci_device_request(usbd_xfer_handle xfer)
2232 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2233 usb_device_request_t *req = &xfer->request;
2234 usbd_device_handle dev = upipe->pipe.device;
2235 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2236 int addr = dev->address;
2237 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2238 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2239 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2240 uhci_soft_qh_t *sqh;
2247 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2248 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2249 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2250 UGETW(req->wIndex), UGETW(req->wLength),
2253 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2254 isread = req->bmRequestType & UT_READ;
2255 len = UGETW(req->wLength);
2257 setup = upipe->u.ctl.setup;
2258 stat = upipe->u.ctl.stat;
2259 sqh = upipe->u.ctl.sqh;
2261 /* Set up data transaction */
2263 upipe->nexttoggle = 1;
2264 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2265 &xfer->dmabuf, &data, &dataend);
2269 dataend->link.std = stat;
2270 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2274 upipe->u.ctl.length = len;
2276 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2278 setup->link.std = next;
2279 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2280 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2282 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2283 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2285 stat->link.std = NULL;
2286 stat->td.td_link = htole32(UHCI_PTR_T);
2287 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2288 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2290 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2291 UHCI_TD_IN (0, endpt, addr, 1));
2292 stat->td.td_buffer = htole32(0);
2295 if (uhcidebug > 10) {
2296 DPRINTF(("uhci_device_request: before transfer\n"));
2297 uhci_dump_tds(setup);
2301 /* Set up interrupt info. */
2303 ii->stdstart = setup;
2307 printf("uhci_device_request: not done, ii=%p\n", ii);
2313 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2316 if (dev->speed == USB_SPEED_LOW)
2317 uhci_add_ls_ctrl(sc, sqh);
2319 uhci_add_hs_ctrl(sc, sqh);
2320 uhci_add_intr_info(sc, ii);
2322 if (uhcidebug > 12) {
2323 uhci_soft_td_t *std;
2324 uhci_soft_qh_t *xqh;
2325 uhci_soft_qh_t *sxqh;
2327 uhci_physaddr_t link;
2328 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2329 for (std = sc->sc_vframes[0].htd, link = 0;
2330 (link & UHCI_PTR_QH) == 0;
2331 std = std->link.std) {
2332 link = le32toh(std->td.td_link);
2335 sxqh = (uhci_soft_qh_t *)std;
2339 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2340 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2343 DPRINTF(("Enqueued QH:\n"));
2345 uhci_dump_tds(sqh->elink);
2348 if (xfer->timeout && !sc->sc_bus.use_polling) {
2349 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2352 xfer->status = USBD_IN_PROGRESS;
2355 return (USBD_NORMAL_COMPLETION);
2359 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2363 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2365 /* Put it on our queue, */
2366 err = usb_insert_transfer(xfer);
2368 /* bail out on error, */
2369 if (err && err != USBD_IN_PROGRESS)
2372 /* XXX should check inuse here */
2374 /* insert into schedule, */
2375 uhci_device_isoc_enter(xfer);
2377 /* and start if the pipe wasn't running */
2379 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2385 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2387 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2388 usbd_device_handle dev = upipe->pipe.device;
2389 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2390 struct iso *iso = &upipe->u.iso;
2391 uhci_soft_td_t *std;
2392 u_int32_t buf, len, status;
2393 int s, i, next, nframes;
2395 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2397 iso->inuse, iso->next, xfer, xfer->nframes));
2402 if (xfer->status == USBD_IN_PROGRESS) {
2403 /* This request has already been entered into the frame list */
2404 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2409 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2410 printf("uhci_device_isoc_enter: overflow!\n");
2415 /* Not in use yet, schedule it a few frames ahead. */
2416 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2417 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2420 xfer->status = USBD_IN_PROGRESS;
2421 UXFER(xfer)->curframe = next;
2423 buf = DMAADDR(&xfer->dmabuf, 0);
2424 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2427 nframes = xfer->nframes;
2429 for (i = 0; i < nframes; i++) {
2430 std = iso->stds[next];
2431 if (++next >= UHCI_VFRAMELIST_COUNT)
2433 len = xfer->frlengths[i];
2434 std->td.td_buffer = htole32(buf);
2435 if (i == nframes - 1)
2436 status |= UHCI_TD_IOC;
2437 std->td.td_status = htole32(status);
2438 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2439 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2441 if (uhcidebug > 5) {
2442 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2449 iso->inuse += xfer->nframes;
2455 uhci_device_isoc_start(usbd_xfer_handle xfer)
2457 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2458 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2459 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2460 uhci_soft_td_t *end;
2463 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2466 return (USBD_IOERROR);
2469 if (xfer->status != USBD_IN_PROGRESS)
2470 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2473 /* Find the last TD */
2474 i = UXFER(xfer)->curframe + xfer->nframes;
2475 if (i >= UHCI_VFRAMELIST_COUNT)
2476 i -= UHCI_VFRAMELIST_COUNT;
2477 end = upipe->u.iso.stds[i];
2481 printf("uhci_device_isoc_start: end == NULL\n");
2482 return (USBD_INVAL);
2488 /* Set up interrupt info. */
2494 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2497 uhci_add_intr_info(sc, ii);
2501 return (USBD_IN_PROGRESS);
2505 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2507 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2508 uhci_soft_td_t **stds = upipe->u.iso.stds;
2509 uhci_soft_td_t *std;
2510 int i, n, s, nframes, maxlen, len;
2514 /* Transfer is already done. */
2515 if (xfer->status != USBD_NOT_STARTED &&
2516 xfer->status != USBD_IN_PROGRESS) {
2521 /* Give xfer the requested abort code. */
2522 xfer->status = USBD_CANCELLED;
2524 /* make hardware ignore it, */
2525 nframes = xfer->nframes;
2526 n = UXFER(xfer)->curframe;
2528 for (i = 0; i < nframes; i++) {
2530 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2531 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2534 if (++n >= UHCI_VFRAMELIST_COUNT)
2538 /* and wait until we are sure the hardware has finished. */
2542 UXFER(xfer)->iinfo.isdone = 1;
2544 /* Run callback and remove from interrupt list. */
2545 usb_transfer_complete(xfer);
2551 uhci_device_isoc_close(usbd_pipe_handle pipe)
2553 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2554 usbd_device_handle dev = upipe->pipe.device;
2555 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2556 uhci_soft_td_t *std, *vstd;
2561 * Make sure all TDs are marked as inactive.
2562 * Wait for completion.
2566 iso = &upipe->u.iso;
2568 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2569 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2570 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2573 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2575 for (vstd = sc->sc_vframes[i].htd;
2576 vstd != NULL && vstd->link.std != std;
2577 vstd = vstd->link.std)
2581 printf("uhci_device_isoc_close: %p not found\n", std);
2585 vstd->link = std->link;
2586 vstd->td.td_link = std->td.td_link;
2587 uhci_free_std(sc, std);
2591 free(iso->stds, M_USBHC);
2595 uhci_setup_isoc(usbd_pipe_handle pipe)
2597 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2598 usbd_device_handle dev = upipe->pipe.device;
2599 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2600 int addr = upipe->pipe.device->address;
2601 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2602 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2603 uhci_soft_td_t *std, *vstd;
2608 iso = &upipe->u.iso;
2609 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2612 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2613 UHCI_TD_OUT(0, endpt, addr, 0);
2615 /* Allocate the TDs and mark as inactive; */
2616 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2617 std = uhci_alloc_std(sc);
2620 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2621 std->td.td_token = htole32(token);
2625 /* Insert TDs into schedule. */
2627 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2629 vstd = sc->sc_vframes[i].htd;
2630 std->link = vstd->link;
2631 std->td.td_link = vstd->td.td_link;
2632 vstd->link.std = std;
2633 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2640 return (USBD_NORMAL_COMPLETION);
2644 uhci_free_std(sc, iso->stds[i]);
2645 free(iso->stds, M_USBHC);
2646 return (USBD_NOMEM);
2650 uhci_device_isoc_done(usbd_xfer_handle xfer)
2652 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2654 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2656 if (ii->xfer != xfer)
2657 /* Not on interrupt list, ignore it. */
2660 if (!uhci_active_intr_info(ii))
2664 if (xfer->busy_free != XFER_BUSY) {
2665 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2666 xfer, xfer->busy_free);
2670 if (ii->stdend == NULL) {
2671 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2679 /* Turn off the interrupt since it is active even if the TD is not. */
2680 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2682 uhci_del_intr_info(ii); /* remove from active list */
2685 if (ii->stdend == NULL) {
2686 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2696 uhci_device_intr_done(usbd_xfer_handle xfer)
2698 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2699 uhci_softc_t *sc = ii->sc;
2700 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2701 uhci_soft_qh_t *sqh;
2704 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2706 npoll = upipe->u.intr.npoll;
2707 for(i = 0; i < npoll; i++) {
2708 sqh = upipe->u.intr.qhs[i];
2710 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2712 uhci_free_std_chain(sc, ii->stdstart, NULL);
2715 if (xfer->pipe->repeat) {
2716 uhci_soft_td_t *data, *dataend;
2718 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2720 /* This alloc cannot fail since we freed the chain above. */
2721 uhci_alloc_std_chain(upipe, sc, xfer->length,
2722 upipe->u.intr.isread, xfer->flags,
2723 &xfer->dmabuf, &data, &dataend);
2724 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2727 if (uhcidebug > 10) {
2728 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2729 uhci_dump_tds(data);
2730 uhci_dump_qh(upipe->u.intr.qhs[0]);
2734 ii->stdstart = data;
2735 ii->stdend = dataend;
2738 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2742 for (i = 0; i < npoll; i++) {
2743 sqh = upipe->u.intr.qhs[i];
2745 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2747 xfer->status = USBD_IN_PROGRESS;
2748 /* The ii is already on the examined list, just leave it. */
2750 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2751 if (uhci_active_intr_info(ii))
2752 uhci_del_intr_info(ii);
2756 /* Deallocate request data structures */
2758 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2760 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2761 uhci_softc_t *sc = ii->sc;
2762 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2765 if (!(xfer->rqflags & URQ_REQUEST))
2766 panic("uhci_device_ctrl_done: not a request");
2769 if (!uhci_active_intr_info(ii))
2772 uhci_del_intr_info(ii); /* remove from active list */
2774 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2775 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2777 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2779 if (upipe->u.ctl.length != 0)
2780 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2782 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2785 /* Deallocate request data structures */
2787 uhci_device_bulk_done(usbd_xfer_handle xfer)
2789 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2790 uhci_softc_t *sc = ii->sc;
2791 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2793 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2794 xfer, ii, sc, upipe));
2796 if (!uhci_active_intr_info(ii))
2799 uhci_del_intr_info(ii); /* remove from active list */
2801 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2803 uhci_free_std_chain(sc, ii->stdstart, NULL);
2805 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2808 /* Add interrupt QH, called with vflock. */
2810 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2812 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2813 uhci_soft_qh_t *eqh;
2815 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2818 sqh->hlink = eqh->hlink;
2819 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2821 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2826 /* Remove interrupt QH. */
2828 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2830 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2831 uhci_soft_qh_t *pqh;
2833 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2835 /* See comment in uhci_remove_ctrl() */
2836 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2837 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2838 delay(UHCI_QH_REMOVE_DELAY);
2841 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2842 pqh->hlink = sqh->hlink;
2843 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2844 delay(UHCI_QH_REMOVE_DELAY);
2851 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2853 uhci_soft_qh_t *sqh;
2855 u_int bestbw, bw, bestoffs, offs;
2857 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2859 printf("uhci_setintr: 0 interval\n");
2860 return (USBD_INVAL);
2863 if (ival > UHCI_VFRAMELIST_COUNT)
2864 ival = UHCI_VFRAMELIST_COUNT;
2865 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2866 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2868 upipe->u.intr.npoll = npoll;
2870 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2873 * Figure out which offset in the schedule that has most
2874 * bandwidth left over.
2876 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2877 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2878 for (bw = i = 0; i < npoll; i++)
2879 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2885 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2887 for(i = 0; i < npoll; i++) {
2888 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2890 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2891 sqh->pos = MOD(i * ival + bestoffs);
2896 /* Enter QHs into the controller data structures. */
2897 for(i = 0; i < npoll; i++)
2898 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2901 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2902 return (USBD_NORMAL_COMPLETION);
2905 /* Open a new pipe. */
2907 uhci_open(usbd_pipe_handle pipe)
2909 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2910 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2911 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2915 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2916 pipe, pipe->device->address,
2917 ed->bEndpointAddress, sc->sc_addr));
2919 upipe->aborting = 0;
2920 upipe->nexttoggle = 0;
2922 if (pipe->device->address == sc->sc_addr) {
2923 switch (ed->bEndpointAddress) {
2924 case USB_CONTROL_ENDPOINT:
2925 pipe->methods = &uhci_root_ctrl_methods;
2927 case UE_DIR_IN | UHCI_INTR_ENDPT:
2928 pipe->methods = &uhci_root_intr_methods;
2931 return (USBD_INVAL);
2934 switch (ed->bmAttributes & UE_XFERTYPE) {
2936 pipe->methods = &uhci_device_ctrl_methods;
2937 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2938 if (upipe->u.ctl.sqh == NULL)
2940 upipe->u.ctl.setup = uhci_alloc_std(sc);
2941 if (upipe->u.ctl.setup == NULL) {
2942 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2945 upipe->u.ctl.stat = uhci_alloc_std(sc);
2946 if (upipe->u.ctl.stat == NULL) {
2947 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2948 uhci_free_std(sc, upipe->u.ctl.setup);
2951 err = usb_allocmem(&sc->sc_bus,
2952 sizeof(usb_device_request_t),
2953 0, &upipe->u.ctl.reqdma);
2955 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2956 uhci_free_std(sc, upipe->u.ctl.setup);
2957 uhci_free_std(sc, upipe->u.ctl.stat);
2962 pipe->methods = &uhci_device_intr_methods;
2963 ival = pipe->interval;
2964 if (ival == USBD_DEFAULT_INTERVAL)
2965 ival = ed->bInterval;
2966 return (uhci_device_setintr(sc, upipe, ival));
2967 case UE_ISOCHRONOUS:
2968 pipe->methods = &uhci_device_isoc_methods;
2969 return (uhci_setup_isoc(pipe));
2971 pipe->methods = &uhci_device_bulk_methods;
2972 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2973 if (upipe->u.bulk.sqh == NULL)
2978 return (USBD_NORMAL_COMPLETION);
2981 return (USBD_NOMEM);
2985 * Data structures and routines to emulate the root hub.
2987 usb_device_descriptor_t uhci_devd = {
2988 USB_DEVICE_DESCRIPTOR_SIZE,
2989 UDESC_DEVICE, /* type */
2990 {0x00, 0x01}, /* USB version */
2991 UDCLASS_HUB, /* class */
2992 UDSUBCLASS_HUB, /* subclass */
2993 UDPROTO_FSHUB, /* protocol */
2994 64, /* max packet */
2995 {0},{0},{0x00,0x01}, /* device id */
2996 1,2,0, /* string indicies */
2997 1 /* # of configurations */
3000 usb_config_descriptor_t uhci_confd = {
3001 USB_CONFIG_DESCRIPTOR_SIZE,
3003 {USB_CONFIG_DESCRIPTOR_SIZE +
3004 USB_INTERFACE_DESCRIPTOR_SIZE +
3005 USB_ENDPOINT_DESCRIPTOR_SIZE},
3013 usb_interface_descriptor_t uhci_ifcd = {
3014 USB_INTERFACE_DESCRIPTOR_SIZE,
3025 usb_endpoint_descriptor_t uhci_endpd = {
3026 USB_ENDPOINT_DESCRIPTOR_SIZE,
3028 UE_DIR_IN | UHCI_INTR_ENDPT,
3034 usb_hub_descriptor_t uhci_hubd_piix = {
3035 USB_HUB_DESCRIPTOR_SIZE,
3038 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
3039 50, /* power on to power good */
3041 { 0x00 }, /* both ports are removable */
3045 uhci_str(usb_string_descriptor_t *p, int l, char *s)
3051 p->bLength = 2 * strlen(s) + 2;
3054 p->bDescriptorType = UDESC_STRING;
3056 for (i = 0; s[i] && l > 1; i++, l -= 2)
3057 USETW2(p->bString[i], 0, s[i]);
3062 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3063 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3064 * should not be used by the USB subsystem. As we cannot issue a
3065 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3066 * will be enabled as part of the reset.
3068 * On the VT83C572, the port cannot be successfully enabled until the
3069 * outstanding "port enable change" and "connection status change"
3070 * events have been reset.
3073 uhci_portreset(uhci_softc_t *sc, int index)
3078 port = UHCI_PORTSC1;
3079 else if (index == 2)
3080 port = UHCI_PORTSC2;
3082 return (USBD_IOERROR);
3084 x = URWMASK(UREAD2(sc, port));
3085 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3087 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3089 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3090 index, UREAD2(sc, port)));
3092 x = URWMASK(UREAD2(sc, port));
3093 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3097 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3098 index, UREAD2(sc, port)));
3100 x = URWMASK(UREAD2(sc, port));
3101 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3103 for (lim = 10; --lim > 0;) {
3104 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3106 x = UREAD2(sc, port);
3108 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3111 if (!(x & UHCI_PORTSC_CCS)) {
3113 * No device is connected (or was disconnected
3114 * during reset). Consider the port reset.
3115 * The delay must be long enough to ensure on
3116 * the initial iteration that the device
3117 * connection will have been registered. 50ms
3118 * appears to be sufficient, but 20ms is not.
3120 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3125 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3127 * Port enabled changed and/or connection
3128 * status changed were set. Reset either or
3129 * both raised flags (by writing a 1 to that
3130 * bit), and wait again for state to settle.
3132 UWRITE2(sc, port, URWMASK(x) |
3133 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3137 if (x & UHCI_PORTSC_PE)
3138 /* Port is enabled */
3141 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3144 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3145 index, UREAD2(sc, port)));
3148 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3149 return (USBD_TIMEOUT);
3153 return (USBD_NORMAL_COMPLETION);
3157 * Simulate a hardware hub by handling all the necessary requests.
3160 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3164 /* Insert last in queue. */
3165 err = usb_insert_transfer(xfer);
3170 * Pipe isn't running (otherwise err would be USBD_INPROG),
3171 * so start it first.
3173 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3177 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3179 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3180 usb_device_request_t *req;
3183 int s, len, value, index, status, change, l, totlen = 0;
3184 usb_port_status_t ps;
3188 return (USBD_IOERROR);
3191 if (!(xfer->rqflags & URQ_REQUEST))
3192 panic("uhci_root_ctrl_transfer: not a request");
3194 req = &xfer->request;
3196 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3197 req->bmRequestType, req->bRequest));
3199 len = UGETW(req->wLength);
3200 value = UGETW(req->wValue);
3201 index = UGETW(req->wIndex);
3204 buf = KERNADDR(&xfer->dmabuf, 0);
3206 #define C(x,y) ((x) | ((y) << 8))
3207 switch(C(req->bRequest, req->bmRequestType)) {
3208 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3209 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3210 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3212 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3213 * for the integrated root hub.
3216 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3218 *(u_int8_t *)buf = sc->sc_conf;
3222 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3223 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3224 switch(value >> 8) {
3226 if ((value & 0xff) != 0) {
3230 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3231 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3232 memcpy(buf, &uhci_devd, l);
3235 if ((value & 0xff) != 0) {
3239 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3240 memcpy(buf, &uhci_confd, l);
3241 buf = (char *)buf + l;
3243 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3245 memcpy(buf, &uhci_ifcd, l);
3246 buf = (char *)buf + l;
3248 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3250 memcpy(buf, &uhci_endpd, l);
3255 *(u_int8_t *)buf = 0;
3257 switch (value & 0xff) {
3258 case 1: /* Vendor */
3259 totlen = uhci_str(buf, len, sc->sc_vendor);
3261 case 2: /* Product */
3262 totlen = uhci_str(buf, len, "UHCI root hub");
3271 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3273 *(u_int8_t *)buf = 0;
3277 case C(UR_GET_STATUS, UT_READ_DEVICE):
3279 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3283 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3284 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3286 USETW(((usb_status_t *)buf)->wStatus, 0);
3290 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3291 if (value >= USB_MAX_DEVICES) {
3295 sc->sc_addr = value;
3297 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3298 if (value != 0 && value != 1) {
3302 sc->sc_conf = value;
3304 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3306 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3307 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3308 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3311 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3313 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3316 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3318 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3319 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3320 "port=%d feature=%d\n",
3323 port = UHCI_PORTSC1;
3324 else if (index == 2)
3325 port = UHCI_PORTSC2;
3331 case UHF_PORT_ENABLE:
3332 x = URWMASK(UREAD2(sc, port));
3333 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3335 case UHF_PORT_SUSPEND:
3336 x = URWMASK(UREAD2(sc, port));
3337 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3339 case UHF_PORT_RESET:
3340 x = URWMASK(UREAD2(sc, port));
3341 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3343 case UHF_C_PORT_CONNECTION:
3344 x = URWMASK(UREAD2(sc, port));
3345 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3347 case UHF_C_PORT_ENABLE:
3348 x = URWMASK(UREAD2(sc, port));
3349 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3351 case UHF_C_PORT_OVER_CURRENT:
3352 x = URWMASK(UREAD2(sc, port));
3353 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3355 case UHF_C_PORT_RESET:
3357 err = USBD_NORMAL_COMPLETION;
3359 case UHF_PORT_CONNECTION:
3360 case UHF_PORT_OVER_CURRENT:
3361 case UHF_PORT_POWER:
3362 case UHF_PORT_LOW_SPEED:
3363 case UHF_C_PORT_SUSPEND:
3369 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3371 port = UHCI_PORTSC1;
3372 else if (index == 2)
3373 port = UHCI_PORTSC2;
3380 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3381 UHCI_PORTSC_LS_SHIFT;
3385 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3386 if ((value & 0xff) != 0) {
3390 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3392 memcpy(buf, &uhci_hubd_piix, l);
3394 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3399 memset(buf, 0, len);
3402 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3404 port = UHCI_PORTSC1;
3405 else if (index == 2)
3406 port = UHCI_PORTSC2;
3415 x = UREAD2(sc, port);
3416 status = change = 0;
3417 if (x & UHCI_PORTSC_CCS)
3418 status |= UPS_CURRENT_CONNECT_STATUS;
3419 if (x & UHCI_PORTSC_CSC)
3420 change |= UPS_C_CONNECT_STATUS;
3421 if (x & UHCI_PORTSC_PE)
3422 status |= UPS_PORT_ENABLED;
3423 if (x & UHCI_PORTSC_POEDC)
3424 change |= UPS_C_PORT_ENABLED;
3425 if (x & UHCI_PORTSC_OCI)
3426 status |= UPS_OVERCURRENT_INDICATOR;
3427 if (x & UHCI_PORTSC_OCIC)
3428 change |= UPS_C_OVERCURRENT_INDICATOR;
3429 if (x & UHCI_PORTSC_SUSP)
3430 status |= UPS_SUSPEND;
3431 if (x & UHCI_PORTSC_LSDA)
3432 status |= UPS_LOW_SPEED;
3433 status |= UPS_PORT_POWER;
3435 change |= UPS_C_PORT_RESET;
3436 USETW(ps.wPortStatus, status);
3437 USETW(ps.wPortChange, change);
3438 l = min(len, sizeof ps);
3439 memcpy(buf, &ps, l);
3442 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3445 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3447 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3449 port = UHCI_PORTSC1;
3450 else if (index == 2)
3451 port = UHCI_PORTSC2;
3457 case UHF_PORT_ENABLE:
3458 x = URWMASK(UREAD2(sc, port));
3459 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3461 case UHF_PORT_SUSPEND:
3462 x = URWMASK(UREAD2(sc, port));
3463 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3465 case UHF_PORT_RESET:
3466 err = uhci_portreset(sc, index);
3468 case UHF_PORT_POWER:
3469 /* Pretend we turned on power */
3470 err = USBD_NORMAL_COMPLETION;
3472 case UHF_C_PORT_CONNECTION:
3473 case UHF_C_PORT_ENABLE:
3474 case UHF_C_PORT_OVER_CURRENT:
3475 case UHF_PORT_CONNECTION:
3476 case UHF_PORT_OVER_CURRENT:
3477 case UHF_PORT_LOW_SPEED:
3478 case UHF_C_PORT_SUSPEND:
3479 case UHF_C_PORT_RESET:
3489 xfer->actlen = totlen;
3490 err = USBD_NORMAL_COMPLETION;
3494 usb_transfer_complete(xfer);
3496 return (USBD_IN_PROGRESS);
3499 /* Abort a root control request. */
3501 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3503 /* Nothing to do, all transfers are synchronous. */
3506 /* Close the root pipe. */
3508 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3510 DPRINTF(("uhci_root_ctrl_close\n"));
3513 /* Abort a root interrupt request. */
3515 uhci_root_intr_abort(usbd_xfer_handle xfer)
3517 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3519 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3520 sc->sc_intr_xfer = NULL;
3522 if (xfer->pipe->intrxfer == xfer) {
3523 DPRINTF(("uhci_root_intr_abort: remove\n"));
3524 xfer->pipe->intrxfer = 0;
3526 xfer->status = USBD_CANCELLED;
3528 UXFER(xfer)->iinfo.isdone = 1;
3530 usb_transfer_complete(xfer);
3534 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3538 /* Insert last in queue. */
3539 err = usb_insert_transfer(xfer);
3544 * Pipe isn't running (otherwise err would be USBD_INPROG),
3545 * so start it first.
3547 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3550 /* Start a transfer on the root interrupt pipe */
3552 uhci_root_intr_start(usbd_xfer_handle xfer)
3554 usbd_pipe_handle pipe = xfer->pipe;
3555 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3557 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3558 xfer, xfer->length, xfer->flags));
3561 return (USBD_IOERROR);
3563 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3564 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3565 sc->sc_intr_xfer = xfer;
3566 return (USBD_IN_PROGRESS);
3569 /* Close the root interrupt pipe. */
3571 uhci_root_intr_close(usbd_pipe_handle pipe)
3573 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3575 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3576 sc->sc_intr_xfer = NULL;
3577 DPRINTF(("uhci_root_intr_close\n"));