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);
590 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
593 #if defined(__NetBSD__) || defined(__OpenBSD__)
594 powerhook_disestablish(sc->sc_powerhook);
595 shutdownhook_disestablish(sc->sc_shutdownhook);
598 /* Free all xfers associated with this HC. */
600 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
603 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
607 /* XXX free other data structures XXX */
608 usb_freemem(&sc->sc_bus, &sc->sc_dma);
614 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
616 return (usb_allocmem(bus, size, 0, dma));
620 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
622 usb_freemem(bus, dma);
626 uhci_allocx(struct usbd_bus *bus)
628 struct uhci_softc *sc = (struct uhci_softc *)bus;
629 usbd_xfer_handle xfer;
631 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
633 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
635 if (xfer->busy_free != XFER_FREE) {
636 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
641 xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_NOWAIT);
644 memset(xfer, 0, sizeof (struct uhci_xfer));
645 UXFER(xfer)->iinfo.sc = sc;
646 usb_init_task(&UXFER(xfer)->abort_task, uhci_timeout_task,
648 UXFER(xfer)->uhci_xfer_flags = 0;
650 UXFER(xfer)->iinfo.isdone = 1;
651 xfer->busy_free = XFER_BUSY;
658 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
660 struct uhci_softc *sc = (struct uhci_softc *)bus;
663 if (xfer->busy_free != XFER_BUSY) {
664 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
668 xfer->busy_free = XFER_FREE;
669 if (!UXFER(xfer)->iinfo.isdone) {
670 printf("uhci_freex: !isdone\n");
674 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
678 * Shut down the controller when the system is going down.
681 uhci_shutdown(void *v)
683 uhci_softc_t *sc = v;
685 DPRINTF(("uhci_shutdown: stopping the HC\n"));
686 uhci_run(sc, 0); /* stop the controller */
690 * Handle suspend/resume.
692 * We need to switch to polling mode here, because this routine is
693 * called from an interrupt context. This is all right since we
694 * are almost suspended anyway.
697 uhci_power(int why, void *v)
699 uhci_softc_t *sc = v;
704 cmd = UREAD2(sc, UHCI_CMD);
706 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
707 sc, why, sc->sc_suspend, cmd));
709 if (why != PWR_RESUME) {
714 if (sc->sc_intr_xfer != NULL)
715 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub,
717 sc->sc_bus.use_polling++;
718 uhci_run(sc, 0); /* stop the controller */
720 /* save some state if BIOS doesn't */
721 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
722 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
724 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
726 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
727 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
728 sc->sc_suspend = why;
729 sc->sc_bus.use_polling--;
730 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
733 if (sc->sc_suspend == PWR_RESUME)
734 printf("uhci_power: weird, resume without suspend.\n");
736 sc->sc_bus.use_polling++;
737 sc->sc_suspend = why;
738 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
739 uhci_globalreset(sc); /* reset the controller */
741 if (cmd & UHCI_CMD_RS)
742 uhci_run(sc, 0); /* in case BIOS has started it */
744 uhci_globalreset(sc);
747 /* restore saved state */
748 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
749 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
750 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
752 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
753 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
754 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
755 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
756 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
757 UHCICMD(sc, UHCI_CMD_MAXP);
758 uhci_run(sc, 1); /* and start traffic again */
759 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
760 sc->sc_bus.use_polling--;
761 if (sc->sc_intr_xfer != NULL)
762 usb_callout(sc->sc_poll_handle, sc->sc_ival,
763 uhci_poll_hub, sc->sc_intr_xfer);
774 uhci_dumpregs(uhci_softc_t *sc)
776 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
777 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
778 USBDEVNAME(sc->sc_bus.bdev),
779 UREAD2(sc, UHCI_CMD),
780 UREAD2(sc, UHCI_STS),
781 UREAD2(sc, UHCI_INTR),
782 UREAD2(sc, UHCI_FRNUM),
783 UREAD4(sc, UHCI_FLBASEADDR),
784 UREAD1(sc, UHCI_SOF),
785 UREAD2(sc, UHCI_PORTSC1),
786 UREAD2(sc, UHCI_PORTSC2)));
790 uhci_dump_td(uhci_soft_td_t *p)
792 char sbuf[128], sbuf2[128];
794 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
795 "token=0x%08lx buffer=0x%08lx\n",
796 p, (long)p->physaddr,
797 (long)le32toh(p->td.td_link),
798 (long)le32toh(p->td.td_status),
799 (long)le32toh(p->td.td_token),
800 (long)le32toh(p->td.td_buffer)));
802 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
804 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
805 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
806 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
807 sbuf2, sizeof(sbuf2));
809 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
810 "D=%d,maxlen=%d\n", sbuf, sbuf2,
811 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
812 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
813 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
814 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
815 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
816 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
817 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
821 uhci_dump_qh(uhci_soft_qh_t *sqh)
823 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
824 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
825 le32toh(sqh->qh.qh_elink)));
833 uhci_dump_all(thesc);
838 uhci_dump_all(uhci_softc_t *sc)
841 printf("intrs=%d\n", sc->sc_bus.no_intrs);
842 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
843 uhci_dump_qh(sc->sc_lctl_start);
848 uhci_dump_qhs(uhci_soft_qh_t *sqh)
852 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
853 * Traverses sideways first, then down.
863 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
867 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
868 uhci_dump_qhs(sqh->hlink);
870 DPRINTF(("No QH\n"));
872 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
873 uhci_dump_tds(sqh->elink);
875 DPRINTF(("No TD\n"));
879 uhci_dump_tds(uhci_soft_td_t *std)
883 for(td = std; td != NULL; td = td->link.std) {
886 /* Check whether the link pointer in this TD marks
887 * the link pointer as end of queue. This avoids
888 * printing the free list in case the queue/TD has
889 * already been moved there (seatbelt).
891 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
892 le32toh(td->td.td_link) == 0)
898 uhci_dump_ii(uhci_intr_info_t *ii)
900 usbd_pipe_handle pipe;
901 usb_endpoint_descriptor_t *ed;
902 usbd_device_handle dev;
905 #define DONE ii->isdone
913 if (ii->xfer == NULL) {
914 printf("ii %p: done=%d xfer=NULL\n",
918 pipe = ii->xfer->pipe;
920 printf("ii %p: done=%d xfer=%p pipe=NULL\n",
924 if (pipe->endpoint == NULL) {
925 printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
926 ii, DONE, ii->xfer, pipe);
929 if (pipe->device == NULL) {
930 printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
931 ii, DONE, ii->xfer, pipe);
934 ed = pipe->endpoint->edesc;
936 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",
937 ii, DONE, ii->xfer, dev,
938 UGETW(dev->ddesc.idVendor),
939 UGETW(dev->ddesc.idProduct),
941 ed->bEndpointAddress, ed->bmAttributes);
945 void uhci_dump_iis(struct uhci_softc *sc);
947 uhci_dump_iis(struct uhci_softc *sc)
949 uhci_intr_info_t *ii;
951 printf("intr_info list:\n");
952 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
957 void iidump(void) { uhci_dump_iis(thesc); }
962 * This routine is executed periodically and simulates interrupts
963 * from the root controller interrupt pipe for port status change.
966 uhci_poll_hub(void *addr)
968 usbd_xfer_handle xfer = addr;
969 usbd_pipe_handle pipe = xfer->pipe;
970 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
974 DPRINTFN(20, ("uhci_poll_hub\n"));
976 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
978 p = KERNADDR(&xfer->dmabuf, 0);
980 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
982 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
985 /* No change, try again in a while */
989 xfer->status = USBD_NORMAL_COMPLETION;
991 xfer->device->bus->intr_context++;
992 usb_transfer_complete(xfer);
993 xfer->device->bus->intr_context--;
998 uhci_root_intr_done(usbd_xfer_handle xfer)
1003 uhci_root_ctrl_done(usbd_xfer_handle xfer)
1008 * Let the last QH loop back to the high speed control transfer QH.
1009 * This is what intel calls "bandwidth reclamation" and improves
1010 * USB performance a lot for some devices.
1011 * If we are already looping, just count it.
1014 uhci_add_loop(uhci_softc_t *sc) {
1019 if (++sc->sc_loops == 1) {
1020 DPRINTFN(5,("uhci_start_loop: add\n"));
1021 /* Note, we don't loop back the soft pointer. */
1022 sc->sc_last_qh->qh.qh_hlink =
1023 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
1028 uhci_rem_loop(uhci_softc_t *sc) {
1033 if (--sc->sc_loops == 0) {
1034 DPRINTFN(5,("uhci_end_loop: remove\n"));
1035 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1039 /* Add high speed control QH, called at splusb(). */
1041 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1043 uhci_soft_qh_t *eqh;
1047 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
1048 eqh = sc->sc_hctl_end;
1049 sqh->hlink = eqh->hlink;
1050 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1052 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1053 sc->sc_hctl_end = sqh;
1054 #ifdef UHCI_CTL_LOOP
1059 /* Remove high speed control QH, called at splusb(). */
1061 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1063 uhci_soft_qh_t *pqh;
1067 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1068 #ifdef UHCI_CTL_LOOP
1072 * The T bit should be set in the elink of the QH so that the HC
1073 * doesn't follow the pointer. This condition may fail if the
1074 * the transferred packet was short so that the QH still points
1075 * at the last used TD.
1076 * In this case we set the T bit and wait a little for the HC
1077 * to stop looking at the TD.
1079 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1080 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1081 delay(UHCI_QH_REMOVE_DELAY);
1084 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1085 pqh->hlink = sqh->hlink;
1086 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1087 delay(UHCI_QH_REMOVE_DELAY);
1088 if (sc->sc_hctl_end == sqh)
1089 sc->sc_hctl_end = pqh;
1092 /* Add low speed control QH, called at splusb(). */
1094 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1096 uhci_soft_qh_t *eqh;
1100 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1101 eqh = sc->sc_lctl_end;
1102 sqh->hlink = eqh->hlink;
1103 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1105 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1106 sc->sc_lctl_end = sqh;
1109 /* Remove low speed control QH, called at splusb(). */
1111 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1113 uhci_soft_qh_t *pqh;
1117 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1118 /* See comment in uhci_remove_hs_ctrl() */
1119 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1120 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1121 delay(UHCI_QH_REMOVE_DELAY);
1123 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1124 pqh->hlink = sqh->hlink;
1125 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1126 delay(UHCI_QH_REMOVE_DELAY);
1127 if (sc->sc_lctl_end == sqh)
1128 sc->sc_lctl_end = pqh;
1131 /* Add bulk QH, called at splusb(). */
1133 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1135 uhci_soft_qh_t *eqh;
1139 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1140 eqh = sc->sc_bulk_end;
1141 sqh->hlink = eqh->hlink;
1142 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1144 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1145 sc->sc_bulk_end = sqh;
1149 /* Remove bulk QH, called at splusb(). */
1151 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1153 uhci_soft_qh_t *pqh;
1157 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1159 /* See comment in uhci_remove_hs_ctrl() */
1160 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1161 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1162 delay(UHCI_QH_REMOVE_DELAY);
1164 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1165 pqh->hlink = sqh->hlink;
1166 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1167 delay(UHCI_QH_REMOVE_DELAY);
1168 if (sc->sc_bulk_end == sqh)
1169 sc->sc_bulk_end = pqh;
1172 Static int uhci_intr1(uhci_softc_t *);
1175 uhci_intr(void *arg)
1177 uhci_softc_t *sc = arg;
1182 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1183 if (sc->sc_bus.use_polling) {
1185 printf("uhci_intr: ignored interrupt while polling\n");
1189 return (uhci_intr1(sc));
1193 uhci_intr1(uhci_softc_t *sc)
1200 * It can happen that an interrupt will be delivered to
1201 * us before the device has been fully attached and the
1202 * softc struct has been configured. Usually this happens
1203 * when kldloading the USB support as a module after the
1204 * system has been booted. If we detect this condition,
1205 * we need to squelch the unwanted interrupts until we're
1208 if (sc->sc_bus.bdev == NULL) {
1209 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1210 uhci_run(sc, 0); /* stop the controller */
1211 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1216 if (uhcidebug > 15) {
1217 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev)));
1221 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1222 if (status == 0) /* The interrupt was not for us. */
1225 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1226 if (sc->sc_suspend != PWR_RESUME)
1227 printf("uhci_intr: suspended sts=0x%x\n", status);
1230 if (sc->sc_suspend != PWR_RESUME) {
1231 printf("%s: interrupt while not operating ignored\n",
1232 USBDEVNAME(sc->sc_bus.bdev));
1233 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1238 if (status & UHCI_STS_USBINT)
1239 ack |= UHCI_STS_USBINT;
1240 if (status & UHCI_STS_USBEI)
1241 ack |= UHCI_STS_USBEI;
1242 if (status & UHCI_STS_RD) {
1245 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1248 if (status & UHCI_STS_HSE) {
1249 ack |= UHCI_STS_HSE;
1250 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
1252 if (status & UHCI_STS_HCPE) {
1253 ack |= UHCI_STS_HCPE;
1254 printf("%s: host controller process error\n",
1255 USBDEVNAME(sc->sc_bus.bdev));
1257 if (status & UHCI_STS_HCH) {
1258 /* no acknowledge needed */
1259 if (!sc->sc_dying) {
1260 printf("%s: host controller halted\n",
1261 USBDEVNAME(sc->sc_bus.bdev));
1270 return (0); /* nothing to acknowledge */
1271 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1273 sc->sc_bus.no_intrs++;
1274 usb_schedsoftintr(&sc->sc_bus);
1276 DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
1282 uhci_softintr(void *v)
1284 uhci_softc_t *sc = v;
1285 uhci_intr_info_t *ii, *nextii;
1287 DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
1288 sc->sc_bus.intr_context));
1290 sc->sc_bus.intr_context++;
1293 * Interrupts on UHCI really suck. When the host controller
1294 * interrupts because a transfer is completed there is no
1295 * way of knowing which transfer it was. You can scan down
1296 * the TDs and QHs of the previous frame to limit the search,
1297 * but that assumes that the interrupt was not delayed by more
1298 * than 1 ms, which may not always be true (e.g. after debug
1299 * output on a slow console).
1300 * We scan all interrupt descriptors to see if any have
1303 LIST_FOREACH_SAFE(ii, &sc->sc_intrhead, list, nextii)
1304 uhci_check_intr(sc, ii);
1306 #ifdef USB_USE_SOFTINTR
1307 if (sc->sc_softwake) {
1308 sc->sc_softwake = 0;
1309 wakeup(&sc->sc_softwake);
1311 #endif /* USB_USE_SOFTINTR */
1313 sc->sc_bus.intr_context--;
1316 /* Check for an interrupt. */
1318 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1320 uhci_soft_td_t *std, *lstd;
1323 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1326 printf("uhci_check_intr: no ii? %p\n", ii);
1330 if (ii->xfer->status == USBD_CANCELLED ||
1331 ii->xfer->status == USBD_TIMEOUT) {
1332 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1336 if (ii->stdstart == NULL)
1341 printf("uhci_check_intr: std==0\n");
1346 * If the last TD is still active we need to check whether there
1347 * is an error somewhere in the middle, or whether there was a
1348 * short packet (SPD and not ACTIVE).
1350 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1351 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1352 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1353 status = le32toh(std->td.td_status);
1354 /* If there's an active TD the xfer isn't done. */
1355 if (status & UHCI_TD_ACTIVE)
1357 /* Any kind of error makes the xfer done. */
1358 if (status & UHCI_TD_STALLED)
1360 /* We want short packets, and it is short: it's done */
1361 if ((status & UHCI_TD_SPD) &&
1362 UHCI_TD_GET_ACTLEN(status) <
1363 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)))
1366 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1371 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1372 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1373 usb_rem_task(ii->xfer->pipe->device, &UXFER(ii->xfer)->abort_task);
1377 /* Called at splusb() */
1379 uhci_idone(uhci_intr_info_t *ii)
1381 usbd_xfer_handle xfer = ii->xfer;
1382 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1383 uhci_soft_td_t *std;
1384 u_int32_t status = 0, nstatus;
1387 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1394 printf("uhci_idone: ii is done!\n ");
1397 printf("uhci_idone: ii=%p is done!\n", ii);
1406 if (xfer->nframes != 0) {
1407 /* Isoc transfer, do things differently. */
1408 uhci_soft_td_t **stds = upipe->u.iso.stds;
1409 int i, n, nframes, len;
1411 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1413 nframes = xfer->nframes;
1415 n = UXFER(xfer)->curframe;
1416 for (i = 0; i < nframes; i++) {
1419 if (uhcidebug > 5) {
1420 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1424 if (++n >= UHCI_VFRAMELIST_COUNT)
1426 status = le32toh(std->td.td_status);
1427 len = UHCI_TD_GET_ACTLEN(status);
1428 xfer->frlengths[i] = len;
1431 upipe->u.iso.inuse -= nframes;
1432 xfer->actlen = actlen;
1433 xfer->status = USBD_NORMAL_COMPLETION;
1438 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1441 uhci_dump_tds(ii->stdstart);
1444 /* The transfer is done, compute actual length and status. */
1446 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1447 nstatus = le32toh(std->td.td_status);
1448 if (nstatus & UHCI_TD_ACTIVE)
1452 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1454 actlen += UHCI_TD_GET_ACTLEN(status);
1457 * UHCI will report CRCTO in addition to a STALL or NAK
1458 * for a SETUP transaction. See section 3.2.2, "TD
1459 * CONTROL AND STATUS".
1461 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1462 status &= ~UHCI_TD_CRCTO;
1465 /* If there are left over TDs we need to update the toggle. */
1467 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1469 status &= UHCI_TD_ERROR;
1470 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1472 xfer->actlen = actlen;
1477 bitmask_snprintf((u_int32_t)status,
1478 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1479 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1480 sbuf, sizeof(sbuf));
1482 DPRINTFN((status == UHCI_TD_STALLED)*10,
1483 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1485 xfer->pipe->device->address,
1486 xfer->pipe->endpoint->edesc->bEndpointAddress,
1490 if (status == UHCI_TD_STALLED)
1491 xfer->status = USBD_STALLED;
1493 xfer->status = USBD_IOERROR; /* more info XXX */
1495 xfer->status = USBD_NORMAL_COMPLETION;
1499 usb_transfer_complete(xfer);
1500 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1504 * Called when a request does not complete.
1507 uhci_timeout(void *addr)
1509 uhci_intr_info_t *ii = addr;
1510 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1511 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1512 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1514 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1517 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1521 /* Execute the abort in a process context. */
1522 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1526 uhci_timeout_task(void *addr)
1528 usbd_xfer_handle xfer = addr;
1531 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1534 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1539 * Wait here until controller claims to have an interrupt.
1540 * Then call uhci_intr and return. Use timeout to avoid waiting
1542 * Only used during boot when interrupts are not enabled yet.
1545 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1547 int timo = xfer->timeout;
1548 uhci_intr_info_t *ii;
1550 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1552 xfer->status = USBD_IN_PROGRESS;
1553 for (; timo >= 0; timo--) {
1554 usb_delay_ms(&sc->sc_bus, 1);
1555 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1556 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1558 if (xfer->status != USBD_IN_PROGRESS)
1563 DPRINTF(("uhci_waitintr: timeout\n"));
1564 for (ii = LIST_FIRST(&sc->sc_intrhead);
1565 ii != NULL && ii->xfer != xfer;
1566 ii = LIST_NEXT(ii, list))
1570 panic("uhci_waitintr: lost intr_info");
1576 uhci_poll(struct usbd_bus *bus)
1578 uhci_softc_t *sc = (uhci_softc_t *)bus;
1580 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1585 uhci_reset(uhci_softc_t *sc)
1589 UHCICMD(sc, UHCI_CMD_HCRESET);
1590 /* The reset bit goes low when the controller is done. */
1591 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1592 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1593 usb_delay_ms(&sc->sc_bus, 1);
1594 if (n >= UHCI_RESET_TIMEOUT)
1595 printf("%s: controller did not reset\n",
1596 USBDEVNAME(sc->sc_bus.bdev));
1600 uhci_run(uhci_softc_t *sc, int run)
1607 DPRINTF(("uhci_run: setting run=%d\n", run));
1608 cmd = UREAD2(sc, UHCI_CMD);
1612 cmd &= ~UHCI_CMD_RS;
1614 for(n = 0; n < 10; n++) {
1615 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1616 /* return when we've entered the state we want */
1617 if (run == running) {
1619 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1620 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1621 return (USBD_NORMAL_COMPLETION);
1623 usb_delay_ms(&sc->sc_bus, 1);
1626 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1627 run ? "start" : "stop");
1628 return (USBD_IOERROR);
1632 * Memory management routines.
1633 * uhci_alloc_std allocates TDs
1634 * uhci_alloc_sqh allocates QHs
1635 * These two routines do their own free list management,
1636 * partly for speed, partly because allocating DMAable memory
1637 * has page size granularaity so much memory would be wasted if
1638 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1642 uhci_alloc_std(uhci_softc_t *sc)
1644 uhci_soft_td_t *std;
1649 if (sc->sc_freetds == NULL) {
1650 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1651 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1652 UHCI_TD_ALIGN, &dma);
1655 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1656 offs = i * UHCI_STD_SIZE;
1657 std = KERNADDR(&dma, offs);
1658 std->physaddr = DMAADDR(&dma, offs);
1659 std->link.std = sc->sc_freetds;
1660 sc->sc_freetds = std;
1663 std = sc->sc_freetds;
1664 sc->sc_freetds = std->link.std;
1665 memset(&std->td, 0, sizeof(uhci_td_t));
1670 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1673 #define TD_IS_FREE 0x12345678
1674 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1675 printf("uhci_free_std: freeing free TD %p\n", std);
1678 std->td.td_token = htole32(TD_IS_FREE);
1680 std->link.std = sc->sc_freetds;
1681 sc->sc_freetds = std;
1685 uhci_alloc_sqh(uhci_softc_t *sc)
1687 uhci_soft_qh_t *sqh;
1692 if (sc->sc_freeqhs == NULL) {
1693 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1694 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1695 UHCI_QH_ALIGN, &dma);
1698 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1699 offs = i * UHCI_SQH_SIZE;
1700 sqh = KERNADDR(&dma, offs);
1701 sqh->physaddr = DMAADDR(&dma, offs);
1702 sqh->hlink = sc->sc_freeqhs;
1703 sc->sc_freeqhs = sqh;
1706 sqh = sc->sc_freeqhs;
1707 sc->sc_freeqhs = sqh->hlink;
1708 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1713 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1715 sqh->hlink = sc->sc_freeqhs;
1716 sc->sc_freeqhs = sqh;
1720 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1721 uhci_soft_td_t *stdend)
1725 for (; std != stdend; std = p) {
1727 uhci_free_std(sc, std);
1732 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1733 int rd, u_int16_t flags, usb_dma_t *dma,
1734 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1736 uhci_soft_td_t *p, *lastp;
1737 uhci_physaddr_t lastlink;
1738 int i, ntd, l, tog, maxp;
1740 int addr = upipe->pipe.device->address;
1741 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1743 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1744 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1745 upipe->pipe.device->speed, flags));
1746 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1748 printf("uhci_alloc_std_chain: maxp=0\n");
1749 return (USBD_INVAL);
1751 ntd = (len + maxp - 1) / maxp;
1752 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1754 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1757 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1758 return (USBD_NORMAL_COMPLETION);
1760 tog = upipe->nexttoggle;
1763 upipe->nexttoggle = tog ^ 1;
1765 lastlink = UHCI_PTR_T;
1767 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1768 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1769 status |= UHCI_TD_LS;
1770 if (flags & USBD_SHORT_XFER_OK)
1771 status |= UHCI_TD_SPD;
1772 for (i = ntd; i >= 0; i--) {
1773 p = uhci_alloc_std(sc);
1775 uhci_free_std_chain(sc, lastp, NULL);
1776 return (USBD_NOMEM);
1778 p->link.std = lastp;
1779 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1781 lastlink = p->physaddr;
1782 p->td.td_status = htole32(status);
1786 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1792 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1793 UHCI_TD_OUT(l, endpt, addr, tog));
1794 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1798 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1799 upipe->nexttoggle));
1800 return (USBD_NORMAL_COMPLETION);
1804 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1806 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1807 upipe->nexttoggle = 0;
1811 uhci_noop(usbd_pipe_handle pipe)
1816 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1820 /* Insert last in queue. */
1821 err = usb_insert_transfer(xfer);
1826 * Pipe isn't running (otherwise err would be USBD_INPROG),
1827 * so start it first.
1829 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1833 uhci_device_bulk_start(usbd_xfer_handle xfer)
1835 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1836 usbd_device_handle dev = upipe->pipe.device;
1837 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1838 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1839 uhci_soft_td_t *data, *dataend;
1840 uhci_soft_qh_t *sqh;
1842 int len, isread, endpt;
1845 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1846 xfer, xfer->length, xfer->flags, ii));
1849 return (USBD_IOERROR);
1852 if (xfer->rqflags & URQ_REQUEST)
1853 panic("uhci_device_bulk_transfer: a request");
1857 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1858 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1859 sqh = upipe->u.bulk.sqh;
1861 upipe->u.bulk.isread = isread;
1862 upipe->u.bulk.length = len;
1864 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1865 &xfer->dmabuf, &data, &dataend);
1868 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1871 if (uhcidebug > 8) {
1872 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1873 uhci_dump_tds(data);
1877 /* Set up interrupt info. */
1879 ii->stdstart = data;
1880 ii->stdend = dataend;
1883 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1889 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1892 uhci_add_bulk(sc, sqh);
1893 uhci_add_intr_info(sc, ii);
1895 if (xfer->timeout && !sc->sc_bus.use_polling) {
1896 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1899 xfer->status = USBD_IN_PROGRESS;
1903 if (uhcidebug > 10) {
1904 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1905 uhci_dump_tds(data);
1909 if (sc->sc_bus.use_polling)
1910 uhci_waitintr(sc, xfer);
1912 return (USBD_IN_PROGRESS);
1915 /* Abort a device bulk request. */
1917 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1919 DPRINTF(("uhci_device_bulk_abort:\n"));
1920 uhci_abort_xfer(xfer, USBD_CANCELLED);
1924 * Abort a device request.
1925 * If this routine is called at splusb() it guarantees that the request
1926 * will be removed from the hardware scheduling and that the callback
1927 * for it will be called with USBD_CANCELLED status.
1928 * It's impossible to guarantee that the requested transfer will not
1929 * have happened since the hardware runs concurrently.
1930 * If the transaction has already happened we rely on the ordinary
1931 * interrupt processing to process it.
1934 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1936 struct uhci_xfer *uxfer = UXFER(xfer);
1937 uhci_intr_info_t *ii = &uxfer->iinfo;
1938 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1939 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1940 uhci_soft_td_t *std;
1943 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1946 /* If we're dying, just do the software part. */
1948 xfer->status = status; /* make software ignore it */
1949 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1950 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1951 usb_transfer_complete(xfer);
1956 if (xfer->device->bus->intr_context || !curproc)
1957 panic("uhci_abort_xfer: not in process context");
1960 * If an abort is already in progress then just wait for it to
1961 * complete and return.
1963 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING) {
1964 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n"));
1965 /* No need to wait if we're aborting from a timeout. */
1966 if (status == USBD_TIMEOUT)
1968 /* Override the status which might be USBD_TIMEOUT. */
1969 xfer->status = status;
1970 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n"));
1971 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTWAIT;
1972 while (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING)
1973 tsleep(&uxfer->uhci_xfer_flags, PZERO, "uhciaw", 0);
1978 * Step 1: Make interrupt routine and hardware ignore xfer.
1981 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTING;
1982 xfer->status = status; /* make software ignore it */
1983 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1984 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1985 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1986 for (std = ii->stdstart; std != NULL; std = std->link.std)
1987 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1991 * Step 2: Wait until we know hardware has finished any possible
1992 * use of the xfer. Also make sure the soft interrupt routine
1995 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1997 #ifdef USB_USE_SOFTINTR
1998 sc->sc_softwake = 1;
1999 #endif /* USB_USE_SOFTINTR */
2000 usb_schedsoftintr(&sc->sc_bus);
2001 #ifdef USB_USE_SOFTINTR
2002 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
2003 tsleep(&sc->sc_softwake, PZERO, "uhciab", 0);
2004 #endif /* USB_USE_SOFTINTR */
2008 * Step 3: Execute callback.
2010 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
2015 /* Do the wakeup first to avoid touching the xfer after the callback. */
2016 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTING;
2017 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTWAIT) {
2018 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTWAIT;
2019 wakeup(&uxfer->uhci_xfer_flags);
2021 usb_transfer_complete(xfer);
2025 /* Close a device bulk pipe. */
2027 uhci_device_bulk_close(usbd_pipe_handle pipe)
2029 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2030 usbd_device_handle dev = upipe->pipe.device;
2031 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2033 uhci_free_sqh(sc, upipe->u.bulk.sqh);
2034 pipe->endpoint->savedtoggle = upipe->nexttoggle;
2038 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
2042 /* Insert last in queue. */
2043 err = usb_insert_transfer(xfer);
2048 * Pipe isn't running (otherwise err would be USBD_INPROG),
2049 * so start it first.
2051 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2055 uhci_device_ctrl_start(usbd_xfer_handle xfer)
2057 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2061 return (USBD_IOERROR);
2064 if (!(xfer->rqflags & URQ_REQUEST))
2065 panic("uhci_device_ctrl_transfer: not a request");
2068 err = uhci_device_request(xfer);
2072 if (sc->sc_bus.use_polling)
2073 uhci_waitintr(sc, xfer);
2074 return (USBD_IN_PROGRESS);
2078 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2082 /* Insert last in queue. */
2083 err = usb_insert_transfer(xfer);
2088 * Pipe isn't running (otherwise err would be USBD_INPROG),
2089 * so start it first.
2091 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2095 uhci_device_intr_start(usbd_xfer_handle xfer)
2097 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2098 usbd_device_handle dev = upipe->pipe.device;
2099 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2100 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2101 uhci_soft_td_t *data, *dataend;
2102 uhci_soft_qh_t *sqh;
2108 return (USBD_IOERROR);
2110 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2111 xfer, xfer->length, xfer->flags));
2114 if (xfer->rqflags & URQ_REQUEST)
2115 panic("uhci_device_intr_transfer: a request");
2118 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2119 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2120 sqh = upipe->u.bulk.sqh;
2122 upipe->u.intr.isread = isread;
2124 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread,
2125 xfer->flags, &xfer->dmabuf, &data,
2129 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2132 if (uhcidebug > 10) {
2133 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2134 uhci_dump_tds(data);
2135 uhci_dump_qh(upipe->u.intr.qhs[0]);
2140 /* Set up interrupt info. */
2142 ii->stdstart = data;
2143 ii->stdend = dataend;
2146 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2151 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2152 upipe->u.intr.qhs[0]));
2153 for (i = 0; i < upipe->u.intr.npoll; i++) {
2154 sqh = upipe->u.intr.qhs[i];
2156 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2158 uhci_add_intr_info(sc, ii);
2159 xfer->status = USBD_IN_PROGRESS;
2163 if (uhcidebug > 10) {
2164 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2165 uhci_dump_tds(data);
2166 uhci_dump_qh(upipe->u.intr.qhs[0]);
2170 return (USBD_IN_PROGRESS);
2173 /* Abort a device control request. */
2175 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2177 DPRINTF(("uhci_device_ctrl_abort:\n"));
2178 uhci_abort_xfer(xfer, USBD_CANCELLED);
2181 /* Close a device control pipe. */
2183 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2187 /* Abort a device interrupt request. */
2189 uhci_device_intr_abort(usbd_xfer_handle xfer)
2191 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2192 if (xfer->pipe->intrxfer == xfer) {
2193 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2194 xfer->pipe->intrxfer = NULL;
2196 uhci_abort_xfer(xfer, USBD_CANCELLED);
2199 /* Close a device interrupt pipe. */
2201 uhci_device_intr_close(usbd_pipe_handle pipe)
2203 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2204 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2208 /* Unlink descriptors from controller data structures. */
2209 npoll = upipe->u.intr.npoll;
2211 for (i = 0; i < npoll; i++)
2212 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2216 * We now have to wait for any activity on the physical
2217 * descriptors to stop.
2219 usb_delay_ms(&sc->sc_bus, 2);
2221 for(i = 0; i < npoll; i++)
2222 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2223 free(upipe->u.intr.qhs, M_USBHC);
2225 /* XXX free other resources */
2229 uhci_device_request(usbd_xfer_handle xfer)
2231 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2232 usb_device_request_t *req = &xfer->request;
2233 usbd_device_handle dev = upipe->pipe.device;
2234 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2235 int addr = dev->address;
2236 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2237 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2238 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2239 uhci_soft_qh_t *sqh;
2246 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2247 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2248 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2249 UGETW(req->wIndex), UGETW(req->wLength),
2252 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2253 isread = req->bmRequestType & UT_READ;
2254 len = UGETW(req->wLength);
2256 setup = upipe->u.ctl.setup;
2257 stat = upipe->u.ctl.stat;
2258 sqh = upipe->u.ctl.sqh;
2260 /* Set up data transaction */
2262 upipe->nexttoggle = 1;
2263 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2264 &xfer->dmabuf, &data, &dataend);
2268 dataend->link.std = stat;
2269 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2273 upipe->u.ctl.length = len;
2275 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2277 setup->link.std = next;
2278 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2279 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2281 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2282 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2284 stat->link.std = NULL;
2285 stat->td.td_link = htole32(UHCI_PTR_T);
2286 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2287 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2289 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2290 UHCI_TD_IN (0, endpt, addr, 1));
2291 stat->td.td_buffer = htole32(0);
2294 if (uhcidebug > 10) {
2295 DPRINTF(("uhci_device_request: before transfer\n"));
2296 uhci_dump_tds(setup);
2300 /* Set up interrupt info. */
2302 ii->stdstart = setup;
2306 printf("uhci_device_request: not done, ii=%p\n", ii);
2312 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2315 if (dev->speed == USB_SPEED_LOW)
2316 uhci_add_ls_ctrl(sc, sqh);
2318 uhci_add_hs_ctrl(sc, sqh);
2319 uhci_add_intr_info(sc, ii);
2321 if (uhcidebug > 12) {
2322 uhci_soft_td_t *std;
2323 uhci_soft_qh_t *xqh;
2324 uhci_soft_qh_t *sxqh;
2326 uhci_physaddr_t link;
2327 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2328 for (std = sc->sc_vframes[0].htd, link = 0;
2329 (link & UHCI_PTR_QH) == 0;
2330 std = std->link.std) {
2331 link = le32toh(std->td.td_link);
2334 sxqh = (uhci_soft_qh_t *)std;
2338 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2339 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2342 DPRINTF(("Enqueued QH:\n"));
2344 uhci_dump_tds(sqh->elink);
2347 if (xfer->timeout && !sc->sc_bus.use_polling) {
2348 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2351 xfer->status = USBD_IN_PROGRESS;
2354 return (USBD_NORMAL_COMPLETION);
2358 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2362 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2364 /* Put it on our queue, */
2365 err = usb_insert_transfer(xfer);
2367 /* bail out on error, */
2368 if (err && err != USBD_IN_PROGRESS)
2371 /* XXX should check inuse here */
2373 /* insert into schedule, */
2374 uhci_device_isoc_enter(xfer);
2376 /* and start if the pipe wasn't running */
2378 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2384 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2386 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2387 usbd_device_handle dev = upipe->pipe.device;
2388 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2389 struct iso *iso = &upipe->u.iso;
2390 uhci_soft_td_t *std;
2391 u_int32_t buf, len, status;
2392 int s, i, next, nframes;
2394 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2396 iso->inuse, iso->next, xfer, xfer->nframes));
2401 if (xfer->status == USBD_IN_PROGRESS) {
2402 /* This request has already been entered into the frame list */
2403 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2408 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2409 printf("uhci_device_isoc_enter: overflow!\n");
2414 /* Not in use yet, schedule it a few frames ahead. */
2415 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2416 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2419 xfer->status = USBD_IN_PROGRESS;
2420 UXFER(xfer)->curframe = next;
2422 buf = DMAADDR(&xfer->dmabuf, 0);
2423 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2426 nframes = xfer->nframes;
2428 for (i = 0; i < nframes; i++) {
2429 std = iso->stds[next];
2430 if (++next >= UHCI_VFRAMELIST_COUNT)
2432 len = xfer->frlengths[i];
2433 std->td.td_buffer = htole32(buf);
2434 if (i == nframes - 1)
2435 status |= UHCI_TD_IOC;
2436 std->td.td_status = htole32(status);
2437 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2438 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2440 if (uhcidebug > 5) {
2441 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2448 iso->inuse += xfer->nframes;
2454 uhci_device_isoc_start(usbd_xfer_handle xfer)
2456 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2457 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2458 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2459 uhci_soft_td_t *end;
2462 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2465 return (USBD_IOERROR);
2468 if (xfer->status != USBD_IN_PROGRESS)
2469 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2472 /* Find the last TD */
2473 i = UXFER(xfer)->curframe + xfer->nframes;
2474 if (i >= UHCI_VFRAMELIST_COUNT)
2475 i -= UHCI_VFRAMELIST_COUNT;
2476 end = upipe->u.iso.stds[i];
2480 printf("uhci_device_isoc_start: end == NULL\n");
2481 return (USBD_INVAL);
2487 /* Set up interrupt info. */
2493 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2496 uhci_add_intr_info(sc, ii);
2500 return (USBD_IN_PROGRESS);
2504 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2506 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2507 uhci_soft_td_t **stds = upipe->u.iso.stds;
2508 uhci_soft_td_t *std;
2509 int i, n, s, nframes, maxlen, len;
2513 /* Transfer is already done. */
2514 if (xfer->status != USBD_NOT_STARTED &&
2515 xfer->status != USBD_IN_PROGRESS) {
2520 /* Give xfer the requested abort code. */
2521 xfer->status = USBD_CANCELLED;
2523 /* make hardware ignore it, */
2524 nframes = xfer->nframes;
2525 n = UXFER(xfer)->curframe;
2527 for (i = 0; i < nframes; i++) {
2529 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2530 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2533 if (++n >= UHCI_VFRAMELIST_COUNT)
2537 /* and wait until we are sure the hardware has finished. */
2541 UXFER(xfer)->iinfo.isdone = 1;
2543 /* Run callback and remove from interrupt list. */
2544 usb_transfer_complete(xfer);
2550 uhci_device_isoc_close(usbd_pipe_handle pipe)
2552 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2553 usbd_device_handle dev = upipe->pipe.device;
2554 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2555 uhci_soft_td_t *std, *vstd;
2560 * Make sure all TDs are marked as inactive.
2561 * Wait for completion.
2565 iso = &upipe->u.iso;
2567 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2568 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2569 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2572 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2574 for (vstd = sc->sc_vframes[i].htd;
2575 vstd != NULL && vstd->link.std != std;
2576 vstd = vstd->link.std)
2580 printf("uhci_device_isoc_close: %p not found\n", std);
2584 vstd->link = std->link;
2585 vstd->td.td_link = std->td.td_link;
2586 uhci_free_std(sc, std);
2590 free(iso->stds, M_USBHC);
2594 uhci_setup_isoc(usbd_pipe_handle pipe)
2596 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2597 usbd_device_handle dev = upipe->pipe.device;
2598 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2599 int addr = upipe->pipe.device->address;
2600 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2601 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2602 uhci_soft_td_t *std, *vstd;
2607 iso = &upipe->u.iso;
2608 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2611 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2612 UHCI_TD_OUT(0, endpt, addr, 0);
2614 /* Allocate the TDs and mark as inactive; */
2615 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2616 std = uhci_alloc_std(sc);
2619 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2620 std->td.td_token = htole32(token);
2624 /* Insert TDs into schedule. */
2626 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2628 vstd = sc->sc_vframes[i].htd;
2629 std->link = vstd->link;
2630 std->td.td_link = vstd->td.td_link;
2631 vstd->link.std = std;
2632 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2639 return (USBD_NORMAL_COMPLETION);
2643 uhci_free_std(sc, iso->stds[i]);
2644 free(iso->stds, M_USBHC);
2645 return (USBD_NOMEM);
2649 uhci_device_isoc_done(usbd_xfer_handle xfer)
2651 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2653 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2655 if (ii->xfer != xfer)
2656 /* Not on interrupt list, ignore it. */
2659 if (!uhci_active_intr_info(ii))
2663 if (xfer->busy_free != XFER_BUSY) {
2664 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2665 xfer, xfer->busy_free);
2669 if (ii->stdend == NULL) {
2670 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2678 /* Turn off the interrupt since it is active even if the TD is not. */
2679 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2681 uhci_del_intr_info(ii); /* remove from active list */
2684 if (ii->stdend == NULL) {
2685 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2695 uhci_device_intr_done(usbd_xfer_handle xfer)
2697 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2698 uhci_softc_t *sc = ii->sc;
2699 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2700 uhci_soft_qh_t *sqh;
2703 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2705 npoll = upipe->u.intr.npoll;
2706 for(i = 0; i < npoll; i++) {
2707 sqh = upipe->u.intr.qhs[i];
2709 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2711 uhci_free_std_chain(sc, ii->stdstart, NULL);
2714 if (xfer->pipe->repeat) {
2715 uhci_soft_td_t *data, *dataend;
2717 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2719 /* This alloc cannot fail since we freed the chain above. */
2720 uhci_alloc_std_chain(upipe, sc, xfer->length,
2721 upipe->u.intr.isread, xfer->flags,
2722 &xfer->dmabuf, &data, &dataend);
2723 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2726 if (uhcidebug > 10) {
2727 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2728 uhci_dump_tds(data);
2729 uhci_dump_qh(upipe->u.intr.qhs[0]);
2733 ii->stdstart = data;
2734 ii->stdend = dataend;
2737 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2741 for (i = 0; i < npoll; i++) {
2742 sqh = upipe->u.intr.qhs[i];
2744 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2746 xfer->status = USBD_IN_PROGRESS;
2747 /* The ii is already on the examined list, just leave it. */
2749 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2750 if (uhci_active_intr_info(ii))
2751 uhci_del_intr_info(ii);
2755 /* Deallocate request data structures */
2757 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2759 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2760 uhci_softc_t *sc = ii->sc;
2761 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2764 if (!(xfer->rqflags & URQ_REQUEST))
2765 panic("uhci_device_ctrl_done: not a request");
2768 if (!uhci_active_intr_info(ii))
2771 uhci_del_intr_info(ii); /* remove from active list */
2773 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2774 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2776 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2778 if (upipe->u.ctl.length != 0)
2779 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2781 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2784 /* Deallocate request data structures */
2786 uhci_device_bulk_done(usbd_xfer_handle xfer)
2788 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2789 uhci_softc_t *sc = ii->sc;
2790 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2792 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2793 xfer, ii, sc, upipe));
2795 if (!uhci_active_intr_info(ii))
2798 uhci_del_intr_info(ii); /* remove from active list */
2800 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2802 uhci_free_std_chain(sc, ii->stdstart, NULL);
2804 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2807 /* Add interrupt QH, called with vflock. */
2809 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2811 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2812 uhci_soft_qh_t *eqh;
2814 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2817 sqh->hlink = eqh->hlink;
2818 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2820 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2825 /* Remove interrupt QH. */
2827 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2829 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2830 uhci_soft_qh_t *pqh;
2832 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2834 /* See comment in uhci_remove_ctrl() */
2835 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2836 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2837 delay(UHCI_QH_REMOVE_DELAY);
2840 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2841 pqh->hlink = sqh->hlink;
2842 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2843 delay(UHCI_QH_REMOVE_DELAY);
2850 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2852 uhci_soft_qh_t *sqh;
2854 u_int bestbw, bw, bestoffs, offs;
2856 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2858 printf("uhci_setintr: 0 interval\n");
2859 return (USBD_INVAL);
2862 if (ival > UHCI_VFRAMELIST_COUNT)
2863 ival = UHCI_VFRAMELIST_COUNT;
2864 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2865 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2867 upipe->u.intr.npoll = npoll;
2869 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2872 * Figure out which offset in the schedule that has most
2873 * bandwidth left over.
2875 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2876 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2877 for (bw = i = 0; i < npoll; i++)
2878 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2884 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2886 for(i = 0; i < npoll; i++) {
2887 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2889 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2890 sqh->pos = MOD(i * ival + bestoffs);
2895 /* Enter QHs into the controller data structures. */
2896 for(i = 0; i < npoll; i++)
2897 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2900 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2901 return (USBD_NORMAL_COMPLETION);
2904 /* Open a new pipe. */
2906 uhci_open(usbd_pipe_handle pipe)
2908 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2909 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2910 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2914 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2915 pipe, pipe->device->address,
2916 ed->bEndpointAddress, sc->sc_addr));
2918 upipe->aborting = 0;
2919 upipe->nexttoggle = pipe->endpoint->savedtoggle;
2921 if (pipe->device->address == sc->sc_addr) {
2922 switch (ed->bEndpointAddress) {
2923 case USB_CONTROL_ENDPOINT:
2924 pipe->methods = &uhci_root_ctrl_methods;
2926 case UE_DIR_IN | UHCI_INTR_ENDPT:
2927 pipe->methods = &uhci_root_intr_methods;
2930 return (USBD_INVAL);
2933 switch (ed->bmAttributes & UE_XFERTYPE) {
2935 pipe->methods = &uhci_device_ctrl_methods;
2936 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2937 if (upipe->u.ctl.sqh == NULL)
2939 upipe->u.ctl.setup = uhci_alloc_std(sc);
2940 if (upipe->u.ctl.setup == NULL) {
2941 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2944 upipe->u.ctl.stat = uhci_alloc_std(sc);
2945 if (upipe->u.ctl.stat == NULL) {
2946 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2947 uhci_free_std(sc, upipe->u.ctl.setup);
2950 err = usb_allocmem(&sc->sc_bus,
2951 sizeof(usb_device_request_t),
2952 0, &upipe->u.ctl.reqdma);
2954 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2955 uhci_free_std(sc, upipe->u.ctl.setup);
2956 uhci_free_std(sc, upipe->u.ctl.stat);
2961 pipe->methods = &uhci_device_intr_methods;
2962 ival = pipe->interval;
2963 if (ival == USBD_DEFAULT_INTERVAL)
2964 ival = ed->bInterval;
2965 return (uhci_device_setintr(sc, upipe, ival));
2966 case UE_ISOCHRONOUS:
2967 pipe->methods = &uhci_device_isoc_methods;
2968 return (uhci_setup_isoc(pipe));
2970 pipe->methods = &uhci_device_bulk_methods;
2971 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2972 if (upipe->u.bulk.sqh == NULL)
2977 return (USBD_NORMAL_COMPLETION);
2980 return (USBD_NOMEM);
2984 * Data structures and routines to emulate the root hub.
2986 usb_device_descriptor_t uhci_devd = {
2987 USB_DEVICE_DESCRIPTOR_SIZE,
2988 UDESC_DEVICE, /* type */
2989 {0x00, 0x01}, /* USB version */
2990 UDCLASS_HUB, /* class */
2991 UDSUBCLASS_HUB, /* subclass */
2992 UDPROTO_FSHUB, /* protocol */
2993 64, /* max packet */
2994 {0},{0},{0x00,0x01}, /* device id */
2995 1,2,0, /* string indicies */
2996 1 /* # of configurations */
2999 usb_config_descriptor_t uhci_confd = {
3000 USB_CONFIG_DESCRIPTOR_SIZE,
3002 {USB_CONFIG_DESCRIPTOR_SIZE +
3003 USB_INTERFACE_DESCRIPTOR_SIZE +
3004 USB_ENDPOINT_DESCRIPTOR_SIZE},
3012 usb_interface_descriptor_t uhci_ifcd = {
3013 USB_INTERFACE_DESCRIPTOR_SIZE,
3024 usb_endpoint_descriptor_t uhci_endpd = {
3025 USB_ENDPOINT_DESCRIPTOR_SIZE,
3027 UE_DIR_IN | UHCI_INTR_ENDPT,
3033 usb_hub_descriptor_t uhci_hubd_piix = {
3034 USB_HUB_DESCRIPTOR_SIZE,
3037 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
3038 50, /* power on to power good */
3040 { 0x00 }, /* both ports are removable */
3044 uhci_str(usb_string_descriptor_t *p, int l, char *s)
3050 p->bLength = 2 * strlen(s) + 2;
3053 p->bDescriptorType = UDESC_STRING;
3055 for (i = 0; s[i] && l > 1; i++, l -= 2)
3056 USETW2(p->bString[i], 0, s[i]);
3061 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3062 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3063 * should not be used by the USB subsystem. As we cannot issue a
3064 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3065 * will be enabled as part of the reset.
3067 * On the VT83C572, the port cannot be successfully enabled until the
3068 * outstanding "port enable change" and "connection status change"
3069 * events have been reset.
3072 uhci_portreset(uhci_softc_t *sc, int index)
3077 port = UHCI_PORTSC1;
3078 else if (index == 2)
3079 port = UHCI_PORTSC2;
3081 return (USBD_IOERROR);
3083 x = URWMASK(UREAD2(sc, port));
3084 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3086 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3088 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3089 index, UREAD2(sc, port)));
3091 x = URWMASK(UREAD2(sc, port));
3092 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3096 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3097 index, UREAD2(sc, port)));
3099 x = URWMASK(UREAD2(sc, port));
3100 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3102 for (lim = 10; --lim > 0;) {
3103 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3105 x = UREAD2(sc, port);
3107 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3110 if (!(x & UHCI_PORTSC_CCS)) {
3112 * No device is connected (or was disconnected
3113 * during reset). Consider the port reset.
3114 * The delay must be long enough to ensure on
3115 * the initial iteration that the device
3116 * connection will have been registered. 50ms
3117 * appears to be sufficient, but 20ms is not.
3119 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3124 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3126 * Port enabled changed and/or connection
3127 * status changed were set. Reset either or
3128 * both raised flags (by writing a 1 to that
3129 * bit), and wait again for state to settle.
3131 UWRITE2(sc, port, URWMASK(x) |
3132 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3136 if (x & UHCI_PORTSC_PE)
3137 /* Port is enabled */
3140 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3143 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3144 index, UREAD2(sc, port)));
3147 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3148 return (USBD_TIMEOUT);
3152 return (USBD_NORMAL_COMPLETION);
3156 * Simulate a hardware hub by handling all the necessary requests.
3159 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3163 /* Insert last in queue. */
3164 err = usb_insert_transfer(xfer);
3169 * Pipe isn't running (otherwise err would be USBD_INPROG),
3170 * so start it first.
3172 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3176 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3178 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3179 usb_device_request_t *req;
3182 int s, len, value, index, status, change, l, totlen = 0;
3183 usb_port_status_t ps;
3187 return (USBD_IOERROR);
3190 if (!(xfer->rqflags & URQ_REQUEST))
3191 panic("uhci_root_ctrl_transfer: not a request");
3193 req = &xfer->request;
3195 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3196 req->bmRequestType, req->bRequest));
3198 len = UGETW(req->wLength);
3199 value = UGETW(req->wValue);
3200 index = UGETW(req->wIndex);
3203 buf = KERNADDR(&xfer->dmabuf, 0);
3205 #define C(x,y) ((x) | ((y) << 8))
3206 switch(C(req->bRequest, req->bmRequestType)) {
3207 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3208 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3209 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3211 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3212 * for the integrated root hub.
3215 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3217 *(u_int8_t *)buf = sc->sc_conf;
3221 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3222 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3223 switch(value >> 8) {
3225 if ((value & 0xff) != 0) {
3229 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3230 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3231 memcpy(buf, &uhci_devd, l);
3234 if ((value & 0xff) != 0) {
3238 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3239 memcpy(buf, &uhci_confd, l);
3240 buf = (char *)buf + l;
3242 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3244 memcpy(buf, &uhci_ifcd, l);
3245 buf = (char *)buf + l;
3247 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3249 memcpy(buf, &uhci_endpd, l);
3254 *(u_int8_t *)buf = 0;
3256 switch (value & 0xff) {
3257 case 1: /* Vendor */
3258 totlen = uhci_str(buf, len, sc->sc_vendor);
3260 case 2: /* Product */
3261 totlen = uhci_str(buf, len, "UHCI root hub");
3270 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3272 *(u_int8_t *)buf = 0;
3276 case C(UR_GET_STATUS, UT_READ_DEVICE):
3278 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3282 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3283 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3285 USETW(((usb_status_t *)buf)->wStatus, 0);
3289 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3290 if (value >= USB_MAX_DEVICES) {
3294 sc->sc_addr = value;
3296 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3297 if (value != 0 && value != 1) {
3301 sc->sc_conf = value;
3303 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3305 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3306 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3307 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3310 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3312 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3315 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3317 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3318 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3319 "port=%d feature=%d\n",
3322 port = UHCI_PORTSC1;
3323 else if (index == 2)
3324 port = UHCI_PORTSC2;
3330 case UHF_PORT_ENABLE:
3331 x = URWMASK(UREAD2(sc, port));
3332 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3334 case UHF_PORT_SUSPEND:
3335 x = URWMASK(UREAD2(sc, port));
3336 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3338 case UHF_PORT_RESET:
3339 x = URWMASK(UREAD2(sc, port));
3340 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3342 case UHF_C_PORT_CONNECTION:
3343 x = URWMASK(UREAD2(sc, port));
3344 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3346 case UHF_C_PORT_ENABLE:
3347 x = URWMASK(UREAD2(sc, port));
3348 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3350 case UHF_C_PORT_OVER_CURRENT:
3351 x = URWMASK(UREAD2(sc, port));
3352 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3354 case UHF_C_PORT_RESET:
3356 err = USBD_NORMAL_COMPLETION;
3358 case UHF_PORT_CONNECTION:
3359 case UHF_PORT_OVER_CURRENT:
3360 case UHF_PORT_POWER:
3361 case UHF_PORT_LOW_SPEED:
3362 case UHF_C_PORT_SUSPEND:
3368 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3370 port = UHCI_PORTSC1;
3371 else if (index == 2)
3372 port = UHCI_PORTSC2;
3379 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3380 UHCI_PORTSC_LS_SHIFT;
3384 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3385 if ((value & 0xff) != 0) {
3389 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3391 memcpy(buf, &uhci_hubd_piix, l);
3393 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3398 memset(buf, 0, len);
3401 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3403 port = UHCI_PORTSC1;
3404 else if (index == 2)
3405 port = UHCI_PORTSC2;
3414 x = UREAD2(sc, port);
3415 status = change = 0;
3416 if (x & UHCI_PORTSC_CCS)
3417 status |= UPS_CURRENT_CONNECT_STATUS;
3418 if (x & UHCI_PORTSC_CSC)
3419 change |= UPS_C_CONNECT_STATUS;
3420 if (x & UHCI_PORTSC_PE)
3421 status |= UPS_PORT_ENABLED;
3422 if (x & UHCI_PORTSC_POEDC)
3423 change |= UPS_C_PORT_ENABLED;
3424 if (x & UHCI_PORTSC_OCI)
3425 status |= UPS_OVERCURRENT_INDICATOR;
3426 if (x & UHCI_PORTSC_OCIC)
3427 change |= UPS_C_OVERCURRENT_INDICATOR;
3428 if (x & UHCI_PORTSC_SUSP)
3429 status |= UPS_SUSPEND;
3430 if (x & UHCI_PORTSC_LSDA)
3431 status |= UPS_LOW_SPEED;
3432 status |= UPS_PORT_POWER;
3434 change |= UPS_C_PORT_RESET;
3435 USETW(ps.wPortStatus, status);
3436 USETW(ps.wPortChange, change);
3437 l = min(len, sizeof ps);
3438 memcpy(buf, &ps, l);
3441 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3444 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3446 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3448 port = UHCI_PORTSC1;
3449 else if (index == 2)
3450 port = UHCI_PORTSC2;
3456 case UHF_PORT_ENABLE:
3457 x = URWMASK(UREAD2(sc, port));
3458 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3460 case UHF_PORT_SUSPEND:
3461 x = URWMASK(UREAD2(sc, port));
3462 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3464 case UHF_PORT_RESET:
3465 err = uhci_portreset(sc, index);
3467 case UHF_PORT_POWER:
3468 /* Pretend we turned on power */
3469 err = USBD_NORMAL_COMPLETION;
3471 case UHF_C_PORT_CONNECTION:
3472 case UHF_C_PORT_ENABLE:
3473 case UHF_C_PORT_OVER_CURRENT:
3474 case UHF_PORT_CONNECTION:
3475 case UHF_PORT_OVER_CURRENT:
3476 case UHF_PORT_LOW_SPEED:
3477 case UHF_C_PORT_SUSPEND:
3478 case UHF_C_PORT_RESET:
3488 xfer->actlen = totlen;
3489 err = USBD_NORMAL_COMPLETION;
3493 usb_transfer_complete(xfer);
3495 return (USBD_IN_PROGRESS);
3498 /* Abort a root control request. */
3500 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3502 /* Nothing to do, all transfers are synchronous. */
3505 /* Close the root pipe. */
3507 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3509 DPRINTF(("uhci_root_ctrl_close\n"));
3512 /* Abort a root interrupt request. */
3514 uhci_root_intr_abort(usbd_xfer_handle xfer)
3516 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3518 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3519 sc->sc_intr_xfer = NULL;
3521 if (xfer->pipe->intrxfer == xfer) {
3522 DPRINTF(("uhci_root_intr_abort: remove\n"));
3523 xfer->pipe->intrxfer = 0;
3525 xfer->status = USBD_CANCELLED;
3527 UXFER(xfer)->iinfo.isdone = 1;
3529 usb_transfer_complete(xfer);
3533 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3537 /* Insert last in queue. */
3538 err = usb_insert_transfer(xfer);
3543 * Pipe isn't running (otherwise err would be USBD_INPROG),
3544 * so start it first.
3546 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3549 /* Start a transfer on the root interrupt pipe */
3551 uhci_root_intr_start(usbd_xfer_handle xfer)
3553 usbd_pipe_handle pipe = xfer->pipe;
3554 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3556 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3557 xfer, xfer->length, xfer->flags));
3560 return (USBD_IOERROR);
3562 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3563 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3564 sc->sc_intr_xfer = xfer;
3565 return (USBD_IN_PROGRESS);
3568 /* Close the root interrupt pipe. */
3570 uhci_root_intr_close(usbd_pipe_handle pipe)
3572 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3574 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3575 sc->sc_intr_xfer = NULL;
3576 DPRINTF(("uhci_root_intr_close\n"));