2 * $NetBSD: uhci.c,v 1.80 2000/01/19 01:16:38 augustss Exp $
3 * $NetBSD: uhci.c,v 1.170 2003/02/19 01:35:04 augustss Exp $
4 * $FreeBSD: src/sys/dev/usb/uhci.c,v 1.149 2003/11/10 00:08:41 joe Exp $
5 * $DragonFly: src/sys/bus/usb/uhci.c,v 1.16 2006/09/05 03:48:09 dillon Exp $
8 /* Also already incorporated from NetBSD:
9 * $NetBSD: uhci.c,v 1.172 2003/02/23 04:19:26 simonb Exp $
10 * $NetBSD: uhci.c,v 1.173 2003/05/13 04:41:59 gson Exp $
11 * $NetBSD: uhci.c,v 1.175 2003/09/12 16:18:08 mycroft Exp $
12 * $NetBSD: uhci.c,v 1.176 2003/11/04 19:11:21 mycroft Exp $
17 * Copyright (c) 1998 The NetBSD Foundation, Inc.
18 * All rights reserved.
20 * This code is derived from software contributed to The NetBSD Foundation
21 * by Lennart Augustsson (lennart@augustsson.net) at
22 * Carlstedt Research & Technology.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the NetBSD
35 * Foundation, Inc. and its contributors.
36 * 4. Neither the name of The NetBSD Foundation nor the names of its
37 * contributors may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
41 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
44 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50 * POSSIBILITY OF SUCH DAMAGE.
54 * USB Universal Host Controller driver.
55 * Handles e.g. PIIX3 and PIIX4.
57 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
58 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
59 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
60 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #if defined(__NetBSD__) || defined(__OpenBSD__)
68 #include <sys/device.h>
69 #include <sys/select.h>
70 #elif defined(__FreeBSD__) || defined(__DragonFly__)
71 #include <sys/endian.h>
72 #include <sys/module.h>
74 #include <machine/bus_pio.h>
75 #if defined(DIAGNOSTIC) && defined(__i386__)
76 #include <machine/cpu.h>
80 #include <sys/queue.h>
81 #include <sys/sysctl.h>
82 #include <sys/thread2.h>
84 #include <machine/bus.h>
85 #include <machine/endian.h>
91 #include "usb_quirks.h"
96 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
97 /*#define UHCI_CTL_LOOP */
99 #if defined(__FreeBSD__) || defined(__DragonFly__)
100 #include <machine/clock.h>
102 #define delay(d) DELAY(d)
105 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
107 #if defined(__OpenBSD__)
108 struct cfdriver uhci_cd = {
109 NULL, "uhci", DV_DULL
115 #define DPRINTF(x) if (uhcidebug) printf x
116 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
119 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
120 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
121 &uhcidebug, 0, "uhci debug level");
122 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
123 &uhcinoloop, 0, "uhci noloop");
125 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
129 #define DPRINTFN(n,x)
133 * The UHCI controller is little endian, so on big endian machines
134 * the data strored in memory needs to be swapped.
136 #if defined(__OpenBSD__)
137 #if BYTE_ORDER == BIG_ENDIAN
138 #define htole32(x) (bswap32(x))
139 #define le32toh(x) (bswap32(x))
141 #define htole32(x) (x)
142 #define le32toh(x) (x)
147 struct usbd_pipe pipe;
151 usbd_xfer_handle abortstart, abortend;
153 /* Info needed for different pipe kinds. */
159 uhci_soft_td_t *setup, *stat;
166 uhci_soft_qh_t **qhs;
176 uhci_soft_td_t **stds;
182 Static void uhci_globalreset(uhci_softc_t *);
183 Static usbd_status uhci_portreset(uhci_softc_t*, int);
184 Static void uhci_reset(uhci_softc_t *);
185 #if defined(__NetBSD__) || defined(__OpenBSD__)
186 Static void uhci_shutdown(void *v);
187 Static void uhci_power(int, void *);
189 Static usbd_status uhci_run(uhci_softc_t *, int run);
190 Static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
191 Static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
192 Static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
193 Static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
195 Static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
197 Static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
200 Static void uhci_free_std_chain(uhci_softc_t *,
201 uhci_soft_td_t *, uhci_soft_td_t *);
202 Static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
203 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
204 uhci_soft_td_t **, uhci_soft_td_t **);
205 Static void uhci_poll_hub(void *);
206 Static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
207 Static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
208 Static void uhci_idone(uhci_intr_info_t *);
210 Static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
212 Static void uhci_timeout(void *);
213 Static void uhci_timeout_task(void *);
214 Static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
215 Static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
216 Static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
217 Static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
218 Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
219 Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
220 Static int uhci_str(usb_string_descriptor_t *, int, char *);
221 Static void uhci_add_loop(uhci_softc_t *sc);
222 Static void uhci_rem_loop(uhci_softc_t *sc);
224 Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
225 Static void uhci_device_isoc_enter(usbd_xfer_handle);
227 Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
228 Static void uhci_freem(struct usbd_bus *, usb_dma_t *);
230 Static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
231 Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
233 Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
234 Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
235 Static void uhci_device_ctrl_abort(usbd_xfer_handle);
236 Static void uhci_device_ctrl_close(usbd_pipe_handle);
237 Static void uhci_device_ctrl_done(usbd_xfer_handle);
239 Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
240 Static usbd_status uhci_device_intr_start(usbd_xfer_handle);
241 Static void uhci_device_intr_abort(usbd_xfer_handle);
242 Static void uhci_device_intr_close(usbd_pipe_handle);
243 Static void uhci_device_intr_done(usbd_xfer_handle);
245 Static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
246 Static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
247 Static void uhci_device_bulk_abort(usbd_xfer_handle);
248 Static void uhci_device_bulk_close(usbd_pipe_handle);
249 Static void uhci_device_bulk_done(usbd_xfer_handle);
251 Static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
252 Static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
253 Static void uhci_device_isoc_abort(usbd_xfer_handle);
254 Static void uhci_device_isoc_close(usbd_pipe_handle);
255 Static void uhci_device_isoc_done(usbd_xfer_handle);
257 Static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
258 Static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
259 Static void uhci_root_ctrl_abort(usbd_xfer_handle);
260 Static void uhci_root_ctrl_close(usbd_pipe_handle);
261 Static void uhci_root_ctrl_done(usbd_xfer_handle);
263 Static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
264 Static usbd_status uhci_root_intr_start(usbd_xfer_handle);
265 Static void uhci_root_intr_abort(usbd_xfer_handle);
266 Static void uhci_root_intr_close(usbd_pipe_handle);
267 Static void uhci_root_intr_done(usbd_xfer_handle);
269 Static usbd_status uhci_open(usbd_pipe_handle);
270 Static void uhci_poll(struct usbd_bus *);
271 Static void uhci_softintr(void *);
273 Static usbd_status uhci_device_request(usbd_xfer_handle xfer);
275 Static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
276 Static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
277 Static usbd_status uhci_device_setintr(uhci_softc_t *sc,
278 struct uhci_pipe *pipe, int ival);
280 Static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
281 Static void uhci_noop(usbd_pipe_handle pipe);
283 Static __inline__ uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
287 Static void uhci_dump_all(uhci_softc_t *);
288 Static void uhci_dumpregs(uhci_softc_t *);
289 Static void uhci_dump_qhs(uhci_soft_qh_t *);
290 Static void uhci_dump_qh(uhci_soft_qh_t *);
291 Static void uhci_dump_tds(uhci_soft_td_t *);
292 Static void uhci_dump_td(uhci_soft_td_t *);
293 Static void uhci_dump_ii(uhci_intr_info_t *ii);
294 void uhci_dump(void);
297 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
298 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
299 #define UWRITE1(sc, r, x) \
300 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
301 } while (/*CONSTCOND*/0)
302 #define UWRITE2(sc, r, x) \
303 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
304 } while (/*CONSTCOND*/0)
305 #define UWRITE4(sc, r, x) \
306 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
307 } while (/*CONSTCOND*/0)
308 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
309 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
310 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
312 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
313 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
315 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
317 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
319 #define UHCI_INTR_ENDPT 1
321 struct usbd_bus_methods uhci_bus_methods = {
331 struct usbd_pipe_methods uhci_root_ctrl_methods = {
332 uhci_root_ctrl_transfer,
333 uhci_root_ctrl_start,
334 uhci_root_ctrl_abort,
335 uhci_root_ctrl_close,
340 struct usbd_pipe_methods uhci_root_intr_methods = {
341 uhci_root_intr_transfer,
342 uhci_root_intr_start,
343 uhci_root_intr_abort,
344 uhci_root_intr_close,
349 struct usbd_pipe_methods uhci_device_ctrl_methods = {
350 uhci_device_ctrl_transfer,
351 uhci_device_ctrl_start,
352 uhci_device_ctrl_abort,
353 uhci_device_ctrl_close,
355 uhci_device_ctrl_done,
358 struct usbd_pipe_methods uhci_device_intr_methods = {
359 uhci_device_intr_transfer,
360 uhci_device_intr_start,
361 uhci_device_intr_abort,
362 uhci_device_intr_close,
363 uhci_device_clear_toggle,
364 uhci_device_intr_done,
367 struct usbd_pipe_methods uhci_device_bulk_methods = {
368 uhci_device_bulk_transfer,
369 uhci_device_bulk_start,
370 uhci_device_bulk_abort,
371 uhci_device_bulk_close,
372 uhci_device_clear_toggle,
373 uhci_device_bulk_done,
376 struct usbd_pipe_methods uhci_device_isoc_methods = {
377 uhci_device_isoc_transfer,
378 uhci_device_isoc_start,
379 uhci_device_isoc_abort,
380 uhci_device_isoc_close,
382 uhci_device_isoc_done,
385 #define uhci_add_intr_info(sc, ii) \
386 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
387 #define uhci_del_intr_info(ii) \
389 ++ii->sc->sc_intrhead_deletion_counter; \
390 LIST_REMOVE((ii), list); \
391 (ii)->list.le_prev = NULL; \
393 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
395 Static __inline__ uhci_soft_qh_t *
396 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
398 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
400 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
401 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
402 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
403 printf("uhci_find_prev_qh: QH not found\n");
412 uhci_globalreset(uhci_softc_t *sc)
414 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
415 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
416 UHCICMD(sc, 0); /* do nothing */
420 uhci_init(uhci_softc_t *sc)
424 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
427 DPRINTFN(1,("uhci_init: start\n"));
436 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
437 uhci_globalreset(sc); /* reset the controller */
440 /* Allocate and initialize real frame array. */
441 err = usb_allocmem(&sc->sc_bus,
442 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
443 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
446 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
447 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
448 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
451 * Allocate a TD, inactive, that hangs from the last QH.
452 * This is to avoid a bug in the PIIX that makes it run berserk
455 std = uhci_alloc_std(sc);
458 std->link.std = NULL;
459 std->td.td_link = htole32(UHCI_PTR_T);
460 std->td.td_status = htole32(0); /* inactive */
461 std->td.td_token = htole32(0);
462 std->td.td_buffer = htole32(0);
464 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
465 lsqh = uhci_alloc_sqh(sc);
469 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
471 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
472 sc->sc_last_qh = lsqh;
474 /* Allocate the dummy QH where bulk traffic will be queued. */
475 bsqh = uhci_alloc_sqh(sc);
479 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
481 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
482 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
484 /* Allocate dummy QH where high speed control traffic will be queued. */
485 chsqh = uhci_alloc_sqh(sc);
489 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
491 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
492 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
494 /* Allocate dummy QH where control traffic will be queued. */
495 clsqh = uhci_alloc_sqh(sc);
499 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
501 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
502 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
505 * Make all (virtual) frame list pointers point to the interrupt
506 * queue heads and the interrupt queue heads at the control
507 * queue head and point the physical frame list to the virtual.
509 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
510 std = uhci_alloc_std(sc);
511 sqh = uhci_alloc_sqh(sc);
512 if (std == NULL || sqh == NULL)
516 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
517 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
518 std->td.td_token = htole32(0);
519 std->td.td_buffer = htole32(0);
521 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
523 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
524 sc->sc_vframes[i].htd = std;
525 sc->sc_vframes[i].etd = std;
526 sc->sc_vframes[i].hqh = sqh;
527 sc->sc_vframes[i].eqh = sqh;
529 j < UHCI_FRAMELIST_COUNT;
530 j += UHCI_VFRAMELIST_COUNT) {
531 sc->sc_pframes[j] = htole32(std->physaddr);
535 LIST_INIT(&sc->sc_intrhead);
537 SIMPLEQ_INIT(&sc->sc_free_xfers);
539 usb_callout_init(sc->sc_poll_handle);
541 /* Set up the bus struct. */
542 sc->sc_bus.methods = &uhci_bus_methods;
543 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
545 #if defined(__NetBSD__) || defined(__OpenBSD__)
546 sc->sc_suspend = PWR_RESUME;
547 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
548 sc->sc_shutdownhook = shutdownhook_establish(uhci_shutdown, sc);
550 DPRINTFN(1,("uhci_init: enabling\n"));
551 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
552 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
554 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
556 return (uhci_run(sc, 1)); /* and here we go... */
559 #if defined(__NetBSD__) || defined(__OpenBSD__)
561 uhci_activate(device_ptr_t self, enum devact act)
563 struct uhci_softc *sc = (struct uhci_softc *)self;
570 case DVACT_DEACTIVATE:
571 if (sc->sc_child != NULL)
572 rv = config_deactivate(sc->sc_child);
579 uhci_detach(struct uhci_softc *sc, int flags)
581 usbd_xfer_handle xfer;
584 if (sc->sc_child != NULL)
585 rv = config_detach(sc->sc_child, flags);
590 #if defined(__NetBSD__) || defined(__OpenBSD__)
591 powerhook_disestablish(sc->sc_powerhook);
592 shutdownhook_disestablish(sc->sc_shutdownhook);
595 /* Free all xfers associated with this HC. */
597 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
600 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
604 /* XXX free other data structures XXX */
611 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
613 return (usb_allocmem(bus, size, 0, dma));
617 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
619 usb_freemem(bus, dma);
623 uhci_allocx(struct usbd_bus *bus)
625 struct uhci_softc *sc = (struct uhci_softc *)bus;
626 usbd_xfer_handle xfer;
628 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
630 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
632 if (xfer->busy_free != XFER_FREE) {
633 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
638 xfer = kmalloc(sizeof(struct uhci_xfer), M_USB, M_INTWAIT);
641 memset(xfer, 0, sizeof (struct uhci_xfer));
642 UXFER(xfer)->iinfo.sc = sc;
644 UXFER(xfer)->iinfo.isdone = 1;
645 xfer->busy_free = XFER_BUSY;
652 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
654 struct uhci_softc *sc = (struct uhci_softc *)bus;
657 if (xfer->busy_free != XFER_BUSY) {
658 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
662 xfer->busy_free = XFER_FREE;
663 if (!UXFER(xfer)->iinfo.isdone) {
664 printf("uhci_freex: !isdone\n");
668 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
672 * Shut down the controller when the system is going down.
675 uhci_shutdown(void *v)
677 uhci_softc_t *sc = v;
679 DPRINTF(("uhci_shutdown: stopping the HC\n"));
680 uhci_run(sc, 0); /* stop the controller */
684 * Handle suspend/resume.
686 * We need to switch to polling mode here, because this routine is
687 * called from an interrupt context. This is all right since we
688 * are almost suspended anyway.
691 uhci_power(int why, void *v)
693 uhci_softc_t *sc = v;
697 cmd = UREAD2(sc, UHCI_CMD);
699 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
700 sc, why, sc->sc_suspend, cmd));
702 if (why != PWR_RESUME) {
707 if (sc->sc_intr_xfer != NULL)
708 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub,
710 sc->sc_bus.use_polling++;
711 uhci_run(sc, 0); /* stop the controller */
713 /* save some state if BIOS doesn't */
714 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
715 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
717 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
719 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
720 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
721 sc->sc_suspend = why;
722 sc->sc_bus.use_polling--;
723 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
726 if (sc->sc_suspend == PWR_RESUME)
727 printf("uhci_power: weird, resume without suspend.\n");
729 sc->sc_bus.use_polling++;
730 sc->sc_suspend = why;
731 if (cmd & UHCI_CMD_RS)
732 uhci_run(sc, 0); /* in case BIOS has started it */
734 /* restore saved state */
735 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
736 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
737 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
739 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
740 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
741 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
742 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
743 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
744 UHCICMD(sc, UHCI_CMD_MAXP);
745 uhci_run(sc, 1); /* and start traffic again */
746 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
747 sc->sc_bus.use_polling--;
748 if (sc->sc_intr_xfer != NULL)
749 usb_callout(sc->sc_poll_handle, sc->sc_ival,
750 uhci_poll_hub, sc->sc_intr_xfer);
761 uhci_dumpregs(uhci_softc_t *sc)
763 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
764 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
765 USBDEVNAME(sc->sc_bus.bdev),
766 UREAD2(sc, UHCI_CMD),
767 UREAD2(sc, UHCI_STS),
768 UREAD2(sc, UHCI_INTR),
769 UREAD2(sc, UHCI_FRNUM),
770 UREAD4(sc, UHCI_FLBASEADDR),
771 UREAD1(sc, UHCI_SOF),
772 UREAD2(sc, UHCI_PORTSC1),
773 UREAD2(sc, UHCI_PORTSC2)));
777 uhci_dump_td(uhci_soft_td_t *p)
779 char sbuf[128], sbuf2[128];
781 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
782 "token=0x%08lx buffer=0x%08lx\n",
783 p, (long)p->physaddr,
784 (long)le32toh(p->td.td_link),
785 (long)le32toh(p->td.td_status),
786 (long)le32toh(p->td.td_token),
787 (long)le32toh(p->td.td_buffer)));
789 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
791 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
792 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
793 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
794 sbuf2, sizeof(sbuf2));
796 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
797 "D=%d,maxlen=%d\n", sbuf, sbuf2,
798 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
799 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
800 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
801 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
802 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
803 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
804 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
808 uhci_dump_qh(uhci_soft_qh_t *sqh)
810 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
811 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
812 le32toh(sqh->qh.qh_elink)));
819 uhci_dump_all(thesc);
824 uhci_dump_all(uhci_softc_t *sc)
827 printf("intrs=%d\n", sc->sc_bus.no_intrs);
828 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
829 uhci_dump_qh(sc->sc_lctl_start);
833 uhci_dump_qhs(uhci_soft_qh_t *sqh)
837 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
838 * Traverses sideways first, then down.
848 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
852 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
853 uhci_dump_qhs(sqh->hlink);
855 DPRINTF(("No QH\n"));
857 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
858 uhci_dump_tds(sqh->elink);
860 DPRINTF(("No TD\n"));
864 uhci_dump_tds(uhci_soft_td_t *std)
868 for(td = std; td != NULL; td = td->link.std) {
871 /* Check whether the link pointer in this TD marks
872 * the link pointer as end of queue. This avoids
873 * printing the free list in case the queue/TD has
874 * already been moved there (seatbelt).
876 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
877 le32toh(td->td.td_link) == 0)
883 uhci_dump_ii(uhci_intr_info_t *ii)
885 usbd_pipe_handle pipe;
886 usb_endpoint_descriptor_t *ed;
887 usbd_device_handle dev;
890 #define DONE ii->isdone
898 if (ii->xfer == NULL) {
899 printf("ii %p: done=%d xfer=NULL\n",
903 pipe = ii->xfer->pipe;
905 printf("ii %p: done=%d xfer=%p pipe=NULL\n",
909 if (pipe->endpoint == NULL) {
910 printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
911 ii, DONE, ii->xfer, pipe);
914 if (pipe->device == NULL) {
915 printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
916 ii, DONE, ii->xfer, pipe);
919 ed = pipe->endpoint->edesc;
921 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",
922 ii, DONE, ii->xfer, dev,
923 UGETW(dev->ddesc.idVendor),
924 UGETW(dev->ddesc.idProduct),
926 ed->bEndpointAddress, ed->bmAttributes);
930 void uhci_dump_iis(struct uhci_softc *sc);
932 uhci_dump_iis(struct uhci_softc *sc)
934 uhci_intr_info_t *ii;
936 printf("intr_info list:\n");
937 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
942 void iidump(void) { uhci_dump_iis(thesc); }
947 * This routine is executed periodically and simulates interrupts
948 * from the root controller interrupt pipe for port status change.
951 uhci_poll_hub(void *addr)
953 usbd_xfer_handle xfer = addr;
954 usbd_pipe_handle pipe = xfer->pipe;
955 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
958 DPRINTFN(20, ("uhci_poll_hub\n"));
960 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
962 p = KERNADDR(&xfer->dmabuf, 0);
964 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
966 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
969 /* No change, try again in a while */
973 xfer->status = USBD_NORMAL_COMPLETION;
975 xfer->device->bus->intr_context++;
976 usb_transfer_complete(xfer);
977 xfer->device->bus->intr_context--;
982 uhci_root_intr_done(usbd_xfer_handle xfer)
987 uhci_root_ctrl_done(usbd_xfer_handle xfer)
992 * Let the last QH loop back to the high speed control transfer QH.
993 * This is what intel calls "bandwidth reclamation" and improves
994 * USB performance a lot for some devices.
995 * If we are already looping, just count it.
998 uhci_add_loop(uhci_softc_t *sc) {
1003 if (++sc->sc_loops == 1) {
1004 DPRINTFN(5,("uhci_start_loop: add\n"));
1005 /* Note, we don't loop back the soft pointer. */
1006 sc->sc_last_qh->qh.qh_hlink =
1007 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
1012 uhci_rem_loop(uhci_softc_t *sc) {
1017 if (--sc->sc_loops == 0) {
1018 DPRINTFN(5,("uhci_end_loop: remove\n"));
1019 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1023 /* Add high speed control QH, called from a critical section. */
1025 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1027 uhci_soft_qh_t *eqh;
1029 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
1030 eqh = sc->sc_hctl_end;
1031 sqh->hlink = eqh->hlink;
1032 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1034 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1035 sc->sc_hctl_end = sqh;
1036 #ifdef UHCI_CTL_LOOP
1041 /* Remove high speed control QH, called from a critical section. */
1043 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1045 uhci_soft_qh_t *pqh;
1047 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1048 #ifdef UHCI_CTL_LOOP
1052 * The T bit should be set in the elink of the QH so that the HC
1053 * doesn't follow the pointer. This condition may fail if the
1054 * the transferred packet was short so that the QH still points
1055 * at the last used TD.
1056 * In this case we set the T bit and wait a little for the HC
1057 * to stop looking at the TD.
1059 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1060 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1061 delay(UHCI_QH_REMOVE_DELAY);
1064 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1065 pqh->hlink = sqh->hlink;
1066 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1067 delay(UHCI_QH_REMOVE_DELAY);
1068 if (sc->sc_hctl_end == sqh)
1069 sc->sc_hctl_end = pqh;
1072 /* Add low speed control QH, called from a critical section. */
1074 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1076 uhci_soft_qh_t *eqh;
1078 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1079 eqh = sc->sc_lctl_end;
1080 sqh->hlink = eqh->hlink;
1081 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1083 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1084 sc->sc_lctl_end = sqh;
1087 /* Remove low speed control QH, called from a critical section. */
1089 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1091 uhci_soft_qh_t *pqh;
1093 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1094 /* See comment in uhci_remove_hs_ctrl() */
1095 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1096 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1097 delay(UHCI_QH_REMOVE_DELAY);
1099 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1100 pqh->hlink = sqh->hlink;
1101 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1102 delay(UHCI_QH_REMOVE_DELAY);
1103 if (sc->sc_lctl_end == sqh)
1104 sc->sc_lctl_end = pqh;
1107 /* Add bulk QH, called from a critical section. */
1109 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1111 uhci_soft_qh_t *eqh;
1113 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1114 eqh = sc->sc_bulk_end;
1115 sqh->hlink = eqh->hlink;
1116 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1118 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1119 sc->sc_bulk_end = sqh;
1123 /* Remove bulk QH, called from a critical section. */
1125 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1127 uhci_soft_qh_t *pqh;
1129 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1131 /* See comment in uhci_remove_hs_ctrl() */
1132 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1133 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1134 delay(UHCI_QH_REMOVE_DELAY);
1136 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1137 pqh->hlink = sqh->hlink;
1138 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1139 delay(UHCI_QH_REMOVE_DELAY);
1140 if (sc->sc_bulk_end == sqh)
1141 sc->sc_bulk_end = pqh;
1144 Static int uhci_intr1(uhci_softc_t *);
1147 uhci_intr(void *arg)
1149 uhci_softc_t *sc = arg;
1154 if (sc->sc_bus.use_polling) {
1156 DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n"));
1161 return (uhci_intr1(sc));
1165 uhci_intr1(uhci_softc_t *sc)
1172 * It can happen that an interrupt will be delivered to
1173 * us before the device has been fully attached and the
1174 * softc struct has been configured. Usually this happens
1175 * when kldloading the USB support as a module after the
1176 * system has been booted. If we detect this condition,
1177 * we need to squelch the unwanted interrupts until we're
1180 if (sc->sc_bus.bdev == NULL) {
1181 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1182 uhci_run(sc, 0); /* stop the controller */
1183 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1188 if (uhcidebug > 15) {
1189 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev)));
1193 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1194 if (status == 0) /* The interrupt was not for us. */
1197 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1198 if (sc->sc_suspend != PWR_RESUME)
1199 printf("uhci_intr: suspended sts=0x%x\n", status);
1202 if (sc->sc_suspend != PWR_RESUME) {
1203 printf("%s: interrupt while not operating ignored\n",
1204 USBDEVNAME(sc->sc_bus.bdev));
1205 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1210 if (status & UHCI_STS_USBINT)
1211 ack |= UHCI_STS_USBINT;
1212 if (status & UHCI_STS_USBEI)
1213 ack |= UHCI_STS_USBEI;
1214 if (status & UHCI_STS_RD) {
1217 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1220 if (status & UHCI_STS_HSE) {
1221 ack |= UHCI_STS_HSE;
1222 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
1224 if (status & UHCI_STS_HCPE) {
1225 ack |= UHCI_STS_HCPE;
1226 printf("%s: host controller process error\n",
1227 USBDEVNAME(sc->sc_bus.bdev));
1229 if (status & UHCI_STS_HCH) {
1230 /* no acknowledge needed */
1231 if (!sc->sc_dying) {
1232 printf("%s: host controller halted\n",
1233 USBDEVNAME(sc->sc_bus.bdev));
1242 return (0); /* nothing to acknowledge */
1243 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1245 sc->sc_bus.no_intrs++;
1246 usb_schedsoftintr(&sc->sc_bus);
1248 DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
1254 uhci_softintr(void *v)
1256 uhci_softc_t *sc = v;
1257 uhci_intr_info_t *ii;
1258 int last_deletion_counter;
1260 DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
1261 sc->sc_bus.intr_context));
1263 sc->sc_bus.intr_context++;
1266 * Interrupts on UHCI really suck. When the host controller
1267 * interrupts because a transfer is completed there is no
1268 * way of knowing which transfer it was. You can scan down
1269 * the TDs and QHs of the previous frame to limit the search,
1270 * but that assumes that the interrupt was not delayed by more
1271 * than 1 ms, which may not always be true (e.g. after debug
1272 * output on a slow console).
1273 * We scan all interrupt descriptors to see if any have
1276 * XXX horrible hack - use a counter to detect if the list is
1277 * modified out from under us and rescan if it is.
1280 last_deletion_counter = sc->sc_intrhead_deletion_counter;
1281 LIST_FOREACH(ii, &sc->sc_intrhead, list) {
1282 uhci_check_intr(sc, ii);
1283 if (sc->sc_intrhead_deletion_counter != last_deletion_counter)
1287 #ifdef USB_USE_SOFTINTR
1288 if (sc->sc_softwake) {
1289 sc->sc_softwake = 0;
1290 wakeup(&sc->sc_softwake);
1292 #endif /* USB_USE_SOFTINTR */
1294 sc->sc_bus.intr_context--;
1297 /* Check for an interrupt. */
1299 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1301 uhci_soft_td_t *std, *lstd;
1304 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1307 printf("uhci_check_intr: no ii? %p\n", ii);
1311 if (ii->xfer->status == USBD_CANCELLED ||
1312 ii->xfer->status == USBD_TIMEOUT) {
1313 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1317 if (ii->stdstart == NULL)
1322 printf("uhci_check_intr: std==0\n");
1327 * If the last TD is still active we need to check whether there
1328 * is an error somewhere in the middle, or whether there was a
1329 * short packet (SPD and not ACTIVE).
1331 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1332 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1333 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1334 status = le32toh(std->td.td_status);
1335 /* If there's an active TD the xfer isn't done. */
1336 if (status & UHCI_TD_ACTIVE)
1338 /* Any kind of error makes the xfer done. */
1339 if (status & UHCI_TD_STALLED)
1341 /* We want short packets, and it is short: it's done */
1342 if ((status & UHCI_TD_SPD) &&
1343 UHCI_TD_GET_ACTLEN(status) <
1344 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) {
1348 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1353 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1354 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1358 /* Called from a critical section. */
1360 uhci_idone(uhci_intr_info_t *ii)
1362 usbd_xfer_handle xfer = ii->xfer;
1363 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1364 uhci_soft_td_t *std;
1365 u_int32_t status = 0, nstatus;
1368 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1375 printf("uhci_idone: ii is done!\n ");
1378 printf("uhci_idone: ii=%p is done!\n", ii);
1387 if (xfer->nframes != 0) {
1388 /* Isoc transfer, do things differently. */
1389 uhci_soft_td_t **stds = upipe->u.iso.stds;
1390 int i, n, nframes, len;
1392 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1394 nframes = xfer->nframes;
1396 n = UXFER(xfer)->curframe;
1397 for (i = 0; i < nframes; i++) {
1400 if (uhcidebug > 5) {
1401 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1405 if (++n >= UHCI_VFRAMELIST_COUNT)
1407 status = le32toh(std->td.td_status);
1408 len = UHCI_TD_GET_ACTLEN(status);
1409 xfer->frlengths[i] = len;
1412 upipe->u.iso.inuse -= nframes;
1413 xfer->actlen = actlen;
1414 xfer->status = USBD_NORMAL_COMPLETION;
1419 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1422 uhci_dump_tds(ii->stdstart);
1425 /* The transfer is done, compute actual length and status. */
1427 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1428 nstatus = le32toh(std->td.td_status);
1429 if (nstatus & UHCI_TD_ACTIVE)
1433 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1434 UHCI_TD_PID_SETUP) {
1435 actlen += UHCI_TD_GET_ACTLEN(status);
1438 * UHCI will report CRCTO in addition to a STALL or NAK
1439 * for a SETUP transaction. See section 3.2.2, "TD
1440 * CONTROL AND STATUS".
1442 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1443 status &= ~UHCI_TD_CRCTO;
1447 /* If there are left over TDs we need to update the toggle. */
1449 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1451 status &= UHCI_TD_ERROR;
1452 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1454 xfer->actlen = actlen;
1459 bitmask_snprintf((u_int32_t)status,
1460 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1461 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1462 sbuf, sizeof(sbuf));
1464 DPRINTFN((status == UHCI_TD_STALLED)*10,
1465 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1467 xfer->pipe->device->address,
1468 xfer->pipe->endpoint->edesc->bEndpointAddress,
1471 if (status == UHCI_TD_STALLED)
1472 xfer->status = USBD_STALLED;
1474 xfer->status = USBD_IOERROR; /* more info XXX */
1476 xfer->status = USBD_NORMAL_COMPLETION;
1480 usb_transfer_complete(xfer);
1481 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1485 * Called when a request does not complete.
1488 uhci_timeout(void *addr)
1490 uhci_intr_info_t *ii = addr;
1491 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1492 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1493 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1495 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1498 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1502 /* Execute the abort in a process context. */
1503 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer);
1504 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1508 uhci_timeout_task(void *addr)
1510 usbd_xfer_handle xfer = addr;
1512 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1515 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1520 * Wait here until controller claims to have an interrupt.
1521 * Then call uhci_intr and return. Use timeout to avoid waiting
1523 * Only used during boot when interrupts are not enabled yet.
1526 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1528 int timo = xfer->timeout;
1529 uhci_intr_info_t *ii;
1531 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1533 xfer->status = USBD_IN_PROGRESS;
1534 for (; timo >= 0; timo--) {
1535 usb_delay_ms(&sc->sc_bus, 1);
1536 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1537 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1539 if (xfer->status != USBD_IN_PROGRESS)
1544 DPRINTF(("uhci_waitintr: timeout\n"));
1545 for (ii = LIST_FIRST(&sc->sc_intrhead);
1546 ii != NULL && ii->xfer != xfer;
1547 ii = LIST_NEXT(ii, list))
1551 panic("uhci_waitintr: lost intr_info");
1557 uhci_poll(struct usbd_bus *bus)
1559 uhci_softc_t *sc = (uhci_softc_t *)bus;
1561 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1566 uhci_reset(uhci_softc_t *sc)
1570 UHCICMD(sc, UHCI_CMD_HCRESET);
1571 /* The reset bit goes low when the controller is done. */
1572 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1573 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1574 usb_delay_ms(&sc->sc_bus, 1);
1575 if (n >= UHCI_RESET_TIMEOUT)
1576 printf("%s: controller did not reset\n",
1577 USBDEVNAME(sc->sc_bus.bdev));
1581 uhci_run(uhci_softc_t *sc, int run)
1588 DPRINTF(("uhci_run: setting run=%d\n", run));
1589 cmd = UREAD2(sc, UHCI_CMD);
1593 cmd &= ~UHCI_CMD_RS;
1595 for(n = 0; n < 10; n++) {
1596 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1597 /* return when we've entered the state we want */
1598 if (run == running) {
1600 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1601 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1602 return (USBD_NORMAL_COMPLETION);
1604 usb_delay_ms(&sc->sc_bus, 1);
1607 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1608 run ? "start" : "stop");
1609 return (USBD_IOERROR);
1613 * Memory management routines.
1614 * uhci_alloc_std allocates TDs
1615 * uhci_alloc_sqh allocates QHs
1616 * These two routines do their own free list management,
1617 * partly for speed, partly because allocating DMAable memory
1618 * has page size granularaity so much memory would be wasted if
1619 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1623 uhci_alloc_std(uhci_softc_t *sc)
1625 uhci_soft_td_t *std;
1630 if (sc->sc_freetds == NULL) {
1631 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1632 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1633 UHCI_TD_ALIGN, &dma);
1636 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1637 offs = i * UHCI_STD_SIZE;
1638 std = KERNADDR(&dma, offs);
1639 std->physaddr = DMAADDR(&dma, offs);
1640 std->link.std = sc->sc_freetds;
1641 sc->sc_freetds = std;
1644 std = sc->sc_freetds;
1645 sc->sc_freetds = std->link.std;
1646 memset(&std->td, 0, sizeof(uhci_td_t));
1651 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1654 #define TD_IS_FREE 0x12345678
1655 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1656 printf("uhci_free_std: freeing free TD %p\n", std);
1659 std->td.td_token = htole32(TD_IS_FREE);
1661 std->link.std = sc->sc_freetds;
1662 sc->sc_freetds = std;
1666 uhci_alloc_sqh(uhci_softc_t *sc)
1668 uhci_soft_qh_t *sqh;
1673 if (sc->sc_freeqhs == NULL) {
1674 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1675 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1676 UHCI_QH_ALIGN, &dma);
1679 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1680 offs = i * UHCI_SQH_SIZE;
1681 sqh = KERNADDR(&dma, offs);
1682 sqh->physaddr = DMAADDR(&dma, offs);
1683 sqh->hlink = sc->sc_freeqhs;
1684 sc->sc_freeqhs = sqh;
1687 sqh = sc->sc_freeqhs;
1688 sc->sc_freeqhs = sqh->hlink;
1689 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1694 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1696 sqh->hlink = sc->sc_freeqhs;
1697 sc->sc_freeqhs = sqh;
1701 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1702 uhci_soft_td_t *stdend)
1706 for (; std != stdend; std = p) {
1708 uhci_free_std(sc, std);
1713 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1714 int rd, u_int16_t flags, usb_dma_t *dma,
1715 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1717 uhci_soft_td_t *p, *lastp;
1718 uhci_physaddr_t lastlink;
1719 int i, ntd, l, tog, maxp;
1721 int addr = upipe->pipe.device->address;
1722 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1724 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1725 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1726 upipe->pipe.device->speed, flags));
1727 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1729 printf("uhci_alloc_std_chain: maxp=0\n");
1730 return (USBD_INVAL);
1732 ntd = (len + maxp - 1) / maxp;
1733 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1735 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1738 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1739 return (USBD_NORMAL_COMPLETION);
1741 tog = upipe->nexttoggle;
1744 upipe->nexttoggle = tog ^ 1;
1746 lastlink = UHCI_PTR_T;
1748 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1749 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1750 status |= UHCI_TD_LS;
1751 if (flags & USBD_SHORT_XFER_OK)
1752 status |= UHCI_TD_SPD;
1753 for (i = ntd; i >= 0; i--) {
1754 p = uhci_alloc_std(sc);
1756 uhci_free_std_chain(sc, lastp, NULL);
1757 return (USBD_NOMEM);
1759 p->link.std = lastp;
1760 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1762 lastlink = p->physaddr;
1763 p->td.td_status = htole32(status);
1767 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1773 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1774 UHCI_TD_OUT(l, endpt, addr, tog));
1775 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1779 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1780 upipe->nexttoggle));
1781 return (USBD_NORMAL_COMPLETION);
1785 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1787 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1788 upipe->nexttoggle = 0;
1792 uhci_noop(usbd_pipe_handle pipe)
1797 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1801 /* Insert last in queue. */
1802 err = usb_insert_transfer(xfer);
1807 * Pipe isn't running (otherwise err would be USBD_INPROG),
1808 * so start it first.
1810 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1814 uhci_device_bulk_start(usbd_xfer_handle xfer)
1816 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1817 usbd_device_handle dev = upipe->pipe.device;
1818 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1819 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1820 uhci_soft_td_t *data, *dataend;
1821 uhci_soft_qh_t *sqh;
1823 int len, isread, endpt;
1825 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1826 xfer, xfer->length, xfer->flags, ii));
1829 return (USBD_IOERROR);
1832 if (xfer->rqflags & URQ_REQUEST)
1833 panic("uhci_device_bulk_transfer: a request");
1837 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1838 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1839 sqh = upipe->u.bulk.sqh;
1841 upipe->u.bulk.isread = isread;
1842 upipe->u.bulk.length = len;
1844 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1845 &xfer->dmabuf, &data, &dataend);
1848 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1851 if (uhcidebug > 8) {
1852 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1853 uhci_dump_tds(data);
1857 /* Set up interrupt info. */
1859 ii->stdstart = data;
1860 ii->stdend = dataend;
1863 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1869 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1872 uhci_add_bulk(sc, sqh);
1873 uhci_add_intr_info(sc, ii);
1875 if (xfer->timeout && !sc->sc_bus.use_polling) {
1876 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1879 xfer->status = USBD_IN_PROGRESS;
1883 if (uhcidebug > 10) {
1884 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1885 uhci_dump_tds(data);
1889 if (sc->sc_bus.use_polling)
1890 uhci_waitintr(sc, xfer);
1892 return (USBD_IN_PROGRESS);
1895 /* Abort a device bulk request. */
1897 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1899 DPRINTF(("uhci_device_bulk_abort:\n"));
1900 uhci_abort_xfer(xfer, USBD_CANCELLED);
1904 * Abort a device request.
1905 * If this routine is called from a critical section. It guarentees that
1906 * the request will be removed from the hardware scheduling and that
1907 * the callback for it will be called with USBD_CANCELLED status.
1908 * It's impossible to guarantee that the requested transfer will not
1909 * have happened since the hardware runs concurrently.
1910 * If the transaction has already happened we rely on the ordinary
1911 * interrupt processing to process it.
1914 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1916 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1917 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1918 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1919 uhci_soft_td_t *std;
1921 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1924 /* If we're dying, just do the software part. */
1926 xfer->status = status; /* make software ignore it */
1927 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1928 usb_transfer_complete(xfer);
1933 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
1934 panic("uhci_abort_xfer: not in process context");
1937 * Step 1: Make interrupt routine and hardware ignore xfer.
1940 xfer->status = status; /* make software ignore it */
1941 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1942 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1943 for (std = ii->stdstart; std != NULL; std = std->link.std)
1944 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1948 * Step 2: Wait until we know hardware has finished any possible
1949 * use of the xfer. Also make sure the soft interrupt routine
1952 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1954 #ifdef USB_USE_SOFTINTR
1955 sc->sc_softwake = 1;
1956 #endif /* USB_USE_SOFTINTR */
1957 usb_schedsoftintr(&sc->sc_bus);
1958 #ifdef USB_USE_SOFTINTR
1959 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1960 tsleep(&sc->sc_softwake, 0, "uhciab", 0);
1961 #endif /* USB_USE_SOFTINTR */
1965 * Step 3: Execute callback.
1969 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1975 usb_transfer_complete(xfer);
1980 /* Close a device bulk pipe. */
1982 uhci_device_bulk_close(usbd_pipe_handle pipe)
1984 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1985 usbd_device_handle dev = upipe->pipe.device;
1986 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1988 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1992 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1996 /* Insert last in queue. */
1997 err = usb_insert_transfer(xfer);
2002 * Pipe isn't running (otherwise err would be USBD_INPROG),
2003 * so start it first.
2005 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2009 uhci_device_ctrl_start(usbd_xfer_handle xfer)
2011 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2015 return (USBD_IOERROR);
2018 if (!(xfer->rqflags & URQ_REQUEST))
2019 panic("uhci_device_ctrl_transfer: not a request");
2022 err = uhci_device_request(xfer);
2026 if (sc->sc_bus.use_polling)
2027 uhci_waitintr(sc, xfer);
2028 return (USBD_IN_PROGRESS);
2032 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2036 /* Insert last in queue. */
2037 err = usb_insert_transfer(xfer);
2042 * Pipe isn't running (otherwise err would be USBD_INPROG),
2043 * so start it first.
2045 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2049 uhci_device_intr_start(usbd_xfer_handle xfer)
2051 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2052 usbd_device_handle dev = upipe->pipe.device;
2053 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2054 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2055 uhci_soft_td_t *data, *dataend;
2056 uhci_soft_qh_t *sqh;
2062 return (USBD_IOERROR);
2064 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2065 xfer, xfer->length, xfer->flags));
2068 if (xfer->rqflags & URQ_REQUEST)
2069 panic("uhci_device_intr_transfer: a request");
2072 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2073 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2074 sqh = upipe->u.bulk.sqh;
2076 upipe->u.intr.isread = isread;
2078 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread,
2079 xfer->flags, &xfer->dmabuf, &data,
2083 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2086 if (uhcidebug > 10) {
2087 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2088 uhci_dump_tds(data);
2089 uhci_dump_qh(upipe->u.intr.qhs[0]);
2094 /* Set up interrupt info. */
2096 ii->stdstart = data;
2097 ii->stdend = dataend;
2100 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2105 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2106 upipe->u.intr.qhs[0]));
2107 for (i = 0; i < upipe->u.intr.npoll; i++) {
2108 sqh = upipe->u.intr.qhs[i];
2110 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2112 uhci_add_intr_info(sc, ii);
2113 xfer->status = USBD_IN_PROGRESS;
2117 if (uhcidebug > 10) {
2118 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2119 uhci_dump_tds(data);
2120 uhci_dump_qh(upipe->u.intr.qhs[0]);
2124 return (USBD_IN_PROGRESS);
2127 /* Abort a device control request. */
2129 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2131 DPRINTF(("uhci_device_ctrl_abort:\n"));
2132 uhci_abort_xfer(xfer, USBD_CANCELLED);
2135 /* Close a device control pipe. */
2137 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2141 /* Abort a device interrupt request. */
2143 uhci_device_intr_abort(usbd_xfer_handle xfer)
2145 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2146 if (xfer->pipe->intrxfer == xfer) {
2147 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2148 xfer->pipe->intrxfer = NULL;
2150 uhci_abort_xfer(xfer, USBD_CANCELLED);
2153 /* Close a device interrupt pipe. */
2155 uhci_device_intr_close(usbd_pipe_handle pipe)
2157 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2158 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2161 /* Unlink descriptors from controller data structures. */
2162 npoll = upipe->u.intr.npoll;
2164 for (i = 0; i < npoll; i++)
2165 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2169 * We now have to wait for any activity on the physical
2170 * descriptors to stop.
2172 usb_delay_ms(&sc->sc_bus, 2);
2174 for(i = 0; i < npoll; i++)
2175 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2176 kfree(upipe->u.intr.qhs, M_USBHC);
2178 /* XXX free other resources */
2182 uhci_device_request(usbd_xfer_handle xfer)
2184 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2185 usb_device_request_t *req = &xfer->request;
2186 usbd_device_handle dev = upipe->pipe.device;
2187 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2188 int addr = dev->address;
2189 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2190 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2191 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2192 uhci_soft_qh_t *sqh;
2198 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2199 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2200 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2201 UGETW(req->wIndex), UGETW(req->wLength),
2204 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2205 isread = req->bmRequestType & UT_READ;
2206 len = UGETW(req->wLength);
2208 setup = upipe->u.ctl.setup;
2209 stat = upipe->u.ctl.stat;
2210 sqh = upipe->u.ctl.sqh;
2212 /* Set up data transaction */
2214 upipe->nexttoggle = 1;
2215 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2216 &xfer->dmabuf, &data, &dataend);
2220 dataend->link.std = stat;
2221 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2225 upipe->u.ctl.length = len;
2227 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2229 setup->link.std = next;
2230 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2231 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2233 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2234 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2235 stat->link.std = NULL;
2236 stat->td.td_link = htole32(UHCI_PTR_T);
2237 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2238 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2241 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2242 UHCI_TD_IN (0, endpt, addr, 1));
2243 stat->td.td_buffer = htole32(0);
2246 if (uhcidebug > 10) {
2247 DPRINTF(("uhci_device_request: before transfer\n"));
2248 uhci_dump_tds(setup);
2252 /* Set up interrupt info. */
2254 ii->stdstart = setup;
2258 printf("uhci_device_request: not done, ii=%p\n", ii);
2264 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2267 if (dev->speed == USB_SPEED_LOW)
2268 uhci_add_ls_ctrl(sc, sqh);
2270 uhci_add_hs_ctrl(sc, sqh);
2271 uhci_add_intr_info(sc, ii);
2273 if (uhcidebug > 12) {
2274 uhci_soft_td_t *std;
2275 uhci_soft_qh_t *xqh;
2276 uhci_soft_qh_t *sxqh;
2278 uhci_physaddr_t link;
2279 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2280 for (std = sc->sc_vframes[0].htd, link = 0;
2281 (link & UHCI_PTR_QH) == 0;
2282 std = std->link.std) {
2283 link = le32toh(std->td.td_link);
2286 sxqh = (uhci_soft_qh_t *)std;
2290 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2291 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2294 DPRINTF(("Enqueued QH:\n"));
2296 uhci_dump_tds(sqh->elink);
2299 if (xfer->timeout && !sc->sc_bus.use_polling) {
2300 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2303 xfer->status = USBD_IN_PROGRESS;
2306 return (USBD_NORMAL_COMPLETION);
2310 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2314 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2316 /* Put it on our queue, */
2317 err = usb_insert_transfer(xfer);
2319 /* bail out on error, */
2320 if (err && err != USBD_IN_PROGRESS)
2323 /* XXX should check inuse here */
2325 /* insert into schedule, */
2326 uhci_device_isoc_enter(xfer);
2328 /* and start if the pipe wasn't running */
2330 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2336 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2338 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2339 usbd_device_handle dev = upipe->pipe.device;
2340 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2341 struct iso *iso = &upipe->u.iso;
2342 uhci_soft_td_t *std;
2343 u_int32_t buf, len, status;
2344 int i, next, nframes;
2346 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2348 iso->inuse, iso->next, xfer, xfer->nframes));
2353 if (xfer->status == USBD_IN_PROGRESS) {
2354 /* This request has already been entered into the frame list */
2355 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2360 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2361 printf("uhci_device_isoc_enter: overflow!\n");
2366 /* Not in use yet, schedule it a few frames ahead. */
2367 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2368 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2371 xfer->status = USBD_IN_PROGRESS;
2372 UXFER(xfer)->curframe = next;
2374 buf = DMAADDR(&xfer->dmabuf, 0);
2375 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2378 nframes = xfer->nframes;
2380 for (i = 0; i < nframes; i++) {
2381 std = iso->stds[next];
2382 if (++next >= UHCI_VFRAMELIST_COUNT)
2384 len = xfer->frlengths[i];
2385 std->td.td_buffer = htole32(buf);
2386 if (i == nframes - 1)
2387 status |= UHCI_TD_IOC;
2388 std->td.td_status = htole32(status);
2389 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2390 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2392 if (uhcidebug > 5) {
2393 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2400 iso->inuse += xfer->nframes;
2406 uhci_device_isoc_start(usbd_xfer_handle xfer)
2408 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2409 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2410 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2411 uhci_soft_td_t *end;
2414 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2417 return (USBD_IOERROR);
2420 if (xfer->status != USBD_IN_PROGRESS)
2421 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2424 /* Find the last TD */
2425 i = UXFER(xfer)->curframe + xfer->nframes;
2426 if (i >= UHCI_VFRAMELIST_COUNT)
2427 i -= UHCI_VFRAMELIST_COUNT;
2428 end = upipe->u.iso.stds[i];
2432 printf("uhci_device_isoc_start: end == NULL\n");
2433 return (USBD_INVAL);
2439 /* Set up interrupt info. */
2445 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2448 uhci_add_intr_info(sc, ii);
2452 return (USBD_IN_PROGRESS);
2456 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2458 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2459 uhci_soft_td_t **stds = upipe->u.iso.stds;
2460 uhci_soft_td_t *std;
2461 int i, n, nframes, maxlen, len;
2465 /* Transfer is already done. */
2466 if (xfer->status != USBD_NOT_STARTED &&
2467 xfer->status != USBD_IN_PROGRESS) {
2472 /* Give xfer the requested abort code. */
2473 xfer->status = USBD_CANCELLED;
2475 /* make hardware ignore it, */
2476 nframes = xfer->nframes;
2477 n = UXFER(xfer)->curframe;
2479 for (i = 0; i < nframes; i++) {
2481 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2482 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2485 if (++n >= UHCI_VFRAMELIST_COUNT)
2489 /* and wait until we are sure the hardware has finished. */
2493 UXFER(xfer)->iinfo.isdone = 1;
2495 /* Run callback and remove from interrupt list. */
2496 usb_transfer_complete(xfer);
2502 uhci_device_isoc_close(usbd_pipe_handle pipe)
2504 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2505 usbd_device_handle dev = upipe->pipe.device;
2506 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2507 uhci_soft_td_t *std, *vstd;
2512 * Make sure all TDs are marked as inactive.
2513 * Wait for completion.
2517 iso = &upipe->u.iso;
2519 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2520 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2521 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2524 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2526 for (vstd = sc->sc_vframes[i].htd;
2527 vstd != NULL && vstd->link.std != std;
2528 vstd = vstd->link.std)
2532 printf("uhci_device_isoc_close: %p not found\n", std);
2536 vstd->link = std->link;
2537 vstd->td.td_link = std->td.td_link;
2538 uhci_free_std(sc, std);
2542 kfree(iso->stds, M_USBHC);
2546 uhci_setup_isoc(usbd_pipe_handle pipe)
2548 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2549 usbd_device_handle dev = upipe->pipe.device;
2550 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2551 int addr = upipe->pipe.device->address;
2552 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2553 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2554 uhci_soft_td_t *std, *vstd;
2559 iso = &upipe->u.iso;
2560 iso->stds = kmalloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2561 M_USBHC, M_INTWAIT);
2563 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2564 UHCI_TD_OUT(0, endpt, addr, 0);
2566 /* Allocate the TDs and mark as inactive; */
2567 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2568 std = uhci_alloc_std(sc);
2571 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2572 std->td.td_token = htole32(token);
2576 /* Insert TDs into schedule. */
2578 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2580 vstd = sc->sc_vframes[i].htd;
2581 std->link = vstd->link;
2582 std->td.td_link = vstd->td.td_link;
2583 vstd->link.std = std;
2584 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2591 return (USBD_NORMAL_COMPLETION);
2595 uhci_free_std(sc, iso->stds[i]);
2596 kfree(iso->stds, M_USBHC);
2597 return (USBD_NOMEM);
2601 uhci_device_isoc_done(usbd_xfer_handle xfer)
2603 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2605 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2607 if (ii->xfer != xfer)
2608 /* Not on interrupt list, ignore it. */
2611 if (!uhci_active_intr_info(ii))
2615 if (xfer->busy_free != XFER_BUSY) {
2616 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2617 xfer, xfer->busy_free);
2621 if (ii->stdend == NULL) {
2622 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2630 /* Turn off the interrupt since it is active even if the TD is not. */
2631 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2633 uhci_del_intr_info(ii); /* remove from active list */
2636 if (ii->stdend == NULL) {
2637 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2647 uhci_device_intr_done(usbd_xfer_handle xfer)
2649 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2650 uhci_softc_t *sc = ii->sc;
2651 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2652 uhci_soft_qh_t *sqh;
2655 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2657 npoll = upipe->u.intr.npoll;
2658 for(i = 0; i < npoll; i++) {
2659 sqh = upipe->u.intr.qhs[i];
2661 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2663 uhci_free_std_chain(sc, ii->stdstart, NULL);
2666 if (xfer->pipe->repeat) {
2667 uhci_soft_td_t *data, *dataend;
2669 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2671 /* This alloc cannot fail since we freed the chain above. */
2672 uhci_alloc_std_chain(upipe, sc, xfer->length,
2673 upipe->u.intr.isread, xfer->flags,
2674 &xfer->dmabuf, &data, &dataend);
2675 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2678 if (uhcidebug > 10) {
2679 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2680 uhci_dump_tds(data);
2681 uhci_dump_qh(upipe->u.intr.qhs[0]);
2685 ii->stdstart = data;
2686 ii->stdend = dataend;
2689 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2693 for (i = 0; i < npoll; i++) {
2694 sqh = upipe->u.intr.qhs[i];
2696 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2698 xfer->status = USBD_IN_PROGRESS;
2699 /* The ii is already on the examined list, just leave it. */
2701 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2702 if (uhci_active_intr_info(ii))
2703 uhci_del_intr_info(ii);
2707 /* Deallocate request data structures */
2709 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2711 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2712 uhci_softc_t *sc = ii->sc;
2713 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2716 if (!(xfer->rqflags & URQ_REQUEST))
2717 panic("uhci_device_ctrl_done: not a request");
2720 if (!uhci_active_intr_info(ii))
2723 uhci_del_intr_info(ii); /* remove from active list */
2725 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2726 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2728 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2730 if (upipe->u.ctl.length != 0)
2731 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2733 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2736 /* Deallocate request data structures */
2738 uhci_device_bulk_done(usbd_xfer_handle xfer)
2740 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2741 uhci_softc_t *sc = ii->sc;
2742 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2744 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2745 xfer, ii, sc, upipe));
2747 if (!uhci_active_intr_info(ii))
2750 uhci_del_intr_info(ii); /* remove from active list */
2752 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2754 uhci_free_std_chain(sc, ii->stdstart, NULL);
2756 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2759 /* Add interrupt QH, called with vflock. */
2761 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2763 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2764 uhci_soft_qh_t *eqh;
2766 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2769 sqh->hlink = eqh->hlink;
2770 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2772 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2777 /* Remove interrupt QH. */
2779 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2781 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2782 uhci_soft_qh_t *pqh;
2784 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2786 /* See comment in uhci_remove_ctrl() */
2787 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2788 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2789 delay(UHCI_QH_REMOVE_DELAY);
2792 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2793 pqh->hlink = sqh->hlink;
2794 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2795 delay(UHCI_QH_REMOVE_DELAY);
2802 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2804 uhci_soft_qh_t *sqh;
2806 u_int bestbw, bw, bestoffs, offs;
2808 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2810 printf("uhci_setintr: 0 interval\n");
2811 return (USBD_INVAL);
2814 if (ival > UHCI_VFRAMELIST_COUNT)
2815 ival = UHCI_VFRAMELIST_COUNT;
2816 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2817 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2819 upipe->u.intr.npoll = npoll;
2821 kmalloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_INTWAIT);
2824 * Figure out which offset in the schedule that has most
2825 * bandwidth left over.
2827 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2828 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2829 for (bw = i = 0; i < npoll; i++)
2830 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2836 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2838 for(i = 0; i < npoll; i++) {
2839 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2841 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2842 sqh->pos = MOD(i * ival + bestoffs);
2847 /* Enter QHs into the controller data structures. */
2848 for(i = 0; i < npoll; i++)
2849 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2852 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2853 return (USBD_NORMAL_COMPLETION);
2856 /* Open a new pipe. */
2858 uhci_open(usbd_pipe_handle pipe)
2860 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2861 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2862 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2866 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2867 pipe, pipe->device->address,
2868 ed->bEndpointAddress, sc->sc_addr));
2870 upipe->aborting = 0;
2871 upipe->nexttoggle = 0;
2873 if (pipe->device->address == sc->sc_addr) {
2874 switch (ed->bEndpointAddress) {
2875 case USB_CONTROL_ENDPOINT:
2876 pipe->methods = &uhci_root_ctrl_methods;
2878 case UE_DIR_IN | UHCI_INTR_ENDPT:
2879 pipe->methods = &uhci_root_intr_methods;
2882 return (USBD_INVAL);
2885 switch (ed->bmAttributes & UE_XFERTYPE) {
2887 pipe->methods = &uhci_device_ctrl_methods;
2888 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2889 if (upipe->u.ctl.sqh == NULL)
2891 upipe->u.ctl.setup = uhci_alloc_std(sc);
2892 if (upipe->u.ctl.setup == NULL) {
2893 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2896 upipe->u.ctl.stat = uhci_alloc_std(sc);
2897 if (upipe->u.ctl.stat == NULL) {
2898 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2899 uhci_free_std(sc, upipe->u.ctl.setup);
2902 err = usb_allocmem(&sc->sc_bus,
2903 sizeof(usb_device_request_t),
2904 0, &upipe->u.ctl.reqdma);
2906 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2907 uhci_free_std(sc, upipe->u.ctl.setup);
2908 uhci_free_std(sc, upipe->u.ctl.stat);
2913 pipe->methods = &uhci_device_intr_methods;
2914 ival = pipe->interval;
2915 if (ival == USBD_DEFAULT_INTERVAL)
2916 ival = ed->bInterval;
2917 return (uhci_device_setintr(sc, upipe, ival));
2918 case UE_ISOCHRONOUS:
2919 pipe->methods = &uhci_device_isoc_methods;
2920 return (uhci_setup_isoc(pipe));
2922 pipe->methods = &uhci_device_bulk_methods;
2923 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2924 if (upipe->u.bulk.sqh == NULL)
2929 return (USBD_NORMAL_COMPLETION);
2932 return (USBD_NOMEM);
2936 * Data structures and routines to emulate the root hub.
2938 usb_device_descriptor_t uhci_devd = {
2939 USB_DEVICE_DESCRIPTOR_SIZE,
2940 UDESC_DEVICE, /* type */
2941 {0x00, 0x01}, /* USB version */
2942 UDCLASS_HUB, /* class */
2943 UDSUBCLASS_HUB, /* subclass */
2944 UDPROTO_FSHUB, /* protocol */
2945 64, /* max packet */
2946 {0},{0},{0x00,0x01}, /* device id */
2947 1,2,0, /* string indicies */
2948 1 /* # of configurations */
2951 usb_config_descriptor_t uhci_confd = {
2952 USB_CONFIG_DESCRIPTOR_SIZE,
2954 {USB_CONFIG_DESCRIPTOR_SIZE +
2955 USB_INTERFACE_DESCRIPTOR_SIZE +
2956 USB_ENDPOINT_DESCRIPTOR_SIZE},
2964 usb_interface_descriptor_t uhci_ifcd = {
2965 USB_INTERFACE_DESCRIPTOR_SIZE,
2976 usb_endpoint_descriptor_t uhci_endpd = {
2977 USB_ENDPOINT_DESCRIPTOR_SIZE,
2979 UE_DIR_IN | UHCI_INTR_ENDPT,
2985 usb_hub_descriptor_t uhci_hubd_piix = {
2986 USB_HUB_DESCRIPTOR_SIZE,
2989 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2990 50, /* power on to power good */
2992 { 0x00 }, /* both ports are removable */
2996 uhci_str(usb_string_descriptor_t *p, int l, char *s)
3002 p->bLength = 2 * strlen(s) + 2;
3005 p->bDescriptorType = UDESC_STRING;
3007 for (i = 0; s[i] && l > 1; i++, l -= 2)
3008 USETW2(p->bString[i], 0, s[i]);
3013 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3014 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3015 * should not be used by the USB subsystem. As we cannot issue a
3016 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3017 * will be enabled as part of the reset.
3019 * On the VT83C572, the port cannot be successfully enabled until the
3020 * outstanding "port enable change" and "connection status change"
3021 * events have been reset.
3024 uhci_portreset(uhci_softc_t *sc, int index)
3029 port = UHCI_PORTSC1;
3030 else if (index == 2)
3031 port = UHCI_PORTSC2;
3033 return (USBD_IOERROR);
3035 x = URWMASK(UREAD2(sc, port));
3036 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3038 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3040 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3041 index, UREAD2(sc, port)));
3043 x = URWMASK(UREAD2(sc, port));
3044 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3048 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3049 index, UREAD2(sc, port)));
3051 x = URWMASK(UREAD2(sc, port));
3052 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3054 for (lim = 10; --lim > 0;) {
3055 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3057 x = UREAD2(sc, port);
3059 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3062 if (!(x & UHCI_PORTSC_CCS)) {
3064 * No device is connected (or was disconnected
3065 * during reset). Consider the port reset.
3066 * The delay must be long enough to ensure on
3067 * the initial iteration that the device
3068 * connection will have been registered. 50ms
3069 * appears to be sufficient, but 20ms is not.
3071 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3076 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3078 * Port enabled changed and/or connection
3079 * status changed were set. Reset either or
3080 * both raised flags (by writing a 1 to that
3081 * bit), and wait again for state to settle.
3083 UWRITE2(sc, port, URWMASK(x) |
3084 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3088 if (x & UHCI_PORTSC_PE)
3089 /* Port is enabled */
3092 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3095 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3096 index, UREAD2(sc, port)));
3099 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3100 return (USBD_TIMEOUT);
3104 return (USBD_NORMAL_COMPLETION);
3108 * Simulate a hardware hub by handling all the necessary requests.
3111 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3115 /* Insert last in queue. */
3116 err = usb_insert_transfer(xfer);
3121 * Pipe isn't running (otherwise err would be USBD_INPROG),
3122 * so start it first.
3124 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3128 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3130 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3131 usb_device_request_t *req;
3134 int len, value, index, status, change, l, totlen = 0;
3135 usb_port_status_t ps;
3139 return (USBD_IOERROR);
3142 if (!(xfer->rqflags & URQ_REQUEST))
3143 panic("uhci_root_ctrl_transfer: not a request");
3145 req = &xfer->request;
3147 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3148 req->bmRequestType, req->bRequest));
3150 len = UGETW(req->wLength);
3151 value = UGETW(req->wValue);
3152 index = UGETW(req->wIndex);
3155 buf = KERNADDR(&xfer->dmabuf, 0);
3157 #define C(x,y) ((x) | ((y) << 8))
3158 switch(C(req->bRequest, req->bmRequestType)) {
3159 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3160 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3161 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3163 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3164 * for the integrated root hub.
3167 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3169 *(u_int8_t *)buf = sc->sc_conf;
3173 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3174 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3177 switch(value >> 8) {
3179 if ((value & 0xff) != 0) {
3183 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3184 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3185 memcpy(buf, &uhci_devd, l);
3188 if ((value & 0xff) != 0) {
3192 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3193 memcpy(buf, &uhci_confd, l);
3194 buf = (char *)buf + l;
3196 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3198 memcpy(buf, &uhci_ifcd, l);
3199 buf = (char *)buf + l;
3201 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3203 memcpy(buf, &uhci_endpd, l);
3206 *(u_int8_t *)buf = 0;
3208 switch (value & 0xff) {
3209 case 1: /* Vendor */
3210 totlen = uhci_str(buf, len, sc->sc_vendor);
3212 case 2: /* Product */
3213 totlen = uhci_str(buf, len, "UHCI root hub");
3222 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3224 *(u_int8_t *)buf = 0;
3228 case C(UR_GET_STATUS, UT_READ_DEVICE):
3230 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3234 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3235 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3237 USETW(((usb_status_t *)buf)->wStatus, 0);
3241 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3242 if (value >= USB_MAX_DEVICES) {
3246 sc->sc_addr = value;
3248 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3249 if (value != 0 && value != 1) {
3253 sc->sc_conf = value;
3255 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3257 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3258 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3259 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3262 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3264 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3267 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3269 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3270 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3271 "port=%d feature=%d\n",
3274 port = UHCI_PORTSC1;
3275 else if (index == 2)
3276 port = UHCI_PORTSC2;
3282 case UHF_PORT_ENABLE:
3283 x = URWMASK(UREAD2(sc, port));
3284 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3286 case UHF_PORT_SUSPEND:
3287 x = URWMASK(UREAD2(sc, port));
3288 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3290 case UHF_PORT_RESET:
3291 x = URWMASK(UREAD2(sc, port));
3292 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3294 case UHF_C_PORT_CONNECTION:
3295 x = URWMASK(UREAD2(sc, port));
3296 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3298 case UHF_C_PORT_ENABLE:
3299 x = URWMASK(UREAD2(sc, port));
3300 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3302 case UHF_C_PORT_OVER_CURRENT:
3303 x = URWMASK(UREAD2(sc, port));
3304 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3306 case UHF_C_PORT_RESET:
3308 err = USBD_NORMAL_COMPLETION;
3310 case UHF_PORT_CONNECTION:
3311 case UHF_PORT_OVER_CURRENT:
3312 case UHF_PORT_POWER:
3313 case UHF_PORT_LOW_SPEED:
3314 case UHF_C_PORT_SUSPEND:
3320 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3322 port = UHCI_PORTSC1;
3323 else if (index == 2)
3324 port = UHCI_PORTSC2;
3331 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3332 UHCI_PORTSC_LS_SHIFT;
3336 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3343 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3345 memcpy(buf, &uhci_hubd_piix, l);
3347 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3352 memset(buf, 0, len);
3355 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3357 port = UHCI_PORTSC1;
3358 else if (index == 2)
3359 port = UHCI_PORTSC2;
3368 x = UREAD2(sc, port);
3369 status = change = 0;
3370 if (x & UHCI_PORTSC_CCS)
3371 status |= UPS_CURRENT_CONNECT_STATUS;
3372 if (x & UHCI_PORTSC_CSC)
3373 change |= UPS_C_CONNECT_STATUS;
3374 if (x & UHCI_PORTSC_PE)
3375 status |= UPS_PORT_ENABLED;
3376 if (x & UHCI_PORTSC_POEDC)
3377 change |= UPS_C_PORT_ENABLED;
3378 if (x & UHCI_PORTSC_OCI)
3379 status |= UPS_OVERCURRENT_INDICATOR;
3380 if (x & UHCI_PORTSC_OCIC)
3381 change |= UPS_C_OVERCURRENT_INDICATOR;
3382 if (x & UHCI_PORTSC_SUSP)
3383 status |= UPS_SUSPEND;
3384 if (x & UHCI_PORTSC_LSDA)
3385 status |= UPS_LOW_SPEED;
3386 status |= UPS_PORT_POWER;
3388 change |= UPS_C_PORT_RESET;
3389 USETW(ps.wPortStatus, status);
3390 USETW(ps.wPortChange, change);
3391 l = min(len, sizeof ps);
3392 memcpy(buf, &ps, l);
3395 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3398 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3400 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3402 port = UHCI_PORTSC1;
3403 else if (index == 2)
3404 port = UHCI_PORTSC2;
3410 case UHF_PORT_ENABLE:
3411 x = URWMASK(UREAD2(sc, port));
3412 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3414 case UHF_PORT_SUSPEND:
3415 x = URWMASK(UREAD2(sc, port));
3416 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3418 case UHF_PORT_RESET:
3419 err = uhci_portreset(sc, index);
3421 case UHF_PORT_POWER:
3422 /* Pretend we turned on power */
3423 err = USBD_NORMAL_COMPLETION;
3425 case UHF_C_PORT_CONNECTION:
3426 case UHF_C_PORT_ENABLE:
3427 case UHF_C_PORT_OVER_CURRENT:
3428 case UHF_PORT_CONNECTION:
3429 case UHF_PORT_OVER_CURRENT:
3430 case UHF_PORT_LOW_SPEED:
3431 case UHF_C_PORT_SUSPEND:
3432 case UHF_C_PORT_RESET:
3442 xfer->actlen = totlen;
3443 err = USBD_NORMAL_COMPLETION;
3447 usb_transfer_complete(xfer);
3449 return (USBD_IN_PROGRESS);
3452 /* Abort a root control request. */
3454 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3456 /* Nothing to do, all transfers are synchronous. */
3459 /* Close the root pipe. */
3461 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3463 DPRINTF(("uhci_root_ctrl_close\n"));
3466 /* Abort a root interrupt request. */
3468 uhci_root_intr_abort(usbd_xfer_handle xfer)
3470 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3472 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3473 sc->sc_intr_xfer = NULL;
3475 if (xfer->pipe->intrxfer == xfer) {
3476 DPRINTF(("uhci_root_intr_abort: remove\n"));
3477 xfer->pipe->intrxfer = 0;
3479 xfer->status = USBD_CANCELLED;
3481 UXFER(xfer)->iinfo.isdone = 1;
3483 usb_transfer_complete(xfer);
3487 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3491 /* Insert last in queue. */
3492 err = usb_insert_transfer(xfer);
3497 * Pipe isn't running (otherwise err would be USBD_INPROG),
3498 * so start it first.
3500 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3503 /* Start a transfer on the root interrupt pipe */
3505 uhci_root_intr_start(usbd_xfer_handle xfer)
3507 usbd_pipe_handle pipe = xfer->pipe;
3508 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3510 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3511 xfer, xfer->length, xfer->flags));
3514 return (USBD_IOERROR);
3516 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3517 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3518 sc->sc_intr_xfer = xfer;
3519 return (USBD_IN_PROGRESS);
3522 /* Close the root interrupt pipe. */
3524 uhci_root_intr_close(usbd_pipe_handle pipe)
3526 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3528 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3529 sc->sc_intr_xfer = NULL;
3530 DPRINTF(("uhci_root_intr_close\n"));