Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.53.2.9
      1  1.53.2.6   nathanw /*	$NetBSD: usb.c,v 1.53.2.9 2002/12/11 06:38:53 thorpej Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4  1.53.2.3   nathanw  * Copyright (c) 1998, 2002 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.44  augustss  * 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.53.2.1   nathanw #include <sys/cdefs.h>
     47  1.53.2.6   nathanw __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.53.2.9 2002/12/11 06:38:53 thorpej Exp $");
     48  1.53.2.1   nathanw 
     49       1.1  augustss #include <sys/param.h>
     50       1.1  augustss #include <sys/systm.h>
     51       1.1  augustss #include <sys/kernel.h>
     52       1.1  augustss #include <sys/malloc.h>
     53       1.1  augustss #include <sys/device.h>
     54      1.13  augustss #include <sys/kthread.h>
     55      1.30  augustss #include <sys/proc.h>
     56      1.22  augustss #include <sys/conf.h>
     57  1.53.2.5   nathanw #include <sys/fcntl.h>
     58       1.1  augustss #include <sys/poll.h>
     59       1.1  augustss #include <sys/select.h>
     60      1.26  augustss #include <sys/vnode.h>
     61      1.26  augustss #include <sys/signalvar.h>
     62       1.1  augustss 
     63       1.1  augustss #include <dev/usb/usb.h>
     64      1.13  augustss #include <dev/usb/usbdi.h>
     65      1.13  augustss #include <dev/usb/usbdi_util.h>
     66       1.1  augustss 
     67      1.26  augustss #define USB_DEV_MINOR 255
     68      1.26  augustss 
     69      1.20  augustss #include <machine/bus.h>
     70       1.7  augustss 
     71       1.1  augustss #include <dev/usb/usbdivar.h>
     72       1.1  augustss #include <dev/usb/usb_quirks.h>
     73       1.1  augustss 
     74       1.1  augustss #ifdef USB_DEBUG
     75      1.16  augustss #define DPRINTF(x)	if (usbdebug) logprintf x
     76      1.16  augustss #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     77       1.1  augustss int	usbdebug = 0;
     78      1.30  augustss #ifdef UHCI_DEBUG
     79      1.33  augustss int	uhcidebug;
     80      1.30  augustss #endif
     81      1.30  augustss #ifdef OHCI_DEBUG
     82      1.33  augustss int	ohcidebug;
     83      1.30  augustss #endif
     84  1.53.2.4   nathanw /*
     85      1.34  augustss  * 0  - do usual exploration
     86      1.34  augustss  * 1  - do not use timeout exploration
     87      1.34  augustss  * >1 - do no exploration
     88      1.34  augustss  */
     89      1.21  augustss int	usb_noexplore = 0;
     90       1.1  augustss #else
     91       1.1  augustss #define DPRINTF(x)
     92       1.1  augustss #define DPRINTFN(n,x)
     93       1.1  augustss #endif
     94       1.1  augustss 
     95       1.1  augustss struct usb_softc {
     96      1.22  augustss 	USBBASEDEVICE	sc_dev;		/* base device */
     97       1.1  augustss 	usbd_bus_handle sc_bus;		/* USB controller */
     98       1.1  augustss 	struct usbd_port sc_port;	/* dummy port for root hub */
     99      1.25  augustss 
    100  1.53.2.3   nathanw 	struct proc	*sc_event_thread;
    101      1.25  augustss 
    102      1.25  augustss 	char		sc_dying;
    103       1.1  augustss };
    104       1.1  augustss 
    105  1.53.2.2   nathanw TAILQ_HEAD(, usb_task) usb_all_tasks;
    106  1.53.2.2   nathanw 
    107  1.53.2.6   nathanw dev_type_open(usbopen);
    108  1.53.2.6   nathanw dev_type_close(usbclose);
    109  1.53.2.6   nathanw dev_type_read(usbread);
    110  1.53.2.6   nathanw dev_type_ioctl(usbioctl);
    111  1.53.2.6   nathanw dev_type_poll(usbpoll);
    112  1.53.2.8   nathanw dev_type_kqfilter(usbkqfilter);
    113  1.53.2.6   nathanw 
    114  1.53.2.6   nathanw const struct cdevsw usb_cdevsw = {
    115  1.53.2.6   nathanw 	usbopen, usbclose, usbread, nowrite, usbioctl,
    116  1.53.2.8   nathanw 	nostop, notty, usbpoll, nommap, usbkqfilter,
    117  1.53.2.6   nathanw };
    118       1.7  augustss 
    119      1.51  augustss Static void	usb_discover(void *);
    120      1.45  augustss Static void	usb_create_event_thread(void *);
    121      1.45  augustss Static void	usb_event_thread(void *);
    122  1.53.2.2   nathanw Static void	usb_task_thread(void *);
    123  1.53.2.2   nathanw Static struct proc *usb_task_thread_proc = NULL;
    124       1.1  augustss 
    125      1.41  augustss #define USB_MAX_EVENTS 100
    126      1.26  augustss struct usb_event_q {
    127      1.26  augustss 	struct usb_event ue;
    128      1.26  augustss 	SIMPLEQ_ENTRY(usb_event_q) next;
    129      1.26  augustss };
    130  1.53.2.4   nathanw Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    131      1.29  augustss 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    132      1.42  augustss Static int usb_nevents = 0;
    133      1.42  augustss Static struct selinfo usb_selevent;
    134  1.53.2.2   nathanw Static usb_proc_ptr usb_async_proc;  /* process that wants USB SIGIO */
    135      1.42  augustss Static int usb_dev_open = 0;
    136      1.45  augustss Static void usb_add_event(int, struct usb_event *);
    137      1.26  augustss 
    138      1.45  augustss Static int usb_get_next_event(struct usb_event *);
    139      1.23  augustss 
    140      1.42  augustss Static const char *usbrev_str[] = USBREV_STR;
    141      1.31  augustss 
    142      1.28  augustss USB_DECLARE_DRIVER(usb);
    143       1.1  augustss 
    144       1.7  augustss USB_MATCH(usb)
    145       1.1  augustss {
    146       1.1  augustss 	DPRINTF(("usbd_match\n"));
    147       1.7  augustss 	return (UMATCH_GENERIC);
    148       1.1  augustss }
    149       1.1  augustss 
    150       1.7  augustss USB_ATTACH(usb)
    151       1.1  augustss {
    152       1.1  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    153       1.1  augustss 	usbd_device_handle dev;
    154      1.29  augustss 	usbd_status err;
    155      1.31  augustss 	int usbrev;
    156  1.53.2.2   nathanw 	int speed;
    157      1.38  augustss 	struct usb_event ue;
    158  1.53.2.4   nathanw 
    159       1.1  augustss 	DPRINTF(("usbd_attach\n"));
    160      1.31  augustss 
    161       1.4  augustss 	usbd_init();
    162       1.1  augustss 	sc->sc_bus = aux;
    163       1.1  augustss 	sc->sc_bus->usbctl = sc;
    164       1.1  augustss 	sc->sc_port.power = USB_MAX_POWER;
    165      1.31  augustss 
    166      1.31  augustss 	usbrev = sc->sc_bus->usbrev;
    167      1.32  augustss 	printf(": USB revision %s", usbrev_str[usbrev]);
    168  1.53.2.2   nathanw 	switch (usbrev) {
    169  1.53.2.2   nathanw 	case USBREV_1_0:
    170  1.53.2.2   nathanw 	case USBREV_1_1:
    171  1.53.2.2   nathanw 		speed = USB_SPEED_FULL;
    172  1.53.2.2   nathanw 		break;
    173  1.53.2.2   nathanw 	case USBREV_2_0:
    174  1.53.2.2   nathanw 		speed = USB_SPEED_HIGH;
    175  1.53.2.2   nathanw 		break;
    176  1.53.2.2   nathanw 	default:
    177      1.31  augustss 		printf(", not supported\n");
    178  1.53.2.2   nathanw 		sc->sc_dying = 1;
    179      1.31  augustss 		USB_ATTACH_ERROR_RETURN;
    180      1.32  augustss 	}
    181      1.32  augustss 	printf("\n");
    182      1.31  augustss 
    183      1.35  augustss 	/* Make sure not to use tsleep() if we are cold booting. */
    184      1.35  augustss 	if (cold)
    185      1.35  augustss 		sc->sc_bus->use_polling++;
    186      1.35  augustss 
    187      1.38  augustss 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    188      1.38  augustss 	usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
    189      1.38  augustss 
    190      1.49  augustss #ifdef USB_USE_SOFTINTR
    191      1.49  augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    192      1.49  augustss 	/* XXX we should have our own level */
    193  1.53.2.4   nathanw 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
    194      1.49  augustss 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
    195      1.49  augustss 	if (sc->sc_bus->soft == NULL) {
    196      1.49  augustss 		printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
    197      1.49  augustss 		sc->sc_dying = 1;
    198  1.53.2.2   nathanw 		USB_ATTACH_ERROR_RETURN;
    199      1.49  augustss 	}
    200      1.49  augustss #else
    201  1.53.2.5   nathanw 	usb_callout_init(sc->sc_bus->softi);
    202      1.49  augustss #endif
    203      1.49  augustss #endif
    204      1.49  augustss 
    205  1.53.2.2   nathanw 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
    206      1.29  augustss 		  &sc->sc_port);
    207      1.29  augustss 	if (!err) {
    208       1.1  augustss 		dev = sc->sc_port.device;
    209      1.29  augustss 		if (dev->hub == NULL) {
    210      1.22  augustss 			sc->sc_dying = 1;
    211  1.53.2.4   nathanw 			printf("%s: root device is not a hub\n",
    212       1.7  augustss 			       USBDEVNAME(sc->sc_dev));
    213       1.7  augustss 			USB_ATTACH_ERROR_RETURN;
    214       1.1  augustss 		}
    215       1.1  augustss 		sc->sc_bus->root_hub = dev;
    216      1.24  augustss #if 1
    217  1.53.2.4   nathanw 		/*
    218      1.24  augustss 		 * Turning this code off will delay attachment of USB devices
    219      1.24  augustss 		 * until the USB event thread is running, which means that
    220      1.24  augustss 		 * the keyboard will not work until after cold boot.
    221      1.24  augustss 		 */
    222      1.36  augustss 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
    223      1.24  augustss 			dev->hub->explore(sc->sc_bus->root_hub);
    224      1.24  augustss #endif
    225       1.1  augustss 	} else {
    226  1.53.2.4   nathanw 		printf("%s: root hub problem, error=%d\n",
    227  1.53.2.4   nathanw 		       USBDEVNAME(sc->sc_dev), err);
    228      1.22  augustss 		sc->sc_dying = 1;
    229       1.1  augustss 	}
    230      1.35  augustss 	if (cold)
    231      1.35  augustss 		sc->sc_bus->use_polling--;
    232       1.7  augustss 
    233      1.37   thorpej 	config_pending_incr();
    234      1.43  augustss 	usb_kthread_create(usb_create_event_thread, sc);
    235      1.28  augustss 
    236       1.7  augustss 	USB_ATTACH_SUCCESS_RETURN;
    237       1.1  augustss }
    238       1.1  augustss 
    239      1.28  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    240      1.13  augustss void
    241      1.45  augustss usb_create_event_thread(void *arg)
    242      1.13  augustss {
    243      1.13  augustss 	struct usb_softc *sc = arg;
    244  1.53.2.2   nathanw 	static int created = 0;
    245      1.13  augustss 
    246      1.43  augustss 	if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    247      1.13  augustss 			   "%s", sc->sc_dev.dv_xname)) {
    248      1.13  augustss 		printf("%s: unable to create event thread for\n",
    249      1.13  augustss 		       sc->sc_dev.dv_xname);
    250      1.13  augustss 		panic("usb_create_event_thread");
    251      1.13  augustss 	}
    252  1.53.2.2   nathanw 	if (!created) {
    253  1.53.2.2   nathanw 		created = 1;
    254  1.53.2.2   nathanw 		TAILQ_INIT(&usb_all_tasks);
    255  1.53.2.2   nathanw 		if (usb_kthread_create1(usb_task_thread, NULL,
    256  1.53.2.2   nathanw 					&usb_task_thread_proc, "usbtask")) {
    257  1.53.2.2   nathanw 			printf("unable to create task thread\n");
    258  1.53.2.2   nathanw 			panic("usb_create_event_thread task");
    259  1.53.2.2   nathanw 		}
    260  1.53.2.2   nathanw 	}
    261      1.13  augustss }
    262      1.13  augustss 
    263      1.52  augustss /*
    264  1.53.2.3   nathanw  * Add a task to be performed by the task thread.  This function can be
    265      1.52  augustss  * called from any context and the task will be executed in a process
    266      1.52  augustss  * context ASAP.
    267      1.52  augustss  */
    268      1.13  augustss void
    269      1.51  augustss usb_add_task(usbd_device_handle dev, struct usb_task *task)
    270      1.51  augustss {
    271      1.51  augustss 	int s;
    272      1.51  augustss 
    273      1.51  augustss 	s = splusb();
    274      1.51  augustss 	if (!task->onqueue) {
    275  1.53.2.2   nathanw 		DPRINTFN(2,("usb_add_task: task=%p\n", task));
    276  1.53.2.2   nathanw 		TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
    277      1.51  augustss 		task->onqueue = 1;
    278  1.53.2.3   nathanw 	} else {
    279  1.53.2.2   nathanw 		DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
    280  1.53.2.3   nathanw 	}
    281  1.53.2.2   nathanw 	wakeup(&usb_all_tasks);
    282      1.51  augustss 	splx(s);
    283      1.51  augustss }
    284      1.51  augustss 
    285      1.51  augustss void
    286      1.53  augustss usb_rem_task(usbd_device_handle dev, struct usb_task *task)
    287      1.53  augustss {
    288      1.53  augustss 	int s;
    289      1.53  augustss 
    290      1.53  augustss 	s = splusb();
    291      1.53  augustss 	if (task->onqueue) {
    292  1.53.2.2   nathanw 		TAILQ_REMOVE(&usb_all_tasks, task, next);
    293      1.53  augustss 		task->onqueue = 0;
    294      1.53  augustss 	}
    295      1.53  augustss 	splx(s);
    296      1.53  augustss }
    297      1.53  augustss 
    298      1.53  augustss void
    299      1.45  augustss usb_event_thread(void *arg)
    300      1.13  augustss {
    301      1.13  augustss 	struct usb_softc *sc = arg;
    302      1.13  augustss 
    303      1.27  augustss 	DPRINTF(("usb_event_thread: start\n"));
    304      1.40  augustss 
    305  1.53.2.2   nathanw 	/*
    306  1.53.2.2   nathanw 	 * In case this controller is a companion controller to an
    307  1.53.2.2   nathanw 	 * EHCI controller we need to wait until the EHCI controller
    308  1.53.2.2   nathanw 	 * has grabbed the port.
    309  1.53.2.3   nathanw 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    310  1.53.2.3   nathanw 	 * know how to synchronize the creation of the threads so it
    311  1.53.2.3   nathanw 	 * will work.
    312  1.53.2.2   nathanw 	 */
    313  1.53.2.2   nathanw 	usb_delay_ms(sc->sc_bus, 500);
    314  1.53.2.2   nathanw 
    315      1.40  augustss 	/* Make sure first discover does something. */
    316      1.40  augustss 	sc->sc_bus->needs_explore = 1;
    317      1.51  augustss 	usb_discover(sc);
    318      1.51  augustss 	config_pending_decr();
    319      1.27  augustss 
    320      1.22  augustss 	while (!sc->sc_dying) {
    321  1.53.2.2   nathanw #ifdef USB_DEBUG
    322  1.53.2.2   nathanw 		if (usb_noexplore < 2)
    323  1.53.2.2   nathanw #endif
    324  1.53.2.2   nathanw 		usb_discover(sc);
    325  1.53.2.2   nathanw #ifdef USB_DEBUG
    326  1.53.2.2   nathanw 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    327  1.53.2.2   nathanw 		    usb_noexplore ? 0 : hz * 60);
    328  1.53.2.2   nathanw #else
    329  1.53.2.2   nathanw 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    330  1.53.2.2   nathanw 		    hz * 60);
    331  1.53.2.2   nathanw #endif
    332  1.53.2.2   nathanw 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    333      1.13  augustss 	}
    334      1.50  augustss 	sc->sc_event_thread = NULL;
    335      1.13  augustss 
    336      1.13  augustss 	/* In case parent is waiting for us to exit. */
    337      1.13  augustss 	wakeup(sc);
    338      1.13  augustss 
    339      1.27  augustss 	DPRINTF(("usb_event_thread: exit\n"));
    340      1.13  augustss 	kthread_exit(0);
    341      1.13  augustss }
    342      1.13  augustss 
    343  1.53.2.2   nathanw void
    344  1.53.2.2   nathanw usb_task_thread(void *arg)
    345  1.53.2.2   nathanw {
    346  1.53.2.2   nathanw 	struct usb_task *task;
    347  1.53.2.2   nathanw 	int s;
    348  1.53.2.2   nathanw 
    349  1.53.2.2   nathanw 	DPRINTF(("usb_task_thread: start\n"));
    350  1.53.2.2   nathanw 
    351  1.53.2.2   nathanw 	s = splusb();
    352  1.53.2.2   nathanw 	for (;;) {
    353  1.53.2.2   nathanw 		task = TAILQ_FIRST(&usb_all_tasks);
    354  1.53.2.2   nathanw 		if (task == NULL) {
    355  1.53.2.2   nathanw 			tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0);
    356  1.53.2.2   nathanw 			task = TAILQ_FIRST(&usb_all_tasks);
    357  1.53.2.2   nathanw 		}
    358  1.53.2.2   nathanw 		DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
    359  1.53.2.2   nathanw 		if (task != NULL) {
    360  1.53.2.2   nathanw 			TAILQ_REMOVE(&usb_all_tasks, task, next);
    361  1.53.2.2   nathanw 			task->onqueue = 0;
    362  1.53.2.2   nathanw 			splx(s);
    363  1.53.2.2   nathanw 			task->fun(task->arg);
    364  1.53.2.2   nathanw 			s = splusb();
    365  1.53.2.2   nathanw 		}
    366  1.53.2.2   nathanw 	}
    367  1.53.2.2   nathanw }
    368  1.53.2.2   nathanw 
    369       1.1  augustss int
    370      1.45  augustss usbctlprint(void *aux, const char *pnp)
    371       1.1  augustss {
    372       1.1  augustss 	/* only "usb"es can attach to host controllers */
    373       1.1  augustss 	if (pnp)
    374       1.1  augustss 		printf("usb at %s", pnp);
    375       1.1  augustss 
    376       1.1  augustss 	return (UNCONF);
    377       1.1  augustss }
    378      1.28  augustss #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    379       1.7  augustss 
    380       1.1  augustss int
    381  1.53.2.2   nathanw usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    382       1.1  augustss {
    383      1.26  augustss 	int unit = minor(dev);
    384      1.26  augustss 	struct usb_softc *sc;
    385      1.26  augustss 
    386      1.26  augustss 	if (unit == USB_DEV_MINOR) {
    387      1.26  augustss 		if (usb_dev_open)
    388      1.26  augustss 			return (EBUSY);
    389      1.26  augustss 		usb_dev_open = 1;
    390      1.26  augustss 		usb_async_proc = 0;
    391      1.26  augustss 		return (0);
    392      1.26  augustss 	}
    393      1.26  augustss 
    394      1.26  augustss 	USB_GET_SC_OPEN(usb, unit, sc);
    395       1.1  augustss 
    396      1.22  augustss 	if (sc->sc_dying)
    397      1.22  augustss 		return (EIO);
    398       1.1  augustss 
    399       1.1  augustss 	return (0);
    400       1.1  augustss }
    401       1.1  augustss 
    402       1.1  augustss int
    403      1.45  augustss usbread(dev_t dev, struct uio *uio, int flag)
    404      1.26  augustss {
    405      1.26  augustss 	struct usb_event ue;
    406      1.26  augustss 	int s, error, n;
    407      1.26  augustss 
    408      1.26  augustss 	if (minor(dev) != USB_DEV_MINOR)
    409      1.26  augustss 		return (ENXIO);
    410      1.26  augustss 
    411      1.26  augustss 	if (uio->uio_resid != sizeof(struct usb_event))
    412      1.26  augustss 		return (EINVAL);
    413      1.26  augustss 
    414      1.26  augustss 	error = 0;
    415      1.26  augustss 	s = splusb();
    416      1.26  augustss 	for (;;) {
    417      1.26  augustss 		n = usb_get_next_event(&ue);
    418      1.26  augustss 		if (n != 0)
    419      1.26  augustss 			break;
    420      1.26  augustss 		if (flag & IO_NDELAY) {
    421      1.26  augustss 			error = EWOULDBLOCK;
    422      1.26  augustss 			break;
    423      1.26  augustss 		}
    424      1.26  augustss 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    425      1.26  augustss 		if (error)
    426      1.26  augustss 			break;
    427      1.26  augustss 	}
    428      1.26  augustss 	splx(s);
    429      1.26  augustss 	if (!error)
    430      1.30  augustss 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    431      1.26  augustss 
    432      1.26  augustss 	return (error);
    433      1.26  augustss }
    434      1.26  augustss 
    435      1.26  augustss int
    436  1.53.2.2   nathanw usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    437       1.1  augustss {
    438      1.26  augustss 	int unit = minor(dev);
    439      1.26  augustss 
    440      1.26  augustss 	if (unit == USB_DEV_MINOR) {
    441      1.26  augustss 		usb_async_proc = 0;
    442      1.26  augustss 		usb_dev_open = 0;
    443      1.26  augustss 	}
    444      1.26  augustss 
    445       1.1  augustss 	return (0);
    446       1.1  augustss }
    447       1.1  augustss 
    448       1.1  augustss int
    449  1.53.2.2   nathanw usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
    450       1.1  augustss {
    451      1.26  augustss 	struct usb_softc *sc;
    452      1.28  augustss 	int unit = minor(devt);
    453      1.26  augustss 
    454      1.26  augustss 	if (unit == USB_DEV_MINOR) {
    455      1.26  augustss 		switch (cmd) {
    456      1.26  augustss 		case FIONBIO:
    457      1.26  augustss 			/* All handled in the upper FS layer. */
    458      1.26  augustss 			return (0);
    459  1.53.2.4   nathanw 
    460      1.26  augustss 		case FIOASYNC:
    461      1.26  augustss 			if (*(int *)data)
    462      1.26  augustss 				usb_async_proc = p;
    463      1.26  augustss 			else
    464      1.26  augustss 				usb_async_proc = 0;
    465      1.26  augustss 			return (0);
    466      1.26  augustss 
    467      1.26  augustss 		default:
    468      1.26  augustss 			return (EINVAL);
    469      1.26  augustss 		}
    470      1.26  augustss 	}
    471      1.26  augustss 
    472      1.26  augustss 	USB_GET_SC(usb, unit, sc);
    473       1.1  augustss 
    474      1.22  augustss 	if (sc->sc_dying)
    475      1.22  augustss 		return (EIO);
    476      1.22  augustss 
    477       1.1  augustss 	switch (cmd) {
    478       1.1  augustss #ifdef USB_DEBUG
    479       1.1  augustss 	case USB_SETDEBUG:
    480  1.53.2.5   nathanw 		if (!(flag & FWRITE))
    481  1.53.2.5   nathanw 			return (EBADF);
    482      1.30  augustss 		usbdebug  = ((*(int *)data) & 0x000000ff);
    483      1.30  augustss #ifdef UHCI_DEBUG
    484      1.30  augustss 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    485      1.30  augustss #endif
    486      1.30  augustss #ifdef OHCI_DEBUG
    487      1.30  augustss 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    488      1.30  augustss #endif
    489       1.1  augustss 		break;
    490  1.53.2.1   nathanw #endif /* USB_DEBUG */
    491       1.1  augustss 	case USB_REQUEST:
    492       1.1  augustss 	{
    493       1.1  augustss 		struct usb_ctl_request *ur = (void *)data;
    494  1.53.2.4   nathanw 		int len = UGETW(ur->ucr_request.wLength);
    495       1.1  augustss 		struct iovec iov;
    496       1.1  augustss 		struct uio uio;
    497       1.1  augustss 		void *ptr = 0;
    498  1.53.2.4   nathanw 		int addr = ur->ucr_addr;
    499      1.29  augustss 		usbd_status err;
    500       1.1  augustss 		int error = 0;
    501       1.1  augustss 
    502  1.53.2.5   nathanw 		if (!(flag & FWRITE))
    503  1.53.2.5   nathanw 			return (EBADF);
    504  1.53.2.5   nathanw 
    505       1.9  augustss 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    506       1.1  augustss 		if (len < 0 || len > 32768)
    507       1.9  augustss 			return (EINVAL);
    508  1.53.2.4   nathanw 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    509       1.1  augustss 		    sc->sc_bus->devices[addr] == 0)
    510       1.9  augustss 			return (EINVAL);
    511       1.1  augustss 		if (len != 0) {
    512  1.53.2.4   nathanw 			iov.iov_base = (caddr_t)ur->ucr_data;
    513       1.1  augustss 			iov.iov_len = len;
    514       1.1  augustss 			uio.uio_iov = &iov;
    515       1.1  augustss 			uio.uio_iovcnt = 1;
    516       1.1  augustss 			uio.uio_resid = len;
    517       1.1  augustss 			uio.uio_offset = 0;
    518       1.1  augustss 			uio.uio_segflg = UIO_USERSPACE;
    519       1.1  augustss 			uio.uio_rw =
    520  1.53.2.4   nathanw 				ur->ucr_request.bmRequestType & UT_READ ?
    521       1.1  augustss 				UIO_READ : UIO_WRITE;
    522       1.1  augustss 			uio.uio_procp = p;
    523       1.1  augustss 			ptr = malloc(len, M_TEMP, M_WAITOK);
    524       1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
    525       1.1  augustss 				error = uiomove(ptr, len, &uio);
    526       1.1  augustss 				if (error)
    527       1.1  augustss 					goto ret;
    528       1.1  augustss 			}
    529       1.1  augustss 		}
    530      1.29  augustss 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    531  1.53.2.4   nathanw 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    532  1.53.2.4   nathanw 			  USBD_DEFAULT_TIMEOUT);
    533      1.29  augustss 		if (err) {
    534       1.1  augustss 			error = EIO;
    535       1.1  augustss 			goto ret;
    536       1.1  augustss 		}
    537       1.1  augustss 		if (len != 0) {
    538       1.1  augustss 			if (uio.uio_rw == UIO_READ) {
    539       1.1  augustss 				error = uiomove(ptr, len, &uio);
    540       1.1  augustss 				if (error)
    541       1.1  augustss 					goto ret;
    542       1.1  augustss 			}
    543       1.1  augustss 		}
    544       1.1  augustss 	ret:
    545       1.1  augustss 		if (ptr)
    546       1.1  augustss 			free(ptr, M_TEMP);
    547       1.1  augustss 		return (error);
    548       1.1  augustss 	}
    549       1.1  augustss 
    550       1.1  augustss 	case USB_DEVICEINFO:
    551       1.1  augustss 	{
    552       1.1  augustss 		struct usb_device_info *di = (void *)data;
    553  1.53.2.4   nathanw 		int addr = di->udi_addr;
    554      1.30  augustss 		usbd_device_handle dev;
    555       1.1  augustss 
    556       1.1  augustss 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    557       1.1  augustss 			return (EINVAL);
    558      1.30  augustss 		dev = sc->sc_bus->devices[addr];
    559      1.30  augustss 		if (dev == NULL)
    560       1.1  augustss 			return (ENXIO);
    561      1.48  augustss 		usbd_fill_deviceinfo(dev, di, 1);
    562       1.1  augustss 		break;
    563       1.1  augustss 	}
    564       1.2  augustss 
    565       1.2  augustss 	case USB_DEVICESTATS:
    566       1.2  augustss 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    567       1.2  augustss 		break;
    568       1.1  augustss 
    569       1.1  augustss 	default:
    570      1.26  augustss 		return (EINVAL);
    571       1.1  augustss 	}
    572       1.1  augustss 	return (0);
    573       1.1  augustss }
    574       1.1  augustss 
    575       1.1  augustss int
    576  1.53.2.2   nathanw usbpoll(dev_t dev, int events, usb_proc_ptr p)
    577       1.1  augustss {
    578      1.26  augustss 	int revents, mask, s;
    579       1.1  augustss 
    580      1.30  augustss 	if (minor(dev) == USB_DEV_MINOR) {
    581      1.30  augustss 		revents = 0;
    582      1.30  augustss 		mask = POLLIN | POLLRDNORM;
    583  1.53.2.4   nathanw 
    584      1.30  augustss 		s = splusb();
    585      1.30  augustss 		if (events & mask && usb_nevents > 0)
    586      1.30  augustss 			revents |= events & mask;
    587      1.30  augustss 		if (revents == 0 && events & mask)
    588      1.30  augustss 			selrecord(p, &usb_selevent);
    589      1.30  augustss 		splx(s);
    590  1.53.2.4   nathanw 
    591      1.30  augustss 		return (revents);
    592      1.30  augustss 	} else {
    593      1.30  augustss 		return (ENXIO);
    594       1.1  augustss 	}
    595       1.1  augustss }
    596       1.1  augustss 
    597  1.53.2.8   nathanw static void
    598  1.53.2.8   nathanw filt_usbrdetach(struct knote *kn)
    599  1.53.2.8   nathanw {
    600  1.53.2.8   nathanw 	int s;
    601  1.53.2.8   nathanw 
    602  1.53.2.8   nathanw 	s = splusb();
    603  1.53.2.9   thorpej 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    604  1.53.2.8   nathanw 	splx(s);
    605  1.53.2.8   nathanw }
    606  1.53.2.8   nathanw 
    607  1.53.2.8   nathanw static int
    608  1.53.2.8   nathanw filt_usbread(struct knote *kn, long hint)
    609  1.53.2.8   nathanw {
    610  1.53.2.8   nathanw 
    611  1.53.2.8   nathanw 	if (usb_nevents == 0)
    612  1.53.2.8   nathanw 		return (0);
    613  1.53.2.8   nathanw 
    614  1.53.2.8   nathanw 	kn->kn_data = sizeof(struct usb_event);
    615  1.53.2.8   nathanw 	return (1);
    616  1.53.2.8   nathanw }
    617  1.53.2.8   nathanw 
    618  1.53.2.8   nathanw static const struct filterops usbread_filtops =
    619  1.53.2.8   nathanw 	{ 1, NULL, filt_usbrdetach, filt_usbread };
    620  1.53.2.8   nathanw 
    621  1.53.2.8   nathanw int
    622  1.53.2.8   nathanw usbkqfilter(dev_t dev, struct knote *kn)
    623  1.53.2.8   nathanw {
    624  1.53.2.8   nathanw 	struct klist *klist;
    625  1.53.2.8   nathanw 	int s;
    626  1.53.2.8   nathanw 
    627  1.53.2.8   nathanw 	switch (kn->kn_filter) {
    628  1.53.2.8   nathanw 	case EVFILT_READ:
    629  1.53.2.8   nathanw 		if (minor(dev) != USB_DEV_MINOR)
    630  1.53.2.8   nathanw 			return (1);
    631  1.53.2.9   thorpej 		klist = &usb_selevent.sel_klist;
    632  1.53.2.8   nathanw 		kn->kn_fop = &usbread_filtops;
    633  1.53.2.8   nathanw 		break;
    634  1.53.2.8   nathanw 
    635  1.53.2.8   nathanw 	default:
    636  1.53.2.8   nathanw 		return (1);
    637  1.53.2.8   nathanw 	}
    638  1.53.2.8   nathanw 
    639  1.53.2.8   nathanw 	kn->kn_hook = NULL;
    640  1.53.2.8   nathanw 
    641  1.53.2.8   nathanw 	s = splusb();
    642  1.53.2.8   nathanw 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    643  1.53.2.8   nathanw 	splx(s);
    644  1.53.2.8   nathanw 
    645  1.53.2.8   nathanw 	return (0);
    646  1.53.2.8   nathanw }
    647  1.53.2.8   nathanw 
    648      1.25  augustss /* Explore device tree from the root. */
    649      1.51  augustss Static void
    650      1.51  augustss usb_discover(void *v)
    651       1.1  augustss {
    652      1.51  augustss 	struct usb_softc *sc = v;
    653      1.51  augustss 
    654      1.51  augustss 	DPRINTFN(2,("usb_discover\n"));
    655      1.51  augustss #ifdef USB_DEBUG
    656      1.51  augustss 	if (usb_noexplore > 1)
    657      1.51  augustss 		return;
    658      1.51  augustss #endif
    659  1.53.2.4   nathanw 	/*
    660      1.25  augustss 	 * We need mutual exclusion while traversing the device tree,
    661      1.25  augustss 	 * but this is guaranteed since this function is only called
    662      1.25  augustss 	 * from the event thread for the controller.
    663      1.25  augustss 	 */
    664      1.30  augustss 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    665      1.13  augustss 		sc->sc_bus->needs_explore = 0;
    666      1.13  augustss 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    667      1.30  augustss 	}
    668       1.1  augustss }
    669       1.1  augustss 
    670       1.1  augustss void
    671      1.51  augustss usb_needs_explore(usbd_device_handle dev)
    672       1.1  augustss {
    673      1.51  augustss 	DPRINTFN(2,("usb_needs_explore\n"));
    674      1.51  augustss 	dev->bus->needs_explore = 1;
    675  1.53.2.2   nathanw 	wakeup(&dev->bus->needs_explore);
    676       1.1  augustss }
    677       1.7  augustss 
    678      1.26  augustss /* Called at splusb() */
    679      1.26  augustss int
    680      1.45  augustss usb_get_next_event(struct usb_event *ue)
    681      1.26  augustss {
    682      1.26  augustss 	struct usb_event_q *ueq;
    683      1.26  augustss 
    684      1.26  augustss 	if (usb_nevents <= 0)
    685      1.26  augustss 		return (0);
    686      1.26  augustss 	ueq = SIMPLEQ_FIRST(&usb_events);
    687  1.53.2.3   nathanw #ifdef DIAGNOSTIC
    688  1.53.2.3   nathanw 	if (ueq == NULL) {
    689  1.53.2.3   nathanw 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
    690  1.53.2.3   nathanw 		usb_nevents = 0;
    691  1.53.2.3   nathanw 		return (0);
    692  1.53.2.3   nathanw 	}
    693  1.53.2.3   nathanw #endif
    694      1.26  augustss 	*ue = ueq->ue;
    695  1.53.2.5   nathanw 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
    696      1.26  augustss 	free(ueq, M_USBDEV);
    697      1.26  augustss 	usb_nevents--;
    698      1.26  augustss 	return (1);
    699      1.26  augustss }
    700      1.26  augustss 
    701      1.26  augustss void
    702      1.45  augustss usbd_add_dev_event(int type, usbd_device_handle udev)
    703      1.38  augustss {
    704      1.38  augustss 	struct usb_event ue;
    705      1.38  augustss 
    706      1.48  augustss 	usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
    707      1.38  augustss 	usb_add_event(type, &ue);
    708      1.38  augustss }
    709      1.38  augustss 
    710      1.38  augustss void
    711      1.45  augustss usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
    712      1.38  augustss {
    713      1.38  augustss 	struct usb_event ue;
    714      1.38  augustss 
    715      1.38  augustss 	ue.u.ue_driver.ue_cookie = udev->cookie;
    716  1.53.2.4   nathanw 	strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
    717      1.38  augustss 	    sizeof ue.u.ue_driver.ue_devname);
    718      1.38  augustss 	usb_add_event(type, &ue);
    719      1.38  augustss }
    720      1.38  augustss 
    721      1.42  augustss Static void
    722      1.45  augustss usb_add_event(int type, struct usb_event *uep)
    723      1.26  augustss {
    724      1.26  augustss 	struct usb_event_q *ueq;
    725      1.26  augustss 	struct usb_event ue;
    726      1.26  augustss 	struct timeval thetime;
    727      1.26  augustss 	int s;
    728      1.26  augustss 
    729      1.38  augustss 	microtime(&thetime);
    730      1.38  augustss 	/* Don't want to wait here inside splusb() */
    731      1.38  augustss 	ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
    732      1.38  augustss 	ueq->ue = *uep;
    733      1.38  augustss 	ueq->ue.ue_type = type;
    734      1.38  augustss 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    735      1.38  augustss 
    736      1.26  augustss 	s = splusb();
    737      1.26  augustss 	if (++usb_nevents >= USB_MAX_EVENTS) {
    738      1.26  augustss 		/* Too many queued events, drop an old one. */
    739      1.26  augustss 		DPRINTFN(-1,("usb: event dropped\n"));
    740      1.26  augustss 		(void)usb_get_next_event(&ue);
    741      1.26  augustss 	}
    742      1.26  augustss 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    743      1.26  augustss 	wakeup(&usb_events);
    744  1.53.2.8   nathanw 	selnotify(&usb_selevent, 0);
    745      1.29  augustss 	if (usb_async_proc != NULL)
    746      1.26  augustss 		psignal(usb_async_proc, SIGIO);
    747      1.26  augustss 	splx(s);
    748      1.39  augustss }
    749  1.53.2.2   nathanw 
    750      1.39  augustss void
    751      1.49  augustss usb_schedsoftintr(usbd_bus_handle bus)
    752      1.39  augustss {
    753  1.53.2.1   nathanw 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
    754      1.49  augustss #ifdef USB_USE_SOFTINTR
    755      1.49  augustss 	if (bus->use_polling) {
    756      1.49  augustss 		bus->methods->soft_intr(bus);
    757      1.49  augustss 	} else {
    758      1.49  augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    759      1.49  augustss 		softintr_schedule(bus->soft);
    760      1.49  augustss #else
    761      1.49  augustss 		if (!callout_pending(&bus->softi))
    762      1.49  augustss 			callout_reset(&bus->softi, 0, bus->methods->soft_intr,
    763      1.49  augustss 			    bus);
    764      1.49  augustss #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
    765      1.49  augustss 	}
    766      1.49  augustss #else
    767      1.39  augustss 	bus->methods->soft_intr(bus);
    768  1.53.2.1   nathanw #endif /* USB_USE_SOFTINTR */
    769      1.26  augustss }
    770      1.26  augustss 
    771       1.7  augustss int
    772      1.45  augustss usb_activate(device_ptr_t self, enum devact act)
    773       1.7  augustss {
    774      1.22  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    775      1.25  augustss 	usbd_device_handle dev = sc->sc_port.device;
    776      1.25  augustss 	int i, rv = 0;
    777      1.22  augustss 
    778      1.22  augustss 	switch (act) {
    779      1.22  augustss 	case DVACT_ACTIVATE:
    780      1.22  augustss 		return (EOPNOTSUPP);
    781      1.22  augustss 
    782      1.22  augustss 	case DVACT_DEACTIVATE:
    783      1.22  augustss 		sc->sc_dying = 1;
    784  1.53.2.3   nathanw 		if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
    785      1.25  augustss 			for (i = 0; dev->subdevs[i]; i++)
    786      1.25  augustss 				rv |= config_deactivate(dev->subdevs[i]);
    787      1.25  augustss 		}
    788      1.22  augustss 		break;
    789      1.22  augustss 	}
    790      1.22  augustss 	return (rv);
    791      1.13  augustss }
    792       1.7  augustss 
    793      1.13  augustss int
    794      1.45  augustss usb_detach(device_ptr_t self, int flags)
    795      1.13  augustss {
    796      1.22  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    797      1.38  augustss 	struct usb_event ue;
    798      1.22  augustss 
    799      1.27  augustss 	DPRINTF(("usb_detach: start\n"));
    800      1.27  augustss 
    801      1.22  augustss 	sc->sc_dying = 1;
    802      1.22  augustss 
    803      1.22  augustss 	/* Make all devices disconnect. */
    804  1.53.2.3   nathanw 	if (sc->sc_port.device != NULL)
    805      1.27  augustss 		usb_disconnect_port(&sc->sc_port, self);
    806      1.22  augustss 
    807      1.22  augustss 	/* Kill off event thread. */
    808  1.53.2.3   nathanw 	if (sc->sc_event_thread != NULL) {
    809  1.53.2.2   nathanw 		wakeup(&sc->sc_bus->needs_explore);
    810      1.22  augustss 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    811      1.22  augustss 			printf("%s: event thread didn't die\n",
    812      1.22  augustss 			       USBDEVNAME(sc->sc_dev));
    813      1.27  augustss 		DPRINTF(("usb_detach: event thread dead\n"));
    814      1.22  augustss 	}
    815      1.22  augustss 
    816      1.26  augustss 	usbd_finish();
    817      1.49  augustss 
    818      1.49  augustss #ifdef USB_USE_SOFTINTR
    819      1.49  augustss #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    820      1.49  augustss 	if (sc->sc_bus->soft != NULL) {
    821      1.49  augustss 		softintr_disestablish(sc->sc_bus->soft);
    822      1.49  augustss 		sc->sc_bus->soft = NULL;
    823      1.49  augustss 	}
    824      1.49  augustss #else
    825      1.49  augustss 	callout_stop(&sc->sc_bus->softi);
    826      1.49  augustss #endif
    827      1.49  augustss #endif
    828      1.38  augustss 
    829      1.38  augustss 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    830      1.38  augustss 	usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
    831      1.38  augustss 
    832       1.7  augustss 	return (0);
    833       1.7  augustss }
    834