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-07-14 18:25:47 +00:00
|
|
|
* $NetBSD: usb.c,v 1.73 2002/09/23 05:51:19 simonb Exp $
|
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
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
|
|
|
|
* http://www.usb.org/developers/data/ and
|
|
|
|
* http://www.usb.org/developers/index.html .
|
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>
|
1999-10-07 19:26:38 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/device.h>
|
1999-01-07 23:07:57 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
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>
|
1999-11-17 22:33:51 +00:00
|
|
|
#include <sys/filio.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
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
|
|
|
#if __FreeBSD_version >= 500014
|
|
|
|
#include <sys/selinfo.h>
|
|
|
|
#else
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/select.h>
|
2001-01-09 04:33:49 +00:00
|
|
|
#endif
|
1999-11-17 22:33:51 +00:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/signalvar.h>
|
2002-07-31 13:33:55 +00:00
|
|
|
#include <sys/sysctl.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
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
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"
|
|
|
|
#endif /* defined(__FreeBSD__) */
|
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
|
1999-04-11 20:50:33 +00:00
|
|
|
#define DPRINTF(x) if (usbdebug) logprintf x
|
|
|
|
#define DPRINTFN(n,x) if (usbdebug>(n)) logprintf 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 {
|
1999-11-17 22:33:51 +00:00
|
|
|
USBBASEDEVICE sc_dev; /* base device */
|
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
|
|
|
};
|
|
|
|
|
2002-04-07 13:58:06 +00:00
|
|
|
TAILQ_HEAD(, usb_task) usb_all_tasks;
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1999-11-17 22:33:51 +00:00
|
|
|
cdev_decl(usb);
|
1999-01-07 23:07:57 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
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 = {
|
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",
|
|
|
|
.d_maj = USB_CDEV_MAJOR,
|
2002-03-11 16:22:15 +00:00
|
|
|
#if __FreeBSD_version < 500014
|
2002-02-15 22:54:10 +00:00
|
|
|
/* bmaj */ -1
|
|
|
|
#endif
|
1998-11-26 23:13:13 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2002-04-02 09:49:36 +00:00
|
|
|
Static void usb_discover(void *);
|
2000-08-04 19:05:49 +00:00
|
|
|
Static void usb_create_event_thread(void *);
|
|
|
|
Static void usb_event_thread(void *);
|
2002-04-07 13:58:06 +00:00
|
|
|
Static void usb_task_thread(void *);
|
|
|
|
Static struct proc *usb_task_thread_proc = NULL;
|
1999-11-17 22:33:51 +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
|
|
|
};
|
2001-06-06 22:00:03 +00:00
|
|
|
Static TAILQ_HEAD(, usb_event_q) usb_events =
|
|
|
|
TAILQ_HEAD_INITIALIZER(usb_events);
|
2000-04-03 20:58:30 +00:00
|
|
|
Static int usb_nevents = 0;
|
|
|
|
Static struct selinfo usb_selevent;
|
2002-01-02 21:56:02 +00:00
|
|
|
Static struct proc *usb_async_proc; /* process that wants USB SIGIO */
|
2000-04-03 20:58:30 +00:00
|
|
|
Static int usb_dev_open = 0;
|
2002-01-28 01:03:19 +00:00
|
|
|
Static void usb_add_event(int, struct usb_event *);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2000-08-04 19:05:49 +00:00
|
|
|
Static int usb_get_next_event(struct usb_event *);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2000-04-03 20:58:30 +00:00
|
|
|
Static const char *usbrev_str[] = USBREV_STR;
|
1999-11-28 20:46:29 +00:00
|
|
|
|
2000-01-20 22:24:35 +00:00
|
|
|
USB_DECLARE_DRIVER_INIT(usb,
|
|
|
|
DEVMETHOD(device_suspend, bus_generic_suspend),
|
|
|
|
DEVMETHOD(device_resume, bus_generic_resume),
|
|
|
|
DEVMETHOD(device_shutdown, bus_generic_shutdown)
|
|
|
|
);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
2000-08-04 19:05:49 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
MODULE_VERSION(usb, 1);
|
|
|
|
#endif
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
USB_MATCH(usb)
|
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
|
|
|
}
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
USB_ATTACH(usb)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
1999-10-07 19:26:38 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1998-11-26 23:13:13 +00:00
|
|
|
struct usb_softc *sc = (struct usb_softc *)self;
|
1999-01-07 23:07:57 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
struct usb_softc *sc = device_get_softc(self);
|
|
|
|
void *aux = device_get_ivars(self);
|
1999-11-28 20:46:29 +00:00
|
|
|
static int global_init_done = 0;
|
1998-11-26 23:13:13 +00:00
|
|
|
#endif
|
|
|
|
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
|
|
|
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
printf("%s", USBDEVNAME(sc->sc_dev));
|
|
|
|
#endif
|
|
|
|
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;
|
1999-11-28 20:46:29 +00:00
|
|
|
USB_ATTACH_ERROR_RETURN;
|
|
|
|
}
|
|
|
|
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++;
|
|
|
|
|
2002-01-28 01:03:19 +00:00
|
|
|
ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
|
|
|
|
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) {
|
|
|
|
printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
|
|
|
|
sc->sc_dying = 1;
|
2002-04-07 11:19:05 +00:00
|
|
|
USB_ATTACH_ERROR_RETURN;
|
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
|
|
|
|
|
2002-04-07 11:19:05 +00:00
|
|
|
err = usbd_new_device(USBDEV(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",
|
1999-01-07 23:07:57 +00:00
|
|
|
USBDEVNAME(sc->sc_dev));
|
|
|
|
USB_ATTACH_ERROR_RETURN;
|
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.
|
|
|
|
*/
|
2002-01-24 20:21:17 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
if (cold)
|
|
|
|
#else
|
|
|
|
if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
|
|
|
|
#endif
|
1999-11-17 22:33:51 +00:00
|
|
|
dev->hub->explore(sc->sc_bus->root_hub);
|
|
|
|
#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",
|
|
|
|
USBDEVNAME(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
|
|
|
|
2002-01-24 20:21:17 +00:00
|
|
|
config_pending_incr();
|
2001-06-06 22:00:03 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
2002-04-01 16:53:29 +00:00
|
|
|
usb_kthread_create(usb_create_event_thread, sc);
|
2001-06-06 22:00:03 +00:00
|
|
|
#endif
|
1999-10-07 19:26:38 +00:00
|
|
|
|
|
|
|
#if defined(__FreeBSD__)
|
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 */
|
1999-10-07 19:26:38 +00:00
|
|
|
make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
|
2001-02-19 18:14:49 +00:00
|
|
|
0660, "usb%d", device_get_unit(self));
|
1999-11-19 13:13:28 +00:00
|
|
|
if (!global_init_done) {
|
1999-11-28 20:46:29 +00:00
|
|
|
/* The device spitting out events */
|
1999-11-19 13:13:28 +00:00
|
|
|
make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT, GID_OPERATOR,
|
2001-02-19 18:14:49 +00:00
|
|
|
0660, "usb");
|
1999-11-19 13:13:28 +00:00
|
|
|
global_init_done = 1;
|
|
|
|
}
|
1999-10-07 19:26:38 +00:00
|
|
|
#endif
|
|
|
|
|
1999-01-07 23:07:57 +00:00
|
|
|
USB_ATTACH_SUCCESS_RETURN;
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2002-04-07 13:58:06 +00:00
|
|
|
static int created = 0;
|
1999-10-07 19:26:38 +00:00
|
|
|
|
2002-04-01 16:53:29 +00:00
|
|
|
if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
|
2001-06-06 22:00:03 +00:00
|
|
|
"%s", USBDEVNAME(sc->sc_dev))) {
|
1999-10-07 19:26:38 +00:00
|
|
|
printf("%s: unable to create event thread for\n",
|
2001-06-06 22:00:03 +00:00
|
|
|
USBDEVNAME(sc->sc_dev));
|
1999-10-07 19:26:38 +00:00
|
|
|
panic("usb_create_event_thread");
|
|
|
|
}
|
2002-04-07 13:58:06 +00:00
|
|
|
if (!created) {
|
|
|
|
created = 1;
|
|
|
|
TAILQ_INIT(&usb_all_tasks);
|
|
|
|
if (usb_kthread_create2(usb_task_thread, NULL,
|
|
|
|
&usb_task_thread_proc, "usbtask")) {
|
|
|
|
printf("unable to create task thread\n");
|
|
|
|
panic("usb_create_event_thread task");
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
usb_add_task(usbd_device_handle dev, struct usb_task *task)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
if (!task->onqueue) {
|
2002-04-07 13:58:06 +00:00
|
|
|
DPRINTFN(2,("usb_add_task: task=%p\n", task));
|
|
|
|
TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
|
2002-04-02 09:49:36 +00:00
|
|
|
task->onqueue = 1;
|
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
|
|
|
}
|
2002-04-07 13:58:06 +00:00
|
|
|
wakeup(&usb_all_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)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
if (task->onqueue) {
|
2002-04-07 13:58:06 +00:00
|
|
|
TAILQ_REMOVE(&usb_all_tasks, task, next);
|
2002-04-02 09:58:16 +00:00
|
|
|
task->onqueue = 0;
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
struct usb_softc *sc = arg;
|
2001-06-06 22:00:03 +00:00
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
mtx_lock(&Giant);
|
|
|
|
#endif
|
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
|
|
|
|
* has grabbed the port.
|
2002-04-07 14:10:52 +00:00
|
|
|
* XXX It would be nicer to do this with a tsleep(), but I don't
|
|
|
|
* know how to synchronize the creation of the threads so it
|
|
|
|
* will work.
|
2002-04-07 14:02:31 +00:00
|
|
|
*/
|
|
|
|
usb_delay_ms(sc->sc_bus, 500);
|
|
|
|
|
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);
|
|
|
|
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"));
|
1999-10-07 19:26:38 +00:00
|
|
|
kthread_exit(0);
|
|
|
|
}
|
|
|
|
|
2002-04-07 13:58:06 +00:00
|
|
|
void
|
|
|
|
usb_task_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct usb_task *task;
|
|
|
|
int s;
|
|
|
|
|
2002-04-08 18:13:21 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
mtx_lock(&Giant);
|
|
|
|
#endif
|
|
|
|
|
2002-04-07 13:58:06 +00:00
|
|
|
DPRINTF(("usb_task_thread: start\n"));
|
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
for (;;) {
|
|
|
|
task = TAILQ_FIRST(&usb_all_tasks);
|
|
|
|
if (task == NULL) {
|
2002-04-07 13:59:32 +00:00
|
|
|
tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0);
|
2002-04-07 13:58:06 +00:00
|
|
|
task = TAILQ_FIRST(&usb_all_tasks);
|
|
|
|
}
|
|
|
|
DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
|
|
|
|
if (task != NULL) {
|
|
|
|
TAILQ_REMOVE(&usb_all_tasks, task, next);
|
|
|
|
task->onqueue = 0;
|
|
|
|
splx(s);
|
|
|
|
task->fun(task->arg);
|
|
|
|
s = splusb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-06 22:00:03 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1998-11-26 23:13:13 +00:00
|
|
|
int
|
2002-01-02 21:56:02 +00:00
|
|
|
usbctlprint(void *aux, const char *pnp)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
|
|
|
/* only "usb"es can attach to host controllers */
|
|
|
|
if (pnp)
|
|
|
|
printf("usb at %s", pnp);
|
|
|
|
|
|
|
|
return (UNCONF);
|
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
#endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
int
|
2002-01-02 21:56:02 +00:00
|
|
|
usbopen(dev_t dev, int flag, int mode, usb_proc_ptr 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
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
|
2002-04-09 12:55:46 +00:00
|
|
|
USB_GET_SC_OPEN(usb, unit, sc);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
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
|
2002-01-02 21:56:02 +00:00
|
|
|
usbread(dev_t 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;
|
|
|
|
if (flag & IO_NDELAY) {
|
|
|
|
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
|
2002-01-02 21:56:02 +00:00
|
|
|
usbclose(dev_t dev, int flag, int mode, usb_proc_ptr 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
|
2002-01-02 21:56:02 +00:00
|
|
|
usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
USB_GET_SC(usb, unit, sc);
|
|
|
|
|
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
2002-02-03 17:03:34 +00:00
|
|
|
#if defined(__FreeBSD__)
|
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;
|
|
|
|
#endif
|
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;
|
2002-01-02 23:31:08 +00:00
|
|
|
uio.uio_procp = 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
|
2002-01-02 21:56:02 +00:00
|
|
|
usbpoll(dev_t dev, int events, usb_proc_ptr 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 {
|
|
|
|
#if defined(__FreeBSD__)
|
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
|
|
|
#else
|
|
|
|
return (ENXIO);
|
1998-11-26 23:13:13 +00:00
|
|
|
#endif
|
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. */
|
2002-04-02 09:49:36 +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;
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
#if defined(__FreeBSD__)
|
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;
|
1999-11-17 22:33:51 +00:00
|
|
|
#endif
|
1998-11-26 23:13:13 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
s = splusb();
|
|
|
|
#endif
|
|
|
|
while (sc->sc_bus->needs_explore && !sc->sc_dying) {
|
1999-10-07 19:26:38 +00:00
|
|
|
sc->sc_bus->needs_explore = 0;
|
1999-11-17 22:33:51 +00:00
|
|
|
#if defined(__FreeBSD__)
|
1999-10-07 19:26:38 +00:00
|
|
|
splx(s);
|
1999-11-17 22:33:51 +00:00
|
|
|
#endif
|
1999-10-07 19:26:38 +00:00
|
|
|
sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
|
1999-11-17 22:33:51 +00:00
|
|
|
#if defined(__FreeBSD__)
|
1999-10-07 19:26:38 +00:00
|
|
|
s = splusb();
|
1999-11-17 22:33:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
splx(s);
|
|
|
|
#endif
|
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
|
|
|
|
usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
|
|
|
|
{
|
|
|
|
struct usb_event ue;
|
|
|
|
|
|
|
|
ue.u.ue_driver.ue_cookie = udev->cookie;
|
2002-02-03 17:03:34 +00:00
|
|
|
strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(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);
|
|
|
|
selwakeup(&usb_selevent);
|
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
|
|
|
}
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
int
|
2002-01-02 21:56:02 +00:00
|
|
|
usb_activate(device_ptr_t self, enum devact act)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
1999-11-17 22:33:51 +00:00
|
|
|
struct usb_softc *sc = (struct usb_softc *)self;
|
|
|
|
usbd_device_handle dev = sc->sc_port.device;
|
|
|
|
int i, rv = 0;
|
|
|
|
|
|
|
|
switch (act) {
|
|
|
|
case DVACT_ACTIVATE:
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
sc->sc_dying = 1;
|
2002-04-07 14:10:52 +00:00
|
|
|
if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
|
1999-11-17 22:33:51 +00:00
|
|
|
for (i = 0; dev->subdevs[i]; i++)
|
|
|
|
rv |= config_deactivate(dev->subdevs[i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (rv);
|
1999-10-07 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-01-02 21:56:02 +00:00
|
|
|
usb_detach(device_ptr_t self, int flags)
|
1999-10-07 19:26:38 +00:00
|
|
|
{
|
1999-11-17 22:33:51 +00:00
|
|
|
struct usb_softc *sc = (struct usb_softc *)self;
|
2002-01-28 01:03:19 +00:00
|
|
|
struct usb_event ue;
|
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",
|
|
|
|
USBDEVNAME(sc->sc_dev));
|
|
|
|
DPRINTF(("usb_detach: event thread dead\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2002-01-28 01:03:19 +00:00
|
|
|
ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
|
|
|
|
usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#elif defined(__FreeBSD__)
|
1999-01-07 23:07:57 +00:00
|
|
|
int
|
|
|
|
usb_detach(device_t self)
|
|
|
|
{
|
1999-04-20 21:10:43 +00:00
|
|
|
DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
|
1999-01-07 23:07:57 +00:00
|
|
|
|
1999-04-20 21:10:43 +00:00
|
|
|
return (EINVAL);
|
1999-01-07 23:07:57 +00:00
|
|
|
}
|
1999-10-07 19:26:38 +00:00
|
|
|
#endif
|
1999-01-07 23:07:57 +00:00
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
|
|
|
|
#if defined(__FreeBSD__)
|
1999-04-16 21:22:55 +00:00
|
|
|
DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
|
1999-10-07 19:26:38 +00:00
|
|
|
DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
|
2003-04-14 14:04:08 +00:00
|
|
|
DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);
|
1998-11-26 23:13:13 +00:00
|
|
|
#endif
|