1 /* $NetBSD: uhci.c,v 1.22 1999/01/08 11:58:25 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 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.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
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>
79 #define delay(d) DELAY(d)
82 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
85 struct usbd_pipe pipe;
86 uhci_intr_info_t *iinfo;
88 /* Info needed for different pipe kinds. */
95 uhci_soft_td_t *setup, *stat;
102 uhci_soft_qh_t **qhs;
116 uhci_soft_td_t **stds;
122 * The uhci_intr_info free list can be global since they contain
123 * no dma specific data. The other free lists do.
125 LIST_HEAD(, uhci_intr_info) uhci_ii_free;
127 void uhci_busreset __P((uhci_softc_t *));
128 usbd_status uhci_run __P((uhci_softc_t *, int run));
129 uhci_soft_td_t *uhci_alloc_std __P((uhci_softc_t *));
130 void uhci_free_std __P((uhci_softc_t *, uhci_soft_td_t *));
131 uhci_soft_qh_t *uhci_alloc_sqh __P((uhci_softc_t *));
132 void uhci_free_sqh __P((uhci_softc_t *, uhci_soft_qh_t *));
133 uhci_intr_info_t *uhci_alloc_intr_info __P((uhci_softc_t *));
134 void uhci_free_intr_info __P((uhci_intr_info_t *ii));
136 void uhci_enter_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *,
137 uhci_intr_info_t *));
138 void uhci_exit_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *));
141 void uhci_free_std_chain __P((uhci_softc_t *,
142 uhci_soft_td_t *, uhci_soft_td_t *));
143 usbd_status uhci_alloc_std_chain __P((struct uhci_pipe *, uhci_softc_t *,
144 int, int, int, usb_dma_t *,
147 void uhci_timo __P((void *));
148 void uhci_waitintr __P((uhci_softc_t *, usbd_request_handle));
149 void uhci_check_intr __P((uhci_softc_t *, uhci_intr_info_t *));
150 void uhci_ii_done __P((uhci_intr_info_t *, int));
151 void uhci_timeout __P((void *));
152 void uhci_wakeup_ctrl __P((void *, int, int, void *, int));
153 void uhci_lock_frames __P((uhci_softc_t *));
154 void uhci_unlock_frames __P((uhci_softc_t *));
155 void uhci_add_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
156 void uhci_add_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
157 void uhci_remove_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
158 void uhci_remove_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
159 int uhci_str __P((usb_string_descriptor_t *, int, char *));
161 void uhci_wakeup_cb __P((usbd_request_handle reqh));
163 usbd_status uhci_device_ctrl_transfer __P((usbd_request_handle));
164 usbd_status uhci_device_ctrl_start __P((usbd_request_handle));
165 void uhci_device_ctrl_abort __P((usbd_request_handle));
166 void uhci_device_ctrl_close __P((usbd_pipe_handle));
167 usbd_status uhci_device_intr_transfer __P((usbd_request_handle));
168 usbd_status uhci_device_intr_start __P((usbd_request_handle));
169 void uhci_device_intr_abort __P((usbd_request_handle));
170 void uhci_device_intr_close __P((usbd_pipe_handle));
171 usbd_status uhci_device_bulk_transfer __P((usbd_request_handle));
172 usbd_status uhci_device_bulk_start __P((usbd_request_handle));
173 void uhci_device_bulk_abort __P((usbd_request_handle));
174 void uhci_device_bulk_close __P((usbd_pipe_handle));
175 usbd_status uhci_device_isoc_transfer __P((usbd_request_handle));
176 usbd_status uhci_device_isoc_start __P((usbd_request_handle));
177 void uhci_device_isoc_abort __P((usbd_request_handle));
178 void uhci_device_isoc_close __P((usbd_pipe_handle));
179 usbd_status uhci_device_isoc_setbuf __P((usbd_pipe_handle, u_int, u_int));
181 usbd_status uhci_root_ctrl_transfer __P((usbd_request_handle));
182 usbd_status uhci_root_ctrl_start __P((usbd_request_handle));
183 void uhci_root_ctrl_abort __P((usbd_request_handle));
184 void uhci_root_ctrl_close __P((usbd_pipe_handle));
185 usbd_status uhci_root_intr_transfer __P((usbd_request_handle));
186 usbd_status uhci_root_intr_start __P((usbd_request_handle));
187 void uhci_root_intr_abort __P((usbd_request_handle));
188 void uhci_root_intr_close __P((usbd_pipe_handle));
190 usbd_status uhci_open __P((usbd_pipe_handle));
191 void uhci_poll __P((struct usbd_bus *));
193 usbd_status uhci_device_request __P((usbd_request_handle reqh));
194 void uhci_ctrl_done __P((uhci_intr_info_t *ii));
195 void uhci_bulk_done __P((uhci_intr_info_t *ii));
197 void uhci_add_intr __P((uhci_softc_t *, int, uhci_soft_qh_t *));
198 void uhci_remove_intr __P((uhci_softc_t *, int, uhci_soft_qh_t *));
199 usbd_status uhci_device_setintr __P((uhci_softc_t *sc,
200 struct uhci_pipe *pipe, int ival));
201 void uhci_intr_done __P((uhci_intr_info_t *ii));
202 void uhci_isoc_done __P((uhci_intr_info_t *ii));
205 static void uhci_dumpregs __P((uhci_softc_t *));
206 void uhci_dump_tds __P((uhci_soft_td_t *));
207 void uhci_dump_qh __P((uhci_soft_qh_t *));
208 void uhci_dump __P((void));
209 void uhci_dump_td __P((uhci_soft_td_t *));
212 #if defined(__NetBSD__)
213 #define UWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
214 #define UWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
215 #define UREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
216 #define UREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
217 #elif defined(__FreeBSD__)
218 #define UWRITE2(sc,r,x) outw((sc)->sc_iobase + (r), (x))
219 #define UWRITE4(sc,r,x) outl((sc)->sc_iobase + (r), (x))
220 #define UREAD1(sc,r) inb((sc)->sc_iobase + (r))
221 #define UREAD2(sc,r) inw((sc)->sc_iobase + (r))
222 #define UREAD4(sc,r) inl((sc)->sc_iobase + (r))
225 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
226 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
228 #define UHCI_RESET_TIMEOUT 100 /* reset timeout */
230 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
232 #define UHCI_INTR_ENDPT 1
234 struct usbd_methods uhci_root_ctrl_methods = {
235 uhci_root_ctrl_transfer,
236 uhci_root_ctrl_start,
237 uhci_root_ctrl_abort,
238 uhci_root_ctrl_close,
242 struct usbd_methods uhci_root_intr_methods = {
243 uhci_root_intr_transfer,
244 uhci_root_intr_start,
245 uhci_root_intr_abort,
246 uhci_root_intr_close,
250 struct usbd_methods uhci_device_ctrl_methods = {
251 uhci_device_ctrl_transfer,
252 uhci_device_ctrl_start,
253 uhci_device_ctrl_abort,
254 uhci_device_ctrl_close,
258 struct usbd_methods uhci_device_intr_methods = {
259 uhci_device_intr_transfer,
260 uhci_device_intr_start,
261 uhci_device_intr_abort,
262 uhci_device_intr_close,
266 struct usbd_methods uhci_device_bulk_methods = {
267 uhci_device_bulk_transfer,
268 uhci_device_bulk_start,
269 uhci_device_bulk_abort,
270 uhci_device_bulk_close,
274 struct usbd_methods uhci_device_isoc_methods = {
275 uhci_device_isoc_transfer,
276 uhci_device_isoc_start,
277 uhci_device_isoc_abort,
278 uhci_device_isoc_close,
279 uhci_device_isoc_setbuf,
286 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
287 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
288 UHCICMD(sc, 0); /* do nothing */
297 uhci_soft_qh_t *csqh, *bsqh, *sqh;
300 static int uhci_global_init_done = 0;
302 DPRINTFN(1,("uhci_init: start\n"));
304 if (!uhci_global_init_done) {
305 uhci_global_init_done = 1;
306 LIST_INIT(&uhci_ii_free);
309 #if defined(USB_DEBUG)
314 uhci_run(sc, 0); /* stop the controller */
315 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
317 /* Allocate and initialize real frame array. */
318 r = usb_allocmem(sc->sc_dmatag,
319 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
320 UHCI_FRAMELIST_ALIGN, &dma);
321 if (r != USBD_NORMAL_COMPLETION)
323 sc->sc_pframes = KERNADDR(&dma);
324 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
325 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&dma)); /* set frame list */
329 /* Allocate the dummy QH where bulk traffic will be queued. */
330 bsqh = uhci_alloc_sqh(sc);
333 bsqh->qh->qh_hlink = UHCI_PTR_T; /* end of QH chain */
334 bsqh->qh->qh_elink = UHCI_PTR_T;
335 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
337 /* Allocate the dummy QH where control traffic will be queued. */
338 csqh = uhci_alloc_sqh(sc);
341 csqh->qh->hlink = bsqh;
342 csqh->qh->qh_hlink = bsqh->physaddr | UHCI_PTR_Q;
343 csqh->qh->qh_elink = UHCI_PTR_T;
344 sc->sc_ctl_start = sc->sc_ctl_end = csqh;
347 * Make all (virtual) frame list pointers point to the interrupt
348 * queue heads and the interrupt queue heads at the control
349 * queue head and point the physical frame list to the virtual.
351 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
352 std = uhci_alloc_std(sc);
353 sqh = uhci_alloc_sqh(sc);
356 std->td->link.sqh = sqh;
357 std->td->td_link = sqh->physaddr | UHCI_PTR_Q;
358 std->td->td_status = UHCI_TD_IOS; /* iso, inactive */
359 std->td->td_token = 0;
360 std->td->td_buffer = 0;
361 sqh->qh->hlink = csqh;
362 sqh->qh->qh_hlink = csqh->physaddr | UHCI_PTR_Q;
364 sqh->qh->qh_elink = UHCI_PTR_T;
365 sc->sc_vframes[i].htd = std;
366 sc->sc_vframes[i].etd = std;
367 sc->sc_vframes[i].hqh = sqh;
368 sc->sc_vframes[i].eqh = sqh;
370 j < UHCI_FRAMELIST_COUNT;
371 j += UHCI_VFRAMELIST_COUNT)
372 sc->sc_pframes[j] = std->physaddr;
375 LIST_INIT(&sc->sc_intrhead);
377 /* Set up the bus struct. */
378 sc->sc_bus.open_pipe = uhci_open;
379 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
380 sc->sc_bus.do_poll = uhci_poll;
382 DPRINTFN(1,("uhci_init: enabling\n"));
383 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
384 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
386 return (uhci_run(sc, 1)); /* and here we go... */
394 printf("%s; regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
395 "flbase=%08x, sof=%02x, portsc1=%04x, portsc2=%04x, "
397 USBDEVNAME(sc->sc_bus.bdev),
398 UREAD2(sc, UHCI_CMD),
399 UREAD2(sc, UHCI_STS),
400 UREAD2(sc, UHCI_INTR),
401 UREAD2(sc, UHCI_FRNUM),
402 UREAD4(sc, UHCI_FLBASEADDR),
403 UREAD1(sc, UHCI_SOF),
404 UREAD2(sc, UHCI_PORTSC1),
405 UREAD2(sc, UHCI_PORTSC2),
406 UREAD2(sc, UHCI_LEGSUP));
415 printf("TD(%p) at %08lx = 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",
416 p, (long)p->physaddr,
417 (long)p->td->td_link,
418 (long)p->td->td_status,
419 (long)p->td->td_token,
420 (long)p->td->td_buffer);
422 printf(" %b %b,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
426 (int)p->td->td_status,
427 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
428 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
429 UHCI_TD_GET_ERRCNT(p->td->td_status),
430 UHCI_TD_GET_ACTLEN(p->td->td_status),
431 UHCI_TD_GET_PID(p->td->td_token),
432 UHCI_TD_GET_DEVADDR(p->td->td_token),
433 UHCI_TD_GET_ENDPT(p->td->td_token),
434 UHCI_TD_GET_DT(p->td->td_token),
435 UHCI_TD_GET_MAXLEN(p->td->td_token));
442 printf("QH(%p) at %08x: hlink=%08x elink=%08x\n", p, (int)p->physaddr,
443 p->qh->qh_hlink, p->qh->qh_elink);
451 uhci_softc_t *sc = uhci;
454 printf("intrs=%d\n", sc->sc_intrs);
455 printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);
456 uhci_dump_qh(sc->sc_ctl_start->qh->hlink);
466 for(p = std; p; p = p->td->link.std)
472 * This routine is executed periodically and simulates interrupts
473 * from the root controller interrupt pipe for port status change.
479 usbd_request_handle reqh = addr;
480 usbd_pipe_handle pipe = reqh->pipe;
481 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
482 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
486 DPRINTFN(15, ("uhci_timo\n"));
488 p = KERNADDR(&upipe->u.intr.datadma);
490 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
492 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
497 reqh->status = USBD_NORMAL_COMPLETION;
500 if (reqh->pipe->intrreqh == reqh) {
501 usb_timeout(uhci_timo, reqh, sc->sc_ival, reqh->timo_handle);
503 usb_freemem(sc->sc_dmatag, &upipe->u.intr.datadma);
504 usb_start_next(reqh->pipe);
515 while (sc->sc_vflock) {
516 sc->sc_vflock |= UHCI_WANT_LOCK;
517 tsleep(&sc->sc_vflock, PRIBIO, "uhcqhl", 0);
519 sc->sc_vflock = UHCI_HAS_LOCK;
524 uhci_unlock_frames(sc)
528 sc->sc_vflock &= ~UHCI_HAS_LOCK;
529 if (sc->sc_vflock & UHCI_WANT_LOCK)
530 wakeup(&sc->sc_vflock);
535 * Allocate an interrupt information struct. A free list is kept
536 * for fast allocation.
539 uhci_alloc_intr_info(sc)
542 uhci_intr_info_t *ii;
544 ii = LIST_FIRST(&uhci_ii_free);
546 LIST_REMOVE(ii, list);
548 ii = malloc(sizeof(uhci_intr_info_t), M_USBDEV, M_NOWAIT);
555 uhci_free_intr_info(ii)
556 uhci_intr_info_t *ii;
558 LIST_INSERT_HEAD(&uhci_ii_free, ii, list); /* and put on free list */
561 /* Add control QH, called at splusb(). */
563 uhci_add_ctrl(sc, sqh)
569 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
570 eqh = sc->sc_ctl_end->qh;
571 sqh->qh->hlink = eqh->hlink;
572 sqh->qh->qh_hlink = eqh->qh_hlink;
574 eqh->qh_hlink = sqh->physaddr | UHCI_PTR_Q;
575 sc->sc_ctl_end = sqh;
578 /* Remove control QH, called at splusb(). */
580 uhci_remove_ctrl(sc, sqh)
586 DPRINTFN(10, ("uhci_remove_ctrl: sqh=%p\n", sqh));
587 for (pqh = sc->sc_ctl_start; pqh->qh->hlink != sqh; pqh=pqh->qh->hlink)
588 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
589 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
590 printf("uhci_remove_ctrl: QH not found\n");
596 pqh->qh->hlink = sqh->qh->hlink;
597 pqh->qh->qh_hlink = sqh->qh->qh_hlink;
598 if (sc->sc_ctl_end == sqh)
599 sc->sc_ctl_end = pqh;
602 /* Add bulk QH, called at splusb(). */
604 uhci_add_bulk(sc, sqh)
610 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
611 eqh = sc->sc_bulk_end->qh;
612 sqh->qh->hlink = eqh->hlink;
613 sqh->qh->qh_hlink = eqh->qh_hlink;
615 eqh->qh_hlink = sqh->physaddr | UHCI_PTR_Q;
616 sc->sc_bulk_end = sqh;
619 /* Remove bulk QH, called at splusb(). */
621 uhci_remove_bulk(sc, sqh)
627 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
628 for (pqh = sc->sc_bulk_start;
629 pqh->qh->hlink != sqh;
630 pqh = pqh->qh->hlink)
631 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
632 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
633 printf("uhci_remove_bulk: QH not found\n");
639 pqh->qh->hlink = sqh->qh->hlink;
640 pqh->qh->qh_hlink = sqh->qh->qh_hlink;
641 if (sc->sc_bulk_end == sqh)
642 sc->sc_bulk_end = pqh;
649 uhci_softc_t *sc = p;
651 uhci_intr_info_t *ii;
654 #if defined(USB_DEBUG)
656 printf("uhci_intr %p\n", sc);
660 status = UREAD2(sc, UHCI_STS);
662 if (status & UHCI_STS_USBINT) {
663 UWRITE2(sc, UHCI_STS, UHCI_STS_USBINT); /* acknowledge */
666 if (status & UHCI_STS_USBEI) {
667 UWRITE2(sc, UHCI_STS, UHCI_STS_USBEI); /* acknowledge */
670 if (status & UHCI_STS_RD) {
671 UWRITE2(sc, UHCI_STS, UHCI_STS_RD); /* acknowledge */
672 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
675 if (status & UHCI_STS_HSE) {
676 UWRITE2(sc, UHCI_STS, UHCI_STS_HSE); /* acknowledge */
677 printf("%s: Host System Error\n", USBDEVNAME(sc->sc_bus.bdev));
680 if (status & UHCI_STS_HCPE) {
681 UWRITE2(sc, UHCI_STS, UHCI_STS_HCPE); /* acknowledge */
682 printf("%s: Host System Error\n", USBDEVNAME(sc->sc_bus.bdev));
685 if (status & UHCI_STS_HCH)
686 printf("%s: controller halted\n", USBDEVNAME(sc->sc_bus.bdev));
691 * Interrupts on UHCI really suck. When the host controller
692 * interrupts because a transfer is completed there is no
693 * way of knowing which transfer it was. You can scan down
694 * the TDs and QHs of the previous frame to limit the search,
695 * but that assumes that the interrupt was not delayed by more
696 * than 1 ms, which may not always be true (e.g. after debug
697 * output on a slow console).
698 * We scan all interrupt descriptors to see if any have
701 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
702 uhci_check_intr(sc, ii);
704 DPRINTFN(10, ("uhci_intr: exit\n"));
708 /* Check for an interrupt. */
710 uhci_check_intr(sc, ii)
712 uhci_intr_info_t *ii;
714 struct uhci_pipe *upipe;
715 uhci_soft_td_t *std, *lstd;
718 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
721 printf("uhci_check_intr: no ii? %p\n", ii);
730 printf("uhci_check_intr: std==0\n");
734 /* If the last TD is still active the whole transfer probably is. */
735 if (lstd->td->td_status & UHCI_TD_ACTIVE) {
736 DPRINTFN(15, ("uhci_check_intr: active ii=%p\n", ii));
737 for (std = ii->stdstart; std != lstd; std = std->td->link.std){
738 status = std->td->td_status;
739 if ((status & UHCI_TD_STALLED) ||
740 (status & (UHCI_TD_SPD | UHCI_TD_ACTIVE)) ==
744 DPRINTFN(15, ("uhci_check_intr: ii=%p std=%p still active\n",
749 upipe = (struct uhci_pipe *)ii->reqh->pipe;
750 upipe->pipe.endpoint->toggle = upipe->newtoggle;
752 usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
756 uhci_ii_done(ii, timo)
757 uhci_intr_info_t *ii;
760 usbd_request_handle reqh = ii->reqh;
763 int len, status, attr;
765 DPRINTFN(10, ("uhci_ii_done: ii=%p ready %d\n", ii, timo));
771 printf("uhci_ii_done: is done!\n");
780 /* The transfer is done, compute length and status. */
781 /* XXX Should stop at first inactive to get toggle right. */
782 /* XXX Is this correct for control xfers? */
783 for (len = status = 0, std = ii->stdstart;
785 std = std->td->link.std) {
786 tst = std->td->td_status;
789 if ((tst & UHCI_TD_ERROR) && uhcidebug) {
790 printf("uhci_ii_done: intr error TD:\n");
794 if (UHCI_TD_GET_PID(std->td->td_token) != UHCI_TD_PID_SETUP)
795 len += UHCI_TD_GET_ACTLEN(tst);
797 status &= UHCI_TD_ERROR;
798 DPRINTFN(10, ("uhci_check_intr: len=%d, status=0x%x\n", len, status));
800 DPRINTFN(-1+(status==UHCI_TD_STALLED),
801 ("uhci_ii_done: error, addr=%d, endpt=0x%02x, "
803 reqh->pipe->device->address,
804 reqh->pipe->endpoint->edesc->bEndpointAddress,
806 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
807 "STALLED\30ACTIVE"));
808 if (status == UHCI_TD_STALLED)
809 reqh->status = USBD_STALLED;
811 reqh->status = USBD_IOERROR; /* more info XXX */
814 reqh->status = USBD_NORMAL_COMPLETION;
818 /* We got a timeout. Make sure transaction is not active. */
819 reqh->status = USBD_TIMEOUT;
820 for (std = ii->stdstart; std != 0; std = std->td->link.std)
821 std->td->td_status &= ~UHCI_TD_ACTIVE;
822 /* XXX should we wait 1 ms */
824 DPRINTFN(5, ("uhci_ii_done: calling handler ii=%p\n", ii));
826 attr = reqh->pipe->endpoint->edesc->bmAttributes;
827 switch (attr & UE_XFERTYPE) {
830 usb_start_next(reqh->pipe);
834 usb_start_next(reqh->pipe);
838 usb_start_next(reqh->pipe);
845 /* And finally execute callback. */
850 * Called when a request does not complete.
856 uhci_intr_info_t *ii = addr;
859 DPRINTF(("uhci_timeout: ii=%p\n", ii));
866 * Wait here until controller claims to have an interrupt.
867 * Then call uhci_intr and return. Use timeout to avoid waiting
869 * Only used during boot when interrupts are not enabled yet.
872 uhci_waitintr(sc, reqh)
874 usbd_request_handle reqh;
876 int timo = reqh->timeout;
878 uhci_intr_info_t *ii;
880 DPRINTFN(10,("uhci_waitintr: timeout = %ds\n", timo));
882 reqh->status = USBD_IN_PROGRESS;
883 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
884 usb_delay_ms(&sc->sc_bus, 1);
885 DPRINTFN(10,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
886 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
888 if (reqh->status != USBD_IN_PROGRESS)
894 DPRINTF(("uhci_waitintr: timeout\n"));
895 for (ii = LIST_FIRST(&sc->sc_intrhead);
896 ii && ii->reqh != reqh;
897 ii = LIST_NEXT(ii, list))
902 panic("uhci_waitintr: lost intr_info\n");
907 struct usbd_bus *bus;
909 uhci_softc_t *sc = (uhci_softc_t *)bus;
911 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
920 uhci_softc_t *sc = p;
923 UHCICMD(sc, UHCI_CMD_HCRESET);
924 /* The reset bit goes low when the controller is done. */
925 for (n = 0; n < UHCI_RESET_TIMEOUT &&
926 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
928 if (n >= UHCI_RESET_TIMEOUT)
929 printf("%s: controller did not reset\n",
930 USBDEVNAME(sc->sc_bus.bdev));
942 running = ((UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) == 0);
943 if (run == running) {
945 return (USBD_NORMAL_COMPLETION);
947 UWRITE2(sc, UHCI_CMD, run ? UHCI_CMD_RS : 0);
948 for(n = 0; n < 10; n++) {
949 running = ((UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) == 0);
950 /* return when we've entered the state we want */
951 if (run == running) {
953 return (USBD_NORMAL_COMPLETION);
955 usb_delay_ms(&sc->sc_bus, 1);
958 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
959 run ? "start" : "stop");
963 return (USBD_IOERROR);
967 * Memory management routines.
968 * uhci_alloc_std allocates TDs
969 * uhci_alloc_sqh allocates QHs
970 * These two routines do their own free list management,
971 * partly for speed, partly because allocating DMAable memory
972 * has page size granularaity so much memory would be wasted if
973 * only one TD/QH (32 bytes) was placed in each allocated chunk.
985 if (!sc->sc_freetds) {
986 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
987 std = malloc(sizeof(uhci_soft_td_t) * UHCI_TD_CHUNK,
991 r = usb_allocmem(sc->sc_dmatag, UHCI_TD_SIZE * UHCI_TD_CHUNK,
992 UHCI_TD_ALIGN, &dma);
993 if (r != USBD_NORMAL_COMPLETION) {
997 for(i = 0; i < UHCI_TD_CHUNK; i++, std++) {
998 std->physaddr = DMAADDR(&dma) + i * UHCI_TD_SIZE;
999 std->td = (uhci_td_t *)
1000 ((char *)KERNADDR(&dma) + i * UHCI_TD_SIZE);
1001 std->td->link.std = sc->sc_freetds;
1002 sc->sc_freetds = std;
1005 std = sc->sc_freetds;
1006 sc->sc_freetds = std->td->link.std;
1007 memset(std->td, 0, UHCI_TD_SIZE);
1012 uhci_free_std(sc, std)
1014 uhci_soft_td_t *std;
1017 #define TD_IS_FREE 0x12345678
1018 if (std->td->td_token == TD_IS_FREE) {
1019 printf("uhci_free_std: freeing free TD %p\n", std);
1022 std->td->td_token = TD_IS_FREE;
1024 std->td->link.std = sc->sc_freetds;
1025 sc->sc_freetds = std;
1032 uhci_soft_qh_t *sqh;
1037 if (!sc->sc_freeqhs) {
1038 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1039 sqh = malloc(sizeof(uhci_soft_qh_t) * UHCI_QH_CHUNK,
1040 M_USBDEV, M_NOWAIT);
1043 r = usb_allocmem(sc->sc_dmatag, UHCI_QH_SIZE * UHCI_QH_CHUNK,
1044 UHCI_QH_ALIGN, &dma);
1045 if (r != USBD_NORMAL_COMPLETION) {
1046 free(sqh, M_USBDEV);
1049 for(i = 0; i < UHCI_QH_CHUNK; i++, sqh++) {
1050 offs = i * UHCI_QH_SIZE;
1051 sqh->physaddr = DMAADDR(&dma) + offs;
1052 sqh->qh = (uhci_qh_t *)
1053 ((char *)KERNADDR(&dma) + offs);
1054 sqh->qh->hlink = sc->sc_freeqhs;
1055 sc->sc_freeqhs = sqh;
1058 sqh = sc->sc_freeqhs;
1059 sc->sc_freeqhs = sqh->qh->hlink;
1060 memset(sqh->qh, 0, UHCI_QH_SIZE);
1065 uhci_free_sqh(sc, sqh)
1067 uhci_soft_qh_t *sqh;
1069 sqh->qh->hlink = sc->sc_freeqhs;
1070 sc->sc_freeqhs = sqh;
1075 * Enter a list of transfers onto a control queue.
1076 * Called at splusb()
1079 uhci_enter_ctl_q(sc, sqh, ii)
1081 uhci_soft_qh_t *sqh;
1082 uhci_intr_info_t *ii;
1084 DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
1090 uhci_free_std_chain(sc, std, stdend)
1092 uhci_soft_td_t *std;
1093 uhci_soft_td_t *stdend;
1097 for (; std != stdend; std = p) {
1098 p = std->td->link.std;
1099 uhci_free_std(sc, std);
1104 uhci_alloc_std_chain(upipe, sc, len, rd, spd, dma, sp, ep)
1105 struct uhci_pipe *upipe;
1109 uhci_soft_td_t **sp, **ep;
1111 uhci_soft_td_t *p, *lastp;
1112 uhci_physaddr_t lastlink;
1113 int i, ntd, l, tog, maxp;
1115 int addr = upipe->pipe.device->address;
1116 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1118 DPRINTFN(15, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
1119 "spd=%d\n", addr, endpt, len,
1120 upipe->pipe.device->lowspeed, spd));
1123 DPRINTFN(-1,("uhci_alloc_std_chain: len=0\n"));
1124 return (USBD_NORMAL_COMPLETION);
1126 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1128 printf("uhci_alloc_std_chain: maxp=0\n");
1129 return (USBD_INVAL);
1131 ntd = (len + maxp - 1) / maxp;
1132 tog = upipe->pipe.endpoint->toggle;
1135 upipe->newtoggle = tog ^ 1;
1137 lastlink = UHCI_PTR_T;
1139 status = UHCI_TD_SET_ERRCNT(2) | UHCI_TD_ACTIVE;
1140 if (upipe->pipe.device->lowspeed)
1141 status |= UHCI_TD_LS;
1143 status |= UHCI_TD_SPD;
1144 for (i = ntd; i >= 0; i--) {
1145 p = uhci_alloc_std(sc);
1147 uhci_free_std_chain(sc, lastp, 0);
1148 return (USBD_NOMEM);
1150 p->td->link.std = lastp;
1151 p->td->td_link = lastlink;
1153 lastlink = p->physaddr;
1154 p->td->td_status = status;
1158 if (l == 0) l = maxp;
1163 rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1164 UHCI_TD_OUT(l, endpt, addr, tog);
1165 p->td->td_buffer = DMAADDR(dma) + i * maxp;
1169 /*upipe->pipe.endpoint->toggle = tog;*/
1170 DPRINTFN(10, ("uhci_alloc_std_chain: oldtog=%d newtog=%d\n",
1171 upipe->pipe.endpoint->toggle, upipe->newtoggle));
1172 return (USBD_NORMAL_COMPLETION);
1176 uhci_device_bulk_transfer(reqh)
1177 usbd_request_handle reqh;
1183 r = usb_insert_transfer(reqh);
1185 if (r != USBD_NORMAL_COMPLETION)
1188 return (uhci_device_bulk_start(reqh));
1192 uhci_device_bulk_start(reqh)
1193 usbd_request_handle reqh;
1195 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1196 usbd_device_handle dev = upipe->pipe.device;
1197 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1198 uhci_intr_info_t *ii = upipe->iinfo;
1199 uhci_soft_td_t *xfer, *xferend;
1200 uhci_soft_qh_t *sqh;
1206 DPRINTFN(3, ("uhci_device_bulk_transfer: reqh=%p buf=%p len=%d "
1208 reqh, reqh->buffer, reqh->length, reqh->flags));
1211 panic("uhci_device_bulk_transfer: a request\n");
1214 dmap = &upipe->u.bulk.datadma;
1215 isread = reqh->pipe->endpoint->edesc->bEndpointAddress & UE_IN;
1216 sqh = upipe->u.bulk.sqh;
1218 upipe->u.bulk.isread = isread;
1219 upipe->u.bulk.length = len;
1221 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1222 if (r != USBD_NORMAL_COMPLETION)
1224 r = uhci_alloc_std_chain(upipe, sc, len, isread,
1225 reqh->flags & USBD_SHORT_XFER_OK,
1226 dmap, &xfer, &xferend);
1227 if (r != USBD_NORMAL_COMPLETION)
1229 xferend->td->td_status |= UHCI_TD_IOC;
1231 if (!isread && len != 0)
1232 memcpy(KERNADDR(dmap), reqh->buffer, len);
1235 if (uhcidebug > 10) {
1236 printf("uhci_device_bulk_transfer: xfer(1)\n");
1237 uhci_dump_tds(xfer);
1241 /* Set up interrupt info. */
1243 ii->stdstart = xfer;
1244 ii->stdend = xferend;
1249 sqh->qh->elink = xfer;
1250 sqh->qh->qh_elink = xfer->physaddr;
1251 sqh->intr_info = ii;
1254 uhci_add_bulk(sc, sqh);
1255 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1257 if (reqh->timeout && !sc->sc_bus.use_polling) {
1258 usb_timeout(uhci_timeout, ii,
1259 MS_TO_TICKS(reqh->timeout), ii->timeout_handle);
1264 if (uhcidebug > 10) {
1265 printf("uhci_device_bulk_transfer: xfer(2)\n");
1266 uhci_dump_tds(xfer);
1270 return (USBD_IN_PROGRESS);
1274 usb_freemem(sc->sc_dmatag, dmap);
1279 /* Abort a device bulk request. */
1281 uhci_device_bulk_abort(reqh)
1282 usbd_request_handle reqh;
1284 /* XXX inactivate */
1285 usb_delay_ms(reqh->pipe->device->bus, 1);/* make sure it is done */
1289 /* Close a device bulk pipe. */
1291 uhci_device_bulk_close(pipe)
1292 usbd_pipe_handle pipe;
1294 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1295 usbd_device_handle dev = upipe->pipe.device;
1296 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1298 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1299 uhci_free_intr_info(upipe->iinfo);
1300 /* XXX free other resources */
1304 uhci_device_ctrl_transfer(reqh)
1305 usbd_request_handle reqh;
1311 r = usb_insert_transfer(reqh);
1313 if (r != USBD_NORMAL_COMPLETION)
1316 return (uhci_device_ctrl_start(reqh));
1320 uhci_device_ctrl_start(reqh)
1321 usbd_request_handle reqh;
1323 uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
1327 panic("uhci_device_ctrl_transfer: not a request\n");
1329 r = uhci_device_request(reqh);
1330 if (r != USBD_NORMAL_COMPLETION)
1333 if (sc->sc_bus.use_polling)
1334 uhci_waitintr(sc, reqh);
1335 return (USBD_IN_PROGRESS);
1339 uhci_device_intr_transfer(reqh)
1340 usbd_request_handle reqh;
1346 r = usb_insert_transfer(reqh);
1348 if (r != USBD_NORMAL_COMPLETION)
1351 return (uhci_device_intr_start(reqh));
1355 uhci_device_intr_start(reqh)
1356 usbd_request_handle reqh;
1358 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1359 usbd_device_handle dev = upipe->pipe.device;
1360 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1361 uhci_intr_info_t *ii = upipe->iinfo;
1362 uhci_soft_td_t *xfer, *xferend;
1363 uhci_soft_qh_t *sqh;
1369 DPRINTFN(3, ("uhci_device_intr_transfer: reqh=%p buf=%p len=%d "
1371 reqh, reqh->buffer, reqh->length, reqh->flags));
1374 panic("uhci_device_intr_transfer: a request\n");
1377 dmap = &upipe->u.intr.datadma;
1379 return (USBD_INVAL); /* XXX should it be? */
1381 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1382 if (r != USBD_NORMAL_COMPLETION)
1384 r = uhci_alloc_std_chain(upipe, sc, len, 1,
1385 reqh->flags & USBD_SHORT_XFER_OK,
1386 dmap, &xfer, &xferend);
1387 if (r != USBD_NORMAL_COMPLETION)
1389 xferend->td->td_status |= UHCI_TD_IOC;
1392 if (uhcidebug > 10) {
1393 printf("uhci_device_intr_transfer: xfer(1)\n");
1394 uhci_dump_tds(xfer);
1395 uhci_dump_qh(upipe->u.intr.qhs[0]);
1400 /* Set up interrupt info. */
1402 ii->stdstart = xfer;
1403 ii->stdend = xferend;
1408 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
1409 upipe->u.intr.qhs[0]));
1410 for (i = 0; i < upipe->u.intr.npoll; i++) {
1411 sqh = upipe->u.intr.qhs[i];
1412 sqh->qh->elink = xfer;
1413 sqh->qh->qh_elink = xfer->physaddr;
1418 if (uhcidebug > 10) {
1419 printf("uhci_device_intr_transfer: xfer(2)\n");
1420 uhci_dump_tds(xfer);
1421 uhci_dump_qh(upipe->u.intr.qhs[0]);
1425 return (USBD_IN_PROGRESS);
1429 usb_freemem(sc->sc_dmatag, dmap);
1434 /* Abort a device control request. */
1436 uhci_device_ctrl_abort(reqh)
1437 usbd_request_handle reqh;
1439 /* XXX inactivate */
1440 usb_delay_ms(reqh->pipe->device->bus, 1); /* make sure it is done */
1444 /* Close a device control pipe. */
1446 uhci_device_ctrl_close(pipe)
1447 usbd_pipe_handle pipe;
1449 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1451 uhci_free_intr_info(upipe->iinfo);
1452 /* XXX free other resources */
1455 /* Abort a device interrupt request. */
1457 uhci_device_intr_abort(reqh)
1458 usbd_request_handle reqh;
1460 struct uhci_pipe *upipe;
1462 DPRINTFN(1, ("uhci_device_intr_abort: reqh=%p\n", reqh));
1463 /* XXX inactivate */
1464 usb_delay_ms(reqh->pipe->device->bus, 2); /* make sure it is done */
1465 if (reqh->pipe->intrreqh == reqh) {
1466 DPRINTF(("uhci_device_intr_abort: remove\n"));
1467 reqh->pipe->intrreqh = 0;
1468 upipe = (struct uhci_pipe *)reqh->pipe;
1469 uhci_intr_done(upipe->u.intr.qhs[0]->intr_info);
1473 /* Close a device interrupt pipe. */
1475 uhci_device_intr_close(pipe)
1476 usbd_pipe_handle pipe;
1478 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1479 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1482 upipe->iinfo->stdstart = 0; /* inactive */
1484 /* Unlink descriptors from controller data structures. */
1485 npoll = upipe->u.intr.npoll;
1486 uhci_lock_frames(sc);
1487 for (i = 0; i < npoll; i++)
1488 uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
1489 upipe->u.intr.qhs[i]);
1490 uhci_unlock_frames(sc);
1493 * We now have to wait for any activity on the physical
1494 * descriptors to stop.
1496 usb_delay_ms(&sc->sc_bus, 2);
1498 for(i = 0; i < npoll; i++)
1499 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
1500 free(upipe->u.intr.qhs, M_USB);
1503 LIST_REMOVE(upipe->iinfo, list); /* remove from active list */
1505 uhci_free_intr_info(upipe->iinfo);
1507 /* XXX free other resources */
1511 uhci_device_request(reqh)
1512 usbd_request_handle reqh;
1514 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1515 usb_device_request_t *req = &reqh->request;
1516 usbd_device_handle dev = upipe->pipe.device;
1517 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1518 int addr = dev->address;
1519 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1520 uhci_intr_info_t *ii = upipe->iinfo;
1521 uhci_soft_td_t *setup, *xfer, *stat, *next, *xferend;
1522 uhci_soft_qh_t *sqh;
1530 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
1531 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1532 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1533 UGETW(req->wIndex), UGETW(req->wLength),
1536 ls = dev->lowspeed ? UHCI_TD_LS : 0;
1537 isread = req->bmRequestType & UT_READ;
1538 len = UGETW(req->wLength);
1540 setup = upipe->u.ctl.setup;
1541 stat = upipe->u.ctl.stat;
1542 sqh = upipe->u.ctl.sqh;
1543 dmap = &upipe->u.ctl.datadma;
1545 /* Set up data transaction */
1547 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1548 if (r != USBD_NORMAL_COMPLETION)
1550 upipe->pipe.endpoint->toggle = 1;
1551 r = uhci_alloc_std_chain(upipe, sc, len, isread,
1552 reqh->flags & USBD_SHORT_XFER_OK,
1553 dmap, &xfer, &xferend);
1554 if (r != USBD_NORMAL_COMPLETION)
1557 xferend->td->link.std = stat;
1558 xferend->td->td_link = stat->physaddr;
1562 upipe->u.ctl.length = len;
1564 memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
1565 if (!isread && len != 0)
1566 memcpy(KERNADDR(dmap), reqh->buffer, len);
1568 setup->td->link.std = next;
1569 setup->td->td_link = next->physaddr;
1570 setup->td->td_status = UHCI_TD_SET_ERRCNT(2) | ls | UHCI_TD_ACTIVE;
1571 setup->td->td_token = UHCI_TD_SETUP(sizeof *req, endpt, addr);
1572 setup->td->td_buffer = DMAADDR(&upipe->u.ctl.reqdma);
1574 stat->td->link.std = 0;
1575 stat->td->td_link = UHCI_PTR_T;
1576 stat->td->td_status = UHCI_TD_SET_ERRCNT(2) | ls |
1577 UHCI_TD_ACTIVE | UHCI_TD_IOC;
1578 stat->td->td_token =
1579 isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
1580 UHCI_TD_IN (0, endpt, addr, 1);
1581 stat->td->td_buffer = 0;
1584 if (uhcidebug > 20) {
1585 printf("uhci_device_request: setup\n");
1586 uhci_dump_td(setup);
1587 printf("uhci_device_request: stat\n");
1592 /* Set up interrupt info. */
1594 ii->stdstart = setup;
1600 sqh->qh->elink = setup;
1601 sqh->qh->qh_elink = setup->physaddr;
1602 sqh->intr_info = ii;
1605 uhci_add_ctrl(sc, sqh);
1606 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1608 if (uhcidebug > 12) {
1609 uhci_soft_td_t *std;
1610 uhci_soft_qh_t *xqh;
1611 uhci_soft_qh_t *sxqh;
1613 uhci_physaddr_t link;
1614 printf("uhci_enter_ctl_q: follow from [0]\n");
1615 for (std = sc->sc_vframes[0].htd, link = 0;
1616 (link & UHCI_PTR_Q) == 0;
1617 std = std->td->link.std) {
1618 link = std->td->td_link;
1621 for (sxqh = xqh = (uhci_soft_qh_t *)std;
1623 xqh = (maxqh++ == 5 || xqh->qh->hlink==sxqh ||
1624 xqh->qh->hlink==xqh ? NULL : xqh->qh->hlink)) {
1628 printf("Enqueued QH:\n");
1630 uhci_dump_tds(sqh->qh->elink);
1633 if (reqh->timeout && !sc->sc_bus.use_polling) {
1634 usb_timeout(uhci_timeout, ii,
1635 MS_TO_TICKS(reqh->timeout), ii->timeout_handle);
1639 return (USBD_NORMAL_COMPLETION);
1643 usb_freemem(sc->sc_dmatag, dmap);
1649 uhci_device_isoc_transfer(reqh)
1650 usbd_request_handle reqh;
1652 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1654 usbd_device_handle dev = upipe->pipe.device;
1655 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1658 DPRINTFN(1,("uhci_device_isoc_transfer: sc=%p\n", sc));
1659 if (upipe->u.iso.bufsize == 0)
1660 return (USBD_INVAL);
1667 uhci_device_isoc_start(reqh)
1668 usbd_request_handle reqh;
1674 uhci_device_isoc_abort(reqh)
1675 usbd_request_handle reqh;
1677 /* XXX Can't abort a single request. */
1681 uhci_device_isoc_close(pipe)
1682 usbd_pipe_handle pipe;
1684 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1685 usbd_device_handle dev = upipe->pipe.device;
1686 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1691 * Make sure all TDs are marked as inactive.
1692 * Wait for completion.
1696 iso = &upipe->u.iso;
1698 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
1699 iso->stds[i]->td->td_status &= ~UHCI_TD_ACTIVE;
1700 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
1702 uhci_lock_frames(sc);
1703 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
1704 uhci_soft_td_t *std, *vstd;
1707 for (vstd = sc->sc_vframes[i % UHCI_VFRAMELIST_COUNT].htd;
1708 vstd && vstd->td->link.std != std;
1709 vstd = vstd->td->link.std)
1713 printf("uhci_device_isoc_close: %p not found\n", std);
1714 uhci_unlock_frames(sc);
1717 vstd->td->link = std->td->link;
1718 vstd->td->td_link = std->td->td_link;
1719 uhci_free_std(sc, std);
1721 uhci_unlock_frames(sc);
1723 for (i = 0; i < iso->nbuf; i++)
1724 usb_freemem(sc->sc_dmatag, &iso->bufs[i]);
1725 free(iso->stds, M_USB);
1726 free(iso->bufs, M_USB);
1728 /* XXX what else? */
1732 uhci_device_isoc_setbuf(pipe, bufsize, nbuf)
1733 usbd_pipe_handle pipe;
1737 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1738 usbd_device_handle dev = upipe->pipe.device;
1739 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1740 int addr = upipe->pipe.device->address;
1741 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1742 int rd = upipe->pipe.endpoint->edesc->bEndpointAddress & UE_IN;
1748 * For simplicity the number of buffers must fit nicely in the frame
1751 if (UHCI_VFRAMELIST_COUNT % nbuf != 0)
1752 return (USBD_INVAL);
1754 iso = &upipe->u.iso;
1755 iso->bufsize = bufsize;
1758 /* Allocate memory for buffers. */
1759 iso->bufs = malloc(nbuf * sizeof(usb_dma_t), M_USB, M_WAITOK);
1760 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
1763 for (i = 0; i < nbuf; i++) {
1764 r = usb_allocmem(sc->sc_dmatag, bufsize, 0, &iso->bufs[i]);
1765 if (r != USBD_NORMAL_COMPLETION) {
1771 /* Allocate the TDs. */
1772 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
1773 iso->stds[i] = uhci_alloc_std(sc);
1774 if (iso->stds[i] == 0)
1778 /* XXX check schedule */
1780 /* XXX interrupts */
1782 /* Insert TDs into schedule, all marked inactive. */
1783 uhci_lock_frames(sc);
1784 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
1785 uhci_soft_td_t *std, *vstd;
1788 std->td->td_status = UHCI_TD_IOS; /* iso, inactive */
1790 rd ? UHCI_TD_IN (0, endpt, addr, 0) :
1791 UHCI_TD_OUT(0, endpt, addr, 0);
1792 std->td->td_buffer = DMAADDR(&iso->bufs[i % nbuf]);
1794 vstd = sc->sc_vframes[i % UHCI_VFRAMELIST_COUNT].htd;
1795 std->td->link = vstd->td->link;
1796 std->td->td_link = vstd->td->td_link;
1797 vstd->td->link.std = std;
1798 vstd->td->td_link = std->physaddr;
1800 uhci_unlock_frames(sc);
1802 return (USBD_NORMAL_COMPLETION);
1806 uhci_free_std(sc, iso->stds[i]);
1808 for (i = 0; i < nbuf; i++)
1809 usb_freemem(sc->sc_dmatag, &iso->bufs[i]);
1810 free(iso->stds, M_USB);
1811 free(iso->bufs, M_USB);
1812 return (USBD_NOMEM);
1817 uhci_intr_info_t *ii;
1823 uhci_intr_info_t *ii;
1825 uhci_softc_t *sc = ii->sc;
1826 usbd_request_handle reqh = ii->reqh;
1827 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1829 uhci_soft_qh_t *sqh;
1832 DPRINTFN(5, ("uhci_intr_done: length=%d\n", reqh->actlen));
1834 dma = &upipe->u.intr.datadma;
1835 memcpy(reqh->buffer, KERNADDR(dma), reqh->actlen);
1836 npoll = upipe->u.intr.npoll;
1837 for(i = 0; i < npoll; i++) {
1838 sqh = upipe->u.intr.qhs[i];
1840 sqh->qh->qh_elink = UHCI_PTR_T;
1842 uhci_free_std_chain(sc, ii->stdstart, 0);
1845 if (reqh->pipe->intrreqh == reqh) {
1846 uhci_soft_td_t *xfer, *xferend;
1848 /* This alloc cannot fail since we freed the chain above. */
1849 uhci_alloc_std_chain(upipe, sc, reqh->length, 1,
1850 reqh->flags & USBD_SHORT_XFER_OK,
1851 dma, &xfer, &xferend);
1852 xferend->td->td_status |= UHCI_TD_IOC;
1855 if (uhcidebug > 10) {
1856 printf("uhci_device_intr_done: xfer(1)\n");
1857 uhci_dump_tds(xfer);
1858 uhci_dump_qh(upipe->u.intr.qhs[0]);
1862 ii->stdstart = xfer;
1863 ii->stdend = xferend;
1867 for (i = 0; i < npoll; i++) {
1868 sqh = upipe->u.intr.qhs[i];
1869 sqh->qh->elink = xfer;
1870 sqh->qh->qh_elink = xfer->physaddr;
1873 usb_freemem(sc->sc_dmatag, dma);
1874 ii->stdstart = 0; /* mark as inactive */
1875 usb_start_next(reqh->pipe);
1879 /* Deallocate request data structures */
1882 uhci_intr_info_t *ii;
1884 uhci_softc_t *sc = ii->sc;
1885 usbd_request_handle reqh = ii->reqh;
1886 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1887 u_int len = upipe->u.ctl.length;
1889 uhci_td_t *htd = ii->stdstart->td;
1893 panic("uhci_ctrl_done: not a request\n");
1896 LIST_REMOVE(ii, list); /* remove from active list */
1898 uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
1901 dma = &upipe->u.ctl.datadma;
1902 if (reqh->request.bmRequestType & UT_READ)
1903 memcpy(reqh->buffer, KERNADDR(dma), len);
1904 uhci_free_std_chain(sc, htd->link.std, ii->stdend);
1905 usb_freemem(sc->sc_dmatag, dma);
1907 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", reqh->actlen));
1910 /* Deallocate request data structures */
1913 uhci_intr_info_t *ii;
1915 uhci_softc_t *sc = ii->sc;
1916 usbd_request_handle reqh = ii->reqh;
1917 struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1918 u_int len = upipe->u.bulk.length;
1920 uhci_td_t *htd = ii->stdstart->td;
1922 LIST_REMOVE(ii, list); /* remove from active list */
1924 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
1927 dma = &upipe->u.bulk.datadma;
1928 if (upipe->u.bulk.isread && len != 0)
1929 memcpy(reqh->buffer, KERNADDR(dma), len);
1930 uhci_free_std_chain(sc, htd->link.std, 0);
1931 usb_freemem(sc->sc_dmatag, dma);
1933 DPRINTFN(4, ("uhci_bulk_done: length=%d\n", reqh->actlen));
1934 /* XXX compute new toggle */
1937 /* Add interrupt QH, called with vflock. */
1939 uhci_add_intr(sc, n, sqh)
1942 uhci_soft_qh_t *sqh;
1944 struct uhci_vframe *vf = &sc->sc_vframes[n];
1947 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
1949 sqh->qh->hlink = eqh->hlink;
1950 sqh->qh->qh_hlink = eqh->qh_hlink;
1952 eqh->qh_hlink = sqh->physaddr | UHCI_PTR_Q;
1957 /* Remove interrupt QH, called with vflock. */
1959 uhci_remove_intr(sc, n, sqh)
1962 uhci_soft_qh_t *sqh;
1964 struct uhci_vframe *vf = &sc->sc_vframes[n];
1965 uhci_soft_qh_t *pqh;
1967 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
1969 for (pqh = vf->hqh; pqh->qh->hlink != sqh; pqh = pqh->qh->hlink)
1970 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
1971 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
1972 printf("uhci_remove_intr: QH not found\n");
1978 pqh->qh->hlink = sqh->qh->hlink;
1979 pqh->qh->qh_hlink = sqh->qh->qh_hlink;
1986 uhci_device_setintr(sc, upipe, ival)
1988 struct uhci_pipe *upipe;
1991 uhci_soft_qh_t *sqh;
1993 u_int bestbw, bw, bestoffs, offs;
1995 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
1997 printf("uhci_setintr: 0 interval\n");
1998 return (USBD_INVAL);
2001 if (ival > UHCI_VFRAMELIST_COUNT)
2002 ival = UHCI_VFRAMELIST_COUNT;
2003 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2004 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
2006 upipe->u.intr.npoll = npoll;
2008 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USB, M_WAITOK);
2011 * Figure out which offset in the schedule that has most
2012 * bandwidth left over.
2014 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2015 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2016 for (bw = i = 0; i < npoll; i++)
2017 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2023 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2025 upipe->iinfo->stdstart = 0;
2026 for(i = 0; i < npoll; i++) {
2027 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2029 sqh->qh->qh_elink = UHCI_PTR_T;
2030 sqh->pos = MOD(i * ival + bestoffs);
2031 sqh->intr_info = upipe->iinfo;
2036 LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
2039 uhci_lock_frames(sc);
2040 /* Enter QHs into the controller data structures. */
2041 for(i = 0; i < npoll; i++)
2042 uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
2043 upipe->u.intr.qhs[i]);
2044 uhci_unlock_frames(sc);
2046 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
2047 return (USBD_NORMAL_COMPLETION);
2050 /* Open a new pipe. */
2053 usbd_pipe_handle pipe;
2055 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2056 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2057 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2060 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2061 pipe, pipe->device->address,
2062 ed->bEndpointAddress, sc->sc_addr));
2063 if (pipe->device->address == sc->sc_addr) {
2064 switch (ed->bEndpointAddress) {
2065 case USB_CONTROL_ENDPOINT:
2066 pipe->methods = &uhci_root_ctrl_methods;
2068 case UE_IN | UHCI_INTR_ENDPT:
2069 pipe->methods = &uhci_root_intr_methods;
2072 return (USBD_INVAL);
2075 upipe->iinfo = uhci_alloc_intr_info(sc);
2076 if (upipe->iinfo == 0)
2077 return (USBD_NOMEM);
2078 switch (ed->bmAttributes & UE_XFERTYPE) {
2080 pipe->methods = &uhci_device_ctrl_methods;
2081 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2082 if (upipe->u.ctl.sqh == 0)
2084 upipe->u.ctl.setup = uhci_alloc_std(sc);
2085 if (upipe->u.ctl.setup == 0) {
2086 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2089 upipe->u.ctl.stat = uhci_alloc_std(sc);
2090 if (upipe->u.ctl.stat == 0) {
2091 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2092 uhci_free_std(sc, upipe->u.ctl.setup);
2095 r = usb_allocmem(sc->sc_dmatag,
2096 sizeof(usb_device_request_t),
2097 0, &upipe->u.ctl.reqdma);
2098 if (r != USBD_NORMAL_COMPLETION) {
2099 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2100 uhci_free_std(sc, upipe->u.ctl.setup);
2101 uhci_free_std(sc, upipe->u.ctl.stat);
2106 pipe->methods = &uhci_device_intr_methods;
2107 return (uhci_device_setintr(sc, upipe, ed->bInterval));
2108 case UE_ISOCHRONOUS:
2109 pipe->methods = &uhci_device_isoc_methods;
2110 upipe->u.iso.nbuf = 0;
2111 return (USBD_NORMAL_COMPLETION);
2113 pipe->methods = &uhci_device_bulk_methods;
2114 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2115 if (upipe->u.bulk.sqh == 0)
2120 return (USBD_NORMAL_COMPLETION);
2123 uhci_free_intr_info(upipe->iinfo);
2124 return (USBD_NOMEM);
2128 * Data structures and routines to emulate the root hub.
2130 usb_device_descriptor_t uhci_devd = {
2131 USB_DEVICE_DESCRIPTOR_SIZE,
2132 UDESC_DEVICE, /* type */
2133 {0x00, 0x01}, /* USB version */
2134 UCLASS_HUB, /* class */
2135 USUBCLASS_HUB, /* subclass */
2137 64, /* max packet */
2138 {0},{0},{0x00,0x01}, /* device id */
2139 1,2,0, /* string indicies */
2140 1 /* # of configurations */
2143 usb_config_descriptor_t uhci_confd = {
2144 USB_CONFIG_DESCRIPTOR_SIZE,
2146 {USB_CONFIG_DESCRIPTOR_SIZE +
2147 USB_INTERFACE_DESCRIPTOR_SIZE +
2148 USB_ENDPOINT_DESCRIPTOR_SIZE},
2156 usb_interface_descriptor_t uhci_ifcd = {
2157 USB_INTERFACE_DESCRIPTOR_SIZE,
2168 usb_endpoint_descriptor_t uhci_endpd = {
2169 USB_ENDPOINT_DESCRIPTOR_SIZE,
2171 UE_IN | UHCI_INTR_ENDPT,
2177 usb_hub_descriptor_t uhci_hubd_piix = {
2178 USB_HUB_DESCRIPTOR_SIZE,
2181 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2182 50, /* power on to power good */
2184 { 0x00 }, /* both ports are removable */
2189 usb_string_descriptor_t *p;
2197 p->bLength = 2 * strlen(s) + 2;
2200 p->bDescriptorType = UDESC_STRING;
2202 for (i = 0; s[i] && l > 1; i++, l -= 2)
2203 USETW2(p->bString[i], 0, s[i]);
2208 * Simulate a hardware hub by handling all the necessary requests.
2211 uhci_root_ctrl_transfer(reqh)
2212 usbd_request_handle reqh;
2218 r = usb_insert_transfer(reqh);
2220 if (r != USBD_NORMAL_COMPLETION)
2223 return (uhci_root_ctrl_start(reqh));
2227 uhci_root_ctrl_start(reqh)
2228 usbd_request_handle reqh;
2230 uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
2231 usb_device_request_t *req;
2234 int len, value, index, status, change, l, totlen = 0;
2235 usb_port_status_t ps;
2239 panic("uhci_root_ctrl_transfer: not a request\n");
2240 req = &reqh->request;
2243 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
2244 req->bmRequestType, req->bRequest));
2246 len = UGETW(req->wLength);
2247 value = UGETW(req->wValue);
2248 index = UGETW(req->wIndex);
2249 #define C(x,y) ((x) | ((y) << 8))
2250 switch(C(req->bRequest, req->bmRequestType)) {
2251 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2252 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2253 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2255 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2256 * for the integrated root hub.
2259 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2261 *(u_int8_t *)buf = sc->sc_conf;
2265 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2266 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
2267 switch(value >> 8) {
2269 if ((value & 0xff) != 0) {
2273 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2274 memcpy(buf, &uhci_devd, l);
2277 if ((value & 0xff) != 0) {
2281 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2282 memcpy(buf, &uhci_confd, l);
2283 buf = (char *)buf + l;
2285 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2287 memcpy(buf, &uhci_ifcd, l);
2288 buf = (char *)buf + l;
2290 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2292 memcpy(buf, &uhci_endpd, l);
2297 *(u_int8_t *)buf = 0;
2299 switch (value & 0xff) {
2300 case 1: /* Vendor */
2301 totlen = uhci_str(buf, len, sc->sc_vendor);
2303 case 2: /* Product */
2304 totlen = uhci_str(buf, len, "UHCI root hub");
2313 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2315 *(u_int8_t *)buf = 0;
2319 case C(UR_GET_STATUS, UT_READ_DEVICE):
2321 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2325 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2326 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2328 USETW(((usb_status_t *)buf)->wStatus, 0);
2332 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2333 if (value >= USB_MAX_DEVICES) {
2337 sc->sc_addr = value;
2339 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2340 if (value != 0 && value != 1) {
2344 sc->sc_conf = value;
2346 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2348 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2349 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2350 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2353 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2355 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2358 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2360 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2361 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2362 "port=%d feature=%d\n",
2365 port = UHCI_PORTSC1;
2366 else if (index == 2)
2367 port = UHCI_PORTSC2;
2373 case UHF_PORT_ENABLE:
2374 x = UREAD2(sc, port);
2375 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2377 case UHF_PORT_SUSPEND:
2378 x = UREAD2(sc, port);
2379 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
2381 case UHF_PORT_RESET:
2382 x = UREAD2(sc, port);
2383 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2385 case UHF_C_PORT_CONNECTION:
2386 x = UREAD2(sc, port);
2387 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2389 case UHF_C_PORT_ENABLE:
2390 x = UREAD2(sc, port);
2391 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2393 case UHF_C_PORT_OVER_CURRENT:
2394 x = UREAD2(sc, port);
2395 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2397 case UHF_C_PORT_RESET:
2399 r = USBD_NORMAL_COMPLETION;
2401 case UHF_PORT_CONNECTION:
2402 case UHF_PORT_OVER_CURRENT:
2403 case UHF_PORT_POWER:
2404 case UHF_PORT_LOW_SPEED:
2405 case UHF_C_PORT_SUSPEND:
2411 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2413 port = UHCI_PORTSC1;
2414 else if (index == 2)
2415 port = UHCI_PORTSC2;
2422 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2423 UHCI_PORTSC_LS_SHIFT;
2427 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2432 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
2434 memcpy(buf, &uhci_hubd_piix, l);
2436 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2441 memset(buf, 0, len);
2444 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2446 port = UHCI_PORTSC1;
2447 else if (index == 2)
2448 port = UHCI_PORTSC2;
2457 x = UREAD2(sc, port);
2458 status = change = 0;
2459 if (x & UHCI_PORTSC_CCS )
2460 status |= UPS_CURRENT_CONNECT_STATUS;
2461 if (x & UHCI_PORTSC_CSC )
2462 change |= UPS_C_CONNECT_STATUS;
2463 if (x & UHCI_PORTSC_PE )
2464 status |= UPS_PORT_ENABLED;
2465 if (x & UHCI_PORTSC_POEDC)
2466 change |= UPS_C_PORT_ENABLED;
2467 if (x & UHCI_PORTSC_OCI )
2468 status |= UPS_OVERCURRENT_INDICATOR;
2469 if (x & UHCI_PORTSC_OCIC )
2470 change |= UPS_C_OVERCURRENT_INDICATOR;
2471 if (x & UHCI_PORTSC_SUSP )
2472 status |= UPS_SUSPEND;
2473 if (x & UHCI_PORTSC_LSDA )
2474 status |= UPS_LOW_SPEED;
2475 status |= UPS_PORT_POWER;
2477 change |= UPS_C_PORT_RESET;
2478 USETW(ps.wPortStatus, status);
2479 USETW(ps.wPortChange, change);
2480 l = min(len, sizeof ps);
2481 memcpy(buf, &ps, l);
2484 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2487 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2489 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2491 port = UHCI_PORTSC1;
2492 else if (index == 2)
2493 port = UHCI_PORTSC2;
2499 case UHF_PORT_ENABLE:
2500 x = UREAD2(sc, port);
2501 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2503 case UHF_PORT_SUSPEND:
2504 x = UREAD2(sc, port);
2505 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2507 case UHF_PORT_RESET:
2508 x = UREAD2(sc, port);
2509 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2510 usb_delay_ms(&sc->sc_bus, 10);
2511 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2513 x = UREAD2(sc, port);
2514 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2516 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
2517 index, UREAD2(sc, port)));
2520 case UHF_C_PORT_CONNECTION:
2521 case UHF_C_PORT_ENABLE:
2522 case UHF_C_PORT_OVER_CURRENT:
2523 case UHF_PORT_CONNECTION:
2524 case UHF_PORT_OVER_CURRENT:
2525 case UHF_PORT_POWER:
2526 case UHF_PORT_LOW_SPEED:
2527 case UHF_C_PORT_SUSPEND:
2528 case UHF_C_PORT_RESET:
2538 reqh->actlen = totlen;
2539 r = USBD_NORMAL_COMPLETION;
2543 usb_start_next(reqh->pipe);
2544 return (USBD_IN_PROGRESS);
2547 /* Abort a root control request. */
2549 uhci_root_ctrl_abort(reqh)
2550 usbd_request_handle reqh;
2552 /* Nothing to do, all transfers are syncronous. */
2555 /* Close the root pipe. */
2557 uhci_root_ctrl_close(pipe)
2558 usbd_pipe_handle pipe;
2560 usb_untimeout(uhci_timo, pipe->intrreqh, pipe->intrreqh->timo_handle);
2561 DPRINTF(("uhci_root_ctrl_close\n"));
2564 /* Abort a root interrupt request. */
2566 uhci_root_intr_abort(reqh)
2567 usbd_request_handle reqh;
2569 usb_untimeout(uhci_timo, reqh, reqh->timo_handle);
2573 uhci_root_intr_transfer(reqh)
2574 usbd_request_handle reqh;
2580 r = usb_insert_transfer(reqh);
2582 if (r != USBD_NORMAL_COMPLETION)
2585 return (uhci_root_intr_start(reqh));
2588 /* Start a transfer on the root interrupt pipe */
2590 uhci_root_intr_start(reqh)
2591 usbd_request_handle reqh;
2593 usbd_pipe_handle pipe = reqh->pipe;
2594 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2595 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2600 DPRINTFN(3, ("uhci_root_intr_transfer: reqh=%p buf=%p len=%d "
2602 reqh, reqh->buffer, reqh->length, reqh->flags));
2605 dmap = &upipe->u.intr.datadma;
2607 return (USBD_INVAL); /* XXX should it be? */
2609 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
2610 if (r != USBD_NORMAL_COMPLETION)
2613 sc->sc_ival = MS_TO_TICKS(reqh->pipe->endpoint->edesc->bInterval);
2614 usb_timeout(uhci_timo, reqh, sc->sc_ival, reqh->timo_handle);
2615 return (USBD_IN_PROGRESS);
2618 /* Close the root interrupt pipe. */
2620 uhci_root_intr_close(pipe)
2621 usbd_pipe_handle pipe;
2623 usb_untimeout(uhci_timo, pipe->intrreqh, pipe->intrreqh->timo_handle);
2624 DPRINTF(("uhci_root_intr_close\n"));