Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.168.2.2
      1 /*	$NetBSD: usb.c,v 1.168.2.2 2018/07/28 04:37:58 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2002, 2008, 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * USB specifications and other documentation can be found at
     35  * http://www.usb.org/developers/docs/ and
     36  * http://www.usb.org/developers/devclass_docs/
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.168.2.2 2018/07/28 04:37:58 pgoyette Exp $");
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_usb.h"
     44 #include "opt_compat_netbsd.h"
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/kmem.h>
     51 #include <sys/device.h>
     52 #include <sys/kthread.h>
     53 #include <sys/proc.h>
     54 #include <sys/conf.h>
     55 #include <sys/fcntl.h>
     56 #include <sys/poll.h>
     57 #include <sys/select.h>
     58 #include <sys/vnode.h>
     59 #include <sys/signalvar.h>
     60 #include <sys/intr.h>
     61 #include <sys/module.h>
     62 #include <sys/mutex.h>
     63 #include <sys/bus.h>
     64 #include <sys/once.h>
     65 #include <sys/atomic.h>
     66 #include <sys/sysctl.h>
     67 #include <sys/compat_stub.h>
     68 
     69 #include <dev/usb/usb.h>
     70 #include <dev/usb/usbdi.h>
     71 #include <dev/usb/usbdi_util.h>
     72 #include <dev/usb/usbdivar.h>
     73 #include <dev/usb/usb_verbose.h>
     74 #include <dev/usb/usb_quirks.h>
     75 #include <dev/usb/usbhist.h>
     76 
     77 #if defined(USB_DEBUG)
     78 
     79 #ifndef USBHIST_SIZE
     80 #define USBHIST_SIZE 50000
     81 #endif
     82 
     83 static struct kern_history_ent usbhistbuf[USBHIST_SIZE];
     84 USBHIST_DEFINE(usbhist) = KERNHIST_INITIALIZER(usbhist, usbhistbuf);
     85 
     86 #endif
     87 
     88 #define USB_DEV_MINOR 255
     89 
     90 #ifdef USB_DEBUG
     91 /*
     92  * 0  - do usual exploration
     93  * 1  - do not use timeout exploration
     94  * >1 - do no exploration
     95  */
     96 int	usb_noexplore = 0;
     97 
     98 int	usbdebug = 0;
     99 SYSCTL_SETUP(sysctl_hw_usb_setup, "sysctl hw.usb setup")
    100 {
    101 	int err;
    102 	const struct sysctlnode *rnode;
    103 	const struct sysctlnode *cnode;
    104 
    105 	err = sysctl_createv(clog, 0, NULL, &rnode,
    106 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usb",
    107 	    SYSCTL_DESCR("usb global controls"),
    108 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    109 
    110 	if (err)
    111 		goto fail;
    112 
    113 	/* control debugging printfs */
    114 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    115 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    116 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    117 	    NULL, 0, &usbdebug, sizeof(usbdebug), CTL_CREATE, CTL_EOL);
    118 	if (err)
    119 		goto fail;
    120 
    121 	return;
    122 fail:
    123 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    124 }
    125 #else
    126 #define	usb_noexplore 0
    127 #endif
    128 
    129 #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usbdebug,FMT,A,B,C,D)
    130 #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
    131 
    132 struct usb_softc {
    133 #if 0
    134 	device_t	sc_dev;		/* base device */
    135 #endif
    136 	struct usbd_bus *sc_bus;		/* USB controller */
    137 	struct usbd_port sc_port;	/* dummy port for root hub */
    138 
    139 	struct lwp	*sc_event_thread;
    140 
    141 	char		sc_dying;
    142 };
    143 
    144 struct usb_taskq {
    145 	TAILQ_HEAD(, usb_task) tasks;
    146 	kmutex_t lock;
    147 	kcondvar_t cv;
    148 	struct lwp *task_thread_lwp;
    149 	const char *name;
    150 };
    151 
    152 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
    153 
    154 dev_type_open(usbopen);
    155 dev_type_close(usbclose);
    156 dev_type_read(usbread);
    157 dev_type_ioctl(usbioctl);
    158 dev_type_poll(usbpoll);
    159 dev_type_kqfilter(usbkqfilter);
    160 
    161 const struct cdevsw usb_cdevsw = {
    162 	.d_open = usbopen,
    163 	.d_close = usbclose,
    164 	.d_read = usbread,
    165 	.d_write = nowrite,
    166 	.d_ioctl = usbioctl,
    167 	.d_stop = nostop,
    168 	.d_tty = notty,
    169 	.d_poll = usbpoll,
    170 	.d_mmap = nommap,
    171 	.d_kqfilter = usbkqfilter,
    172 	.d_discard = nodiscard,
    173 	.d_flag = D_OTHER
    174 };
    175 
    176 Static void	usb_discover(struct usb_softc *);
    177 Static void	usb_create_event_thread(device_t);
    178 Static void	usb_event_thread(void *);
    179 Static void	usb_task_thread(void *);
    180 
    181 #define USB_MAX_EVENTS 100
    182 struct usb_event_q {
    183 	struct usb_event ue;
    184 	SIMPLEQ_ENTRY(usb_event_q) next;
    185 };
    186 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    187 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    188 Static int usb_nevents = 0;
    189 Static struct selinfo usb_selevent;
    190 Static kmutex_t usb_event_lock;
    191 Static kcondvar_t usb_event_cv;
    192 Static proc_t *usb_async_proc;  /* process that wants USB SIGIO */
    193 Static void *usb_async_sih;
    194 Static int usb_dev_open = 0;
    195 Static struct usb_event *usb_alloc_event(void);
    196 Static void usb_free_event(struct usb_event *);
    197 Static void usb_add_event(int, struct usb_event *);
    198 Static int usb_get_next_event(struct usb_event *);
    199 Static void usb_async_intr(void *);
    200 Static void usb_soft_intr(void *);
    201 
    202 Static const char *usbrev_str[] = USBREV_STR;
    203 
    204 static int usb_match(device_t, cfdata_t, void *);
    205 static void usb_attach(device_t, device_t, void *);
    206 static int usb_detach(device_t, int);
    207 static int usb_activate(device_t, enum devact);
    208 static void usb_childdet(device_t, device_t);
    209 static int usb_once_init(void);
    210 static void usb_doattach(device_t);
    211 
    212 extern struct cfdriver usb_cd;
    213 
    214 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
    215     usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
    216     DVF_DETACH_SHUTDOWN);
    217 
    218 static const char *taskq_names[] = USB_TASKQ_NAMES;
    219 
    220 int
    221 usb_match(device_t parent, cfdata_t match, void *aux)
    222 {
    223 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    224 
    225 	return UMATCH_GENERIC;
    226 }
    227 
    228 void
    229 usb_attach(device_t parent, device_t self, void *aux)
    230 {
    231 	static ONCE_DECL(init_control);
    232 	struct usb_softc *sc = device_private(self);
    233 	int usbrev;
    234 
    235 	sc->sc_bus = aux;
    236 	usbrev = sc->sc_bus->ub_revision;
    237 
    238 	aprint_naive("\n");
    239 	aprint_normal(": USB revision %s", usbrev_str[usbrev]);
    240 	switch (usbrev) {
    241 	case USBREV_1_0:
    242 	case USBREV_1_1:
    243 	case USBREV_2_0:
    244 	case USBREV_3_0:
    245 	case USBREV_3_1:
    246 		break;
    247 	default:
    248 		aprint_error(", not supported\n");
    249 		sc->sc_dying = 1;
    250 		return;
    251 	}
    252 	aprint_normal("\n");
    253 
    254 	/* XXX we should have our own level */
    255 	sc->sc_bus->ub_soft = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
    256 	    usb_soft_intr, sc->sc_bus);
    257 	if (sc->sc_bus->ub_soft == NULL) {
    258 		aprint_error("%s: can't register softintr\n",
    259 			     device_xname(self));
    260 		sc->sc_dying = 1;
    261 		return;
    262 	}
    263 
    264 	sc->sc_bus->ub_methods->ubm_getlock(sc->sc_bus, &sc->sc_bus->ub_lock);
    265 	KASSERT(sc->sc_bus->ub_lock != NULL);
    266 
    267 	RUN_ONCE(&init_control, usb_once_init);
    268 	config_interrupts(self, usb_doattach);
    269 }
    270 
    271 static int
    272 usb_once_init(void)
    273 {
    274 	struct usb_taskq *taskq;
    275 	int i;
    276 
    277 	USBHIST_LINK_STATIC(usbhist);
    278 
    279 	selinit(&usb_selevent);
    280 	mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
    281 	cv_init(&usb_event_cv, "usbrea");
    282 
    283 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    284 		taskq = &usb_taskq[i];
    285 
    286 		TAILQ_INIT(&taskq->tasks);
    287 		/*
    288 		 * Since USB task methods usb_{add,rem}_task are callable
    289 		 * from any context, we have to make this lock a spinlock.
    290 		 */
    291 		mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
    292 		cv_init(&taskq->cv, "usbtsk");
    293 		taskq->name = taskq_names[i];
    294 		if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    295 		    usb_task_thread, taskq, &taskq->task_thread_lwp,
    296 		    "%s", taskq->name)) {
    297 			printf("unable to create task thread: %s\n", taskq->name);
    298 			panic("usb_create_event_thread task");
    299 		}
    300 		/*
    301 		 * XXX we should make sure these threads are alive before
    302 		 * end up using them in usb_doattach().
    303 		 */
    304 	}
    305 	return 0;
    306 }
    307 
    308 static void
    309 usb_doattach(device_t self)
    310 {
    311 	struct usb_softc *sc = device_private(self);
    312 	struct usbd_device *dev;
    313 	usbd_status err;
    314 	int speed;
    315 	struct usb_event *ue;
    316 
    317 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    318 
    319 	sc->sc_bus->ub_usbctl = self;
    320 	sc->sc_port.up_power = USB_MAX_POWER;
    321 
    322 	switch (sc->sc_bus->ub_revision) {
    323 	case USBREV_1_0:
    324 	case USBREV_1_1:
    325 		speed = USB_SPEED_FULL;
    326 		break;
    327 	case USBREV_2_0:
    328 		speed = USB_SPEED_HIGH;
    329 		break;
    330 	case USBREV_3_0:
    331 		speed = USB_SPEED_SUPER;
    332 		break;
    333 	case USBREV_3_1:
    334 		speed = USB_SPEED_SUPER_PLUS;
    335 		break;
    336 	default:
    337 		panic("usb_doattach");
    338 	}
    339 
    340 	cv_init(&sc->sc_bus->ub_needsexplore_cv, "usbevt");
    341 
    342 	ue = usb_alloc_event();
    343 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
    344 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    345 
    346 	err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
    347 		  &sc->sc_port);
    348 	if (!err) {
    349 		dev = sc->sc_port.up_dev;
    350 		if (dev->ud_hub == NULL) {
    351 			sc->sc_dying = 1;
    352 			aprint_error("%s: root device is not a hub\n",
    353 				     device_xname(self));
    354 			return;
    355 		}
    356 		sc->sc_bus->ub_roothub = dev;
    357 		usb_create_event_thread(self);
    358 #if 1
    359 		/*
    360 		 * Turning this code off will delay attachment of USB devices
    361 		 * until the USB event thread is running, which means that
    362 		 * the keyboard will not work until after cold boot.
    363 		 */
    364 		if (cold && (device_cfdata(self)->cf_flags & 1))
    365 			dev->ud_hub->uh_explore(sc->sc_bus->ub_roothub);
    366 #endif
    367 	} else {
    368 		aprint_error("%s: root hub problem, error=%s\n",
    369 			     device_xname(self), usbd_errstr(err));
    370 		sc->sc_dying = 1;
    371 	}
    372 
    373 	config_pending_incr(self);
    374 
    375 	if (!pmf_device_register(self, NULL, NULL))
    376 		aprint_error_dev(self, "couldn't establish power handler\n");
    377 
    378 	usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    379 	   usb_async_intr, NULL);
    380 
    381 	return;
    382 }
    383 
    384 void
    385 usb_create_event_thread(device_t self)
    386 {
    387 	struct usb_softc *sc = device_private(self);
    388 
    389 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    390 	    usb_event_thread, sc, &sc->sc_event_thread,
    391 	    "%s", device_xname(self))) {
    392 		printf("%s: unable to create event thread for\n",
    393 		       device_xname(self));
    394 		panic("usb_create_event_thread");
    395 	}
    396 }
    397 
    398 /*
    399  * Add a task to be performed by the task thread.  This function can be
    400  * called from any context and the task will be executed in a process
    401  * context ASAP.
    402  */
    403 void
    404 usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
    405 {
    406 	struct usb_taskq *taskq;
    407 
    408 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    409 
    410 	KASSERT(0 <= queue);
    411 	KASSERT(queue < USB_NUM_TASKQS);
    412 	taskq = &usb_taskq[queue];
    413 	mutex_enter(&taskq->lock);
    414 	if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
    415 	    USB_NUM_TASKQS) {
    416 		DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
    417 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    418 		cv_signal(&taskq->cv);
    419 	} else {
    420 		DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
    421 	}
    422 	mutex_exit(&taskq->lock);
    423 }
    424 
    425 /*
    426  * XXX This does not wait for completion!  Most uses need such an
    427  * operation.  Urgh...
    428  */
    429 void
    430 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
    431 {
    432 	unsigned queue;
    433 
    434 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    435 
    436 	while ((queue = task->queue) != USB_NUM_TASKQS) {
    437 		struct usb_taskq *taskq = &usb_taskq[queue];
    438 		mutex_enter(&taskq->lock);
    439 		if (__predict_true(task->queue == queue)) {
    440 			TAILQ_REMOVE(&taskq->tasks, task, next);
    441 			task->queue = USB_NUM_TASKQS;
    442 			mutex_exit(&taskq->lock);
    443 			break;
    444 		}
    445 		mutex_exit(&taskq->lock);
    446 	}
    447 }
    448 
    449 void
    450 usb_event_thread(void *arg)
    451 {
    452 	struct usb_softc *sc = arg;
    453 
    454 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    455 
    456 	/*
    457 	 * In case this controller is a companion controller to an
    458 	 * EHCI controller we need to wait until the EHCI controller
    459 	 * has grabbed the port.
    460 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    461 	 * know how to synchronize the creation of the threads so it
    462 	 * will work.
    463 	 */
    464 	usb_delay_ms(sc->sc_bus, 500);
    465 
    466 	/* Make sure first discover does something. */
    467 	mutex_enter(sc->sc_bus->ub_lock);
    468 	sc->sc_bus->ub_needsexplore = 1;
    469 	usb_discover(sc);
    470 	mutex_exit(sc->sc_bus->ub_lock);
    471 	config_pending_decr(sc->sc_bus->ub_usbctl);
    472 
    473 	mutex_enter(sc->sc_bus->ub_lock);
    474 	while (!sc->sc_dying) {
    475 		if (usb_noexplore < 2)
    476 			usb_discover(sc);
    477 
    478 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
    479 		    sc->sc_bus->ub_lock, usb_noexplore ? 0 : hz * 60);
    480 
    481 		DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
    482 	}
    483 	sc->sc_event_thread = NULL;
    484 
    485 	/* In case parent is waiting for us to exit. */
    486 	cv_signal(&sc->sc_bus->ub_needsexplore_cv);
    487 	mutex_exit(sc->sc_bus->ub_lock);
    488 
    489 	DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
    490 	kthread_exit(0);
    491 }
    492 
    493 void
    494 usb_task_thread(void *arg)
    495 {
    496 	struct usb_task *task;
    497 	struct usb_taskq *taskq;
    498 	bool mpsafe;
    499 
    500 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    501 
    502 	taskq = arg;
    503 	DPRINTF("start taskq %#jx", (uintptr_t)taskq, 0, 0, 0);
    504 
    505 	mutex_enter(&taskq->lock);
    506 	for (;;) {
    507 		task = TAILQ_FIRST(&taskq->tasks);
    508 		if (task == NULL) {
    509 			cv_wait(&taskq->cv, &taskq->lock);
    510 			task = TAILQ_FIRST(&taskq->tasks);
    511 		}
    512 		DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
    513 		if (task != NULL) {
    514 			mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
    515 			TAILQ_REMOVE(&taskq->tasks, task, next);
    516 			task->queue = USB_NUM_TASKQS;
    517 			mutex_exit(&taskq->lock);
    518 
    519 			if (!mpsafe)
    520 				KERNEL_LOCK(1, curlwp);
    521 			task->fun(task->arg);
    522 			/* Can't dereference task after this point.  */
    523 			if (!mpsafe)
    524 				KERNEL_UNLOCK_ONE(curlwp);
    525 
    526 			mutex_enter(&taskq->lock);
    527 		}
    528 	}
    529 	mutex_exit(&taskq->lock);
    530 }
    531 
    532 int
    533 usbctlprint(void *aux, const char *pnp)
    534 {
    535 	/* only "usb"es can attach to host controllers */
    536 	if (pnp)
    537 		aprint_normal("usb at %s", pnp);
    538 
    539 	return UNCONF;
    540 }
    541 
    542 int
    543 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    544 {
    545 	int unit = minor(dev);
    546 	struct usb_softc *sc;
    547 
    548 	if (unit == USB_DEV_MINOR) {
    549 		if (usb_dev_open)
    550 			return EBUSY;
    551 		usb_dev_open = 1;
    552 		mutex_enter(proc_lock);
    553 		usb_async_proc = 0;
    554 		mutex_exit(proc_lock);
    555 		return 0;
    556 	}
    557 
    558 	sc = device_lookup_private(&usb_cd, unit);
    559 	if (!sc)
    560 		return ENXIO;
    561 
    562 	if (sc->sc_dying)
    563 		return EIO;
    564 
    565 	return 0;
    566 }
    567 
    568 int
    569 usbread(dev_t dev, struct uio *uio, int flag)
    570 {
    571 	struct usb_event *ue;
    572 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    573 	int useold = 0;
    574 	int error, n;
    575 
    576 	if (minor(dev) != USB_DEV_MINOR)
    577 		return ENXIO;
    578 
    579 	switch (uio->uio_resid) {
    580 	case sizeof(struct usb_event_old):
    581 		ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
    582 		useold = 1;
    583 		/* FALLTHRU */
    584 	case sizeof(struct usb_event):
    585 		ue = usb_alloc_event();
    586 		break;
    587 	default:
    588 		return EINVAL;
    589 	}
    590 
    591 	error = 0;
    592 	mutex_enter(&usb_event_lock);
    593 	for (;;) {
    594 		n = usb_get_next_event(ue);
    595 		if (n != 0)
    596 			break;
    597 		if (flag & IO_NDELAY) {
    598 			error = EWOULDBLOCK;
    599 			break;
    600 		}
    601 		error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
    602 		if (error)
    603 			break;
    604 	}
    605 	mutex_exit(&usb_event_lock);
    606 	if (!error) {
    607 		if (useold) { /* copy fields to old struct */
    608 			error = (*usb30_copy_to_old)(ue, ueo, uio);
    609 			if (error == ENOSYS)
    610 				error = EINVAL;
    611 
    612 			if (!error)
    613 				error = uiomove((void *)ueo, sizeof(*ueo), uio);
    614 		} else
    615 			error = uiomove((void *)ue, sizeof(*ue), uio);
    616 	}
    617 	usb_free_event(ue);
    618 	if (ueo)
    619 		kmem_free(ueo, sizeof(struct usb_event_old));
    620 
    621 	return error;
    622 }
    623 
    624 int
    625 usbclose(dev_t dev, int flag, int mode,
    626     struct lwp *l)
    627 {
    628 	int unit = minor(dev);
    629 
    630 	if (unit == USB_DEV_MINOR) {
    631 		mutex_enter(proc_lock);
    632 		usb_async_proc = 0;
    633 		mutex_exit(proc_lock);
    634 		usb_dev_open = 0;
    635 	}
    636 
    637 	return 0;
    638 }
    639 
    640 int
    641 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    642 {
    643 	struct usb_softc *sc;
    644 	int unit = minor(devt);
    645 
    646 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    647 
    648 	if (unit == USB_DEV_MINOR) {
    649 		switch (cmd) {
    650 		case FIONBIO:
    651 			/* All handled in the upper FS layer. */
    652 			return 0;
    653 
    654 		case FIOASYNC:
    655 			mutex_enter(proc_lock);
    656 			if (*(int *)data)
    657 				usb_async_proc = l->l_proc;
    658 			else
    659 				usb_async_proc = 0;
    660 			mutex_exit(proc_lock);
    661 			return 0;
    662 
    663 		default:
    664 			return EINVAL;
    665 		}
    666 	}
    667 
    668 	sc = device_lookup_private(&usb_cd, unit);
    669 
    670 	if (sc->sc_dying)
    671 		return EIO;
    672 
    673 	int error = 0;
    674 	DPRINTF("cmd %#jx", cmd, 0, 0, 0);
    675 	switch (cmd) {
    676 #ifdef USB_DEBUG
    677 	case USB_SETDEBUG:
    678 		if (!(flag & FWRITE))
    679 			return EBADF;
    680 		usbdebug  = ((*(int *)data) & 0x000000ff);
    681 		break;
    682 #endif /* USB_DEBUG */
    683 	case USB_REQUEST:
    684 	{
    685 		struct usb_ctl_request *ur = (void *)data;
    686 		int len = UGETW(ur->ucr_request.wLength);
    687 		struct iovec iov;
    688 		struct uio uio;
    689 		void *ptr = 0;
    690 		int addr = ur->ucr_addr;
    691 		usbd_status err;
    692 
    693 		if (!(flag & FWRITE)) {
    694 			error = EBADF;
    695 			goto fail;
    696 		}
    697 
    698 		DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
    699 		if (len < 0 || len > 32768) {
    700 			error = EINVAL;
    701 			goto fail;
    702 		}
    703 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    704 			error = EINVAL;
    705 			goto fail;
    706 		}
    707 		size_t dindex = usb_addr2dindex(addr);
    708 		if (sc->sc_bus->ub_devices[dindex] == NULL) {
    709 			error = EINVAL;
    710 			goto fail;
    711 		}
    712 		if (len != 0) {
    713 			iov.iov_base = (void *)ur->ucr_data;
    714 			iov.iov_len = len;
    715 			uio.uio_iov = &iov;
    716 			uio.uio_iovcnt = 1;
    717 			uio.uio_resid = len;
    718 			uio.uio_offset = 0;
    719 			uio.uio_rw =
    720 				ur->ucr_request.bmRequestType & UT_READ ?
    721 				UIO_READ : UIO_WRITE;
    722 			uio.uio_vmspace = l->l_proc->p_vmspace;
    723 			ptr = kmem_alloc(len, KM_SLEEP);
    724 			if (uio.uio_rw == UIO_WRITE) {
    725 				error = uiomove(ptr, len, &uio);
    726 				if (error)
    727 					goto ret;
    728 			}
    729 		}
    730 		err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
    731 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    732 			  USBD_DEFAULT_TIMEOUT);
    733 		if (err) {
    734 			error = EIO;
    735 			goto ret;
    736 		}
    737 		if (len > ur->ucr_actlen)
    738 			len = ur->ucr_actlen;
    739 		if (len != 0) {
    740 			if (uio.uio_rw == UIO_READ) {
    741 				error = uiomove(ptr, len, &uio);
    742 				if (error)
    743 					goto ret;
    744 			}
    745 		}
    746 	ret:
    747 		if (ptr) {
    748 			len = UGETW(ur->ucr_request.wLength);
    749 			kmem_free(ptr, len);
    750 		}
    751 		break;
    752 	}
    753 
    754 	case USB_DEVICEINFO:
    755 	{
    756 		struct usbd_device *dev;
    757 		struct usb_device_info *di = (void *)data;
    758 		int addr = di->udi_addr;
    759 
    760 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    761 			error = EINVAL;
    762 			goto fail;
    763 		}
    764 		size_t dindex = usb_addr2dindex(addr);
    765 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    766 			error = ENXIO;
    767 			goto fail;
    768 		}
    769 		usbd_fill_deviceinfo(dev, di, 1);
    770 		break;
    771 	}
    772 
    773 	case USB_DEVICEINFO_OLD:
    774 	{
    775 		struct usbd_device *dev;
    776 		struct usb_device_info_old *di = (void *)data;
    777 		int addr = di->udi_addr;
    778 
    779 		if (addr < 1 || addr >= USB_MAX_DEVICES) {
    780 			error = EINVAL;
    781 			goto fail;
    782 		}
    783 		size_t dindex = usb_addr2dindex(addr);
    784 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    785 			error = ENXIO;
    786 			goto fail;
    787 		}
    788 		error = (*usbd30_fill_deviceinfo_old)(dev, di, 1);
    789 		if (error == ENOSYS)
    790 			error = EINVAL;
    791 		if (error)
    792 			goto fail;
    793 	}
    794 
    795 	case USB_DEVICESTATS:
    796 		*(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
    797 		break;
    798 
    799 	default:
    800 		error = EINVAL;
    801 	}
    802 
    803 fail:
    804 
    805 	DPRINTF("... done (error = %jd)", error, 0, 0, 0);
    806 
    807 	return error;
    808 }
    809 
    810 int
    811 usbpoll(dev_t dev, int events, struct lwp *l)
    812 {
    813 	int revents, mask;
    814 
    815 	if (minor(dev) == USB_DEV_MINOR) {
    816 		revents = 0;
    817 		mask = POLLIN | POLLRDNORM;
    818 
    819 		mutex_enter(&usb_event_lock);
    820 		if (events & mask && usb_nevents > 0)
    821 			revents |= events & mask;
    822 		if (revents == 0 && events & mask)
    823 			selrecord(l, &usb_selevent);
    824 		mutex_exit(&usb_event_lock);
    825 
    826 		return revents;
    827 	} else {
    828 		return 0;
    829 	}
    830 }
    831 
    832 static void
    833 filt_usbrdetach(struct knote *kn)
    834 {
    835 
    836 	mutex_enter(&usb_event_lock);
    837 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    838 	mutex_exit(&usb_event_lock);
    839 }
    840 
    841 static int
    842 filt_usbread(struct knote *kn, long hint)
    843 {
    844 
    845 	if (usb_nevents == 0)
    846 		return 0;
    847 
    848 	kn->kn_data = sizeof(struct usb_event);
    849 	return 1;
    850 }
    851 
    852 static const struct filterops usbread_filtops = {
    853 	.f_isfd = 1,
    854 	.f_attach = NULL,
    855 	.f_detach = filt_usbrdetach,
    856 	.f_event = filt_usbread,
    857 };
    858 
    859 int
    860 usbkqfilter(dev_t dev, struct knote *kn)
    861 {
    862 	struct klist *klist;
    863 
    864 	switch (kn->kn_filter) {
    865 	case EVFILT_READ:
    866 		if (minor(dev) != USB_DEV_MINOR)
    867 			return 1;
    868 		klist = &usb_selevent.sel_klist;
    869 		kn->kn_fop = &usbread_filtops;
    870 		break;
    871 
    872 	default:
    873 		return EINVAL;
    874 	}
    875 
    876 	kn->kn_hook = NULL;
    877 
    878 	mutex_enter(&usb_event_lock);
    879 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    880 	mutex_exit(&usb_event_lock);
    881 
    882 	return 0;
    883 }
    884 
    885 /* Explore device tree from the root. */
    886 Static void
    887 usb_discover(struct usb_softc *sc)
    888 {
    889 
    890 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    891 
    892 	KASSERT(mutex_owned(sc->sc_bus->ub_lock));
    893 
    894 	if (usb_noexplore > 1)
    895 		return;
    896 	/*
    897 	 * We need mutual exclusion while traversing the device tree,
    898 	 * but this is guaranteed since this function is only called
    899 	 * from the event thread for the controller.
    900 	 *
    901 	 * Also, we now have sc_bus->ub_lock held.
    902 	 */
    903 	while (sc->sc_bus->ub_needsexplore && !sc->sc_dying) {
    904 		sc->sc_bus->ub_needsexplore = 0;
    905 		mutex_exit(sc->sc_bus->ub_lock);
    906 		sc->sc_bus->ub_roothub->ud_hub->uh_explore(sc->sc_bus->ub_roothub);
    907 		mutex_enter(sc->sc_bus->ub_lock);
    908 	}
    909 }
    910 
    911 void
    912 usb_needs_explore(struct usbd_device *dev)
    913 {
    914 
    915 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    916 
    917 	mutex_enter(dev->ud_bus->ub_lock);
    918 	dev->ud_bus->ub_needsexplore = 1;
    919 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
    920 	mutex_exit(dev->ud_bus->ub_lock);
    921 }
    922 
    923 void
    924 usb_needs_reattach(struct usbd_device *dev)
    925 {
    926 
    927 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    928 
    929 	mutex_enter(dev->ud_bus->ub_lock);
    930 	dev->ud_powersrc->up_reattach = 1;
    931 	dev->ud_bus->ub_needsexplore = 1;
    932 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
    933 	mutex_exit(dev->ud_bus->ub_lock);
    934 }
    935 
    936 /* Called at with usb_event_lock held. */
    937 int
    938 usb_get_next_event(struct usb_event *ue)
    939 {
    940 	struct usb_event_q *ueq;
    941 
    942 	KASSERT(mutex_owned(&usb_event_lock));
    943 
    944 	if (usb_nevents <= 0)
    945 		return 0;
    946 	ueq = SIMPLEQ_FIRST(&usb_events);
    947 #ifdef DIAGNOSTIC
    948 	if (ueq == NULL) {
    949 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
    950 		usb_nevents = 0;
    951 		return 0;
    952 	}
    953 #endif
    954 	if (ue)
    955 		*ue = ueq->ue;
    956 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
    957 	usb_free_event((struct usb_event *)(void *)ueq);
    958 	usb_nevents--;
    959 	return 1;
    960 }
    961 
    962 void
    963 usbd_add_dev_event(int type, struct usbd_device *udev)
    964 {
    965 	struct usb_event *ue = usb_alloc_event();
    966 
    967 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
    968 	usb_add_event(type, ue);
    969 }
    970 
    971 void
    972 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
    973 {
    974 	struct usb_event *ue = usb_alloc_event();
    975 
    976 	ue->u.ue_driver.ue_cookie = udev->ud_cookie;
    977 	strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
    978 	    sizeof(ue->u.ue_driver.ue_devname));
    979 	usb_add_event(type, ue);
    980 }
    981 
    982 Static struct usb_event *
    983 usb_alloc_event(void)
    984 {
    985 	/* Yes, this is right; we allocate enough so that we can use it later */
    986 	return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
    987 }
    988 
    989 Static void
    990 usb_free_event(struct usb_event *uep)
    991 {
    992 	kmem_free(uep, sizeof(struct usb_event_q));
    993 }
    994 
    995 Static void
    996 usb_add_event(int type, struct usb_event *uep)
    997 {
    998 	struct usb_event_q *ueq;
    999 	struct timeval thetime;
   1000 
   1001 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1002 
   1003 	microtime(&thetime);
   1004 	/* Don't want to wait here with usb_event_lock held */
   1005 	ueq = (struct usb_event_q *)(void *)uep;
   1006 	ueq->ue = *uep;
   1007 	ueq->ue.ue_type = type;
   1008 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
   1009 
   1010 	mutex_enter(&usb_event_lock);
   1011 	if (++usb_nevents >= USB_MAX_EVENTS) {
   1012 		/* Too many queued events, drop an old one. */
   1013 		DPRINTF("event dropped", 0, 0, 0, 0);
   1014 		(void)usb_get_next_event(0);
   1015 	}
   1016 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
   1017 	cv_signal(&usb_event_cv);
   1018 	selnotify(&usb_selevent, 0, 0);
   1019 	if (usb_async_proc != NULL) {
   1020 		kpreempt_disable();
   1021 		softint_schedule(usb_async_sih);
   1022 		kpreempt_enable();
   1023 	}
   1024 	mutex_exit(&usb_event_lock);
   1025 }
   1026 
   1027 Static void
   1028 usb_async_intr(void *cookie)
   1029 {
   1030 	proc_t *proc;
   1031 
   1032 	mutex_enter(proc_lock);
   1033 	if ((proc = usb_async_proc) != NULL)
   1034 		psignal(proc, SIGIO);
   1035 	mutex_exit(proc_lock);
   1036 }
   1037 
   1038 Static void
   1039 usb_soft_intr(void *arg)
   1040 {
   1041 	struct usbd_bus *bus = arg;
   1042 
   1043 	mutex_enter(bus->ub_lock);
   1044 	bus->ub_methods->ubm_softint(bus);
   1045 	mutex_exit(bus->ub_lock);
   1046 }
   1047 
   1048 void
   1049 usb_schedsoftintr(struct usbd_bus *bus)
   1050 {
   1051 
   1052 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1053 
   1054 	DPRINTFN(10, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
   1055 
   1056 	if (bus->ub_usepolling) {
   1057 		bus->ub_methods->ubm_softint(bus);
   1058 	} else {
   1059 		kpreempt_disable();
   1060 		softint_schedule(bus->ub_soft);
   1061 		kpreempt_enable();
   1062 	}
   1063 }
   1064 
   1065 int
   1066 usb_activate(device_t self, enum devact act)
   1067 {
   1068 	struct usb_softc *sc = device_private(self);
   1069 
   1070 	switch (act) {
   1071 	case DVACT_DEACTIVATE:
   1072 		sc->sc_dying = 1;
   1073 		return 0;
   1074 	default:
   1075 		return EOPNOTSUPP;
   1076 	}
   1077 }
   1078 
   1079 void
   1080 usb_childdet(device_t self, device_t child)
   1081 {
   1082 	int i;
   1083 	struct usb_softc *sc = device_private(self);
   1084 	struct usbd_device *dev;
   1085 
   1086 	if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
   1087 		return;
   1088 
   1089 	for (i = 0; i < dev->ud_subdevlen; i++)
   1090 		if (dev->ud_subdevs[i] == child)
   1091 			dev->ud_subdevs[i] = NULL;
   1092 }
   1093 
   1094 int
   1095 usb_detach(device_t self, int flags)
   1096 {
   1097 	struct usb_softc *sc = device_private(self);
   1098 	struct usb_event *ue;
   1099 	int rc;
   1100 
   1101 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1102 
   1103 	/* Make all devices disconnect. */
   1104 	if (sc->sc_port.up_dev != NULL &&
   1105 	    (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
   1106 		return rc;
   1107 
   1108 	pmf_device_deregister(self);
   1109 	/* Kill off event thread. */
   1110 	sc->sc_dying = 1;
   1111 	while (sc->sc_event_thread != NULL) {
   1112 		mutex_enter(sc->sc_bus->ub_lock);
   1113 		cv_signal(&sc->sc_bus->ub_needsexplore_cv);
   1114 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
   1115 		    sc->sc_bus->ub_lock, hz * 60);
   1116 		mutex_exit(sc->sc_bus->ub_lock);
   1117 	}
   1118 	DPRINTF("event thread dead", 0, 0, 0, 0);
   1119 
   1120 	if (sc->sc_bus->ub_soft != NULL) {
   1121 		softint_disestablish(sc->sc_bus->ub_soft);
   1122 		sc->sc_bus->ub_soft = NULL;
   1123 	}
   1124 
   1125 	ue = usb_alloc_event();
   1126 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
   1127 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1128 
   1129 	cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
   1130 
   1131 	return 0;
   1132 }
   1133