Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.39
      1  1.39  augustss /*	$NetBSD: usb.c,v 1.39 2000/02/22 11:30:56 augustss Exp $	*/
      2  1.30  augustss /*	$FreeBSD: src/sys/dev/usb/usb.c,v 1.20 1999/11/17 22:33:46 n_hibma Exp $	*/
      3   1.1  augustss 
      4   1.1  augustss /*
      5   1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6   1.1  augustss  * All rights reserved.
      7   1.1  augustss  *
      8   1.5  augustss  * This code is derived from software contributed to The NetBSD Foundation
      9   1.5  augustss  * by Lennart Augustsson (augustss (at) carlstedt.se) at
     10   1.5  augustss  * Carlstedt Research & Technology.
     11   1.1  augustss  *
     12   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13   1.1  augustss  * modification, are permitted provided that the following conditions
     14   1.1  augustss  * are met:
     15   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20   1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     21   1.1  augustss  *    must display the following acknowledgement:
     22   1.1  augustss  *        This product includes software developed by the NetBSD
     23   1.1  augustss  *        Foundation, Inc. and its contributors.
     24   1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25   1.1  augustss  *    contributors may be used to endorse or promote products derived
     26   1.1  augustss  *    from this software without specific prior written permission.
     27   1.1  augustss  *
     28   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     39   1.1  augustss  */
     40   1.1  augustss 
     41   1.1  augustss /*
     42   1.8  augustss  * USB specifications and other documentation can be found at
     43   1.8  augustss  * http://www.usb.org/developers/data/ and
     44   1.8  augustss  * http://www.usb.org/developers/index.html .
     45   1.1  augustss  */
     46   1.1  augustss 
     47   1.1  augustss #include <sys/param.h>
     48   1.1  augustss #include <sys/systm.h>
     49   1.1  augustss #include <sys/kernel.h>
     50   1.1  augustss #include <sys/malloc.h>
     51  1.17  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
     52   1.1  augustss #include <sys/device.h>
     53  1.13  augustss #include <sys/kthread.h>
     54  1.30  augustss #include <sys/proc.h>
     55   1.7  augustss #elif defined(__FreeBSD__)
     56   1.7  augustss #include <sys/module.h>
     57   1.7  augustss #include <sys/bus.h>
     58  1.30  augustss #include <sys/filio.h>
     59   1.7  augustss #include <sys/uio.h>
     60   1.7  augustss #endif
     61  1.22  augustss #include <sys/conf.h>
     62   1.1  augustss #include <sys/poll.h>
     63   1.1  augustss #include <sys/select.h>
     64  1.26  augustss #include <sys/vnode.h>
     65  1.26  augustss #include <sys/signalvar.h>
     66   1.1  augustss 
     67   1.1  augustss #include <dev/usb/usb.h>
     68  1.13  augustss #include <dev/usb/usbdi.h>
     69  1.13  augustss #include <dev/usb/usbdi_util.h>
     70   1.1  augustss 
     71  1.26  augustss #define USB_DEV_MINOR 255
     72  1.26  augustss 
     73   1.7  augustss #if defined(__FreeBSD__)
     74   1.7  augustss MALLOC_DEFINE(M_USB, "USB", "USB");
     75   1.7  augustss MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
     76  1.13  augustss MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
     77   1.7  augustss 
     78   1.7  augustss #include "usb_if.h"
     79   1.7  augustss #endif /* defined(__FreeBSD__) */
     80  1.20  augustss 
     81  1.20  augustss #include <machine/bus.h>
     82   1.7  augustss 
     83   1.1  augustss #include <dev/usb/usbdivar.h>
     84   1.1  augustss #include <dev/usb/usb_quirks.h>
     85   1.1  augustss 
     86   1.1  augustss #ifdef USB_DEBUG
     87  1.16  augustss #define DPRINTF(x)	if (usbdebug) logprintf x
     88  1.16  augustss #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     89   1.1  augustss int	usbdebug = 0;
     90  1.30  augustss #ifdef UHCI_DEBUG
     91  1.33  augustss int	uhcidebug;
     92  1.30  augustss #endif
     93  1.30  augustss #ifdef OHCI_DEBUG
     94  1.33  augustss int	ohcidebug;
     95  1.30  augustss #endif
     96  1.34  augustss /*
     97  1.34  augustss  * 0  - do usual exploration
     98  1.34  augustss  * 1  - do not use timeout exploration
     99  1.34  augustss  * >1 - do no exploration
    100  1.34  augustss  */
    101  1.21  augustss int	usb_noexplore = 0;
    102   1.1  augustss #else
    103   1.1  augustss #define DPRINTF(x)
    104   1.1  augustss #define DPRINTFN(n,x)
    105   1.1  augustss #endif
    106   1.1  augustss 
    107   1.1  augustss struct usb_softc {
    108  1.22  augustss 	USBBASEDEVICE	sc_dev;		/* base device */
    109   1.1  augustss 	usbd_bus_handle sc_bus;		/* USB controller */
    110   1.1  augustss 	struct usbd_port sc_port;	/* dummy port for root hub */
    111  1.25  augustss 
    112  1.30  augustss #if defined(__FreeBSD__)
    113  1.30  augustss 	/* This part should be deleted when kthreads is available */
    114  1.22  augustss 	struct selinfo	sc_consel;	/* waiting for connect change */
    115  1.30  augustss #else
    116  1.22  augustss 	struct proc    *sc_event_thread;
    117  1.30  augustss #endif
    118  1.25  augustss 
    119  1.25  augustss 	char		sc_dying;
    120   1.1  augustss };
    121   1.1  augustss 
    122  1.17  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    123  1.22  augustss cdev_decl(usb);
    124   1.7  augustss #elif defined(__FreeBSD__)
    125   1.7  augustss d_open_t  usbopen;
    126   1.7  augustss d_close_t usbclose;
    127  1.30  augustss d_read_t usbread;
    128   1.7  augustss d_ioctl_t usbioctl;
    129   1.7  augustss int usbpoll __P((dev_t, int, struct proc *));
    130   1.7  augustss 
    131   1.7  augustss struct cdevsw usb_cdevsw = {
    132  1.28  augustss 	/* open */      usbopen,
    133  1.28  augustss 	/* close */     usbclose,
    134  1.28  augustss 	/* read */      noread,
    135  1.28  augustss 	/* write */     nowrite,
    136  1.28  augustss 	/* ioctl */     usbioctl,
    137  1.28  augustss 	/* poll */      usbpoll,
    138  1.28  augustss 	/* mmap */      nommap,
    139  1.28  augustss 	/* strategy */  nostrategy,
    140  1.28  augustss 	/* name */      "usb",
    141  1.28  augustss 	/* maj */       USB_CDEV_MAJOR,
    142  1.28  augustss 	/* dump */      nodump,
    143  1.28  augustss 	/* psize */     nopsize,
    144  1.28  augustss 	/* flags */     0,
    145  1.28  augustss 	/* bmaj */      -1
    146   1.7  augustss };
    147   1.7  augustss #endif
    148   1.7  augustss 
    149  1.29  augustss static usbd_status usb_discover __P((struct usb_softc *));
    150  1.29  augustss static void	usb_create_event_thread __P((void *));
    151  1.29  augustss static void	usb_event_thread __P((void *));
    152   1.1  augustss 
    153  1.26  augustss #define USB_MAX_EVENTS 50
    154  1.26  augustss struct usb_event_q {
    155  1.26  augustss 	struct usb_event ue;
    156  1.26  augustss 	SIMPLEQ_ENTRY(usb_event_q) next;
    157  1.26  augustss };
    158  1.29  augustss static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    159  1.29  augustss 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    160  1.29  augustss static int usb_nevents = 0;
    161  1.29  augustss static struct selinfo usb_selevent;
    162  1.29  augustss static struct proc *usb_async_proc;  /* process who wants USB SIGIO */
    163  1.29  augustss static int usb_dev_open = 0;
    164  1.38  augustss static void usb_add_event __P((int, struct usb_event *));
    165  1.26  augustss 
    166  1.29  augustss static int usb_get_next_event __P((struct usb_event *));
    167  1.26  augustss 
    168  1.30  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    169  1.23  augustss /* Flag to see if we are in the cold boot process. */
    170  1.23  augustss extern int cold;
    171  1.30  augustss #endif
    172  1.23  augustss 
    173  1.31  augustss static const char *usbrev_str[] = USBREV_STR;
    174  1.31  augustss 
    175  1.28  augustss USB_DECLARE_DRIVER(usb);
    176   1.1  augustss 
    177   1.7  augustss USB_MATCH(usb)
    178   1.1  augustss {
    179   1.1  augustss 	DPRINTF(("usbd_match\n"));
    180   1.7  augustss 	return (UMATCH_GENERIC);
    181   1.1  augustss }
    182   1.1  augustss 
    183   1.7  augustss USB_ATTACH(usb)
    184   1.1  augustss {
    185  1.17  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    186   1.1  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    187   1.7  augustss #elif defined(__FreeBSD__)
    188  1.11  augustss 	struct usb_softc *sc = device_get_softc(self);
    189  1.11  augustss 	void *aux = device_get_ivars(self);
    190   1.7  augustss #endif
    191   1.1  augustss 	usbd_device_handle dev;
    192  1.29  augustss 	usbd_status err;
    193  1.31  augustss 	int usbrev;
    194  1.38  augustss 	struct usb_event ue;
    195   1.1  augustss 
    196  1.32  augustss #if defined(__FreeBSD__)
    197  1.32  augustss 	printf("%s", USBDEVNAME(sc->sc_dev));
    198  1.11  augustss 	sc->sc_dev = self;
    199   1.7  augustss #endif
    200   1.4  augustss 
    201   1.1  augustss 	DPRINTF(("usbd_attach\n"));
    202  1.31  augustss 
    203   1.4  augustss 	usbd_init();
    204   1.1  augustss 	sc->sc_bus = aux;
    205   1.1  augustss 	sc->sc_bus->usbctl = sc;
    206   1.1  augustss 	sc->sc_port.power = USB_MAX_POWER;
    207  1.31  augustss 
    208  1.31  augustss 	usbrev = sc->sc_bus->usbrev;
    209  1.32  augustss 	printf(": USB revision %s", usbrev_str[usbrev]);
    210  1.31  augustss 	if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
    211  1.31  augustss 		printf(", not supported\n");
    212  1.31  augustss 		USB_ATTACH_ERROR_RETURN;
    213  1.32  augustss 	}
    214  1.32  augustss 	printf("\n");
    215  1.31  augustss 
    216  1.35  augustss 	/* Make sure not to use tsleep() if we are cold booting. */
    217  1.35  augustss 	if (cold)
    218  1.35  augustss 		sc->sc_bus->use_polling++;
    219  1.35  augustss 
    220  1.38  augustss 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    221  1.38  augustss 	usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
    222  1.38  augustss 
    223  1.29  augustss 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
    224  1.29  augustss 		  &sc->sc_port);
    225  1.29  augustss 	if (!err) {
    226   1.1  augustss 		dev = sc->sc_port.device;
    227  1.29  augustss 		if (dev->hub == NULL) {
    228  1.22  augustss 			sc->sc_dying = 1;
    229   1.7  augustss 			printf("%s: root device is not a hub\n",
    230   1.7  augustss 			       USBDEVNAME(sc->sc_dev));
    231   1.7  augustss 			USB_ATTACH_ERROR_RETURN;
    232   1.1  augustss 		}
    233   1.1  augustss 		sc->sc_bus->root_hub = dev;
    234  1.24  augustss #if 1
    235  1.24  augustss 		/*
    236  1.24  augustss 		 * Turning this code off will delay attachment of USB devices
    237  1.24  augustss 		 * until the USB event thread is running, which means that
    238  1.24  augustss 		 * the keyboard will not work until after cold boot.
    239  1.24  augustss 		 */
    240  1.36  augustss 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
    241  1.24  augustss 			dev->hub->explore(sc->sc_bus->root_hub);
    242  1.24  augustss #endif
    243   1.1  augustss 	} else {
    244   1.1  augustss 		printf("%s: root hub problem, error=%d\n",
    245  1.29  augustss 		       USBDEVNAME(sc->sc_dev), err);
    246  1.22  augustss 		sc->sc_dying = 1;
    247   1.1  augustss 	}
    248  1.35  augustss 	if (cold)
    249  1.35  augustss 		sc->sc_bus->use_polling--;
    250   1.7  augustss 
    251  1.37   thorpej 	config_pending_incr();
    252  1.14   thorpej 	kthread_create(usb_create_event_thread, sc);
    253  1.28  augustss 
    254  1.28  augustss #if defined(__FreeBSD__)
    255  1.28  augustss 	make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
    256  1.28  augustss 		 0644, "usb%d", device_get_unit(self));
    257  1.28  augustss #endif
    258  1.13  augustss 
    259   1.7  augustss 	USB_ATTACH_SUCCESS_RETURN;
    260   1.1  augustss }
    261   1.1  augustss 
    262  1.28  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    263  1.13  augustss void
    264  1.13  augustss usb_create_event_thread(arg)
    265  1.13  augustss 	void *arg;
    266  1.13  augustss {
    267  1.13  augustss 	struct usb_softc *sc = arg;
    268  1.13  augustss 
    269  1.22  augustss 	if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    270  1.13  augustss 			   "%s", sc->sc_dev.dv_xname)) {
    271  1.13  augustss 		printf("%s: unable to create event thread for\n",
    272  1.13  augustss 		       sc->sc_dev.dv_xname);
    273  1.13  augustss 		panic("usb_create_event_thread");
    274  1.13  augustss 	}
    275  1.13  augustss }
    276  1.13  augustss 
    277  1.13  augustss void
    278  1.13  augustss usb_event_thread(arg)
    279  1.13  augustss 	void *arg;
    280  1.13  augustss {
    281  1.13  augustss 	struct usb_softc *sc = arg;
    282  1.37   thorpej 	int first = 1;
    283  1.13  augustss 
    284  1.27  augustss 	DPRINTF(("usb_event_thread: start\n"));
    285  1.27  augustss 
    286  1.22  augustss 	while (!sc->sc_dying) {
    287  1.21  augustss #ifdef USB_DEBUG
    288  1.34  augustss 		if (usb_noexplore < 2)
    289  1.21  augustss #endif
    290  1.22  augustss 		usb_discover(sc);
    291  1.37   thorpej 		if (first) {
    292  1.37   thorpej 			config_pending_decr();
    293  1.37   thorpej 			first = 0;
    294  1.37   thorpej 		}
    295  1.34  augustss 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    296  1.34  augustss #ifdef USB_DEBUG
    297  1.34  augustss 			     usb_noexplore ? 0 :
    298  1.34  augustss #endif
    299  1.34  augustss 			     hz*60
    300  1.34  augustss 			);
    301  1.13  augustss 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    302  1.13  augustss 	}
    303  1.22  augustss 	sc->sc_event_thread = 0;
    304  1.13  augustss 
    305  1.13  augustss 	/* In case parent is waiting for us to exit. */
    306  1.13  augustss 	wakeup(sc);
    307  1.13  augustss 
    308  1.27  augustss 	DPRINTF(("usb_event_thread: exit\n"));
    309  1.13  augustss 	kthread_exit(0);
    310  1.13  augustss }
    311  1.13  augustss 
    312   1.1  augustss int
    313   1.1  augustss usbctlprint(aux, pnp)
    314   1.1  augustss 	void *aux;
    315   1.1  augustss 	const char *pnp;
    316   1.1  augustss {
    317   1.1  augustss 	/* only "usb"es can attach to host controllers */
    318   1.1  augustss 	if (pnp)
    319   1.1  augustss 		printf("usb at %s", pnp);
    320   1.1  augustss 
    321   1.1  augustss 	return (UNCONF);
    322   1.1  augustss }
    323  1.28  augustss #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    324   1.7  augustss 
    325   1.1  augustss int
    326   1.1  augustss usbopen(dev, flag, mode, p)
    327   1.1  augustss 	dev_t dev;
    328   1.1  augustss 	int flag, mode;
    329   1.1  augustss 	struct proc *p;
    330   1.1  augustss {
    331  1.26  augustss 	int unit = minor(dev);
    332  1.26  augustss 	struct usb_softc *sc;
    333  1.26  augustss 
    334  1.26  augustss 	if (unit == USB_DEV_MINOR) {
    335  1.26  augustss 		if (usb_dev_open)
    336  1.26  augustss 			return (EBUSY);
    337  1.26  augustss 		usb_dev_open = 1;
    338  1.26  augustss 		usb_async_proc = 0;
    339  1.26  augustss 		return (0);
    340  1.26  augustss 	}
    341  1.26  augustss 
    342  1.26  augustss 	USB_GET_SC_OPEN(usb, unit, sc);
    343   1.1  augustss 
    344  1.22  augustss 	if (sc->sc_dying)
    345  1.22  augustss 		return (EIO);
    346   1.1  augustss 
    347   1.1  augustss 	return (0);
    348   1.1  augustss }
    349   1.1  augustss 
    350   1.1  augustss int
    351  1.26  augustss usbread(dev, uio, flag)
    352  1.26  augustss 	dev_t dev;
    353  1.26  augustss 	struct uio *uio;
    354  1.26  augustss 	int flag;
    355  1.26  augustss {
    356  1.26  augustss 	struct usb_event ue;
    357  1.26  augustss 	int s, error, n;
    358  1.26  augustss 
    359  1.26  augustss 	if (minor(dev) != USB_DEV_MINOR)
    360  1.26  augustss 		return (ENXIO);
    361  1.26  augustss 
    362  1.26  augustss 	if (uio->uio_resid != sizeof(struct usb_event))
    363  1.26  augustss 		return (EINVAL);
    364  1.26  augustss 
    365  1.26  augustss 	error = 0;
    366  1.26  augustss 	s = splusb();
    367  1.26  augustss 	for (;;) {
    368  1.26  augustss 		n = usb_get_next_event(&ue);
    369  1.26  augustss 		if (n != 0)
    370  1.26  augustss 			break;
    371  1.26  augustss 		if (flag & IO_NDELAY) {
    372  1.26  augustss 			error = EWOULDBLOCK;
    373  1.26  augustss 			break;
    374  1.26  augustss 		}
    375  1.26  augustss 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    376  1.26  augustss 		if (error)
    377  1.26  augustss 			break;
    378  1.26  augustss 	}
    379  1.26  augustss 	splx(s);
    380  1.26  augustss 	if (!error)
    381  1.30  augustss 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    382  1.26  augustss 
    383  1.26  augustss 	return (error);
    384  1.26  augustss }
    385  1.26  augustss 
    386  1.26  augustss int
    387   1.1  augustss usbclose(dev, flag, mode, p)
    388   1.1  augustss 	dev_t dev;
    389   1.1  augustss 	int flag, mode;
    390   1.1  augustss 	struct proc *p;
    391   1.1  augustss {
    392  1.26  augustss 	int unit = minor(dev);
    393  1.26  augustss 
    394  1.26  augustss 	if (unit == USB_DEV_MINOR) {
    395  1.26  augustss 		usb_async_proc = 0;
    396  1.26  augustss 		usb_dev_open = 0;
    397  1.26  augustss 	}
    398  1.26  augustss 
    399   1.1  augustss 	return (0);
    400   1.1  augustss }
    401   1.1  augustss 
    402   1.1  augustss int
    403  1.28  augustss usbioctl(devt, cmd, data, flag, p)
    404  1.28  augustss 	dev_t devt;
    405   1.1  augustss 	u_long cmd;
    406   1.1  augustss 	caddr_t data;
    407   1.1  augustss 	int flag;
    408   1.1  augustss 	struct proc *p;
    409   1.1  augustss {
    410  1.26  augustss 	struct usb_softc *sc;
    411  1.28  augustss 	int unit = minor(devt);
    412  1.26  augustss 
    413  1.26  augustss 	if (unit == USB_DEV_MINOR) {
    414  1.26  augustss 		switch (cmd) {
    415  1.26  augustss 		case FIONBIO:
    416  1.26  augustss 			/* All handled in the upper FS layer. */
    417  1.26  augustss 			return (0);
    418  1.26  augustss 
    419  1.26  augustss 		case FIOASYNC:
    420  1.26  augustss 			if (*(int *)data)
    421  1.26  augustss 				usb_async_proc = p;
    422  1.26  augustss 			else
    423  1.26  augustss 				usb_async_proc = 0;
    424  1.26  augustss 			return (0);
    425  1.26  augustss 
    426  1.26  augustss 		default:
    427  1.26  augustss 			return (EINVAL);
    428  1.26  augustss 		}
    429  1.26  augustss 	}
    430  1.26  augustss 
    431  1.26  augustss 	USB_GET_SC(usb, unit, sc);
    432   1.1  augustss 
    433  1.22  augustss 	if (sc->sc_dying)
    434  1.22  augustss 		return (EIO);
    435  1.22  augustss 
    436   1.1  augustss 	switch (cmd) {
    437  1.28  augustss #if defined(__FreeBSD__)
    438  1.30  augustss 	/* This part should be deleted when kthreads is available */
    439  1.28  augustss   	case USB_DISCOVER:
    440  1.28  augustss   		usb_discover(sc);
    441  1.28  augustss   		break;
    442  1.28  augustss #endif
    443   1.1  augustss #ifdef USB_DEBUG
    444   1.1  augustss 	case USB_SETDEBUG:
    445  1.30  augustss 		usbdebug  = ((*(int *)data) & 0x000000ff);
    446  1.30  augustss #ifdef UHCI_DEBUG
    447  1.30  augustss 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    448  1.30  augustss #endif
    449  1.30  augustss #ifdef OHCI_DEBUG
    450  1.30  augustss 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    451  1.30  augustss #endif
    452   1.1  augustss 		break;
    453   1.1  augustss #endif
    454   1.1  augustss 	case USB_REQUEST:
    455   1.1  augustss 	{
    456   1.1  augustss 		struct usb_ctl_request *ur = (void *)data;
    457   1.1  augustss 		int len = UGETW(ur->request.wLength);
    458   1.1  augustss 		struct iovec iov;
    459   1.1  augustss 		struct uio uio;
    460   1.1  augustss 		void *ptr = 0;
    461   1.1  augustss 		int addr = ur->addr;
    462  1.29  augustss 		usbd_status err;
    463   1.1  augustss 		int error = 0;
    464   1.1  augustss 
    465   1.9  augustss 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    466   1.1  augustss 		if (len < 0 || len > 32768)
    467   1.9  augustss 			return (EINVAL);
    468   1.1  augustss 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    469   1.1  augustss 		    sc->sc_bus->devices[addr] == 0)
    470   1.9  augustss 			return (EINVAL);
    471   1.1  augustss 		if (len != 0) {
    472   1.1  augustss 			iov.iov_base = (caddr_t)ur->data;
    473   1.1  augustss 			iov.iov_len = len;
    474   1.1  augustss 			uio.uio_iov = &iov;
    475   1.1  augustss 			uio.uio_iovcnt = 1;
    476   1.1  augustss 			uio.uio_resid = len;
    477   1.1  augustss 			uio.uio_offset = 0;
    478   1.1  augustss 			uio.uio_segflg = UIO_USERSPACE;
    479   1.1  augustss 			uio.uio_rw =
    480   1.1  augustss 				ur->request.bmRequestType & UT_READ ?
    481   1.1  augustss 				UIO_READ : UIO_WRITE;
    482   1.1  augustss 			uio.uio_procp = p;
    483   1.1  augustss 			ptr = malloc(len, M_TEMP, M_WAITOK);
    484   1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
    485   1.1  augustss 				error = uiomove(ptr, len, &uio);
    486   1.1  augustss 				if (error)
    487   1.1  augustss 					goto ret;
    488   1.1  augustss 			}
    489   1.1  augustss 		}
    490  1.29  augustss 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    491  1.29  augustss 			  &ur->request, ptr, ur->flags, &ur->actlen);
    492  1.29  augustss 		if (err) {
    493   1.1  augustss 			error = EIO;
    494   1.1  augustss 			goto ret;
    495   1.1  augustss 		}
    496   1.1  augustss 		if (len != 0) {
    497   1.1  augustss 			if (uio.uio_rw == UIO_READ) {
    498   1.1  augustss 				error = uiomove(ptr, len, &uio);
    499   1.1  augustss 				if (error)
    500   1.1  augustss 					goto ret;
    501   1.1  augustss 			}
    502   1.1  augustss 		}
    503   1.1  augustss 	ret:
    504   1.1  augustss 		if (ptr)
    505   1.1  augustss 			free(ptr, M_TEMP);
    506   1.1  augustss 		return (error);
    507   1.1  augustss 	}
    508   1.1  augustss 
    509   1.1  augustss 	case USB_DEVICEINFO:
    510   1.1  augustss 	{
    511   1.1  augustss 		struct usb_device_info *di = (void *)data;
    512   1.1  augustss 		int addr = di->addr;
    513  1.30  augustss 		usbd_device_handle dev;
    514   1.1  augustss 
    515   1.1  augustss 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    516   1.1  augustss 			return (EINVAL);
    517  1.30  augustss 		dev = sc->sc_bus->devices[addr];
    518  1.30  augustss 		if (dev == NULL)
    519   1.1  augustss 			return (ENXIO);
    520  1.30  augustss 		usbd_fill_deviceinfo(dev, di);
    521   1.1  augustss 		break;
    522   1.1  augustss 	}
    523   1.2  augustss 
    524   1.2  augustss 	case USB_DEVICESTATS:
    525   1.2  augustss 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    526   1.2  augustss 		break;
    527   1.1  augustss 
    528   1.1  augustss 	default:
    529  1.26  augustss 		return (EINVAL);
    530   1.1  augustss 	}
    531   1.1  augustss 	return (0);
    532   1.1  augustss }
    533   1.1  augustss 
    534   1.1  augustss int
    535   1.1  augustss usbpoll(dev, events, p)
    536   1.1  augustss 	dev_t dev;
    537   1.1  augustss 	int events;
    538   1.1  augustss 	struct proc *p;
    539   1.1  augustss {
    540  1.26  augustss 	int revents, mask, s;
    541   1.1  augustss 
    542  1.30  augustss 	if (minor(dev) == USB_DEV_MINOR) {
    543  1.30  augustss 		revents = 0;
    544  1.30  augustss 		mask = POLLIN | POLLRDNORM;
    545  1.30  augustss 
    546  1.30  augustss 		s = splusb();
    547  1.30  augustss 		if (events & mask && usb_nevents > 0)
    548  1.30  augustss 			revents |= events & mask;
    549  1.30  augustss 		if (revents == 0 && events & mask)
    550  1.30  augustss 			selrecord(p, &usb_selevent);
    551  1.30  augustss 		splx(s);
    552  1.30  augustss 
    553  1.30  augustss 		return (revents);
    554  1.30  augustss 	} else {
    555  1.30  augustss #if defined(__FreeBSD__)
    556  1.30  augustss 		/* This part should be deleted when kthreads is available */
    557  1.30  augustss 		struct usb_softc *sc;
    558  1.30  augustss 		int unit = minor(dev);
    559  1.30  augustss 
    560  1.30  augustss 		USB_GET_SC(usb, unit, sc);
    561  1.30  augustss 
    562  1.30  augustss 		revents = 0;
    563  1.30  augustss 		mask = POLLOUT | POLLRDNORM;
    564  1.22  augustss 
    565  1.30  augustss 		s = splusb();
    566  1.30  augustss 		if (events & mask && sc->sc_bus->needs_explore)
    567  1.26  augustss 			revents |= events & mask;
    568  1.30  augustss 		if (revents == 0 && events & mask)
    569  1.30  augustss 			selrecord(p, &sc->sc_consel);
    570  1.30  augustss 		splx(s);
    571  1.26  augustss 
    572  1.30  augustss 		return (revents);
    573  1.30  augustss #else
    574  1.30  augustss 		return (ENXIO);
    575  1.30  augustss #endif
    576   1.1  augustss 	}
    577   1.1  augustss }
    578   1.1  augustss 
    579  1.25  augustss /* Explore device tree from the root. */
    580   1.1  augustss usbd_status
    581   1.1  augustss usb_discover(sc)
    582   1.1  augustss 	struct usb_softc *sc;
    583   1.1  augustss {
    584  1.30  augustss #if defined(__FreeBSD__)
    585  1.30  augustss 	/* The splxxx parts should be deleted when kthreads is available */
    586  1.30  augustss 	int s;
    587  1.30  augustss #endif
    588  1.30  augustss 
    589  1.25  augustss 	/*
    590  1.25  augustss 	 * We need mutual exclusion while traversing the device tree,
    591  1.25  augustss 	 * but this is guaranteed since this function is only called
    592  1.25  augustss 	 * from the event thread for the controller.
    593  1.25  augustss 	 */
    594  1.30  augustss #if defined(__FreeBSD__)
    595  1.30  augustss 	s = splusb();
    596  1.30  augustss #endif
    597  1.30  augustss 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    598  1.13  augustss 		sc->sc_bus->needs_explore = 0;
    599  1.30  augustss #if defined(__FreeBSD__)
    600  1.30  augustss 		splx(s);
    601  1.30  augustss #endif
    602  1.13  augustss 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    603  1.30  augustss #if defined(__FreeBSD__)
    604  1.30  augustss 		s = splusb();
    605  1.30  augustss #endif
    606  1.30  augustss 	}
    607  1.30  augustss #if defined(__FreeBSD__)
    608  1.30  augustss 	splx(s);
    609  1.30  augustss #endif
    610  1.30  augustss 
    611  1.13  augustss 	return (USBD_NORMAL_COMPLETION);
    612   1.1  augustss }
    613   1.1  augustss 
    614   1.1  augustss void
    615   1.1  augustss usb_needs_explore(bus)
    616   1.1  augustss 	usbd_bus_handle bus;
    617   1.1  augustss {
    618   1.1  augustss 	bus->needs_explore = 1;
    619  1.30  augustss #if defined(__FreeBSD__)
    620  1.30  augustss 	/* This part should be deleted when kthreads is available */
    621  1.30  augustss 	selwakeup(&bus->usbctl->sc_consel);
    622  1.30  augustss #endif
    623  1.13  augustss 	wakeup(&bus->needs_explore);
    624   1.1  augustss }
    625   1.7  augustss 
    626  1.26  augustss /* Called at splusb() */
    627  1.26  augustss int
    628  1.26  augustss usb_get_next_event(ue)
    629  1.26  augustss 	struct usb_event *ue;
    630  1.26  augustss {
    631  1.26  augustss 	struct usb_event_q *ueq;
    632  1.26  augustss 
    633  1.26  augustss 	if (usb_nevents <= 0)
    634  1.26  augustss 		return (0);
    635  1.26  augustss 	ueq = SIMPLEQ_FIRST(&usb_events);
    636  1.26  augustss 	*ue = ueq->ue;
    637  1.26  augustss 	SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
    638  1.26  augustss 	free(ueq, M_USBDEV);
    639  1.26  augustss 	usb_nevents--;
    640  1.26  augustss 	return (1);
    641  1.26  augustss }
    642  1.26  augustss 
    643  1.26  augustss void
    644  1.38  augustss usbd_add_dev_event(type, udev)
    645  1.38  augustss 	int type;
    646  1.38  augustss 	usbd_device_handle udev;
    647  1.38  augustss {
    648  1.38  augustss 	struct usb_event ue;
    649  1.38  augustss 
    650  1.38  augustss 	usbd_fill_deviceinfo(udev, &ue.u.ue_device);
    651  1.38  augustss 	usb_add_event(type, &ue);
    652  1.38  augustss }
    653  1.38  augustss 
    654  1.38  augustss void
    655  1.38  augustss usbd_add_drv_event(type, udev, dev)
    656  1.38  augustss 	int type;
    657  1.38  augustss 	usbd_device_handle udev;
    658  1.38  augustss 	device_ptr_t dev;
    659  1.38  augustss {
    660  1.38  augustss 	struct usb_event ue;
    661  1.38  augustss 
    662  1.38  augustss 	ue.u.ue_driver.ue_cookie = udev->cookie;
    663  1.38  augustss 	strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
    664  1.38  augustss 	    sizeof ue.u.ue_driver.ue_devname);
    665  1.38  augustss 	usb_add_event(type, &ue);
    666  1.38  augustss }
    667  1.38  augustss 
    668  1.38  augustss static void
    669  1.38  augustss usb_add_event(type, uep)
    670  1.26  augustss 	int type;
    671  1.38  augustss 	struct usb_event *uep;
    672  1.26  augustss {
    673  1.26  augustss 	struct usb_event_q *ueq;
    674  1.26  augustss 	struct usb_event ue;
    675  1.26  augustss 	struct timeval thetime;
    676  1.26  augustss 	int s;
    677  1.26  augustss 
    678  1.38  augustss 	microtime(&thetime);
    679  1.38  augustss 	/* Don't want to wait here inside splusb() */
    680  1.38  augustss 	ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
    681  1.38  augustss 	ueq->ue = *uep;
    682  1.38  augustss 	ueq->ue.ue_type = type;
    683  1.38  augustss 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    684  1.38  augustss 
    685  1.26  augustss 	s = splusb();
    686  1.26  augustss 	if (++usb_nevents >= USB_MAX_EVENTS) {
    687  1.26  augustss 		/* Too many queued events, drop an old one. */
    688  1.26  augustss 		DPRINTFN(-1,("usb: event dropped\n"));
    689  1.26  augustss 		(void)usb_get_next_event(&ue);
    690  1.26  augustss 	}
    691  1.26  augustss 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    692  1.26  augustss 	wakeup(&usb_events);
    693  1.26  augustss 	selwakeup(&usb_selevent);
    694  1.29  augustss 	if (usb_async_proc != NULL)
    695  1.26  augustss 		psignal(usb_async_proc, SIGIO);
    696  1.26  augustss 	splx(s);
    697  1.39  augustss }
    698  1.39  augustss void
    699  1.39  augustss usb_schedsoftintr(bus)
    700  1.39  augustss 	struct usbd_bus *bus;
    701  1.39  augustss {
    702  1.39  augustss 	bus->methods->soft_intr(bus);
    703  1.26  augustss }
    704  1.26  augustss 
    705  1.28  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    706   1.7  augustss int
    707  1.13  augustss usb_activate(self, act)
    708  1.19  augustss 	device_ptr_t self;
    709  1.13  augustss 	enum devact act;
    710   1.7  augustss {
    711  1.22  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    712  1.25  augustss 	usbd_device_handle dev = sc->sc_port.device;
    713  1.25  augustss 	int i, rv = 0;
    714  1.22  augustss 
    715  1.22  augustss 	switch (act) {
    716  1.22  augustss 	case DVACT_ACTIVATE:
    717  1.22  augustss 		return (EOPNOTSUPP);
    718  1.22  augustss 		break;
    719  1.22  augustss 
    720  1.22  augustss 	case DVACT_DEACTIVATE:
    721  1.22  augustss 		sc->sc_dying = 1;
    722  1.25  augustss 		if (dev && dev->cdesc && dev->subdevs) {
    723  1.25  augustss 			for (i = 0; dev->subdevs[i]; i++)
    724  1.25  augustss 				rv |= config_deactivate(dev->subdevs[i]);
    725  1.25  augustss 		}
    726  1.22  augustss 		break;
    727  1.22  augustss 	}
    728  1.22  augustss 	return (rv);
    729  1.13  augustss }
    730   1.7  augustss 
    731  1.13  augustss int
    732  1.13  augustss usb_detach(self, flags)
    733  1.19  augustss 	device_ptr_t self;
    734  1.13  augustss 	int flags;
    735  1.13  augustss {
    736  1.22  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    737  1.38  augustss 	struct usb_event ue;
    738  1.22  augustss 
    739  1.27  augustss 	DPRINTF(("usb_detach: start\n"));
    740  1.27  augustss 
    741  1.22  augustss 	sc->sc_dying = 1;
    742  1.22  augustss 
    743  1.22  augustss 	/* Make all devices disconnect. */
    744  1.25  augustss 	if (sc->sc_port.device)
    745  1.27  augustss 		usb_disconnect_port(&sc->sc_port, self);
    746  1.22  augustss 
    747  1.22  augustss 	/* Kill off event thread. */
    748  1.22  augustss 	if (sc->sc_event_thread) {
    749  1.22  augustss 		wakeup(&sc->sc_bus->needs_explore);
    750  1.22  augustss 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    751  1.22  augustss 			printf("%s: event thread didn't die\n",
    752  1.22  augustss 			       USBDEVNAME(sc->sc_dev));
    753  1.27  augustss 		DPRINTF(("usb_detach: event thread dead\n"));
    754  1.22  augustss 	}
    755  1.22  augustss 
    756  1.26  augustss 	usbd_finish();
    757  1.38  augustss 
    758  1.38  augustss 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    759  1.38  augustss 	usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
    760  1.38  augustss 
    761   1.7  augustss 	return (0);
    762   1.7  augustss }
    763  1.28  augustss #elif defined(__FreeBSD__)
    764  1.28  augustss int
    765  1.28  augustss usb_detach(device_t self)
    766  1.28  augustss {
    767  1.28  augustss 	DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
    768  1.28  augustss 
    769  1.28  augustss 	return (EINVAL);
    770  1.28  augustss }
    771  1.28  augustss #endif
    772  1.28  augustss 
    773   1.7  augustss 
    774  1.13  augustss #if defined(__FreeBSD__)
    775  1.28  augustss DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
    776  1.28  augustss DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
    777   1.7  augustss #endif
    778