1 /* $NetBSD: uhci.c,v 1.67 1999/11/18 23:32:28 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_xfer __P((usbd_xfer_handle,
181 usbd_status status));
182 static void uhci_abort_xfer_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 *));
192 static usbd_status uhci_setup_isoc __P((usbd_pipe_handle pipe));
193 static void uhci_device_isoc_enter __P((usbd_xfer_handle));
195 static usbd_status uhci_allocm __P((struct usbd_bus *, usb_dma_t *,
197 static void uhci_freem __P((struct usbd_bus *, usb_dma_t *));
199 static usbd_status uhci_device_ctrl_transfer __P((usbd_xfer_handle));
200 static usbd_status uhci_device_ctrl_start __P((usbd_xfer_handle));
201 static void uhci_device_ctrl_abort __P((usbd_xfer_handle));
202 static void uhci_device_ctrl_close __P((usbd_pipe_handle));
203 static void uhci_device_ctrl_done __P((usbd_xfer_handle));
205 static usbd_status uhci_device_intr_transfer __P((usbd_xfer_handle));
206 static usbd_status uhci_device_intr_start __P((usbd_xfer_handle));
207 static void uhci_device_intr_abort __P((usbd_xfer_handle));
208 static void uhci_device_intr_close __P((usbd_pipe_handle));
209 static void uhci_device_intr_done __P((usbd_xfer_handle));
211 static usbd_status uhci_device_bulk_transfer __P((usbd_xfer_handle));
212 static usbd_status uhci_device_bulk_start __P((usbd_xfer_handle));
213 static void uhci_device_bulk_abort __P((usbd_xfer_handle));
214 static void uhci_device_bulk_close __P((usbd_pipe_handle));
215 static void uhci_device_bulk_done __P((usbd_xfer_handle));
217 static usbd_status uhci_device_isoc_transfer __P((usbd_xfer_handle));
218 static usbd_status uhci_device_isoc_start __P((usbd_xfer_handle));
219 static void uhci_device_isoc_abort __P((usbd_xfer_handle));
220 static void uhci_device_isoc_close __P((usbd_pipe_handle));
221 static void uhci_device_isoc_done __P((usbd_xfer_handle));
223 static usbd_status uhci_root_ctrl_transfer __P((usbd_xfer_handle));
224 static usbd_status uhci_root_ctrl_start __P((usbd_xfer_handle));
225 static void uhci_root_ctrl_abort __P((usbd_xfer_handle));
226 static void uhci_root_ctrl_close __P((usbd_pipe_handle));
228 static usbd_status uhci_root_intr_transfer __P((usbd_xfer_handle));
229 static usbd_status uhci_root_intr_start __P((usbd_xfer_handle));
230 static void uhci_root_intr_abort __P((usbd_xfer_handle));
231 static void uhci_root_intr_close __P((usbd_pipe_handle));
232 static void uhci_root_intr_done __P((usbd_xfer_handle));
234 static usbd_status uhci_open __P((usbd_pipe_handle));
235 static void uhci_poll __P((struct usbd_bus *));
237 static usbd_status uhci_device_request __P((usbd_xfer_handle xfer));
239 static void uhci_add_intr __P((uhci_softc_t *, int,
241 static void uhci_remove_intr __P((uhci_softc_t *, int,
243 static usbd_status uhci_device_setintr __P((uhci_softc_t *sc,
244 struct uhci_pipe *pipe, int ival));
246 static void uhci_device_clear_toggle __P((usbd_pipe_handle pipe));
247 static void uhci_noop __P((usbd_pipe_handle pipe));
250 static void uhci_dumpregs __P((uhci_softc_t *));
251 static void uhci_dump_qhs __P((uhci_soft_qh_t *));
252 static void uhci_dump_qh __P((uhci_soft_qh_t *));
253 static void uhci_dump_tds __P((uhci_soft_td_t *));
254 static void uhci_dump_td __P((uhci_soft_td_t *));
257 #define UWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
258 #define UWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
259 #define UREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
260 #define UREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
261 #define UREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
263 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
264 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
266 #define UHCI_RESET_TIMEOUT 100 /* reset timeout */
268 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
270 #define UHCI_INTR_ENDPT 1
272 struct usbd_bus_methods uhci_bus_methods = {
279 struct usbd_pipe_methods uhci_root_ctrl_methods = {
280 uhci_root_ctrl_transfer,
281 uhci_root_ctrl_start,
282 uhci_root_ctrl_abort,
283 uhci_root_ctrl_close,
288 struct usbd_pipe_methods uhci_root_intr_methods = {
289 uhci_root_intr_transfer,
290 uhci_root_intr_start,
291 uhci_root_intr_abort,
292 uhci_root_intr_close,
297 struct usbd_pipe_methods uhci_device_ctrl_methods = {
298 uhci_device_ctrl_transfer,
299 uhci_device_ctrl_start,
300 uhci_device_ctrl_abort,
301 uhci_device_ctrl_close,
303 uhci_device_ctrl_done,
306 struct usbd_pipe_methods uhci_device_intr_methods = {
307 uhci_device_intr_transfer,
308 uhci_device_intr_start,
309 uhci_device_intr_abort,
310 uhci_device_intr_close,
311 uhci_device_clear_toggle,
312 uhci_device_intr_done,
315 struct usbd_pipe_methods uhci_device_bulk_methods = {
316 uhci_device_bulk_transfer,
317 uhci_device_bulk_start,
318 uhci_device_bulk_abort,
319 uhci_device_bulk_close,
320 uhci_device_clear_toggle,
321 uhci_device_bulk_done,
324 struct usbd_pipe_methods uhci_device_isoc_methods = {
325 uhci_device_isoc_transfer,
326 uhci_device_isoc_start,
327 uhci_device_isoc_abort,
328 uhci_device_isoc_close,
330 uhci_device_isoc_done,
337 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
338 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
339 UHCICMD(sc, 0); /* do nothing */
348 uhci_soft_qh_t *csqh, *bsqh, *sqh;
351 DPRINTFN(1,("uhci_init: start\n"));
358 uhci_run(sc, 0); /* stop the controller */
359 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
363 /* Allocate and initialize real frame array. */
364 err = usb_allocmem(&sc->sc_bus,
365 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
366 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
369 sc->sc_pframes = KERNADDR(&sc->sc_dma);
370 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
371 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma)); /* set frame list*/
373 /* Allocate the dummy QH where bulk traffic will be queued. */
374 bsqh = uhci_alloc_sqh(sc);
377 bsqh->qh.qh_hlink = LE(UHCI_PTR_T); /* end of QH chain */
378 bsqh->qh.qh_elink = LE(UHCI_PTR_T);
379 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
381 /* Allocate the dummy QH where control traffic will be queued. */
382 csqh = uhci_alloc_sqh(sc);
386 csqh->qh.qh_hlink = LE(bsqh->physaddr | UHCI_PTR_Q);
387 csqh->qh.qh_elink = LE(UHCI_PTR_T);
388 sc->sc_ctl_start = sc->sc_ctl_end = csqh;
391 * Make all (virtual) frame list pointers point to the interrupt
392 * queue heads and the interrupt queue heads at the control
393 * queue head and point the physical frame list to the virtual.
395 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
396 std = uhci_alloc_std(sc);
397 sqh = uhci_alloc_sqh(sc);
398 if (std == NULL || sqh == NULL)
401 std->td.td_link = LE(sqh->physaddr | UHCI_PTR_Q);
402 std->td.td_status = LE(UHCI_TD_IOS); /* iso, inactive */
403 std->td.td_token = LE(0);
404 std->td.td_buffer = LE(0);
406 sqh->qh.qh_hlink = LE(csqh->physaddr | UHCI_PTR_Q);
408 sqh->qh.qh_elink = LE(UHCI_PTR_T);
409 sc->sc_vframes[i].htd = std;
410 sc->sc_vframes[i].etd = std;
411 sc->sc_vframes[i].hqh = sqh;
412 sc->sc_vframes[i].eqh = sqh;
414 j < UHCI_FRAMELIST_COUNT;
415 j += UHCI_VFRAMELIST_COUNT)
416 sc->sc_pframes[j] = LE(std->physaddr);
419 LIST_INIT(&sc->sc_intrhead);
421 /* Set up the bus struct. */
422 sc->sc_bus.methods = &uhci_bus_methods;
423 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
425 sc->sc_suspend = PWR_RESUME;
426 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
428 DPRINTFN(1,("uhci_init: enabling\n"));
429 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
430 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
432 return (uhci_run(sc, 1)); /* and here we go... */
435 #if defined(__NetBSD__) || defined(__OpenBSD__)
437 uhci_activate(self, act)
441 struct uhci_softc *sc = (struct uhci_softc *)self;
449 case DVACT_DEACTIVATE:
450 if (sc->sc_child != NULL)
451 rv = config_deactivate(sc->sc_child);
458 uhci_detach(sc, flags)
459 struct uhci_softc *sc;
464 if (sc->sc_child != NULL)
465 rv = config_detach(sc->sc_child, flags);
470 powerhook_disestablish(sc->sc_powerhook);
471 /* free data structures XXX */
478 uhci_allocm(bus, dma, size)
479 struct usbd_bus *bus;
483 return (usb_allocmem(&((struct uhci_softc *)bus)->sc_bus, size, 0,
489 struct usbd_bus *bus;
492 usb_freemem(&((struct uhci_softc *)bus)->sc_bus, dma);
495 #if defined(__NetBSD__)
497 * Handle suspend/resume.
499 * We need to switch to polling mode here, because this routine is
500 * called from an intterupt context. This is all right since we
501 * are almost suspended anyway.
508 uhci_softc_t *sc = v;
513 cmd = UREAD2(sc, UHCI_CMD);
515 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
516 sc, why, sc->sc_suspend, cmd));
518 if (why != PWR_RESUME) {
524 usb_untimeout(uhci_timo, sc->sc_has_timo,
525 sc->sc_has_timo->timo_handle);
526 sc->sc_bus.use_polling++;
527 uhci_run(sc, 0); /* stop the controller */
528 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
529 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
530 sc->sc_suspend = why;
531 sc->sc_bus.use_polling--;
532 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
535 * XXX We should really do much more here in case the
536 * controller registers have been lost and BIOS has
540 if (sc->sc_suspend == PWR_RESUME)
541 printf("uhci_power: weird, resume without suspend.\n");
543 sc->sc_bus.use_polling++;
544 sc->sc_suspend = why;
545 if (cmd & UHCI_CMD_RS)
546 uhci_run(sc, 0); /* in case BIOS has started it */
547 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
548 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
549 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
550 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
551 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
552 uhci_run(sc, 1); /* and start traffic again */
553 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
554 sc->sc_bus.use_polling--;
556 usb_timeout(uhci_timo, sc->sc_has_timo,
557 sc->sc_ival, sc->sc_has_timo->timo_handle);
572 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
573 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
574 USBDEVNAME(sc->sc_bus.bdev),
575 UREAD2(sc, UHCI_CMD),
576 UREAD2(sc, UHCI_STS),
577 UREAD2(sc, UHCI_INTR),
578 UREAD2(sc, UHCI_FRNUM),
579 UREAD4(sc, UHCI_FLBASEADDR),
580 UREAD1(sc, UHCI_SOF),
581 UREAD2(sc, UHCI_PORTSC1),
582 UREAD2(sc, UHCI_PORTSC2)));
589 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
590 "token=0x%08lx buffer=0x%08lx\n",
591 p, (long)p->physaddr,
592 (long)LE(p->td.td_link),
593 (long)LE(p->td.td_status),
594 (long)LE(p->td.td_token),
595 (long)LE(p->td.td_buffer)));
596 DPRINTFN(-1,(" %b %b,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
598 (int)LE(p->td.td_link),
600 (int)LE(p->td.td_status),
601 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
602 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
603 UHCI_TD_GET_ERRCNT(LE(p->td.td_status)),
604 UHCI_TD_GET_ACTLEN(LE(p->td.td_status)),
605 UHCI_TD_GET_PID(LE(p->td.td_token)),
606 UHCI_TD_GET_DEVADDR(LE(p->td.td_token)),
607 UHCI_TD_GET_ENDPT(LE(p->td.td_token)),
608 UHCI_TD_GET_DT(LE(p->td.td_token)),
609 UHCI_TD_GET_MAXLEN(LE(p->td.td_token))));
616 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
617 (int)sqh->physaddr, LE(sqh->qh.qh_hlink), LE(sqh->qh.qh_elink)));
625 uhci_softc_t *sc = uhci;
628 printf("intrs=%d\n", sc->sc_bus.no_intrs);
629 printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);
630 uhci_dump_qh(sc->sc_ctl_start->qh.hlink);
641 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
642 * Traverses sideways first, then down.
652 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
656 if (sqh->hlink != NULL && !(sqh->qh.qh_hlink & UHCI_PTR_T))
657 uhci_dump_qhs(sqh->hlink);
659 DPRINTF(("No QH\n"));
661 if (sqh->elink != NULL && !(sqh->qh.qh_elink & UHCI_PTR_T))
662 uhci_dump_tds(sqh->elink);
664 DPRINTF(("No TD\n"));
673 for(td = std; td != NULL; td = td->link.std) {
676 /* Check whether the link pointer in this TD marks
677 * the link pointer as end of queue. This avoids
678 * printing the free list in case the queue/TD has
679 * already been moved there (seatbelt).
681 if (td->td.td_link & UHCI_PTR_T ||
689 * This routine is executed periodically and simulates interrupts
690 * from the root controller interrupt pipe for port status change.
696 usbd_xfer_handle xfer = addr;
697 usbd_pipe_handle pipe = xfer->pipe;
698 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
702 DPRINTFN(20, ("uhci_timo\n"));
704 usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
706 p = KERNADDR(&xfer->dmabuf);
708 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
710 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
713 /* No change, try again in a while */
717 xfer->status = USBD_NORMAL_COMPLETION;
720 xfer->device->bus->intr_context++;
721 usb_transfer_complete(xfer);
722 xfer->device->bus->intr_context--;
727 uhci_root_intr_done(xfer)
728 usbd_xfer_handle xfer;
739 while (sc->sc_vflock) {
740 sc->sc_vflock |= UHCI_WANT_LOCK;
741 tsleep(&sc->sc_vflock, PRIBIO, "uhcqhl", 0);
743 sc->sc_vflock = UHCI_HAS_LOCK;
748 uhci_unlock_frames(sc)
753 sc->sc_vflock &= ~UHCI_HAS_LOCK;
754 if (sc->sc_vflock & UHCI_WANT_LOCK)
755 wakeup(&sc->sc_vflock);
760 * Allocate an interrupt information struct. A free list is kept
761 * for fast allocation.
764 uhci_alloc_intr_info(sc)
767 uhci_intr_info_t *ii;
769 ii = LIST_FIRST(&uhci_ii_free);
771 LIST_REMOVE(ii, list);
773 ii = malloc(sizeof(uhci_intr_info_t), M_USBHC, M_NOWAIT);
776 #if defined(__FreeBSD__)
777 callout_handle_init(&ii->timeout_handle);
784 uhci_free_intr_info(ii)
785 uhci_intr_info_t *ii;
787 LIST_INSERT_HEAD(&uhci_ii_free, ii, list); /* and put on free list */
790 /* Add control QH, called at splusb(). */
792 uhci_add_ctrl(sc, sqh)
800 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
801 eqh = sc->sc_ctl_end;
802 sqh->hlink = eqh->hlink;
803 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
805 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
806 sc->sc_ctl_end = sqh;
809 /* Remove control QH, called at splusb(). */
811 uhci_remove_ctrl(sc, sqh)
819 DPRINTFN(10, ("uhci_remove_ctrl: sqh=%p\n", sqh));
820 for (pqh = sc->sc_ctl_start; pqh->hlink != sqh; pqh=pqh->hlink)
821 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
822 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
823 printf("uhci_remove_ctrl: QH not found\n");
829 pqh->hlink = sqh->hlink;
830 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
831 if (sc->sc_ctl_end == sqh)
832 sc->sc_ctl_end = pqh;
835 /* Add bulk QH, called at splusb(). */
837 uhci_add_bulk(sc, sqh)
845 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
846 eqh = sc->sc_bulk_end;
847 sqh->hlink = eqh->hlink;
848 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
850 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
851 sc->sc_bulk_end = sqh;
854 /* Remove bulk QH, called at splusb(). */
856 uhci_remove_bulk(sc, sqh)
864 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
865 for (pqh = sc->sc_bulk_start; pqh->hlink != sqh; pqh = pqh->hlink)
866 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
867 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
868 printf("uhci_remove_bulk: QH not found\n");
874 pqh->hlink = sqh->hlink;
875 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
876 if (sc->sc_bulk_end == sqh)
877 sc->sc_bulk_end = pqh;
884 uhci_softc_t *sc = arg;
887 uhci_intr_info_t *ii;
890 * It can happen that an interrupt will be delivered to
891 * us before the device has been fully attached and the
892 * softc struct has been configured. Usually this happens
893 * when kldloading the USB support as a module after the
894 * system has been booted. If we detect this condition,
895 * we need to squelch the unwanted interrupts until we're
898 if (sc->sc_bus.bdev == NULL) {
899 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
900 uhci_run(sc, 0); /* stop the controller */
901 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
906 if (uhcidebug > 15) {
907 DPRINTF(("%s: uhci_intr\n", USBDEVNAME(sc->sc_bus.bdev)));
912 status = UREAD2(sc, UHCI_STS);
913 if (status == 0) /* The interrupt was not for us. */
916 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
917 if (sc->sc_suspend != PWR_RESUME)
918 printf("uhci_intr: suspended sts=0x%x\n", status);
922 if (status & UHCI_STS_USBINT)
923 ack |= UHCI_STS_USBINT;
924 if (status & UHCI_STS_USBEI)
925 ack |= UHCI_STS_USBEI;
926 if (status & UHCI_STS_RD) {
928 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
930 if (status & UHCI_STS_HSE) {
932 printf("%s: host controller process error\n",
933 USBDEVNAME(sc->sc_bus.bdev));
935 if (status & UHCI_STS_HCPE) {
936 ack |= UHCI_STS_HCPE;
937 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
939 if (status & UHCI_STS_HCH) {
940 /* no acknowledge needed */
941 printf("%s: host controller halted\n",
942 USBDEVNAME(sc->sc_bus.bdev));
945 if (ack) /* acknowledge the ints */
946 UWRITE2(sc, UHCI_STS, ack);
947 else /* nothing to acknowledge */
950 sc->sc_bus.intr_context++;
951 sc->sc_bus.no_intrs++;
954 * Interrupts on UHCI really suck. When the host controller
955 * interrupts because a transfer is completed there is no
956 * way of knowing which transfer it was. You can scan down
957 * the TDs and QHs of the previous frame to limit the search,
958 * but that assumes that the interrupt was not delayed by more
959 * than 1 ms, which may not always be true (e.g. after debug
960 * output on a slow console).
961 * We scan all interrupt descriptors to see if any have
964 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
965 uhci_check_intr(sc, ii);
967 DPRINTFN(10, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
969 sc->sc_bus.intr_context--;
974 /* Check for an interrupt. */
976 uhci_check_intr(sc, ii)
978 uhci_intr_info_t *ii;
980 uhci_soft_td_t *std, *lstd;
983 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
986 printf("uhci_check_intr: no ii? %p\n", ii);
990 if (ii->stdstart == NULL)
995 printf("uhci_check_intr: std==0\n");
1000 * If the last TD is still active we need to check whether there
1001 * is a an error somewhere in the middle, or whether there was a
1002 * short packet (SPD and not ACTIVE).
1004 if (LE(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1005 DPRINTFN(15, ("uhci_check_intr: active ii=%p\n", ii));
1006 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1007 status = LE(std->td.td_status);
1008 if ((status & UHCI_TD_STALLED) ||
1009 (status & (UHCI_TD_SPD | UHCI_TD_ACTIVE)) ==
1013 DPRINTFN(15, ("uhci_check_intr: ii=%p std=%p still active\n",
1018 usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
1022 /* Called at splusb() */
1025 uhci_intr_info_t *ii;
1027 usbd_xfer_handle xfer = ii->xfer;
1028 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1029 uhci_soft_td_t *std;
1030 u_int32_t status = 0, nstatus;
1038 printf("uhci_idone: ii=%p is done!\n", ii);
1046 if (xfer->status == USBD_CANCELLED ||
1047 xfer->status == USBD_TIMEOUT) {
1048 DPRINTF(("uhci_idone: aborted xfer=%p\n", xfer));
1052 if (xfer->nframes != 0) {
1053 /* Isoc transfer, do things differently. */
1054 uhci_soft_td_t **stds = upipe->u.iso.stds;
1057 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1059 nframes = xfer->nframes;
1061 n = xfer->hcprivint;
1062 for (i = 0; i < nframes; i++) {
1065 if (uhcidebug > 5) {
1066 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1070 if (++n >= UHCI_VFRAMELIST_COUNT)
1072 status = LE(std->td.td_status);
1073 actlen += UHCI_TD_GET_ACTLEN(status);
1075 upipe->u.iso.inuse -= nframes;
1076 xfer->actlen = actlen;
1077 xfer->status = USBD_NORMAL_COMPLETION;
1079 usb_transfer_complete(xfer);
1084 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1087 uhci_dump_tds(ii->stdstart);
1090 /* The transfer is done, compute actual length and status. */
1092 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1093 nstatus = LE(std->td.td_status);
1094 if (nstatus & UHCI_TD_ACTIVE)
1098 if (UHCI_TD_GET_PID(LE(std->td.td_token)) != UHCI_TD_PID_SETUP)
1099 actlen += UHCI_TD_GET_ACTLEN(status);
1101 /* If there are left over TDs we need to update the toggle. */
1103 upipe->nexttoggle = UHCI_TD_GET_DT(LE(std->td.td_token));
1105 status &= UHCI_TD_ERROR;
1106 DPRINTFN(10, ("uhci_check_intr: actlen=%d, status=0x%x\n",
1108 xfer->actlen = actlen;
1110 DPRINTFN((status&UHCI_TD_STALLED)*10,
1111 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1113 xfer->pipe->device->address,
1114 xfer->pipe->endpoint->edesc->bEndpointAddress,
1116 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
1117 "STALLED\30ACTIVE"));
1118 if (status == UHCI_TD_STALLED)
1119 xfer->status = USBD_STALLED;
1121 xfer->status = USBD_IOERROR; /* more info XXX */
1123 xfer->status = USBD_NORMAL_COMPLETION;
1126 usb_transfer_complete(xfer);
1130 * Called when a request does not complete.
1136 uhci_intr_info_t *ii = addr;
1138 DPRINTF(("uhci_timeout: ii=%p\n", ii));
1142 uhci_dump_tds(ii->stdstart);
1145 ii->xfer->device->bus->intr_context++;
1146 uhci_abort_xfer(ii->xfer, USBD_TIMEOUT);
1147 ii->xfer->device->bus->intr_context--;
1151 * Wait here until controller claims to have an interrupt.
1152 * Then call uhci_intr and return. Use timeout to avoid waiting
1154 * Only used during boot when interrupts are not enabled yet.
1157 uhci_waitintr(sc, xfer)
1159 usbd_xfer_handle xfer;
1161 int timo = xfer->timeout;
1162 uhci_intr_info_t *ii;
1164 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1166 xfer->status = USBD_IN_PROGRESS;
1167 for (; timo >= 0; timo--) {
1168 usb_delay_ms(&sc->sc_bus, 1);
1169 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1170 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
1172 if (xfer->status != USBD_IN_PROGRESS)
1178 DPRINTF(("uhci_waitintr: timeout\n"));
1179 for (ii = LIST_FIRST(&sc->sc_intrhead);
1180 ii != NULL && ii->xfer != xfer;
1181 ii = LIST_NEXT(ii, list))
1185 panic("uhci_waitintr: lost intr_info\n");
1192 struct usbd_bus *bus;
1194 uhci_softc_t *sc = (uhci_softc_t *)bus;
1196 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
1205 uhci_softc_t *sc = p;
1208 UHCICMD(sc, UHCI_CMD_HCRESET);
1209 /* The reset bit goes low when the controller is done. */
1210 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1211 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1213 if (n >= UHCI_RESET_TIMEOUT)
1214 printf("%s: controller did not reset\n",
1215 USBDEVNAME(sc->sc_bus.bdev));
1228 DPRINTF(("uhci_run: setting run=%d\n", run));
1230 * When activating the controller, set the MAXP bit.
1231 * Certain high speed devices such as network adapters
1232 * require this in order to avoid babble errors that
1233 * can cause an endpoint stall.
1235 UHCICMD(sc, run ? UHCI_CMD_RS|UHCI_CMD_MAXP : 0);
1236 for(n = 0; n < 10; n++) {
1237 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1238 /* return when we've entered the state we want */
1239 if (run == running) {
1241 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1242 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1243 return (USBD_NORMAL_COMPLETION);
1245 usb_delay_ms(&sc->sc_bus, 1);
1248 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1249 run ? "start" : "stop");
1250 return (USBD_IOERROR);
1254 * Memory management routines.
1255 * uhci_alloc_std allocates TDs
1256 * uhci_alloc_sqh allocates QHs
1257 * These two routines do their own free list management,
1258 * partly for speed, partly because allocating DMAable memory
1259 * has page size granularaity so much memory would be wasted if
1260 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1267 uhci_soft_td_t *std;
1272 if (sc->sc_freetds == NULL) {
1273 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1274 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1275 UHCI_TD_ALIGN, &dma);
1278 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1279 offs = i * UHCI_STD_SIZE;
1280 std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
1281 std->physaddr = DMAADDR(&dma) + offs;
1282 std->link.std = sc->sc_freetds;
1283 sc->sc_freetds = std;
1286 std = sc->sc_freetds;
1287 sc->sc_freetds = std->link.std;
1288 memset(&std->td, 0, sizeof(uhci_td_t));
1293 uhci_free_std(sc, std)
1295 uhci_soft_td_t *std;
1298 #define TD_IS_FREE 0x12345678
1299 if (std->td.td_token == LE(TD_IS_FREE)) {
1300 printf("uhci_free_std: freeing free TD %p\n", std);
1303 std->td.td_token = LE(TD_IS_FREE);
1305 std->link.std = sc->sc_freetds;
1306 sc->sc_freetds = std;
1313 uhci_soft_qh_t *sqh;
1318 if (sc->sc_freeqhs == NULL) {
1319 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1320 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1321 UHCI_QH_ALIGN, &dma);
1324 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1325 offs = i * UHCI_SQH_SIZE;
1326 sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
1327 sqh->physaddr = DMAADDR(&dma) + offs;
1328 sqh->hlink = sc->sc_freeqhs;
1329 sc->sc_freeqhs = sqh;
1332 sqh = sc->sc_freeqhs;
1333 sc->sc_freeqhs = sqh->hlink;
1334 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1339 uhci_free_sqh(sc, sqh)
1341 uhci_soft_qh_t *sqh;
1343 sqh->hlink = sc->sc_freeqhs;
1344 sc->sc_freeqhs = sqh;
1349 * Enter a list of transfers onto a control queue.
1350 * Called at splusb()
1353 uhci_enter_ctl_q(sc, sqh, ii)
1355 uhci_soft_qh_t *sqh;
1356 uhci_intr_info_t *ii;
1358 DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
1364 uhci_free_std_chain(sc, std, stdend)
1366 uhci_soft_td_t *std;
1367 uhci_soft_td_t *stdend;
1371 for (; std != stdend; std = p) {
1373 uhci_free_std(sc, std);
1378 uhci_alloc_std_chain(upipe, sc, len, rd, shortok, dma, sp, ep)
1379 struct uhci_pipe *upipe;
1381 int len, rd, shortok;
1383 uhci_soft_td_t **sp, **ep;
1385 uhci_soft_td_t *p, *lastp;
1386 uhci_physaddr_t lastlink;
1387 int i, ntd, l, tog, maxp;
1389 int addr = upipe->pipe.device->address;
1390 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1392 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
1393 "shortok=%d\n", addr, UE_GET_ADDR(endpt), len,
1394 upipe->pipe.device->lowspeed, shortok));
1397 DPRINTFN(-1,("uhci_alloc_std_chain: len=0\n"));
1398 return (USBD_NORMAL_COMPLETION);
1400 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1402 printf("uhci_alloc_std_chain: maxp=0\n");
1403 return (USBD_INVAL);
1405 ntd = (len + maxp - 1) / maxp;
1406 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1407 tog = upipe->nexttoggle;
1410 upipe->nexttoggle = tog ^ 1;
1412 lastlink = UHCI_PTR_T;
1414 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1415 if (upipe->pipe.device->lowspeed)
1416 status |= UHCI_TD_LS;
1418 status |= UHCI_TD_SPD;
1419 for (i = ntd; i >= 0; i--) {
1420 p = uhci_alloc_std(sc);
1422 uhci_free_std_chain(sc, lastp, 0);
1423 return (USBD_NOMEM);
1425 p->link.std = lastp;
1426 if (lastlink == UHCI_PTR_T)
1427 p->td.td_link = LE(lastlink);
1429 p->td.td_link = LE(lastlink|UHCI_PTR_VF);
1431 lastlink = p->physaddr;
1432 p->td.td_status = LE(status);
1436 if (l == 0) l = maxp;
1441 LE(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1442 UHCI_TD_OUT(l, endpt, addr, tog));
1443 p->td.td_buffer = LE(DMAADDR(dma) + i * maxp);
1447 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1448 upipe->nexttoggle));
1449 return (USBD_NORMAL_COMPLETION);
1453 uhci_device_clear_toggle(pipe)
1454 usbd_pipe_handle pipe;
1456 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1457 upipe->nexttoggle = 0;
1462 usbd_pipe_handle pipe;
1467 uhci_device_bulk_transfer(xfer)
1468 usbd_xfer_handle xfer;
1472 /* Insert last in queue. */
1473 err = usb_insert_transfer(xfer);
1477 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1480 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1484 uhci_device_bulk_start(xfer)
1485 usbd_xfer_handle xfer;
1487 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1488 usbd_device_handle dev = upipe->pipe.device;
1489 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1490 uhci_intr_info_t *ii = upipe->iinfo;
1491 uhci_soft_td_t *data, *dataend;
1492 uhci_soft_qh_t *sqh;
1494 int len, isread, endpt;
1497 DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
1498 xfer, xfer->length, xfer->flags));
1501 if (xfer->rqflags & URQ_REQUEST)
1502 panic("uhci_device_bulk_transfer: a request\n");
1506 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
1507 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1508 sqh = upipe->u.bulk.sqh;
1510 upipe->u.bulk.isread = isread;
1511 upipe->u.bulk.length = len;
1513 err = uhci_alloc_std_chain(upipe, sc, len, isread,
1514 xfer->flags & USBD_SHORT_XFER_OK,
1515 &xfer->dmabuf, &data, &dataend);
1518 dataend->td.td_status |= LE(UHCI_TD_IOC);
1521 if (uhcidebug > 8) {
1522 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1523 uhci_dump_tds(data);
1527 /* Set up interrupt info. */
1529 ii->stdstart = data;
1530 ii->stdend = dataend;
1531 #if defined(__FreeBSD__)
1532 callout_handle_init(&ii->timeout_handle);
1539 sqh->qh.qh_elink = LE(data->physaddr);
1540 sqh->intr_info = ii;
1543 uhci_add_bulk(sc, sqh);
1544 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1546 if (xfer->timeout && !sc->sc_bus.use_polling) {
1547 usb_timeout(uhci_timeout, ii, MS_TO_TICKS(xfer->timeout),
1548 ii->timeout_handle);
1553 if (uhcidebug > 10) {
1554 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1555 uhci_dump_tds(data);
1559 if (sc->sc_bus.use_polling)
1560 uhci_waitintr(sc, xfer);
1562 return (USBD_IN_PROGRESS);
1565 /* Abort a device bulk request. */
1567 uhci_device_bulk_abort(xfer)
1568 usbd_xfer_handle xfer;
1570 DPRINTF(("uhci_device_bulk_abort:\n"));
1571 uhci_abort_xfer(xfer, USBD_CANCELLED);
1575 uhci_abort_xfer(xfer, status)
1576 usbd_xfer_handle xfer;
1579 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1580 uhci_intr_info_t *ii = upipe->iinfo;
1581 uhci_soft_td_t *std;
1583 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1585 /* Make interrupt routine ignore it, */
1586 xfer->status = status;
1588 /* don't timeout, */
1589 usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
1591 /* make hardware ignore it, */
1592 for (std = ii->stdstart; std != 0; std = std->link.std)
1593 std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1597 /* make sure hardware has completed, */
1598 if (xfer->device->bus->intr_context) {
1599 /* We have no process context, so we can't use tsleep(). */
1600 timeout(uhci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
1602 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
1603 KASSERT(intr_nesting_level == 0,
1604 ("ohci_abort_req in interrupt context"));
1606 usb_delay_ms(xfer->pipe->device->bus, 1);
1607 /* and call final part of interrupt handler. */
1608 uhci_abort_xfer_end(xfer);
1613 uhci_abort_xfer_end(v)
1616 usbd_xfer_handle xfer = v;
1620 usb_transfer_complete(xfer);
1624 /* Close a device bulk pipe. */
1626 uhci_device_bulk_close(pipe)
1627 usbd_pipe_handle pipe;
1629 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1630 usbd_device_handle dev = upipe->pipe.device;
1631 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1633 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1634 uhci_free_intr_info(upipe->iinfo);
1635 /* XXX free other resources */
1639 uhci_device_ctrl_transfer(xfer)
1640 usbd_xfer_handle xfer;
1644 /* Insert last in queue. */
1645 err = usb_insert_transfer(xfer);
1649 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1652 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1656 uhci_device_ctrl_start(xfer)
1657 usbd_xfer_handle xfer;
1659 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1663 if (!(xfer->rqflags & URQ_REQUEST))
1664 panic("uhci_device_ctrl_transfer: not a request\n");
1667 err = uhci_device_request(xfer);
1671 if (sc->sc_bus.use_polling)
1672 uhci_waitintr(sc, xfer);
1673 return (USBD_IN_PROGRESS);
1677 uhci_device_intr_transfer(xfer)
1678 usbd_xfer_handle xfer;
1682 /* Insert last in queue. */
1683 err = usb_insert_transfer(xfer);
1687 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1690 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1694 uhci_device_intr_start(xfer)
1695 usbd_xfer_handle xfer;
1697 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1698 usbd_device_handle dev = upipe->pipe.device;
1699 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1700 uhci_intr_info_t *ii = upipe->iinfo;
1701 uhci_soft_td_t *data, *dataend;
1702 uhci_soft_qh_t *sqh;
1706 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
1707 xfer, xfer->length, xfer->flags));
1710 if (xfer->rqflags & URQ_REQUEST)
1711 panic("uhci_device_intr_transfer: a request\n");
1714 err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
1715 xfer->flags & USBD_SHORT_XFER_OK,
1716 &xfer->dmabuf, &data, &dataend);
1719 dataend->td.td_status |= LE(UHCI_TD_IOC);
1722 if (uhcidebug > 10) {
1723 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
1724 uhci_dump_tds(data);
1725 uhci_dump_qh(upipe->u.intr.qhs[0]);
1730 /* Set up interrupt info. */
1732 ii->stdstart = data;
1733 ii->stdend = dataend;
1734 #if defined(__FreeBSD__)
1735 callout_handle_init(&ii->timeout_handle);
1741 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
1742 upipe->u.intr.qhs[0]));
1743 for (i = 0; i < upipe->u.intr.npoll; i++) {
1744 sqh = upipe->u.intr.qhs[i];
1746 sqh->qh.qh_elink = LE(data->physaddr);
1751 if (uhcidebug > 10) {
1752 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
1753 uhci_dump_tds(data);
1754 uhci_dump_qh(upipe->u.intr.qhs[0]);
1758 return (USBD_IN_PROGRESS);
1761 /* Abort a device control request. */
1763 uhci_device_ctrl_abort(xfer)
1764 usbd_xfer_handle xfer;
1766 DPRINTF(("uhci_device_ctrl_abort:\n"));
1767 uhci_abort_xfer(xfer, USBD_CANCELLED);
1770 /* Close a device control pipe. */
1772 uhci_device_ctrl_close(pipe)
1773 usbd_pipe_handle pipe;
1775 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1777 uhci_free_intr_info(upipe->iinfo);
1778 /* XXX free other resources? */
1781 /* Abort a device interrupt request. */
1783 uhci_device_intr_abort(xfer)
1784 usbd_xfer_handle xfer;
1786 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
1787 if (xfer->pipe->intrxfer == xfer) {
1788 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
1789 xfer->pipe->intrxfer = 0;
1791 uhci_abort_xfer(xfer, USBD_CANCELLED);
1794 /* Close a device interrupt pipe. */
1796 uhci_device_intr_close(pipe)
1797 usbd_pipe_handle pipe;
1799 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1800 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1803 upipe->iinfo->stdstart = 0; /* inactive */
1805 /* Unlink descriptors from controller data structures. */
1806 npoll = upipe->u.intr.npoll;
1807 uhci_lock_frames(sc);
1808 for (i = 0; i < npoll; i++)
1809 uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
1810 upipe->u.intr.qhs[i]);
1811 uhci_unlock_frames(sc);
1814 * We now have to wait for any activity on the physical
1815 * descriptors to stop.
1817 usb_delay_ms(&sc->sc_bus, 2);
1819 for(i = 0; i < npoll; i++)
1820 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
1821 free(upipe->u.intr.qhs, M_USBHC);
1824 LIST_REMOVE(upipe->iinfo, list); /* remove from active list */
1826 uhci_free_intr_info(upipe->iinfo);
1828 /* XXX free other resources */
1832 uhci_device_request(xfer)
1833 usbd_xfer_handle xfer;
1835 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1836 usb_device_request_t *req = &xfer->request;
1837 usbd_device_handle dev = upipe->pipe.device;
1838 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1839 int addr = dev->address;
1840 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1841 uhci_intr_info_t *ii = upipe->iinfo;
1842 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
1843 uhci_soft_qh_t *sqh;
1850 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
1851 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1852 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1853 UGETW(req->wIndex), UGETW(req->wLength),
1856 ls = dev->lowspeed ? UHCI_TD_LS : 0;
1857 isread = req->bmRequestType & UT_READ;
1858 len = UGETW(req->wLength);
1860 setup = upipe->u.ctl.setup;
1861 stat = upipe->u.ctl.stat;
1862 sqh = upipe->u.ctl.sqh;
1864 /* Set up data transaction */
1866 upipe->nexttoggle = 1;
1867 err = uhci_alloc_std_chain(upipe, sc, len, isread,
1868 xfer->flags & USBD_SHORT_XFER_OK,
1869 &xfer->dmabuf, &data, &dataend);
1873 dataend->link.std = stat;
1874 dataend->td.td_link = LE(stat->physaddr|UHCI_PTR_VF);
1878 upipe->u.ctl.length = len;
1880 memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
1882 setup->link.std = next;
1883 setup->td.td_link = LE(next->physaddr|UHCI_PTR_VF);
1884 setup->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls | UHCI_TD_ACTIVE);
1885 setup->td.td_token = LE(UHCI_TD_SETUP(sizeof *req, endpt, addr));
1886 setup->td.td_buffer = LE(DMAADDR(&upipe->u.ctl.reqdma));
1889 stat->td.td_link = LE(UHCI_PTR_T);
1890 stat->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls |
1891 UHCI_TD_ACTIVE | UHCI_TD_IOC);
1893 LE(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
1894 UHCI_TD_IN (0, endpt, addr, 1));
1895 stat->td.td_buffer = LE(0);
1898 if (uhcidebug > 10) {
1899 DPRINTF(("uhci_device_request: before transfer\n"));
1900 uhci_dump_tds(setup);
1904 /* Set up interrupt info. */
1906 ii->stdstart = setup;
1908 #if defined(__FreeBSD__)
1909 callout_handle_init(&ii->timeout_handle);
1916 sqh->qh.qh_elink = LE(setup->physaddr);
1917 sqh->intr_info = ii;
1920 uhci_add_ctrl(sc, sqh);
1921 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1923 if (uhcidebug > 12) {
1924 uhci_soft_td_t *std;
1925 uhci_soft_qh_t *xqh;
1926 uhci_soft_qh_t *sxqh;
1928 uhci_physaddr_t link;
1929 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
1930 for (std = sc->sc_vframes[0].htd, link = 0;
1931 (link & UHCI_PTR_Q) == 0;
1932 std = std->link.std) {
1933 link = LE(std->td.td_link);
1936 sxqh = (uhci_soft_qh_t *)std;
1940 xqh = (maxqh++ == 5 || xqh->hlink==sxqh ||
1941 xqh->hlink==xqh ? NULL : xqh->hlink)) {
1944 DPRINTF(("Enqueued QH:\n"));
1946 uhci_dump_tds(sqh->elink);
1949 if (xfer->timeout && !sc->sc_bus.use_polling) {
1950 usb_timeout(uhci_timeout, ii,
1951 MS_TO_TICKS(xfer->timeout), ii->timeout_handle);
1955 return (USBD_NORMAL_COMPLETION);
1959 uhci_device_isoc_transfer(xfer)
1960 usbd_xfer_handle xfer;
1964 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
1966 /* Put it on our queue, */
1967 err = usb_insert_transfer(xfer);
1969 /* bail out on error, */
1970 if (err && err != USBD_IN_PROGRESS)
1973 /* XXX should check inuse here */
1975 /* insert into schedule, */
1976 uhci_device_isoc_enter(xfer);
1978 /* and put on interrupt list if the pipe wasn't running */
1980 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1986 uhci_device_isoc_enter(xfer)
1987 usbd_xfer_handle xfer;
1989 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1990 usbd_device_handle dev = upipe->pipe.device;
1991 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1992 struct iso *iso = &upipe->u.iso;
1993 uhci_soft_td_t *std;
1994 u_int32_t buf, len, status;
1995 int s, i, next, nframes;
1997 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
1999 iso->inuse, iso->next, xfer, xfer->nframes));
2001 if (xfer->status == USBD_IN_PROGRESS) {
2002 /* This request has already been entered into the frame list */
2007 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2008 printf("uhci_device_isoc_enter: overflow!\n");
2013 /* Not in use yet, schedule it a few frames ahead. */
2014 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2015 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2018 xfer->status = USBD_IN_PROGRESS;
2019 xfer->hcprivint = next;
2021 buf = DMAADDR(&xfer->dmabuf);
2022 status = LE(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2025 nframes = xfer->nframes;
2027 for (i = 0; i < nframes; i++) {
2028 std = iso->stds[next];
2029 if (++next >= UHCI_VFRAMELIST_COUNT)
2031 len = xfer->frlengths[i];
2032 std->td.td_buffer = LE(buf);
2033 if (i == nframes - 1)
2034 status |= LE(UHCI_TD_IOC);
2035 std->td.td_status = status;
2036 std->td.td_token &= LE(~UHCI_TD_MAXLEN_MASK);
2037 std->td.td_token |= LE(UHCI_TD_SET_MAXLEN(len));
2039 if (uhcidebug > 5) {
2040 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2047 iso->inuse += xfer->nframes;
2053 uhci_device_isoc_start(xfer)
2054 usbd_xfer_handle xfer;
2056 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2057 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2058 uhci_intr_info_t *ii = upipe->iinfo;
2059 uhci_soft_td_t *end;
2063 if (xfer->status != USBD_IN_PROGRESS)
2064 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2067 /* Find the last TD */
2068 i = xfer->hcprivint + xfer->nframes;
2069 if (i >= UHCI_VFRAMELIST_COUNT)
2070 i -= UHCI_VFRAMELIST_COUNT;
2071 end = upipe->u.iso.stds[i];
2075 /* Set up interrupt info. */
2079 #if defined(__FreeBSD__)
2080 callout_handle_init(&ii->timeout_handle);
2085 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
2089 return (USBD_IN_PROGRESS);
2093 uhci_device_isoc_abort(xfer)
2094 usbd_xfer_handle xfer;
2096 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2097 uhci_intr_info_t *ii = upipe->iinfo;
2098 uhci_soft_td_t **stds = upipe->u.iso.stds;
2099 uhci_soft_td_t *std;
2102 /* Make interrupt routine ignore it, */
2103 xfer->status = USBD_CANCELLED;
2105 /* make hardware ignore it, */
2106 nframes = xfer->nframes;
2107 n = xfer->hcprivint;
2108 for (i = 0; i < nframes; i++) {
2110 std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2111 if (++n >= UHCI_VFRAMELIST_COUNT)
2117 /* make sure hardware has completed, */
2118 if (xfer->device->bus->intr_context) {
2119 /* We have no process context, so we can't use tsleep(). */
2120 timeout(uhci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
2122 usb_delay_ms(xfer->pipe->device->bus, 1);
2123 /* and call final part of interrupt handler. */
2124 uhci_abort_xfer_end(xfer);
2129 uhci_device_isoc_close(pipe)
2130 usbd_pipe_handle pipe;
2132 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2133 usbd_device_handle dev = upipe->pipe.device;
2134 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2135 uhci_soft_td_t *std, *vstd;
2140 * Make sure all TDs are marked as inactive.
2141 * Wait for completion.
2145 iso = &upipe->u.iso;
2147 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2148 iso->stds[i]->td.td_status &= LE(~UHCI_TD_ACTIVE);
2149 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2151 uhci_lock_frames(sc);
2152 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2154 for (vstd = sc->sc_vframes[i].htd;
2155 vstd != NULL && vstd->link.std != std;
2156 vstd = vstd->link.std)
2160 printf("uhci_device_isoc_close: %p not found\n", std);
2161 uhci_unlock_frames(sc);
2164 vstd->link = std->link;
2165 vstd->td.td_link = std->td.td_link;
2166 uhci_free_std(sc, std);
2168 uhci_unlock_frames(sc);
2170 free(iso->stds, M_USBHC);
2174 uhci_setup_isoc(pipe)
2175 usbd_pipe_handle pipe;
2177 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2178 usbd_device_handle dev = upipe->pipe.device;
2179 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2180 int addr = upipe->pipe.device->address;
2181 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2182 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2183 uhci_soft_td_t *std, *vstd;
2188 iso = &upipe->u.iso;
2189 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2192 token = LE(rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2193 UHCI_TD_OUT(0, endpt, addr, 0));
2195 /* Allocate the TDs and mark as inactive; */
2196 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2197 std = uhci_alloc_std(sc);
2200 std->td.td_status = LE(UHCI_TD_IOS); /* iso, inactive */
2201 std->td.td_token = token;
2205 /* Insert TDs into schedule. */
2206 uhci_lock_frames(sc);
2207 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2209 vstd = sc->sc_vframes[i].htd;
2210 std->link = vstd->link;
2211 std->td.td_link = vstd->td.td_link;
2212 vstd->link.std = std;
2213 vstd->td.td_link = LE(std->physaddr);
2215 uhci_unlock_frames(sc);
2220 return (USBD_NORMAL_COMPLETION);
2224 uhci_free_std(sc, iso->stds[i]);
2225 free(iso->stds, M_USBHC);
2226 return (USBD_NOMEM);
2230 uhci_device_isoc_done(xfer)
2231 usbd_xfer_handle xfer;
2233 uhci_intr_info_t *ii = xfer->hcpriv;
2235 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2237 /* Turn off the interrupt since it is active even if the TD is not. */
2238 ii->stdend->td.td_status &= LE(~UHCI_TD_IOC);
2240 LIST_REMOVE(ii, list); /* remove from active list */
2244 uhci_device_intr_done(xfer)
2245 usbd_xfer_handle xfer;
2247 uhci_intr_info_t *ii = xfer->hcpriv;
2248 uhci_softc_t *sc = ii->sc;
2249 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2250 uhci_soft_qh_t *sqh;
2253 DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
2255 npoll = upipe->u.intr.npoll;
2256 for(i = 0; i < npoll; i++) {
2257 sqh = upipe->u.intr.qhs[i];
2259 sqh->qh.qh_elink = LE(UHCI_PTR_T);
2261 uhci_free_std_chain(sc, ii->stdstart, 0);
2264 if (xfer->pipe->repeat) {
2265 uhci_soft_td_t *data, *dataend;
2267 /* This alloc cannot fail since we freed the chain above. */
2268 uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
2269 xfer->flags & USBD_SHORT_XFER_OK,
2270 &xfer->dmabuf, &data, &dataend);
2271 dataend->td.td_status |= LE(UHCI_TD_IOC);
2274 if (uhcidebug > 10) {
2275 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2276 uhci_dump_tds(data);
2277 uhci_dump_qh(upipe->u.intr.qhs[0]);
2281 ii->stdstart = data;
2282 ii->stdend = dataend;
2283 #if defined(__FreeBSD__)
2284 callout_handle_init(&ii->timeout_handle);
2289 for (i = 0; i < npoll; i++) {
2290 sqh = upipe->u.intr.qhs[i];
2292 sqh->qh.qh_elink = LE(data->physaddr);
2295 ii->stdstart = 0; /* mark as inactive */
2299 /* Deallocate request data structures */
2301 uhci_device_ctrl_done(xfer)
2302 usbd_xfer_handle xfer;
2304 uhci_intr_info_t *ii = xfer->hcpriv;
2305 uhci_softc_t *sc = ii->sc;
2306 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2309 if (!(xfer->rqflags & URQ_REQUEST))
2310 panic("uhci_ctrl_done: not a request\n");
2313 LIST_REMOVE(ii, list); /* remove from active list */
2315 uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
2317 if (upipe->u.ctl.length != 0)
2318 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2320 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
2323 /* Deallocate request data structures */
2325 uhci_device_bulk_done(xfer)
2326 usbd_xfer_handle xfer;
2328 uhci_intr_info_t *ii = xfer->hcpriv;
2329 uhci_softc_t *sc = ii->sc;
2330 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2332 LIST_REMOVE(ii, list); /* remove from active list */
2334 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2336 uhci_free_std_chain(sc, ii->stdstart, 0);
2338 DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
2341 /* Add interrupt QH, called with vflock. */
2343 uhci_add_intr(sc, n, sqh)
2346 uhci_soft_qh_t *sqh;
2348 struct uhci_vframe *vf = &sc->sc_vframes[n];
2349 uhci_soft_qh_t *eqh;
2351 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
2353 sqh->hlink = eqh->hlink;
2354 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2356 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
2361 /* Remove interrupt QH, called with vflock. */
2363 uhci_remove_intr(sc, n, sqh)
2366 uhci_soft_qh_t *sqh;
2368 struct uhci_vframe *vf = &sc->sc_vframes[n];
2369 uhci_soft_qh_t *pqh;
2371 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
2373 for (pqh = vf->hqh; pqh->hlink != sqh; pqh = pqh->hlink)
2374 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
2375 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
2376 DPRINTF(("uhci_remove_intr: QH not found\n"));
2382 pqh->hlink = sqh->hlink;
2383 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2390 uhci_device_setintr(sc, upipe, ival)
2392 struct uhci_pipe *upipe;
2395 uhci_soft_qh_t *sqh;
2397 u_int bestbw, bw, bestoffs, offs;
2399 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
2401 printf("uhci_setintr: 0 interval\n");
2402 return (USBD_INVAL);
2405 if (ival > UHCI_VFRAMELIST_COUNT)
2406 ival = UHCI_VFRAMELIST_COUNT;
2407 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2408 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
2410 upipe->u.intr.npoll = npoll;
2412 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2415 * Figure out which offset in the schedule that has most
2416 * bandwidth left over.
2418 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2419 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2420 for (bw = i = 0; i < npoll; i++)
2421 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2427 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2429 upipe->iinfo->stdstart = 0;
2430 for(i = 0; i < npoll; i++) {
2431 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2433 sqh->qh.qh_elink = LE(UHCI_PTR_T);
2434 sqh->pos = MOD(i * ival + bestoffs);
2435 sqh->intr_info = upipe->iinfo;
2440 LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
2443 uhci_lock_frames(sc);
2444 /* Enter QHs into the controller data structures. */
2445 for(i = 0; i < npoll; i++)
2446 uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
2447 upipe->u.intr.qhs[i]);
2448 uhci_unlock_frames(sc);
2450 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
2451 return (USBD_NORMAL_COMPLETION);
2454 /* Open a new pipe. */
2457 usbd_pipe_handle pipe;
2459 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2460 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2461 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2464 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2465 pipe, pipe->device->address,
2466 ed->bEndpointAddress, sc->sc_addr));
2467 if (pipe->device->address == sc->sc_addr) {
2468 switch (ed->bEndpointAddress) {
2469 case USB_CONTROL_ENDPOINT:
2470 pipe->methods = &uhci_root_ctrl_methods;
2472 case UE_DIR_IN | UHCI_INTR_ENDPT:
2473 pipe->methods = &uhci_root_intr_methods;
2476 return (USBD_INVAL);
2479 upipe->iinfo = uhci_alloc_intr_info(sc);
2480 if (upipe->iinfo == 0)
2481 return (USBD_NOMEM);
2482 switch (ed->bmAttributes & UE_XFERTYPE) {
2484 pipe->methods = &uhci_device_ctrl_methods;
2485 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2486 if (upipe->u.ctl.sqh == NULL)
2488 upipe->u.ctl.setup = uhci_alloc_std(sc);
2489 if (upipe->u.ctl.setup == NULL) {
2490 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2493 upipe->u.ctl.stat = uhci_alloc_std(sc);
2494 if (upipe->u.ctl.stat == NULL) {
2495 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2496 uhci_free_std(sc, upipe->u.ctl.setup);
2499 err = usb_allocmem(&sc->sc_bus,
2500 sizeof(usb_device_request_t),
2501 0, &upipe->u.ctl.reqdma);
2503 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2504 uhci_free_std(sc, upipe->u.ctl.setup);
2505 uhci_free_std(sc, upipe->u.ctl.stat);
2510 pipe->methods = &uhci_device_intr_methods;
2511 return (uhci_device_setintr(sc, upipe, ed->bInterval));
2512 case UE_ISOCHRONOUS:
2513 pipe->methods = &uhci_device_isoc_methods;
2514 return (uhci_setup_isoc(pipe));
2516 pipe->methods = &uhci_device_bulk_methods;
2517 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2518 if (upipe->u.bulk.sqh == NULL)
2523 return (USBD_NORMAL_COMPLETION);
2526 uhci_free_intr_info(upipe->iinfo);
2527 return (USBD_NOMEM);
2531 * Data structures and routines to emulate the root hub.
2533 usb_device_descriptor_t uhci_devd = {
2534 USB_DEVICE_DESCRIPTOR_SIZE,
2535 UDESC_DEVICE, /* type */
2536 {0x00, 0x01}, /* USB version */
2537 UCLASS_HUB, /* class */
2538 USUBCLASS_HUB, /* subclass */
2540 64, /* max packet */
2541 {0},{0},{0x00,0x01}, /* device id */
2542 1,2,0, /* string indicies */
2543 1 /* # of configurations */
2546 usb_config_descriptor_t uhci_confd = {
2547 USB_CONFIG_DESCRIPTOR_SIZE,
2549 {USB_CONFIG_DESCRIPTOR_SIZE +
2550 USB_INTERFACE_DESCRIPTOR_SIZE +
2551 USB_ENDPOINT_DESCRIPTOR_SIZE},
2559 usb_interface_descriptor_t uhci_ifcd = {
2560 USB_INTERFACE_DESCRIPTOR_SIZE,
2571 usb_endpoint_descriptor_t uhci_endpd = {
2572 USB_ENDPOINT_DESCRIPTOR_SIZE,
2574 UE_DIR_IN | UHCI_INTR_ENDPT,
2580 usb_hub_descriptor_t uhci_hubd_piix = {
2581 USB_HUB_DESCRIPTOR_SIZE,
2584 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2585 50, /* power on to power good */
2587 { 0x00 }, /* both ports are removable */
2592 usb_string_descriptor_t *p;
2600 p->bLength = 2 * strlen(s) + 2;
2603 p->bDescriptorType = UDESC_STRING;
2605 for (i = 0; s[i] && l > 1; i++, l -= 2)
2606 USETW2(p->bString[i], 0, s[i]);
2611 * Simulate a hardware hub by handling all the necessary requests.
2614 uhci_root_ctrl_transfer(xfer)
2615 usbd_xfer_handle xfer;
2619 /* Insert last in queue. */
2620 err = usb_insert_transfer(xfer);
2624 /* Pipe isn't running (otherwise err would be USBD_INPROG),
2627 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2631 uhci_root_ctrl_start(xfer)
2632 usbd_xfer_handle xfer;
2634 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2635 usb_device_request_t *req;
2638 int s, len, value, index, status, change, l, totlen = 0;
2639 usb_port_status_t ps;
2643 if (!(xfer->rqflags & URQ_REQUEST))
2644 panic("uhci_root_ctrl_transfer: not a request\n");
2646 req = &xfer->request;
2648 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
2649 req->bmRequestType, req->bRequest));
2651 len = UGETW(req->wLength);
2652 value = UGETW(req->wValue);
2653 index = UGETW(req->wIndex);
2656 buf = KERNADDR(&xfer->dmabuf);
2658 #define C(x,y) ((x) | ((y) << 8))
2659 switch(C(req->bRequest, req->bmRequestType)) {
2660 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2661 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2662 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2664 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2665 * for the integrated root hub.
2668 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2670 *(u_int8_t *)buf = sc->sc_conf;
2674 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2675 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
2676 switch(value >> 8) {
2678 if ((value & 0xff) != 0) {
2682 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2683 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
2684 memcpy(buf, &uhci_devd, l);
2687 if ((value & 0xff) != 0) {
2691 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2692 memcpy(buf, &uhci_confd, l);
2693 buf = (char *)buf + l;
2695 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2697 memcpy(buf, &uhci_ifcd, l);
2698 buf = (char *)buf + l;
2700 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2702 memcpy(buf, &uhci_endpd, l);
2707 *(u_int8_t *)buf = 0;
2709 switch (value & 0xff) {
2710 case 1: /* Vendor */
2711 totlen = uhci_str(buf, len, sc->sc_vendor);
2713 case 2: /* Product */
2714 totlen = uhci_str(buf, len, "UHCI root hub");
2723 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2725 *(u_int8_t *)buf = 0;
2729 case C(UR_GET_STATUS, UT_READ_DEVICE):
2731 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2735 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2736 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2738 USETW(((usb_status_t *)buf)->wStatus, 0);
2742 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2743 if (value >= USB_MAX_DEVICES) {
2747 sc->sc_addr = value;
2749 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2750 if (value != 0 && value != 1) {
2754 sc->sc_conf = value;
2756 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2758 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2759 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2760 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2763 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2765 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2768 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2770 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2771 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2772 "port=%d feature=%d\n",
2775 port = UHCI_PORTSC1;
2776 else if (index == 2)
2777 port = UHCI_PORTSC2;
2783 case UHF_PORT_ENABLE:
2784 x = UREAD2(sc, port);
2785 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2787 case UHF_PORT_SUSPEND:
2788 x = UREAD2(sc, port);
2789 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
2791 case UHF_PORT_RESET:
2792 x = UREAD2(sc, port);
2793 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2795 case UHF_C_PORT_CONNECTION:
2796 x = UREAD2(sc, port);
2797 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2799 case UHF_C_PORT_ENABLE:
2800 x = UREAD2(sc, port);
2801 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2803 case UHF_C_PORT_OVER_CURRENT:
2804 x = UREAD2(sc, port);
2805 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2807 case UHF_C_PORT_RESET:
2809 err = USBD_NORMAL_COMPLETION;
2811 case UHF_PORT_CONNECTION:
2812 case UHF_PORT_OVER_CURRENT:
2813 case UHF_PORT_POWER:
2814 case UHF_PORT_LOW_SPEED:
2815 case UHF_C_PORT_SUSPEND:
2821 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2823 port = UHCI_PORTSC1;
2824 else if (index == 2)
2825 port = UHCI_PORTSC2;
2832 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2833 UHCI_PORTSC_LS_SHIFT;
2837 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2842 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
2844 memcpy(buf, &uhci_hubd_piix, l);
2846 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2851 memset(buf, 0, len);
2854 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2856 port = UHCI_PORTSC1;
2857 else if (index == 2)
2858 port = UHCI_PORTSC2;
2867 x = UREAD2(sc, port);
2868 status = change = 0;
2869 if (x & UHCI_PORTSC_CCS )
2870 status |= UPS_CURRENT_CONNECT_STATUS;
2871 if (x & UHCI_PORTSC_CSC )
2872 change |= UPS_C_CONNECT_STATUS;
2873 if (x & UHCI_PORTSC_PE )
2874 status |= UPS_PORT_ENABLED;
2875 if (x & UHCI_PORTSC_POEDC)
2876 change |= UPS_C_PORT_ENABLED;
2877 if (x & UHCI_PORTSC_OCI )
2878 status |= UPS_OVERCURRENT_INDICATOR;
2879 if (x & UHCI_PORTSC_OCIC )
2880 change |= UPS_C_OVERCURRENT_INDICATOR;
2881 if (x & UHCI_PORTSC_SUSP )
2882 status |= UPS_SUSPEND;
2883 if (x & UHCI_PORTSC_LSDA )
2884 status |= UPS_LOW_SPEED;
2885 status |= UPS_PORT_POWER;
2887 change |= UPS_C_PORT_RESET;
2888 USETW(ps.wPortStatus, status);
2889 USETW(ps.wPortChange, change);
2890 l = min(len, sizeof ps);
2891 memcpy(buf, &ps, l);
2894 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2897 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2899 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2901 port = UHCI_PORTSC1;
2902 else if (index == 2)
2903 port = UHCI_PORTSC2;
2909 case UHF_PORT_ENABLE:
2910 x = UREAD2(sc, port);
2911 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2913 case UHF_PORT_SUSPEND:
2914 x = UREAD2(sc, port);
2915 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2917 case UHF_PORT_RESET:
2918 x = UREAD2(sc, port);
2919 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2920 usb_delay_ms(&sc->sc_bus, 10);
2921 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2923 x = UREAD2(sc, port);
2924 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2926 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
2927 index, UREAD2(sc, port)));
2930 case UHF_C_PORT_CONNECTION:
2931 case UHF_C_PORT_ENABLE:
2932 case UHF_C_PORT_OVER_CURRENT:
2933 case UHF_PORT_CONNECTION:
2934 case UHF_PORT_OVER_CURRENT:
2935 case UHF_PORT_POWER:
2936 case UHF_PORT_LOW_SPEED:
2937 case UHF_C_PORT_SUSPEND:
2938 case UHF_C_PORT_RESET:
2948 xfer->actlen = totlen;
2949 err = USBD_NORMAL_COMPLETION;
2954 usb_transfer_complete(xfer);
2956 return (USBD_IN_PROGRESS);
2959 /* Abort a root control request. */
2961 uhci_root_ctrl_abort(xfer)
2962 usbd_xfer_handle xfer;
2964 /* Nothing to do, all transfers are syncronous. */
2967 /* Close the root pipe. */
2969 uhci_root_ctrl_close(pipe)
2970 usbd_pipe_handle pipe;
2972 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2974 sc->sc_has_timo = 0;
2975 DPRINTF(("uhci_root_ctrl_close\n"));
2978 /* Abort a root interrupt request. */
2980 uhci_root_intr_abort(xfer)
2981 usbd_xfer_handle xfer;
2983 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2985 usb_untimeout(uhci_timo, xfer, xfer->timo_handle);
2986 sc->sc_has_timo = 0;
2988 if (xfer->pipe->intrxfer == xfer) {
2989 DPRINTF(("uhci_root_intr_abort: remove\n"));
2990 xfer->pipe->intrxfer = 0;
2992 xfer->status = USBD_CANCELLED;
2993 usb_transfer_complete(xfer);
2997 uhci_root_intr_transfer(xfer)
2998 usbd_xfer_handle xfer;
3002 /* Insert last in queue. */
3003 err = usb_insert_transfer(xfer);
3007 /* Pipe isn't running (otherwise err would be USBD_INPROG),
3010 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3013 /* Start a transfer on the root interrupt pipe */
3015 uhci_root_intr_start(xfer)
3016 usbd_xfer_handle xfer;
3018 usbd_pipe_handle pipe = xfer->pipe;
3019 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3021 DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
3022 xfer, xfer->length, xfer->flags));
3024 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3025 usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
3026 sc->sc_has_timo = xfer;
3027 return (USBD_IN_PROGRESS);
3030 /* Close the root interrupt pipe. */
3032 uhci_root_intr_close(pipe)
3033 usbd_pipe_handle pipe;
3035 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3037 usb_untimeout(uhci_timo, pipe->intrxfer, pipe->intrxfer->timo_handle);
3038 sc->sc_has_timo = 0;
3039 DPRINTF(("uhci_root_intr_close\n"));