1 /* $NetBSD: uhci.c,v 1.10 1998/08/02 22:30:52 augustss Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * Author: Lennart Augustsson <augustss@carlstedt.se>
8 * Carlstedt Research & Technology
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
40 * USB Universal Host Controller driver.
41 * Handles PIIX3 and PIIX4.
43 * Data sheets: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
44 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
45 * UHCI spec: http://www.intel.com/design/usb/uhci11d.pdf
46 * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
49 #include <dev/usb/usb_port.h>
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #if defined(__NetBSD__)
56 #include <sys/device.h>
57 #elif defined(__FreeBSD__)
58 #include <sys/module.h>
62 #include <sys/queue.h>
63 #include <sys/select.h>
65 #include <machine/bus.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdivar.h>
70 #include <dev/usb/usb_mem.h>
71 #include <dev/usb/usb_quirks.h>
73 #include <dev/usb/uhcireg.h>
74 #include <dev/usb/uhcivar.h>
76 #if defined(__FreeBSD__)
77 #include <machine/clock.h>
78 #include "dev/usb/queue.addendum.h"
80 #define delay(d) DELAY(d)
83 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
86 struct usbd_pipe pipe;
87 uhci_intr_info_t *iinfo;
89 /* Info needed for different pipe kinds. */
96 uhci_soft_td_t *setup, *stat, *xferend;
103 uhci_soft_qh_t **qhs;
116 * The uhci_intr_info free list can be global since they contain
117 * no dma specific data. The other free lists do.
119 LIST_HEAD(, uhci_intr_info) uhci_ii_free;
121 void uhci_busreset __P((uhci_softc_t *));
122 void uhci_run __P((uhci_softc_t *, int run));
123 uhci_soft_td_t *uhci_alloc_std __P((uhci_softc_t *));
124 void uhci_free_std __P((uhci_softc_t *, uhci_soft_td_t *));
125 uhci_soft_qh_t *uhci_alloc_sqh __P((uhci_softc_t *));
126 void uhci_free_sqh __P((uhci_softc_t *, uhci_soft_qh_t *));
127 uhci_intr_info_t *uhci_alloc_intr_info __P((uhci_softc_t *));
128 void uhci_free_intr_info __P((uhci_intr_info_t *ii));
129 void uhci_enter_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *,
130 uhci_intr_info_t *));
131 void uhci_exit_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *));
133 void uhci_free_std_chain __P((uhci_softc_t *,
134 uhci_soft_td_t *, uhci_soft_td_t *));
135 usbd_status uhci_alloc_std_chain __P((struct uhci_pipe *, uhci_softc_t *,
136 int, int, usb_dma_t *,
139 void uhci_timo __P((void *));
140 void uhci_waitintr __P((uhci_softc_t *, usbd_request_handle));
141 void uhci_check_intr __P((uhci_softc_t *, uhci_intr_info_t *));
142 void uhci_ii_done __P((uhci_intr_info_t *, int));
143 void uhci_timeout __P((void *));
144 void uhci_wakeup_ctrl __P((void *, int, int, void *, int));
145 void uhci_lock_frames __P((uhci_softc_t *));
146 void uhci_unlock_frames __P((uhci_softc_t *));
147 void uhci_add_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
148 void uhci_add_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
149 void uhci_remove_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
150 void uhci_remove_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
151 int uhci_str __P((usb_string_descriptor_t *, int, char *));
153 void uhci_device_close __P((struct uhci_pipe *));
155 void uhci_wakeup_cb __P((usbd_request_handle reqh));
157 usbd_status uhci_device_ctrl_transfer __P((usbd_request_handle));
158 void uhci_device_ctrl_abort __P((usbd_request_handle));
159 void uhci_device_ctrl_close __P((usbd_pipe_handle));
160 usbd_status uhci_device_intr_transfer __P((usbd_request_handle));
161 void uhci_device_intr_abort __P((usbd_request_handle));
162 void uhci_device_intr_close __P((usbd_pipe_handle));
163 usbd_status uhci_device_bulk_transfer __P((usbd_request_handle));
164 void uhci_device_bulk_abort __P((usbd_request_handle));
165 void uhci_device_bulk_close __P((usbd_pipe_handle));
167 usbd_status uhci_root_ctrl_transfer __P((usbd_request_handle));
168 void uhci_root_ctrl_abort __P((usbd_request_handle));
169 void uhci_root_ctrl_close __P((usbd_pipe_handle));
170 usbd_status uhci_root_intr_transfer __P((usbd_request_handle));
171 void uhci_root_intr_abort __P((usbd_request_handle));
172 void uhci_root_intr_close __P((usbd_pipe_handle));
174 usbd_status uhci_open __P((usbd_pipe_handle));
175 void uhci_poll __P((struct usbd_bus *));
177 usbd_status uhci_device_request __P((usbd_request_handle reqh));
178 void uhci_ctrl_done __P((uhci_intr_info_t *ii));
179 void uhci_bulk_done __P((uhci_intr_info_t *ii));
181 void uhci_add_intr __P((uhci_softc_t *, int, uhci_soft_qh_t *));
182 void uhci_remove_intr __P((uhci_softc_t *, int, uhci_soft_qh_t *));
183 usbd_status uhci_device_setintr __P((uhci_softc_t *sc,
184 struct uhci_pipe *pipe, int ival));
185 void uhci_intr_done __P((uhci_intr_info_t *ii));
188 static void uhci_dumpregs __P((uhci_softc_t *));
189 void uhci_dump_tds __P((uhci_soft_td_t *));
190 void uhci_dump_qh __P((uhci_soft_qh_t *));
191 void uhci_dump __P((void));
192 void uhci_dump_td __P((uhci_soft_td_t *));
195 #if defined(__NetBSD__)
196 #define UWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
197 #define UWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
198 #define UREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
199 #define UREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
200 #elif defined(__FreeBSD__)
201 #define UWRITE2(sc,r,x) outw((sc)->sc_iobase + (r), (x))
202 #define UWRITE4(sc,r,x) outl((sc)->sc_iobase + (r), (x))
203 #define UREAD2(sc,r) inw((sc)->sc_iobase + (r))
204 #define UREAD4(sc,r) inl((sc)->sc_iobase + (r))
207 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
208 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
210 #define UHCI_RESET_TIMEOUT 100 /* reset timeout */
211 #define UHCI_CTRL_TIMEOUT 500 /* control transaction timeout */
212 #define UHCI_ISO_DELAY 50 /* delay of start of iso */
214 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
216 #define UHCI_INTR_ENDPT 1
218 struct usbd_methods uhci_root_ctrl_methods = {
219 uhci_root_ctrl_transfer,
220 uhci_root_ctrl_abort,
221 uhci_root_ctrl_close,
225 struct usbd_methods uhci_root_intr_methods = {
226 uhci_root_intr_transfer,
227 uhci_root_intr_abort,
228 uhci_root_intr_close,
232 struct usbd_methods uhci_device_ctrl_methods = {
233 uhci_device_ctrl_transfer,
234 uhci_device_ctrl_abort,
235 uhci_device_ctrl_close,
239 struct usbd_methods uhci_device_intr_methods = {
240 uhci_device_intr_transfer,
241 uhci_device_intr_abort,
242 uhci_device_intr_close,
246 struct usbd_methods uhci_device_bulk_methods = {
247 uhci_device_bulk_transfer,
248 uhci_device_bulk_abort,
249 uhci_device_bulk_close,
257 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
258 usbd_delay_ms(&sc->sc_bus, USB_RESET_DELAY); /* wait at least 10ms */
259 UHCICMD(sc, 0); /* do nothing */
268 uhci_soft_qh_t *csqh, *bsqh, *sqh;
271 static int uhci_global_init_done = 0;
273 DPRINTFN(1,("uhci_init: start\n"));
275 if (!uhci_global_init_done) {
276 uhci_global_init_done = 1;
277 LIST_INIT(&uhci_ii_free);
280 #if defined(USB_DEBUG)
285 uhci_run(sc, 0); /* stop the controller */
286 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
288 /* Allocate and initialize real frame array. */
289 r = usb_allocmem(sc->sc_dmatag,
290 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
291 UHCI_FRAMELIST_ALIGN, &dma);
292 if (r != USBD_NORMAL_COMPLETION)
294 sc->sc_pframes = KERNADDR(&dma);
295 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
296 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&dma)); /* set frame list */
300 /* Allocate the dummy QH where bulk traffic will be queued. */
301 bsqh = uhci_alloc_sqh(sc);
304 bsqh->qh->qh_hlink = UHCI_PTR_T; /* end of QH chain */
305 bsqh->qh->qh_elink = UHCI_PTR_T;
306 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
308 /* Allocate the dummy QH where control traffic will be queued. */
309 csqh = uhci_alloc_sqh(sc);
312 csqh->qh->hlink = bsqh;
313 csqh->qh->qh_hlink = bsqh->physaddr | UHCI_PTR_Q;
314 csqh->qh->qh_elink = UHCI_PTR_T;
315 sc->sc_ctl_start = sc->sc_ctl_end = csqh;
318 * Make all (virtual) frame list pointers point to the interrupt
319 * queue heads and the interrupt queue heads at the control
320 * queue head and point the physical frame list to the virtual.
322 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
323 std = uhci_alloc_std(sc);
324 sqh = uhci_alloc_sqh(sc);
327 std->td->link.sqh = sqh;
328 std->td->td_link = sqh->physaddr | UHCI_PTR_Q;
329 std->td->td_status = UHCI_TD_IOS; /* iso, inactive */
330 std->td->td_token = 0;
331 std->td->td_buffer = 0;
332 sqh->qh->hlink = csqh;
333 sqh->qh->qh_hlink = csqh->physaddr | UHCI_PTR_Q;
335 sqh->qh->qh_elink = UHCI_PTR_T;
336 sc->sc_vframes[i].htd = std;
337 sc->sc_vframes[i].etd = std;
338 sc->sc_vframes[i].hqh = sqh;
339 sc->sc_vframes[i].eqh = sqh;
341 j < UHCI_FRAMELIST_COUNT;
342 j += UHCI_VFRAMELIST_COUNT)
343 sc->sc_pframes[j] = std->physaddr;
346 LIST_INIT(&sc->sc_intrhead);
348 /* Set up the bus struct. */
349 sc->sc_bus.open_pipe = uhci_open;
350 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
351 sc->sc_bus.do_poll = uhci_poll;
353 DPRINTFN(1,("uhci_init: enabling\n"));
354 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
355 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
357 uhci_run(sc, 1); /* and here we go... */
358 return (USBD_NORMAL_COMPLETION);
366 DEVICE_MSG(sc->sc_bus.bdev,("regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
367 UREAD2(sc, UHCI_CMD),
368 UREAD2(sc, UHCI_STS),
369 UREAD2(sc, UHCI_INTR),
370 UREAD2(sc, UHCI_FRNUM),
371 UREAD2(sc, UHCI_FLBASEADDR),
372 UREAD2(sc, UHCI_SOF),
373 UREAD2(sc, UHCI_PORTSC1),
374 UREAD2(sc, UHCI_PORTSC2)));
383 printf("TD(%p) at %08lx = 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",
384 p, (long)p->physaddr,
385 (long)p->td->td_link,
386 (long)p->td->td_status,
387 (long)p->td->td_token,
388 (long)p->td->td_buffer);
390 printf(" %b %b,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,D=%d,maxlen=%d\n",
391 (long)p->td->td_link,
393 (long)p->td->td_status,
394 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
395 UHCI_TD_GET_ERRCNT(p->td->td_status),
396 UHCI_TD_GET_ACTLEN(p->td->td_status),
397 UHCI_TD_GET_PID(p->td->td_token),
398 UHCI_TD_GET_DEVADDR(p->td->td_token),
399 UHCI_TD_GET_ENDPT(p->td->td_token),
400 UHCI_TD_GET_DT(p->td->td_token),
401 UHCI_TD_GET_MAXLEN(p->td->td_token));
408 printf("QH(%p) at %08x: hlink=%08x elink=%08x\n", p, (int)p->physaddr,
409 p->qh->qh_hlink, p->qh->qh_elink);
417 uhci_softc_t *sc = uhci;
420 printf("intrs=%d\n", sc->sc_intrs);
421 printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);
422 uhci_dump_qh(sc->sc_ctl_start->qh->hlink);
432 for(p = std; p; p = p->td->link.std)
438 * This routine is executed periodically and simulates interrupts
439 * from the root controller interrupt pipe for port status change.
445 usbd_request_handle reqh = addr;
446 usbd_pipe_handle pipe = reqh->pipe;
447 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
448 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
452 DPRINTFN(15, ("uhci_timo\n"));
454 p = KERNADDR(&upipe->u.intr.datadma);
456 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
458 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
462 reqh->status = USBD_NORMAL_COMPLETION;
467 if (reqh->pipe->intrreqh == reqh) {
468 #if defined(__NetBSD__)
469 timeout(uhci_timo, reqh, sc->sc_ival);
470 #elif defined(__FreeBSD__)
471 /* To avoid race conditions we first initialise the struct
472 * before we use it. The timeout might happen between the
473 * setting of the timeout and the setting of timo_handle
475 callout_handle_init(&reqh->timo_handle);
476 reqh->timo_handle = timeout(uhci_timo, reqh, sc->sc_ival);
479 usb_freemem(sc->sc_dmatag, &upipe->u.intr.datadma);
489 while (sc->sc_vflock) {
490 sc->sc_vflock |= UHCI_WANT_LOCK;
491 tsleep(&sc->sc_vflock, PRIBIO, "uhcqhl", 0);
493 sc->sc_vflock = UHCI_HAS_LOCK;
498 uhci_unlock_frames(sc)
502 sc->sc_vflock &= ~UHCI_HAS_LOCK;
503 if (sc->sc_vflock & UHCI_WANT_LOCK)
504 wakeup(&sc->sc_vflock);
509 * Allocate an interrupt information struct. A free list is kept
510 * for fast allocation.
513 uhci_alloc_intr_info(sc)
516 uhci_intr_info_t *ii;
518 ii = LIST_FIRST(&uhci_ii_free);
520 LIST_REMOVE(ii, list);
522 ii = malloc(sizeof(uhci_intr_info_t), M_USBDEV, M_NOWAIT);
529 uhci_free_intr_info(ii)
530 uhci_intr_info_t *ii;
532 LIST_INSERT_HEAD(&uhci_ii_free, ii, list); /* and put on free list */
535 /* Add control QH, called at splusb(). */
537 uhci_add_ctrl(sc, sqh)
543 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
544 eqh = sc->sc_ctl_end->qh;
545 sqh->qh->hlink = eqh->hlink;
546 sqh->qh->qh_hlink = eqh->qh_hlink;
548 eqh->qh_hlink = sqh->physaddr | UHCI_PTR_Q;
549 sc->sc_ctl_end = sqh;
552 /* Remove control QH, called at splusb(). */
554 uhci_remove_ctrl(sc, sqh)
560 DPRINTFN(10, ("uhci_remove_ctrl: sqh=%p\n", sqh));
561 for (pqh = sc->sc_ctl_start; pqh->qh->hlink != sqh; pqh=pqh->qh->hlink)
562 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
563 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
564 printf("uhci_remove_ctrl: QH not found\n");
570 pqh->qh->hlink = sqh->qh->hlink;
571 pqh->qh->qh_hlink = sqh->qh->qh_hlink;
572 if (sc->sc_ctl_end == sqh)
573 sc->sc_ctl_end = pqh;
576 /* Add bulk QH, called at splusb(). */
578 uhci_add_bulk(sc, sqh)
584 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
585 eqh = sc->sc_bulk_end->qh;
586 sqh->qh->hlink = eqh->hlink;
587 sqh->qh->qh_hlink = eqh->qh_hlink;
589 eqh->qh_hlink = sqh->physaddr | UHCI_PTR_Q;
590 sc->sc_bulk_end = sqh;
593 /* Remove bulk QH, called at splusb(). */
595 uhci_remove_bulk(sc, sqh)
601 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
602 for (pqh = sc->sc_bulk_start; pqh->qh->hlink != sqh; pqh=pqh->qh->hlink)
603 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
604 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
605 printf("uhci_remove_bulk: QH not found\n");
611 pqh->qh->hlink = sqh->qh->hlink;
612 pqh->qh->qh_hlink = sqh->qh->qh_hlink;
613 if (sc->sc_bulk_end == sqh)
614 sc->sc_bulk_end = pqh;
621 uhci_softc_t *sc = p;
623 uhci_intr_info_t *ii;
626 #if defined(USB_DEBUG)
628 DEVICE_MSG(sc->sc_bus.bdev, ("uhci_intr %p\n", sc));
632 status = UREAD2(sc, UHCI_STS);
634 if (status & UHCI_STS_USBINT) {
635 UWRITE2(sc, UHCI_STS, UHCI_STS_USBINT); /* acknowledge */
638 if (status & UHCI_STS_USBEI) {
639 UWRITE2(sc, UHCI_STS, UHCI_STS_USBEI); /* acknowledge */
642 if (status & UHCI_STS_RD) {
643 UWRITE2(sc, UHCI_STS, UHCI_STS_RD); /* acknowledge */
644 DEVICE_MSG(sc->sc_bus.bdev, ("resume detect\n"));
647 if (status & UHCI_STS_HSE) {
648 UWRITE2(sc, UHCI_STS, UHCI_STS_HSE); /* acknowledge */
649 DEVICE_MSG(sc->sc_bus.bdev, ("Host System Error\n"));
652 if (status & UHCI_STS_HCPE) {
653 UWRITE2(sc, UHCI_STS, UHCI_STS_HCPE); /* acknowledge */
654 DEVICE_MSG(sc->sc_bus.bdev, ("Host System Error\n"));
657 if (status & UHCI_STS_HCH)
658 DEVICE_ERROR(sc->sc_bus.bdev, ("controller halted\n"));
663 * Interrupts on UHCI really suck. When the host controller
664 * interrupts because a transfer is completed there is no
665 * way of knowing which transfer it was. You can scan down
666 * the TDs and QHs of the previous frame to limit the search,
667 * but that assumes that the interrupt was not delayed by more
668 * than 1 ms, which may not always be true (e.g. after debug
669 * output on a slow console).
670 * We scan all interrupt descriptors to see if any have
673 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
674 uhci_check_intr(sc, ii);
676 DPRINTFN(10, ("uhci_intr: exit\n"));
680 /* Check for an interrupt. */
682 uhci_check_intr(sc, ii)
684 uhci_intr_info_t *ii;
686 struct uhci_pipe *upipe;
687 uhci_soft_td_t *std, *lstd;
689 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
692 printf("uhci_check_intr: no ii? %p\n", ii);
701 printf("uhci_check_intr: std==0\n");
705 /* If the last TD is still active the whole transfer probably is. */
706 if (lstd->td->td_status & UHCI_TD_ACTIVE) {
707 DPRINTFN(15, ("uhci_check_intr: active ii=%p\n", ii));
708 for (std = ii->stdstart; std != lstd; std = std->td->link.std)
709 if (std->td->td_status & UHCI_TD_STALLED)
711 DPRINTFN(15, ("uhci_check_intr: ii=%p still active\n", ii));
715 upipe = (struct uhci_pipe *)ii->reqh->pipe;
716 upipe->pipe.endpoint->toggle = upipe->newtoggle;
718 #if defined(__NetBSD__)
719 untimeout(uhci_timeout, ii);
720 #elif defined(__FreeBSD__)
721 untimeout(uhci_timeout, ii, ii->timeout_handle);
726 uhci_ii_done(ii, timo)
727 uhci_intr_info_t *ii;
730 usbd_request_handle reqh = ii->reqh;
735 DPRINTFN(10, ("uhci_ii_done: ii=%p ready %d\n", ii, timo));
741 printf("uhci_ii_done: is done!\n");
750 /* The transfer is done, compute length and status. */
751 for (len = status = 0, std = ii->stdstart;
753 std = std->td->link.std) {
754 tst = std->td->td_status;
757 if ((tst & UHCI_TD_ERROR) && uhcidebug>10) {
758 printf("uhci_ii_done: intr error TD:\n");
762 if (UHCI_TD_GET_PID(std->td->td_token) != UHCI_TD_PID_SETUP)
763 len += UHCI_TD_GET_ACTLEN(tst);
765 status &= UHCI_TD_ERROR;
766 DPRINTFN(10, ("uhci_ii_done: len=%d\n", len));
769 ("uhci_ii_done: error, status 0x%b\n", (long)status,
770 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27STALLED\30ACTIVE"));
771 if (status == UHCI_TD_STALLED)
772 reqh->status = USBD_STALLED;
774 reqh->status = USBD_IOERROR; /* more info XXX */
777 reqh->status = USBD_NORMAL_COMPLETION;
781 /* We got a timeout. Make sure transaction is not active. */
782 reqh->status = USBD_TIMEOUT;
783 for (std = ii->stdstart; std != 0; std = std->td->link.std)
784 std->td->td_status &= ~UHCI_TD_ACTIVE;
785 /* XXX should we wait 1 ms */
787 DPRINTFN(5, ("uhci_ii_done: calling handler ii=%p\n", ii));
789 switch (reqh->pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
794 printf("uhci_ii_done: ISO??\n");
804 /* And finally execute callback. */
812 uhci_intr_info_t *ii = addr;
815 DPRINTF(("uhci_timeout: ii=%p\n", ii));
822 * Wait here until controller claims to have an interrupt.
823 * Then call uhci_intr and return. Use timeout to avoid waiting
827 uhci_waitintr(sc, reqh)
829 usbd_request_handle reqh;
831 int timo = reqh->timeout;
835 DPRINTFN(10,("uhci_waitintr: timout = %ds\n", timo));
837 reqh->status = USBD_IN_PROGRESS;
838 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 100000) {
839 /* NWH replaced by usbd_delay_ms
841 NWH and descreased frequency from 1ms to 100ms, see also usecs -=...
843 usbd_delay_ms(&(sc->sc_bus), 100);
845 DPRINTFN(10,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
847 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
849 if (reqh->status != USBD_IN_PROGRESS)
853 reqh->status = USBD_TIMEOUT;
859 struct usbd_bus *bus;
861 uhci_softc_t *sc = (uhci_softc_t *)bus;
863 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
872 uhci_softc_t *sc = p;
875 UHCICMD(sc, UHCI_CMD_HCRESET);
876 /* The reset bit goes low when the controller is done. */
877 for (n = 0; n < UHCI_RESET_TIMEOUT &&
878 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
880 if (n >= UHCI_RESET_TIMEOUT)
881 DEVICE_ERROR(sc->sc_bus.bdev, ("controller did not reset\n"));
892 run = (run? UHCI_STS_HCH:0);
893 s = splusb(); /* XXX really? */
894 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
895 if (run == running) {
899 UWRITE2(sc, UHCI_CMD, run ? UHCI_CMD_RS : 0);
900 for(n = 0; n < 100; n++) {
901 /* XXX NWH should this not be a delay of 2ms (>1024usecs)
902 * followed by one check? This fails in case a large transfer
903 * is going on on a fast processor.
905 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
906 /* return when we've entered the state we want */
907 if (run == running) {
913 DEVICE_ERROR(sc->sc_bus.bdev, ("cannot %s\n", (run ? "start" : "stop")));
917 * Memory management routines.
918 * uhci_alloc_std allocates TDs
919 * uhci_alloc_sqh allocates QHs
920 * These two routines do their own free list management,
921 * partly for speed, partly because allocating DMAable memory
922 * has page size granularaity so much memory would be wasted if
923 * only one TD/QH (32 bytes) was placed in each alloacted chunk.
935 if (!sc->sc_freetds) {
936 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
937 std = malloc(sizeof(uhci_soft_td_t) * UHCI_TD_CHUNK,
941 r = usb_allocmem(sc->sc_dmatag, UHCI_TD_SIZE * UHCI_TD_CHUNK,
942 UHCI_TD_ALIGN, &dma);
943 if (r != USBD_NORMAL_COMPLETION) {
947 for(i = 0; i < UHCI_TD_CHUNK; i++, std++) {
948 std->physaddr = DMAADDR(&dma) +
950 std->td = (uhci_td_t *)
951 ((char *)KERNADDR(&dma) + i * UHCI_TD_SIZE);
952 std->td->link.std = sc->sc_freetds;
953 sc->sc_freetds = std;
956 std = sc->sc_freetds;
957 sc->sc_freetds = std->td->link.std;
958 memset(std->td, 0, UHCI_TD_SIZE);
963 uhci_free_std(sc, std)
968 #define TD_IS_FREE 0x12345678
969 if (std->td->td_token == TD_IS_FREE) {
970 printf("uhci_free_std: freeing free TD %p\n", std);
973 std->td->td_token = TD_IS_FREE;
975 std->td->link.std = sc->sc_freetds;
976 sc->sc_freetds = std;
988 if (!sc->sc_freeqhs) {
989 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
990 sqh = malloc(sizeof(uhci_soft_qh_t) * UHCI_QH_CHUNK,
994 r = usb_allocmem(sc->sc_dmatag, UHCI_QH_SIZE * UHCI_QH_CHUNK,
995 UHCI_QH_ALIGN, &dma);
996 if (r != USBD_NORMAL_COMPLETION) {
1000 for(i = 0; i < UHCI_QH_CHUNK; i++, sqh++) {
1001 offs = i * UHCI_QH_SIZE;
1002 sqh->physaddr = DMAADDR(&dma) + offs;
1003 sqh->qh = (uhci_qh_t *)
1004 ((char *)KERNADDR(&dma) + offs);
1005 sqh->qh->hlink = sc->sc_freeqhs;
1006 sc->sc_freeqhs = sqh;
1009 sqh = sc->sc_freeqhs;
1010 sc->sc_freeqhs = sqh->qh->hlink;
1011 memset(sqh->qh, 0, UHCI_QH_SIZE);
1016 uhci_free_sqh(sc, sqh)
1018 uhci_soft_qh_t *sqh;
1020 sqh->qh->hlink = sc->sc_freeqhs;
1021 sc->sc_freeqhs = sqh;
1025 * Enter a list of transfers onto a control queue.
1026 * Called at splusb()
1029 uhci_enter_ctl_q(sc, sqh, ii)
1031 uhci_soft_qh_t *sqh;
1032 uhci_intr_info_t *ii;
1034 DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
1039 uhci_free_std_chain(sc, std, stdend)
1041 uhci_soft_td_t *std;
1042 uhci_soft_td_t *stdend;
1046 for (; std != stdend; std = p) {
1047 p = std->td->link.std;
1048 uhci_free_std(sc, std);
1053 uhci_alloc_std_chain(upipe, sc, len, rd, dma, sp, ep)
1054 struct uhci_pipe *upipe;
1058 uhci_soft_td_t **sp, **ep;
1060 uhci_soft_td_t *p, *lastp;
1061 uhci_physaddr_t lastlink;
1063 int i, ntd, l, tog, maxp;
1064 int addr = upipe->pipe.device->address;
1065 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1067 DPRINTFN(15, ("uhci_alloc_std_chain: len=%d\n", len));
1070 printf("uhci_alloc_std_chain: len=0\n");
1071 return (USBD_NORMAL_COMPLETION);
1073 ls = upipe->pipe.device->lowspeed ? UHCI_TD_LS : 0;
1074 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1076 printf("uhci_alloc_std_chain: maxp=0\n");
1077 return (USBD_INVAL);
1079 ntd = (len + maxp - 1) / maxp;
1080 tog = upipe->pipe.endpoint->toggle;
1083 upipe->newtoggle = tog ^ 1;
1085 lastlink = UHCI_PTR_T;
1087 for (i = ntd; i >= 0; i--) {
1088 p = uhci_alloc_std(sc);
1090 uhci_free_std_chain(sc, lastp, 0);
1091 return (USBD_NOMEM);
1093 p->td->link.std = lastp;
1094 p->td->td_link = lastlink;
1096 lastlink = p->physaddr;
1097 p->td->td_status = UHCI_TD_SET_ERRCNT(2) | ls | UHCI_TD_ACTIVE;
1101 if (l == 0) l = maxp;
1106 rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1107 UHCI_TD_OUT(l, endpt, addr, tog);
1108 p->td->td_buffer = DMAADDR(dma) + i * maxp;
1112 /*upipe->pipe.endpoint->toggle = tog;*/
1113 DPRINTFN(10, ("uhci_alloc_std_chain: oldtog=%d newtog=%d\n",
1114 upipe->pipe.endpoint->toggle, upipe->newtoggle));
1115 return (USBD_NORMAL_COMPLETION);
1119 uhci_device_bulk_transfer(reqh)
1120 usbd_request_handle reqh;
1122 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1123 usbd_device_handle dev = upipe->pipe.device;
1124 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1125 uhci_intr_info_t *ii = upipe->iinfo;
1126 uhci_soft_td_t *xfer, *xferend;
1127 uhci_soft_qh_t *sqh;
1133 DPRINTFN(3, ("uhci_device_bulk_transfer: reqh=%p buf=%p len=%d flags=%d\n",
1134 reqh, reqh->buffer, reqh->length, reqh->flags));
1137 panic("uhci_device_bulk_transfer: a request\n");
1140 dmap = &upipe->u.bulk.datadma;
1141 isread = reqh->pipe->endpoint->edesc->bEndpointAddress & UE_IN;
1142 sqh = upipe->u.bulk.sqh;
1144 upipe->u.bulk.isread = isread;
1145 upipe->u.bulk.length = len;
1147 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1148 if (r != USBD_NORMAL_COMPLETION)
1150 r = uhci_alloc_std_chain(upipe, sc, len, isread,
1151 dmap, &xfer, &xferend);
1152 if (r != USBD_NORMAL_COMPLETION)
1154 xferend->td->td_status |= UHCI_TD_IOC;
1156 if (!isread && len != 0)
1157 memcpy(KERNADDR(dmap), reqh->buffer, len);
1160 if (uhcidebug > 10) {
1161 printf("uhci_device_bulk_transfer: xfer(1)\n");
1162 uhci_dump_tds(xfer);
1166 /* Set up interrupt info. */
1168 ii->stdstart = xfer;
1169 ii->stdend = xferend;
1174 sqh->qh->elink = xfer;
1175 sqh->qh->qh_elink = xfer->physaddr;
1176 sqh->intr_info = ii;
1179 uhci_add_bulk(sc, sqh);
1180 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1182 if (reqh->timeout && !sc->sc_bus.use_polling) {
1183 #if defined(__NetBSD__)
1184 timeout(uhci_timeout, ii, MS_TO_TICKS(reqh->timeout));
1185 #elif defined(__FreeBSD__)
1186 /* To avoid race conditions we first initialise the struct
1187 * before we use it. The timeout might happen between the
1188 * setting of the timeout and the setting of timeout_handle
1190 callout_handle_init(&ii->timeout_handle);
1191 ii->timeout_handle = timeout(uhci_timeout, ii,
1192 MS_TO_TICKS(reqh->timeout));
1198 if (uhcidebug > 10) {
1199 printf("uhci_device_bulk_transfer: xfer(2)\n");
1200 uhci_dump_tds(xfer);
1204 return (USBD_IN_PROGRESS);
1208 usb_freemem(sc->sc_dmatag, dmap);
1213 /* Abort a device bulk request. */
1215 uhci_device_bulk_abort(reqh)
1216 usbd_request_handle reqh;
1218 /* XXX inactivate */
1219 usbd_delay_ms(reqh->pipe->device->bus, 1); /* make sure it is finished */
1223 /* Close a device bulk pipe. */
1225 uhci_device_bulk_close(pipe)
1226 usbd_pipe_handle pipe;
1228 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1229 usbd_device_handle dev = upipe->pipe.device;
1230 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1232 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1233 uhci_free_intr_info(upipe->iinfo);
1234 /* XXX free other resources */
1238 uhci_device_ctrl_transfer(reqh)
1239 usbd_request_handle reqh;
1241 uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
1245 panic("uhci_device_ctrl_transfer: not a request\n");
1247 r = uhci_device_request(reqh);
1248 if (r != USBD_NORMAL_COMPLETION)
1251 if (sc->sc_bus.use_polling)
1252 uhci_waitintr(sc, reqh);
1253 return (USBD_IN_PROGRESS);
1257 uhci_device_intr_transfer(reqh)
1258 usbd_request_handle reqh;
1260 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1261 usbd_device_handle dev = upipe->pipe.device;
1262 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1263 uhci_intr_info_t *ii = upipe->iinfo;
1264 uhci_soft_td_t *xfer, *xferend;
1265 uhci_soft_qh_t *sqh;
1271 DPRINTFN(3, ("uhci_device_intr_transfer: reqh=%p buf=%p len=%d flags=%d\n",
1272 reqh, reqh->buffer, reqh->length, reqh->flags));
1275 panic("uhci_device_intr_transfer: a request\n");
1278 dmap = &upipe->u.intr.datadma;
1280 return (USBD_INVAL); /* XXX should it be? */
1282 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1283 if (r != USBD_NORMAL_COMPLETION)
1285 r = uhci_alloc_std_chain(upipe, sc, len, 1, dmap, &xfer, &xferend);
1286 if (r != USBD_NORMAL_COMPLETION)
1288 xferend->td->td_status |= UHCI_TD_IOC;
1291 if (uhcidebug > 10) {
1292 printf("uhci_device_intr_transfer: xfer(1)\n");
1293 uhci_dump_tds(xfer);
1294 uhci_dump_qh(upipe->u.intr.qhs[0]);
1299 /* Set up interrupt info. */
1301 ii->stdstart = xfer;
1302 ii->stdend = xferend;
1307 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n", upipe->u.intr.qhs[0]));
1308 for (i = 0; i < upipe->u.intr.npoll; i++) {
1309 sqh = upipe->u.intr.qhs[i];
1310 sqh->qh->elink = xfer;
1311 sqh->qh->qh_elink = xfer->physaddr;
1316 if (uhcidebug > 10) {
1317 printf("uhci_device_intr_transfer: xfer(2)\n");
1318 uhci_dump_tds(xfer);
1319 uhci_dump_qh(upipe->u.intr.qhs[0]);
1323 return (USBD_IN_PROGRESS);
1327 usb_freemem(sc->sc_dmatag, dmap);
1332 /* Abort a device control request. */
1334 uhci_device_ctrl_abort(reqh)
1335 usbd_request_handle reqh;
1337 /* XXX inactivate */
1338 usbd_delay_ms(reqh->pipe->device->bus, 1); /* make sure it is finished */
1342 /* Close a device control pipe. */
1344 uhci_device_ctrl_close(pipe)
1345 usbd_pipe_handle pipe;
1347 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1349 uhci_free_intr_info(upipe->iinfo);
1350 /* XXX free other resources */
1353 /* Abort a device interrupt request. */
1355 uhci_device_intr_abort(reqh)
1356 usbd_request_handle reqh;
1358 struct uhci_pipe *upipe;
1360 DPRINTFN(1, ("uhci_device_intr_abort: reqh=%p\n", reqh));
1361 /* XXX inactivate */
1362 usbd_delay_ms(reqh->pipe->device->bus, 2); /* make sure it is finished */
1363 if (reqh->pipe->intrreqh == reqh) {
1364 DPRINTF(("uhci_device_intr_abort: remove\n"));
1365 reqh->pipe->intrreqh = 0;
1366 upipe = (struct uhci_pipe *)reqh->pipe;
1367 uhci_intr_done(upipe->u.intr.qhs[0]->intr_info);
1371 /* Close a device interrupt pipe. */
1373 uhci_device_intr_close(pipe)
1374 usbd_pipe_handle pipe;
1376 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1377 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1380 upipe->iinfo->stdstart = 0; /* inactive */
1382 /* Unlink descriptors from controller data structures. */
1383 npoll = upipe->u.intr.npoll;
1384 uhci_lock_frames(sc);
1385 for (i = 0; i < npoll; i++)
1386 uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
1387 upipe->u.intr.qhs[i]);
1388 uhci_unlock_frames(sc);
1391 * We now have to wait for any activity on the physical
1392 * descriptors to stop.
1394 usbd_delay_ms(&sc->sc_bus, 2);
1396 for(i = 0; i < npoll; i++)
1397 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
1398 free(upipe->u.intr.qhs, M_USB);
1401 LIST_REMOVE(upipe->iinfo, list); /* remove from active list */
1403 uhci_free_intr_info(upipe->iinfo);
1405 /* XXX free other resources */
1409 uhci_device_request(reqh)
1410 usbd_request_handle reqh;
1412 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1413 usb_device_request_t *req = &reqh->request;
1414 usbd_device_handle dev = upipe->pipe.device;
1415 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1416 int addr = dev->address;
1417 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1418 uhci_intr_info_t *ii = upipe->iinfo;
1419 uhci_soft_td_t *setup, *xfer, *stat, *next, *xferend;
1420 uhci_soft_qh_t *sqh;
1428 DPRINTFN(5,("uhci_device_control type=0x%02x, request=0x%02x, wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1429 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1430 UGETW(req->wIndex), UGETW(req->wLength),
1433 ls = dev->lowspeed ? UHCI_TD_LS : 0;
1434 isread = req->bmRequestType & UT_READ;
1435 len = UGETW(req->wLength);
1437 setup = upipe->u.ctl.setup;
1438 stat = upipe->u.ctl.stat;
1439 sqh = upipe->u.ctl.sqh;
1440 dmap = &upipe->u.ctl.datadma;
1442 /* Set up data transaction */
1444 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1445 if (r != USBD_NORMAL_COMPLETION)
1447 upipe->pipe.endpoint->toggle = 1;
1448 r = uhci_alloc_std_chain(upipe, sc, len, isread,
1449 dmap, &xfer, &xferend);
1450 if (r != USBD_NORMAL_COMPLETION)
1453 xferend->td->link.std = stat;
1454 xferend->td->td_link = stat->physaddr;
1459 upipe->u.ctl.length = len;
1460 upipe->u.ctl.xferend = xferend;
1462 memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
1463 if (!isread && len != 0)
1464 memcpy(KERNADDR(dmap), reqh->buffer, len);
1466 setup->td->link.std = next;
1467 setup->td->td_link = next->physaddr;
1468 setup->td->td_status = UHCI_TD_SET_ERRCNT(2) | ls | UHCI_TD_ACTIVE;
1469 setup->td->td_token = UHCI_TD_SETUP(sizeof *req, endpt, addr);
1470 setup->td->td_buffer = DMAADDR(&upipe->u.ctl.reqdma);
1472 stat->td->link.std = 0;
1473 stat->td->td_link = UHCI_PTR_T;
1474 stat->td->td_status = UHCI_TD_SET_ERRCNT(2) | ls |
1475 UHCI_TD_ACTIVE | UHCI_TD_IOC;
1476 stat->td->td_token =
1477 isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
1478 UHCI_TD_IN (0, endpt, addr, 1);
1479 stat->td->td_buffer = 0;
1482 if (uhcidebug > 20) {
1483 printf("uhci_device_request: setup\n");
1484 uhci_dump_td(setup);
1485 printf("uhci_device_request: stat\n");
1490 /* Set up interrupt info. */
1492 ii->stdstart = setup;
1498 sqh->qh->elink = setup;
1499 sqh->qh->qh_elink = setup->physaddr;
1500 sqh->intr_info = ii;
1503 uhci_add_ctrl(sc, sqh);
1504 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1506 if (uhcidebug > 12) {
1507 uhci_soft_td_t *std;
1508 uhci_soft_qh_t *xqh;
1509 uhci_soft_qh_t *sxqh;
1511 uhci_physaddr_t link;
1512 printf("uhci_enter_ctl_q: follow from [0]\n");
1513 for (std = sc->sc_vframes[0].htd, link = 0;
1514 (link & UHCI_PTR_Q) == 0;
1515 std = std->td->link.std) {
1516 link = std->td->td_link;
1519 for (sxqh = xqh = (uhci_soft_qh_t *)std;
1521 /* FIXME NWH seems to be a circular list ??
1522 * checking for beginning of list end of list
1523 * and printing a maximum of 5 QH's ...
1524 xqh = xqh->qh->hlink)
1526 xqh = (maxqh++ == 5 || xqh->qh->hlink==sxqh || xqh->qh->hlink==xqh? NULL : xqh->qh->hlink)) {
1530 printf("Enqueued QH:\n");
1532 uhci_dump_tds(sqh->qh->elink);
1535 if (reqh->timeout && !sc->sc_bus.use_polling) {
1536 #if defined(__NetBSD__)
1537 timeout(uhci_timeout, ii, MS_TO_TICKS(reqh->timeout));
1538 #elif defined(__FreeBSD__)
1539 /* To avoid race conditions we first initialise the struct
1540 * before we use it. The timeout may happen between the setting
1541 * of the timeout and the setting of callout_handle
1543 callout_handle_init(&ii->timeout_handle);
1544 ii->timeout_handle = timeout(uhci_timeout, ii,
1545 MS_TO_TICKS(reqh->timeout));
1550 return (USBD_NORMAL_COMPLETION);
1554 usb_freemem(sc->sc_dmatag, dmap);
1561 uhci_intr_info_t *ii;
1563 uhci_softc_t *sc = ii->sc;
1564 usbd_request_handle reqh = ii->reqh;
1565 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1567 uhci_soft_qh_t *sqh;
1570 DPRINTFN(5, ("uhci_intr_done: length=%d\n", reqh->actlen));
1572 dma = &upipe->u.intr.datadma;
1573 memcpy(reqh->buffer, KERNADDR(dma), reqh->actlen);
1574 npoll = upipe->u.intr.npoll;
1575 for(i = 0; i < npoll; i++) {
1576 sqh = upipe->u.intr.qhs[i];
1578 sqh->qh->qh_elink = UHCI_PTR_T;
1580 uhci_free_std_chain(sc, ii->stdstart, 0);
1583 if (reqh->pipe->intrreqh == reqh) {
1584 uhci_soft_td_t *xfer, *xferend;
1586 /* This alloc cannot fail since we freed the chain above. */
1587 uhci_alloc_std_chain(upipe, sc, reqh->length, 1, dma,
1589 xferend->td->td_status |= UHCI_TD_IOC;
1592 if (uhcidebug > 10) {
1593 printf("uhci_device_intr_done: xfer(1)\n");
1594 uhci_dump_tds(xfer);
1595 uhci_dump_qh(upipe->u.intr.qhs[0]);
1599 ii->stdstart = xfer;
1600 ii->stdend = xferend;
1604 for (i = 0; i < npoll; i++) {
1605 sqh = upipe->u.intr.qhs[i];
1606 sqh->qh->elink = xfer;
1607 sqh->qh->qh_elink = xfer->physaddr;
1610 usb_freemem(sc->sc_dmatag, dma);
1611 ii->stdstart = 0; /* mark as inactive */
1615 /* Deallocate request data structures */
1618 uhci_intr_info_t *ii;
1620 uhci_softc_t *sc = ii->sc;
1621 usbd_request_handle reqh = ii->reqh;
1622 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1623 u_int len = upipe->u.ctl.length;
1625 uhci_td_t *htd = ii->stdstart->td;
1629 panic("uhci_ctrl_done: not a request\n");
1632 LIST_REMOVE(ii, list); /* remove from active list */
1634 uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
1637 dma = &upipe->u.ctl.datadma;
1638 if (reqh->request.bmRequestType & UT_READ)
1639 memcpy(reqh->buffer, KERNADDR(dma), len);
1640 uhci_free_std_chain(sc, htd->link.std, ii->stdend);
1641 usb_freemem(sc->sc_dmatag, dma);
1643 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", reqh->actlen));
1646 /* Deallocate request data structures */
1649 uhci_intr_info_t *ii;
1651 uhci_softc_t *sc = ii->sc;
1652 usbd_request_handle reqh = ii->reqh;
1653 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1654 u_int len = upipe->u.bulk.length;
1656 uhci_td_t *htd = ii->stdstart->td;
1658 LIST_REMOVE(ii, list); /* remove from active list */
1660 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
1663 dma = &upipe->u.bulk.datadma;
1664 if (upipe->u.bulk.isread && len != 0)
1665 memcpy(reqh->buffer, KERNADDR(dma), len);
1666 uhci_free_std_chain(sc, htd->link.std, 0);
1667 usb_freemem(sc->sc_dmatag, dma);
1669 DPRINTFN(4, ("uhci_bulk_done: length=%d\n", reqh->actlen));
1670 /* XXX compute new toggle */
1673 /* Add interrupt QH, called with vflock. */
1675 uhci_add_intr(sc, n, sqh)
1678 uhci_soft_qh_t *sqh;
1680 struct uhci_vframe *vf = &sc->sc_vframes[n];
1683 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
1685 sqh->qh->hlink = eqh->hlink;
1686 sqh->qh->qh_hlink = eqh->qh_hlink;
1688 eqh->qh_hlink = sqh->physaddr | UHCI_PTR_Q;
1693 /* Remove interrupt QH, called with vflock. */
1695 uhci_remove_intr(sc, n, sqh)
1698 uhci_soft_qh_t *sqh;
1700 struct uhci_vframe *vf = &sc->sc_vframes[n];
1701 uhci_soft_qh_t *pqh;
1703 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
1705 for (pqh = vf->hqh; pqh->qh->hlink != sqh; pqh = pqh->qh->hlink)
1706 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
1707 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
1708 printf("uhci_remove_intr: QH not found\n");
1714 pqh->qh->hlink = sqh->qh->hlink;
1715 pqh->qh->qh_hlink = sqh->qh->qh_hlink;
1722 uhci_device_setintr(sc, upipe, ival)
1724 struct uhci_pipe *upipe;
1727 uhci_soft_qh_t *sqh;
1729 u_int bestbw, bw, bestoffs, offs;
1731 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
1733 printf("uhci_setintr: 0 interval\n");
1734 return (USBD_INVAL);
1737 if (ival > UHCI_VFRAMELIST_COUNT)
1738 ival = UHCI_VFRAMELIST_COUNT;
1739 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
1740 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
1742 upipe->u.intr.npoll = npoll;
1744 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USB, M_WAITOK);
1747 * Figure out which offset in the schedule that has most
1748 * bandwidth left over.
1750 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
1751 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
1752 for (bw = i = 0; i < npoll; i++)
1753 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
1759 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
1761 upipe->iinfo->stdstart = 0;
1762 for(i = 0; i < npoll; i++) {
1763 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
1765 sqh->qh->qh_elink = UHCI_PTR_T;
1766 sqh->pos = MOD(i * ival + bestoffs);
1767 sqh->intr_info = upipe->iinfo;
1772 LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
1775 uhci_lock_frames(sc);
1776 /* Enter QHs into the controller data structures. */
1777 for(i = 0; i < npoll; i++)
1778 uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
1779 upipe->u.intr.qhs[i]);
1780 uhci_unlock_frames(sc);
1782 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
1783 return (USBD_NORMAL_COMPLETION);
1786 /* Open a new pipe. */
1789 usbd_pipe_handle pipe;
1791 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1792 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1793 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1796 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1797 pipe, pipe->device->address,
1798 ed->bEndpointAddress, sc->sc_addr));
1799 if (pipe->device->address == sc->sc_addr) {
1800 switch (ed->bEndpointAddress) {
1801 case USB_CONTROL_ENDPOINT:
1802 pipe->methods = &uhci_root_ctrl_methods;
1804 case UE_IN | UHCI_INTR_ENDPT:
1805 pipe->methods = &uhci_root_intr_methods;
1808 return (USBD_INVAL);
1811 upipe->iinfo = uhci_alloc_intr_info(sc);
1812 if (upipe->iinfo == 0)
1813 return (USBD_NOMEM);
1814 switch (ed->bmAttributes & UE_XFERTYPE) {
1816 pipe->methods = &uhci_device_ctrl_methods;
1817 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
1818 if (upipe->u.ctl.sqh == 0)
1820 upipe->u.ctl.setup = uhci_alloc_std(sc);
1821 if (upipe->u.ctl.setup == 0) {
1822 uhci_free_sqh(sc, upipe->u.ctl.sqh);
1825 upipe->u.ctl.stat = uhci_alloc_std(sc);
1826 if (upipe->u.ctl.stat == 0) {
1827 uhci_free_sqh(sc, upipe->u.ctl.sqh);
1828 uhci_free_std(sc, upipe->u.ctl.setup);
1831 r = usb_allocmem(sc->sc_dmatag,
1832 sizeof(usb_device_request_t),
1833 0, &upipe->u.ctl.reqdma);
1834 if (r != USBD_NORMAL_COMPLETION) {
1835 uhci_free_sqh(sc, upipe->u.ctl.sqh);
1836 uhci_free_std(sc, upipe->u.ctl.setup);
1837 uhci_free_std(sc, upipe->u.ctl.stat);
1842 pipe->methods = &uhci_device_intr_methods;
1843 return (uhci_device_setintr(sc, upipe, ed->bInterval));
1844 case UE_ISOCHRONOUS:
1845 printf("uhci_open: iso not implemented\n");
1848 pipe->methods = &uhci_device_bulk_methods;
1849 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
1850 if (upipe->u.bulk.sqh == 0)
1855 return (USBD_NORMAL_COMPLETION);
1858 uhci_free_intr_info(upipe->iinfo);
1859 return (USBD_NOMEM);
1863 * Data structures and routines to emulate the root hub.
1865 usb_device_descriptor_t uhci_devd = {
1866 USB_DEVICE_DESCRIPTOR_SIZE,
1867 UDESC_DEVICE, /* type */
1868 {0x00, 0x01}, /* USB version */
1869 UCLASS_HUB, /* class */
1870 USUBCLASS_HUB, /* subclass */
1872 64, /* max packet */
1873 {0},{0},{0x00,0x01}, /* device id */
1874 1,2,0, /* string indicies */
1875 1 /* # of configurations */
1878 usb_config_descriptor_t uhci_confd = {
1879 USB_CONFIG_DESCRIPTOR_SIZE,
1881 {USB_CONFIG_DESCRIPTOR_SIZE +
1882 USB_INTERFACE_DESCRIPTOR_SIZE +
1883 USB_ENDPOINT_DESCRIPTOR_SIZE},
1891 usb_interface_descriptor_t uhci_ifcd = {
1892 USB_INTERFACE_DESCRIPTOR_SIZE,
1903 usb_endpoint_descriptor_t uhci_endpd = {
1904 USB_ENDPOINT_DESCRIPTOR_SIZE,
1906 UE_IN | UHCI_INTR_ENDPT,
1912 usb_hub_descriptor_t uhci_hubd_piix = {
1913 USB_HUB_DESCRIPTOR_SIZE,
1916 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
1917 50, /* power on to power good */
1919 { 0x00 }, /* both ports are removable */
1920 { 0x00 }, /* no ports can power down individually */
1925 usb_string_descriptor_t *p;
1933 p->bLength = 2 * strlen(s) + 2;
1936 p->bDescriptorType = UDESC_STRING;
1938 for (i = 0; s[i] && l > 1; i++, l -= 2)
1939 USETW2(p->bString[i], 0, s[i]);
1944 * Simulate a hardware hub by handling all the necessary requests.
1947 uhci_root_ctrl_transfer(reqh)
1948 usbd_request_handle reqh;
1950 uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
1951 usb_device_request_t *req;
1954 int len, value, index, status, change, l, totlen = 0;
1955 usb_port_status_t ps;
1959 panic("uhci_root_ctrl_transfer: not a request\n");
1960 req = &reqh->request;
1963 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
1964 req->bmRequestType, req->bRequest));
1966 len = UGETW(req->wLength);
1967 value = UGETW(req->wValue);
1968 index = UGETW(req->wIndex);
1969 #define C(x,y) ((x) | ((y) << 8))
1970 switch(C(req->bRequest, req->bmRequestType)) {
1971 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1972 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1973 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1975 * DEVICE_REMOTE_WAKEUP and ENDPOINT_STALL are no-ops
1976 * for the integrated root hub.
1979 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1981 *(u_int8_t *)buf = sc->sc_conf;
1985 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1986 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
1987 switch(value >> 8) {
1989 if ((value & 0xff) != 0) {
1993 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1994 memcpy(buf, &uhci_devd, l);
1997 if ((value & 0xff) != 0) {
2001 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2002 memcpy(buf, &uhci_confd, l);
2003 buf = (char *)buf + l;
2005 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2007 memcpy(buf, &uhci_ifcd, l);
2008 buf = (char *)buf + l;
2010 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2012 memcpy(buf, &uhci_endpd, l);
2017 *(u_int8_t *)buf = 0;
2019 switch (value & 0xff) {
2020 case 1: /* Vendor */
2021 totlen = uhci_str(buf, len, sc->sc_vendor);
2023 case 2: /* Product */
2024 totlen = uhci_str(buf, len, "UHCI root hub");
2033 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2035 *(u_int8_t *)buf = 0;
2039 case C(UR_GET_STATUS, UT_READ_DEVICE):
2041 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2045 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2046 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2048 USETW(((usb_status_t *)buf)->wStatus, 0);
2052 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2053 if (value >= USB_MAX_DEVICES) {
2057 sc->sc_addr = value;
2059 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2060 if (value != 0 && value != 1) {
2064 sc->sc_conf = value;
2066 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2068 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2069 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2070 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2073 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2075 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2078 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2080 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2081 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE port=%d feature=%d\n",
2084 port = UHCI_PORTSC1;
2085 else if (index == 2)
2086 port = UHCI_PORTSC2;
2092 case UHF_PORT_ENABLE:
2093 x = UREAD2(sc, port);
2094 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2096 case UHF_PORT_SUSPEND:
2097 x = UREAD2(sc, port);
2098 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
2100 case UHF_PORT_RESET:
2101 x = UREAD2(sc, port);
2102 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2104 case UHF_C_PORT_CONNECTION:
2105 x = UREAD2(sc, port);
2106 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2108 case UHF_C_PORT_ENABLE:
2109 x = UREAD2(sc, port);
2110 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2112 case UHF_C_PORT_OVER_CURRENT:
2113 x = UREAD2(sc, port);
2114 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2116 case UHF_C_PORT_RESET:
2118 r = USBD_NORMAL_COMPLETION;
2120 case UHF_PORT_CONNECTION:
2121 case UHF_PORT_OVER_CURRENT:
2122 case UHF_PORT_POWER:
2123 case UHF_PORT_LOW_SPEED:
2124 case UHF_C_PORT_SUSPEND:
2130 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2132 port = UHCI_PORTSC1;
2133 else if (index == 2)
2134 port = UHCI_PORTSC2;
2141 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2142 UHCI_PORTSC_LS_SHIFT;
2146 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2151 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
2153 memcpy(buf, &uhci_hubd_piix, l);
2155 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2160 memset(buf, 0, len);
2163 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2165 port = UHCI_PORTSC1;
2166 else if (index == 2)
2167 port = UHCI_PORTSC2;
2176 x = UREAD2(sc, port);
2177 status = change = 0;
2178 if (x & UHCI_PORTSC_CCS )
2179 status |= UPS_CURRENT_CONNECT_STATUS;
2180 if (x & UHCI_PORTSC_CSC )
2181 change |= UPS_C_CONNECT_STATUS;
2182 if (x & UHCI_PORTSC_PE )
2183 status |= UPS_PORT_ENABLED;
2184 if (x & UHCI_PORTSC_POEDC)
2185 change |= UPS_C_PORT_ENABLED;
2186 if (x & UHCI_PORTSC_OCI )
2187 status |= UPS_OVERCURRENT_INDICATOR;
2188 if (x & UHCI_PORTSC_OCIC )
2189 change |= UPS_C_OVERCURRENT_INDICATOR;
2190 if (x & UHCI_PORTSC_SUSP )
2191 status |= UPS_SUSPEND;
2192 if (x & UHCI_PORTSC_LSDA )
2193 status |= UPS_LOW_SPEED;
2194 status |= UPS_PORT_POWER;
2196 change |= UPS_C_PORT_RESET;
2197 USETW(ps.wPortStatus, status);
2198 USETW(ps.wPortChange, change);
2199 l = min(len, sizeof ps);
2200 memcpy(buf, &ps, l);
2203 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2206 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2208 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2210 port = UHCI_PORTSC1;
2211 else if (index == 2)
2212 port = UHCI_PORTSC2;
2218 case UHF_PORT_ENABLE:
2219 x = UREAD2(sc, port);
2220 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2222 case UHF_PORT_SUSPEND:
2223 x = UREAD2(sc, port);
2224 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2226 case UHF_PORT_RESET:
2227 x = UREAD2(sc, port);
2228 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2229 usbd_delay_ms(&sc->sc_bus, 10);
2230 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2232 x = UREAD2(sc, port);
2233 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2235 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
2236 index, UREAD2(sc, port)));
2239 case UHF_C_PORT_CONNECTION:
2240 case UHF_C_PORT_ENABLE:
2241 case UHF_C_PORT_OVER_CURRENT:
2242 case UHF_PORT_CONNECTION:
2243 case UHF_PORT_OVER_CURRENT:
2244 case UHF_PORT_POWER:
2245 case UHF_PORT_LOW_SPEED:
2246 case UHF_C_PORT_SUSPEND:
2247 case UHF_C_PORT_RESET:
2257 reqh->actlen = totlen;
2258 r = USBD_NORMAL_COMPLETION;
2262 return (USBD_IN_PROGRESS);
2265 /* Abort a root control request. */
2267 uhci_root_ctrl_abort(reqh)
2268 usbd_request_handle reqh;
2270 /* Nothing to do, all transfers are syncronous. */
2273 /* Close the root pipe. */
2275 uhci_root_ctrl_close(pipe)
2276 usbd_pipe_handle pipe;
2278 #if defined(__NetBSD__)
2279 untimeout(uhci_timo, pipe->intrreqh);
2280 #elif defined(__FreeBSD__)
2281 untimeout(uhci_timo, pipe->intrreqh, pipe->intrreqh->timo_handle);
2283 DPRINTF(("uhci_root_ctrl_close\n"));
2286 /* Abort a root interrupt request. */
2288 uhci_root_intr_abort(reqh)
2289 usbd_request_handle reqh;
2291 #if defined(__NetBSD__)
2292 untimeout(uhci_timo, reqh);
2293 #elif defined(__FreeBSD__)
2294 untimeout(uhci_timo, reqh, reqh->timo_handle);
2298 /* Start a transfer on the root interrupt pipe */
2300 uhci_root_intr_transfer(reqh)
2301 usbd_request_handle reqh;
2303 usbd_pipe_handle pipe = reqh->pipe;
2304 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2305 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2310 DPRINTFN(3, ("uhci_root_intr_transfer: reqh=%p buf=%p len=%d flags=%d\n",
2311 reqh, reqh->buffer, reqh->length, reqh->flags));
2314 dmap = &upipe->u.intr.datadma;
2316 return (USBD_INVAL); /* XXX should it be? */
2318 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
2319 if (r != USBD_NORMAL_COMPLETION)
2322 sc->sc_ival = MS_TO_TICKS(reqh->pipe->endpoint->edesc->bInterval);
2323 #if defined(__NetBSD__)
2324 timeout(uhci_timo, reqh, sc->sc_ival);
2325 #elif defined(__FreeBSD__)
2326 /* To avoid race conditions we first initialise the struct
2327 * before we use it. The timeout happen between the setting
2328 * of the timeout and the setting of callout_handle
2330 callout_handle_init(&reqh->timo_handle);
2331 reqh->timo_handle = timeout(uhci_timo, reqh, sc->sc_ival);
2333 return (USBD_IN_PROGRESS);
2336 /* Close the root interrupt pipe. */
2338 uhci_root_intr_close(pipe)
2339 usbd_pipe_handle pipe;
2341 #if defined(__NetBSD__)
2342 untimeout(uhci_timo, pipe->intrreqh);
2343 #elif defined(__FreeBSD__)
2344 untimeout(uhci_timo, pipe->intrreqh, pipe->intrreqh->timo_handle);
2346 DPRINTF(("uhci_root_intr_close\n"));