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