Tue Dec 6 05:06:50 2011 UTC ()
my compiles-but-un-runtime-tested port of uhci to usbmp branch.
based upon the ohci and ehci changes.


(mrg)
diff -r1.240.6.1 -r1.240.6.2 src/sys/dev/usb/uhci.c
diff -r1.48 -r1.48.12.1 src/sys/dev/usb/uhcivar.h

cvs diff -r1.240.6.1 -r1.240.6.2 src/sys/dev/usb/uhci.c (expand / switch to unified diff)

--- src/sys/dev/usb/uhci.c 2011/12/04 13:23:17 1.240.6.1
+++ src/sys/dev/usb/uhci.c 2011/12/06 05:06:50 1.240.6.2
@@ -1,23 +1,24 @@ @@ -1,23 +1,24 @@
1/* $NetBSD: uhci.c,v 1.240.6.1 2011/12/04 13:23:17 jmcneill Exp $ */ 1/* $NetBSD: uhci.c,v 1.240.6.2 2011/12/06 05:06:50 mrg Exp $ */
2/* $FreeBSD: src/sys/dev/usb/uhci.c,v 1.33 1999/11/17 22:33:41 n_hibma Exp $ */ 2/* $FreeBSD: src/sys/dev/usb/uhci.c,v 1.33 1999/11/17 22:33:41 n_hibma Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc. 5 * Copyright (c) 1998, 2004, 2011 The NetBSD Foundation, Inc.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * This code is derived from software contributed to The NetBSD Foundation 8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at 9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology. 10 * Carlstedt Research & Technology, Jared D. McNeill (jmcneill@invisible.ca)
 11 * and Matthew R. Green.
11 * 12 *
12 * Redistribution and use in source and binary forms, with or without 13 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 14 * modification, are permitted provided that the following conditions
14 * are met: 15 * are met:
15 * 1. Redistributions of source code must retain the above copyright 16 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer. 17 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright 18 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the 19 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution. 20 * documentation and/or other materials provided with the distribution.
20 * 21 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
@@ -32,34 +33,34 @@ @@ -32,34 +33,34 @@
32 */ 33 */
33 34
34/* 35/*
35 * USB Universal Host Controller driver. 36 * USB Universal Host Controller driver.
36 * Handles e.g. PIIX3 and PIIX4. 37 * Handles e.g. PIIX3 and PIIX4.
37 * 38 *
38 * UHCI spec: http://www.intel.com/technology/usb/spec.htm 39 * UHCI spec: http://www.intel.com/technology/usb/spec.htm
39 * USB spec: http://www.usb.org/developers/docs/ 40 * USB spec: http://www.usb.org/developers/docs/
40 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf 41 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
41 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf 42 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
42 */ 43 */
43 44
44#include <sys/cdefs.h> 45#include <sys/cdefs.h>
45__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.240.6.1 2011/12/04 13:23:17 jmcneill Exp $"); 46__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.240.6.2 2011/12/06 05:06:50 mrg Exp $");
46 47
47#include "opt_usb.h" 48#include "opt_usb.h"
48 49
49#include <sys/param.h> 50#include <sys/param.h>
50#include <sys/systm.h> 51#include <sys/systm.h>
51#include <sys/kernel.h> 52#include <sys/kernel.h>
52#include <sys/malloc.h> 53#include <sys/kmem.h>
53#include <sys/device.h> 54#include <sys/device.h>
54#include <sys/select.h> 55#include <sys/select.h>
55#include <sys/extent.h> 56#include <sys/extent.h>
56#include <sys/proc.h> 57#include <sys/proc.h>
57#include <sys/queue.h> 58#include <sys/queue.h>
58#include <sys/bus.h> 59#include <sys/bus.h>
59 60
60#include <machine/endian.h> 61#include <machine/endian.h>
61 62
62#include <dev/usb/usb.h> 63#include <dev/usb/usb.h>
63#include <dev/usb/usbdi.h> 64#include <dev/usb/usbdi.h>
64#include <dev/usb/usbdivar.h> 65#include <dev/usb/usbdivar.h>
65#include <dev/usb/usb_mem.h> 66#include <dev/usb/usb_mem.h>
@@ -161,26 +162,28 @@ Static void uhci_remove_ls_ctrl(uhci_so @@ -161,26 +162,28 @@ Static void uhci_remove_ls_ctrl(uhci_so
161Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *); 162Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
162Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *); 163Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
163Static void uhci_add_loop(uhci_softc_t *sc); 164Static void uhci_add_loop(uhci_softc_t *sc);
164Static void uhci_rem_loop(uhci_softc_t *sc); 165Static void uhci_rem_loop(uhci_softc_t *sc);
165 166
166Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe); 167Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
167Static void uhci_device_isoc_enter(usbd_xfer_handle); 168Static void uhci_device_isoc_enter(usbd_xfer_handle);
168 169
169Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t); 170Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
170Static void uhci_freem(struct usbd_bus *, usb_dma_t *); 171Static void uhci_freem(struct usbd_bus *, usb_dma_t *);
171 172
172Static usbd_xfer_handle uhci_allocx(struct usbd_bus *); 173Static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
173Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle); 174Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
 175Static void uhci_get_locks(struct usbd_bus *, kmutex_t **,
 176 kmutex_t **);
174 177
175Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle); 178Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
176Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle); 179Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
177Static void uhci_device_ctrl_abort(usbd_xfer_handle); 180Static void uhci_device_ctrl_abort(usbd_xfer_handle);
178Static void uhci_device_ctrl_close(usbd_pipe_handle); 181Static void uhci_device_ctrl_close(usbd_pipe_handle);
179Static void uhci_device_ctrl_done(usbd_xfer_handle); 182Static void uhci_device_ctrl_done(usbd_xfer_handle);
180 183
181Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle); 184Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
182Static usbd_status uhci_device_intr_start(usbd_xfer_handle); 185Static usbd_status uhci_device_intr_start(usbd_xfer_handle);
183Static void uhci_device_intr_abort(usbd_xfer_handle); 186Static void uhci_device_intr_abort(usbd_xfer_handle);
184Static void uhci_device_intr_close(usbd_pipe_handle); 187Static void uhci_device_intr_close(usbd_pipe_handle);
185Static void uhci_device_intr_done(usbd_xfer_handle); 188Static void uhci_device_intr_done(usbd_xfer_handle);
186 189
@@ -278,27 +281,27 @@ UREAD4(uhci_softc_t *sc, bus_size_t r) @@ -278,27 +281,27 @@ UREAD4(uhci_softc_t *sc, bus_size_t r)
278 281
279#define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK) 282#define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
280 283
281#define UHCI_INTR_ENDPT 1 284#define UHCI_INTR_ENDPT 1
282 285
283const struct usbd_bus_methods uhci_bus_methods = { 286const struct usbd_bus_methods uhci_bus_methods = {
284 uhci_open, 287 uhci_open,
285 uhci_softintr, 288 uhci_softintr,
286 uhci_poll, 289 uhci_poll,
287 uhci_allocm, 290 uhci_allocm,
288 uhci_freem, 291 uhci_freem,
289 uhci_allocx, 292 uhci_allocx,
290 uhci_freex, 293 uhci_freex,
291 NULL, /* uhci_get_locks */ 294 uhci_get_locks,
292}; 295};
293 296
294const struct usbd_pipe_methods uhci_root_ctrl_methods = { 297const struct usbd_pipe_methods uhci_root_ctrl_methods = {
295 uhci_root_ctrl_transfer, 298 uhci_root_ctrl_transfer,
296 uhci_root_ctrl_start, 299 uhci_root_ctrl_start,
297 uhci_root_ctrl_abort, 300 uhci_root_ctrl_abort,
298 uhci_root_ctrl_close, 301 uhci_root_ctrl_close,
299 uhci_noop, 302 uhci_noop,
300 uhci_root_ctrl_done, 303 uhci_root_ctrl_done,
301}; 304};
302 305
303const struct usbd_pipe_methods uhci_root_intr_methods = { 306const struct usbd_pipe_methods uhci_root_intr_methods = {
304 uhci_root_intr_transfer, 307 uhci_root_intr_transfer,
@@ -513,27 +516,31 @@ uhci_init(uhci_softc_t *sc) @@ -513,27 +516,31 @@ uhci_init(uhci_softc_t *sc)
513 j < UHCI_FRAMELIST_COUNT; 516 j < UHCI_FRAMELIST_COUNT;
514 j += UHCI_VFRAMELIST_COUNT) 517 j += UHCI_VFRAMELIST_COUNT)
515 sc->sc_pframes[j] = htole32(std->physaddr); 518 sc->sc_pframes[j] = htole32(std->physaddr);
516 } 519 }
517 usb_syncmem(&sc->sc_dma, 0, 520 usb_syncmem(&sc->sc_dma, 0,
518 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t), 521 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
519 BUS_DMASYNC_PREWRITE); 522 BUS_DMASYNC_PREWRITE);
520 523
521 524
522 LIST_INIT(&sc->sc_intrhead); 525 LIST_INIT(&sc->sc_intrhead);
523 526
524 SIMPLEQ_INIT(&sc->sc_free_xfers); 527 SIMPLEQ_INIT(&sc->sc_free_xfers);
525 528
526 callout_init(&sc->sc_poll_handle, 0); 529 callout_init(&sc->sc_poll_handle, CALLOUT_MPSAFE);
 530
 531 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
 532 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
 533 cv_init(&sc->sc_softwake_cv, "uhciab");
527 534
528 /* Set up the bus struct. */ 535 /* Set up the bus struct. */
529 sc->sc_bus.methods = &uhci_bus_methods; 536 sc->sc_bus.methods = &uhci_bus_methods;
530 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe); 537 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
531 538
532 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */ 539 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
533 540
534 DPRINTFN(1,("uhci_init: enabling\n")); 541 DPRINTFN(1,("uhci_init: enabling\n"));
535 542
536 err = uhci_run(sc, 1); /* and here we go... */ 543 err = uhci_run(sc, 1); /* and here we go... */
537 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE | 544 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
538 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */ 545 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
539 return err; 546 return err;
@@ -570,67 +577,73 @@ uhci_detach(struct uhci_softc *sc, int f @@ -570,67 +577,73 @@ uhci_detach(struct uhci_softc *sc, int f
570 577
571 if (sc->sc_child != NULL) 578 if (sc->sc_child != NULL)
572 rv = config_detach(sc->sc_child, flags); 579 rv = config_detach(sc->sc_child, flags);
573 580
574 if (rv != 0) 581 if (rv != 0)
575 return (rv); 582 return (rv);
576 583
577 /* Free all xfers associated with this HC. */ 584 /* Free all xfers associated with this HC. */
578 for (;;) { 585 for (;;) {
579 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers); 586 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
580 if (xfer == NULL) 587 if (xfer == NULL)
581 break; 588 break;
582 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next); 589 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
583 free(xfer, M_USB); 590 kmem_free(xfer, sizeof(struct uhci_xfer));
584 } 591 }
585 592
586 callout_halt(&sc->sc_poll_handle, NULL); 593 callout_halt(&sc->sc_poll_handle, NULL);
587 callout_destroy(&sc->sc_poll_handle); 594 callout_destroy(&sc->sc_poll_handle);
588 595
 596 cv_destroy(&sc->sc_softwake_cv);
 597
 598 mutex_destroy(&sc->sc_lock);
 599 mutex_destroy(&sc->sc_intr_lock);
 600
589 /* XXX free other data structures XXX */ 601 /* XXX free other data structures XXX */
590 602
591 return (rv); 603 return (rv);
592} 604}
593 605
594usbd_status 606usbd_status
595uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size) 607uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
596{ 608{
597 struct uhci_softc *sc = bus->hci_private; 609 struct uhci_softc *sc = bus->hci_private;
598 usbd_status status; 610 usbd_status status;
599 u_int32_t n; 611 u_int32_t n;
600 612
601 /* 613 /*
602 * XXX 614 * XXX
603 * Since we are allocating a buffer we can assume that we will 615 * Since we are allocating a buffer we can assume that we will
604 * need TDs for it. Since we don't want to allocate those from 616 * need TDs for it. Since we don't want to allocate those from
605 * an interrupt context, we allocate them here and free them again. 617 * an interrupt context, we allocate them here and free them again.
606 * This is no guarantee that we'll get the TDs next time... 618 * This is no guarantee that we'll get the TDs next time...
607 */ 619 */
608 n = size / 8; 620 n = size / 8;
609 if (n > 16) { 621 if (n > 16) {
610 u_int32_t i; 622 u_int32_t i;
611 uhci_soft_td_t **stds; 623 uhci_soft_td_t **stds;
 624
612 DPRINTF(("uhci_allocm: get %d TDs\n", n)); 625 DPRINTF(("uhci_allocm: get %d TDs\n", n));
613 stds = malloc(sizeof(uhci_soft_td_t *) * n, M_TEMP, 626 stds = kmem_alloc(sizeof(uhci_soft_td_t *) * n, KM_SLEEP);
614 M_WAITOK|M_ZERO); 627 if (!stds)
615 for(i=0; i < n; i++) 628 return USBD_NOMEM;
 629 for(i = 0; i < n; i++)
616 stds[i] = uhci_alloc_std(sc); 630 stds[i] = uhci_alloc_std(sc);
617 for(i=0; i < n; i++) 631 for(i = 0; i < n; i++)
618 if (stds[i] != NULL) 632 if (stds[i] != NULL)
619 uhci_free_std(sc, stds[i]); 633 uhci_free_std(sc, stds[i]);
620 free(stds, M_TEMP); 634 kmem_free(stds, sizeof(uhci_soft_td_t *) * n);
621 } 635 }
622 636
623 
624 status = usb_allocmem(&sc->sc_bus, size, 0, dma); 637 status = usb_allocmem(&sc->sc_bus, size, 0, dma);
625 if (status == USBD_NOMEM) 638 if (status == USBD_NOMEM)
626 status = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size); 639 status = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size);
627 return status; 640 return status;
628} 641}
629 642
630void 643void
631uhci_freem(struct usbd_bus *bus, usb_dma_t *dma) 644uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
632{ 645{
633 if (dma->block->flags & USB_DMA_RESERVE) { 646 if (dma->block->flags & USB_DMA_RESERVE) {
634 usb_reserve_freem(&((struct uhci_softc *)bus)->sc_dma_reserve, 647 usb_reserve_freem(&((struct uhci_softc *)bus)->sc_dma_reserve,
635 dma); 648 dma);
636 return; 649 return;
@@ -644,27 +657,27 @@ uhci_allocx(struct usbd_bus *bus) @@ -644,27 +657,27 @@ uhci_allocx(struct usbd_bus *bus)
644 struct uhci_softc *sc = bus->hci_private; 657 struct uhci_softc *sc = bus->hci_private;
645 usbd_xfer_handle xfer; 658 usbd_xfer_handle xfer;
646 659
647 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers); 660 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
648 if (xfer != NULL) { 661 if (xfer != NULL) {
649 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next); 662 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
650#ifdef DIAGNOSTIC 663#ifdef DIAGNOSTIC
651 if (xfer->busy_free != XFER_FREE) { 664 if (xfer->busy_free != XFER_FREE) {
652 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer, 665 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
653 xfer->busy_free); 666 xfer->busy_free);
654 } 667 }
655#endif 668#endif
656 } else { 669 } else {
657 xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_NOWAIT); 670 xfer = kmem_alloc(sizeof(struct uhci_xfer), KM_SLEEP);
658 } 671 }
659 if (xfer != NULL) { 672 if (xfer != NULL) {
660 memset(xfer, 0, sizeof (struct uhci_xfer)); 673 memset(xfer, 0, sizeof (struct uhci_xfer));
661 UXFER(xfer)->iinfo.sc = sc; 674 UXFER(xfer)->iinfo.sc = sc;
662#ifdef DIAGNOSTIC 675#ifdef DIAGNOSTIC
663 UXFER(xfer)->iinfo.isdone = 1; 676 UXFER(xfer)->iinfo.isdone = 1;
664 xfer->busy_free = XFER_BUSY; 677 xfer->busy_free = XFER_BUSY;
665#endif 678#endif
666 } 679 }
667 return (xfer); 680 return (xfer);
668} 681}
669 682
670void 683void
@@ -675,41 +688,50 @@ uhci_freex(struct usbd_bus *bus, usbd_xf @@ -675,41 +688,50 @@ uhci_freex(struct usbd_bus *bus, usbd_xf
675#ifdef DIAGNOSTIC 688#ifdef DIAGNOSTIC
676 if (xfer->busy_free != XFER_BUSY) { 689 if (xfer->busy_free != XFER_BUSY) {
677 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer, 690 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
678 xfer->busy_free); 691 xfer->busy_free);
679 } 692 }
680 xfer->busy_free = XFER_FREE; 693 xfer->busy_free = XFER_FREE;
681 if (!UXFER(xfer)->iinfo.isdone) { 694 if (!UXFER(xfer)->iinfo.isdone) {
682 printf("uhci_freex: !isdone\n"); 695 printf("uhci_freex: !isdone\n");
683 } 696 }
684#endif 697#endif
685 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next); 698 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
686} 699}
687 700
 701Static void
 702uhci_get_locks(struct usbd_bus *bus, kmutex_t **intr, kmutex_t **thread)
 703{
 704 struct uhci_softc *sc = bus->hci_private;
 705
 706 *intr = &sc->sc_intr_lock;
 707 *thread = &sc->sc_lock;
 708}
 709
 710
688/* 711/*
689 * Handle suspend/resume. 712 * Handle suspend/resume.
690 * 713 *
691 * We need to switch to polling mode here, because this routine is 714 * We need to switch to polling mode here, because this routine is
692 * called from an interrupt context. This is all right since we 715 * called from an interrupt context. This is all right since we
693 * are almost suspended anyway. 716 * are almost suspended anyway.
694 */ 717 */
695bool 718bool
696uhci_resume(device_t dv, const pmf_qual_t *qual) 719uhci_resume(device_t dv, const pmf_qual_t *qual)
697{ 720{
698 uhci_softc_t *sc = device_private(dv); 721 uhci_softc_t *sc = device_private(dv);
699 int cmd; 722 int cmd;
700 int s; 
701 723
702 s = splhardusb(); 724 mutex_spin_enter(&sc->sc_intr_lock);
703 725
704 cmd = UREAD2(sc, UHCI_CMD); 726 cmd = UREAD2(sc, UHCI_CMD);
705 sc->sc_bus.use_polling++; 727 sc->sc_bus.use_polling++;
706 UWRITE2(sc, UHCI_INTR, 0); 728 UWRITE2(sc, UHCI_INTR, 0);
707 uhci_globalreset(sc); 729 uhci_globalreset(sc);
708 uhci_reset(sc); 730 uhci_reset(sc);
709 if (cmd & UHCI_CMD_RS) 731 if (cmd & UHCI_CMD_RS)
710 uhci_run(sc, 0); 732 uhci_run(sc, 0);
711 733
712 /* restore saved state */ 734 /* restore saved state */
713 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); 735 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
714 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum); 736 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
715 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof); 737 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
@@ -722,65 +744,64 @@ uhci_resume(device_t dv, const pmf_qual_ @@ -722,65 +744,64 @@ uhci_resume(device_t dv, const pmf_qual_
722 UHCICMD(sc, UHCI_CMD_MAXP); 744 UHCICMD(sc, UHCI_CMD_MAXP);
723 uhci_run(sc, 1); /* and start traffic again */ 745 uhci_run(sc, 1); /* and start traffic again */
724 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY); 746 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
725 sc->sc_bus.use_polling--; 747 sc->sc_bus.use_polling--;
726 if (sc->sc_intr_xfer != NULL) 748 if (sc->sc_intr_xfer != NULL)
727 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, 749 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub,
728 sc->sc_intr_xfer); 750 sc->sc_intr_xfer);
729#ifdef UHCI_DEBUG 751#ifdef UHCI_DEBUG
730 if (uhcidebug > 2) 752 if (uhcidebug > 2)
731 uhci_dumpregs(sc); 753 uhci_dumpregs(sc);
732#endif 754#endif
733 755
734 sc->sc_suspend = PWR_RESUME; 756 sc->sc_suspend = PWR_RESUME;
735 splx(s); 757 mutex_spin_exit(&sc->sc_intr_lock);
736 758
737 return true; 759 return true;
738} 760}
739 761
740bool 762bool
741uhci_suspend(device_t dv, const pmf_qual_t *qual) 763uhci_suspend(device_t dv, const pmf_qual_t *qual)
742{ 764{
743 uhci_softc_t *sc = device_private(dv); 765 uhci_softc_t *sc = device_private(dv);
744 int cmd; 766 int cmd;
745 int s; 
746 767
747 s = splhardusb(); 768 mutex_spin_enter(&sc->sc_intr_lock);
748 769
749 cmd = UREAD2(sc, UHCI_CMD); 770 cmd = UREAD2(sc, UHCI_CMD);
750 771
751#ifdef UHCI_DEBUG 772#ifdef UHCI_DEBUG
752 if (uhcidebug > 2) 773 if (uhcidebug > 2)
753 uhci_dumpregs(sc); 774 uhci_dumpregs(sc);
754#endif 775#endif
755 if (sc->sc_intr_xfer != NULL) 776 if (sc->sc_intr_xfer != NULL)
756 callout_stop(&sc->sc_poll_handle); 777 callout_stop(&sc->sc_poll_handle);
757 sc->sc_suspend = PWR_SUSPEND; 778 sc->sc_suspend = PWR_SUSPEND;
758 sc->sc_bus.use_polling++; 779 sc->sc_bus.use_polling++;
759 780
760 uhci_run(sc, 0); /* stop the controller */ 781 uhci_run(sc, 0); /* stop the controller */
761 cmd &= ~UHCI_CMD_RS; 782 cmd &= ~UHCI_CMD_RS;
762 783
763 /* save some state if BIOS doesn't */ 784 /* save some state if BIOS doesn't */
764 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM); 785 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
765 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF); 786 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
766 787
767 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */ 788 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
768 789
769 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter suspend */ 790 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter suspend */
770 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT); 791 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
771 sc->sc_bus.use_polling--; 792 sc->sc_bus.use_polling--;
772 793
773 splx(s); 794 mutex_spin_exit(&sc->sc_intr_lock);
774 795
775 return true; 796 return true;
776} 797}
777 798
778#ifdef UHCI_DEBUG 799#ifdef UHCI_DEBUG
779Static void 800Static void
780uhci_dumpregs(uhci_softc_t *sc) 801uhci_dumpregs(uhci_softc_t *sc)
781{ 802{
782 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, " 803 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
783 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n", 804 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
784 device_xname(sc->sc_dev), 805 device_xname(sc->sc_dev),
785 UREAD2(sc, UHCI_CMD), 806 UREAD2(sc, UHCI_CMD),
786 UREAD2(sc, UHCI_STS), 807 UREAD2(sc, UHCI_STS),
@@ -982,53 +1003,52 @@ void iidump(void) { uhci_dump_iis(thesc) @@ -982,53 +1003,52 @@ void iidump(void) { uhci_dump_iis(thesc)
982 1003
983#endif 1004#endif
984 1005
985/* 1006/*
986 * This routine is executed periodically and simulates interrupts 1007 * This routine is executed periodically and simulates interrupts
987 * from the root controller interrupt pipe for port status change. 1008 * from the root controller interrupt pipe for port status change.
988 */ 1009 */
989void 1010void
990uhci_poll_hub(void *addr) 1011uhci_poll_hub(void *addr)
991{ 1012{
992 usbd_xfer_handle xfer = addr; 1013 usbd_xfer_handle xfer = addr;
993 usbd_pipe_handle pipe = xfer->pipe; 1014 usbd_pipe_handle pipe = xfer->pipe;
994 uhci_softc_t *sc; 1015 uhci_softc_t *sc;
995 int s; 
996 u_char *p; 1016 u_char *p;
997 1017
998 DPRINTFN(20, ("uhci_poll_hub\n")); 1018 DPRINTFN(20, ("uhci_poll_hub\n"));
999 1019
1000 if (__predict_false(pipe->device == NULL || pipe->device->bus == NULL)) 1020 if (__predict_false(pipe->device == NULL || pipe->device->bus == NULL))
1001 return; /* device has detached */ 1021 return; /* device has detached */
1002 sc = pipe->device->bus->hci_private; 1022 sc = pipe->device->bus->hci_private;
1003 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer); 1023 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
1004 1024
1005 p = KERNADDR(&xfer->dmabuf, 0); 1025 p = KERNADDR(&xfer->dmabuf, 0);
1006 p[0] = 0; 1026 p[0] = 0;
1007 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC)) 1027 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
1008 p[0] |= 1<<1; 1028 p[0] |= 1<<1;
1009 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC)) 1029 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
1010 p[0] |= 1<<2; 1030 p[0] |= 1<<2;
1011 if (p[0] == 0) 1031 if (p[0] == 0)
1012 /* No change, try again in a while */ 1032 /* No change, try again in a while */
1013 return; 1033 return;
1014 1034
1015 xfer->actlen = 1; 1035 xfer->actlen = 1;
1016 xfer->status = USBD_NORMAL_COMPLETION; 1036 xfer->status = USBD_NORMAL_COMPLETION;
1017 s = splusb(); 1037 mutex_enter(&sc->sc_lock);
1018 xfer->device->bus->intr_context++; 1038 xfer->device->bus->intr_context++;
1019 usb_transfer_complete(xfer); 1039 usb_transfer_complete(xfer);
1020 xfer->device->bus->intr_context--; 1040 xfer->device->bus->intr_context--;
1021 splx(s); 1041 mutex_exit(&sc->sc_lock);
1022} 1042}
1023 1043
1024void 1044void
1025uhci_root_intr_done(usbd_xfer_handle xfer) 1045uhci_root_intr_done(usbd_xfer_handle xfer)
1026{ 1046{
1027} 1047}
1028 1048
1029void 1049void
1030uhci_root_ctrl_done(usbd_xfer_handle xfer) 1050uhci_root_ctrl_done(usbd_xfer_handle xfer)
1031{ 1051{
1032} 1052}
1033 1053
1034/* 1054/*
@@ -1259,53 +1279,62 @@ uhci_remove_bulk(uhci_softc_t *sc, uhci_ @@ -1259,53 +1279,62 @@ uhci_remove_bulk(uhci_softc_t *sc, uhci_
1259 usb_syncmem(&pqh->dma, pqh->offs + offsetof(uhci_qh_t, qh_hlink), 1279 usb_syncmem(&pqh->dma, pqh->offs + offsetof(uhci_qh_t, qh_hlink),
1260 sizeof(pqh->qh.qh_hlink), BUS_DMASYNC_PREWRITE); 1280 sizeof(pqh->qh.qh_hlink), BUS_DMASYNC_PREWRITE);
1261 delay(UHCI_QH_REMOVE_DELAY); 1281 delay(UHCI_QH_REMOVE_DELAY);
1262 if (sc->sc_bulk_end == sqh) 1282 if (sc->sc_bulk_end == sqh)
1263 sc->sc_bulk_end = pqh; 1283 sc->sc_bulk_end = pqh;
1264} 1284}
1265 1285
1266Static int uhci_intr1(uhci_softc_t *); 1286Static int uhci_intr1(uhci_softc_t *);
1267 1287
1268int 1288int
1269uhci_intr(void *arg) 1289uhci_intr(void *arg)
1270{ 1290{
1271 uhci_softc_t *sc = arg; 1291 uhci_softc_t *sc = arg;
 1292 int ret = 0;
 1293
 1294 mutex_spin_enter(&sc->sc_intr_lock);
1272 1295
1273 if (sc->sc_dying || !device_has_power(sc->sc_dev)) 1296 if (sc->sc_dying || !device_has_power(sc->sc_dev))
1274 return (0); 1297 goto done;
1275 1298
1276 if (sc->sc_bus.use_polling || UREAD2(sc, UHCI_INTR) == 0) { 1299 if (sc->sc_bus.use_polling || UREAD2(sc, UHCI_INTR) == 0) {
1277#ifdef DIAGNOSTIC 1300#ifdef DIAGNOSTIC
1278 DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n")); 1301 DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n"));
1279#endif 1302#endif
1280 return (0); 1303 goto done;
1281 } 1304 }
1282 1305
1283 return (uhci_intr1(sc)); 1306 ret = uhci_intr1(sc);
 1307
 1308 done:
 1309 mutex_spin_exit(&sc->sc_intr_lock);
 1310 return ret;
1284} 1311}
1285 1312
1286int 1313int
1287uhci_intr1(uhci_softc_t *sc) 1314uhci_intr1(uhci_softc_t *sc)
1288{ 1315{
1289 int status; 1316 int status;
1290 int ack; 1317 int ack;
1291 1318
1292#ifdef UHCI_DEBUG 1319#ifdef UHCI_DEBUG
1293 if (uhcidebug > 15) { 1320 if (uhcidebug > 15) {
1294 DPRINTF(("%s: uhci_intr1\n", device_xname(sc->sc_dev))); 1321 DPRINTF(("%s: uhci_intr1\n", device_xname(sc->sc_dev)));
1295 uhci_dumpregs(sc); 1322 uhci_dumpregs(sc);
1296 } 1323 }
1297#endif 1324#endif
1298 1325
 1326 KASSERT(mutex_owned(&sc->sc_lock));
 1327
1299 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS; 1328 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1300 if (status == 0) /* The interrupt was not for us. */ 1329 if (status == 0) /* The interrupt was not for us. */
1301 return (0); 1330 return (0);
1302 1331
1303 if (sc->sc_suspend != PWR_RESUME) { 1332 if (sc->sc_suspend != PWR_RESUME) {
1304#ifdef DIAGNOSTIC 1333#ifdef DIAGNOSTIC
1305 printf("%s: interrupt while not operating ignored\n", 1334 printf("%s: interrupt while not operating ignored\n",
1306 device_xname(sc->sc_dev)); 1335 device_xname(sc->sc_dev));
1307#endif 1336#endif
1308 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */ 1337 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1309 return (0); 1338 return (0);
1310 } 1339 }
1311 1340
@@ -1355,52 +1384,54 @@ uhci_intr1(uhci_softc_t *sc) @@ -1355,52 +1384,54 @@ uhci_intr1(uhci_softc_t *sc)
1355 return (1); 1384 return (1);
1356} 1385}
1357 1386
1358void 1387void
1359uhci_softintr(void *v) 1388uhci_softintr(void *v)
1360{ 1389{
1361 struct usbd_bus *bus = v; 1390 struct usbd_bus *bus = v;
1362 uhci_softc_t *sc = bus->hci_private; 1391 uhci_softc_t *sc = bus->hci_private;
1363 uhci_intr_info_t *ii, *nextii; 1392 uhci_intr_info_t *ii, *nextii;
1364 1393
1365 DPRINTFN(10,("%s: uhci_softintr (%d)\n", device_xname(sc->sc_dev), 1394 DPRINTFN(10,("%s: uhci_softintr (%d)\n", device_xname(sc->sc_dev),
1366 sc->sc_bus.intr_context)); 1395 sc->sc_bus.intr_context));
1367 1396
 1397 mutex_enter(&sc->sc_lock);
 1398
1368 sc->sc_bus.intr_context++; 1399 sc->sc_bus.intr_context++;
1369 1400
1370 /* 1401 /*
1371 * Interrupts on UHCI really suck. When the host controller 1402 * Interrupts on UHCI really suck. When the host controller
1372 * interrupts because a transfer is completed there is no 1403 * interrupts because a transfer is completed there is no
1373 * way of knowing which transfer it was. You can scan down 1404 * way of knowing which transfer it was. You can scan down
1374 * the TDs and QHs of the previous frame to limit the search, 1405 * the TDs and QHs of the previous frame to limit the search,
1375 * but that assumes that the interrupt was not delayed by more 1406 * but that assumes that the interrupt was not delayed by more
1376 * than 1 ms, which may not always be true (e.g. after debug 1407 * than 1 ms, which may not always be true (e.g. after debug
1377 * output on a slow console). 1408 * output on a slow console).
1378 * We scan all interrupt descriptors to see if any have 1409 * We scan all interrupt descriptors to see if any have
1379 * completed. 1410 * completed.
1380 */ 1411 */
1381 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = nextii) { 1412 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = nextii) {
1382 nextii = LIST_NEXT(ii, list); 1413 nextii = LIST_NEXT(ii, list);
1383 uhci_check_intr(sc, ii); 1414 uhci_check_intr(sc, ii);
1384 } 1415 }
1385 1416
1386#ifdef USB_USE_SOFTINTR 
1387 if (sc->sc_softwake) { 1417 if (sc->sc_softwake) {
1388 sc->sc_softwake = 0; 1418 sc->sc_softwake = 0;
1389 wakeup(&sc->sc_softwake); 1419 cv_broadcast(&sc->sc_softwake_cv);
1390 } 1420 }
1391#endif /* USB_USE_SOFTINTR */ 
1392 1421
1393 sc->sc_bus.intr_context--; 1422 sc->sc_bus.intr_context--;
 1423
 1424 mutex_exit(&sc->sc_lock);
1394} 1425}
1395 1426
1396/* Check for an interrupt. */ 1427/* Check for an interrupt. */
1397void 1428void
1398uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii) 1429uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1399{ 1430{
1400 uhci_soft_td_t *std, *lstd; 1431 uhci_soft_td_t *std, *lstd;
1401 u_int32_t status; 1432 u_int32_t status;
1402 1433
1403 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii)); 1434 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1404#ifdef DIAGNOSTIC 1435#ifdef DIAGNOSTIC
1405 if (ii == NULL) { 1436 if (ii == NULL) {
1406 printf("uhci_check_intr: no ii? %p\n", ii); 1437 printf("uhci_check_intr: no ii? %p\n", ii);
@@ -1475,26 +1506,27 @@ uhci_check_intr(uhci_softc_t *sc, uhci_i @@ -1475,26 +1506,27 @@ uhci_check_intr(uhci_softc_t *sc, uhci_i
1475/* Called at splusb() */ 1506/* Called at splusb() */
1476void 1507void
1477uhci_idone(uhci_intr_info_t *ii) 1508uhci_idone(uhci_intr_info_t *ii)
1478{ 1509{
1479 usbd_xfer_handle xfer = ii->xfer; 1510 usbd_xfer_handle xfer = ii->xfer;
1480 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 1511 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1481 uhci_soft_td_t *std; 1512 uhci_soft_td_t *std;
1482 u_int32_t status = 0, nstatus; 1513 u_int32_t status = 0, nstatus;
1483 int actlen; 1514 int actlen;
1484 1515
1485 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii)); 1516 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1486#ifdef DIAGNOSTIC 1517#ifdef DIAGNOSTIC
1487 { 1518 {
 1519 /* XXX SMP? */
1488 int s = splhigh(); 1520 int s = splhigh();
1489 if (ii->isdone) { 1521 if (ii->isdone) {
1490 splx(s); 1522 splx(s);
1491#ifdef UHCI_DEBUG 1523#ifdef UHCI_DEBUG
1492 printf("uhci_idone: ii is done!\n "); 1524 printf("uhci_idone: ii is done!\n ");
1493 uhci_dump_ii(ii); 1525 uhci_dump_ii(ii);
1494#else 1526#else
1495 printf("uhci_idone: ii=%p is done!\n", ii); 1527 printf("uhci_idone: ii=%p is done!\n", ii);
1496#endif 1528#endif
1497 return; 1529 return;
1498 } 1530 }
1499 ii->isdone = 1; 1531 ii->isdone = 1;
1500 splx(s); 1532 splx(s);
@@ -1621,124 +1653,131 @@ uhci_timeout(void *addr) @@ -1621,124 +1653,131 @@ uhci_timeout(void *addr)
1621 return; 1653 return;
1622 } 1654 }
1623 1655
1624 /* Execute the abort in a process context. */ 1656 /* Execute the abort in a process context. */
1625 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer); 1657 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer);
1626 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task, 1658 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task,
1627 USB_TASKQ_HC); 1659 USB_TASKQ_HC);
1628} 1660}
1629 1661
1630void 1662void
1631uhci_timeout_task(void *addr) 1663uhci_timeout_task(void *addr)
1632{ 1664{
1633 usbd_xfer_handle xfer = addr; 1665 usbd_xfer_handle xfer = addr;
1634 int s; 
1635 1666
1636 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer)); 1667 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1637 1668
1638 s = splusb(); 
1639 uhci_abort_xfer(xfer, USBD_TIMEOUT); 1669 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1640 splx(s); 
1641} 1670}
1642 1671
1643/* 1672/*
1644 * Wait here until controller claims to have an interrupt. 1673 * Wait here until controller claims to have an interrupt.
1645 * Then call uhci_intr and return. Use timeout to avoid waiting 1674 * Then call uhci_intr and return. Use timeout to avoid waiting
1646 * too long. 1675 * too long.
1647 * Only used during boot when interrupts are not enabled yet. 1676 * Only used during boot when interrupts are not enabled yet.
1648 */ 1677 */
1649void 1678void
1650uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer) 1679uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1651{ 1680{
1652 int timo = xfer->timeout; 1681 int timo = xfer->timeout;
1653 uhci_intr_info_t *ii; 1682 uhci_intr_info_t *ii;
1654 1683
 1684 mutex_enter(&sc->sc_lock);
 1685
1655 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo)); 1686 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1656 1687
1657 xfer->status = USBD_IN_PROGRESS; 1688 xfer->status = USBD_IN_PROGRESS;
1658 for (; timo >= 0; timo--) { 1689 for (; timo >= 0; timo--) {
1659 usb_delay_ms(&sc->sc_bus, 1); 1690 usb_delay_ms(&sc->sc_bus, 1);
1660 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS))); 1691 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1661 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) { 1692 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
 1693 mutex_spin_enter(&sc->sc_intr_lock);
1662 uhci_intr1(sc); 1694 uhci_intr1(sc);
 1695 mutex_spin_exit(&sc->sc_intr_lock);
1663 if (xfer->status != USBD_IN_PROGRESS) 1696 if (xfer->status != USBD_IN_PROGRESS)
1664 return; 1697 goto done;
1665 } 1698 }
1666 } 1699 }
1667 1700
1668 /* Timeout */ 1701 /* Timeout */
1669 DPRINTF(("uhci_waitintr: timeout\n")); 1702 DPRINTF(("uhci_waitintr: timeout\n"));
1670 for (ii = LIST_FIRST(&sc->sc_intrhead); 1703 for (ii = LIST_FIRST(&sc->sc_intrhead);
1671 ii != NULL && ii->xfer != xfer; 1704 ii != NULL && ii->xfer != xfer;
1672 ii = LIST_NEXT(ii, list)) 1705 ii = LIST_NEXT(ii, list))
1673 ; 1706 ;
1674#ifdef DIAGNOSTIC 1707#ifdef DIAGNOSTIC
1675 if (ii == NULL) 1708 if (ii == NULL)
1676 panic("uhci_waitintr: lost intr_info"); 1709 panic("uhci_waitintr: lost intr_info");
1677#endif 1710#endif
1678 uhci_idone(ii); 1711 uhci_idone(ii);
 1712
 1713done:
 1714 mutex_exit(&sc->sc_lock);
1679} 1715}
1680 1716
1681void 1717void
1682uhci_poll(struct usbd_bus *bus) 1718uhci_poll(struct usbd_bus *bus)
1683{ 1719{
1684 uhci_softc_t *sc = bus->hci_private; 1720 uhci_softc_t *sc = bus->hci_private;
1685 1721
1686 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) 1722 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
 1723 mutex_spin_enter(&sc->sc_intr_lock);
1687 uhci_intr1(sc); 1724 uhci_intr1(sc);
 1725 mutex_spin_exit(&sc->sc_intr_lock);
 1726 }
1688} 1727}
1689 1728
1690void 1729void
1691uhci_reset(uhci_softc_t *sc) 1730uhci_reset(uhci_softc_t *sc)
1692{ 1731{
1693 int n; 1732 int n;
1694 1733
1695 UHCICMD(sc, UHCI_CMD_HCRESET); 1734 UHCICMD(sc, UHCI_CMD_HCRESET);
1696 /* The reset bit goes low when the controller is done. */ 1735 /* The reset bit goes low when the controller is done. */
1697 for (n = 0; n < UHCI_RESET_TIMEOUT && 1736 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1698 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++) 1737 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1699 usb_delay_ms(&sc->sc_bus, 1); 1738 usb_delay_ms(&sc->sc_bus, 1);
1700 if (n >= UHCI_RESET_TIMEOUT) 1739 if (n >= UHCI_RESET_TIMEOUT)
1701 printf("%s: controller did not reset\n", 1740 printf("%s: controller did not reset\n",
1702 device_xname(sc->sc_dev)); 1741 device_xname(sc->sc_dev));
1703} 1742}
1704 1743
1705usbd_status 1744usbd_status
1706uhci_run(uhci_softc_t *sc, int run) 1745uhci_run(uhci_softc_t *sc, int run)
1707{ 1746{
1708 int s, n, running; 1747 int n, running;
1709 u_int16_t cmd; 1748 u_int16_t cmd;
1710 1749
1711 run = run != 0; 1750 run = run != 0;
1712 s = splhardusb(); 1751 mutex_spin_enter(&sc->sc_intr_lock);
1713 DPRINTF(("uhci_run: setting run=%d\n", run)); 1752 DPRINTF(("uhci_run: setting run=%d\n", run));
1714 cmd = UREAD2(sc, UHCI_CMD); 1753 cmd = UREAD2(sc, UHCI_CMD);
1715 if (run) 1754 if (run)
1716 cmd |= UHCI_CMD_RS; 1755 cmd |= UHCI_CMD_RS;
1717 else 1756 else
1718 cmd &= ~UHCI_CMD_RS; 1757 cmd &= ~UHCI_CMD_RS;
1719 UHCICMD(sc, cmd); 1758 UHCICMD(sc, cmd);
1720 for(n = 0; n < 10; n++) { 1759 for(n = 0; n < 10; n++) {
1721 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH); 1760 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1722 /* return when we've entered the state we want */ 1761 /* return when we've entered the state we want */
1723 if (run == running) { 1762 if (run == running) {
1724 splx(s); 1763 mutex_spin_exit(&sc->sc_intr_lock);
1725 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n", 1764 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1726 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS))); 1765 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1727 return (USBD_NORMAL_COMPLETION); 1766 return (USBD_NORMAL_COMPLETION);
1728 } 1767 }
1729 usb_delay_ms(&sc->sc_bus, 1); 1768 usb_delay_ms(&sc->sc_bus, 1);
1730 } 1769 }
1731 splx(s); 1770 mutex_spin_exit(&sc->sc_intr_lock);
1732 printf("%s: cannot %s\n", device_xname(sc->sc_dev), 1771 printf("%s: cannot %s\n", device_xname(sc->sc_dev),
1733 run ? "start" : "stop"); 1772 run ? "start" : "stop");
1734 return (USBD_IOERROR); 1773 return (USBD_IOERROR);
1735} 1774}
1736 1775
1737/* 1776/*
1738 * Memory management routines. 1777 * Memory management routines.
1739 * uhci_alloc_std allocates TDs 1778 * uhci_alloc_std allocates TDs
1740 * uhci_alloc_sqh allocates QHs 1779 * uhci_alloc_sqh allocates QHs
1741 * These two routines do their own free list management, 1780 * These two routines do their own free list management,
1742 * partly for speed, partly because allocating DMAable memory 1781 * partly for speed, partly because allocating DMAable memory
1743 * has page size granularaity so much memory would be wasted if 1782 * has page size granularaity so much memory would be wasted if
1744 * only one TD/QH (32 bytes) was placed in each allocated chunk. 1783 * only one TD/QH (32 bytes) was placed in each allocated chunk.
@@ -1748,27 +1787,27 @@ uhci_soft_td_t * @@ -1748,27 +1787,27 @@ uhci_soft_td_t *
1748uhci_alloc_std(uhci_softc_t *sc) 1787uhci_alloc_std(uhci_softc_t *sc)
1749{ 1788{
1750 uhci_soft_td_t *std; 1789 uhci_soft_td_t *std;
1751 usbd_status err; 1790 usbd_status err;
1752 int i, offs; 1791 int i, offs;
1753 usb_dma_t dma; 1792 usb_dma_t dma;
1754 1793
1755 if (sc->sc_freetds == NULL) { 1794 if (sc->sc_freetds == NULL) {
1756 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n")); 1795 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1757 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK, 1796 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1758 UHCI_TD_ALIGN, &dma); 1797 UHCI_TD_ALIGN, &dma);
1759 if (err) 1798 if (err)
1760 return (0); 1799 return (0);
1761 for(i = 0; i < UHCI_STD_CHUNK; i++) { 1800 for (i = 0; i < UHCI_STD_CHUNK; i++) {
1762 offs = i * UHCI_STD_SIZE; 1801 offs = i * UHCI_STD_SIZE;
1763 std = KERNADDR(&dma, offs); 1802 std = KERNADDR(&dma, offs);
1764 std->physaddr = DMAADDR(&dma, offs); 1803 std->physaddr = DMAADDR(&dma, offs);
1765 std->dma = dma; 1804 std->dma = dma;
1766 std->offs = offs; 1805 std->offs = offs;
1767 std->link.std = sc->sc_freetds; 1806 std->link.std = sc->sc_freetds;
1768 sc->sc_freetds = std; 1807 sc->sc_freetds = std;
1769 } 1808 }
1770 } 1809 }
1771 std = sc->sc_freetds; 1810 std = sc->sc_freetds;
1772 sc->sc_freetds = std->link.std; 1811 sc->sc_freetds = std->link.std;
1773 memset(&std->td, 0, sizeof(uhci_td_t)); 1812 memset(&std->td, 0, sizeof(uhci_td_t));
1774 return std; 1813 return std;
@@ -1863,26 +1902,29 @@ uhci_alloc_std_chain(struct uhci_pipe *u @@ -1863,26 +1902,29 @@ uhci_alloc_std_chain(struct uhci_pipe *u
1863 int rd, u_int16_t flags, usb_dma_t *dma, 1902 int rd, u_int16_t flags, usb_dma_t *dma,
1864 uhci_soft_td_t **sp, uhci_soft_td_t **ep) 1903 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1865{ 1904{
1866 uhci_soft_td_t *p, *lastp; 1905 uhci_soft_td_t *p, *lastp;
1867 uhci_physaddr_t lastlink; 1906 uhci_physaddr_t lastlink;
1868 int i, ntd, l, tog, maxp; 1907 int i, ntd, l, tog, maxp;
1869 u_int32_t status; 1908 u_int32_t status;
1870 int addr = upipe->pipe.device->address; 1909 int addr = upipe->pipe.device->address;
1871 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 1910 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1872 1911
1873 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d " 1912 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1874 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len, 1913 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1875 upipe->pipe.device->speed, flags)); 1914 upipe->pipe.device->speed, flags));
 1915
 1916 KASSERT(mutex_owned(&sc->sc_lock));
 1917
1876 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize); 1918 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1877 if (maxp == 0) { 1919 if (maxp == 0) {
1878 printf("uhci_alloc_std_chain: maxp=0\n"); 1920 printf("uhci_alloc_std_chain: maxp=0\n");
1879 return (USBD_INVAL); 1921 return (USBD_INVAL);
1880 } 1922 }
1881 ntd = (len + maxp - 1) / maxp; 1923 ntd = (len + maxp - 1) / maxp;
1882 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0) 1924 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1883 ntd++; 1925 ntd++;
1884 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd)); 1926 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1885 if (ntd == 0) { 1927 if (ntd == 0) {
1886 *sp = *ep = 0; 1928 *sp = *ep = 0;
1887 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n")); 1929 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1888 return (USBD_NORMAL_COMPLETION); 1930 return (USBD_NORMAL_COMPLETION);
@@ -1940,76 +1982,82 @@ uhci_device_clear_toggle(usbd_pipe_handl @@ -1940,76 +1982,82 @@ uhci_device_clear_toggle(usbd_pipe_handl
1940{ 1982{
1941 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 1983 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1942 upipe->nexttoggle = 0; 1984 upipe->nexttoggle = 0;
1943} 1985}
1944 1986
1945void 1987void
1946uhci_noop(usbd_pipe_handle pipe) 1988uhci_noop(usbd_pipe_handle pipe)
1947{ 1989{
1948} 1990}
1949 1991
1950usbd_status 1992usbd_status
1951uhci_device_bulk_transfer(usbd_xfer_handle xfer) 1993uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1952{ 1994{
 1995 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
1953 usbd_status err; 1996 usbd_status err;
1954 1997
1955 /* Insert last in queue. */ 1998 /* Insert last in queue. */
 1999 mutex_enter(&sc->sc_lock);
1956 err = usb_insert_transfer(xfer); 2000 err = usb_insert_transfer(xfer);
 2001 mutex_exit(&sc->sc_lock);
1957 if (err) 2002 if (err)
1958 return (err); 2003 return (err);
1959 2004
1960 /* 2005 /*
1961 * Pipe isn't running (otherwise err would be USBD_INPROG), 2006 * Pipe isn't running (otherwise err would be USBD_INPROG),
1962 * so start it first. 2007 * so start it first.
1963 */ 2008 */
1964 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2009 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1965} 2010}
1966 2011
1967usbd_status 2012usbd_status
1968uhci_device_bulk_start(usbd_xfer_handle xfer) 2013uhci_device_bulk_start(usbd_xfer_handle xfer)
1969{ 2014{
1970 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2015 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1971 usbd_device_handle dev = upipe->pipe.device; 2016 usbd_device_handle dev = upipe->pipe.device;
1972 uhci_softc_t *sc = dev->bus->hci_private; 2017 uhci_softc_t *sc = dev->bus->hci_private;
1973 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2018 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1974 uhci_soft_td_t *data, *dataend; 2019 uhci_soft_td_t *data, *dataend;
1975 uhci_soft_qh_t *sqh; 2020 uhci_soft_qh_t *sqh;
1976 usbd_status err; 2021 usbd_status err;
1977 int len, isread, endpt; 2022 int len, isread, endpt;
1978 int s; 
1979 2023
1980 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n", 2024 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1981 xfer, xfer->length, xfer->flags, ii)); 2025 xfer, xfer->length, xfer->flags, ii));
1982 2026
1983 if (sc->sc_dying) 2027 if (sc->sc_dying)
1984 return (USBD_IOERROR); 2028 return (USBD_IOERROR);
1985 2029
1986#ifdef DIAGNOSTIC 2030#ifdef DIAGNOSTIC
1987 if (xfer->rqflags & URQ_REQUEST) 2031 if (xfer->rqflags & URQ_REQUEST)
1988 panic("uhci_device_bulk_transfer: a request"); 2032 panic("uhci_device_bulk_transfer: a request");
1989#endif 2033#endif
1990 2034
 2035 mutex_enter(&sc->sc_lock);
 2036
1991 len = xfer->length; 2037 len = xfer->length;
1992 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 2038 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1993 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 2039 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1994 sqh = upipe->u.bulk.sqh; 2040 sqh = upipe->u.bulk.sqh;
1995 2041
1996 upipe->u.bulk.isread = isread; 2042 upipe->u.bulk.isread = isread;
1997 upipe->u.bulk.length = len; 2043 upipe->u.bulk.length = len;
1998 2044
1999 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags, 2045 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2000 &xfer->dmabuf, &data, &dataend); 2046 &xfer->dmabuf, &data, &dataend);
2001 if (err) 2047 if (err) {
 2048 mutex_exit(&sc->sc_lock);
2002 return (err); 2049 return (err);
 2050 }
2003 dataend->td.td_status |= htole32(UHCI_TD_IOC); 2051 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2004 usb_syncmem(&dataend->dma, 2052 usb_syncmem(&dataend->dma,
2005 dataend->offs + offsetof(uhci_td_t, td_status), 2053 dataend->offs + offsetof(uhci_td_t, td_status),
2006 sizeof(dataend->td.td_status), 2054 sizeof(dataend->td.td_status),
2007 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2055 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2008 2056
2009 2057
2010#ifdef UHCI_DEBUG 2058#ifdef UHCI_DEBUG
2011 if (uhcidebug > 8) { 2059 if (uhcidebug > 8) {
2012 DPRINTF(("uhci_device_bulk_transfer: data(1)\n")); 2060 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
2013 uhci_dump_tds(data); 2061 uhci_dump_tds(data);
2014 } 2062 }
2015#endif 2063#endif
@@ -2019,47 +2067,46 @@ uhci_device_bulk_start(usbd_xfer_handle  @@ -2019,47 +2067,46 @@ uhci_device_bulk_start(usbd_xfer_handle
2019 ii->stdstart = data; 2067 ii->stdstart = data;
2020 ii->stdend = dataend; 2068 ii->stdend = dataend;
2021#ifdef DIAGNOSTIC 2069#ifdef DIAGNOSTIC
2022 if (!ii->isdone) { 2070 if (!ii->isdone) {
2023 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii); 2071 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
2024 } 2072 }
2025 ii->isdone = 0; 2073 ii->isdone = 0;
2026#endif 2074#endif
2027 2075
2028 sqh->elink = data; 2076 sqh->elink = data;
2029 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD); 2077 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2030 /* uhci_add_bulk() will do usb_syncmem(sqh) */ 2078 /* uhci_add_bulk() will do usb_syncmem(sqh) */
2031 2079
2032 s = splusb(); 
2033 uhci_add_bulk(sc, sqh); 2080 uhci_add_bulk(sc, sqh);
2034 uhci_add_intr_info(sc, ii); 2081 uhci_add_intr_info(sc, ii);
2035 2082
2036 if (xfer->timeout && !sc->sc_bus.use_polling) { 2083 if (xfer->timeout && !sc->sc_bus.use_polling) {
2037 callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout), 2084 callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
2038 uhci_timeout, ii); 2085 uhci_timeout, ii);
2039 } 2086 }
2040 xfer->status = USBD_IN_PROGRESS; 2087 xfer->status = USBD_IN_PROGRESS;
2041 splx(s); 
2042 2088
2043#ifdef UHCI_DEBUG 2089#ifdef UHCI_DEBUG
2044 if (uhcidebug > 10) { 2090 if (uhcidebug > 10) {
2045 DPRINTF(("uhci_device_bulk_transfer: data(2)\n")); 2091 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
2046 uhci_dump_tds(data); 2092 uhci_dump_tds(data);
2047 } 2093 }
2048#endif 2094#endif
2049 2095
2050 if (sc->sc_bus.use_polling) 2096 if (sc->sc_bus.use_polling)
2051 uhci_waitintr(sc, xfer); 2097 uhci_waitintr(sc, xfer);
2052 2098
 2099 mutex_exit(&sc->sc_lock);
2053 return (USBD_IN_PROGRESS); 2100 return (USBD_IN_PROGRESS);
2054} 2101}
2055 2102
2056/* Abort a device bulk request. */ 2103/* Abort a device bulk request. */
2057void 2104void
2058uhci_device_bulk_abort(usbd_xfer_handle xfer) 2105uhci_device_bulk_abort(usbd_xfer_handle xfer)
2059{ 2106{
2060 DPRINTF(("uhci_device_bulk_abort:\n")); 2107 DPRINTF(("uhci_device_bulk_abort:\n"));
2061 uhci_abort_xfer(xfer, USBD_CANCELLED); 2108 uhci_abort_xfer(xfer, USBD_CANCELLED);
2062} 2109}
2063 2110
2064/* 2111/*
2065 * Abort a device request. 2112 * Abort a device request.
@@ -2068,199 +2115,200 @@ uhci_device_bulk_abort(usbd_xfer_handle  @@ -2068,199 +2115,200 @@ uhci_device_bulk_abort(usbd_xfer_handle
2068 * for it will be called with USBD_CANCELLED status. 2115 * for it will be called with USBD_CANCELLED status.
2069 * It's impossible to guarantee that the requested transfer will not 2116 * It's impossible to guarantee that the requested transfer will not
2070 * have happened since the hardware runs concurrently. 2117 * have happened since the hardware runs concurrently.
2071 * If the transaction has already happened we rely on the ordinary 2118 * If the transaction has already happened we rely on the ordinary
2072 * interrupt processing to process it. 2119 * interrupt processing to process it.
2073 */ 2120 */
2074void 2121void
2075uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status) 2122uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2076{ 2123{
2077 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2124 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2078 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2125 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2079 uhci_softc_t *sc = upipe->pipe.device->bus->hci_private; 2126 uhci_softc_t *sc = upipe->pipe.device->bus->hci_private;
2080 uhci_soft_td_t *std; 2127 uhci_soft_td_t *std;
2081 int s; 
2082 int wake; 2128 int wake;
2083 2129
2084 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status)); 2130 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
2085 2131
2086 if (sc->sc_dying) { 2132 if (sc->sc_dying) {
2087 /* If we're dying, just do the software part. */ 2133 /* If we're dying, just do the software part. */
2088 s = splusb(); 2134 mutex_enter(&sc->sc_lock);
2089 xfer->status = status; /* make software ignore it */ 2135 xfer->status = status; /* make software ignore it */
2090 callout_stop(&xfer->timeout_handle); 2136 callout_stop(&xfer->timeout_handle);
2091 usb_transfer_complete(xfer); 2137 usb_transfer_complete(xfer);
2092 splx(s); 2138 mutex_exit(&sc->sc_lock);
2093 return; 2139 return;
2094 } 2140 }
2095 2141
2096 if (xfer->device->bus->intr_context || !curproc) 2142 if (xfer->device->bus->intr_context || !curproc)
2097 panic("uhci_abort_xfer: not in process context"); 2143 panic("uhci_abort_xfer: not in process context");
2098 2144
 2145 mutex_enter(&sc->sc_lock);
 2146
2099 /* 2147 /*
2100 * If an abort is already in progress then just wait for it to 2148 * If an abort is already in progress then just wait for it to
2101 * complete and return. 2149 * complete and return.
2102 */ 2150 */
2103 if (xfer->hcflags & UXFER_ABORTING) { 2151 if (xfer->hcflags & UXFER_ABORTING) {
2104 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n")); 2152 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n"));
2105#ifdef DIAGNOSTIC 2153#ifdef DIAGNOSTIC
2106 if (status == USBD_TIMEOUT) 2154 if (status == USBD_TIMEOUT)
2107 printf("uhci_abort_xfer: TIMEOUT while aborting\n"); 2155 printf("uhci_abort_xfer: TIMEOUT while aborting\n");
2108#endif 2156#endif
2109 /* Override the status which might be USBD_TIMEOUT. */ 2157 /* Override the status which might be USBD_TIMEOUT. */
2110 xfer->status = status; 2158 xfer->status = status;
2111 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n")); 2159 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n"));
2112 xfer->hcflags |= UXFER_ABORTWAIT; 2160 xfer->hcflags |= UXFER_ABORTWAIT;
2113 while (xfer->hcflags & UXFER_ABORTING) 2161 while (xfer->hcflags & UXFER_ABORTING)
2114 tsleep(&xfer->hcflags, PZERO, "uhciaw", 0); 2162 cv_wait(&xfer->hccv, &sc->sc_lock);
2115 return; 2163 goto done;
2116 } 2164 }
2117 xfer->hcflags |= UXFER_ABORTING; 2165 xfer->hcflags |= UXFER_ABORTING;
2118 2166
2119 /* 2167 /*
2120 * Step 1: Make interrupt routine and hardware ignore xfer. 2168 * Step 1: Make interrupt routine and hardware ignore xfer.
2121 */ 2169 */
2122 s = splusb(); 
2123 xfer->status = status; /* make software ignore it */ 2170 xfer->status = status; /* make software ignore it */
2124 callout_stop(&xfer->timeout_handle); 2171 callout_stop(&xfer->timeout_handle);
2125 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii)); 2172 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
2126 for (std = ii->stdstart; std != NULL; std = std->link.std) { 2173 for (std = ii->stdstart; std != NULL; std = std->link.std) {
2127 usb_syncmem(&std->dma, 2174 usb_syncmem(&std->dma,
2128 std->offs + offsetof(uhci_td_t, td_status), 2175 std->offs + offsetof(uhci_td_t, td_status),
2129 sizeof(std->td.td_status), 2176 sizeof(std->td.td_status),
2130 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD); 2177 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2131 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC)); 2178 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2132 usb_syncmem(&std->dma, 2179 usb_syncmem(&std->dma,
2133 std->offs + offsetof(uhci_td_t, td_status), 2180 std->offs + offsetof(uhci_td_t, td_status),
2134 sizeof(std->td.td_status), 2181 sizeof(std->td.td_status),
2135 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2182 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2136 } 2183 }
2137 splx(s); 
2138 2184
2139 /* 2185 /*
2140 * Step 2: Wait until we know hardware has finished any possible 2186 * Step 2: Wait until we know hardware has finished any possible
2141 * use of the xfer. Also make sure the soft interrupt routine 2187 * use of the xfer. Also make sure the soft interrupt routine
2142 * has run. 2188 * has run.
2143 */ 2189 */
2144 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */ 2190 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
2145 s = splusb(); 
2146#ifdef USB_USE_SOFTINTR 
2147 sc->sc_softwake = 1; 2191 sc->sc_softwake = 1;
2148#endif /* USB_USE_SOFTINTR */ 
2149 usb_schedsoftintr(&sc->sc_bus); 2192 usb_schedsoftintr(&sc->sc_bus);
2150#ifdef USB_USE_SOFTINTR 2193 DPRINTFN(1,("uhci_abort_xfer: cv_wait\n"));
2151 DPRINTFN(1,("uhci_abort_xfer: tsleep\n")); 2194 cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
2152 tsleep(&sc->sc_softwake, PZERO, "uhciab", 0); 
2153#endif /* USB_USE_SOFTINTR */ 
2154 splx(s); 
2155 2195
2156 /* 2196 /*
2157 * Step 3: Execute callback. 2197 * Step 3: Execute callback.
2158 */ 2198 */
2159 DPRINTFN(1,("uhci_abort_xfer: callback\n")); 2199 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
2160 s = splusb(); 
2161#ifdef DIAGNOSTIC 2200#ifdef DIAGNOSTIC
2162 ii->isdone = 1; 2201 ii->isdone = 1;
2163#endif 2202#endif
2164 wake = xfer->hcflags & UXFER_ABORTWAIT; 2203 wake = xfer->hcflags & UXFER_ABORTWAIT;
2165 xfer->hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT); 2204 xfer->hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
2166 usb_transfer_complete(xfer); 2205 usb_transfer_complete(xfer);
2167 if (wake) 2206 if (wake)
2168 wakeup(&xfer->hcflags); 2207 cv_broadcast(&xfer->hccv);
2169 splx(s); 2208done:
 2209 mutex_exit(&sc->sc_lock);
2170} 2210}
2171 2211
2172/* Close a device bulk pipe. */ 2212/* Close a device bulk pipe. */
2173void 2213void
2174uhci_device_bulk_close(usbd_pipe_handle pipe) 2214uhci_device_bulk_close(usbd_pipe_handle pipe)
2175{ 2215{
2176 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2216 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2177 usbd_device_handle dev = upipe->pipe.device; 2217 usbd_device_handle dev = upipe->pipe.device;
2178 uhci_softc_t *sc = dev->bus->hci_private; 2218 uhci_softc_t *sc = dev->bus->hci_private;
2179 2219
2180 uhci_free_sqh(sc, upipe->u.bulk.sqh); 2220 uhci_free_sqh(sc, upipe->u.bulk.sqh);
2181 2221
2182 pipe->endpoint->datatoggle = upipe->nexttoggle; 2222 pipe->endpoint->datatoggle = upipe->nexttoggle;
2183} 2223}
2184 2224
2185usbd_status 2225usbd_status
2186uhci_device_ctrl_transfer(usbd_xfer_handle xfer) 2226uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
2187{ 2227{
 2228 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
2188 usbd_status err; 2229 usbd_status err;
2189 2230
2190 /* Insert last in queue. */ 2231 /* Insert last in queue. */
 2232 mutex_enter(&sc->sc_lock);
2191 err = usb_insert_transfer(xfer); 2233 err = usb_insert_transfer(xfer);
 2234 mutex_exit(&sc->sc_lock);
2192 if (err) 2235 if (err)
2193 return (err); 2236 return (err);
2194 2237
2195 /* 2238 /*
2196 * Pipe isn't running (otherwise err would be USBD_INPROG), 2239 * Pipe isn't running (otherwise err would be USBD_INPROG),
2197 * so start it first. 2240 * so start it first.
2198 */ 2241 */
2199 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2242 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2200} 2243}
2201 2244
2202usbd_status 2245usbd_status
2203uhci_device_ctrl_start(usbd_xfer_handle xfer) 2246uhci_device_ctrl_start(usbd_xfer_handle xfer)
2204{ 2247{
2205 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private; 2248 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
2206 usbd_status err; 2249 usbd_status err;
2207 2250
2208 if (sc->sc_dying) 2251 if (sc->sc_dying)
2209 return (USBD_IOERROR); 2252 return (USBD_IOERROR);
2210 2253
2211#ifdef DIAGNOSTIC 2254#ifdef DIAGNOSTIC
2212 if (!(xfer->rqflags & URQ_REQUEST)) 2255 if (!(xfer->rqflags & URQ_REQUEST))
2213 panic("uhci_device_ctrl_transfer: not a request"); 2256 panic("uhci_device_ctrl_transfer: not a request");
2214#endif 2257#endif
2215 2258
 2259 mutex_enter(&sc->sc_lock);
2216 err = uhci_device_request(xfer); 2260 err = uhci_device_request(xfer);
 2261 mutex_exit(&sc->sc_lock);
2217 if (err) 2262 if (err)
2218 return (err); 2263 return (err);
2219 2264
2220 if (sc->sc_bus.use_polling) 2265 if (sc->sc_bus.use_polling)
2221 uhci_waitintr(sc, xfer); 2266 uhci_waitintr(sc, xfer);
2222 return (USBD_IN_PROGRESS); 2267 return (USBD_IN_PROGRESS);
2223} 2268}
2224 2269
2225usbd_status 2270usbd_status
2226uhci_device_intr_transfer(usbd_xfer_handle xfer) 2271uhci_device_intr_transfer(usbd_xfer_handle xfer)
2227{ 2272{
 2273 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
2228 usbd_status err; 2274 usbd_status err;
2229 2275
2230 /* Insert last in queue. */ 2276 /* Insert last in queue. */
 2277 mutex_enter(&sc->sc_lock);
2231 err = usb_insert_transfer(xfer); 2278 err = usb_insert_transfer(xfer);
 2279 mutex_exit(&sc->sc_lock);
2232 if (err) 2280 if (err)
2233 return (err); 2281 return (err);
2234 2282
2235 /* 2283 /*
2236 * Pipe isn't running (otherwise err would be USBD_INPROG), 2284 * Pipe isn't running (otherwise err would be USBD_INPROG),
2237 * so start it first. 2285 * so start it first.
2238 */ 2286 */
2239 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2287 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2240} 2288}
2241 2289
2242usbd_status 2290usbd_status
2243uhci_device_intr_start(usbd_xfer_handle xfer) 2291uhci_device_intr_start(usbd_xfer_handle xfer)
2244{ 2292{
2245 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2293 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2246 usbd_device_handle dev = upipe->pipe.device; 2294 usbd_device_handle dev = upipe->pipe.device;
2247 uhci_softc_t *sc = dev->bus->hci_private; 2295 uhci_softc_t *sc = dev->bus->hci_private;
2248 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2296 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2249 uhci_soft_td_t *data, *dataend; 2297 uhci_soft_td_t *data, *dataend;
2250 uhci_soft_qh_t *sqh; 2298 uhci_soft_qh_t *sqh;
2251 usbd_status err; 2299 usbd_status err;
2252 int isread, endpt; 2300 int isread, endpt;
2253 int i, s; 2301 int i;
2254 2302
2255 if (sc->sc_dying) 2303 if (sc->sc_dying)
2256 return (USBD_IOERROR); 2304 return (USBD_IOERROR);
2257 2305
2258 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n", 2306 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2259 xfer, xfer->length, xfer->flags)); 2307 xfer, xfer->length, xfer->flags));
2260 2308
2261#ifdef DIAGNOSTIC 2309#ifdef DIAGNOSTIC
2262 if (xfer->rqflags & URQ_REQUEST) 2310 if (xfer->rqflags & URQ_REQUEST)
2263 panic("uhci_device_intr_transfer: a request"); 2311 panic("uhci_device_intr_transfer: a request");
2264#endif 2312#endif
2265 2313
2266 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 2314 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
@@ -2277,52 +2325,52 @@ uhci_device_intr_start(usbd_xfer_handle  @@ -2277,52 +2325,52 @@ uhci_device_intr_start(usbd_xfer_handle
2277 usb_syncmem(&dataend->dma, 2325 usb_syncmem(&dataend->dma,
2278 dataend->offs + offsetof(uhci_td_t, td_status), 2326 dataend->offs + offsetof(uhci_td_t, td_status),
2279 sizeof(dataend->td.td_status), 2327 sizeof(dataend->td.td_status),
2280 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2328 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2281 2329
2282#ifdef UHCI_DEBUG 2330#ifdef UHCI_DEBUG
2283 if (uhcidebug > 10) { 2331 if (uhcidebug > 10) {
2284 DPRINTF(("uhci_device_intr_transfer: data(1)\n")); 2332 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2285 uhci_dump_tds(data); 2333 uhci_dump_tds(data);
2286 uhci_dump_qh(upipe->u.intr.qhs[0]); 2334 uhci_dump_qh(upipe->u.intr.qhs[0]);
2287 } 2335 }
2288#endif 2336#endif
2289 2337
2290 s = splusb(); 2338 mutex_enter(&sc->sc_lock);
2291 /* Set up interrupt info. */ 2339 /* Set up interrupt info. */
2292 ii->xfer = xfer; 2340 ii->xfer = xfer;
2293 ii->stdstart = data; 2341 ii->stdstart = data;
2294 ii->stdend = dataend; 2342 ii->stdend = dataend;
2295#ifdef DIAGNOSTIC 2343#ifdef DIAGNOSTIC
2296 if (!ii->isdone) { 2344 if (!ii->isdone) {
2297 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii); 2345 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2298 } 2346 }
2299 ii->isdone = 0; 2347 ii->isdone = 0;
2300#endif 2348#endif
2301 2349
2302 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n", 2350 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2303 upipe->u.intr.qhs[0])); 2351 upipe->u.intr.qhs[0]));
2304 for (i = 0; i < upipe->u.intr.npoll; i++) { 2352 for (i = 0; i < upipe->u.intr.npoll; i++) {
2305 sqh = upipe->u.intr.qhs[i]; 2353 sqh = upipe->u.intr.qhs[i];
2306 sqh->elink = data; 2354 sqh->elink = data;
2307 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD); 2355 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2308 usb_syncmem(&sqh->dma, 2356 usb_syncmem(&sqh->dma,
2309 sqh->offs + offsetof(uhci_qh_t, qh_elink), 2357 sqh->offs + offsetof(uhci_qh_t, qh_elink),
2310 sizeof(sqh->qh.qh_elink), 2358 sizeof(sqh->qh.qh_elink),
2311 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2359 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2312 } 2360 }
2313 uhci_add_intr_info(sc, ii); 2361 uhci_add_intr_info(sc, ii);
2314 xfer->status = USBD_IN_PROGRESS; 2362 xfer->status = USBD_IN_PROGRESS;
2315 splx(s); 2363 mutex_exit(&sc->sc_lock);
2316 2364
2317#ifdef UHCI_DEBUG 2365#ifdef UHCI_DEBUG
2318 if (uhcidebug > 10) { 2366 if (uhcidebug > 10) {
2319 DPRINTF(("uhci_device_intr_transfer: data(2)\n")); 2367 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2320 uhci_dump_tds(data); 2368 uhci_dump_tds(data);
2321 uhci_dump_qh(upipe->u.intr.qhs[0]); 2369 uhci_dump_qh(upipe->u.intr.qhs[0]);
2322 } 2370 }
2323#endif 2371#endif
2324 2372
2325 return (USBD_IN_PROGRESS); 2373 return (USBD_IN_PROGRESS);
2326} 2374}
2327 2375
2328/* Abort a device control request. */ 2376/* Abort a device control request. */
@@ -2348,65 +2396,65 @@ uhci_device_intr_abort(usbd_xfer_handle  @@ -2348,65 +2396,65 @@ uhci_device_intr_abort(usbd_xfer_handle
2348 DPRINTFN(1,("uhci_device_intr_abort: remove\n")); 2396 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2349 xfer->pipe->intrxfer = NULL; 2397 xfer->pipe->intrxfer = NULL;
2350 } 2398 }
2351 uhci_abort_xfer(xfer, USBD_CANCELLED); 2399 uhci_abort_xfer(xfer, USBD_CANCELLED);
2352} 2400}
2353 2401
2354/* Close a device interrupt pipe. */ 2402/* Close a device interrupt pipe. */
2355void 2403void
2356uhci_device_intr_close(usbd_pipe_handle pipe) 2404uhci_device_intr_close(usbd_pipe_handle pipe)
2357{ 2405{
2358 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2406 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2359 uhci_softc_t *sc = pipe->device->bus->hci_private; 2407 uhci_softc_t *sc = pipe->device->bus->hci_private;
2360 int i, npoll; 2408 int i, npoll;
2361 int s; 
2362 2409
2363 /* Unlink descriptors from controller data structures. */ 2410 /* Unlink descriptors from controller data structures. */
2364 npoll = upipe->u.intr.npoll; 2411 npoll = upipe->u.intr.npoll;
2365 s = splusb(); 2412 mutex_enter(&sc->sc_lock);
2366 for (i = 0; i < npoll; i++) 2413 for (i = 0; i < npoll; i++)
2367 uhci_remove_intr(sc, upipe->u.intr.qhs[i]); 2414 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2368 splx(s); 2415 mutex_exit(&sc->sc_lock);
2369 2416
2370 /* 2417 /*
2371 * We now have to wait for any activity on the physical 2418 * We now have to wait for any activity on the physical
2372 * descriptors to stop. 2419 * descriptors to stop.
2373 */ 2420 */
2374 usb_delay_ms(&sc->sc_bus, 2); 2421 usb_delay_ms(&sc->sc_bus, 2);
2375 2422
2376 for(i = 0; i < npoll; i++) 2423 for(i = 0; i < npoll; i++)
2377 uhci_free_sqh(sc, upipe->u.intr.qhs[i]); 2424 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2378 free(upipe->u.intr.qhs, M_USBHC); 2425 kmem_free(upipe->u.intr.qhs, npoll * sizeof(uhci_soft_qh_t *));
2379 2426
2380 /* XXX free other resources */ 2427 /* XXX free other resources */
2381} 2428}
2382 2429
2383usbd_status 2430usbd_status
2384uhci_device_request(usbd_xfer_handle xfer) 2431uhci_device_request(usbd_xfer_handle xfer)
2385{ 2432{
2386 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2433 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2387 usb_device_request_t *req = &xfer->request; 2434 usb_device_request_t *req = &xfer->request;
2388 usbd_device_handle dev = upipe->pipe.device; 2435 usbd_device_handle dev = upipe->pipe.device;
2389 uhci_softc_t *sc = dev->bus->hci_private; 2436 uhci_softc_t *sc = dev->bus->hci_private;
2390 int addr = dev->address; 2437 int addr = dev->address;
2391 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 2438 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2392 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2439 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2393 uhci_soft_td_t *setup, *data, *stat, *next, *dataend; 2440 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2394 uhci_soft_qh_t *sqh; 2441 uhci_soft_qh_t *sqh;
2395 int len; 2442 int len;
2396 u_int32_t ls; 2443 u_int32_t ls;
2397 usbd_status err; 2444 usbd_status err;
2398 int isread; 2445 int isread;
2399 int s; 2446
 2447 KASSERT(mutex_owned(&sc->sc_lock));
2400 2448
2401 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, " 2449 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2402 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n", 2450 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2403 req->bmRequestType, req->bRequest, UGETW(req->wValue), 2451 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2404 UGETW(req->wIndex), UGETW(req->wLength), 2452 UGETW(req->wIndex), UGETW(req->wLength),
2405 addr, endpt)); 2453 addr, endpt));
2406 2454
2407 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0; 2455 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2408 isread = req->bmRequestType & UT_READ; 2456 isread = req->bmRequestType & UT_READ;
2409 len = UGETW(req->wLength); 2457 len = UGETW(req->wLength);
2410 2458
2411 setup = upipe->u.ctl.setup; 2459 setup = upipe->u.ctl.setup;
2412 stat = upipe->u.ctl.stat; 2460 stat = upipe->u.ctl.stat;
@@ -2466,27 +2514,27 @@ uhci_device_request(usbd_xfer_handle xfe @@ -2466,27 +2514,27 @@ uhci_device_request(usbd_xfer_handle xfe
2466 ii->stdstart = setup; 2514 ii->stdstart = setup;
2467 ii->stdend = stat; 2515 ii->stdend = stat;
2468#ifdef DIAGNOSTIC 2516#ifdef DIAGNOSTIC
2469 if (!ii->isdone) { 2517 if (!ii->isdone) {
2470 printf("uhci_device_request: not done, ii=%p\n", ii); 2518 printf("uhci_device_request: not done, ii=%p\n", ii);
2471 } 2519 }
2472 ii->isdone = 0; 2520 ii->isdone = 0;
2473#endif 2521#endif
2474 2522
2475 sqh->elink = setup; 2523 sqh->elink = setup;
2476 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD); 2524 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2477 /* uhci_add_?s_ctrl() will do usb_syncmem(sqh) */ 2525 /* uhci_add_?s_ctrl() will do usb_syncmem(sqh) */
2478 2526
2479 s = splusb(); 2527 mutex_enter(&sc->sc_lock);
2480 if (dev->speed == USB_SPEED_LOW) 2528 if (dev->speed == USB_SPEED_LOW)
2481 uhci_add_ls_ctrl(sc, sqh); 2529 uhci_add_ls_ctrl(sc, sqh);
2482 else 2530 else
2483 uhci_add_hs_ctrl(sc, sqh); 2531 uhci_add_hs_ctrl(sc, sqh);
2484 uhci_add_intr_info(sc, ii); 2532 uhci_add_intr_info(sc, ii);
2485#ifdef UHCI_DEBUG 2533#ifdef UHCI_DEBUG
2486 if (uhcidebug > 12) { 2534 if (uhcidebug > 12) {
2487 uhci_soft_td_t *std; 2535 uhci_soft_td_t *std;
2488 uhci_soft_qh_t *xqh; 2536 uhci_soft_qh_t *xqh;
2489 uhci_soft_qh_t *sxqh; 2537 uhci_soft_qh_t *sxqh;
2490 int maxqh = 0; 2538 int maxqh = 0;
2491 uhci_physaddr_t link; 2539 uhci_physaddr_t link;
2492 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n")); 2540 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
@@ -2504,67 +2552,70 @@ uhci_device_request(usbd_xfer_handle xfe @@ -2504,67 +2552,70 @@ uhci_device_request(usbd_xfer_handle xfe
2504 xqh->hlink == xqh ? NULL : xqh->hlink)) { 2552 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2505 uhci_dump_qh(xqh); 2553 uhci_dump_qh(xqh);
2506 } 2554 }
2507 DPRINTF(("Enqueued QH:\n")); 2555 DPRINTF(("Enqueued QH:\n"));
2508 uhci_dump_qh(sqh); 2556 uhci_dump_qh(sqh);
2509 uhci_dump_tds(sqh->elink); 2557 uhci_dump_tds(sqh->elink);
2510 } 2558 }
2511#endif 2559#endif
2512 if (xfer->timeout && !sc->sc_bus.use_polling) { 2560 if (xfer->timeout && !sc->sc_bus.use_polling) {
2513 callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout), 2561 callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
2514 uhci_timeout, ii); 2562 uhci_timeout, ii);
2515 } 2563 }
2516 xfer->status = USBD_IN_PROGRESS; 2564 xfer->status = USBD_IN_PROGRESS;
2517 splx(s); 2565 mutex_exit(&sc->sc_lock);
2518 2566
2519 return (USBD_NORMAL_COMPLETION); 2567 return (USBD_NORMAL_COMPLETION);
2520} 2568}
2521 2569
2522usbd_status 2570usbd_status
2523uhci_device_isoc_transfer(usbd_xfer_handle xfer) 2571uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2524{ 2572{
 2573 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
2525 usbd_status err; 2574 usbd_status err;
2526 2575
2527 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer)); 2576 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2528 2577
2529 /* Put it on our queue, */ 2578 /* Put it on our queue, */
 2579 mutex_enter(&sc->sc_lock);
2530 err = usb_insert_transfer(xfer); 2580 err = usb_insert_transfer(xfer);
 2581 mutex_exit(&sc->sc_lock);
2531 2582
2532 /* bail out on error, */ 2583 /* bail out on error, */
2533 if (err && err != USBD_IN_PROGRESS) 2584 if (err && err != USBD_IN_PROGRESS)
2534 return (err); 2585 return (err);
2535 2586
2536 /* XXX should check inuse here */ 2587 /* XXX should check inuse here */
2537 2588
2538 /* insert into schedule, */ 2589 /* insert into schedule, */
2539 uhci_device_isoc_enter(xfer); 2590 uhci_device_isoc_enter(xfer);
2540 2591
2541 /* and start if the pipe wasn't running */ 2592 /* and start if the pipe wasn't running */
2542 if (!err) 2593 if (!err)
2543 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue)); 2594 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2544 2595
2545 return (err); 2596 return (err);
2546} 2597}
2547 2598
2548void 2599void
2549uhci_device_isoc_enter(usbd_xfer_handle xfer) 2600uhci_device_isoc_enter(usbd_xfer_handle xfer)
2550{ 2601{
2551 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2602 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2552 usbd_device_handle dev = upipe->pipe.device; 2603 usbd_device_handle dev = upipe->pipe.device;
2553 uhci_softc_t *sc = dev->bus->hci_private; 2604 uhci_softc_t *sc = dev->bus->hci_private;
2554 struct iso *iso = &upipe->u.iso; 2605 struct iso *iso = &upipe->u.iso;
2555 uhci_soft_td_t *std; 2606 uhci_soft_td_t *std;
2556 u_int32_t buf, len, status, offs; 2607 u_int32_t buf, len, status, offs;
2557 int s, i, next, nframes; 2608 int i, next, nframes;
2558 int rd = UE_GET_DIR(upipe->pipe.endpoint->edesc->bEndpointAddress) == UE_DIR_IN; 2609 int rd = UE_GET_DIR(upipe->pipe.endpoint->edesc->bEndpointAddress) == UE_DIR_IN;
2559 2610
2560 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p " 2611 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2561 "nframes=%d\n", 2612 "nframes=%d\n",
2562 iso->inuse, iso->next, xfer, xfer->nframes)); 2613 iso->inuse, iso->next, xfer, xfer->nframes));
2563 2614
2564 if (sc->sc_dying) 2615 if (sc->sc_dying)
2565 return; 2616 return;
2566 2617
2567 if (xfer->status == USBD_IN_PROGRESS) { 2618 if (xfer->status == USBD_IN_PROGRESS) {
2568 /* This request has already been entered into the frame list */ 2619 /* This request has already been entered into the frame list */
2569 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer); 2620 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2570 /* XXX */ 2621 /* XXX */
@@ -2581,27 +2632,27 @@ uhci_device_isoc_enter(usbd_xfer_handle  @@ -2581,27 +2632,27 @@ uhci_device_isoc_enter(usbd_xfer_handle
2581 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT; 2632 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2582 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next)); 2633 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2583 } 2634 }
2584 2635
2585 xfer->status = USBD_IN_PROGRESS; 2636 xfer->status = USBD_IN_PROGRESS;
2586 UXFER(xfer)->curframe = next; 2637 UXFER(xfer)->curframe = next;
2587 2638
2588 buf = DMAADDR(&xfer->dmabuf, 0); 2639 buf = DMAADDR(&xfer->dmabuf, 0);
2589 offs = 0; 2640 offs = 0;
2590 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) | 2641 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2591 UHCI_TD_ACTIVE | 2642 UHCI_TD_ACTIVE |
2592 UHCI_TD_IOS); 2643 UHCI_TD_IOS);
2593 nframes = xfer->nframes; 2644 nframes = xfer->nframes;
2594 s = splusb(); 2645 mutex_enter(&sc->sc_lock);
2595 for (i = 0; i < nframes; i++) { 2646 for (i = 0; i < nframes; i++) {
2596 std = iso->stds[next]; 2647 std = iso->stds[next];
2597 if (++next >= UHCI_VFRAMELIST_COUNT) 2648 if (++next >= UHCI_VFRAMELIST_COUNT)
2598 next = 0; 2649 next = 0;
2599 len = xfer->frlengths[i]; 2650 len = xfer->frlengths[i];
2600 std->td.td_buffer = htole32(buf); 2651 std->td.td_buffer = htole32(buf);
2601 usb_syncmem(&xfer->dmabuf, offs, len,  2652 usb_syncmem(&xfer->dmabuf, offs, len,
2602 rd ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 2653 rd ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2603 if (i == nframes - 1) 2654 if (i == nframes - 1)
2604 status |= UHCI_TD_IOC; 2655 status |= UHCI_TD_IOC;
2605 std->td.td_status = htole32(status); 2656 std->td.td_status = htole32(status);
2606 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK); 2657 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2607 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len)); 2658 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
@@ -2609,93 +2660,96 @@ uhci_device_isoc_enter(usbd_xfer_handle  @@ -2609,93 +2660,96 @@ uhci_device_isoc_enter(usbd_xfer_handle
2609 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2660 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2610#ifdef UHCI_DEBUG 2661#ifdef UHCI_DEBUG
2611 if (uhcidebug > 5) { 2662 if (uhcidebug > 5) {
2612 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i)); 2663 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2613 uhci_dump_td(std); 2664 uhci_dump_td(std);
2614 } 2665 }
2615#endif 2666#endif
2616 buf += len; 2667 buf += len;
2617 offs += len; 2668 offs += len;
2618 } 2669 }
2619 iso->next = next; 2670 iso->next = next;
2620 iso->inuse += xfer->nframes; 2671 iso->inuse += xfer->nframes;
2621 2672
2622 splx(s); 2673 mutex_exit(&sc->sc_lock);
2623} 2674}
2624 2675
2625usbd_status 2676usbd_status
2626uhci_device_isoc_start(usbd_xfer_handle xfer) 2677uhci_device_isoc_start(usbd_xfer_handle xfer)
2627{ 2678{
2628 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2679 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2629 uhci_softc_t *sc = upipe->pipe.device->bus->hci_private; 2680 uhci_softc_t *sc = upipe->pipe.device->bus->hci_private;
2630 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2681 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2631 uhci_soft_td_t *end; 2682 uhci_soft_td_t *end;
2632 int s, i; 2683 int i;
2633 2684
2634 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer)); 2685 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2635 2686
2636 if (sc->sc_dying) 2687 mutex_enter(&sc->sc_lock);
 2688
 2689 if (sc->sc_dying) {
 2690 mutex_exit(&sc->sc_lock);
2637 return (USBD_IOERROR); 2691 return (USBD_IOERROR);
 2692 }
2638 2693
2639#ifdef DIAGNOSTIC 2694#ifdef DIAGNOSTIC
2640 if (xfer->status != USBD_IN_PROGRESS) 2695 if (xfer->status != USBD_IN_PROGRESS)
2641 printf("uhci_device_isoc_start: not in progress %p\n", xfer); 2696 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2642#endif 2697#endif
2643 2698
2644 /* Find the last TD */ 2699 /* Find the last TD */
2645 i = UXFER(xfer)->curframe + xfer->nframes; 2700 i = UXFER(xfer)->curframe + xfer->nframes;
2646 if (i >= UHCI_VFRAMELIST_COUNT) 2701 if (i >= UHCI_VFRAMELIST_COUNT)
2647 i -= UHCI_VFRAMELIST_COUNT; 2702 i -= UHCI_VFRAMELIST_COUNT;
2648 end = upipe->u.iso.stds[i]; 2703 end = upipe->u.iso.stds[i];
2649 2704
2650#ifdef DIAGNOSTIC 2705#ifdef DIAGNOSTIC
2651 if (end == NULL) { 2706 if (end == NULL) {
2652 printf("uhci_device_isoc_start: end == NULL\n"); 2707 printf("uhci_device_isoc_start: end == NULL\n");
2653 return (USBD_INVAL); 2708 return (USBD_INVAL);
2654 } 2709 }
2655#endif 2710#endif
2656 2711
2657 s = splusb(); 
2658 
2659 /* Set up interrupt info. */ 2712 /* Set up interrupt info. */
2660 ii->xfer = xfer; 2713 ii->xfer = xfer;
2661 ii->stdstart = end; 2714 ii->stdstart = end;
2662 ii->stdend = end; 2715 ii->stdend = end;
2663#ifdef DIAGNOSTIC 2716#ifdef DIAGNOSTIC
2664 if (!ii->isdone) 2717 if (!ii->isdone)
2665 printf("uhci_device_isoc_start: not done, ii=%p\n", ii); 2718 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2666 ii->isdone = 0; 2719 ii->isdone = 0;
2667#endif 2720#endif
2668 uhci_add_intr_info(sc, ii); 2721 uhci_add_intr_info(sc, ii);
2669 2722
2670 splx(s); 2723 mutex_exit(&sc->sc_lock);
2671 2724
2672 return (USBD_IN_PROGRESS); 2725 return (USBD_IN_PROGRESS);
2673} 2726}
2674 2727
2675void 2728void
2676uhci_device_isoc_abort(usbd_xfer_handle xfer) 2729uhci_device_isoc_abort(usbd_xfer_handle xfer)
2677{ 2730{
 2731 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
2678 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2732 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2679 uhci_soft_td_t **stds = upipe->u.iso.stds; 2733 uhci_soft_td_t **stds = upipe->u.iso.stds;
2680 uhci_soft_td_t *std; 2734 uhci_soft_td_t *std;
2681 int i, n, s, nframes, maxlen, len; 2735 int i, n, nframes, maxlen, len;
2682 2736
2683 s = splusb(); 2737 mutex_enter(&sc->sc_lock);
2684 2738
2685 /* Transfer is already done. */ 2739 /* Transfer is already done. */
2686 if (xfer->status != USBD_NOT_STARTED && 2740 if (xfer->status != USBD_NOT_STARTED &&
2687 xfer->status != USBD_IN_PROGRESS) { 2741 xfer->status != USBD_IN_PROGRESS) {
2688 splx(s); 2742 mutex_exit(&sc->sc_lock);
2689 return; 2743 return;
2690 } 2744 }
2691 2745
2692 /* Give xfer the requested abort code. */ 2746 /* Give xfer the requested abort code. */
2693 xfer->status = USBD_CANCELLED; 2747 xfer->status = USBD_CANCELLED;
2694 2748
2695 /* make hardware ignore it, */ 2749 /* make hardware ignore it, */
2696 nframes = xfer->nframes; 2750 nframes = xfer->nframes;
2697 n = UXFER(xfer)->curframe; 2751 n = UXFER(xfer)->curframe;
2698 maxlen = 0; 2752 maxlen = 0;
2699 for (i = 0; i < nframes; i++) { 2753 for (i = 0; i < nframes; i++) {
2700 std = stds[n]; 2754 std = stds[n];
2701 usb_syncmem(&std->dma, 2755 usb_syncmem(&std->dma,
@@ -2717,157 +2771,162 @@ uhci_device_isoc_abort(usbd_xfer_handle  @@ -2717,157 +2771,162 @@ uhci_device_isoc_abort(usbd_xfer_handle
2717 if (++n >= UHCI_VFRAMELIST_COUNT) 2771 if (++n >= UHCI_VFRAMELIST_COUNT)
2718 n = 0; 2772 n = 0;
2719 } 2773 }
2720 2774
2721 /* and wait until we are sure the hardware has finished. */ 2775 /* and wait until we are sure the hardware has finished. */
2722 delay(maxlen); 2776 delay(maxlen);
2723 2777
2724#ifdef DIAGNOSTIC 2778#ifdef DIAGNOSTIC
2725 UXFER(xfer)->iinfo.isdone = 1; 2779 UXFER(xfer)->iinfo.isdone = 1;
2726#endif 2780#endif
2727 /* Run callback and remove from interrupt list. */ 2781 /* Run callback and remove from interrupt list. */
2728 usb_transfer_complete(xfer); 2782 usb_transfer_complete(xfer);
2729 2783
2730 splx(s); 2784 mutex_exit(&sc->sc_lock);
2731} 2785}
2732 2786
2733void 2787void
2734uhci_device_isoc_close(usbd_pipe_handle pipe) 2788uhci_device_isoc_close(usbd_pipe_handle pipe)
2735{ 2789{
2736 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2790 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2737 usbd_device_handle dev = upipe->pipe.device; 2791 usbd_device_handle dev = upipe->pipe.device;
2738 uhci_softc_t *sc = dev->bus->hci_private; 2792 uhci_softc_t *sc = dev->bus->hci_private;
2739 uhci_soft_td_t *std, *vstd; 2793 uhci_soft_td_t *std, *vstd;
2740 struct iso *iso; 2794 struct iso *iso;
2741 int i, s; 2795 int i;
2742 2796
2743 /* 2797 /*
2744 * Make sure all TDs are marked as inactive. 2798 * Make sure all TDs are marked as inactive.
2745 * Wait for completion. 2799 * Wait for completion.
2746 * Unschedule. 2800 * Unschedule.
2747 * Deallocate. 2801 * Deallocate.
2748 */ 2802 */
2749 iso = &upipe->u.iso; 2803 iso = &upipe->u.iso;
2750 2804
 2805 mutex_enter(&sc->sc_lock);
2751 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) { 2806 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2752 std = iso->stds[i]; 2807 std = iso->stds[i];
2753 usb_syncmem(&std->dma, 2808 usb_syncmem(&std->dma,
2754 std->offs + offsetof(uhci_td_t, td_status), 2809 std->offs + offsetof(uhci_td_t, td_status),
2755 sizeof(std->td.td_status), 2810 sizeof(std->td.td_status),
2756 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD); 2811 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2757 std->td.td_status &= htole32(~UHCI_TD_ACTIVE); 2812 std->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2758 usb_syncmem(&std->dma, 2813 usb_syncmem(&std->dma,
2759 std->offs + offsetof(uhci_td_t, td_status), 2814 std->offs + offsetof(uhci_td_t, td_status),
2760 sizeof(std->td.td_status), 2815 sizeof(std->td.td_status),
2761 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2816 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2762 } 2817 }
2763 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */ 2818 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2764 2819
2765 s = splusb(); 
2766 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) { 2820 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2767 std = iso->stds[i]; 2821 std = iso->stds[i];
2768 for (vstd = sc->sc_vframes[i].htd; 2822 for (vstd = sc->sc_vframes[i].htd;
2769 vstd != NULL && vstd->link.std != std; 2823 vstd != NULL && vstd->link.std != std;
2770 vstd = vstd->link.std) 2824 vstd = vstd->link.std)
2771 ; 2825 ;
2772 if (vstd == NULL) { 2826 if (vstd == NULL) {
2773 /*panic*/ 2827 /*panic*/
2774 printf("uhci_device_isoc_close: %p not found\n", std); 2828 printf("uhci_device_isoc_close: %p not found\n", std);
2775 splx(s); 2829 mutex_exit(&sc->sc_lock);
2776 return; 2830 return;
2777 } 2831 }
2778 vstd->link = std->link; 2832 vstd->link = std->link;
2779 usb_syncmem(&std->dma, 2833 usb_syncmem(&std->dma,
2780 std->offs + offsetof(uhci_td_t, td_link), 2834 std->offs + offsetof(uhci_td_t, td_link),
2781 sizeof(std->td.td_link), 2835 sizeof(std->td.td_link),
2782 BUS_DMASYNC_POSTWRITE); 2836 BUS_DMASYNC_POSTWRITE);
2783 vstd->td.td_link = std->td.td_link; 2837 vstd->td.td_link = std->td.td_link;
2784 usb_syncmem(&vstd->dma, 2838 usb_syncmem(&vstd->dma,
2785 vstd->offs + offsetof(uhci_td_t, td_link), 2839 vstd->offs + offsetof(uhci_td_t, td_link),
2786 sizeof(vstd->td.td_link), 2840 sizeof(vstd->td.td_link),
2787 BUS_DMASYNC_PREWRITE); 2841 BUS_DMASYNC_PREWRITE);
2788 uhci_free_std(sc, std); 2842 uhci_free_std(sc, std);
2789 } 2843 }
2790 splx(s); 2844 mutex_exit(&sc->sc_lock);
2791 2845
2792 free(iso->stds, M_USBHC); 2846 kmem_free(iso->stds, UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *));
2793} 2847}
2794 2848
2795usbd_status 2849usbd_status
2796uhci_setup_isoc(usbd_pipe_handle pipe) 2850uhci_setup_isoc(usbd_pipe_handle pipe)
2797{ 2851{
2798 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2852 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2799 usbd_device_handle dev = upipe->pipe.device; 2853 usbd_device_handle dev = upipe->pipe.device;
2800 uhci_softc_t *sc = dev->bus->hci_private; 2854 uhci_softc_t *sc = dev->bus->hci_private;
2801 int addr = upipe->pipe.device->address; 2855 int addr = upipe->pipe.device->address;
2802 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 2856 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2803 int rd = UE_GET_DIR(endpt) == UE_DIR_IN; 2857 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2804 uhci_soft_td_t *std, *vstd; 2858 uhci_soft_td_t *std, *vstd;
2805 u_int32_t token; 2859 u_int32_t token;
2806 struct iso *iso; 2860 struct iso *iso;
2807 int i, s; 2861 int i;
2808 2862
2809 iso = &upipe->u.iso; 2863 iso = &upipe->u.iso;
2810 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *), 2864 iso->stds = kmem_alloc(UHCI_VFRAMELIST_COUNT *
2811 M_USBHC, M_WAITOK); 2865 sizeof (uhci_soft_td_t *),
 2866 KM_SLEEP);
 2867 if (iso->stds == NULL)
 2868 return USBD_NOMEM;
2812 2869
2813 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) : 2870 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2814 UHCI_TD_OUT(0, endpt, addr, 0); 2871 UHCI_TD_OUT(0, endpt, addr, 0);
2815 2872
 2873 mutex_enter(&sc->sc_lock);
 2874
2816 /* Allocate the TDs and mark as inactive; */ 2875 /* Allocate the TDs and mark as inactive; */
2817 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) { 2876 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2818 std = uhci_alloc_std(sc); 2877 std = uhci_alloc_std(sc);
2819 if (std == 0) 2878 if (std == 0)
2820 goto bad; 2879 goto bad;
2821 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */ 2880 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2822 std->td.td_token = htole32(token); 2881 std->td.td_token = htole32(token);
2823 usb_syncmem(&std->dma, std->offs, sizeof(std->td), 2882 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
2824 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2883 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2825 iso->stds[i] = std; 2884 iso->stds[i] = std;
2826 } 2885 }
2827 2886
2828 /* Insert TDs into schedule. */ 2887 /* Insert TDs into schedule. */
2829 s = splusb(); 
2830 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) { 2888 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2831 std = iso->stds[i]; 2889 std = iso->stds[i];
2832 vstd = sc->sc_vframes[i].htd; 2890 vstd = sc->sc_vframes[i].htd;
2833 usb_syncmem(&vstd->dma, 2891 usb_syncmem(&vstd->dma,
2834 vstd->offs + offsetof(uhci_td_t, td_link), 2892 vstd->offs + offsetof(uhci_td_t, td_link),
2835 sizeof(vstd->td.td_link), 2893 sizeof(vstd->td.td_link),
2836 BUS_DMASYNC_POSTWRITE); 2894 BUS_DMASYNC_POSTWRITE);
2837 std->link = vstd->link; 2895 std->link = vstd->link;
2838 std->td.td_link = vstd->td.td_link; 2896 std->td.td_link = vstd->td.td_link;
2839 usb_syncmem(&std->dma, 2897 usb_syncmem(&std->dma,
2840 std->offs + offsetof(uhci_td_t, td_link), 2898 std->offs + offsetof(uhci_td_t, td_link),
2841 sizeof(std->td.td_link), 2899 sizeof(std->td.td_link),
2842 BUS_DMASYNC_PREWRITE); 2900 BUS_DMASYNC_PREWRITE);
2843 vstd->link.std = std; 2901 vstd->link.std = std;
2844 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD); 2902 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2845 usb_syncmem(&vstd->dma, 2903 usb_syncmem(&vstd->dma,
2846 vstd->offs + offsetof(uhci_td_t, td_link), 2904 vstd->offs + offsetof(uhci_td_t, td_link),
2847 sizeof(vstd->td.td_link), 2905 sizeof(vstd->td.td_link),
2848 BUS_DMASYNC_PREWRITE); 2906 BUS_DMASYNC_PREWRITE);
2849 } 2907 }
2850 splx(s); 2908 mutex_exit(&sc->sc_lock);
2851 2909
2852 iso->next = -1; 2910 iso->next = -1;
2853 iso->inuse = 0; 2911 iso->inuse = 0;
2854 2912
2855 return (USBD_NORMAL_COMPLETION); 2913 return (USBD_NORMAL_COMPLETION);
2856 2914
2857 bad: 2915 bad:
2858 while (--i >= 0) 2916 while (--i >= 0)
2859 uhci_free_std(sc, iso->stds[i]); 2917 uhci_free_std(sc, iso->stds[i]);
2860 free(iso->stds, M_USBHC); 2918 mutex_exit(&sc->sc_lock);
 2919 kmem_free(iso->stds, UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *));
2861 return (USBD_NOMEM); 2920 return (USBD_NOMEM);
2862} 2921}
2863 2922
2864void 2923void
2865uhci_device_isoc_done(usbd_xfer_handle xfer) 2924uhci_device_isoc_done(usbd_xfer_handle xfer)
2866{ 2925{
2867 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2926 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2868 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2927 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2869 int i, offs; 2928 int i, offs;
2870 int rd = UE_GET_DIR(upipe->pipe.endpoint->edesc->bEndpointAddress) == UE_DIR_IN; 2929 int rd = UE_GET_DIR(upipe->pipe.endpoint->edesc->bEndpointAddress) == UE_DIR_IN;
2871 2930
2872 2931
2873 DPRINTFN(4, ("uhci_isoc_done: length=%d, busy_free=0x%08x\n", 2932 DPRINTFN(4, ("uhci_isoc_done: length=%d, busy_free=0x%08x\n",
@@ -2912,26 +2971,28 @@ uhci_device_isoc_done(usbd_xfer_handle x @@ -2912,26 +2971,28 @@ uhci_device_isoc_done(usbd_xfer_handle x
2912} 2971}
2913 2972
2914void 2973void
2915uhci_device_intr_done(usbd_xfer_handle xfer) 2974uhci_device_intr_done(usbd_xfer_handle xfer)
2916{ 2975{
2917 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2976 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2918 uhci_softc_t *sc = ii->sc; 2977 uhci_softc_t *sc = ii->sc;
2919 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2978 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2920 uhci_soft_qh_t *sqh; 2979 uhci_soft_qh_t *sqh;
2921 int i, npoll, isread; 2980 int i, npoll, isread;
2922 2981
2923 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen)); 2982 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2924 2983
 2984 KASSERT(mutex_owned(&sc->sc_lock));
 2985
2925 npoll = upipe->u.intr.npoll; 2986 npoll = upipe->u.intr.npoll;
2926 for(i = 0; i < npoll; i++) { 2987 for(i = 0; i < npoll; i++) {
2927 sqh = upipe->u.intr.qhs[i]; 2988 sqh = upipe->u.intr.qhs[i];
2928 sqh->elink = NULL; 2989 sqh->elink = NULL;
2929 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 2990 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2930 usb_syncmem(&sqh->dma, 2991 usb_syncmem(&sqh->dma,
2931 sqh->offs + offsetof(uhci_qh_t, qh_elink), 2992 sqh->offs + offsetof(uhci_qh_t, qh_elink),
2932 sizeof(sqh->qh.qh_elink), 2993 sizeof(sqh->qh.qh_elink),
2933 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2994 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2934 } 2995 }
2935 uhci_free_std_chain(sc, ii->stdstart, NULL); 2996 uhci_free_std_chain(sc, ii->stdstart, NULL);
2936 2997
2937 isread = UE_GET_DIR(upipe->pipe.endpoint->edesc->bEndpointAddress) == UE_DIR_IN; 2998 isread = UE_GET_DIR(upipe->pipe.endpoint->edesc->bEndpointAddress) == UE_DIR_IN;
@@ -2988,26 +3049,28 @@ uhci_device_intr_done(usbd_xfer_handle x @@ -2988,26 +3049,28 @@ uhci_device_intr_done(usbd_xfer_handle x
2988 } 3049 }
2989} 3050}
2990 3051
2991/* Deallocate request data structures */ 3052/* Deallocate request data structures */
2992void 3053void
2993uhci_device_ctrl_done(usbd_xfer_handle xfer) 3054uhci_device_ctrl_done(usbd_xfer_handle xfer)
2994{ 3055{
2995 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 3056 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2996 uhci_softc_t *sc = ii->sc; 3057 uhci_softc_t *sc = ii->sc;
2997 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 3058 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2998 int len = UGETW(xfer->request.wLength); 3059 int len = UGETW(xfer->request.wLength);
2999 int isread = (xfer->request.bmRequestType & UT_READ); 3060 int isread = (xfer->request.bmRequestType & UT_READ);
3000 3061
 3062 KASSERT(mutex_owned(&sc->sc_lock));
 3063
3001#ifdef DIAGNOSTIC 3064#ifdef DIAGNOSTIC
3002 if (!(xfer->rqflags & URQ_REQUEST)) 3065 if (!(xfer->rqflags & URQ_REQUEST))
3003 panic("uhci_device_ctrl_done: not a request"); 3066 panic("uhci_device_ctrl_done: not a request");
3004#endif 3067#endif
3005 3068
3006 if (!uhci_active_intr_info(ii)) 3069 if (!uhci_active_intr_info(ii))
3007 return; 3070 return;
3008 3071
3009 uhci_del_intr_info(ii); /* remove from active list */ 3072 uhci_del_intr_info(ii); /* remove from active list */
3010 3073
3011 if (upipe->pipe.device->speed == USB_SPEED_LOW) 3074 if (upipe->pipe.device->speed == USB_SPEED_LOW)
3012 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh); 3075 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
3013 else 3076 else
@@ -3027,26 +3090,28 @@ uhci_device_ctrl_done(usbd_xfer_handle x @@ -3027,26 +3090,28 @@ uhci_device_ctrl_done(usbd_xfer_handle x
3027} 3090}
3028 3091
3029/* Deallocate request data structures */ 3092/* Deallocate request data structures */
3030void 3093void
3031uhci_device_bulk_done(usbd_xfer_handle xfer) 3094uhci_device_bulk_done(usbd_xfer_handle xfer)
3032{ 3095{
3033 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 3096 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
3034 uhci_softc_t *sc = ii->sc; 3097 uhci_softc_t *sc = ii->sc;
3035 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 3098 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
3036 3099
3037 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n", 3100 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
3038 xfer, ii, sc, upipe)); 3101 xfer, ii, sc, upipe));
3039 3102
 3103 KASSERT(mutex_owned(&sc->sc_lock));
 3104
3040 if (!uhci_active_intr_info(ii)) 3105 if (!uhci_active_intr_info(ii))
3041 return; 3106 return;
3042 3107
3043 uhci_del_intr_info(ii); /* remove from active list */ 3108 uhci_del_intr_info(ii); /* remove from active list */
3044 3109
3045 uhci_remove_bulk(sc, upipe->u.bulk.sqh); 3110 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
3046 3111
3047 uhci_free_std_chain(sc, ii->stdstart, NULL); 3112 uhci_free_std_chain(sc, ii->stdstart, NULL);
3048 3113
3049 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen)); 3114 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
3050} 3115}
3051 3116
3052/* Add interrupt QH, called with vflock. */ 3117/* Add interrupt QH, called with vflock. */
@@ -3108,95 +3173,100 @@ uhci_remove_intr(uhci_softc_t *sc, uhci_ @@ -3108,95 +3173,100 @@ uhci_remove_intr(uhci_softc_t *sc, uhci_
3108 usb_syncmem(&pqh->dma, pqh->offs + offsetof(uhci_qh_t, qh_hlink), 3173 usb_syncmem(&pqh->dma, pqh->offs + offsetof(uhci_qh_t, qh_hlink),
3109 sizeof(pqh->qh.qh_hlink), 3174 sizeof(pqh->qh.qh_hlink),
3110 BUS_DMASYNC_PREWRITE); 3175 BUS_DMASYNC_PREWRITE);
3111 delay(UHCI_QH_REMOVE_DELAY); 3176 delay(UHCI_QH_REMOVE_DELAY);
3112 if (vf->eqh == sqh) 3177 if (vf->eqh == sqh)
3113 vf->eqh = pqh; 3178 vf->eqh = pqh;
3114 vf->bandwidth--; 3179 vf->bandwidth--;
3115} 3180}
3116 3181
3117usbd_status 3182usbd_status
3118uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival) 3183uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
3119{ 3184{
3120 uhci_soft_qh_t *sqh; 3185 uhci_soft_qh_t *sqh;
3121 int i, npoll, s; 3186 int i, npoll;
3122 u_int bestbw, bw, bestoffs, offs; 3187 u_int bestbw, bw, bestoffs, offs;
3123 3188
3124 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe)); 3189 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
3125 if (ival == 0) { 3190 if (ival == 0) {
3126 printf("uhci_device_setintr: 0 interval\n"); 3191 printf("uhci_device_setintr: 0 interval\n");
3127 return (USBD_INVAL); 3192 return (USBD_INVAL);
3128 } 3193 }
3129 3194
3130 if (ival > UHCI_VFRAMELIST_COUNT) 3195 if (ival > UHCI_VFRAMELIST_COUNT)
3131 ival = UHCI_VFRAMELIST_COUNT; 3196 ival = UHCI_VFRAMELIST_COUNT;
3132 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival; 3197 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
3133 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll)); 3198 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
3134 3199
3135 upipe->u.intr.npoll = npoll; 3200 upipe->u.intr.npoll = npoll;
3136 upipe->u.intr.qhs = 3201 upipe->u.intr.qhs =
3137 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK); 3202 kmem_alloc(npoll * sizeof(uhci_soft_qh_t *), KM_SLEEP);
 3203 if (upipe->u.intr.qhs == NULL)
 3204 return USBD_NOMEM;
3138 3205
3139 /* 3206 /*
3140 * Figure out which offset in the schedule that has most 3207 * Figure out which offset in the schedule that has most
3141 * bandwidth left over. 3208 * bandwidth left over.
3142 */ 3209 */
3143#define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1)) 3210#define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
3144 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) { 3211 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
3145 for (bw = i = 0; i < npoll; i++) 3212 for (bw = i = 0; i < npoll; i++)
3146 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth; 3213 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
3147 if (bw < bestbw) { 3214 if (bw < bestbw) {
3148 bestbw = bw; 3215 bestbw = bw;
3149 bestoffs = offs; 3216 bestoffs = offs;
3150 } 3217 }
3151 } 3218 }
3152 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs)); 3219 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
3153 3220
 3221 mutex_enter(&sc->sc_lock);
3154 for(i = 0; i < npoll; i++) { 3222 for(i = 0; i < npoll; i++) {
3155 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc); 3223 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
3156 sqh->elink = NULL; 3224 sqh->elink = NULL;
3157 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 3225 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
3158 usb_syncmem(&sqh->dma, 3226 usb_syncmem(&sqh->dma,
3159 sqh->offs + offsetof(uhci_qh_t, qh_elink), 3227 sqh->offs + offsetof(uhci_qh_t, qh_elink),
3160 sizeof(sqh->qh.qh_elink), 3228 sizeof(sqh->qh.qh_elink),
3161 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 3229 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3162 sqh->pos = MOD(i * ival + bestoffs); 3230 sqh->pos = MOD(i * ival + bestoffs);
3163 } 3231 }
3164#undef MOD 3232#undef MOD
3165 3233
3166 s = splusb(); 
3167 /* Enter QHs into the controller data structures. */ 3234 /* Enter QHs into the controller data structures. */
3168 for(i = 0; i < npoll; i++) 3235 for(i = 0; i < npoll; i++)
3169 uhci_add_intr(sc, upipe->u.intr.qhs[i]); 3236 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
3170 splx(s); 3237 mutex_exit(&sc->sc_lock);
3171 3238
3172 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe)); 3239 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
3173 return (USBD_NORMAL_COMPLETION); 3240 return (USBD_NORMAL_COMPLETION);
3174} 3241}
3175 3242
3176/* Open a new pipe. */ 3243/* Open a new pipe. */
3177usbd_status 3244usbd_status
3178uhci_open(usbd_pipe_handle pipe) 3245uhci_open(usbd_pipe_handle pipe)
3179{ 3246{
3180 uhci_softc_t *sc = pipe->device->bus->hci_private; 3247 uhci_softc_t *sc = pipe->device->bus->hci_private;
3181 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 3248 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
3182 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc; 3249 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
3183 usbd_status err; 3250 usbd_status err = USBD_NOMEM;
3184 int ival; 3251 int ival;
3185 3252
3186 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n", 3253 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
3187 pipe, pipe->device->address, 3254 pipe, pipe->device->address,
3188 ed->bEndpointAddress, sc->sc_addr)); 3255 ed->bEndpointAddress, sc->sc_addr));
3189 3256
 3257 if (sc->sc_dying)
 3258 return USBD_IOERROR;
 3259
3190 upipe->aborting = 0; 3260 upipe->aborting = 0;
3191 /* toggle state needed for bulk endpoints */ 3261 /* toggle state needed for bulk endpoints */
3192 upipe->nexttoggle = pipe->endpoint->datatoggle; 3262 upipe->nexttoggle = pipe->endpoint->datatoggle;
3193 3263
3194 if (pipe->device->address == sc->sc_addr) { 3264 if (pipe->device->address == sc->sc_addr) {
3195 switch (ed->bEndpointAddress) { 3265 switch (ed->bEndpointAddress) {
3196 case USB_CONTROL_ENDPOINT: 3266 case USB_CONTROL_ENDPOINT:
3197 pipe->methods = &uhci_root_ctrl_methods; 3267 pipe->methods = &uhci_root_ctrl_methods;
3198 break; 3268 break;
3199 case UE_DIR_IN | UHCI_INTR_ENDPT: 3269 case UE_DIR_IN | UHCI_INTR_ENDPT:
3200 pipe->methods = &uhci_root_intr_methods; 3270 pipe->methods = &uhci_root_intr_methods;
3201 break; 3271 break;
3202 default: 3272 default:
@@ -3240,27 +3310,27 @@ uhci_open(usbd_pipe_handle pipe) @@ -3240,27 +3310,27 @@ uhci_open(usbd_pipe_handle pipe)
3240 pipe->methods = &uhci_device_isoc_methods; 3310 pipe->methods = &uhci_device_isoc_methods;
3241 return (uhci_setup_isoc(pipe)); 3311 return (uhci_setup_isoc(pipe));
3242 case UE_BULK: 3312 case UE_BULK:
3243 pipe->methods = &uhci_device_bulk_methods; 3313 pipe->methods = &uhci_device_bulk_methods;
3244 upipe->u.bulk.sqh = uhci_alloc_sqh(sc); 3314 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
3245 if (upipe->u.bulk.sqh == NULL) 3315 if (upipe->u.bulk.sqh == NULL)
3246 goto bad; 3316 goto bad;
3247 break; 3317 break;
3248 } 3318 }
3249 } 3319 }
3250 return (USBD_NORMAL_COMPLETION); 3320 return (USBD_NORMAL_COMPLETION);
3251 3321
3252 bad: 3322 bad:
3253 return (USBD_NOMEM); 3323 return USBD_NOMEM;
3254} 3324}
3255 3325
3256/* 3326/*
3257 * Data structures and routines to emulate the root hub. 3327 * Data structures and routines to emulate the root hub.
3258 */ 3328 */
3259usb_device_descriptor_t uhci_devd = { 3329usb_device_descriptor_t uhci_devd = {
3260 USB_DEVICE_DESCRIPTOR_SIZE, 3330 USB_DEVICE_DESCRIPTOR_SIZE,
3261 UDESC_DEVICE, /* type */ 3331 UDESC_DEVICE, /* type */
3262 {0x00, 0x01}, /* USB version */ 3332 {0x00, 0x01}, /* USB version */
3263 UDCLASS_HUB, /* class */ 3333 UDCLASS_HUB, /* class */
3264 UDSUBCLASS_HUB, /* subclass */ 3334 UDSUBCLASS_HUB, /* subclass */
3265 UDPROTO_FSHUB, /* protocol */ 3335 UDPROTO_FSHUB, /* protocol */
3266 64, /* max packet */ 3336 64, /* max packet */
@@ -3405,48 +3475,51 @@ uhci_portreset(uhci_softc_t *sc, int ind @@ -3405,48 +3475,51 @@ uhci_portreset(uhci_softc_t *sc, int ind
3405 return (USBD_TIMEOUT); 3475 return (USBD_TIMEOUT);
3406 } 3476 }
3407 3477
3408 sc->sc_isreset = 1; 3478 sc->sc_isreset = 1;
3409 return (USBD_NORMAL_COMPLETION); 3479 return (USBD_NORMAL_COMPLETION);
3410} 3480}
3411 3481
3412/* 3482/*
3413 * Simulate a hardware hub by handling all the necessary requests. 3483 * Simulate a hardware hub by handling all the necessary requests.
3414 */ 3484 */
3415usbd_status 3485usbd_status
3416uhci_root_ctrl_transfer(usbd_xfer_handle xfer) 3486uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3417{ 3487{
 3488 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
3418 usbd_status err; 3489 usbd_status err;
3419 3490
3420 /* Insert last in queue. */ 3491 /* Insert last in queue. */
 3492 mutex_enter(&sc->sc_lock);
3421 err = usb_insert_transfer(xfer); 3493 err = usb_insert_transfer(xfer);
 3494 mutex_exit(&sc->sc_lock);
3422 if (err) 3495 if (err)
3423 return (err); 3496 return (err);
3424 3497
3425 /* 3498 /*
3426 * Pipe isn't running (otherwise err would be USBD_INPROG), 3499 * Pipe isn't running (otherwise err would be USBD_INPROG),
3427 * so start it first. 3500 * so start it first.
3428 */ 3501 */
3429 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 3502 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3430} 3503}
3431 3504
3432usbd_status 3505usbd_status
3433uhci_root_ctrl_start(usbd_xfer_handle xfer) 3506uhci_root_ctrl_start(usbd_xfer_handle xfer)
3434{ 3507{
3435 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private; 3508 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
3436 usb_device_request_t *req; 3509 usb_device_request_t *req;
3437 void *buf = NULL; 3510 void *buf = NULL;
3438 int port, x; 3511 int port, x;
3439 int s, len, value, index, status, change, l, totlen = 0; 3512 int len, value, index, status, change, l, totlen = 0;
3440 usb_port_status_t ps; 3513 usb_port_status_t ps;
3441 usbd_status err; 3514 usbd_status err;
3442 3515
3443 if (sc->sc_dying) 3516 if (sc->sc_dying)
3444 return (USBD_IOERROR); 3517 return (USBD_IOERROR);
3445 3518
3446#ifdef DIAGNOSTIC 3519#ifdef DIAGNOSTIC
3447 if (!(xfer->rqflags & URQ_REQUEST)) 3520 if (!(xfer->rqflags & URQ_REQUEST))
3448 panic("uhci_root_ctrl_transfer: not a request"); 3521 panic("uhci_root_ctrl_transfer: not a request");
3449#endif 3522#endif
3450 req = &xfer->request; 3523 req = &xfer->request;
3451 3524
3452 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n", 3525 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
@@ -3749,29 +3822,29 @@ uhci_root_ctrl_start(usbd_xfer_handle xf @@ -3749,29 +3822,29 @@ uhci_root_ctrl_start(usbd_xfer_handle xf
3749 default: 3822 default:
3750 err = USBD_IOERROR; 3823 err = USBD_IOERROR;
3751 goto ret; 3824 goto ret;
3752 } 3825 }
3753 break; 3826 break;
3754 default: 3827 default:
3755 err = USBD_IOERROR; 3828 err = USBD_IOERROR;
3756 goto ret; 3829 goto ret;
3757 } 3830 }
3758 xfer->actlen = totlen; 3831 xfer->actlen = totlen;
3759 err = USBD_NORMAL_COMPLETION; 3832 err = USBD_NORMAL_COMPLETION;
3760 ret: 3833 ret:
3761 xfer->status = err; 3834 xfer->status = err;
3762 s = splusb(); 3835 mutex_enter(&sc->sc_lock);
3763 usb_transfer_complete(xfer); 3836 usb_transfer_complete(xfer);
3764 splx(s); 3837 mutex_exit(&sc->sc_lock);
3765 return (USBD_IN_PROGRESS); 3838 return (USBD_IN_PROGRESS);
3766} 3839}
3767 3840
3768/* Abort a root control request. */ 3841/* Abort a root control request. */
3769void 3842void
3770uhci_root_ctrl_abort(usbd_xfer_handle xfer) 3843uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3771{ 3844{
3772 /* Nothing to do, all transfers are synchronous. */ 3845 /* Nothing to do, all transfers are synchronous. */
3773} 3846}
3774 3847
3775/* Close the root pipe. */ 3848/* Close the root pipe. */
3776void 3849void
3777uhci_root_ctrl_close(usbd_pipe_handle pipe) 3850uhci_root_ctrl_close(usbd_pipe_handle pipe)
@@ -3792,30 +3865,33 @@ uhci_root_intr_abort(usbd_xfer_handle xf @@ -3792,30 +3865,33 @@ uhci_root_intr_abort(usbd_xfer_handle xf
3792 DPRINTF(("uhci_root_intr_abort: remove\n")); 3865 DPRINTF(("uhci_root_intr_abort: remove\n"));
3793 xfer->pipe->intrxfer = 0; 3866 xfer->pipe->intrxfer = 0;
3794 } 3867 }
3795 xfer->status = USBD_CANCELLED; 3868 xfer->status = USBD_CANCELLED;
3796#ifdef DIAGNOSTIC 3869#ifdef DIAGNOSTIC
3797 UXFER(xfer)->iinfo.isdone = 1; 3870 UXFER(xfer)->iinfo.isdone = 1;
3798#endif 3871#endif
3799 usb_transfer_complete(xfer); 3872 usb_transfer_complete(xfer);
3800} 3873}
3801 3874
3802usbd_status 3875usbd_status
3803uhci_root_intr_transfer(usbd_xfer_handle xfer) 3876uhci_root_intr_transfer(usbd_xfer_handle xfer)
3804{ 3877{
 3878 uhci_softc_t *sc = xfer->pipe->device->bus->hci_private;
3805 usbd_status err; 3879 usbd_status err;
3806 3880
3807 /* Insert last in queue. */ 3881 /* Insert last in queue. */
 3882 mutex_enter(&sc->sc_lock);
3808 err = usb_insert_transfer(xfer); 3883 err = usb_insert_transfer(xfer);
 3884 mutex_exit(&sc->sc_lock);
3809 if (err) 3885 if (err)
3810 return (err); 3886 return (err);
3811 3887
3812 /* 3888 /*
3813 * Pipe isn't running (otherwise err would be USBD_INPROG), 3889 * Pipe isn't running (otherwise err would be USBD_INPROG),
3814 * start first 3890 * start first
3815 */ 3891 */
3816 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 3892 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3817} 3893}
3818 3894
3819/* Start a transfer on the root interrupt pipe */ 3895/* Start a transfer on the root interrupt pipe */
3820usbd_status 3896usbd_status
3821uhci_root_intr_start(usbd_xfer_handle xfer) 3897uhci_root_intr_start(usbd_xfer_handle xfer)

cvs diff -r1.48 -r1.48.12.1 src/sys/dev/usb/uhcivar.h (expand / switch to unified diff)

--- src/sys/dev/usb/uhcivar.h 2010/11/03 22:34:23 1.48
+++ src/sys/dev/usb/uhcivar.h 2011/12/06 05:06:50 1.48.12.1
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: uhcivar.h,v 1.48 2010/11/03 22:34:23 dyoung Exp $ */ 1/* $NetBSD: uhcivar.h,v 1.48.12.1 2011/12/06 05:06:50 mrg Exp $ */
2/* $FreeBSD: src/sys/dev/usb/uhcivar.h,v 1.14 1999/11/17 22:33:42 n_hibma Exp $ */ 2/* $FreeBSD: src/sys/dev/usb/uhcivar.h,v 1.14 1999/11/17 22:33:42 n_hibma Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * This code is derived from software contributed to The NetBSD Foundation 8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at 9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology. 10 * Carlstedt Research & Technology.
11 * 11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 13 * modification, are permitted provided that the following conditions
14 * are met: 14 * are met:
@@ -123,26 +123,30 @@ struct uhci_vframe { @@ -123,26 +123,30 @@ struct uhci_vframe {
123 uhci_soft_td_t *etd; /* pointer to last TD */ 123 uhci_soft_td_t *etd; /* pointer to last TD */
124 uhci_soft_qh_t *hqh; /* pointer to dummy QH */ 124 uhci_soft_qh_t *hqh; /* pointer to dummy QH */
125 uhci_soft_qh_t *eqh; /* pointer to last QH */ 125 uhci_soft_qh_t *eqh; /* pointer to last QH */
126 u_int bandwidth; /* max bandwidth used by this frame */ 126 u_int bandwidth; /* max bandwidth used by this frame */
127}; 127};
128 128
129typedef struct uhci_softc { 129typedef struct uhci_softc {
130 device_t sc_dev; 130 device_t sc_dev;
131 struct usbd_bus sc_bus; 131 struct usbd_bus sc_bus;
132 bus_space_tag_t iot; 132 bus_space_tag_t iot;
133 bus_space_handle_t ioh; 133 bus_space_handle_t ioh;
134 bus_size_t sc_size; 134 bus_size_t sc_size;
135 135
 136 kmutex_t sc_lock;
 137 kmutex_t sc_intr_lock;
 138 kcondvar_t sc_softwake_cv;
 139
136 uhci_physaddr_t *sc_pframes; 140 uhci_physaddr_t *sc_pframes;
137 usb_dma_t sc_dma; 141 usb_dma_t sc_dma;
138 struct uhci_vframe sc_vframes[UHCI_VFRAMELIST_COUNT]; 142 struct uhci_vframe sc_vframes[UHCI_VFRAMELIST_COUNT];
139 143
140 uhci_soft_qh_t *sc_lctl_start; /* dummy QH for low speed control */ 144 uhci_soft_qh_t *sc_lctl_start; /* dummy QH for low speed control */
141 uhci_soft_qh_t *sc_lctl_end; /* last control QH */ 145 uhci_soft_qh_t *sc_lctl_end; /* last control QH */
142 uhci_soft_qh_t *sc_hctl_start; /* dummy QH for high speed control */ 146 uhci_soft_qh_t *sc_hctl_start; /* dummy QH for high speed control */
143 uhci_soft_qh_t *sc_hctl_end; /* last control QH */ 147 uhci_soft_qh_t *sc_hctl_end; /* last control QH */
144 uhci_soft_qh_t *sc_bulk_start; /* dummy QH for bulk */ 148 uhci_soft_qh_t *sc_bulk_start; /* dummy QH for bulk */
145 uhci_soft_qh_t *sc_bulk_end; /* last bulk transfer */ 149 uhci_soft_qh_t *sc_bulk_end; /* last bulk transfer */
146 uhci_soft_qh_t *sc_last_qh; /* dummy QH at the end */ 150 uhci_soft_qh_t *sc_last_qh; /* dummy QH at the end */
147 u_int32_t sc_loops; /* number of QHs that wants looping */ 151 u_int32_t sc_loops; /* number of QHs that wants looping */
148 152