1999-11-28 20:46:29 +00:00
|
|
|
/* $NetBSD: usb.c,v 1.33 1999/11/22 21:57:09 augustss Exp $ */
|
1999-01-13 01:09:14 +00:00
|
|
|
/* $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
|
|
|
|
* by Lennart Augustsson (augustss@carlstedt.se) at
|
|
|
|
* 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>
|
|
|
|
#include <sys/malloc.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-10-07 19:26:38 +00:00
|
|
|
#include <sys/kthread.h>
|
1999-11-08 21:06:21 +00:00
|
|
|
#include <sys/proc.h>
|
1999-01-07 23:07:57 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
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
|
1999-11-17 22:33:51 +00:00
|
|
|
#include <sys/conf.h>
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/select.h>
|
1999-11-17 22:33:51 +00:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/signalvar.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>
|
|
|
|
|
|
|
|
#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;
|
1999-11-17 22:33:51 +00:00
|
|
|
#ifdef UHCI_DEBUG
|
|
|
|
extern int uhcidebug;
|
|
|
|
#endif
|
|
|
|
#ifdef OHCI_DEBUG
|
|
|
|
extern int ohcidebug;
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
/* This part should be deleted when kthreads is available */
|
|
|
|
struct selinfo sc_consel; /* waiting for connect change */
|
|
|
|
#else
|
|
|
|
struct proc *sc_event_thread;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char sc_dying;
|
1998-11-26 23:13:13 +00:00
|
|
|
};
|
|
|
|
|
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__)
|
1998-11-26 23:13:13 +00:00
|
|
|
d_open_t usbopen;
|
|
|
|
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;
|
|
|
|
int usbpoll __P((dev_t, int, struct proc *));
|
|
|
|
|
|
|
|
struct cdevsw usb_cdevsw = {
|
1999-10-07 19:26:38 +00:00
|
|
|
/* open */ usbopen,
|
|
|
|
/* close */ usbclose,
|
1999-11-19 13:13:28 +00:00
|
|
|
/* read */ usbread,
|
1999-10-07 19:26:38 +00:00
|
|
|
/* write */ nowrite,
|
|
|
|
/* ioctl */ usbioctl,
|
|
|
|
/* poll */ usbpoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ nostrategy,
|
|
|
|
/* name */ "usb",
|
|
|
|
/* maj */ USB_CDEV_MAJOR,
|
|
|
|
/* dump */ nodump,
|
|
|
|
/* psize */ nopsize,
|
|
|
|
/* flags */ 0,
|
|
|
|
/* bmaj */ -1
|
1998-11-26 23:13:13 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
static usbd_status usb_discover __P((struct usb_softc *));
|
1999-11-19 13:13:28 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1999-11-17 22:33:51 +00:00
|
|
|
static void usb_create_event_thread __P((void *));
|
|
|
|
static void usb_event_thread __P((void *));
|
1999-11-19 13:13:28 +00:00
|
|
|
#endif
|
1999-11-17 22:33:51 +00:00
|
|
|
|
|
|
|
#define USB_MAX_EVENTS 50
|
|
|
|
struct usb_event_q {
|
|
|
|
struct usb_event ue;
|
|
|
|
SIMPLEQ_ENTRY(usb_event_q) next;
|
|
|
|
};
|
|
|
|
static SIMPLEQ_HEAD(, usb_event_q) usb_events =
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(usb_events);
|
|
|
|
static int usb_nevents = 0;
|
|
|
|
static struct selinfo usb_selevent;
|
|
|
|
static struct proc *usb_async_proc; /* process who wants USB SIGIO */
|
|
|
|
static int usb_dev_open = 0;
|
|
|
|
|
|
|
|
static int usb_get_next_event __P((struct usb_event *));
|
|
|
|
|
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
/* Flag to see if we are in the cold boot process. */
|
|
|
|
extern int cold;
|
|
|
|
#endif
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-28 20:46:29 +00:00
|
|
|
static const char *usbrev_str[] = USBREV_STR;
|
|
|
|
|
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
|
|
|
|
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;
|
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]);
|
|
|
|
if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
|
|
|
|
printf(", not supported\n");
|
|
|
|
USB_ATTACH_ERROR_RETURN;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
|
|
|
|
&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;
|
1999-01-07 23:07:57 +00:00
|
|
|
printf("%s: root device is not a hub\n",
|
|
|
|
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
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (cold) {
|
|
|
|
sc->sc_bus->use_polling++;
|
|
|
|
dev->hub->explore(sc->sc_bus->root_hub);
|
|
|
|
sc->sc_bus->use_polling--;
|
|
|
|
}
|
|
|
|
#endif
|
1998-11-26 23:13:13 +00:00
|
|
|
} else {
|
1999-01-07 23:07:57 +00:00
|
|
|
printf("%s: root hub problem, error=%d\n",
|
1999-11-17 22:33:51 +00:00
|
|
|
USBDEVNAME(sc->sc_dev), err);
|
|
|
|
sc->sc_dying = 1;
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
kthread_create(usb_create_event_thread, sc);
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__)
|
1999-11-28 20:46:29 +00:00
|
|
|
/* The per controller devices (used for usb_discover) */
|
1999-10-07 19:26:38 +00:00
|
|
|
make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
|
1999-11-17 22:33:51 +00:00
|
|
|
0644, "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,
|
|
|
|
0644, "usb");
|
|
|
|
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
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
void
|
|
|
|
usb_create_event_thread(arg)
|
|
|
|
void *arg;
|
|
|
|
{
|
|
|
|
struct usb_softc *sc = arg;
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
|
1999-10-07 19:26:38 +00:00
|
|
|
"%s", sc->sc_dev.dv_xname)) {
|
|
|
|
printf("%s: unable to create event thread for\n",
|
|
|
|
sc->sc_dev.dv_xname);
|
|
|
|
panic("usb_create_event_thread");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usb_event_thread(arg)
|
|
|
|
void *arg;
|
|
|
|
{
|
|
|
|
struct usb_softc *sc = arg;
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
DPRINTF(("usb_event_thread: start\n"));
|
|
|
|
|
|
|
|
while (!sc->sc_dying) {
|
|
|
|
#ifdef USB_DEBUG
|
1999-11-28 20:46:29 +00:00
|
|
|
if (usb_noexplore < 2)
|
1999-11-17 22:33:51 +00:00
|
|
|
#endif
|
|
|
|
usb_discover(sc);
|
1999-11-28 20:46:29 +00:00
|
|
|
(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
|
|
|
|
#ifdef USB_DEBUG
|
|
|
|
usb_noexplore ? 0 :
|
|
|
|
#endif
|
|
|
|
hz*60
|
|
|
|
);
|
1999-10-07 19:26:38 +00:00
|
|
|
DPRINTFN(2,("usb_event_thread: woke up\n"));
|
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
sc->sc_event_thread = 0;
|
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);
|
|
|
|
}
|
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
int
|
|
|
|
usbctlprint(aux, pnp)
|
|
|
|
void *aux;
|
|
|
|
const char *pnp;
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
usbopen(dev, flag, mode, p)
|
|
|
|
dev_t dev;
|
|
|
|
int flag, mode;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
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);
|
1999-11-19 13:13:28 +00:00
|
|
|
} else {
|
|
|
|
USB_GET_SC_OPEN(usb, unit, sc);
|
1999-11-17 22:33:51 +00:00
|
|
|
|
1999-11-19 13:13:28 +00:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return (EIO);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-19 13:13:28 +00:00
|
|
|
return (0);
|
|
|
|
}
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
int
|
|
|
|
usbread(dev, uio, flag)
|
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
|
|
|
int flag;
|
|
|
|
{
|
|
|
|
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
|
|
|
|
usbclose(dev, flag, mode, p)
|
|
|
|
dev_t dev;
|
|
|
|
int flag, mode;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
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
|
1999-10-07 19:26:38 +00:00
|
|
|
usbioctl(devt, cmd, data, flag, p)
|
|
|
|
dev_t devt;
|
1998-11-26 23:13:13 +00:00
|
|
|
u_long cmd;
|
|
|
|
caddr_t data;
|
|
|
|
int flag;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
case FIOASYNC:
|
|
|
|
if (*(int *)data)
|
|
|
|
usb_async_proc = p;
|
|
|
|
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) {
|
1999-11-17 22:33:51 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
/* This part should be deleted when kthreads is available */
|
|
|
|
case USB_DISCOVER:
|
|
|
|
usb_discover(sc);
|
|
|
|
break;
|
|
|
|
#endif
|
1998-11-26 23:13:13 +00:00
|
|
|
#ifdef USB_DEBUG
|
|
|
|
case USB_SETDEBUG:
|
1999-11-17 22:33:51 +00:00
|
|
|
usbdebug = ((*(int *)data) & 0x000000ff);
|
|
|
|
#ifdef UHCI_DEBUG
|
|
|
|
uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
|
|
|
|
#endif
|
|
|
|
#ifdef OHCI_DEBUG
|
|
|
|
ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
|
1998-11-26 23:13:13 +00:00
|
|
|
#endif
|
|
|
|
break;
|
1999-10-07 19:26:38 +00:00
|
|
|
#endif
|
1998-11-26 23:13:13 +00:00
|
|
|
case USB_REQUEST:
|
|
|
|
{
|
|
|
|
struct usb_ctl_request *ur = (void *)data;
|
|
|
|
int len = UGETW(ur->request.wLength);
|
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio;
|
|
|
|
void *ptr = 0;
|
|
|
|
int addr = ur->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);
|
1998-11-26 23:13:13 +00:00
|
|
|
if (addr < 0 || addr >= USB_MAX_DEVICES ||
|
|
|
|
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) {
|
|
|
|
iov.iov_base = (caddr_t)ur->data;
|
|
|
|
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 =
|
|
|
|
ur->request.bmRequestType & UT_READ ?
|
|
|
|
UIO_READ : UIO_WRITE;
|
|
|
|
uio.uio_procp = p;
|
|
|
|
ptr = malloc(len, M_TEMP, M_WAITOK);
|
|
|
|
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],
|
|
|
|
&ur->request, ptr, ur->flags, &ur->actlen);
|
|
|
|
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;
|
|
|
|
int addr = di->addr;
|
|
|
|
usbd_device_handle dev;
|
|
|
|
|
|
|
|
if (addr < 1 || addr >= USB_MAX_DEVICES)
|
|
|
|
return (EINVAL);
|
|
|
|
dev = sc->sc_bus->devices[addr];
|
|
|
|
if (dev == 0)
|
|
|
|
return (ENXIO);
|
1999-01-07 23:07:57 +00:00
|
|
|
usbd_fill_deviceinfo(dev, di);
|
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
|
|
|
|
usbpoll(dev, events, p)
|
|
|
|
dev_t dev;
|
|
|
|
int events;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
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;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
s = splusb();
|
1999-11-21 17:22:30 +00:00
|
|
|
if ((events & mask) && usb_nevents > 0)
|
1999-11-17 22:33:51 +00:00
|
|
|
revents |= events & mask;
|
1999-11-21 17:22:30 +00:00
|
|
|
if (revents == 0 && (events & mask)) {
|
|
|
|
DPRINTF(("usb: sleeping on %p\n", &usb_selevent));
|
1999-11-17 22:33:51 +00:00
|
|
|
selrecord(p, &usb_selevent);
|
1999-11-21 17:22:30 +00:00
|
|
|
}
|
1999-11-17 22:33:51 +00:00
|
|
|
splx(s);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
return (revents);
|
|
|
|
} else {
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
/* This part should be deleted when kthreads is available */
|
|
|
|
struct usb_softc *sc;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
USB_GET_SC(usb, unit, sc);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
revents = 0;
|
|
|
|
mask = POLLOUT | POLLRDNORM;
|
|
|
|
|
|
|
|
s = splusb();
|
1999-11-21 17:22:30 +00:00
|
|
|
if ((events & mask) && sc->sc_bus->needs_explore)
|
1999-11-17 22:33:51 +00:00
|
|
|
revents |= events & mask;
|
1999-11-21 17:22:30 +00:00
|
|
|
if (revents == 0 && (events & mask))
|
1999-11-17 22:33:51 +00:00
|
|
|
selrecord(p, &sc->sc_consel);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return (revents);
|
|
|
|
#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. */
|
1998-11-26 23:13:13 +00:00
|
|
|
usbd_status
|
|
|
|
usb_discover(sc)
|
|
|
|
struct usb_softc *sc;
|
|
|
|
{
|
1999-11-17 22:33:51 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
/* The splxxx parts should be deleted when kthreads is available */
|
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
|
|
|
|
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
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
return (USBD_NORMAL_COMPLETION);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usb_needs_explore(bus)
|
|
|
|
usbd_bus_handle bus;
|
|
|
|
{
|
|
|
|
bus->needs_explore = 1;
|
1999-11-17 22:33:51 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
/* This part should be deleted when kthreads is available */
|
1998-11-26 23:13:13 +00:00
|
|
|
selwakeup(&bus->usbctl->sc_consel);
|
1999-11-17 22:33:51 +00:00
|
|
|
#endif
|
1999-10-07 19:26:38 +00:00
|
|
|
wakeup(&bus->needs_explore);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
/* Called at splusb() */
|
|
|
|
int
|
|
|
|
usb_get_next_event(ue)
|
|
|
|
struct usb_event *ue;
|
|
|
|
{
|
|
|
|
struct usb_event_q *ueq;
|
|
|
|
|
|
|
|
if (usb_nevents <= 0)
|
|
|
|
return (0);
|
|
|
|
ueq = SIMPLEQ_FIRST(&usb_events);
|
|
|
|
*ue = ueq->ue;
|
|
|
|
SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
|
|
|
|
free(ueq, M_USBDEV);
|
|
|
|
usb_nevents--;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usbd_add_event(type, dev)
|
|
|
|
int type;
|
|
|
|
usbd_device_handle dev;
|
|
|
|
{
|
|
|
|
struct usb_event_q *ueq;
|
|
|
|
struct usb_event ue;
|
|
|
|
struct timeval thetime;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
if (++usb_nevents >= USB_MAX_EVENTS) {
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
/* Don't want to wait here inside splusb() */
|
|
|
|
ueq = malloc(sizeof *ueq, M_USBDEV, M_NOWAIT);
|
|
|
|
if (ueq == NULL) {
|
|
|
|
printf("usb: no memory, event dropped\n");
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ueq->ue.ue_type = type;
|
|
|
|
ueq->ue.ue_cookie = dev->cookie;
|
|
|
|
usbd_fill_deviceinfo(dev, &ueq->ue.ue_device);
|
|
|
|
microtime(&thetime);
|
|
|
|
TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
|
|
|
|
SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
|
|
|
|
wakeup(&usb_events);
|
|
|
|
selwakeup(&usb_selevent);
|
|
|
|
if (usb_async_proc != NULL)
|
|
|
|
psignal(usb_async_proc, SIGIO);
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
1999-10-07 19:26:38 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
int
|
|
|
|
usb_activate(self, act)
|
|
|
|
device_ptr_t self;
|
|
|
|
enum devact act;
|
|
|
|
{
|
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);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
if (dev && dev->cdesc && dev->subdevs) {
|
|
|
|
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
|
|
|
|
usb_detach(self, flags)
|
|
|
|
device_ptr_t self;
|
|
|
|
int flags;
|
|
|
|
{
|
1999-11-17 22:33:51 +00:00
|
|
|
struct usb_softc *sc = (struct usb_softc *)self;
|
|
|
|
|
|
|
|
DPRINTF(("usb_detach: start\n"));
|
|
|
|
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
|
|
|
|
/* Make all devices disconnect. */
|
|
|
|
if (sc->sc_port.device)
|
|
|
|
usb_disconnect_port(&sc->sc_port, self);
|
|
|
|
|
|
|
|
/* Kill off event thread. */
|
|
|
|
if (sc->sc_event_thread) {
|
|
|
|
wakeup(&sc->sc_bus->needs_explore);
|
|
|
|
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();
|
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);
|
1998-11-26 23:13:13 +00:00
|
|
|
#endif
|