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