1 /* $NetBSD: uhci.c,v 1.63 1999/11/12 00:34:57 augustss Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss@carlstedt.se) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * USB Universal Host Controller driver.
43 * Handles e.g. PIIX3 and PIIX4.
45 * Data sheets: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
46 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
47 * UHCI spec: http://www.intel.com/design/usb/uhci11d.pdf
48 * USB spec: http://www.usb.org/developers/data/usb11.pdf
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #if defined(__NetBSD__) || defined(__OpenBSD__)
56 #include <sys/device.h>
57 #include <sys/select.h>
58 #elif defined(__FreeBSD__)
59 #include <sys/module.h>
61 #include <machine/bus_pio.h>
62 #if defined(DIAGNOSTIC) && defined(__i386__)
63 #include <machine/cpu.h>
67 #include <sys/queue.h>
69 #include <machine/bus.h>
70 #include <machine/endian.h>
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdivar.h>
75 #include <dev/usb/usb_mem.h>
76 #include <dev/usb/usb_quirks.h>
78 #include <dev/usb/uhcireg.h>
79 #include <dev/usb/uhcivar.h>
81 #if defined(__FreeBSD__)
82 #include <machine/clock.h>
84 #define delay(d) DELAY(d)
87 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
89 #if defined(__OpenBSD__)
90 struct cfdriver uhci_cd = {
96 #define DPRINTF(x) if (uhcidebug) printf x
97 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
101 #define DPRINTFN(n,x)
105 * The UHCI controller is little endian, so on big endian machines
106 * the data strored in memory needs to be swapped.
108 #if BYTE_ORDER == BIG_ENDIAN
109 #define LE(x) (bswap32(x))
115 struct usbd_pipe pipe;
116 uhci_intr_info_t *iinfo;
118 /* Info needed for different pipe kinds. */
124 uhci_soft_td_t *setup, *stat;
130 uhci_soft_qh_t **qhs;
140 uhci_soft_td_t **stds;
147 * The uhci_intr_info free list can be global since they contain
148 * no dma specific data. The other free lists do.
150 LIST_HEAD(, uhci_intr_info) uhci_ii_free;
152 static void uhci_busreset __P((uhci_softc_t *));
153 #if defined(__NetBSD__) || defined(__OpenBSD__)
154 static void uhci_power __P((int, void *));
156 static usbd_status uhci_run __P((uhci_softc_t *, int run));
157 static uhci_soft_td_t *uhci_alloc_std __P((uhci_softc_t *));
158 static void uhci_free_std __P((uhci_softc_t *, uhci_soft_td_t *));
159 static uhci_soft_qh_t *uhci_alloc_sqh __P((uhci_softc_t *));
160 static void uhci_free_sqh __P((uhci_softc_t *, uhci_soft_qh_t *));
161 static uhci_intr_info_t *uhci_alloc_intr_info __P((uhci_softc_t *));
162 static void uhci_free_intr_info __P((uhci_intr_info_t *ii));
164 static void uhci_enter_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *,
165 uhci_intr_info_t *));
166 static void uhci_exit_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *));
169 static void uhci_free_std_chain __P((uhci_softc_t *,
170 uhci_soft_td_t *, uhci_soft_td_t *));
171 static usbd_status uhci_alloc_std_chain __P((struct uhci_pipe *,
172 uhci_softc_t *, int, int, int, usb_dma_t *,
173 uhci_soft_td_t **, uhci_soft_td_t **));
174 static void uhci_timo __P((void *));
175 static void uhci_waitintr __P((uhci_softc_t *,
177 static void uhci_check_intr __P((uhci_softc_t *,
178 uhci_intr_info_t *));
179 static void uhci_idone __P((uhci_intr_info_t *));
180 static void uhci_abort_req __P((usbd_xfer_handle,
181 usbd_status status));
182 static void uhci_abort_req_end __P((void *v));
183 static void uhci_timeout __P((void *));
184 static void uhci_lock_frames __P((uhci_softc_t *));
185 static void uhci_unlock_frames __P((uhci_softc_t *));
186 static void uhci_add_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
187 static void uhci_add_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
188 static void uhci_remove_ctrl __P((uhci_softc_t *,uhci_soft_qh_t *));
189 static void uhci_remove_bulk __P((uhci_softc_t *,uhci_soft_qh_t *));
190 static int uhci_str __P((usb_string_descriptor_t *, int, char *));
191 static usbd_status uhci_setup_isoc __P((usbd_pipe_handle pipe));
192 static void uhci_device_isoc_enter __P((usbd_xfer_handle));
194 static usbd_status uhci_allocm __P((struct usbd_bus *, usb_dma_t *,
196 static void uhci_freem __P((struct usbd_bus *, usb_dma_t *));
198 static usbd_status uhci_device_ctrl_transfer __P((usbd_xfer_handle));
199 static usbd_status uhci_device_ctrl_start __P((usbd_xfer_handle));
200 static void uhci_device_ctrl_abort __P((usbd_xfer_handle));
201 static void uhci_device_ctrl_close __P((usbd_pipe_handle));
202 static void uhci_device_ctrl_done __P((usbd_xfer_handle));
204 static usbd_status uhci_device_intr_transfer __P((usbd_xfer_handle));
205 static usbd_status uhci_device_intr_start __P((usbd_xfer_handle));
206 static void uhci_device_intr_abort __P((usbd_xfer_handle));
207 static void uhci_device_intr_close __P((usbd_pipe_handle));
208 static void uhci_device_intr_done __P((usbd_xfer_handle));
210 static usbd_status uhci_device_bulk_transfer __P((usbd_xfer_handle));
211 static usbd_status uhci_device_bulk_start __P((usbd_xfer_handle));
212 static void uhci_device_bulk_abort __P((usbd_xfer_handle));
213 static void uhci_device_bulk_close __P((usbd_pipe_handle));
214 static void uhci_device_bulk_done __P((usbd_xfer_handle));
216 static usbd_status uhci_device_isoc_transfer __P((usbd_xfer_handle));
217 static usbd_status uhci_device_isoc_start __P((usbd_xfer_handle));
218 static void uhci_device_isoc_abort __P((usbd_xfer_handle));
219 static void uhci_device_isoc_close __P((usbd_pipe_handle));
220 static void uhci_device_isoc_done __P((usbd_xfer_handle));
222 static usbd_status uhci_root_ctrl_transfer __P((usbd_xfer_handle));
223 static usbd_status uhci_root_ctrl_start __P((usbd_xfer_handle));
224 static void uhci_root_ctrl_abort __P((usbd_xfer_handle));
225 static void uhci_root_ctrl_close __P((usbd_pipe_handle));
227 static usbd_status uhci_root_intr_transfer __P((usbd_xfer_handle));
228 static usbd_status uhci_root_intr_start __P((usbd_xfer_handle));
229 static void uhci_root_intr_abort __P((usbd_xfer_handle));
230 static void uhci_root_intr_close __P((usbd_pipe_handle));
231 static void uhci_root_intr_done __P((usbd_xfer_handle));
233 static usbd_status uhci_open __P((usbd_pipe_handle));
234 static void uhci_poll __P((struct usbd_bus *));
236 static usbd_status uhci_device_request __P((usbd_xfer_handle xfer));
238 static void uhci_add_intr __P((uhci_softc_t *, int,
240 static void uhci_remove_intr __P((uhci_softc_t *, int,
242 static usbd_status uhci_device_setintr __P((uhci_softc_t *sc,
243 struct uhci_pipe *pipe, int ival));
245 static void uhci_device_clear_toggle __P((usbd_pipe_handle pipe));
246 static void uhci_noop __P((usbd_pipe_handle pipe));
249 static void uhci_dumpregs __P((uhci_softc_t *));
250 static void uhci_dump_qhs __P((uhci_soft_qh_t *));
251 static void uhci_dump_qh __P((uhci_soft_qh_t *));
252 static void uhci_dump_tds __P((uhci_soft_td_t *));
253 static void uhci_dump_td __P((uhci_soft_td_t *));
256 #define UWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
257 #define UWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
258 #define UREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
259 #define UREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
260 #define UREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
262 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
263 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
265 #define UHCI_RESET_TIMEOUT 100 /* reset timeout */
267 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
269 #define UHCI_INTR_ENDPT 1
271 struct usbd_bus_methods uhci_bus_methods = {
278 struct usbd_pipe_methods uhci_root_ctrl_methods = {
279 uhci_root_ctrl_transfer,
280 uhci_root_ctrl_start,
281 uhci_root_ctrl_abort,
282 uhci_root_ctrl_close,
287 struct usbd_pipe_methods uhci_root_intr_methods = {
288 uhci_root_intr_transfer,
289 uhci_root_intr_start,
290 uhci_root_intr_abort,
291 uhci_root_intr_close,
296 struct usbd_pipe_methods uhci_device_ctrl_methods = {
297 uhci_device_ctrl_transfer,
298 uhci_device_ctrl_start,
299 uhci_device_ctrl_abort,
300 uhci_device_ctrl_close,
302 uhci_device_ctrl_done,
305 struct usbd_pipe_methods uhci_device_intr_methods = {
306 uhci_device_intr_transfer,
307 uhci_device_intr_start,
308 uhci_device_intr_abort,
309 uhci_device_intr_close,
310 uhci_device_clear_toggle,
311 uhci_device_intr_done,
314 struct usbd_pipe_methods uhci_device_bulk_methods = {
315 uhci_device_bulk_transfer,
316 uhci_device_bulk_start,
317 uhci_device_bulk_abort,
318 uhci_device_bulk_close,
319 uhci_device_clear_toggle,
320 uhci_device_bulk_done,
323 struct usbd_pipe_methods uhci_device_isoc_methods = {
324 uhci_device_isoc_transfer,
325 uhci_device_isoc_start,
326 uhci_device_isoc_abort,
327 uhci_device_isoc_close,
329 uhci_device_isoc_done,
336 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
337 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
338 UHCICMD(sc, 0); /* do nothing */
347 uhci_soft_qh_t *csqh, *bsqh, *sqh;
350 DPRINTFN(1,("uhci_init: start\n"));
357 uhci_run(sc, 0); /* stop the controller */
358 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
362 /* Allocate and initialize real frame array. */
363 err = usb_allocmem(&sc->sc_bus,
364 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
365 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
368 sc->sc_pframes = KERNADDR(&sc->sc_dma);
369 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
370 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma)); /* set frame list*/
372 /* Allocate the dummy QH where bulk traffic will be queued. */
373 bsqh = uhci_alloc_sqh(sc);
376 bsqh->qh.qh_hlink = LE(UHCI_PTR_T); /* end of QH chain */
377 bsqh->qh.qh_elink = LE(UHCI_PTR_T);
378 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
380 /* Allocate the dummy QH where control traffic will be queued. */
381 csqh = uhci_alloc_sqh(sc);
385 csqh->qh.qh_hlink = LE(bsqh->physaddr | UHCI_PTR_Q);
386 csqh->qh.qh_elink = LE(UHCI_PTR_T);
387 sc->sc_ctl_start = sc->sc_ctl_end = csqh;
390 * Make all (virtual) frame list pointers point to the interrupt
391 * queue heads and the interrupt queue heads at the control
392 * queue head and point the physical frame list to the virtual.
394 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
395 std = uhci_alloc_std(sc);
396 sqh = uhci_alloc_sqh(sc);
397 if (std == NULL || sqh == NULL)
400 std->td.td_link = LE(sqh->physaddr | UHCI_PTR_Q);
401 std->td.td_status = LE(UHCI_TD_IOS); /* iso, inactive */
402 std->td.td_token = LE(0);
403 std->td.td_buffer = LE(0);
405 sqh->qh.qh_hlink = LE(csqh->physaddr | UHCI_PTR_Q);
407 sqh->qh.qh_elink = LE(UHCI_PTR_T);
408 sc->sc_vframes[i].htd = std;
409 sc->sc_vframes[i].etd = std;
410 sc->sc_vframes[i].hqh = sqh;
411 sc->sc_vframes[i].eqh = sqh;
413 j < UHCI_FRAMELIST_COUNT;
414 j += UHCI_VFRAMELIST_COUNT)
415 sc->sc_pframes[j] = LE(std->physaddr);
418 LIST_INIT(&sc->sc_intrhead);
420 /* Set up the bus struct. */
421 sc->sc_bus.methods = &uhci_bus_methods;
422 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
424 sc->sc_suspend = PWR_RESUME;
425 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
427 DPRINTFN(1,("uhci_init: enabling\n"));
428 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
429 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
431 return (uhci_run(sc, 1)); /* and here we go... */
434 #if defined(__NetBSD__) || defined(__OpenBSD__)
436 uhci_activate(self, act)
440 struct uhci_softc *sc = (struct uhci_softc *)self;
448 case DVACT_DEACTIVATE:
449 if (sc->sc_child != NULL)
450 rv = config_deactivate(sc->sc_child);
457 uhci_detach(sc, flags)
458 struct uhci_softc *sc;
463 if (sc->sc_child != NULL)
464 rv = config_detach(sc->sc_child, flags);
469 powerhook_disestablish(sc->sc_powerhook);
470 /* free data structures XXX */
477 uhci_allocm(bus, dma, size)
478 struct usbd_bus *bus;
482 return (usb_allocmem(&((struct uhci_softc *)bus)->sc_bus, size, 0,
488 struct usbd_bus *bus;
491 usb_freemem(&((struct uhci_softc *)bus)->sc_bus, dma);
494 #if defined(__NetBSD__)
496 * Handle suspend/resume.
498 * We need to switch to polling mode here, because this routine is
499 * called from an intterupt context. This is all right since we
500 * are almost suspended anyway.
507 uhci_softc_t *sc = v;
512 cmd = UREAD2(sc, UHCI_CMD);
514 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
515 sc, why, sc->sc_suspend, cmd));
517 if (why != PWR_RESUME) {
523 usb_untimeout(uhci_timo, sc->sc_has_timo,
524 sc->sc_has_timo->timo_handle);
525 sc->sc_bus.use_polling++;
526 uhci_run(sc, 0); /* stop the controller */
527 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
528 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
529 sc->sc_suspend = why;
530 sc->sc_bus.use_polling--;
531 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
534 * XXX We should really do much more here in case the
535 * controller registers have been lost and BIOS has
539 if (sc->sc_suspend == PWR_RESUME)
540 printf("uhci_power: weird, resume without suspend.\n");
542 sc->sc_bus.use_polling++;
543 sc->sc_suspend = why;
544 if (cmd & UHCI_CMD_RS)
545 uhci_run(sc, 0); /* in case BIOS has started it */
546 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
547 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
548 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
549 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
550 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
551 uhci_run(sc, 1); /* and start traffic again */
552 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
553 sc->sc_bus.use_polling--;
555 usb_timeout(uhci_timo, sc->sc_has_timo,
556 sc->sc_ival, sc->sc_has_timo->timo_handle);
564 #endif /* defined(__NetBSD__) */
571 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
572 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
573 USBDEVNAME(sc->sc_bus.bdev),
574 UREAD2(sc, UHCI_CMD),
575 UREAD2(sc, UHCI_STS),
576 UREAD2(sc, UHCI_INTR),
577 UREAD2(sc, UHCI_FRNUM),
578 UREAD4(sc, UHCI_FLBASEADDR),
579 UREAD1(sc, UHCI_SOF),
580 UREAD2(sc, UHCI_PORTSC1),
581 UREAD2(sc, UHCI_PORTSC2)));
588 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
589 "token=0x%08lx buffer=0x%08lx\n",
590 p, (long)p->physaddr,
591 (long)LE(p->td.td_link),
592 (long)LE(p->td.td_status),
593 (long)LE(p->td.td_token),
594 (long)LE(p->td.td_buffer)));
595 DPRINTFN(-1,(" %b %b,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
597 (int)LE(p->td.td_link),
599 (int)LE(p->td.td_status),
600 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
601 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
602 UHCI_TD_GET_ERRCNT(LE(p->td.td_status)),
603 UHCI_TD_GET_ACTLEN(LE(p->td.td_status)),
604 UHCI_TD_GET_PID(LE(p->td.td_token)),
605 UHCI_TD_GET_DEVADDR(LE(p->td.td_token)),
606 UHCI_TD_GET_ENDPT(LE(p->td.td_token)),
607 UHCI_TD_GET_DT(LE(p->td.td_token)),
608 UHCI_TD_GET_MAXLEN(LE(p->td.td_token))));
615 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
616 (int)sqh->physaddr, LE(sqh->qh.qh_hlink), LE(sqh->qh.qh_elink)));
624 uhci_softc_t *sc = uhci;
627 printf("intrs=%d\n", sc->sc_bus.no_intrs);
628 printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);
629 uhci_dump_qh(sc->sc_ctl_start->qh.hlink);
640 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
641 * Traverses sideways first, then down.
651 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
655 if (sqh->hlink != NULL && !(sqh->qh.qh_hlink & UHCI_PTR_T))
656 uhci_dump_qhs(sqh->hlink);
658 DPRINTF(("No QH\n"));
660 if (sqh->elink != NULL && !(sqh->qh.qh_elink & UHCI_PTR_T))
661 uhci_dump_tds(sqh->elink);
663 DPRINTF(("No TD\n"));
672 for(td = std; td != NULL; td = td->link.std) {
675 /* Check whether the link pointer in this TD marks
676 * the link pointer as end of queue. This avoids
677 * printing the free list in case the queue/TD has
678 * already been moved there (seatbelt).
680 if (td->td.td_link & UHCI_PTR_T ||
688 * This routine is executed periodically and simulates interrupts
689 * from the root controller interrupt pipe for port status change.
695 usbd_xfer_handle xfer = addr;
696 usbd_pipe_handle pipe = xfer->pipe;
697 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
701 DPRINTFN(20, ("uhci_timo\n"));
703 usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
705 p = KERNADDR(&xfer->dmabuf);
707 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
709 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
712 /* No change, try again in a while */
716 xfer->status = USBD_NORMAL_COMPLETION;
719 xfer->device->bus->intr_context++;
720 usb_transfer_complete(xfer);
721 xfer->device->bus->intr_context--;
726 uhci_root_intr_done(xfer)
727 usbd_xfer_handle xfer;
738 while (sc->sc_vflock) {
739 sc->sc_vflock |= UHCI_WANT_LOCK;
740 tsleep(&sc->sc_vflock, PRIBIO, "uhcqhl", 0);
742 sc->sc_vflock = UHCI_HAS_LOCK;
747 uhci_unlock_frames(sc)
752 sc->sc_vflock &= ~UHCI_HAS_LOCK;
753 if (sc->sc_vflock & UHCI_WANT_LOCK)
754 wakeup(&sc->sc_vflock);
759 * Allocate an interrupt information struct. A free list is kept
760 * for fast allocation.
763 uhci_alloc_intr_info(sc)
766 uhci_intr_info_t *ii;
768 ii = LIST_FIRST(&uhci_ii_free);
770 LIST_REMOVE(ii, list);
772 ii = malloc(sizeof(uhci_intr_info_t), M_USBHC, M_NOWAIT);
775 #if defined(__FreeBSD__)
776 callout_handle_init(&ii->timeout_handle);
783 uhci_free_intr_info(ii)
784 uhci_intr_info_t *ii;
786 LIST_INSERT_HEAD(&uhci_ii_free, ii, list); /* and put on free list */
789 /* Add control QH, called at splusb(). */
791 uhci_add_ctrl(sc, sqh)
799 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
800 eqh = sc->sc_ctl_end;
801 sqh->hlink = eqh->hlink;
802 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
804 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
805 sc->sc_ctl_end = sqh;
808 /* Remove control QH, called at splusb(). */
810 uhci_remove_ctrl(sc, sqh)
818 DPRINTFN(10, ("uhci_remove_ctrl: sqh=%p\n", sqh));
819 for (pqh = sc->sc_ctl_start; pqh->hlink != sqh; pqh=pqh->hlink)
820 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
821 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
822 printf("uhci_remove_ctrl: QH not found\n");
828 pqh->hlink = sqh->hlink;
829 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
830 if (sc->sc_ctl_end == sqh)
831 sc->sc_ctl_end = pqh;
834 /* Add bulk QH, called at splusb(). */
836 uhci_add_bulk(sc, sqh)
844 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
845 eqh = sc->sc_bulk_end;
846 sqh->hlink = eqh->hlink;
847 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
849 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
850 sc->sc_bulk_end = sqh;
853 /* Remove bulk QH, called at splusb(). */
855 uhci_remove_bulk(sc, sqh)
863 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
864 for (pqh = sc->sc_bulk_start; pqh->hlink != sqh; pqh = pqh->hlink)
865 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
866 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
867 printf("uhci_remove_bulk: QH not found\n");
873 pqh->hlink = sqh->hlink;
874 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
875 if (sc->sc_bulk_end == sqh)
876 sc->sc_bulk_end = pqh;
883 uhci_softc_t *sc = arg;
886 uhci_intr_info_t *ii;
889 if (uhcidebug > 15) {
890 DPRINTF(("%s: uhci_intr\n", USBDEVNAME(sc->sc_bus.bdev)));
895 status = UREAD2(sc, UHCI_STS);
896 if (status == 0) /* The interrupt was not for us. */
899 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
900 if (sc->sc_suspend != PWR_RESUME)
901 printf("uhci_intr: suspended sts=0x%x\n", status);
905 if (status & UHCI_STS_USBINT)
906 ack |= UHCI_STS_USBINT;
907 if (status & UHCI_STS_USBEI)
908 ack |= UHCI_STS_USBEI;
909 if (status & UHCI_STS_RD) {
911 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
913 if (status & UHCI_STS_HSE) {
915 printf("%s: host controller process error\n",
916 USBDEVNAME(sc->sc_bus.bdev));
918 if (status & UHCI_STS_HCPE) {
919 ack |= UHCI_STS_HCPE;
920 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
922 if (status & UHCI_STS_HCH) {
923 /* no acknowledge needed */
924 printf("%s: host controller halted\n",
925 USBDEVNAME(sc->sc_bus.bdev));
928 if (ack) /* acknowledge the ints */
929 UWRITE2(sc, UHCI_STS, ack);
930 else /* nothing to acknowledge */
933 sc->sc_bus.intr_context++;
934 sc->sc_bus.no_intrs++;
937 * Interrupts on UHCI really suck. When the host controller
938 * interrupts because a transfer is completed there is no
939 * way of knowing which transfer it was. You can scan down
940 * the TDs and QHs of the previous frame to limit the search,
941 * but that assumes that the interrupt was not delayed by more
942 * than 1 ms, which may not always be true (e.g. after debug
943 * output on a slow console).
944 * We scan all interrupt descriptors to see if any have
947 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
948 uhci_check_intr(sc, ii);
950 DPRINTFN(10, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
952 sc->sc_bus.intr_context--;
957 /* Check for an interrupt. */
959 uhci_check_intr(sc, ii)
961 uhci_intr_info_t *ii;
963 uhci_soft_td_t *std, *lstd;
966 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
969 printf("uhci_check_intr: no ii? %p\n", ii);
973 if (ii->stdstart == NULL)
978 printf("uhci_check_intr: std==0\n");
983 * If the last TD is still active we need to check whether there
984 * is a an error somewhere in the middle, or whether there was a
985 * short packet (SPD and not ACTIVE).
987 if (LE(lstd->td.td_status) & UHCI_TD_ACTIVE) {
988 DPRINTFN(15, ("uhci_check_intr: active ii=%p\n", ii));
989 for (std = ii->stdstart; std != lstd; std = std->link.std) {
990 status = LE(std->td.td_status);
991 if ((status & UHCI_TD_STALLED) ||
992 (status & (UHCI_TD_SPD | UHCI_TD_ACTIVE)) ==
996 DPRINTFN(15, ("uhci_check_intr: ii=%p std=%p still active\n",
1001 usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
1005 /* Called at splusb() */
1008 uhci_intr_info_t *ii;
1010 usbd_xfer_handle xfer = ii->xfer;
1011 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1012 uhci_soft_td_t *std;
1013 u_int32_t status = 0, nstatus;
1021 printf("uhci_idone: ii=%p is done!\n", ii);
1029 if (xfer->status == USBD_CANCELLED ||
1030 xfer->status == USBD_TIMEOUT) {
1031 DPRINTF(("uhci_idone: aborted xfer=%p\n", xfer));
1035 if (xfer->nframes != 0) {
1036 /* Isoc transfer, do things differently. */
1037 uhci_soft_td_t **stds = upipe->u.iso.stds;
1040 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1042 nframes = xfer->nframes;
1044 n = xfer->hcprivint;
1045 for (i = 0; i < nframes; i++) {
1048 if (uhcidebug > 5) {
1049 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1053 if (++n >= UHCI_VFRAMELIST_COUNT)
1055 status = LE(std->td.td_status);
1056 actlen += UHCI_TD_GET_ACTLEN(status);
1058 upipe->u.iso.inuse -= nframes;
1059 xfer->actlen = actlen;
1060 xfer->status = USBD_NORMAL_COMPLETION;
1062 usb_transfer_complete(xfer);
1067 DPRINTFN(10, ("uhci_idone: ii=%p ready\n", ii));
1069 uhci_dump_tds(ii->stdstart);
1072 /* The transfer is done, compute actual length and status. */
1074 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1075 nstatus = LE(std->td.td_status);
1076 if (nstatus & UHCI_TD_ACTIVE)
1080 if (UHCI_TD_GET_PID(LE(std->td.td_token)) != UHCI_TD_PID_SETUP)
1081 actlen += UHCI_TD_GET_ACTLEN(status);
1083 /* If there are left over TDs we need to update the toggle. */
1085 upipe->nexttoggle = UHCI_TD_GET_DT(LE(std->td.td_token));
1087 status &= UHCI_TD_ERROR;
1088 DPRINTFN(10, ("uhci_check_intr: actlen=%d, status=0x%x\n",
1090 xfer->actlen = actlen;
1092 DPRINTFN((status&UHCI_TD_STALLED)*10,
1093 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1095 xfer->pipe->device->address,
1096 xfer->pipe->endpoint->edesc->bEndpointAddress,
1098 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
1099 "STALLED\30ACTIVE"));
1100 if (status == UHCI_TD_STALLED)
1101 xfer->status = USBD_STALLED;
1103 xfer->status = USBD_IOERROR; /* more info XXX */
1105 xfer->status = USBD_NORMAL_COMPLETION;
1108 usb_transfer_complete(xfer);
1112 * Called when a request does not complete.
1118 uhci_intr_info_t *ii = addr;
1120 DPRINTF(("uhci_timeout: ii=%p\n", ii));
1124 uhci_dump_tds(ii->stdstart);
1127 ii->xfer->device->bus->intr_context++;
1128 uhci_abort_req(ii->xfer, USBD_TIMEOUT);
1129 ii->xfer->device->bus->intr_context--;
1133 * Wait here until controller claims to have an interrupt.
1134 * Then call uhci_intr and return. Use timeout to avoid waiting
1136 * Only used during boot when interrupts are not enabled yet.
1139 uhci_waitintr(sc, xfer)
1141 usbd_xfer_handle xfer;
1143 int timo = xfer->timeout;
1144 uhci_intr_info_t *ii;
1146 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1148 xfer->status = USBD_IN_PROGRESS;
1149 for (; timo >= 0; timo--) {
1150 usb_delay_ms(&sc->sc_bus, 1);
1151 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1152 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
1154 if (xfer->status != USBD_IN_PROGRESS)
1160 DPRINTF(("uhci_waitintr: timeout\n"));
1161 for (ii = LIST_FIRST(&sc->sc_intrhead);
1162 ii != NULL && ii->xfer != xfer;
1163 ii = LIST_NEXT(ii, list))
1167 panic("uhci_waitintr: lost intr_info\n");
1174 struct usbd_bus *bus;
1176 uhci_softc_t *sc = (uhci_softc_t *)bus;
1178 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
1187 uhci_softc_t *sc = p;
1190 UHCICMD(sc, UHCI_CMD_HCRESET);
1191 /* The reset bit goes low when the controller is done. */
1192 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1193 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1195 if (n >= UHCI_RESET_TIMEOUT)
1196 printf("%s: controller did not reset\n",
1197 USBDEVNAME(sc->sc_bus.bdev));
1210 DPRINTF(("uhci_run: setting run=%d\n", run));
1211 UHCICMD(sc, run ? UHCI_CMD_RS : 0);
1212 for(n = 0; n < 10; n++) {
1213 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1214 /* return when we've entered the state we want */
1215 if (run == running) {
1217 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1218 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1219 return (USBD_NORMAL_COMPLETION);
1221 usb_delay_ms(&sc->sc_bus, 1);
1224 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1225 run ? "start" : "stop");
1226 return (USBD_IOERROR);
1230 * Memory management routines.
1231 * uhci_alloc_std allocates TDs
1232 * uhci_alloc_sqh allocates QHs
1233 * These two routines do their own free list management,
1234 * partly for speed, partly because allocating DMAable memory
1235 * has page size granularaity so much memory would be wasted if
1236 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1243 uhci_soft_td_t *std;
1248 if (sc->sc_freetds == NULL) {
1249 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1250 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1251 UHCI_TD_ALIGN, &dma);
1254 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1255 offs = i * UHCI_STD_SIZE;
1256 std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) + offs);
1257 std->physaddr = DMAADDR(&dma) + offs;
1258 std->link.std = sc->sc_freetds;
1259 sc->sc_freetds = std;
1262 std = sc->sc_freetds;
1263 sc->sc_freetds = std->link.std;
1264 memset(&std->td, 0, sizeof(uhci_td_t));
1269 uhci_free_std(sc, std)
1271 uhci_soft_td_t *std;
1274 #define TD_IS_FREE 0x12345678
1275 if (std->td.td_token == LE(TD_IS_FREE)) {
1276 printf("uhci_free_std: freeing free TD %p\n", std);
1279 std->td.td_token = LE(TD_IS_FREE);
1281 std->link.std = sc->sc_freetds;
1282 sc->sc_freetds = std;
1289 uhci_soft_qh_t *sqh;
1294 if (sc->sc_freeqhs == NULL) {
1295 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1296 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1297 UHCI_QH_ALIGN, &dma);
1300 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1301 offs = i * UHCI_SQH_SIZE;
1302 sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
1303 sqh->physaddr = DMAADDR(&dma) + offs;
1304 sqh->hlink = sc->sc_freeqhs;
1305 sc->sc_freeqhs = sqh;
1308 sqh = sc->sc_freeqhs;
1309 sc->sc_freeqhs = sqh->hlink;
1310 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1315 uhci_free_sqh(sc, sqh)
1317 uhci_soft_qh_t *sqh;
1319 sqh->hlink = sc->sc_freeqhs;
1320 sc->sc_freeqhs = sqh;
1325 * Enter a list of transfers onto a control queue.
1326 * Called at splusb()
1329 uhci_enter_ctl_q(sc, sqh, ii)
1331 uhci_soft_qh_t *sqh;
1332 uhci_intr_info_t *ii;
1334 DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
1340 uhci_free_std_chain(sc, std, stdend)
1342 uhci_soft_td_t *std;
1343 uhci_soft_td_t *stdend;
1347 for (; std != stdend; std = p) {
1349 uhci_free_std(sc, std);
1354 uhci_alloc_std_chain(upipe, sc, len, rd, shortok, dma, sp, ep)
1355 struct uhci_pipe *upipe;
1357 int len, rd, shortok;
1359 uhci_soft_td_t **sp, **ep;
1361 uhci_soft_td_t *p, *lastp;
1362 uhci_physaddr_t lastlink;
1363 int i, ntd, l, tog, maxp;
1365 int addr = upipe->pipe.device->address;
1366 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1368 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
1369 "shortok=%d\n", addr, UE_GET_ADDR(endpt), len,
1370 upipe->pipe.device->lowspeed, shortok));
1373 DPRINTFN(-1,("uhci_alloc_std_chain: len=0\n"));
1374 return (USBD_NORMAL_COMPLETION);
1376 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1378 printf("uhci_alloc_std_chain: maxp=0\n");
1379 return (USBD_INVAL);
1381 ntd = (len + maxp - 1) / maxp;
1382 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1383 tog = upipe->nexttoggle;
1386 upipe->nexttoggle = tog ^ 1;
1388 lastlink = UHCI_PTR_T;
1390 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1391 if (upipe->pipe.device->lowspeed)
1392 status |= UHCI_TD_LS;
1394 status |= UHCI_TD_SPD;
1395 for (i = ntd; i >= 0; i--) {
1396 p = uhci_alloc_std(sc);
1398 uhci_free_std_chain(sc, lastp, 0);
1399 return (USBD_NOMEM);
1401 p->link.std = lastp;
1402 p->td.td_link = LE(lastlink);
1404 lastlink = p->physaddr;
1405 p->td.td_status = LE(status);
1409 if (l == 0) l = maxp;
1414 LE(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1415 UHCI_TD_OUT(l, endpt, addr, tog));
1416 p->td.td_buffer = LE(DMAADDR(dma) + i * maxp);
1420 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1421 upipe->nexttoggle));
1422 return (USBD_NORMAL_COMPLETION);
1426 uhci_device_clear_toggle(pipe)
1427 usbd_pipe_handle pipe;
1429 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1430 upipe->nexttoggle = 0;
1435 usbd_pipe_handle pipe;
1440 uhci_device_bulk_transfer(xfer)
1441 usbd_xfer_handle xfer;
1445 /* Insert last in queue. */
1446 err = usb_insert_transfer(xfer);
1450 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1453 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1457 uhci_device_bulk_start(xfer)
1458 usbd_xfer_handle xfer;
1460 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1461 usbd_device_handle dev = upipe->pipe.device;
1462 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1463 uhci_intr_info_t *ii = upipe->iinfo;
1464 uhci_soft_td_t *data, *dataend;
1465 uhci_soft_qh_t *sqh;
1467 int len, isread, endpt;
1470 DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
1471 xfer, xfer->length, xfer->flags));
1474 if (xfer->rqflags & URQ_REQUEST)
1475 panic("uhci_device_bulk_transfer: a request\n");
1479 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
1480 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1481 sqh = upipe->u.bulk.sqh;
1483 upipe->u.bulk.isread = isread;
1484 upipe->u.bulk.length = len;
1486 err = uhci_alloc_std_chain(upipe, sc, len, isread,
1487 xfer->flags & USBD_SHORT_XFER_OK,
1488 &xfer->dmabuf, &data, &dataend);
1491 dataend->td.td_status |= LE(UHCI_TD_IOC);
1494 if (uhcidebug > 8) {
1495 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1496 uhci_dump_tds(data);
1500 /* Set up interrupt info. */
1502 ii->stdstart = data;
1503 ii->stdend = dataend;
1504 #if defined(__FreeBSD__)
1505 callout_handle_init(&ii->timeout_handle);
1512 sqh->qh.qh_elink = LE(data->physaddr);
1513 sqh->intr_info = ii;
1516 uhci_add_bulk(sc, sqh);
1517 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1519 if (xfer->timeout && !sc->sc_bus.use_polling) {
1520 usb_timeout(uhci_timeout, ii, MS_TO_TICKS(xfer->timeout),
1521 ii->timeout_handle);
1526 if (uhcidebug > 10) {
1527 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1528 uhci_dump_tds(data);
1532 if (sc->sc_bus.use_polling)
1533 uhci_waitintr(sc, xfer);
1535 return (USBD_IN_PROGRESS);
1538 /* Abort a device bulk request. */
1540 uhci_device_bulk_abort(xfer)
1541 usbd_xfer_handle xfer;
1543 DPRINTF(("uhci_device_bulk_abort:\n"));
1544 uhci_abort_req(xfer, USBD_CANCELLED);
1548 uhci_abort_req(xfer, status)
1549 usbd_xfer_handle xfer;
1552 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1553 uhci_intr_info_t *ii = upipe->iinfo;
1554 uhci_soft_td_t *std;
1556 /* Make interrupt routine ignore it, */
1557 xfer->status = status;
1559 /* don't timeout, */
1560 usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
1562 /* make hardware ignore it, */
1563 for (std = ii->stdstart; std != 0; std = std->link.std)
1564 std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1568 /* make sure hardware has completed, */
1569 if (xfer->device->bus->intr_context) {
1570 /* We have no process context, so we can't use tsleep(). */
1571 timeout(uhci_abort_req_end, xfer, hz / USB_FRAMES_PER_SECOND);
1573 #if defined(DIAGNOSTIC) && defined(__i386__)
1574 KASSERT(intr_nesting_level == 0,
1575 ("ohci_abort_req in interrupt context"));
1577 usb_delay_ms(xfer->pipe->device->bus, 1);
1578 /* and call final part of interrupt handler. */
1579 uhci_abort_req_end(xfer);
1584 uhci_abort_req_end(v)
1587 usbd_xfer_handle xfer = v;
1591 usb_transfer_complete(xfer);
1595 /* Close a device bulk pipe. */
1597 uhci_device_bulk_close(pipe)
1598 usbd_pipe_handle pipe;
1600 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1601 usbd_device_handle dev = upipe->pipe.device;
1602 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1604 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1605 uhci_free_intr_info(upipe->iinfo);
1606 /* XXX free other resources */
1610 uhci_device_ctrl_transfer(xfer)
1611 usbd_xfer_handle xfer;
1615 /* Insert last in queue. */
1616 err = usb_insert_transfer(xfer);
1620 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1623 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1627 uhci_device_ctrl_start(xfer)
1628 usbd_xfer_handle xfer;
1630 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1634 if (!(xfer->rqflags & URQ_REQUEST))
1635 panic("uhci_device_ctrl_transfer: not a request\n");
1638 err = uhci_device_request(xfer);
1642 if (sc->sc_bus.use_polling)
1643 uhci_waitintr(sc, xfer);
1644 return (USBD_IN_PROGRESS);
1648 uhci_device_intr_transfer(xfer)
1649 usbd_xfer_handle xfer;
1653 /* Insert last in queue. */
1654 err = usb_insert_transfer(xfer);
1658 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1661 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1665 uhci_device_intr_start(xfer)
1666 usbd_xfer_handle xfer;
1668 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1669 usbd_device_handle dev = upipe->pipe.device;
1670 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1671 uhci_intr_info_t *ii = upipe->iinfo;
1672 uhci_soft_td_t *data, *dataend;
1673 uhci_soft_qh_t *sqh;
1677 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
1678 xfer, xfer->length, xfer->flags));
1681 if (xfer->rqflags & URQ_REQUEST)
1682 panic("uhci_device_intr_transfer: a request\n");
1685 err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
1686 xfer->flags & USBD_SHORT_XFER_OK,
1687 &xfer->dmabuf, &data, &dataend);
1690 dataend->td.td_status |= LE(UHCI_TD_IOC);
1693 if (uhcidebug > 10) {
1694 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
1695 uhci_dump_tds(data);
1696 uhci_dump_qh(upipe->u.intr.qhs[0]);
1701 /* Set up interrupt info. */
1703 ii->stdstart = data;
1704 ii->stdend = dataend;
1705 #if defined(__FreeBSD__)
1706 callout_handle_init(&ii->timeout_handle);
1712 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
1713 upipe->u.intr.qhs[0]));
1714 for (i = 0; i < upipe->u.intr.npoll; i++) {
1715 sqh = upipe->u.intr.qhs[i];
1717 sqh->qh.qh_elink = LE(data->physaddr);
1722 if (uhcidebug > 10) {
1723 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
1724 uhci_dump_tds(data);
1725 uhci_dump_qh(upipe->u.intr.qhs[0]);
1729 return (USBD_IN_PROGRESS);
1732 /* Abort a device control request. */
1734 uhci_device_ctrl_abort(xfer)
1735 usbd_xfer_handle xfer;
1737 DPRINTF(("uhci_device_ctrl_abort:\n"));
1738 uhci_abort_req(xfer, USBD_CANCELLED);
1741 /* Close a device control pipe. */
1743 uhci_device_ctrl_close(pipe)
1744 usbd_pipe_handle pipe;
1746 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1748 uhci_free_intr_info(upipe->iinfo);
1749 /* XXX free other resources? */
1752 /* Abort a device interrupt request. */
1754 uhci_device_intr_abort(xfer)
1755 usbd_xfer_handle xfer;
1757 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
1758 if (xfer->pipe->intrxfer == xfer) {
1759 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
1760 xfer->pipe->intrxfer = 0;
1762 uhci_abort_req(xfer, USBD_CANCELLED);
1765 /* Close a device interrupt pipe. */
1767 uhci_device_intr_close(pipe)
1768 usbd_pipe_handle pipe;
1770 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1771 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1774 upipe->iinfo->stdstart = 0; /* inactive */
1776 /* Unlink descriptors from controller data structures. */
1777 npoll = upipe->u.intr.npoll;
1778 uhci_lock_frames(sc);
1779 for (i = 0; i < npoll; i++)
1780 uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
1781 upipe->u.intr.qhs[i]);
1782 uhci_unlock_frames(sc);
1785 * We now have to wait for any activity on the physical
1786 * descriptors to stop.
1788 usb_delay_ms(&sc->sc_bus, 2);
1790 for(i = 0; i < npoll; i++)
1791 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
1792 free(upipe->u.intr.qhs, M_USBHC);
1795 LIST_REMOVE(upipe->iinfo, list); /* remove from active list */
1797 uhci_free_intr_info(upipe->iinfo);
1799 /* XXX free other resources */
1803 uhci_device_request(xfer)
1804 usbd_xfer_handle xfer;
1806 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1807 usb_device_request_t *req = &xfer->request;
1808 usbd_device_handle dev = upipe->pipe.device;
1809 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1810 int addr = dev->address;
1811 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1812 uhci_intr_info_t *ii = upipe->iinfo;
1813 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
1814 uhci_soft_qh_t *sqh;
1821 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
1822 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1823 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1824 UGETW(req->wIndex), UGETW(req->wLength),
1827 ls = dev->lowspeed ? UHCI_TD_LS : 0;
1828 isread = req->bmRequestType & UT_READ;
1829 len = UGETW(req->wLength);
1831 setup = upipe->u.ctl.setup;
1832 stat = upipe->u.ctl.stat;
1833 sqh = upipe->u.ctl.sqh;
1835 /* Set up data transaction */
1837 upipe->nexttoggle = 1;
1838 err = uhci_alloc_std_chain(upipe, sc, len, isread,
1839 xfer->flags & USBD_SHORT_XFER_OK,
1840 &xfer->dmabuf, &data, &dataend);
1844 dataend->link.std = stat;
1845 dataend->td.td_link = LE(stat->physaddr);
1849 upipe->u.ctl.length = len;
1851 memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
1853 setup->link.std = next;
1854 setup->td.td_link = LE(next->physaddr);
1855 setup->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls | UHCI_TD_ACTIVE);
1856 setup->td.td_token = LE(UHCI_TD_SETUP(sizeof *req, endpt, addr));
1857 setup->td.td_buffer = LE(DMAADDR(&upipe->u.ctl.reqdma));
1860 stat->td.td_link = LE(UHCI_PTR_T);
1861 stat->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls |
1862 UHCI_TD_ACTIVE | UHCI_TD_IOC);
1864 LE(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
1865 UHCI_TD_IN (0, endpt, addr, 1));
1866 stat->td.td_buffer = LE(0);
1869 if (uhcidebug > 10) {
1870 DPRINTF(("uhci_device_request: before transfer\n"));
1871 uhci_dump_tds(setup);
1875 /* Set up interrupt info. */
1877 ii->stdstart = setup;
1879 #if defined(__FreeBSD__)
1880 callout_handle_init(&ii->timeout_handle);
1887 sqh->qh.qh_elink = LE(setup->physaddr);
1888 sqh->intr_info = ii;
1891 uhci_add_ctrl(sc, sqh);
1892 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1894 if (uhcidebug > 12) {
1895 uhci_soft_td_t *std;
1896 uhci_soft_qh_t *xqh;
1897 uhci_soft_qh_t *sxqh;
1899 uhci_physaddr_t link;
1900 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
1901 for (std = sc->sc_vframes[0].htd, link = 0;
1902 (link & UHCI_PTR_Q) == 0;
1903 std = std->link.std) {
1904 link = LE(std->td.td_link);
1907 sxqh = (uhci_soft_qh_t *)std;
1911 xqh = (maxqh++ == 5 || xqh->hlink==sxqh ||
1912 xqh->hlink==xqh ? NULL : xqh->hlink)) {
1915 DPRINTF(("Enqueued QH:\n"));
1917 uhci_dump_tds(sqh->elink);
1920 if (xfer->timeout && !sc->sc_bus.use_polling) {
1921 usb_timeout(uhci_timeout, ii,
1922 MS_TO_TICKS(xfer->timeout), ii->timeout_handle);
1926 return (USBD_NORMAL_COMPLETION);
1930 uhci_device_isoc_transfer(xfer)
1931 usbd_xfer_handle xfer;
1935 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
1937 /* Put it on our queue, */
1938 err = usb_insert_transfer(xfer);
1940 /* bail out on error, */
1941 if (err && err != USBD_IN_PROGRESS)
1944 /* XXX should check inuse here */
1946 /* insert into schedule, */
1947 uhci_device_isoc_enter(xfer);
1949 /* and put on interrupt list if the pipe wasn't running */
1951 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1957 uhci_device_isoc_enter(xfer)
1958 usbd_xfer_handle xfer;
1960 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1961 usbd_device_handle dev = upipe->pipe.device;
1962 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1963 struct iso *iso = &upipe->u.iso;
1964 uhci_soft_td_t *std;
1965 u_int32_t buf, len, status;
1966 int s, i, next, nframes;
1968 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
1970 iso->inuse, iso->next, xfer, xfer->nframes));
1972 if (xfer->status == USBD_IN_PROGRESS) {
1973 /* This request has already been entered into the frame list */
1977 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
1978 printf("uhci_device_isoc_enter: overflow!\n");
1983 /* Not in use yet, schedule it a few frames ahead. */
1984 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
1985 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
1988 xfer->status = USBD_IN_PROGRESS;
1989 xfer->hcprivint = next;
1991 buf = DMAADDR(&xfer->dmabuf);
1992 status = LE(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
1995 nframes = xfer->nframes;
1997 for (i = 0; i < nframes; i++) {
1998 std = iso->stds[next];
1999 if (++next >= UHCI_VFRAMELIST_COUNT)
2001 len = xfer->frlengths[i];
2002 std->td.td_buffer = LE(buf);
2003 if (i == nframes - 1)
2004 status |= LE(UHCI_TD_IOC);
2005 std->td.td_status = status;
2006 std->td.td_token &= LE(~UHCI_TD_MAXLEN_MASK);
2007 std->td.td_token |= LE(UHCI_TD_SET_MAXLEN(len));
2009 if (uhcidebug > 5) {
2010 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2017 iso->inuse += xfer->nframes;
2023 uhci_device_isoc_start(xfer)
2024 usbd_xfer_handle xfer;
2026 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2027 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2028 uhci_intr_info_t *ii = upipe->iinfo;
2029 uhci_soft_td_t *end;
2033 if (xfer->status != USBD_IN_PROGRESS)
2034 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2037 /* Find the last TD */
2038 i = xfer->hcprivint + xfer->nframes;
2039 if (i >= UHCI_VFRAMELIST_COUNT)
2040 i -= UHCI_VFRAMELIST_COUNT;
2041 end = upipe->u.iso.stds[i];
2045 /* Set up interrupt info. */
2049 #if defined(__FreeBSD__)
2050 callout_handle_init(&ii->timeout_handle);
2055 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
2059 return (USBD_IN_PROGRESS);
2063 uhci_device_isoc_abort(xfer)
2064 usbd_xfer_handle xfer;
2066 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2067 uhci_intr_info_t *ii = upipe->iinfo;
2068 uhci_soft_td_t **stds = upipe->u.iso.stds;
2069 uhci_soft_td_t *std;
2072 /* Make interrupt routine ignore it, */
2073 xfer->status = USBD_CANCELLED;
2075 /* make hardware ignore it, */
2076 nframes = xfer->nframes;
2077 n = xfer->hcprivint;
2078 for (i = 0; i < nframes; i++) {
2080 std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2081 if (++n >= UHCI_VFRAMELIST_COUNT)
2087 /* make sure hardware has completed, */
2088 if (xfer->device->bus->intr_context) {
2089 /* We have no process context, so we can't use tsleep(). */
2090 timeout(uhci_abort_req_end, xfer, hz / USB_FRAMES_PER_SECOND);
2092 usb_delay_ms(xfer->pipe->device->bus, 1);
2093 /* and call final part of interrupt handler. */
2094 uhci_abort_req_end(xfer);
2099 uhci_device_isoc_close(pipe)
2100 usbd_pipe_handle pipe;
2102 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2103 usbd_device_handle dev = upipe->pipe.device;
2104 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2105 uhci_soft_td_t *std, *vstd;
2110 * Make sure all TDs are marked as inactive.
2111 * Wait for completion.
2115 iso = &upipe->u.iso;
2117 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2118 iso->stds[i]->td.td_status &= LE(~UHCI_TD_ACTIVE);
2119 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2121 uhci_lock_frames(sc);
2122 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2124 for (vstd = sc->sc_vframes[i].htd;
2125 vstd != NULL && vstd->link.std != std;
2126 vstd = vstd->link.std)
2130 printf("uhci_device_isoc_close: %p not found\n", std);
2131 uhci_unlock_frames(sc);
2134 vstd->link = std->link;
2135 vstd->td.td_link = std->td.td_link;
2136 uhci_free_std(sc, std);
2138 uhci_unlock_frames(sc);
2140 free(iso->stds, M_USBHC);
2144 uhci_setup_isoc(pipe)
2145 usbd_pipe_handle pipe;
2147 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2148 usbd_device_handle dev = upipe->pipe.device;
2149 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2150 int addr = upipe->pipe.device->address;
2151 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2152 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2153 uhci_soft_td_t *std, *vstd;
2158 iso = &upipe->u.iso;
2159 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2162 token = LE(rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2163 UHCI_TD_OUT(0, endpt, addr, 0));
2165 /* Allocate the TDs and mark as inactive; */
2166 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2167 std = uhci_alloc_std(sc);
2170 std->td.td_status = LE(UHCI_TD_IOS); /* iso, inactive */
2171 std->td.td_token = token;
2175 /* Insert TDs into schedule. */
2176 uhci_lock_frames(sc);
2177 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2179 vstd = sc->sc_vframes[i].htd;
2180 std->link = vstd->link;
2181 std->td.td_link = vstd->td.td_link;
2182 vstd->link.std = std;
2183 vstd->td.td_link = LE(std->physaddr);
2185 uhci_unlock_frames(sc);
2190 return (USBD_NORMAL_COMPLETION);
2194 uhci_free_std(sc, iso->stds[i]);
2195 free(iso->stds, M_USBHC);
2196 return (USBD_NOMEM);
2200 uhci_device_isoc_done(xfer)
2201 usbd_xfer_handle xfer;
2203 uhci_intr_info_t *ii = xfer->hcpriv;
2205 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2207 /* Turn off the interrupt since it is active even if the TD is not. */
2208 ii->stdend->td.td_status &= LE(~UHCI_TD_IOC);
2210 LIST_REMOVE(ii, list); /* remove from active list */
2214 uhci_device_intr_done(xfer)
2215 usbd_xfer_handle xfer;
2217 uhci_intr_info_t *ii = xfer->hcpriv;
2218 uhci_softc_t *sc = ii->sc;
2219 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2220 uhci_soft_qh_t *sqh;
2223 DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
2225 npoll = upipe->u.intr.npoll;
2226 for(i = 0; i < npoll; i++) {
2227 sqh = upipe->u.intr.qhs[i];
2229 sqh->qh.qh_elink = LE(UHCI_PTR_T);
2231 uhci_free_std_chain(sc, ii->stdstart, 0);
2234 if (xfer->pipe->repeat) {
2235 uhci_soft_td_t *data, *dataend;
2237 /* This alloc cannot fail since we freed the chain above. */
2238 uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
2239 xfer->flags & USBD_SHORT_XFER_OK,
2240 &xfer->dmabuf, &data, &dataend);
2241 dataend->td.td_status |= LE(UHCI_TD_IOC);
2244 if (uhcidebug > 10) {
2245 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2246 uhci_dump_tds(data);
2247 uhci_dump_qh(upipe->u.intr.qhs[0]);
2251 ii->stdstart = data;
2252 ii->stdend = dataend;
2253 #if defined(__FreeBSD__)
2254 callout_handle_init(&ii->timeout_handle);
2259 for (i = 0; i < npoll; i++) {
2260 sqh = upipe->u.intr.qhs[i];
2262 sqh->qh.qh_elink = LE(data->physaddr);
2265 ii->stdstart = 0; /* mark as inactive */
2269 /* Deallocate request data structures */
2271 uhci_device_ctrl_done(xfer)
2272 usbd_xfer_handle xfer;
2274 uhci_intr_info_t *ii = xfer->hcpriv;
2275 uhci_softc_t *sc = ii->sc;
2276 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2279 if (!(xfer->rqflags & URQ_REQUEST))
2280 panic("uhci_ctrl_done: not a request\n");
2283 LIST_REMOVE(ii, list); /* remove from active list */
2285 uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
2287 if (upipe->u.ctl.length != 0)
2288 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2290 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
2293 /* Deallocate request data structures */
2295 uhci_device_bulk_done(xfer)
2296 usbd_xfer_handle xfer;
2298 uhci_intr_info_t *ii = xfer->hcpriv;
2299 uhci_softc_t *sc = ii->sc;
2300 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2302 LIST_REMOVE(ii, list); /* remove from active list */
2304 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2306 uhci_free_std_chain(sc, ii->stdstart, 0);
2308 DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
2311 /* Add interrupt QH, called with vflock. */
2313 uhci_add_intr(sc, n, sqh)
2316 uhci_soft_qh_t *sqh;
2318 struct uhci_vframe *vf = &sc->sc_vframes[n];
2319 uhci_soft_qh_t *eqh;
2321 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
2323 sqh->hlink = eqh->hlink;
2324 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2326 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
2331 /* Remove interrupt QH, called with vflock. */
2333 uhci_remove_intr(sc, n, sqh)
2336 uhci_soft_qh_t *sqh;
2338 struct uhci_vframe *vf = &sc->sc_vframes[n];
2339 uhci_soft_qh_t *pqh;
2341 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
2343 for (pqh = vf->hqh; pqh->hlink != sqh; pqh = pqh->hlink)
2344 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
2345 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
2346 DPRINTF(("uhci_remove_intr: QH not found\n"));
2352 pqh->hlink = sqh->hlink;
2353 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2360 uhci_device_setintr(sc, upipe, ival)
2362 struct uhci_pipe *upipe;
2365 uhci_soft_qh_t *sqh;
2367 u_int bestbw, bw, bestoffs, offs;
2369 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
2371 printf("uhci_setintr: 0 interval\n");
2372 return (USBD_INVAL);
2375 if (ival > UHCI_VFRAMELIST_COUNT)
2376 ival = UHCI_VFRAMELIST_COUNT;
2377 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2378 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
2380 upipe->u.intr.npoll = npoll;
2382 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2385 * Figure out which offset in the schedule that has most
2386 * bandwidth left over.
2388 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2389 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2390 for (bw = i = 0; i < npoll; i++)
2391 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2397 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2399 upipe->iinfo->stdstart = 0;
2400 for(i = 0; i < npoll; i++) {
2401 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2403 sqh->qh.qh_elink = LE(UHCI_PTR_T);
2404 sqh->pos = MOD(i * ival + bestoffs);
2405 sqh->intr_info = upipe->iinfo;
2410 LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
2413 uhci_lock_frames(sc);
2414 /* Enter QHs into the controller data structures. */
2415 for(i = 0; i < npoll; i++)
2416 uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
2417 upipe->u.intr.qhs[i]);
2418 uhci_unlock_frames(sc);
2420 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
2421 return (USBD_NORMAL_COMPLETION);
2424 /* Open a new pipe. */
2427 usbd_pipe_handle pipe;
2429 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2430 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2431 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2434 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2435 pipe, pipe->device->address,
2436 ed->bEndpointAddress, sc->sc_addr));
2437 if (pipe->device->address == sc->sc_addr) {
2438 switch (ed->bEndpointAddress) {
2439 case USB_CONTROL_ENDPOINT:
2440 pipe->methods = &uhci_root_ctrl_methods;
2442 case UE_DIR_IN | UHCI_INTR_ENDPT:
2443 pipe->methods = &uhci_root_intr_methods;
2446 return (USBD_INVAL);
2449 upipe->iinfo = uhci_alloc_intr_info(sc);
2450 if (upipe->iinfo == 0)
2451 return (USBD_NOMEM);
2452 switch (ed->bmAttributes & UE_XFERTYPE) {
2454 pipe->methods = &uhci_device_ctrl_methods;
2455 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2456 if (upipe->u.ctl.sqh == NULL)
2458 upipe->u.ctl.setup = uhci_alloc_std(sc);
2459 if (upipe->u.ctl.setup == NULL) {
2460 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2463 upipe->u.ctl.stat = uhci_alloc_std(sc);
2464 if (upipe->u.ctl.stat == NULL) {
2465 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2466 uhci_free_std(sc, upipe->u.ctl.setup);
2469 err = usb_allocmem(&sc->sc_bus,
2470 sizeof(usb_device_request_t),
2471 0, &upipe->u.ctl.reqdma);
2473 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2474 uhci_free_std(sc, upipe->u.ctl.setup);
2475 uhci_free_std(sc, upipe->u.ctl.stat);
2480 pipe->methods = &uhci_device_intr_methods;
2481 return (uhci_device_setintr(sc, upipe, ed->bInterval));
2482 case UE_ISOCHRONOUS:
2483 pipe->methods = &uhci_device_isoc_methods;
2484 return (uhci_setup_isoc(pipe));
2486 pipe->methods = &uhci_device_bulk_methods;
2487 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2488 if (upipe->u.bulk.sqh == NULL)
2493 return (USBD_NORMAL_COMPLETION);
2496 uhci_free_intr_info(upipe->iinfo);
2497 return (USBD_NOMEM);
2501 * Data structures and routines to emulate the root hub.
2503 usb_device_descriptor_t uhci_devd = {
2504 USB_DEVICE_DESCRIPTOR_SIZE,
2505 UDESC_DEVICE, /* type */
2506 {0x00, 0x01}, /* USB version */
2507 UCLASS_HUB, /* class */
2508 USUBCLASS_HUB, /* subclass */
2510 64, /* max packet */
2511 {0},{0},{0x00,0x01}, /* device id */
2512 1,2,0, /* string indicies */
2513 1 /* # of configurations */
2516 usb_config_descriptor_t uhci_confd = {
2517 USB_CONFIG_DESCRIPTOR_SIZE,
2519 {USB_CONFIG_DESCRIPTOR_SIZE +
2520 USB_INTERFACE_DESCRIPTOR_SIZE +
2521 USB_ENDPOINT_DESCRIPTOR_SIZE},
2529 usb_interface_descriptor_t uhci_ifcd = {
2530 USB_INTERFACE_DESCRIPTOR_SIZE,
2541 usb_endpoint_descriptor_t uhci_endpd = {
2542 USB_ENDPOINT_DESCRIPTOR_SIZE,
2544 UE_DIR_IN | UHCI_INTR_ENDPT,
2550 usb_hub_descriptor_t uhci_hubd_piix = {
2551 USB_HUB_DESCRIPTOR_SIZE,
2554 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2555 50, /* power on to power good */
2557 { 0x00 }, /* both ports are removable */
2562 usb_string_descriptor_t *p;
2570 p->bLength = 2 * strlen(s) + 2;
2573 p->bDescriptorType = UDESC_STRING;
2575 for (i = 0; s[i] && l > 1; i++, l -= 2)
2576 USETW2(p->bString[i], 0, s[i]);
2581 * Simulate a hardware hub by handling all the necessary requests.
2584 uhci_root_ctrl_transfer(xfer)
2585 usbd_xfer_handle xfer;
2589 /* Insert last in queue. */
2590 err = usb_insert_transfer(xfer);
2594 /* Pipe isn't running (otherwise err would be USBD_INPROG),
2597 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2601 uhci_root_ctrl_start(xfer)
2602 usbd_xfer_handle xfer;
2604 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2605 usb_device_request_t *req;
2608 int s, len, value, index, status, change, l, totlen = 0;
2609 usb_port_status_t ps;
2613 if (!(xfer->rqflags & URQ_REQUEST))
2614 panic("uhci_root_ctrl_transfer: not a request\n");
2616 req = &xfer->request;
2618 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
2619 req->bmRequestType, req->bRequest));
2621 len = UGETW(req->wLength);
2622 value = UGETW(req->wValue);
2623 index = UGETW(req->wIndex);
2626 buf = KERNADDR(&xfer->dmabuf);
2628 #define C(x,y) ((x) | ((y) << 8))
2629 switch(C(req->bRequest, req->bmRequestType)) {
2630 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2631 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2632 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2634 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2635 * for the integrated root hub.
2638 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2640 *(u_int8_t *)buf = sc->sc_conf;
2644 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2645 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
2646 switch(value >> 8) {
2648 if ((value & 0xff) != 0) {
2652 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2653 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
2654 memcpy(buf, &uhci_devd, l);
2657 if ((value & 0xff) != 0) {
2661 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2662 memcpy(buf, &uhci_confd, l);
2663 buf = (char *)buf + l;
2665 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2667 memcpy(buf, &uhci_ifcd, l);
2668 buf = (char *)buf + l;
2670 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2672 memcpy(buf, &uhci_endpd, l);
2677 *(u_int8_t *)buf = 0;
2679 switch (value & 0xff) {
2680 case 1: /* Vendor */
2681 totlen = uhci_str(buf, len, sc->sc_vendor);
2683 case 2: /* Product */
2684 totlen = uhci_str(buf, len, "UHCI root hub");
2693 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2695 *(u_int8_t *)buf = 0;
2699 case C(UR_GET_STATUS, UT_READ_DEVICE):
2701 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2705 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2706 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2708 USETW(((usb_status_t *)buf)->wStatus, 0);
2712 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2713 if (value >= USB_MAX_DEVICES) {
2717 sc->sc_addr = value;
2719 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2720 if (value != 0 && value != 1) {
2724 sc->sc_conf = value;
2726 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2728 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2729 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2730 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2733 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2735 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2738 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2740 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2741 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2742 "port=%d feature=%d\n",
2745 port = UHCI_PORTSC1;
2746 else if (index == 2)
2747 port = UHCI_PORTSC2;
2753 case UHF_PORT_ENABLE:
2754 x = UREAD2(sc, port);
2755 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2757 case UHF_PORT_SUSPEND:
2758 x = UREAD2(sc, port);
2759 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
2761 case UHF_PORT_RESET:
2762 x = UREAD2(sc, port);
2763 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2765 case UHF_C_PORT_CONNECTION:
2766 x = UREAD2(sc, port);
2767 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2769 case UHF_C_PORT_ENABLE:
2770 x = UREAD2(sc, port);
2771 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2773 case UHF_C_PORT_OVER_CURRENT:
2774 x = UREAD2(sc, port);
2775 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2777 case UHF_C_PORT_RESET:
2779 err = USBD_NORMAL_COMPLETION;
2781 case UHF_PORT_CONNECTION:
2782 case UHF_PORT_OVER_CURRENT:
2783 case UHF_PORT_POWER:
2784 case UHF_PORT_LOW_SPEED:
2785 case UHF_C_PORT_SUSPEND:
2791 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2793 port = UHCI_PORTSC1;
2794 else if (index == 2)
2795 port = UHCI_PORTSC2;
2802 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2803 UHCI_PORTSC_LS_SHIFT;
2807 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2812 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
2814 memcpy(buf, &uhci_hubd_piix, l);
2816 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2821 memset(buf, 0, len);
2824 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2826 port = UHCI_PORTSC1;
2827 else if (index == 2)
2828 port = UHCI_PORTSC2;
2837 x = UREAD2(sc, port);
2838 status = change = 0;
2839 if (x & UHCI_PORTSC_CCS )
2840 status |= UPS_CURRENT_CONNECT_STATUS;
2841 if (x & UHCI_PORTSC_CSC )
2842 change |= UPS_C_CONNECT_STATUS;
2843 if (x & UHCI_PORTSC_PE )
2844 status |= UPS_PORT_ENABLED;
2845 if (x & UHCI_PORTSC_POEDC)
2846 change |= UPS_C_PORT_ENABLED;
2847 if (x & UHCI_PORTSC_OCI )
2848 status |= UPS_OVERCURRENT_INDICATOR;
2849 if (x & UHCI_PORTSC_OCIC )
2850 change |= UPS_C_OVERCURRENT_INDICATOR;
2851 if (x & UHCI_PORTSC_SUSP )
2852 status |= UPS_SUSPEND;
2853 if (x & UHCI_PORTSC_LSDA )
2854 status |= UPS_LOW_SPEED;
2855 status |= UPS_PORT_POWER;
2857 change |= UPS_C_PORT_RESET;
2858 USETW(ps.wPortStatus, status);
2859 USETW(ps.wPortChange, change);
2860 l = min(len, sizeof ps);
2861 memcpy(buf, &ps, l);
2864 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2867 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2869 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2871 port = UHCI_PORTSC1;
2872 else if (index == 2)
2873 port = UHCI_PORTSC2;
2879 case UHF_PORT_ENABLE:
2880 x = UREAD2(sc, port);
2881 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2883 case UHF_PORT_SUSPEND:
2884 x = UREAD2(sc, port);
2885 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2887 case UHF_PORT_RESET:
2888 x = UREAD2(sc, port);
2889 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2890 usb_delay_ms(&sc->sc_bus, 10);
2891 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2893 x = UREAD2(sc, port);
2894 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2896 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
2897 index, UREAD2(sc, port)));
2900 case UHF_C_PORT_CONNECTION:
2901 case UHF_C_PORT_ENABLE:
2902 case UHF_C_PORT_OVER_CURRENT:
2903 case UHF_PORT_CONNECTION:
2904 case UHF_PORT_OVER_CURRENT:
2905 case UHF_PORT_POWER:
2906 case UHF_PORT_LOW_SPEED:
2907 case UHF_C_PORT_SUSPEND:
2908 case UHF_C_PORT_RESET:
2918 xfer->actlen = totlen;
2919 err = USBD_NORMAL_COMPLETION;
2924 usb_transfer_complete(xfer);
2926 return (USBD_IN_PROGRESS);
2929 /* Abort a root control request. */
2931 uhci_root_ctrl_abort(xfer)
2932 usbd_xfer_handle xfer;
2934 /* Nothing to do, all transfers are syncronous. */
2937 /* Close the root pipe. */
2939 uhci_root_ctrl_close(pipe)
2940 usbd_pipe_handle pipe;
2942 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2944 sc->sc_has_timo = 0;
2945 DPRINTF(("uhci_root_ctrl_close\n"));
2948 /* Abort a root interrupt request. */
2950 uhci_root_intr_abort(xfer)
2951 usbd_xfer_handle xfer;
2953 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2955 usb_untimeout(uhci_timo, xfer, xfer->timo_handle);
2956 sc->sc_has_timo = 0;
2958 if (xfer->pipe->intrxfer == xfer) {
2959 DPRINTF(("uhci_root_intr_abort: remove\n"));
2960 xfer->pipe->intrxfer = 0;
2962 xfer->status = USBD_CANCELLED;
2963 usb_transfer_complete(xfer);
2967 uhci_root_intr_transfer(xfer)
2968 usbd_xfer_handle xfer;
2972 /* Insert last in queue. */
2973 err = usb_insert_transfer(xfer);
2977 /* Pipe isn't running (otherwise err would be USBD_INPROG),
2980 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2983 /* Start a transfer on the root interrupt pipe */
2985 uhci_root_intr_start(xfer)
2986 usbd_xfer_handle xfer;
2988 usbd_pipe_handle pipe = xfer->pipe;
2989 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2991 DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
2992 xfer, xfer->length, xfer->flags));
2994 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
2995 usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
2996 sc->sc_has_timo = xfer;
2997 return (USBD_IN_PROGRESS);
3000 /* Close the root interrupt pipe. */
3002 uhci_root_intr_close(pipe)
3003 usbd_pipe_handle pipe;
3005 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3007 usb_untimeout(uhci_timo, pipe->intrxfer, pipe->intrxfer->timo_handle);
3008 sc->sc_has_timo = 0;
3009 DPRINTF(("uhci_root_intr_close\n"));