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 #include <sys/endian.h>
69 #include <sys/module.h>
71 #if defined(DIAGNOSTIC) && defined(__i386__)
72 #include <machine/cpu.h>
75 #include <sys/queue.h>
76 #include <sys/sysctl.h>
78 #include <machine/bus.h>
79 #include <machine/endian.h>
81 #include <dev/usb/usb.h>
82 #include <dev/usb/usbdi.h>
83 #include <dev/usb/usbdivar.h>
84 #include <dev/usb/usb_mem.h>
85 #include <dev/usb/usb_quirks.h>
87 #include <dev/usb/uhcireg.h>
88 #include <dev/usb/uhcivar.h>
90 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
91 /*#define UHCI_CTL_LOOP */
93 #define delay(d) DELAY(d)
95 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
99 #define DPRINTF(x) if (uhcidebug) printf x
100 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
103 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
104 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
105 &uhcidebug, 0, "uhci debug level");
106 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
107 &uhcinoloop, 0, "uhci noloop");
108 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
111 #define DPRINTFN(n,x)
115 struct usbd_pipe pipe;
119 usbd_xfer_handle abortstart, abortend;
121 /* Info needed for different pipe kinds. */
127 uhci_soft_td_t *setup, *stat;
134 uhci_soft_qh_t **qhs;
144 uhci_soft_td_t **stds;
150 static void uhci_globalreset(uhci_softc_t *);
151 static usbd_status uhci_portreset(uhci_softc_t*, int);
152 static void uhci_reset(uhci_softc_t *);
153 #if defined(__NetBSD__) || defined(__OpenBSD__)
154 static void uhci_shutdown(void *v);
155 static void uhci_power(int, void *);
157 static usbd_status uhci_run(uhci_softc_t *, int run);
158 static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
159 static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
160 static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
161 static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
162 static usbd_status uhci_aux_dma_alloc(uhci_softc_t *, uhci_soft_td_t *,
163 void *data, int len);
164 static uhci_physaddr_t uhci_aux_dma_prepare(uhci_soft_td_t *, int);
165 static void uhci_aux_dma_complete(uhci_soft_td_t *, int);
167 static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
169 static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
172 static void uhci_free_std_chain(uhci_softc_t *,
173 uhci_soft_td_t *, uhci_soft_td_t *);
174 static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
175 uhci_softc_t *, int, int, u_int16_t,
176 usbd_xfer_handle xfer,
177 uhci_soft_td_t **, uhci_soft_td_t **);
178 static void uhci_poll_hub(void *);
179 static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
180 static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
181 static void uhci_idone(uhci_intr_info_t *);
183 static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
184 static void uhci_transfer_complete(usbd_xfer_handle xfer);
186 static void uhci_timeout(void *);
187 static void uhci_timeout_task(void *);
188 static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
189 static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
190 static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
191 static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
192 static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
193 static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
194 static int uhci_str(usb_string_descriptor_t *, int, char *);
195 static void uhci_add_loop(uhci_softc_t *sc);
196 static void uhci_rem_loop(uhci_softc_t *sc);
198 static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
199 static void uhci_device_isoc_enter(usbd_xfer_handle);
201 static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
202 static void uhci_freem(struct usbd_bus *, usb_dma_t *);
204 static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
205 static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
207 static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
208 static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
209 static void uhci_device_ctrl_abort(usbd_xfer_handle);
210 static void uhci_device_ctrl_close(usbd_pipe_handle);
211 static void uhci_device_ctrl_done(usbd_xfer_handle);
213 static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
214 static usbd_status uhci_device_intr_start(usbd_xfer_handle);
215 static void uhci_device_intr_abort(usbd_xfer_handle);
216 static void uhci_device_intr_close(usbd_pipe_handle);
217 static void uhci_device_intr_done(usbd_xfer_handle);
219 static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
220 static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
221 static void uhci_device_bulk_abort(usbd_xfer_handle);
222 static void uhci_device_bulk_close(usbd_pipe_handle);
223 static void uhci_device_bulk_done(usbd_xfer_handle);
225 static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
226 static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
227 static void uhci_device_isoc_abort(usbd_xfer_handle);
228 static void uhci_device_isoc_close(usbd_pipe_handle);
229 static void uhci_device_isoc_done(usbd_xfer_handle);
231 static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
232 static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
233 static void uhci_root_ctrl_abort(usbd_xfer_handle);
234 static void uhci_root_ctrl_close(usbd_pipe_handle);
235 static void uhci_root_ctrl_done(usbd_xfer_handle);
237 static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
238 static usbd_status uhci_root_intr_start(usbd_xfer_handle);
239 static void uhci_root_intr_abort(usbd_xfer_handle);
240 static void uhci_root_intr_close(usbd_pipe_handle);
241 static void uhci_root_intr_done(usbd_xfer_handle);
243 static usbd_status uhci_open(usbd_pipe_handle);
244 static void uhci_poll(struct usbd_bus *);
245 static void uhci_softintr(void *);
247 static usbd_status uhci_device_request(usbd_xfer_handle xfer);
249 static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
250 static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
251 static usbd_status uhci_device_setintr(uhci_softc_t *sc,
252 struct uhci_pipe *pipe, int ival);
254 static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
255 static void uhci_noop(usbd_pipe_handle pipe);
257 static __inline uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
261 static void uhci_dump_all(uhci_softc_t *);
262 static void uhci_dumpregs(uhci_softc_t *);
263 static void uhci_dump_qhs(uhci_soft_qh_t *);
264 static void uhci_dump_qh(uhci_soft_qh_t *);
265 static void uhci_dump_tds(uhci_soft_td_t *);
266 static void uhci_dump_td(uhci_soft_td_t *);
267 static void uhci_dump_ii(uhci_intr_info_t *ii);
268 void uhci_dump(void);
271 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
272 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
273 #define UWRITE1(sc, r, x) \
274 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
275 } while (/*CONSTCOND*/0)
276 #define UWRITE2(sc, r, x) \
277 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
278 } while (/*CONSTCOND*/0)
279 #define UWRITE4(sc, r, x) \
280 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
281 } while (/*CONSTCOND*/0)
282 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
283 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
284 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
286 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
287 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
289 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
291 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
293 #define UHCI_INTR_ENDPT 1
295 struct usbd_bus_methods uhci_bus_methods = {
305 struct usbd_pipe_methods uhci_root_ctrl_methods = {
306 uhci_root_ctrl_transfer,
307 uhci_root_ctrl_start,
308 uhci_root_ctrl_abort,
309 uhci_root_ctrl_close,
314 struct usbd_pipe_methods uhci_root_intr_methods = {
315 uhci_root_intr_transfer,
316 uhci_root_intr_start,
317 uhci_root_intr_abort,
318 uhci_root_intr_close,
323 struct usbd_pipe_methods uhci_device_ctrl_methods = {
324 uhci_device_ctrl_transfer,
325 uhci_device_ctrl_start,
326 uhci_device_ctrl_abort,
327 uhci_device_ctrl_close,
329 uhci_device_ctrl_done,
332 struct usbd_pipe_methods uhci_device_intr_methods = {
333 uhci_device_intr_transfer,
334 uhci_device_intr_start,
335 uhci_device_intr_abort,
336 uhci_device_intr_close,
337 uhci_device_clear_toggle,
338 uhci_device_intr_done,
341 struct usbd_pipe_methods uhci_device_bulk_methods = {
342 uhci_device_bulk_transfer,
343 uhci_device_bulk_start,
344 uhci_device_bulk_abort,
345 uhci_device_bulk_close,
346 uhci_device_clear_toggle,
347 uhci_device_bulk_done,
350 struct usbd_pipe_methods uhci_device_isoc_methods = {
351 uhci_device_isoc_transfer,
352 uhci_device_isoc_start,
353 uhci_device_isoc_abort,
354 uhci_device_isoc_close,
356 uhci_device_isoc_done,
359 #define uhci_add_intr_info(sc, ii) \
360 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
361 #define uhci_del_intr_info(ii) \
363 LIST_REMOVE((ii), list); \
364 (ii)->list.le_prev = NULL; \
366 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
368 static __inline uhci_soft_qh_t *
369 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
371 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
373 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
374 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
375 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
376 printf("uhci_find_prev_qh: QH not found\n");
385 uhci_globalreset(uhci_softc_t *sc)
387 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
388 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
389 UHCICMD(sc, 0); /* do nothing */
393 uhci_init(uhci_softc_t *sc)
397 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
400 DPRINTFN(1,("uhci_init: start\n"));
409 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
410 uhci_globalreset(sc); /* reset the controller */
413 /* Allocate and initialize real frame array. */
414 err = usb_allocmem(&sc->sc_bus,
415 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
416 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
419 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
420 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
421 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
424 * Allocate a TD, inactive, that hangs from the last QH.
425 * This is to avoid a bug in the PIIX that makes it run berserk
428 std = uhci_alloc_std(sc);
431 std->link.std = NULL;
432 std->td.td_link = htole32(UHCI_PTR_T);
433 std->td.td_status = htole32(0); /* inactive */
434 std->td.td_token = htole32(0);
435 std->td.td_buffer = htole32(0);
437 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
438 lsqh = uhci_alloc_sqh(sc);
442 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
444 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
445 sc->sc_last_qh = lsqh;
447 /* Allocate the dummy QH where bulk traffic will be queued. */
448 bsqh = uhci_alloc_sqh(sc);
452 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
454 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
455 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
457 /* Allocate dummy QH where high speed control traffic will be queued. */
458 chsqh = uhci_alloc_sqh(sc);
462 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
464 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
465 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
467 /* Allocate dummy QH where control traffic will be queued. */
468 clsqh = uhci_alloc_sqh(sc);
471 clsqh->hlink = chsqh;
472 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
474 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
475 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
478 * Make all (virtual) frame list pointers point to the interrupt
479 * queue heads and the interrupt queue heads at the control
480 * queue head and point the physical frame list to the virtual.
482 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
483 std = uhci_alloc_std(sc);
484 sqh = uhci_alloc_sqh(sc);
485 if (std == NULL || sqh == NULL)
488 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
489 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
490 std->td.td_token = htole32(0);
491 std->td.td_buffer = htole32(0);
493 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
495 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
496 sc->sc_vframes[i].htd = std;
497 sc->sc_vframes[i].etd = std;
498 sc->sc_vframes[i].hqh = sqh;
499 sc->sc_vframes[i].eqh = sqh;
501 j < UHCI_FRAMELIST_COUNT;
502 j += UHCI_VFRAMELIST_COUNT)
503 sc->sc_pframes[j] = htole32(std->physaddr);
506 LIST_INIT(&sc->sc_intrhead);
508 STAILQ_INIT(&sc->sc_free_xfers);
510 callout_init(&sc->sc_poll_handle, 0);
512 /* Set up the bus struct. */
513 sc->sc_bus.methods = &uhci_bus_methods;
514 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
516 #if defined(__NetBSD__) || defined(__OpenBSD__)
517 sc->sc_suspend = PWR_RESUME;
518 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
519 sc->sc_shutdownhook = shutdownhook_establish(uhci_shutdown, sc);
522 DPRINTFN(1,("uhci_init: enabling\n"));
523 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
524 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
526 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
528 return (uhci_run(sc, 1)); /* and here we go... */
532 uhci_detach(struct uhci_softc *sc, int flags)
534 usbd_xfer_handle xfer;
539 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
542 #if defined(__NetBSD__) || defined(__OpenBSD__)
543 powerhook_disestablish(sc->sc_powerhook);
544 shutdownhook_disestablish(sc->sc_shutdownhook);
547 /* Free all xfers associated with this HC. */
549 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
552 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
556 /* XXX free other data structures XXX */
557 usb_freemem(&sc->sc_bus, &sc->sc_dma);
563 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
565 return (usb_allocmem(bus, size, 0, dma));
569 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
571 usb_freemem(bus, dma);
575 uhci_allocx(struct usbd_bus *bus)
577 struct uhci_softc *sc = (struct uhci_softc *)bus;
578 usbd_xfer_handle xfer;
580 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
582 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
584 if (xfer->busy_free != XFER_FREE) {
585 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
590 xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_NOWAIT);
593 memset(xfer, 0, sizeof (struct uhci_xfer));
594 UXFER(xfer)->iinfo.sc = sc;
595 usb_init_task(&UXFER(xfer)->abort_task, uhci_timeout_task,
597 UXFER(xfer)->uhci_xfer_flags = 0;
599 UXFER(xfer)->iinfo.isdone = 1;
600 xfer->busy_free = XFER_BUSY;
607 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
609 struct uhci_softc *sc = (struct uhci_softc *)bus;
612 if (xfer->busy_free != XFER_BUSY) {
613 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
617 xfer->busy_free = XFER_FREE;
618 if (!UXFER(xfer)->iinfo.isdone) {
619 printf("uhci_freex: !isdone\n");
623 STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
627 * Shut down the controller when the system is going down.
630 uhci_shutdown(void *v)
632 uhci_softc_t *sc = v;
634 DPRINTF(("uhci_shutdown: stopping the HC\n"));
635 uhci_run(sc, 0); /* stop the controller */
639 * Handle suspend/resume.
641 * We need to switch to polling mode here, because this routine is
642 * called from an interrupt context. This is all right since we
643 * are almost suspended anyway.
646 uhci_power(int why, void *v)
648 uhci_softc_t *sc = v;
653 cmd = UREAD2(sc, UHCI_CMD);
655 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
656 sc, why, sc->sc_suspend, cmd));
658 if (why != PWR_RESUME) {
663 if (sc->sc_intr_xfer != NULL)
664 callout_stop(&sc->sc_poll_handle);
665 sc->sc_bus.use_polling++;
666 uhci_run(sc, 0); /* stop the controller */
669 /* save some state if BIOS doesn't */
670 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
671 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
673 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
675 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
676 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
677 sc->sc_suspend = why;
678 sc->sc_bus.use_polling--;
679 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
682 if (sc->sc_suspend == PWR_RESUME)
683 printf("uhci_power: weird, resume without suspend.\n");
685 sc->sc_bus.use_polling++;
686 sc->sc_suspend = why;
687 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
688 uhci_globalreset(sc); /* reset the controller */
690 if (cmd & UHCI_CMD_RS)
691 uhci_run(sc, 0); /* in case BIOS has started it */
693 uhci_globalreset(sc);
696 /* restore saved state */
697 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
698 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
699 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
701 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
702 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
703 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
704 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
705 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
706 UHCICMD(sc, UHCI_CMD_MAXP);
707 uhci_run(sc, 1); /* and start traffic again */
708 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
709 sc->sc_bus.use_polling--;
710 if (sc->sc_intr_xfer != NULL)
711 callout_reset(&sc->sc_poll_handle, sc->sc_ival,
712 uhci_poll_hub, sc->sc_intr_xfer);
723 uhci_dumpregs(uhci_softc_t *sc)
725 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
726 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
727 device_get_nameunit(sc->sc_bus.bdev),
728 UREAD2(sc, UHCI_CMD),
729 UREAD2(sc, UHCI_STS),
730 UREAD2(sc, UHCI_INTR),
731 UREAD2(sc, UHCI_FRNUM),
732 UREAD4(sc, UHCI_FLBASEADDR),
733 UREAD1(sc, UHCI_SOF),
734 UREAD2(sc, UHCI_PORTSC1),
735 UREAD2(sc, UHCI_PORTSC2)));
739 uhci_dump_td(uhci_soft_td_t *p)
741 char sbuf[128], sbuf2[128];
743 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
744 "token=0x%08lx buffer=0x%08lx\n",
745 p, (long)p->physaddr,
746 (long)le32toh(p->td.td_link),
747 (long)le32toh(p->td.td_status),
748 (long)le32toh(p->td.td_token),
749 (long)le32toh(p->td.td_buffer)));
751 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
753 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
754 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
755 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
756 sbuf2, sizeof(sbuf2));
758 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
759 "D=%d,maxlen=%d\n", sbuf, sbuf2,
760 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
761 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
762 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
763 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
764 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
765 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
766 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
770 uhci_dump_qh(uhci_soft_qh_t *sqh)
772 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
773 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
774 le32toh(sqh->qh.qh_elink)));
782 uhci_dump_all(thesc);
787 uhci_dump_all(uhci_softc_t *sc)
790 printf("intrs=%d\n", sc->sc_bus.no_intrs);
791 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
792 uhci_dump_qh(sc->sc_lctl_start);
797 uhci_dump_qhs(uhci_soft_qh_t *sqh)
801 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
802 * Traverses sideways first, then down.
812 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
816 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
817 uhci_dump_qhs(sqh->hlink);
819 DPRINTF(("No QH\n"));
821 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
822 uhci_dump_tds(sqh->elink);
824 DPRINTF(("No TD\n"));
828 uhci_dump_tds(uhci_soft_td_t *std)
832 for(td = std; td != NULL; td = td->link.std) {
835 /* Check whether the link pointer in this TD marks
836 * the link pointer as end of queue. This avoids
837 * printing the free list in case the queue/TD has
838 * already been moved there (seatbelt).
840 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
841 le32toh(td->td.td_link) == 0)
847 uhci_dump_ii(uhci_intr_info_t *ii)
849 usbd_pipe_handle pipe;
850 usb_endpoint_descriptor_t *ed;
851 usbd_device_handle dev;
854 #define DONE ii->isdone
862 if (ii->xfer == NULL) {
863 printf("ii %p: done=%d xfer=NULL\n",
867 pipe = ii->xfer->pipe;
869 printf("ii %p: done=%d xfer=%p pipe=NULL\n",
873 if (pipe->endpoint == NULL) {
874 printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
875 ii, DONE, ii->xfer, pipe);
878 if (pipe->device == NULL) {
879 printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
880 ii, DONE, ii->xfer, pipe);
883 ed = pipe->endpoint->edesc;
885 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",
886 ii, DONE, ii->xfer, dev,
887 UGETW(dev->ddesc.idVendor),
888 UGETW(dev->ddesc.idProduct),
890 ed->bEndpointAddress, ed->bmAttributes);
894 void uhci_dump_iis(struct uhci_softc *sc);
896 uhci_dump_iis(struct uhci_softc *sc)
898 uhci_intr_info_t *ii;
900 printf("intr_info list:\n");
901 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
906 void iidump(void) { uhci_dump_iis(thesc); }
911 * This routine is executed periodically and simulates interrupts
912 * from the root controller interrupt pipe for port status change.
915 uhci_poll_hub(void *addr)
917 usbd_xfer_handle xfer = addr;
918 usbd_pipe_handle pipe = xfer->pipe;
919 usbd_device_handle dev = pipe->device;
920 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
924 DPRINTFN(20, ("uhci_poll_hub\n"));
926 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
930 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
932 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
935 /* No change, try again in a while */
939 xfer->status = USBD_NORMAL_COMPLETION;
941 dev->bus->intr_context++;
942 uhci_transfer_complete(xfer);
943 dev->bus->intr_context--;
948 uhci_root_intr_done(usbd_xfer_handle xfer)
953 uhci_root_ctrl_done(usbd_xfer_handle xfer)
958 * Let the last QH loop back to the high speed control transfer QH.
959 * This is what intel calls "bandwidth reclamation" and improves
960 * USB performance a lot for some devices.
961 * If we are already looping, just count it.
964 uhci_add_loop(uhci_softc_t *sc) {
969 if (++sc->sc_loops == 1) {
970 DPRINTFN(5,("uhci_start_loop: add\n"));
971 /* Note, we don't loop back the soft pointer. */
972 sc->sc_last_qh->qh.qh_hlink =
973 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
978 uhci_rem_loop(uhci_softc_t *sc) {
983 if (--sc->sc_loops == 0) {
984 DPRINTFN(5,("uhci_end_loop: remove\n"));
985 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
989 /* Add high speed control QH, called at splusb(). */
991 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
997 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
998 eqh = sc->sc_hctl_end;
999 sqh->hlink = eqh->hlink;
1000 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1002 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1003 sc->sc_hctl_end = sqh;
1004 #ifdef UHCI_CTL_LOOP
1009 /* Remove high speed control QH, called at splusb(). */
1011 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1013 uhci_soft_qh_t *pqh;
1017 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1018 #ifdef UHCI_CTL_LOOP
1022 * The T bit should be set in the elink of the QH so that the HC
1023 * doesn't follow the pointer. This condition may fail if the
1024 * the transferred packet was short so that the QH still points
1025 * at the last used TD.
1026 * In this case we set the T bit and wait a little for the HC
1027 * to stop looking at the TD.
1029 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1030 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1031 delay(UHCI_QH_REMOVE_DELAY);
1034 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1035 pqh->hlink = sqh->hlink;
1036 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1037 delay(UHCI_QH_REMOVE_DELAY);
1038 if (sc->sc_hctl_end == sqh)
1039 sc->sc_hctl_end = pqh;
1042 /* Add low speed control QH, called at splusb(). */
1044 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1046 uhci_soft_qh_t *eqh;
1050 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1051 eqh = sc->sc_lctl_end;
1052 sqh->hlink = eqh->hlink;
1053 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1055 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1056 sc->sc_lctl_end = sqh;
1059 /* Remove low speed control QH, called at splusb(). */
1061 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1063 uhci_soft_qh_t *pqh;
1067 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1068 /* See comment in uhci_remove_hs_ctrl() */
1069 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1070 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1071 delay(UHCI_QH_REMOVE_DELAY);
1073 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1074 pqh->hlink = sqh->hlink;
1075 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1076 delay(UHCI_QH_REMOVE_DELAY);
1077 if (sc->sc_lctl_end == sqh)
1078 sc->sc_lctl_end = pqh;
1081 /* Add bulk QH, called at splusb(). */
1083 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1085 uhci_soft_qh_t *eqh;
1089 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1090 eqh = sc->sc_bulk_end;
1091 sqh->hlink = eqh->hlink;
1092 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1094 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1095 sc->sc_bulk_end = sqh;
1099 /* Remove bulk QH, called at splusb(). */
1101 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1103 uhci_soft_qh_t *pqh;
1107 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1109 /* See comment in uhci_remove_hs_ctrl() */
1110 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1111 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1112 delay(UHCI_QH_REMOVE_DELAY);
1114 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1115 pqh->hlink = sqh->hlink;
1116 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1117 delay(UHCI_QH_REMOVE_DELAY);
1118 if (sc->sc_bulk_end == sqh)
1119 sc->sc_bulk_end = pqh;
1122 static int uhci_intr1(uhci_softc_t *);
1125 uhci_intr(void *arg)
1127 uhci_softc_t *sc = arg;
1132 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1133 if (sc->sc_bus.use_polling) {
1135 printf("uhci_intr: ignored interrupt while polling\n");
1139 return (uhci_intr1(sc));
1143 uhci_intr1(uhci_softc_t *sc)
1150 * It can happen that an interrupt will be delivered to
1151 * us before the device has been fully attached and the
1152 * softc struct has been configured. Usually this happens
1153 * when kldloading the USB support as a module after the
1154 * system has been booted. If we detect this condition,
1155 * we need to squelch the unwanted interrupts until we're
1158 if (sc->sc_bus.bdev == NULL) {
1159 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1160 uhci_run(sc, 0); /* stop the controller */
1161 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1166 if (uhcidebug > 15) {
1167 DPRINTF(("%s: uhci_intr1\n", device_get_nameunit(sc->sc_bus.bdev)));
1171 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1172 if (status == 0) /* The interrupt was not for us. */
1175 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1176 if (sc->sc_suspend != PWR_RESUME)
1177 printf("uhci_intr: suspended sts=0x%x\n", status);
1180 if (sc->sc_suspend != PWR_RESUME) {
1181 printf("%s: interrupt while not operating ignored\n",
1182 device_get_nameunit(sc->sc_bus.bdev));
1183 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1188 if (status & UHCI_STS_USBINT)
1189 ack |= UHCI_STS_USBINT;
1190 if (status & UHCI_STS_USBEI)
1191 ack |= UHCI_STS_USBEI;
1192 if (status & UHCI_STS_RD) {
1195 printf("%s: resume detect\n", device_get_nameunit(sc->sc_bus.bdev));
1198 if (status & UHCI_STS_HSE) {
1199 ack |= UHCI_STS_HSE;
1200 printf("%s: host system error\n", device_get_nameunit(sc->sc_bus.bdev));
1202 if (status & UHCI_STS_HCPE) {
1203 ack |= UHCI_STS_HCPE;
1204 printf("%s: host controller process error\n",
1205 device_get_nameunit(sc->sc_bus.bdev));
1207 if (status & UHCI_STS_HCH) {
1208 /* no acknowledge needed */
1209 if (!sc->sc_dying) {
1210 printf("%s: host controller halted\n",
1211 device_get_nameunit(sc->sc_bus.bdev));
1220 return (0); /* nothing to acknowledge */
1221 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1223 sc->sc_bus.no_intrs++;
1224 usb_schedsoftintr(&sc->sc_bus);
1226 DPRINTFN(15, ("%s: uhci_intr: exit\n", device_get_nameunit(sc->sc_bus.bdev)));
1232 uhci_softintr(void *v)
1234 uhci_softc_t *sc = v;
1235 uhci_intr_info_t *ii, *nextii;
1237 DPRINTFN(10,("%s: uhci_softintr (%d)\n", device_get_nameunit(sc->sc_bus.bdev),
1238 sc->sc_bus.intr_context));
1240 sc->sc_bus.intr_context++;
1243 * Interrupts on UHCI really suck. When the host controller
1244 * interrupts because a transfer is completed there is no
1245 * way of knowing which transfer it was. You can scan down
1246 * the TDs and QHs of the previous frame to limit the search,
1247 * but that assumes that the interrupt was not delayed by more
1248 * than 1 ms, which may not always be true (e.g. after debug
1249 * output on a slow console).
1250 * We scan all interrupt descriptors to see if any have
1253 LIST_FOREACH_SAFE(ii, &sc->sc_intrhead, list, nextii)
1254 uhci_check_intr(sc, ii);
1256 #ifdef USB_USE_SOFTINTR
1257 if (sc->sc_softwake) {
1258 sc->sc_softwake = 0;
1259 wakeup(&sc->sc_softwake);
1261 #endif /* USB_USE_SOFTINTR */
1263 sc->sc_bus.intr_context--;
1266 /* Check for an interrupt. */
1268 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1270 uhci_soft_td_t *std, *lstd;
1273 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1276 printf("uhci_check_intr: no ii? %p\n", ii);
1280 if (ii->xfer->status == USBD_CANCELLED ||
1281 ii->xfer->status == USBD_TIMEOUT) {
1282 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1286 if (ii->stdstart == NULL)
1291 printf("uhci_check_intr: std==0\n");
1296 * If the last TD is still active we need to check whether there
1297 * is an error somewhere in the middle, or whether there was a
1298 * short packet (SPD and not ACTIVE).
1300 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1301 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1302 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1303 status = le32toh(std->td.td_status);
1304 /* If there's an active TD the xfer isn't done. */
1305 if (status & UHCI_TD_ACTIVE)
1307 /* Any kind of error makes the xfer done. */
1308 if (status & UHCI_TD_STALLED)
1310 /* We want short packets, and it is short: it's done */
1311 if ((status & UHCI_TD_SPD) &&
1312 UHCI_TD_GET_ACTLEN(status) <
1313 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)))
1316 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1321 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1322 callout_stop(&ii->xfer->timeout_handle);
1323 usb_rem_task(ii->xfer->pipe->device, &UXFER(ii->xfer)->abort_task);
1327 /* Called at splusb() */
1329 uhci_idone(uhci_intr_info_t *ii)
1331 usbd_xfer_handle xfer = ii->xfer;
1332 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1333 uhci_soft_td_t *std;
1334 u_int32_t status = 0, nstatus;
1337 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1344 printf("uhci_idone: ii is done!\n ");
1347 printf("uhci_idone: ii=%p is done!\n", ii);
1356 if (xfer->nframes != 0) {
1357 /* Isoc transfer, do things differently. */
1358 uhci_soft_td_t **stds = upipe->u.iso.stds;
1359 int i, n, nframes, len;
1361 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1363 nframes = xfer->nframes;
1365 n = UXFER(xfer)->curframe;
1366 for (i = 0; i < nframes; i++) {
1369 if (uhcidebug > 5) {
1370 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1374 if (++n >= UHCI_VFRAMELIST_COUNT)
1376 status = le32toh(std->td.td_status);
1377 len = UHCI_TD_GET_ACTLEN(status);
1378 xfer->frlengths[i] = len;
1381 upipe->u.iso.inuse -= nframes;
1382 xfer->actlen = actlen;
1383 xfer->status = USBD_NORMAL_COMPLETION;
1388 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1391 uhci_dump_tds(ii->stdstart);
1394 /* The transfer is done, compute actual length and status. */
1396 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1397 nstatus = le32toh(std->td.td_status);
1398 if (nstatus & UHCI_TD_ACTIVE)
1402 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1404 actlen += UHCI_TD_GET_ACTLEN(status);
1407 * UHCI will report CRCTO in addition to a STALL or NAK
1408 * for a SETUP transaction. See section 3.2.2, "TD
1409 * CONTROL AND STATUS".
1411 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1412 status &= ~UHCI_TD_CRCTO;
1415 /* If there are left over TDs we need to update the toggle. */
1417 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1419 status &= UHCI_TD_ERROR;
1420 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1422 xfer->actlen = actlen;
1427 bitmask_snprintf((u_int32_t)status,
1428 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1429 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1430 sbuf, sizeof(sbuf));
1432 DPRINTFN((status == UHCI_TD_STALLED)*10,
1433 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1435 xfer->pipe->device->address,
1436 xfer->pipe->endpoint->edesc->bEndpointAddress,
1440 if (status == UHCI_TD_STALLED)
1441 xfer->status = USBD_STALLED;
1443 xfer->status = USBD_IOERROR; /* more info XXX */
1445 xfer->status = USBD_NORMAL_COMPLETION;
1449 uhci_transfer_complete(xfer);
1450 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1454 * Called when a request does not complete.
1457 uhci_timeout(void *addr)
1459 uhci_intr_info_t *ii = addr;
1460 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1461 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1462 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1464 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1467 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1471 /* Execute the abort in a process context. */
1472 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task,
1477 uhci_timeout_task(void *addr)
1479 usbd_xfer_handle xfer = addr;
1482 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1485 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1490 * Wait here until controller claims to have an interrupt.
1491 * Then call uhci_intr and return. Use timeout to avoid waiting
1493 * Only used during boot when interrupts are not enabled yet.
1496 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1498 int timo = xfer->timeout;
1499 uhci_intr_info_t *ii;
1501 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1503 xfer->status = USBD_IN_PROGRESS;
1504 for (; timo >= 0; timo--) {
1505 usb_delay_ms(&sc->sc_bus, 1);
1506 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1507 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1509 if (xfer->status != USBD_IN_PROGRESS)
1514 DPRINTF(("uhci_waitintr: timeout\n"));
1515 for (ii = LIST_FIRST(&sc->sc_intrhead);
1516 ii != NULL && ii->xfer != xfer;
1517 ii = LIST_NEXT(ii, list))
1521 panic("uhci_waitintr: lost intr_info");
1527 uhci_poll(struct usbd_bus *bus)
1529 uhci_softc_t *sc = (uhci_softc_t *)bus;
1531 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1536 uhci_reset(uhci_softc_t *sc)
1540 UHCICMD(sc, UHCI_CMD_HCRESET);
1541 /* The reset bit goes low when the controller is done. */
1542 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1543 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1544 usb_delay_ms(&sc->sc_bus, 1);
1545 if (n >= UHCI_RESET_TIMEOUT)
1546 printf("%s: controller did not reset\n",
1547 device_get_nameunit(sc->sc_bus.bdev));
1551 uhci_run(uhci_softc_t *sc, int run)
1558 DPRINTF(("uhci_run: setting run=%d\n", run));
1559 cmd = UREAD2(sc, UHCI_CMD);
1563 cmd &= ~UHCI_CMD_RS;
1565 for(n = 0; n < 10; n++) {
1566 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1567 /* return when we've entered the state we want */
1568 if (run == running) {
1570 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1571 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1572 return (USBD_NORMAL_COMPLETION);
1574 usb_delay_ms(&sc->sc_bus, 1);
1577 printf("%s: cannot %s\n", device_get_nameunit(sc->sc_bus.bdev),
1578 run ? "start" : "stop");
1579 return (USBD_IOERROR);
1583 * Memory management routines.
1584 * uhci_alloc_std allocates TDs
1585 * uhci_alloc_sqh allocates QHs
1586 * These two routines do their own free list management,
1587 * partly for speed, partly because allocating DMAable memory
1588 * has page size granularaity so much memory would be wasted if
1589 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1593 uhci_alloc_std(uhci_softc_t *sc)
1595 uhci_soft_td_t *std;
1600 if (sc->sc_freetds == NULL) {
1601 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1602 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1603 UHCI_TD_ALIGN, &dma);
1606 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1607 offs = i * UHCI_STD_SIZE;
1608 std = KERNADDR(&dma, offs);
1609 std->physaddr = DMAADDR(&dma, offs);
1610 std->link.std = sc->sc_freetds;
1611 std->aux_dma.block = NULL;
1612 std->aux_data = NULL;
1614 sc->sc_freetds = std;
1617 std = sc->sc_freetds;
1618 sc->sc_freetds = std->link.std;
1619 memset(&std->td, 0, sizeof(uhci_td_t));
1624 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1627 #define TD_IS_FREE 0x12345678
1628 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1629 printf("uhci_free_std: freeing free TD %p\n", std);
1632 std->td.td_token = htole32(TD_IS_FREE);
1634 if (std->aux_dma.block != NULL) {
1635 usb_freemem(&sc->sc_bus, &std->aux_dma);
1636 std->aux_dma.block = NULL;
1637 std->aux_data = NULL;
1640 std->link.std = sc->sc_freetds;
1641 sc->sc_freetds = std;
1645 uhci_alloc_sqh(uhci_softc_t *sc)
1647 uhci_soft_qh_t *sqh;
1652 if (sc->sc_freeqhs == NULL) {
1653 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1654 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1655 UHCI_QH_ALIGN, &dma);
1658 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1659 offs = i * UHCI_SQH_SIZE;
1660 sqh = KERNADDR(&dma, offs);
1661 sqh->physaddr = DMAADDR(&dma, offs);
1662 sqh->hlink = sc->sc_freeqhs;
1663 sc->sc_freeqhs = sqh;
1666 sqh = sc->sc_freeqhs;
1667 sc->sc_freeqhs = sqh->hlink;
1668 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1673 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1675 sqh->hlink = sc->sc_freeqhs;
1676 sc->sc_freeqhs = sqh;
1680 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1681 uhci_soft_td_t *stdend)
1685 for (; std != stdend; std = p) {
1687 uhci_free_std(sc, std);
1692 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1693 int rd, u_int16_t flags, usbd_xfer_handle xfer,
1694 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1696 struct usb_dma_mapping *dma = &xfer->dmamap;
1697 uhci_soft_td_t *p, *prevp, *startp;
1698 int err, i, ntd, l, tog, maxp, seg, segoff;
1700 int addr = upipe->pipe.device->address;
1701 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1703 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1704 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1705 upipe->pipe.device->speed, flags));
1706 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1708 printf("uhci_alloc_std_chain: maxp=0\n");
1709 return (USBD_INVAL);
1711 ntd = (len + maxp - 1) / maxp;
1713 flags |= USBD_FORCE_SHORT_XFER;
1714 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1716 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1717 KASSERT(ntd > 0, ("uhci_alloc_std_chain: ntd=0"));
1718 tog = upipe->nexttoggle;
1721 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1722 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1723 status |= UHCI_TD_LS;
1724 if (flags & USBD_SHORT_XFER_OK)
1725 status |= UHCI_TD_SPD;
1728 for (i = 0; i < ntd; i++) {
1729 p = uhci_alloc_std(sc);
1731 uhci_free_std_chain(sc, startp, NULL);
1732 return (USBD_NOMEM);
1735 if (prevp != NULL) {
1736 prevp->link.std = p;
1737 prevp->td.td_link = htole32(p->physaddr | UHCI_PTR_VF |
1742 p->td.td_status = htole32(status);
1746 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1752 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1753 UHCI_TD_OUT(l, endpt, addr, tog));
1755 KASSERT(seg < dma->nsegs || l == 0,
1756 ("uhci_alloc_std_chain: too few segments"));
1758 p->td.td_buffer = 0;
1759 } else if (l > dma->segs[seg].ds_len - segoff) {
1760 /* UHCI can't handle non-contiguous data. */
1761 err = uhci_aux_dma_alloc(sc, p, (char *)xfer->buffer +
1764 uhci_free_std_chain(sc, startp, NULL);
1767 p->td.td_buffer = htole32(uhci_aux_dma_prepare(p, rd));
1768 l -= dma->segs[seg].ds_len - segoff;
1770 KASSERT(seg < dma->nsegs,
1771 ("uhci_alloc_std_chain: too few segments 2"));
1774 p->td.td_buffer = htole32(dma->segs[seg].ds_addr +
1778 if (l > 0 && segoff >= dma->segs[seg].ds_len) {
1779 KASSERT(segoff == dma->segs[seg].ds_len,
1780 ("uhci_alloc_std_chain: overlap"));
1781 if (i * maxp + l != len) {
1789 prevp->td.td_link = htole32(UHCI_PTR_T | UHCI_PTR_VF | UHCI_PTR_TD);
1790 upipe->nexttoggle = tog;
1792 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1793 upipe->nexttoggle));
1794 return (USBD_NORMAL_COMPLETION);
1798 * Allocate a physically contiguous buffer to handle cases where UHCI
1799 * cannot handle a packet because it is not physically contiguous.
1800 * If the usb_dma_t was already allocated this just ensures it is
1801 * large enough for the specified size.
1804 uhci_aux_dma_alloc(uhci_softc_t *sc, uhci_soft_td_t *std, void *data, int len)
1808 if (std->aux_dma.block == NULL || std->aux_dma.block->size < len) {
1809 /* Align to avoid crossing a page boundary. */
1813 align = 1 << fls(len);
1815 if (std->aux_dma.block != NULL)
1816 usb_freemem(&sc->sc_bus, &std->aux_dma);
1817 std->aux_dma.block = NULL;
1818 err = usb_allocmem(&sc->sc_bus, len, align, &std->aux_dma);
1822 std->aux_data = data;
1825 return (USBD_NORMAL_COMPLETION);
1828 static uhci_physaddr_t
1829 uhci_aux_dma_prepare(uhci_soft_td_t *std, int isread)
1832 bcopy(std->aux_data, KERNADDR(&std->aux_dma, 0), std->aux_len);
1833 bus_dmamap_sync(std->aux_dma.block->tag,
1834 std->aux_dma.block->map, BUS_DMASYNC_PREWRITE);
1837 return (DMAADDR(&std->aux_dma, 0));
1841 uhci_aux_dma_complete(uhci_soft_td_t *std, int isread)
1844 bus_dmamap_sync(std->aux_dma.block->tag,
1845 std->aux_dma.block->map, BUS_DMASYNC_POSTREAD);
1846 bcopy(KERNADDR(&std->aux_dma, 0), std->aux_data, std->aux_len);
1851 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1853 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1854 upipe->nexttoggle = 0;
1858 uhci_noop(usbd_pipe_handle pipe)
1863 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1867 /* Insert last in queue. */
1868 err = usb_insert_transfer(xfer);
1873 * Pipe isn't running (otherwise err would be USBD_INPROG),
1874 * so start it first.
1876 return (uhci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
1880 uhci_device_bulk_start(usbd_xfer_handle xfer)
1882 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1883 usbd_device_handle dev = upipe->pipe.device;
1884 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1885 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1886 uhci_soft_td_t *data, *dataend;
1887 uhci_soft_qh_t *sqh;
1889 int len, isread, endpt;
1892 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1893 xfer, xfer->length, xfer->flags, ii));
1896 return (USBD_IOERROR);
1899 if (xfer->rqflags & URQ_REQUEST)
1900 panic("uhci_device_bulk_transfer: a request");
1904 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1905 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1906 sqh = upipe->u.bulk.sqh;
1908 upipe->u.bulk.isread = isread;
1909 upipe->u.bulk.length = len;
1911 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags, xfer,
1915 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1918 if (uhcidebug > 8) {
1919 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1920 uhci_dump_tds(data);
1924 /* Set up interrupt info. */
1926 ii->stdstart = data;
1927 ii->stdend = dataend;
1930 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1936 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1939 uhci_add_bulk(sc, sqh);
1940 uhci_add_intr_info(sc, ii);
1942 if (xfer->timeout && !sc->sc_bus.use_polling) {
1943 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1946 xfer->status = USBD_IN_PROGRESS;
1950 if (uhcidebug > 10) {
1951 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1952 uhci_dump_tds(data);
1956 if (sc->sc_bus.use_polling)
1957 uhci_waitintr(sc, xfer);
1959 return (USBD_IN_PROGRESS);
1962 /* Abort a device bulk request. */
1964 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1966 DPRINTF(("uhci_device_bulk_abort:\n"));
1967 uhci_abort_xfer(xfer, USBD_CANCELLED);
1971 * Abort a device request.
1972 * If this routine is called at splusb() it guarantees that the request
1973 * will be removed from the hardware scheduling and that the callback
1974 * for it will be called with USBD_CANCELLED status.
1975 * It's impossible to guarantee that the requested transfer will not
1976 * have happened since the hardware runs concurrently.
1977 * If the transaction has already happened we rely on the ordinary
1978 * interrupt processing to process it.
1981 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1983 struct uhci_xfer *uxfer = UXFER(xfer);
1984 uhci_intr_info_t *ii = &uxfer->iinfo;
1985 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1986 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1987 uhci_soft_td_t *std;
1990 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1993 /* If we're dying, just do the software part. */
1995 xfer->status = status; /* make software ignore it */
1996 callout_stop(&xfer->timeout_handle);
1997 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1998 uhci_transfer_complete(xfer);
2003 if (xfer->device->bus->intr_context || !curproc)
2004 panic("uhci_abort_xfer: not in process context");
2007 * If an abort is already in progress then just wait for it to
2008 * complete and return.
2010 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING) {
2011 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n"));
2012 /* No need to wait if we're aborting from a timeout. */
2013 if (status == USBD_TIMEOUT)
2015 /* Override the status which might be USBD_TIMEOUT. */
2016 xfer->status = status;
2017 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n"));
2018 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTWAIT;
2019 while (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING)
2020 tsleep(&uxfer->uhci_xfer_flags, PZERO, "uhciaw", 0);
2025 * Step 1: Make interrupt routine and hardware ignore xfer.
2028 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTING;
2029 xfer->status = status; /* make software ignore it */
2030 callout_stop(&xfer->timeout_handle);
2031 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
2032 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
2033 for (std = ii->stdstart; std != NULL; std = std->link.std)
2034 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2038 * Step 2: Wait until we know hardware has finished any possible
2039 * use of the xfer. Also make sure the soft interrupt routine
2042 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
2044 #ifdef USB_USE_SOFTINTR
2045 sc->sc_softwake = 1;
2046 #endif /* USB_USE_SOFTINTR */
2047 usb_schedsoftintr(&sc->sc_bus);
2048 #ifdef USB_USE_SOFTINTR
2049 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
2050 tsleep(&sc->sc_softwake, PZERO, "uhciab", 0);
2051 #endif /* USB_USE_SOFTINTR */
2055 * Step 3: Execute callback.
2057 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
2062 /* Do the wakeup first to avoid touching the xfer after the callback. */
2063 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTING;
2064 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTWAIT) {
2065 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTWAIT;
2066 wakeup(&uxfer->uhci_xfer_flags);
2068 uhci_transfer_complete(xfer);
2073 * Perform any UHCI-specific transfer completion operations, then
2074 * call usb_transfer_complete().
2077 uhci_transfer_complete(usbd_xfer_handle xfer)
2079 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2080 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2084 /* XXX, must be an easier way to detect reads... */
2085 isread = ((xfer->rqflags & URQ_REQUEST) &&
2086 (xfer->request.bmRequestType & UT_READ)) ||
2087 (xfer->pipe->endpoint->edesc->bEndpointAddress & UE_DIR_IN);
2089 /* Copy back from any auxillary buffers after a read operation. */
2090 if (xfer->nframes == 0) {
2091 for (p = ii->stdstart; p != NULL; p = p->link.std) {
2092 if (p->aux_data != NULL)
2093 uhci_aux_dma_complete(p, isread);
2096 if (xfer->nframes != 0) {
2097 /* Isoc transfer, do things differently. */
2098 n = UXFER(xfer)->curframe;
2099 for (i = 0; i < xfer->nframes; i++) {
2100 p = upipe->u.iso.stds[n];
2101 if (p->aux_data != NULL)
2102 uhci_aux_dma_complete(p, isread);
2103 if (++n >= UHCI_VFRAMELIST_COUNT)
2109 usb_transfer_complete(xfer);
2112 /* Close a device bulk pipe. */
2114 uhci_device_bulk_close(usbd_pipe_handle pipe)
2116 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2117 usbd_device_handle dev = upipe->pipe.device;
2118 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2120 uhci_free_sqh(sc, upipe->u.bulk.sqh);
2121 pipe->endpoint->savedtoggle = upipe->nexttoggle;
2125 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
2129 /* Insert last in queue. */
2130 err = usb_insert_transfer(xfer);
2135 * Pipe isn't running (otherwise err would be USBD_INPROG),
2136 * so start it first.
2138 return (uhci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2142 uhci_device_ctrl_start(usbd_xfer_handle xfer)
2144 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2148 return (USBD_IOERROR);
2151 if (!(xfer->rqflags & URQ_REQUEST))
2152 panic("uhci_device_ctrl_transfer: not a request");
2155 err = uhci_device_request(xfer);
2159 if (sc->sc_bus.use_polling)
2160 uhci_waitintr(sc, xfer);
2161 return (USBD_IN_PROGRESS);
2165 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2169 /* Insert last in queue. */
2170 err = usb_insert_transfer(xfer);
2175 * Pipe isn't running (otherwise err would be USBD_INPROG),
2176 * so start it first.
2178 return (uhci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
2182 uhci_device_intr_start(usbd_xfer_handle xfer)
2184 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2185 usbd_device_handle dev = upipe->pipe.device;
2186 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2187 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2188 uhci_soft_td_t *data, *dataend;
2189 uhci_soft_qh_t *sqh;
2195 return (USBD_IOERROR);
2197 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2198 xfer, xfer->length, xfer->flags));
2201 if (xfer->rqflags & URQ_REQUEST)
2202 panic("uhci_device_intr_transfer: a request");
2205 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2206 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2207 sqh = upipe->u.bulk.sqh;
2209 upipe->u.intr.isread = isread;
2211 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread, xfer->flags,
2212 xfer, &data, &dataend);
2215 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2218 if (uhcidebug > 10) {
2219 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2220 uhci_dump_tds(data);
2221 uhci_dump_qh(upipe->u.intr.qhs[0]);
2226 /* Set up interrupt info. */
2228 ii->stdstart = data;
2229 ii->stdend = dataend;
2232 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2237 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2238 upipe->u.intr.qhs[0]));
2239 for (i = 0; i < upipe->u.intr.npoll; i++) {
2240 sqh = upipe->u.intr.qhs[i];
2242 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2244 uhci_add_intr_info(sc, ii);
2245 xfer->status = USBD_IN_PROGRESS;
2249 if (uhcidebug > 10) {
2250 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2251 uhci_dump_tds(data);
2252 uhci_dump_qh(upipe->u.intr.qhs[0]);
2256 return (USBD_IN_PROGRESS);
2259 /* Abort a device control request. */
2261 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2263 DPRINTF(("uhci_device_ctrl_abort:\n"));
2264 uhci_abort_xfer(xfer, USBD_CANCELLED);
2267 /* Close a device control pipe. */
2269 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2273 /* Abort a device interrupt request. */
2275 uhci_device_intr_abort(usbd_xfer_handle xfer)
2277 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2278 if (xfer->pipe->intrxfer == xfer) {
2279 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2280 xfer->pipe->intrxfer = NULL;
2282 uhci_abort_xfer(xfer, USBD_CANCELLED);
2285 /* Close a device interrupt pipe. */
2287 uhci_device_intr_close(usbd_pipe_handle pipe)
2289 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2290 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2294 /* Unlink descriptors from controller data structures. */
2295 npoll = upipe->u.intr.npoll;
2297 for (i = 0; i < npoll; i++)
2298 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2302 * We now have to wait for any activity on the physical
2303 * descriptors to stop.
2305 usb_delay_ms(&sc->sc_bus, 2);
2307 for(i = 0; i < npoll; i++)
2308 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2309 free(upipe->u.intr.qhs, M_USBHC);
2311 /* XXX free other resources */
2315 uhci_device_request(usbd_xfer_handle xfer)
2317 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2318 usb_device_request_t *req = &xfer->request;
2319 usbd_device_handle dev = upipe->pipe.device;
2320 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2321 int addr = dev->address;
2322 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2323 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2324 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2325 uhci_soft_qh_t *sqh;
2332 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2333 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2334 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2335 UGETW(req->wIndex), UGETW(req->wLength),
2338 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2339 isread = req->bmRequestType & UT_READ;
2340 len = UGETW(req->wLength);
2342 setup = upipe->u.ctl.setup;
2343 stat = upipe->u.ctl.stat;
2344 sqh = upipe->u.ctl.sqh;
2346 /* Set up data transaction */
2348 upipe->nexttoggle = 1;
2349 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2350 xfer, &data, &dataend);
2354 dataend->link.std = stat;
2355 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2359 upipe->u.ctl.length = len;
2361 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2363 setup->link.std = next;
2364 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2365 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2367 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2368 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2370 stat->link.std = NULL;
2371 stat->td.td_link = htole32(UHCI_PTR_T);
2372 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2373 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2375 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2376 UHCI_TD_IN (0, endpt, addr, 1));
2377 stat->td.td_buffer = htole32(0);
2380 if (uhcidebug > 10) {
2381 DPRINTF(("uhci_device_request: before transfer\n"));
2382 uhci_dump_tds(setup);
2386 /* Set up interrupt info. */
2388 ii->stdstart = setup;
2392 printf("uhci_device_request: not done, ii=%p\n", ii);
2398 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2401 if (dev->speed == USB_SPEED_LOW)
2402 uhci_add_ls_ctrl(sc, sqh);
2404 uhci_add_hs_ctrl(sc, sqh);
2405 uhci_add_intr_info(sc, ii);
2407 if (uhcidebug > 12) {
2408 uhci_soft_td_t *std;
2409 uhci_soft_qh_t *xqh;
2410 uhci_soft_qh_t *sxqh;
2412 uhci_physaddr_t link;
2413 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2414 for (std = sc->sc_vframes[0].htd, link = 0;
2415 (link & UHCI_PTR_QH) == 0;
2416 std = std->link.std) {
2417 link = le32toh(std->td.td_link);
2420 sxqh = (uhci_soft_qh_t *)std;
2424 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2425 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2428 DPRINTF(("Enqueued QH:\n"));
2430 uhci_dump_tds(sqh->elink);
2433 if (xfer->timeout && !sc->sc_bus.use_polling) {
2434 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2437 xfer->status = USBD_IN_PROGRESS;
2440 return (USBD_NORMAL_COMPLETION);
2444 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2448 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2450 /* Put it on our queue, */
2451 err = usb_insert_transfer(xfer);
2453 /* bail out on error, */
2454 if (err && err != USBD_IN_PROGRESS)
2457 /* XXX should check inuse here */
2459 /* insert into schedule, */
2460 uhci_device_isoc_enter(xfer);
2462 /* and start if the pipe wasn't running */
2464 uhci_device_isoc_start(STAILQ_FIRST(&xfer->pipe->queue));
2470 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2472 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2473 usbd_device_handle dev = upipe->pipe.device;
2474 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2475 struct iso *iso = &upipe->u.iso;
2476 uhci_soft_td_t *std;
2478 u_int32_t len, status;
2479 int err, s, i, isread, next, nframes, seg, segoff;
2481 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2483 iso->inuse, iso->next, xfer, xfer->nframes));
2488 if (xfer->status == USBD_IN_PROGRESS) {
2489 /* This request has already been entered into the frame list */
2490 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2495 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2496 printf("uhci_device_isoc_enter: overflow!\n");
2501 /* Not in use yet, schedule it a few frames ahead. */
2502 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2503 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2506 xfer->status = USBD_IN_PROGRESS;
2507 UXFER(xfer)->curframe = next;
2511 dataptr = xfer->allocbuf; /* Normal buffers not possible for isoc? */
2512 isread = xfer->pipe->endpoint->edesc->bEndpointAddress & UE_DIR_IN;
2513 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2516 nframes = xfer->nframes;
2518 for (i = 0; i < nframes; i++) {
2519 std = iso->stds[next];
2520 if (++next >= UHCI_VFRAMELIST_COUNT)
2522 len = xfer->frlengths[i];
2523 KASSERT(seg < xfer->dmamap.nsegs,
2524 ("uhci_device_isoc_enter: too few segments"));
2525 if (len + segoff > xfer->dmamap.segs[seg].ds_len) {
2526 /* UHCI can't handle non-contiguous data. */
2527 err = uhci_aux_dma_alloc(sc, std, dataptr, len);
2530 printf("uhci_device_isoc_enter: aux alloc\n");
2531 std->td.td_buffer = htole32(uhci_aux_dma_prepare(std,
2534 while (segoff >= xfer->dmamap.segs[seg].ds_len) {
2535 KASSERT(seg < xfer->dmamap.nsegs - 1 ||
2536 segoff == xfer->dmamap.segs[seg].ds_len,
2537 ("uhci_device_isoc_enter: overlap2"));
2538 segoff -= xfer->dmamap.segs[seg].ds_len;
2543 htole32(xfer->dmamap.segs[seg].ds_addr + segoff);
2545 if (segoff >= xfer->dmamap.segs[seg].ds_len) {
2546 KASSERT(segoff == xfer->dmamap.segs[seg].ds_len,
2547 ("uhci_device_isoc_enter: overlap"));
2552 if (i == nframes - 1)
2553 status |= UHCI_TD_IOC;
2554 std->td.td_status = htole32(status);
2555 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2556 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2558 if (uhcidebug > 5) {
2559 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2563 dataptr = (char *)dataptr + len;
2566 iso->inuse += xfer->nframes;
2572 uhci_device_isoc_start(usbd_xfer_handle xfer)
2574 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2575 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2576 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2577 uhci_soft_td_t *end;
2580 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2583 return (USBD_IOERROR);
2586 if (xfer->status != USBD_IN_PROGRESS)
2587 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2590 /* Find the last TD */
2591 i = UXFER(xfer)->curframe + xfer->nframes;
2592 if (i >= UHCI_VFRAMELIST_COUNT)
2593 i -= UHCI_VFRAMELIST_COUNT;
2594 end = upipe->u.iso.stds[i];
2598 printf("uhci_device_isoc_start: end == NULL\n");
2599 return (USBD_INVAL);
2605 /* Set up interrupt info. */
2611 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2614 uhci_add_intr_info(sc, ii);
2618 return (USBD_IN_PROGRESS);
2622 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2624 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2625 uhci_soft_td_t **stds = upipe->u.iso.stds;
2626 uhci_soft_td_t *std;
2627 int i, n, s, nframes, maxlen, len;
2631 /* Transfer is already done. */
2632 if (xfer->status != USBD_NOT_STARTED &&
2633 xfer->status != USBD_IN_PROGRESS) {
2638 /* Give xfer the requested abort code. */
2639 xfer->status = USBD_CANCELLED;
2641 /* make hardware ignore it, */
2642 nframes = xfer->nframes;
2643 n = UXFER(xfer)->curframe;
2645 for (i = 0; i < nframes; i++) {
2647 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2648 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2651 if (++n >= UHCI_VFRAMELIST_COUNT)
2655 /* and wait until we are sure the hardware has finished. */
2659 UXFER(xfer)->iinfo.isdone = 1;
2661 /* Run callback and remove from interrupt list. */
2662 uhci_transfer_complete(xfer);
2668 uhci_device_isoc_close(usbd_pipe_handle pipe)
2670 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2671 usbd_device_handle dev = upipe->pipe.device;
2672 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2673 uhci_soft_td_t *std, *vstd;
2678 * Make sure all TDs are marked as inactive.
2679 * Wait for completion.
2683 iso = &upipe->u.iso;
2685 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2686 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2687 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2690 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2692 for (vstd = sc->sc_vframes[i].htd;
2693 vstd != NULL && vstd->link.std != std;
2694 vstd = vstd->link.std)
2698 printf("uhci_device_isoc_close: %p not found\n", std);
2702 vstd->link = std->link;
2703 vstd->td.td_link = std->td.td_link;
2704 uhci_free_std(sc, std);
2708 free(iso->stds, M_USBHC);
2712 uhci_setup_isoc(usbd_pipe_handle pipe)
2714 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2715 usbd_device_handle dev = upipe->pipe.device;
2716 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2717 int addr = upipe->pipe.device->address;
2718 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2719 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2720 uhci_soft_td_t *std, *vstd;
2725 iso = &upipe->u.iso;
2726 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2729 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2730 UHCI_TD_OUT(0, endpt, addr, 0);
2732 /* Allocate the TDs and mark as inactive; */
2733 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2734 std = uhci_alloc_std(sc);
2737 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2738 std->td.td_token = htole32(token);
2742 /* Insert TDs into schedule. */
2744 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2746 vstd = sc->sc_vframes[i].htd;
2747 std->link = vstd->link;
2748 std->td.td_link = vstd->td.td_link;
2749 vstd->link.std = std;
2750 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2757 return (USBD_NORMAL_COMPLETION);
2761 uhci_free_std(sc, iso->stds[i]);
2762 free(iso->stds, M_USBHC);
2763 return (USBD_NOMEM);
2767 uhci_device_isoc_done(usbd_xfer_handle xfer)
2769 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2771 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2773 if (ii->xfer != xfer)
2774 /* Not on interrupt list, ignore it. */
2777 if (!uhci_active_intr_info(ii))
2781 if (xfer->busy_free != XFER_BUSY) {
2782 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2783 xfer, xfer->busy_free);
2787 if (ii->stdend == NULL) {
2788 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2796 /* Turn off the interrupt since it is active even if the TD is not. */
2797 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2799 uhci_del_intr_info(ii); /* remove from active list */
2802 if (ii->stdend == NULL) {
2803 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2810 ii->stdstart = NULL;
2815 uhci_device_intr_done(usbd_xfer_handle xfer)
2817 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2818 uhci_softc_t *sc = ii->sc;
2819 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2820 uhci_soft_qh_t *sqh;
2823 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2825 npoll = upipe->u.intr.npoll;
2826 for(i = 0; i < npoll; i++) {
2827 sqh = upipe->u.intr.qhs[i];
2829 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2831 uhci_free_std_chain(sc, ii->stdstart, NULL);
2834 if (xfer->pipe->repeat) {
2835 uhci_soft_td_t *data, *dataend;
2837 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2839 /* This alloc cannot fail since we freed the chain above. */
2840 uhci_alloc_std_chain(upipe, sc, xfer->length,
2841 upipe->u.intr.isread, xfer->flags, xfer,
2843 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2846 if (uhcidebug > 10) {
2847 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2848 uhci_dump_tds(data);
2849 uhci_dump_qh(upipe->u.intr.qhs[0]);
2853 ii->stdstart = data;
2854 ii->stdend = dataend;
2857 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2861 for (i = 0; i < npoll; i++) {
2862 sqh = upipe->u.intr.qhs[i];
2864 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2866 xfer->status = USBD_IN_PROGRESS;
2867 /* The ii is already on the examined list, just leave it. */
2869 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2870 if (uhci_active_intr_info(ii)) {
2871 uhci_del_intr_info(ii);
2872 ii->stdstart = NULL;
2878 /* Deallocate request data structures */
2880 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2882 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2883 uhci_softc_t *sc = ii->sc;
2884 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2887 if (!(xfer->rqflags & URQ_REQUEST))
2888 panic("uhci_device_ctrl_done: not a request");
2891 if (!uhci_active_intr_info(ii))
2894 uhci_del_intr_info(ii); /* remove from active list */
2896 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2897 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2899 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2901 if (upipe->u.ctl.length != 0)
2902 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2903 ii->stdstart = NULL;
2906 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2909 /* Deallocate request data structures */
2911 uhci_device_bulk_done(usbd_xfer_handle xfer)
2913 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2914 uhci_softc_t *sc = ii->sc;
2915 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2917 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2918 xfer, ii, sc, upipe));
2920 if (!uhci_active_intr_info(ii))
2923 uhci_del_intr_info(ii); /* remove from active list */
2925 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2927 uhci_free_std_chain(sc, ii->stdstart, NULL);
2928 ii->stdstart = NULL;
2931 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2934 /* Add interrupt QH, called with vflock. */
2936 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2938 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2939 uhci_soft_qh_t *eqh;
2941 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2944 sqh->hlink = eqh->hlink;
2945 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2947 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2952 /* Remove interrupt QH. */
2954 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2956 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2957 uhci_soft_qh_t *pqh;
2959 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2961 /* See comment in uhci_remove_ctrl() */
2962 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2963 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2964 delay(UHCI_QH_REMOVE_DELAY);
2967 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2968 pqh->hlink = sqh->hlink;
2969 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2970 delay(UHCI_QH_REMOVE_DELAY);
2977 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2979 uhci_soft_qh_t *sqh;
2981 u_int bestbw, bw, bestoffs, offs;
2983 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2985 printf("uhci_setintr: 0 interval\n");
2986 return (USBD_INVAL);
2989 if (ival > UHCI_VFRAMELIST_COUNT)
2990 ival = UHCI_VFRAMELIST_COUNT;
2991 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2992 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2994 upipe->u.intr.npoll = npoll;
2996 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2999 * Figure out which offset in the schedule that has most
3000 * bandwidth left over.
3002 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
3003 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
3004 for (bw = i = 0; i < npoll; i++)
3005 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
3011 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
3013 for(i = 0; i < npoll; i++) {
3014 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
3016 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
3017 sqh->pos = MOD(i * ival + bestoffs);
3022 /* Enter QHs into the controller data structures. */
3023 for(i = 0; i < npoll; i++)
3024 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
3027 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
3028 return (USBD_NORMAL_COMPLETION);
3031 /* Open a new pipe. */
3033 uhci_open(usbd_pipe_handle pipe)
3035 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3036 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
3037 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
3041 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
3042 pipe, pipe->device->address,
3043 ed->bEndpointAddress, sc->sc_addr));
3045 upipe->aborting = 0;
3046 upipe->nexttoggle = pipe->endpoint->savedtoggle;
3048 if (pipe->device->address == sc->sc_addr) {
3049 switch (ed->bEndpointAddress) {
3050 case USB_CONTROL_ENDPOINT:
3051 pipe->methods = &uhci_root_ctrl_methods;
3053 case UE_DIR_IN | UHCI_INTR_ENDPT:
3054 pipe->methods = &uhci_root_intr_methods;
3057 return (USBD_INVAL);
3060 switch (ed->bmAttributes & UE_XFERTYPE) {
3062 pipe->methods = &uhci_device_ctrl_methods;
3063 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
3064 if (upipe->u.ctl.sqh == NULL)
3066 upipe->u.ctl.setup = uhci_alloc_std(sc);
3067 if (upipe->u.ctl.setup == NULL) {
3068 uhci_free_sqh(sc, upipe->u.ctl.sqh);
3071 upipe->u.ctl.stat = uhci_alloc_std(sc);
3072 if (upipe->u.ctl.stat == NULL) {
3073 uhci_free_sqh(sc, upipe->u.ctl.sqh);
3074 uhci_free_std(sc, upipe->u.ctl.setup);
3077 err = usb_allocmem(&sc->sc_bus,
3078 sizeof(usb_device_request_t),
3079 0, &upipe->u.ctl.reqdma);
3081 uhci_free_sqh(sc, upipe->u.ctl.sqh);
3082 uhci_free_std(sc, upipe->u.ctl.setup);
3083 uhci_free_std(sc, upipe->u.ctl.stat);
3088 pipe->methods = &uhci_device_intr_methods;
3089 ival = pipe->interval;
3090 if (ival == USBD_DEFAULT_INTERVAL)
3091 ival = ed->bInterval;
3092 return (uhci_device_setintr(sc, upipe, ival));
3093 case UE_ISOCHRONOUS:
3094 pipe->methods = &uhci_device_isoc_methods;
3095 return (uhci_setup_isoc(pipe));
3097 pipe->methods = &uhci_device_bulk_methods;
3098 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
3099 if (upipe->u.bulk.sqh == NULL)
3104 return (USBD_NORMAL_COMPLETION);
3107 return (USBD_NOMEM);
3111 * Data structures and routines to emulate the root hub.
3113 usb_device_descriptor_t uhci_devd = {
3114 USB_DEVICE_DESCRIPTOR_SIZE,
3115 UDESC_DEVICE, /* type */
3116 {0x00, 0x01}, /* USB version */
3117 UDCLASS_HUB, /* class */
3118 UDSUBCLASS_HUB, /* subclass */
3119 UDPROTO_FSHUB, /* protocol */
3120 64, /* max packet */
3121 {0},{0},{0x00,0x01}, /* device id */
3122 1,2,0, /* string indicies */
3123 1 /* # of configurations */
3126 usb_config_descriptor_t uhci_confd = {
3127 USB_CONFIG_DESCRIPTOR_SIZE,
3129 {USB_CONFIG_DESCRIPTOR_SIZE +
3130 USB_INTERFACE_DESCRIPTOR_SIZE +
3131 USB_ENDPOINT_DESCRIPTOR_SIZE},
3139 usb_interface_descriptor_t uhci_ifcd = {
3140 USB_INTERFACE_DESCRIPTOR_SIZE,
3151 usb_endpoint_descriptor_t uhci_endpd = {
3152 USB_ENDPOINT_DESCRIPTOR_SIZE,
3154 UE_DIR_IN | UHCI_INTR_ENDPT,
3160 usb_hub_descriptor_t uhci_hubd_piix = {
3161 USB_HUB_DESCRIPTOR_SIZE,
3164 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
3165 50, /* power on to power good */
3167 { 0x00 }, /* both ports are removable */
3171 uhci_str(usb_string_descriptor_t *p, int l, char *s)
3177 p->bLength = 2 * strlen(s) + 2;
3180 p->bDescriptorType = UDESC_STRING;
3182 for (i = 0; s[i] && l > 1; i++, l -= 2)
3183 USETW2(p->bString[i], 0, s[i]);
3188 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3189 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3190 * should not be used by the USB subsystem. As we cannot issue a
3191 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3192 * will be enabled as part of the reset.
3194 * On the VT83C572, the port cannot be successfully enabled until the
3195 * outstanding "port enable change" and "connection status change"
3196 * events have been reset.
3199 uhci_portreset(uhci_softc_t *sc, int index)
3204 port = UHCI_PORTSC1;
3205 else if (index == 2)
3206 port = UHCI_PORTSC2;
3208 return (USBD_IOERROR);
3210 x = URWMASK(UREAD2(sc, port));
3211 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3213 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3215 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3216 index, UREAD2(sc, port)));
3218 x = URWMASK(UREAD2(sc, port));
3219 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3223 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3224 index, UREAD2(sc, port)));
3226 x = URWMASK(UREAD2(sc, port));
3227 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3229 for (lim = 10; --lim > 0;) {
3230 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3232 x = UREAD2(sc, port);
3234 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3237 if (!(x & UHCI_PORTSC_CCS)) {
3239 * No device is connected (or was disconnected
3240 * during reset). Consider the port reset.
3241 * The delay must be long enough to ensure on
3242 * the initial iteration that the device
3243 * connection will have been registered. 50ms
3244 * appears to be sufficient, but 20ms is not.
3246 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3251 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3253 * Port enabled changed and/or connection
3254 * status changed were set. Reset either or
3255 * both raised flags (by writing a 1 to that
3256 * bit), and wait again for state to settle.
3258 UWRITE2(sc, port, URWMASK(x) |
3259 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3263 if (x & UHCI_PORTSC_PE)
3264 /* Port is enabled */
3267 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3270 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3271 index, UREAD2(sc, port)));
3274 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3275 return (USBD_TIMEOUT);
3279 return (USBD_NORMAL_COMPLETION);
3283 * Simulate a hardware hub by handling all the necessary requests.
3286 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3290 /* Insert last in queue. */
3291 err = usb_insert_transfer(xfer);
3296 * Pipe isn't running (otherwise err would be USBD_INPROG),
3297 * so start it first.
3299 return (uhci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
3303 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3305 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3306 usb_device_request_t *req;
3309 int s, len, value, index, status, change, l, totlen = 0;
3310 usb_port_status_t ps;
3314 return (USBD_IOERROR);
3317 if (!(xfer->rqflags & URQ_REQUEST))
3318 panic("uhci_root_ctrl_transfer: not a request");
3320 req = &xfer->request;
3322 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3323 req->bmRequestType, req->bRequest));
3325 len = UGETW(req->wLength);
3326 value = UGETW(req->wValue);
3327 index = UGETW(req->wIndex);
3332 #define C(x,y) ((x) | ((y) << 8))
3333 switch(C(req->bRequest, req->bmRequestType)) {
3334 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3335 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3336 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3338 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3339 * for the integrated root hub.
3342 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3344 *(u_int8_t *)buf = sc->sc_conf;
3348 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3349 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3350 switch(value >> 8) {
3352 if ((value & 0xff) != 0) {
3356 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3357 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3358 memcpy(buf, &uhci_devd, l);
3361 if ((value & 0xff) != 0) {
3365 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3366 memcpy(buf, &uhci_confd, l);
3367 buf = (char *)buf + l;
3369 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3371 memcpy(buf, &uhci_ifcd, l);
3372 buf = (char *)buf + l;
3374 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3376 memcpy(buf, &uhci_endpd, l);
3381 *(u_int8_t *)buf = 0;
3383 switch (value & 0xff) {
3384 case 1: /* Vendor */
3385 totlen = uhci_str(buf, len, sc->sc_vendor);
3387 case 2: /* Product */
3388 totlen = uhci_str(buf, len, "UHCI root hub");
3397 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3399 *(u_int8_t *)buf = 0;
3403 case C(UR_GET_STATUS, UT_READ_DEVICE):
3405 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3409 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3410 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3412 USETW(((usb_status_t *)buf)->wStatus, 0);
3416 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3417 if (value >= USB_MAX_DEVICES) {
3421 sc->sc_addr = value;
3423 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3424 if (value != 0 && value != 1) {
3428 sc->sc_conf = value;
3430 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3432 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3433 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3434 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3437 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3439 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3442 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3444 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3445 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3446 "port=%d feature=%d\n",
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 x = URWMASK(UREAD2(sc, port));
3467 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3469 case UHF_C_PORT_CONNECTION:
3470 x = URWMASK(UREAD2(sc, port));
3471 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3473 case UHF_C_PORT_ENABLE:
3474 x = URWMASK(UREAD2(sc, port));
3475 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3477 case UHF_C_PORT_OVER_CURRENT:
3478 x = URWMASK(UREAD2(sc, port));
3479 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3481 case UHF_C_PORT_RESET:
3483 err = USBD_NORMAL_COMPLETION;
3485 case UHF_PORT_CONNECTION:
3486 case UHF_PORT_OVER_CURRENT:
3487 case UHF_PORT_POWER:
3488 case UHF_PORT_LOW_SPEED:
3489 case UHF_C_PORT_SUSPEND:
3495 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3497 port = UHCI_PORTSC1;
3498 else if (index == 2)
3499 port = UHCI_PORTSC2;
3506 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3507 UHCI_PORTSC_LS_SHIFT;
3511 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3512 if ((value & 0xff) != 0) {
3516 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3518 memcpy(buf, &uhci_hubd_piix, l);
3520 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3525 memset(buf, 0, len);
3528 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3530 port = UHCI_PORTSC1;
3531 else if (index == 2)
3532 port = UHCI_PORTSC2;
3541 x = UREAD2(sc, port);
3542 status = change = 0;
3543 if (x & UHCI_PORTSC_CCS)
3544 status |= UPS_CURRENT_CONNECT_STATUS;
3545 if (x & UHCI_PORTSC_CSC)
3546 change |= UPS_C_CONNECT_STATUS;
3547 if (x & UHCI_PORTSC_PE)
3548 status |= UPS_PORT_ENABLED;
3549 if (x & UHCI_PORTSC_POEDC)
3550 change |= UPS_C_PORT_ENABLED;
3551 if (x & UHCI_PORTSC_OCI)
3552 status |= UPS_OVERCURRENT_INDICATOR;
3553 if (x & UHCI_PORTSC_OCIC)
3554 change |= UPS_C_OVERCURRENT_INDICATOR;
3555 if (x & UHCI_PORTSC_SUSP)
3556 status |= UPS_SUSPEND;
3557 if (x & UHCI_PORTSC_LSDA)
3558 status |= UPS_LOW_SPEED;
3559 status |= UPS_PORT_POWER;
3561 change |= UPS_C_PORT_RESET;
3562 USETW(ps.wPortStatus, status);
3563 USETW(ps.wPortChange, change);
3564 l = min(len, sizeof ps);
3565 memcpy(buf, &ps, l);
3568 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3571 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3573 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3575 port = UHCI_PORTSC1;
3576 else if (index == 2)
3577 port = UHCI_PORTSC2;
3583 case UHF_PORT_ENABLE:
3584 x = URWMASK(UREAD2(sc, port));
3585 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3587 case UHF_PORT_SUSPEND:
3588 x = URWMASK(UREAD2(sc, port));
3589 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3591 case UHF_PORT_RESET:
3592 err = uhci_portreset(sc, index);
3594 case UHF_PORT_POWER:
3595 /* Pretend we turned on power */
3596 err = USBD_NORMAL_COMPLETION;
3598 case UHF_C_PORT_CONNECTION:
3599 case UHF_C_PORT_ENABLE:
3600 case UHF_C_PORT_OVER_CURRENT:
3601 case UHF_PORT_CONNECTION:
3602 case UHF_PORT_OVER_CURRENT:
3603 case UHF_PORT_LOW_SPEED:
3604 case UHF_C_PORT_SUSPEND:
3605 case UHF_C_PORT_RESET:
3615 xfer->actlen = totlen;
3616 err = USBD_NORMAL_COMPLETION;
3620 uhci_transfer_complete(xfer);
3622 return (USBD_IN_PROGRESS);
3625 /* Abort a root control request. */
3627 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3629 /* Nothing to do, all transfers are synchronous. */
3632 /* Close the root pipe. */
3634 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3636 DPRINTF(("uhci_root_ctrl_close\n"));
3639 /* Abort a root interrupt request. */
3641 uhci_root_intr_abort(usbd_xfer_handle xfer)
3643 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3645 callout_stop(&sc->sc_poll_handle);
3646 sc->sc_intr_xfer = NULL;
3648 if (xfer->pipe->intrxfer == xfer) {
3649 DPRINTF(("uhci_root_intr_abort: remove\n"));
3650 xfer->pipe->intrxfer = 0;
3652 xfer->status = USBD_CANCELLED;
3654 UXFER(xfer)->iinfo.isdone = 1;
3656 uhci_transfer_complete(xfer);
3660 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3664 /* Insert last in queue. */
3665 err = usb_insert_transfer(xfer);
3670 * Pipe isn't running (otherwise err would be USBD_INPROG),
3671 * so start it first.
3673 return (uhci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3676 /* Start a transfer on the root interrupt pipe */
3678 uhci_root_intr_start(usbd_xfer_handle xfer)
3680 usbd_pipe_handle pipe = xfer->pipe;
3681 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3683 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3684 xfer, xfer->length, xfer->flags));
3687 return (USBD_IOERROR);
3689 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3690 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3691 sc->sc_intr_xfer = xfer;
3692 return (USBD_IN_PROGRESS);
3695 /* Close the root interrupt pipe. */
3697 uhci_root_intr_close(usbd_pipe_handle pipe)
3699 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3701 callout_stop(&sc->sc_poll_handle);
3702 sc->sc_intr_xfer = NULL;
3703 DPRINTF(("uhci_root_intr_close\n"));