2003-07-14 20:08:21 +00:00
|
|
|
/* $NetBSD: usb.c,v 1.68 2002/02/20 20:30:12 christos Exp $ */
|
1998-11-26 23:13:13 +00:00
|
|
|
|
2003-07-14 18:25:47 +00:00
|
|
|
/* Also already merged from NetBSD:
|
2003-07-14 20:08:21 +00:00
|
|
|
* $NetBSD: usb.c,v 1.70 2002/05/09 21:54:32 augustss Exp $
|
2003-11-09 23:54:21 +00:00
|
|
|
* $NetBSD: usb.c,v 1.71 2002/06/01 23:51:04 lukem Exp $
|
2003-07-14 18:25:47 +00:00
|
|
|
* $NetBSD: usb.c,v 1.73 2002/09/23 05:51:19 simonb Exp $
|
2004-05-23 14:01:23 +00:00
|
|
|
* $NetBSD: usb.c,v 1.80 2003/11/07 17:03:25 wiz Exp $
|
2003-07-14 18:25:47 +00:00
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2005-01-06 01:43:34 +00:00
|
|
|
/*-
|
1998-11-26 23:13:13 +00:00
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1999-01-07 23:07:57 +00:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2000-05-14 16:43:10 +00:00
|
|
|
* by Lennart Augustsson (lennart@augustsson.net) at
|
1999-01-07 23:07:57 +00:00
|
|
|
* Carlstedt Research & Technology.
|
1998-11-26 23:13:13 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1999-01-07 23:07:57 +00:00
|
|
|
* USB specifications and other documentation can be found at
|
2004-05-23 14:01:23 +00:00
|
|
|
* http://www.usb.org/developers/docs/ and
|
|
|
|
* http://www.usb.org/developers/devclass_docs/
|
1998-11-26 23:13:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/malloc.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/mutex.h>
|
2001-06-06 22:00:03 +00:00
|
|
|
#include <sys/unistd.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
2004-12-22 17:35:52 +00:00
|
|
|
#include <sys/fcntl.h>
|
1999-11-17 22:33:51 +00:00
|
|
|
#include <sys/filio.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/uio.h>
|
2002-01-02 23:31:08 +00:00
|
|
|
#include <sys/kthread.h>
|
2001-03-07 03:37:06 +00:00
|
|
|
#include <sys/proc.h>
|
1999-11-17 22:33:51 +00:00
|
|
|
#include <sys/conf.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/poll.h>
|
2001-01-09 04:33:49 +00:00
|
|
|
#include <sys/selinfo.h>
|
1999-11-17 22:33:51 +00:00
|
|
|
#include <sys/signalvar.h>
|
2002-07-31 13:33:55 +00:00
|
|
|
#include <sys/sysctl.h>
|
2004-12-13 07:08:22 +00:00
|
|
|
#include <sys/uio.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
#include <dev/usb/usb.h>
|
1999-10-07 19:26:38 +00:00
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <dev/usb/usbdi_util.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-19 13:13:28 +00:00
|
|
|
#define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
|
|
|
|
#define USB_DEV_MINOR 255 /* event queue device */
|
1999-11-17 22:33:51 +00:00
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
MALLOC_DEFINE(M_USB, "USB", "USB");
|
|
|
|
MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
|
1999-10-07 19:26:38 +00:00
|
|
|
MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
|
1999-01-07 23:07:57 +00:00
|
|
|
|
|
|
|
#include "usb_if.h"
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
#include <machine/bus.h>
|
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <dev/usb/usbdivar.h>
|
|
|
|
#include <dev/usb/usb_quirks.h>
|
|
|
|
|
2002-07-31 13:33:55 +00:00
|
|
|
/* Define this unconditionally in case a kernel module is loaded that
|
|
|
|
* has been compiled with debugging options.
|
|
|
|
*/
|
2002-08-08 12:05:51 +00:00
|
|
|
SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
|
2002-07-31 13:33:55 +00:00
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
#ifdef USB_DEBUG
|
2007-06-20 05:11:37 +00:00
|
|
|
#define DPRINTF(x) if (usbdebug) printf x
|
|
|
|
#define DPRINTFN(n,x) if (usbdebug>(n)) printf x
|
1999-10-07 19:26:38 +00:00
|
|
|
int usbdebug = 0;
|
2002-08-08 12:05:51 +00:00
|
|
|
SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
|
2002-07-31 13:33:55 +00:00
|
|
|
&usbdebug, 0, "usb debug level");
|
2002-02-03 17:03:34 +00:00
|
|
|
/*
|
1999-11-28 20:46:29 +00:00
|
|
|
* 0 - do usual exploration
|
|
|
|
* 1 - do not use timeout exploration
|
|
|
|
* >1 - do no exploration
|
|
|
|
*/
|
1999-11-17 22:33:51 +00:00
|
|
|
int usb_noexplore = 0;
|
1998-11-26 23:13:13 +00:00
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct usb_softc {
|
2006-09-07 00:06:42 +00:00
|
|
|
device_t sc_dev; /* base device */
|
2004-08-02 15:37:35 +00:00
|
|
|
struct cdev *sc_usbdev; /* /dev/usbN device */
|
2005-03-17 19:41:19 +00:00
|
|
|
TAILQ_ENTRY(usb_softc) sc_coldexplist; /* cold needs-explore list */
|
1998-11-26 23:13:13 +00:00
|
|
|
usbd_bus_handle sc_bus; /* USB controller */
|
|
|
|
struct usbd_port sc_port; /* dummy port for root hub */
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2002-04-07 14:10:52 +00:00
|
|
|
struct proc *sc_event_thread;
|
1999-11-17 22:33:51 +00:00
|
|
|
|
|
|
|
char sc_dying;
|
1998-11-26 23:13:13 +00:00
|
|
|
};
|
|
|
|
|
2006-10-19 01:15:58 +00:00
|
|
|
struct usb_taskq {
|
|
|
|
TAILQ_HEAD(, usb_task) tasks;
|
|
|
|
struct proc *task_thread_proc;
|
|
|
|
const char *name;
|
|
|
|
int taskcreated; /* task thread exists. */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
|
2002-04-07 13:58:06 +00:00
|
|
|
|
2002-02-03 17:03:34 +00:00
|
|
|
d_open_t usbopen;
|
1998-11-26 23:13:13 +00:00
|
|
|
d_close_t usbclose;
|
1999-11-17 22:33:51 +00:00
|
|
|
d_read_t usbread;
|
1998-11-26 23:13:13 +00:00
|
|
|
d_ioctl_t usbioctl;
|
2001-09-12 08:38:13 +00:00
|
|
|
d_poll_t usbpoll;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
struct cdevsw usb_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_flags = D_NEEDGIANT,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = usbopen,
|
|
|
|
.d_close = usbclose,
|
|
|
|
.d_read = usbread,
|
|
|
|
.d_ioctl = usbioctl,
|
|
|
|
.d_poll = usbpoll,
|
|
|
|
.d_name = "usb",
|
1998-11-26 23:13:13 +00:00
|
|
|
};
|
|
|
|
|
2006-09-06 23:44:25 +00:00
|
|
|
static void usb_discover(void *);
|
|
|
|
static void usb_create_event_thread(void *);
|
|
|
|
static void usb_event_thread(void *);
|
|
|
|
static void usb_task_thread(void *);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2006-09-06 23:44:25 +00:00
|
|
|
static struct cdev *usb_dev; /* The /dev/usb device. */
|
|
|
|
static int usb_ndevs; /* Number of /dev/usbN devices. */
|
2005-03-17 19:41:19 +00:00
|
|
|
/* Busses to explore at the end of boot-time device configuration. */
|
2006-09-06 23:44:25 +00:00
|
|
|
static TAILQ_HEAD(, usb_softc) usb_coldexplist =
|
2005-03-17 19:41:19 +00:00
|
|
|
TAILQ_HEAD_INITIALIZER(usb_coldexplist);
|
2004-08-02 15:37:35 +00:00
|
|
|
|
2002-01-26 12:40:03 +00:00
|
|
|
#define USB_MAX_EVENTS 100
|
1999-11-17 22:33:51 +00:00
|
|
|
struct usb_event_q {
|
|
|
|
struct usb_event ue;
|
2001-06-06 22:00:03 +00:00
|
|
|
TAILQ_ENTRY(usb_event_q) next;
|
1999-11-17 22:33:51 +00:00
|
|
|
};
|
2006-09-06 23:44:25 +00:00
|
|
|
static TAILQ_HEAD(, usb_event_q) usb_events =
|
2001-06-06 22:00:03 +00:00
|
|
|
TAILQ_HEAD_INITIALIZER(usb_events);
|
2006-09-06 23:44:25 +00:00
|
|
|
static int usb_nevents = 0;
|
|
|
|
static struct selinfo usb_selevent;
|
|
|
|
static struct proc *usb_async_proc; /* process that wants USB SIGIO */
|
|
|
|
static int usb_dev_open = 0;
|
|
|
|
static void usb_add_event(int, struct usb_event *);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2006-09-06 23:44:25 +00:00
|
|
|
static int usb_get_next_event(struct usb_event *);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2006-09-06 23:44:25 +00:00
|
|
|
static const char *usbrev_str[] = USBREV_STR;
|
1999-11-28 20:46:29 +00:00
|
|
|
|
2007-06-18 22:30:08 +00:00
|
|
|
static device_probe_t usb_match;
|
|
|
|
static device_attach_t usb_attach;
|
|
|
|
static device_detach_t usb_detach;
|
|
|
|
static bus_child_detached_t usb_child_detached;
|
|
|
|
|
|
|
|
static device_method_t usb_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_probe, usb_match),
|
|
|
|
DEVMETHOD(device_attach, usb_attach),
|
|
|
|
DEVMETHOD(device_detach, usb_detach),
|
|
|
|
DEVMETHOD(device_suspend, bus_generic_suspend),
|
|
|
|
DEVMETHOD(device_resume, bus_generic_resume),
|
|
|
|
DEVMETHOD(device_shutdown, bus_generic_shutdown),
|
|
|
|
|
|
|
|
/* Bus interface */
|
|
|
|
DEVMETHOD(bus_child_detached, usb_child_detached),
|
|
|
|
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
1998-11-26 23:13:13 +00:00
|
|
|
|
2007-06-18 22:30:08 +00:00
|
|
|
static driver_t usb_driver = {
|
|
|
|
"usb",
|
|
|
|
usb_methods,
|
|
|
|
sizeof(struct usb_softc)
|
|
|
|
};
|
|
|
|
|
|
|
|
static devclass_t usb_devclass;
|
|
|
|
|
|
|
|
DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
|
|
|
|
DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
|
|
|
|
DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);
|
|
|
|
DRIVER_MODULE(usb, slhci, usb_driver, usb_devclass, 0, 0);
|
2000-08-04 19:05:49 +00:00
|
|
|
MODULE_VERSION(usb, 1);
|
|
|
|
|
2007-06-13 05:37:59 +00:00
|
|
|
static int
|
|
|
|
usb_match(device_t self)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
|
|
|
DPRINTF(("usbd_match\n"));
|
1999-01-07 23:07:57 +00:00
|
|
|
return (UMATCH_GENERIC);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
2007-06-13 05:37:59 +00:00
|
|
|
static int
|
|
|
|
usb_attach(device_t self)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-01-07 23:07:57 +00:00
|
|
|
struct usb_softc *sc = device_get_softc(self);
|
|
|
|
void *aux = device_get_ivars(self);
|
1998-11-26 23:13:13 +00:00
|
|
|
usbd_device_handle dev;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1999-11-28 20:46:29 +00:00
|
|
|
int usbrev;
|
2002-04-07 11:19:05 +00:00
|
|
|
int speed;
|
2002-01-28 01:03:19 +00:00
|
|
|
struct usb_event ue;
|
1999-11-19 13:13:28 +00:00
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
sc->sc_dev = self;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
DPRINTF(("usbd_attach\n"));
|
1999-11-28 20:46:29 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_init();
|
1998-11-26 23:13:13 +00:00
|
|
|
sc->sc_bus = aux;
|
|
|
|
sc->sc_bus->usbctl = sc;
|
|
|
|
sc->sc_port.power = USB_MAX_POWER;
|
1999-11-28 20:46:29 +00:00
|
|
|
|
2006-09-07 00:06:42 +00:00
|
|
|
printf("%s", device_get_nameunit(sc->sc_dev));
|
1999-11-28 20:46:29 +00:00
|
|
|
usbrev = sc->sc_bus->usbrev;
|
|
|
|
printf(": USB revision %s", usbrev_str[usbrev]);
|
2002-04-07 11:19:05 +00:00
|
|
|
switch (usbrev) {
|
|
|
|
case USBREV_1_0:
|
|
|
|
case USBREV_1_1:
|
|
|
|
speed = USB_SPEED_FULL;
|
|
|
|
break;
|
|
|
|
case USBREV_2_0:
|
|
|
|
speed = USB_SPEED_HIGH;
|
|
|
|
break;
|
|
|
|
default:
|
1999-11-28 20:46:29 +00:00
|
|
|
printf(", not supported\n");
|
2002-04-07 11:19:05 +00:00
|
|
|
sc->sc_dying = 1;
|
2007-06-12 15:37:19 +00:00
|
|
|
return ENXIO;
|
1999-11-28 20:46:29 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2002-01-24 20:21:17 +00:00
|
|
|
/* Make sure not to use tsleep() if we are cold booting. */
|
|
|
|
if (cold)
|
|
|
|
sc->sc_bus->use_polling++;
|
|
|
|
|
2006-09-07 00:06:42 +00:00
|
|
|
ue.u.ue_ctrlr.ue_bus = device_get_unit(sc->sc_dev);
|
2002-01-28 01:03:19 +00:00
|
|
|
usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
|
|
|
|
|
2002-04-01 21:34:01 +00:00
|
|
|
#ifdef USB_USE_SOFTINTR
|
|
|
|
#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
|
|
|
|
/* XXX we should have our own level */
|
2002-04-09 12:55:46 +00:00
|
|
|
sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
|
2002-04-01 21:34:01 +00:00
|
|
|
sc->sc_bus->methods->soft_intr, sc->sc_bus);
|
|
|
|
if (sc->sc_bus->soft == NULL) {
|
2006-09-07 00:06:42 +00:00
|
|
|
printf("%s: can't register softintr\n", device_get_nameunit(sc->sc_dev));
|
2002-04-01 21:34:01 +00:00
|
|
|
sc->sc_dying = 1;
|
2007-06-12 15:37:19 +00:00
|
|
|
return ENXIO;
|
2002-04-01 21:34:01 +00:00
|
|
|
}
|
|
|
|
#else
|
2002-05-09 21:08:34 +00:00
|
|
|
usb_callout_init(sc->sc_bus->softi);
|
2002-04-01 21:34:01 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2007-06-12 16:52:07 +00:00
|
|
|
err = usbd_new_device(sc->sc_dev, sc->sc_bus, 0, speed, 0,
|
1999-11-17 22:33:51 +00:00
|
|
|
&sc->sc_port);
|
|
|
|
if (!err) {
|
1998-11-26 23:13:13 +00:00
|
|
|
dev = sc->sc_port.device;
|
1999-11-17 22:33:51 +00:00
|
|
|
if (dev->hub == NULL) {
|
|
|
|
sc->sc_dying = 1;
|
2002-02-03 17:03:34 +00:00
|
|
|
printf("%s: root device is not a hub\n",
|
2006-09-07 00:06:42 +00:00
|
|
|
device_get_nameunit(sc->sc_dev));
|
2007-06-12 15:37:19 +00:00
|
|
|
return ENXIO;
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
sc->sc_bus->root_hub = dev;
|
1999-11-17 22:33:51 +00:00
|
|
|
#if 1
|
2002-02-03 17:03:34 +00:00
|
|
|
/*
|
1999-11-17 22:33:51 +00:00
|
|
|
* Turning this code off will delay attachment of USB devices
|
|
|
|
* until the USB event thread is running, which means that
|
|
|
|
* the keyboard will not work until after cold boot.
|
|
|
|
*/
|
2005-03-27 15:31:23 +00:00
|
|
|
if (cold) {
|
|
|
|
/* Explore high-speed busses before others. */
|
|
|
|
if (speed == USB_SPEED_HIGH)
|
|
|
|
dev->hub->explore(sc->sc_bus->root_hub);
|
|
|
|
else
|
|
|
|
TAILQ_INSERT_TAIL(&usb_coldexplist, sc,
|
|
|
|
sc_coldexplist);
|
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
#endif
|
1998-11-26 23:13:13 +00:00
|
|
|
} else {
|
2002-02-03 17:03:34 +00:00
|
|
|
printf("%s: root hub problem, error=%d\n",
|
2006-09-07 00:06:42 +00:00
|
|
|
device_get_nameunit(sc->sc_dev), err);
|
1999-11-17 22:33:51 +00:00
|
|
|
sc->sc_dying = 1;
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
2002-01-24 20:21:17 +00:00
|
|
|
if (cold)
|
|
|
|
sc->sc_bus->use_polling--;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
2007-06-18 22:30:08 +00:00
|
|
|
/* XXX really do right config_pending_incr(); */
|
2001-06-06 22:00:03 +00:00
|
|
|
usb_create_event_thread(sc);
|
1999-11-28 20:46:29 +00:00
|
|
|
/* The per controller devices (used for usb_discover) */
|
2001-06-06 22:00:03 +00:00
|
|
|
/* XXX This is redundant now, but old usbd's will want it */
|
2004-08-02 15:37:35 +00:00
|
|
|
sc->sc_usbdev = make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT,
|
|
|
|
GID_OPERATOR, 0660, "usb%d", device_get_unit(self));
|
|
|
|
if (usb_ndevs++ == 0) {
|
1999-11-28 20:46:29 +00:00
|
|
|
/* The device spitting out events */
|
2004-08-02 15:37:35 +00:00
|
|
|
usb_dev = make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT,
|
|
|
|
GID_OPERATOR, 0660, "usb");
|
1999-11-19 13:13:28 +00:00
|
|
|
}
|
2007-06-12 15:37:19 +00:00
|
|
|
return 0;
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
2006-10-19 01:15:58 +00:00
|
|
|
static const char *taskq_names[] = USB_TASKQ_NAMES;
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
void
|
2002-01-02 21:56:02 +00:00
|
|
|
usb_create_event_thread(void *arg)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
|
|
|
struct usb_softc *sc = arg;
|
2006-10-19 01:15:58 +00:00
|
|
|
struct usb_taskq *taskq;
|
|
|
|
int i;
|
1999-10-07 19:26:38 +00:00
|
|
|
|
2007-10-20 23:23:23 +00:00
|
|
|
if (kproc_create(usb_event_thread, sc, &sc->sc_event_thread,
|
2007-06-12 18:52:07 +00:00
|
|
|
RFHIGHPID, 0, device_get_nameunit(sc->sc_dev))) {
|
1999-10-07 19:26:38 +00:00
|
|
|
printf("%s: unable to create event thread for\n",
|
2006-09-07 00:06:42 +00:00
|
|
|
device_get_nameunit(sc->sc_dev));
|
1999-10-07 19:26:38 +00:00
|
|
|
panic("usb_create_event_thread");
|
|
|
|
}
|
2006-10-19 01:15:58 +00:00
|
|
|
for (i = 0; i < USB_NUM_TASKQS; i++) {
|
|
|
|
taskq = &usb_taskq[i];
|
|
|
|
|
|
|
|
if (taskq->taskcreated == 0) {
|
|
|
|
taskq->taskcreated = 1;
|
|
|
|
taskq->name = taskq_names[i];
|
|
|
|
TAILQ_INIT(&taskq->tasks);
|
2007-10-20 23:23:23 +00:00
|
|
|
if (kproc_create(usb_task_thread, taskq,
|
2007-06-12 18:52:07 +00:00
|
|
|
&taskq->task_thread_proc, RFHIGHPID, 0,
|
|
|
|
taskq->name)) {
|
2006-10-19 01:15:58 +00:00
|
|
|
printf("unable to create task thread\n");
|
|
|
|
panic("usb_create_event_thread task");
|
|
|
|
}
|
2002-04-07 13:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
1999-10-07 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
2002-04-02 09:51:10 +00:00
|
|
|
/*
|
2002-04-07 14:10:52 +00:00
|
|
|
* Add a task to be performed by the task thread. This function can be
|
2002-04-02 09:51:10 +00:00
|
|
|
* called from any context and the task will be executed in a process
|
|
|
|
* context ASAP.
|
|
|
|
*/
|
2002-04-02 09:49:36 +00:00
|
|
|
void
|
2006-10-19 01:15:58 +00:00
|
|
|
usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
|
2002-04-02 09:49:36 +00:00
|
|
|
{
|
2006-10-19 01:15:58 +00:00
|
|
|
struct usb_taskq *taskq;
|
2002-04-02 09:49:36 +00:00
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splusb();
|
2006-10-19 01:15:58 +00:00
|
|
|
taskq = &usb_taskq[queue];
|
|
|
|
if (task->queue == -1) {
|
2002-04-07 13:58:06 +00:00
|
|
|
DPRINTFN(2,("usb_add_task: task=%p\n", task));
|
2006-10-19 01:15:58 +00:00
|
|
|
TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
|
|
|
|
task->queue = queue;
|
2002-04-07 14:10:52 +00:00
|
|
|
} else {
|
2002-04-07 13:58:06 +00:00
|
|
|
DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
|
2002-04-07 14:10:52 +00:00
|
|
|
}
|
2006-10-19 01:15:58 +00:00
|
|
|
wakeup(&taskq->tasks);
|
2002-04-02 09:49:36 +00:00
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
2002-04-02 09:58:16 +00:00
|
|
|
void
|
|
|
|
usb_rem_task(usbd_device_handle dev, struct usb_task *task)
|
|
|
|
{
|
2006-10-19 01:15:58 +00:00
|
|
|
struct usb_taskq *taskq;
|
2002-04-02 09:58:16 +00:00
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splusb();
|
2006-10-19 01:15:58 +00:00
|
|
|
if (task->queue != -1) {
|
|
|
|
taskq = &usb_taskq[task->queue];
|
|
|
|
TAILQ_REMOVE(&taskq->tasks, task, next);
|
|
|
|
task->queue = -1;
|
2002-04-02 09:58:16 +00:00
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
void
|
2002-01-02 21:56:02 +00:00
|
|
|
usb_event_thread(void *arg)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
2006-05-23 01:27:23 +00:00
|
|
|
static int newthread_wchan;
|
1999-10-07 19:26:38 +00:00
|
|
|
struct usb_softc *sc = arg;
|
2001-06-06 22:00:03 +00:00
|
|
|
|
|
|
|
mtx_lock(&Giant);
|
1999-10-07 19:26:38 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
DPRINTF(("usb_event_thread: start\n"));
|
|
|
|
|
2002-04-07 14:02:31 +00:00
|
|
|
/*
|
|
|
|
* In case this controller is a companion controller to an
|
2002-04-09 12:55:46 +00:00
|
|
|
* EHCI controller we need to wait until the EHCI controller
|
2006-05-23 01:27:23 +00:00
|
|
|
* has grabbed the port. What we do here is wait until no new
|
|
|
|
* USB threads have been created in a while. XXX we actually
|
|
|
|
* just want to wait for the PCI slot to be fully scanned.
|
|
|
|
*
|
|
|
|
* Note that when you `kldload usb' it actually attaches the
|
|
|
|
* devices in order that the drivers appear in the kld, not the
|
|
|
|
* normal PCI order, since the addition of each driver within
|
|
|
|
* usb.ko (ohci, ehci etc.) causes a separate PCI bus re-scan.
|
2002-04-07 14:02:31 +00:00
|
|
|
*/
|
2006-05-23 01:27:23 +00:00
|
|
|
wakeup(&newthread_wchan);
|
|
|
|
for (;;) {
|
|
|
|
if (tsleep(&newthread_wchan , PWAIT, "usbets", hz * 4) != 0)
|
|
|
|
break;
|
|
|
|
}
|
2002-04-07 14:02:31 +00:00
|
|
|
|
2002-04-01 16:29:24 +00:00
|
|
|
/* Make sure first discover does something. */
|
|
|
|
sc->sc_bus->needs_explore = 1;
|
2002-04-02 09:49:36 +00:00
|
|
|
usb_discover(sc);
|
2007-06-18 22:30:08 +00:00
|
|
|
/* XXX really do right config_pending_decr(); */
|
2002-04-01 16:29:24 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
while (!sc->sc_dying) {
|
2002-04-07 13:58:06 +00:00
|
|
|
#ifdef USB_DEBUG
|
|
|
|
if (usb_noexplore < 2)
|
|
|
|
#endif
|
|
|
|
usb_discover(sc);
|
|
|
|
#ifdef USB_DEBUG
|
|
|
|
(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
|
|
|
|
usb_noexplore ? 0 : hz * 60);
|
|
|
|
#else
|
|
|
|
(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
|
|
|
|
hz * 60);
|
|
|
|
#endif
|
|
|
|
DPRINTFN(2,("usb_event_thread: woke up\n"));
|
1999-10-07 19:26:38 +00:00
|
|
|
}
|
2002-01-02 21:56:02 +00:00
|
|
|
sc->sc_event_thread = NULL;
|
1999-10-07 19:26:38 +00:00
|
|
|
|
|
|
|
/* In case parent is waiting for us to exit. */
|
|
|
|
wakeup(sc);
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
DPRINTF(("usb_event_thread: exit\n"));
|
2008-05-20 12:34:30 +00:00
|
|
|
while (mtx_owned(&Giant))
|
|
|
|
mtx_unlock(&Giant);
|
2007-10-20 23:23:23 +00:00
|
|
|
kproc_exit(0);
|
1999-10-07 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
2002-04-07 13:58:06 +00:00
|
|
|
void
|
|
|
|
usb_task_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct usb_task *task;
|
2006-10-19 01:15:58 +00:00
|
|
|
struct usb_taskq *taskq;
|
2002-04-07 13:58:06 +00:00
|
|
|
int s;
|
|
|
|
|
2002-04-08 18:13:21 +00:00
|
|
|
mtx_lock(&Giant);
|
|
|
|
|
2006-10-19 01:15:58 +00:00
|
|
|
taskq = arg;
|
|
|
|
DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
|
2002-04-07 13:58:06 +00:00
|
|
|
|
|
|
|
s = splusb();
|
2004-08-02 15:37:35 +00:00
|
|
|
while (usb_ndevs > 0) {
|
2006-10-19 01:15:58 +00:00
|
|
|
task = TAILQ_FIRST(&taskq->tasks);
|
2002-04-07 13:58:06 +00:00
|
|
|
if (task == NULL) {
|
2006-10-19 01:15:58 +00:00
|
|
|
tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
|
|
|
|
task = TAILQ_FIRST(&taskq->tasks);
|
2002-04-07 13:58:06 +00:00
|
|
|
}
|
|
|
|
DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
|
|
|
|
if (task != NULL) {
|
2006-10-19 01:15:58 +00:00
|
|
|
TAILQ_REMOVE(&taskq->tasks, task, next);
|
|
|
|
task->queue = -1;
|
2002-04-07 13:58:06 +00:00
|
|
|
splx(s);
|
|
|
|
task->fun(task->arg);
|
|
|
|
s = splusb();
|
|
|
|
}
|
|
|
|
}
|
2004-08-02 15:37:35 +00:00
|
|
|
splx(s);
|
|
|
|
|
2006-10-19 01:15:58 +00:00
|
|
|
taskq->taskcreated = 0;
|
|
|
|
wakeup(&taskq->taskcreated);
|
2004-08-02 15:37:35 +00:00
|
|
|
|
|
|
|
DPRINTF(("usb_event_thread: exit\n"));
|
2008-05-20 12:34:30 +00:00
|
|
|
while (mtx_owned(&Giant))
|
|
|
|
mtx_unlock(&Giant);
|
2007-10-20 23:23:23 +00:00
|
|
|
kproc_exit(0);
|
2002-04-07 13:58:06 +00:00
|
|
|
}
|
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
int
|
2007-06-12 17:30:54 +00:00
|
|
|
usbopen(struct cdev *dev, int flag, int mode, struct thread *p)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-11-19 13:13:28 +00:00
|
|
|
int unit = USBUNIT(dev);
|
1999-11-17 22:33:51 +00:00
|
|
|
struct usb_softc *sc;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
if (unit == USB_DEV_MINOR) {
|
|
|
|
if (usb_dev_open)
|
|
|
|
return (EBUSY);
|
|
|
|
usb_dev_open = 1;
|
|
|
|
usb_async_proc = 0;
|
|
|
|
return (0);
|
2002-04-09 12:55:46 +00:00
|
|
|
}
|
2007-06-18 22:30:08 +00:00
|
|
|
sc = devclass_get_softc(usb_devclass, unit);
|
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
2002-04-09 12:55:46 +00:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
return (0);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
int
|
2004-06-16 09:47:26 +00:00
|
|
|
usbread(struct cdev *dev, struct uio *uio, int flag)
|
1999-11-17 22:33:51 +00:00
|
|
|
{
|
|
|
|
struct usb_event ue;
|
1999-11-19 13:13:28 +00:00
|
|
|
int unit = USBUNIT(dev);
|
1999-11-17 22:33:51 +00:00
|
|
|
int s, error, n;
|
|
|
|
|
1999-11-19 13:13:28 +00:00
|
|
|
if (unit != USB_DEV_MINOR)
|
|
|
|
return (ENODEV);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
|
|
|
if (uio->uio_resid != sizeof(struct usb_event))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
s = splusb();
|
|
|
|
for (;;) {
|
|
|
|
n = usb_get_next_event(&ue);
|
|
|
|
if (n != 0)
|
|
|
|
break;
|
2004-12-22 17:35:52 +00:00
|
|
|
if (flag & O_NONBLOCK) {
|
1999-11-17 22:33:51 +00:00
|
|
|
error = EWOULDBLOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
if (!error)
|
|
|
|
error = uiomove((void *)&ue, uio->uio_resid, uio);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
int
|
2007-06-12 17:30:54 +00:00
|
|
|
usbclose(struct cdev *dev, int flag, int mode, struct thread *p)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-11-19 13:13:28 +00:00
|
|
|
int unit = USBUNIT(dev);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
|
|
|
if (unit == USB_DEV_MINOR) {
|
|
|
|
usb_async_proc = 0;
|
|
|
|
usb_dev_open = 0;
|
|
|
|
}
|
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-06-12 17:30:54 +00:00
|
|
|
usbioctl(struct cdev *devt, u_long cmd, caddr_t data, int flag, struct thread *p)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-11-17 22:33:51 +00:00
|
|
|
struct usb_softc *sc;
|
1999-11-19 13:13:28 +00:00
|
|
|
int unit = USBUNIT(devt);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
|
|
|
if (unit == USB_DEV_MINOR) {
|
|
|
|
switch (cmd) {
|
|
|
|
case FIONBIO:
|
|
|
|
/* All handled in the upper FS layer. */
|
|
|
|
return (0);
|
2002-02-03 17:03:34 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
case FIOASYNC:
|
|
|
|
if (*(int *)data)
|
2002-01-02 16:33:53 +00:00
|
|
|
usb_async_proc = p->td_proc;
|
1999-11-17 22:33:51 +00:00
|
|
|
else
|
|
|
|
usb_async_proc = 0;
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
}
|
2007-06-18 22:30:08 +00:00
|
|
|
sc = devclass_get_softc(usb_devclass, unit);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
2001-06-06 22:00:03 +00:00
|
|
|
/* This part should be deleted */
|
1999-11-17 22:33:51 +00:00
|
|
|
case USB_DISCOVER:
|
|
|
|
break;
|
1998-11-26 23:13:13 +00:00
|
|
|
case USB_REQUEST:
|
|
|
|
{
|
|
|
|
struct usb_ctl_request *ur = (void *)data;
|
2002-02-20 20:47:21 +00:00
|
|
|
int len = UGETW(ur->ucr_request.wLength);
|
1998-11-26 23:13:13 +00:00
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio;
|
|
|
|
void *ptr = 0;
|
2002-02-20 20:47:21 +00:00
|
|
|
int addr = ur->ucr_addr;
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_status err;
|
1998-11-26 23:13:13 +00:00
|
|
|
int error = 0;
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
|
1998-11-26 23:13:13 +00:00
|
|
|
if (len < 0 || len > 32768)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
2002-02-03 17:03:34 +00:00
|
|
|
if (addr < 0 || addr >= USB_MAX_DEVICES ||
|
1998-11-26 23:13:13 +00:00
|
|
|
sc->sc_bus->devices[addr] == 0)
|
1999-01-07 23:07:57 +00:00
|
|
|
return (EINVAL);
|
1998-11-26 23:13:13 +00:00
|
|
|
if (len != 0) {
|
2002-02-20 20:47:21 +00:00
|
|
|
iov.iov_base = (caddr_t)ur->ucr_data;
|
1998-11-26 23:13:13 +00:00
|
|
|
iov.iov_len = len;
|
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_resid = len;
|
|
|
|
uio.uio_offset = 0;
|
|
|
|
uio.uio_segflg = UIO_USERSPACE;
|
|
|
|
uio.uio_rw =
|
2002-02-20 20:47:21 +00:00
|
|
|
ur->ucr_request.bmRequestType & UT_READ ?
|
1998-11-26 23:13:13 +00:00
|
|
|
UIO_READ : UIO_WRITE;
|
2006-09-07 00:06:42 +00:00
|
|
|
uio.uio_td = p;
|
2003-02-19 05:47:46 +00:00
|
|
|
ptr = malloc(len, M_TEMP, M_WAITOK);
|
1998-11-26 23:13:13 +00:00
|
|
|
if (uio.uio_rw == UIO_WRITE) {
|
|
|
|
error = uiomove(ptr, len, &uio);
|
|
|
|
if (error)
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_do_request_flags(sc->sc_bus->devices[addr],
|
2002-05-06 18:23:36 +00:00
|
|
|
&ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
|
|
|
|
USBD_DEFAULT_TIMEOUT);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (err) {
|
1998-11-26 23:13:13 +00:00
|
|
|
error = EIO;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if (len != 0) {
|
|
|
|
if (uio.uio_rw == UIO_READ) {
|
|
|
|
error = uiomove(ptr, len, &uio);
|
|
|
|
if (error)
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret:
|
|
|
|
if (ptr)
|
|
|
|
free(ptr, M_TEMP);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
case USB_DEVICEINFO:
|
|
|
|
{
|
|
|
|
struct usb_device_info *di = (void *)data;
|
2002-02-20 20:47:21 +00:00
|
|
|
int addr = di->udi_addr;
|
1998-11-26 23:13:13 +00:00
|
|
|
usbd_device_handle dev;
|
|
|
|
|
|
|
|
if (addr < 1 || addr >= USB_MAX_DEVICES)
|
|
|
|
return (EINVAL);
|
|
|
|
dev = sc->sc_bus->devices[addr];
|
2002-01-02 21:56:02 +00:00
|
|
|
if (dev == NULL)
|
1998-11-26 23:13:13 +00:00
|
|
|
return (ENXIO);
|
2001-07-05 10:12:59 +00:00
|
|
|
usbd_fill_deviceinfo(dev, di, 1);
|
1998-11-26 23:13:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case USB_DEVICESTATS:
|
|
|
|
*(struct usb_device_stats *)data = sc->sc_bus->stats;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-11-17 22:33:51 +00:00
|
|
|
return (EINVAL);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-06-12 17:30:54 +00:00
|
|
|
usbpoll(struct cdev *dev, int events, struct thread *p)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-11-17 22:33:51 +00:00
|
|
|
int revents, mask, s;
|
1999-11-19 13:13:28 +00:00
|
|
|
int unit = USBUNIT(dev);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-19 13:13:28 +00:00
|
|
|
if (unit == USB_DEV_MINOR) {
|
1999-11-17 22:33:51 +00:00
|
|
|
revents = 0;
|
|
|
|
mask = POLLIN | POLLRDNORM;
|
2002-02-03 17:03:34 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
s = splusb();
|
2002-01-02 21:56:02 +00:00
|
|
|
if (events & mask && usb_nevents > 0)
|
1999-11-17 22:33:51 +00:00
|
|
|
revents |= events & mask;
|
2002-01-02 21:56:02 +00:00
|
|
|
if (revents == 0 && events & mask)
|
2002-01-02 16:33:53 +00:00
|
|
|
selrecord(p, &usb_selevent);
|
1999-11-17 22:33:51 +00:00
|
|
|
splx(s);
|
2002-02-03 17:03:34 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
return (revents);
|
|
|
|
} else {
|
2001-06-06 22:00:03 +00:00
|
|
|
return (0); /* select/poll never wakes up - back compat */
|
1999-11-17 22:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
/* Explore device tree from the root. */
|
2006-09-06 23:44:25 +00:00
|
|
|
static void
|
2002-01-02 21:56:02 +00:00
|
|
|
usb_discover(void *v)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
2002-01-02 21:56:02 +00:00
|
|
|
struct usb_softc *sc = v;
|
|
|
|
|
2001-06-06 22:00:03 +00:00
|
|
|
/* splxxx should be changed to mutexes for preemption safety some day */
|
1998-11-26 23:13:13 +00:00
|
|
|
int s;
|
|
|
|
|
2002-04-02 09:49:36 +00:00
|
|
|
DPRINTFN(2,("usb_discover\n"));
|
|
|
|
#ifdef USB_DEBUG
|
|
|
|
if (usb_noexplore > 1)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2002-02-03 17:03:34 +00:00
|
|
|
/*
|
1999-11-17 22:33:51 +00:00
|
|
|
* We need mutual exclusion while traversing the device tree,
|
|
|
|
* but this is guaranteed since this function is only called
|
|
|
|
* from the event thread for the controller.
|
|
|
|
*/
|
|
|
|
s = splusb();
|
|
|
|
while (sc->sc_bus->needs_explore && !sc->sc_dying) {
|
1999-10-07 19:26:38 +00:00
|
|
|
sc->sc_bus->needs_explore = 0;
|
|
|
|
splx(s);
|
|
|
|
sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
|
|
|
|
s = splusb();
|
1999-11-17 22:33:51 +00:00
|
|
|
}
|
|
|
|
splx(s);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-04-02 09:49:36 +00:00
|
|
|
usb_needs_explore(usbd_device_handle dev)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
2002-04-02 09:49:36 +00:00
|
|
|
DPRINTFN(2,("usb_needs_explore\n"));
|
|
|
|
dev->bus->needs_explore = 1;
|
2002-04-07 13:58:06 +00:00
|
|
|
wakeup(&dev->bus->needs_explore);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
/* Called at splusb() */
|
|
|
|
int
|
2002-01-02 21:56:02 +00:00
|
|
|
usb_get_next_event(struct usb_event *ue)
|
1999-11-17 22:33:51 +00:00
|
|
|
{
|
|
|
|
struct usb_event_q *ueq;
|
|
|
|
|
|
|
|
if (usb_nevents <= 0)
|
|
|
|
return (0);
|
2001-06-06 22:00:03 +00:00
|
|
|
ueq = TAILQ_FIRST(&usb_events);
|
2002-04-07 14:12:56 +00:00
|
|
|
#ifdef DIAGNOSTIC
|
2001-06-06 22:00:03 +00:00
|
|
|
if (ueq == NULL) {
|
|
|
|
printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
|
|
|
|
usb_nevents = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
2002-04-07 14:12:56 +00:00
|
|
|
#endif
|
1999-11-17 22:33:51 +00:00
|
|
|
*ue = ueq->ue;
|
2001-06-06 22:00:03 +00:00
|
|
|
TAILQ_REMOVE(&usb_events, ueq, next);
|
1999-11-17 22:33:51 +00:00
|
|
|
free(ueq, M_USBDEV);
|
|
|
|
usb_nevents--;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-01-28 01:03:19 +00:00
|
|
|
usbd_add_dev_event(int type, usbd_device_handle udev)
|
|
|
|
{
|
|
|
|
struct usb_event ue;
|
|
|
|
|
|
|
|
usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
|
|
|
|
usb_add_event(type, &ue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-09-06 23:44:25 +00:00
|
|
|
usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
|
2002-01-28 01:03:19 +00:00
|
|
|
{
|
|
|
|
struct usb_event ue;
|
|
|
|
|
|
|
|
ue.u.ue_driver.ue_cookie = udev->cookie;
|
2006-09-07 00:06:42 +00:00
|
|
|
strncpy(ue.u.ue_driver.ue_devname, device_get_nameunit(dev),
|
2002-01-28 01:03:19 +00:00
|
|
|
sizeof ue.u.ue_driver.ue_devname);
|
|
|
|
usb_add_event(type, &ue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usb_add_event(int type, struct usb_event *uep)
|
1999-11-17 22:33:51 +00:00
|
|
|
{
|
2002-02-14 08:22:37 +00:00
|
|
|
struct usb_event_q *ueq;
|
1999-11-17 22:33:51 +00:00
|
|
|
struct usb_event ue;
|
|
|
|
struct timeval thetime;
|
|
|
|
int s;
|
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
|
2002-01-28 01:03:19 +00:00
|
|
|
ueq->ue = *uep;
|
|
|
|
ueq->ue.ue_type = type;
|
|
|
|
microtime(&thetime);
|
|
|
|
TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
s = splusb();
|
2002-01-28 01:03:19 +00:00
|
|
|
if (USB_EVENT_IS_DETACH(type)) {
|
2002-02-14 08:22:37 +00:00
|
|
|
struct usb_event_q *ueqi, *ueqi_next;
|
|
|
|
|
|
|
|
for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
|
|
|
|
ueqi_next = TAILQ_NEXT(ueqi, next);
|
|
|
|
if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
|
2002-02-20 20:47:21 +00:00
|
|
|
uep->u.ue_device.udi_cookie.cookie) {
|
2002-02-14 08:22:37 +00:00
|
|
|
TAILQ_REMOVE(&usb_events, ueqi, next);
|
|
|
|
free(ueqi, M_USBDEV);
|
2001-06-06 22:00:03 +00:00
|
|
|
usb_nevents--;
|
2002-02-14 08:22:37 +00:00
|
|
|
ueqi_next = TAILQ_FIRST(&usb_events);
|
2001-06-06 22:00:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (usb_nevents >= USB_MAX_EVENTS) {
|
1999-11-17 22:33:51 +00:00
|
|
|
/* Too many queued events, drop an old one. */
|
1999-11-21 17:22:30 +00:00
|
|
|
DPRINTF(("usb: event dropped\n"));
|
1999-11-17 22:33:51 +00:00
|
|
|
(void)usb_get_next_event(&ue);
|
|
|
|
}
|
2001-06-06 22:00:03 +00:00
|
|
|
TAILQ_INSERT_TAIL(&usb_events, ueq, next);
|
|
|
|
usb_nevents++;
|
1999-11-17 22:33:51 +00:00
|
|
|
wakeup(&usb_events);
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&usb_selevent, PZERO);
|
2001-03-07 03:37:06 +00:00
|
|
|
if (usb_async_proc != NULL) {
|
|
|
|
PROC_LOCK(usb_async_proc);
|
1999-11-17 22:33:51 +00:00
|
|
|
psignal(usb_async_proc, SIGIO);
|
2001-03-07 03:37:06 +00:00
|
|
|
PROC_UNLOCK(usb_async_proc);
|
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
2002-01-26 12:48:57 +00:00
|
|
|
void
|
2002-04-01 21:34:01 +00:00
|
|
|
usb_schedsoftintr(usbd_bus_handle bus)
|
2002-01-26 12:48:57 +00:00
|
|
|
{
|
2002-04-02 10:47:01 +00:00
|
|
|
DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
|
2002-04-01 21:34:01 +00:00
|
|
|
#ifdef USB_USE_SOFTINTR
|
|
|
|
if (bus->use_polling) {
|
|
|
|
bus->methods->soft_intr(bus);
|
|
|
|
} else {
|
|
|
|
#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
|
|
|
|
softintr_schedule(bus->soft);
|
|
|
|
#else
|
|
|
|
if (!callout_pending(&bus->softi))
|
|
|
|
callout_reset(&bus->softi, 0, bus->methods->soft_intr,
|
|
|
|
bus);
|
|
|
|
#endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
|
|
|
|
}
|
|
|
|
#else
|
2002-01-26 12:48:57 +00:00
|
|
|
bus->methods->soft_intr(bus);
|
2002-04-02 10:48:58 +00:00
|
|
|
#endif /* USB_USE_SOFTINTR */
|
2002-01-26 12:48:57 +00:00
|
|
|
}
|
|
|
|
|
2007-06-13 05:37:59 +00:00
|
|
|
static int
|
|
|
|
usb_detach(device_t self)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
2007-06-18 22:30:08 +00:00
|
|
|
struct usb_softc *sc = device_get_softc(self);
|
2002-01-28 01:03:19 +00:00
|
|
|
struct usb_event ue;
|
2006-10-19 01:15:58 +00:00
|
|
|
struct usb_taskq *taskq;
|
|
|
|
int i;
|
1999-11-17 22:33:51 +00:00
|
|
|
|
|
|
|
DPRINTF(("usb_detach: start\n"));
|
|
|
|
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
|
|
|
|
/* Make all devices disconnect. */
|
2002-04-07 14:10:52 +00:00
|
|
|
if (sc->sc_port.device != NULL)
|
1999-11-17 22:33:51 +00:00
|
|
|
usb_disconnect_port(&sc->sc_port, self);
|
|
|
|
|
|
|
|
/* Kill off event thread. */
|
2002-04-07 14:10:52 +00:00
|
|
|
if (sc->sc_event_thread != NULL) {
|
2002-04-07 13:58:06 +00:00
|
|
|
wakeup(&sc->sc_bus->needs_explore);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (tsleep(sc, PWAIT, "usbdet", hz * 60))
|
|
|
|
printf("%s: event thread didn't die\n",
|
2006-09-07 00:06:42 +00:00
|
|
|
device_get_nameunit(sc->sc_dev));
|
1999-11-17 22:33:51 +00:00
|
|
|
DPRINTF(("usb_detach: event thread dead\n"));
|
|
|
|
}
|
|
|
|
|
2004-08-02 15:37:35 +00:00
|
|
|
destroy_dev(sc->sc_usbdev);
|
|
|
|
if (--usb_ndevs == 0) {
|
|
|
|
destroy_dev(usb_dev);
|
|
|
|
usb_dev = NULL;
|
2006-10-19 01:15:58 +00:00
|
|
|
for (i = 0; i < USB_NUM_TASKQS; i++) {
|
|
|
|
taskq = &usb_taskq[i];
|
|
|
|
wakeup(&taskq->tasks);
|
|
|
|
if (tsleep(&taskq->taskcreated, PWAIT, "usbtdt",
|
|
|
|
hz * 60)) {
|
|
|
|
printf("usb task thread %s didn't die\n",
|
|
|
|
taskq->name);
|
|
|
|
}
|
|
|
|
}
|
2004-08-02 15:37:35 +00:00
|
|
|
}
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
usbd_finish();
|
2002-01-28 01:03:19 +00:00
|
|
|
|
2002-04-01 21:34:01 +00:00
|
|
|
#ifdef USB_USE_SOFTINTR
|
|
|
|
#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
|
|
|
|
if (sc->sc_bus->soft != NULL) {
|
|
|
|
softintr_disestablish(sc->sc_bus->soft);
|
|
|
|
sc->sc_bus->soft = NULL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
callout_stop(&sc->sc_bus->softi);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-09-07 00:06:42 +00:00
|
|
|
ue.u.ue_ctrlr.ue_bus = device_get_unit(sc->sc_dev);
|
2002-01-28 01:03:19 +00:00
|
|
|
usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2004-08-02 15:37:35 +00:00
|
|
|
|
2006-09-06 23:44:25 +00:00
|
|
|
static void
|
2004-08-02 15:37:35 +00:00
|
|
|
usb_child_detached(device_t self, device_t child)
|
1999-01-07 23:07:57 +00:00
|
|
|
{
|
2004-08-02 15:37:35 +00:00
|
|
|
struct usb_softc *sc = device_get_softc(self);
|
1999-01-07 23:07:57 +00:00
|
|
|
|
2004-08-02 15:37:35 +00:00
|
|
|
/* XXX, should check it is the right device. */
|
|
|
|
sc->sc_port.device = NULL;
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
1999-10-07 19:26:38 +00:00
|
|
|
|
2005-03-27 15:31:23 +00:00
|
|
|
/* Explore USB busses at the end of device configuration. */
|
2006-09-06 23:44:25 +00:00
|
|
|
static void
|
2005-03-17 19:41:19 +00:00
|
|
|
usb_cold_explore(void *arg)
|
|
|
|
{
|
|
|
|
struct usb_softc *sc;
|
|
|
|
|
2005-03-19 19:27:38 +00:00
|
|
|
KASSERT(cold || TAILQ_EMPTY(&usb_coldexplist),
|
|
|
|
("usb_cold_explore: busses to explore when !cold"));
|
2005-03-17 19:41:19 +00:00
|
|
|
while (!TAILQ_EMPTY(&usb_coldexplist)) {
|
|
|
|
sc = TAILQ_FIRST(&usb_coldexplist);
|
|
|
|
TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
|
|
|
|
|
|
|
|
sc->sc_bus->use_polling++;
|
|
|
|
sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
|
|
|
|
sc->sc_bus->use_polling--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
|
|
|
|
usb_cold_explore, NULL);
|