xref: /netbsd-src/sys/dev/usb/usb.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: usb.c,v 1.180 2019/08/21 10:48:37 mrg 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@augustsson.net) at
9  * Carlstedt Research & Technology and Matthew R. Green (mrg@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.180 2019/08/21 10:48:37 mrg 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 #include "ioconf.h"
78 
79 #if defined(USB_DEBUG)
80 
81 #ifndef USBHIST_SIZE
82 #define USBHIST_SIZE 50000
83 #endif
84 
85 static struct kern_history_ent usbhistbuf[USBHIST_SIZE];
86 USBHIST_DEFINE(usbhist) = KERNHIST_INITIALIZER(usbhist, usbhistbuf);
87 
88 #endif
89 
90 #define USB_DEV_MINOR 255
91 
92 #ifdef USB_DEBUG
93 /*
94  * 0  - do usual exploration
95  * 1  - do not use timeout exploration
96  * >1 - do no exploration
97  */
98 int	usb_noexplore = 0;
99 
100 int	usbdebug = 0;
101 SYSCTL_SETUP(sysctl_hw_usb_setup, "sysctl hw.usb setup")
102 {
103 	int err;
104 	const struct sysctlnode *rnode;
105 	const struct sysctlnode *cnode;
106 
107 	err = sysctl_createv(clog, 0, NULL, &rnode,
108 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usb",
109 	    SYSCTL_DESCR("usb global controls"),
110 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
111 
112 	if (err)
113 		goto fail;
114 
115 	/* control debugging printfs */
116 	err = sysctl_createv(clog, 0, &rnode, &cnode,
117 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
118 	    "debug", SYSCTL_DESCR("Enable debugging output"),
119 	    NULL, 0, &usbdebug, sizeof(usbdebug), CTL_CREATE, CTL_EOL);
120 	if (err)
121 		goto fail;
122 
123 	return;
124 fail:
125 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
126 }
127 #else
128 #define	usb_noexplore 0
129 #endif
130 
131 #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usbdebug,FMT,A,B,C,D)
132 #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
133 
134 struct usb_softc {
135 #if 0
136 	device_t	sc_dev;		/* base device */
137 #endif
138 	struct usbd_bus *sc_bus;		/* USB controller */
139 	struct usbd_port sc_port;	/* dummy port for root hub */
140 
141 	struct lwp	*sc_event_thread;
142 
143 	char		sc_dying;
144 	bool		sc_pmf_registered;
145 };
146 
147 struct usb_taskq {
148 	TAILQ_HEAD(, usb_task) tasks;
149 	kmutex_t lock;
150 	kcondvar_t cv;
151 	struct lwp *task_thread_lwp;
152 	const char *name;
153 	struct usb_task *current_task;
154 };
155 
156 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
157 
158 dev_type_open(usbopen);
159 dev_type_close(usbclose);
160 dev_type_read(usbread);
161 dev_type_ioctl(usbioctl);
162 dev_type_poll(usbpoll);
163 dev_type_kqfilter(usbkqfilter);
164 
165 const struct cdevsw usb_cdevsw = {
166 	.d_open = usbopen,
167 	.d_close = usbclose,
168 	.d_read = usbread,
169 	.d_write = nowrite,
170 	.d_ioctl = usbioctl,
171 	.d_stop = nostop,
172 	.d_tty = notty,
173 	.d_poll = usbpoll,
174 	.d_mmap = nommap,
175 	.d_kqfilter = usbkqfilter,
176 	.d_discard = nodiscard,
177 	.d_flag = D_OTHER
178 };
179 
180 Static void	usb_discover(struct usb_softc *);
181 Static void	usb_create_event_thread(device_t);
182 Static void	usb_event_thread(void *);
183 Static void	usb_task_thread(void *);
184 
185 #define USB_MAX_EVENTS 100
186 struct usb_event_q {
187 	struct usb_event ue;
188 	SIMPLEQ_ENTRY(usb_event_q) next;
189 };
190 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
191 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
192 Static int usb_nevents = 0;
193 Static struct selinfo usb_selevent;
194 Static kmutex_t usb_event_lock;
195 Static kcondvar_t usb_event_cv;
196 /* XXX this is gross and broken */
197 Static proc_t *usb_async_proc;  /* process that wants USB SIGIO */
198 Static void *usb_async_sih;
199 Static int usb_dev_open = 0;
200 Static struct usb_event *usb_alloc_event(void);
201 Static void usb_free_event(struct usb_event *);
202 Static void usb_add_event(int, struct usb_event *);
203 Static int usb_get_next_event(struct usb_event *);
204 Static void usb_async_intr(void *);
205 Static void usb_soft_intr(void *);
206 
207 Static const char *usbrev_str[] = USBREV_STR;
208 
209 static int usb_match(device_t, cfdata_t, void *);
210 static void usb_attach(device_t, device_t, void *);
211 static int usb_detach(device_t, int);
212 static int usb_activate(device_t, enum devact);
213 static void usb_childdet(device_t, device_t);
214 static int usb_once_init(void);
215 static void usb_doattach(device_t);
216 
217 
218 
219 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
220     usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
221     DVF_DETACH_SHUTDOWN);
222 
223 static const char *taskq_names[] = USB_TASKQ_NAMES;
224 
225 int
226 usb_match(device_t parent, cfdata_t match, void *aux)
227 {
228 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
229 
230 	return UMATCH_GENERIC;
231 }
232 
233 void
234 usb_attach(device_t parent, device_t self, void *aux)
235 {
236 	static ONCE_DECL(init_control);
237 	struct usb_softc *sc = device_private(self);
238 	int usbrev;
239 
240 	sc->sc_bus = aux;
241 	usbrev = sc->sc_bus->ub_revision;
242 
243 	cv_init(&sc->sc_bus->ub_needsexplore_cv, "usbevt");
244 	sc->sc_pmf_registered = false;
245 
246 	aprint_naive("\n");
247 	aprint_normal(": USB revision %s", usbrev_str[usbrev]);
248 	switch (usbrev) {
249 	case USBREV_1_0:
250 	case USBREV_1_1:
251 	case USBREV_2_0:
252 	case USBREV_3_0:
253 	case USBREV_3_1:
254 		break;
255 	default:
256 		aprint_error(", not supported\n");
257 		sc->sc_dying = 1;
258 		return;
259 	}
260 	aprint_normal("\n");
261 
262 	/* XXX we should have our own level */
263 	sc->sc_bus->ub_soft = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
264 	    usb_soft_intr, sc->sc_bus);
265 	if (sc->sc_bus->ub_soft == NULL) {
266 		aprint_error("%s: can't register softintr\n",
267 			     device_xname(self));
268 		sc->sc_dying = 1;
269 		return;
270 	}
271 
272 	sc->sc_bus->ub_methods->ubm_getlock(sc->sc_bus, &sc->sc_bus->ub_lock);
273 	KASSERT(sc->sc_bus->ub_lock != NULL);
274 
275 	RUN_ONCE(&init_control, usb_once_init);
276 	config_interrupts(self, usb_doattach);
277 }
278 
279 static int
280 usb_once_init(void)
281 {
282 	struct usb_taskq *taskq;
283 	int i;
284 
285 	USBHIST_LINK_STATIC(usbhist);
286 
287 	selinit(&usb_selevent);
288 	mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
289 	cv_init(&usb_event_cv, "usbrea");
290 
291 	for (i = 0; i < USB_NUM_TASKQS; i++) {
292 		taskq = &usb_taskq[i];
293 
294 		TAILQ_INIT(&taskq->tasks);
295 		/*
296 		 * Since USB task methods usb_{add,rem}_task are callable
297 		 * from any context, we have to make this lock a spinlock.
298 		 */
299 		mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
300 		cv_init(&taskq->cv, "usbtsk");
301 		taskq->name = taskq_names[i];
302 		taskq->current_task = NULL;
303 		if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
304 		    usb_task_thread, taskq, &taskq->task_thread_lwp,
305 		    "%s", taskq->name)) {
306 			printf("unable to create task thread: %s\n", taskq->name);
307 			panic("usb_create_event_thread task");
308 		}
309 		/*
310 		 * XXX we should make sure these threads are alive before
311 		 * end up using them in usb_doattach().
312 		 */
313 	}
314 
315 	KASSERT(usb_async_sih == NULL);
316 	usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
317 	   usb_async_intr, NULL);
318 
319 	return 0;
320 }
321 
322 static void
323 usb_doattach(device_t self)
324 {
325 	struct usb_softc *sc = device_private(self);
326 	struct usbd_device *dev;
327 	usbd_status err;
328 	int speed;
329 	struct usb_event *ue;
330 
331 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
332 
333 	sc->sc_bus->ub_usbctl = self;
334 	sc->sc_port.up_power = USB_MAX_POWER;
335 
336 	switch (sc->sc_bus->ub_revision) {
337 	case USBREV_1_0:
338 	case USBREV_1_1:
339 		speed = USB_SPEED_FULL;
340 		break;
341 	case USBREV_2_0:
342 		speed = USB_SPEED_HIGH;
343 		break;
344 	case USBREV_3_0:
345 		speed = USB_SPEED_SUPER;
346 		break;
347 	case USBREV_3_1:
348 		speed = USB_SPEED_SUPER_PLUS;
349 		break;
350 	default:
351 		panic("usb_doattach");
352 	}
353 
354 	ue = usb_alloc_event();
355 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
356 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
357 
358 	err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
359 		  &sc->sc_port);
360 	if (!err) {
361 		dev = sc->sc_port.up_dev;
362 		if (dev->ud_hub == NULL) {
363 			sc->sc_dying = 1;
364 			aprint_error("%s: root device is not a hub\n",
365 				     device_xname(self));
366 			return;
367 		}
368 		sc->sc_bus->ub_roothub = dev;
369 		usb_create_event_thread(self);
370 	} else {
371 		aprint_error("%s: root hub problem, error=%s\n",
372 			     device_xname(self), usbd_errstr(err));
373 		sc->sc_dying = 1;
374 	}
375 
376 	/*
377 	 * Drop this reference after the first set of attachments in the
378 	 * event thread.
379 	 */
380 	config_pending_incr(self);
381 
382 	if (!pmf_device_register(self, NULL, NULL))
383 		aprint_error_dev(self, "couldn't establish power handler\n");
384 	else
385 		sc->sc_pmf_registered = true;
386 
387 	return;
388 }
389 
390 void
391 usb_create_event_thread(device_t self)
392 {
393 	struct usb_softc *sc = device_private(self);
394 
395 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
396 	    usb_event_thread, sc, &sc->sc_event_thread,
397 	    "%s", device_xname(self))) {
398 		printf("%s: unable to create event thread for\n",
399 		       device_xname(self));
400 		panic("usb_create_event_thread");
401 	}
402 }
403 
404 /*
405  * Add a task to be performed by the task thread.  This function can be
406  * called from any context and the task will be executed in a process
407  * context ASAP.
408  */
409 void
410 usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
411 {
412 	struct usb_taskq *taskq;
413 
414 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
415 
416 	KASSERT(0 <= queue);
417 	KASSERT(queue < USB_NUM_TASKQS);
418 	taskq = &usb_taskq[queue];
419 	mutex_enter(&taskq->lock);
420 	if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
421 	    USB_NUM_TASKQS) {
422 		DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
423 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
424 		cv_signal(&taskq->cv);
425 	} else {
426 		DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
427 	}
428 	mutex_exit(&taskq->lock);
429 }
430 
431 /*
432  * usb_rem_task(dev, task)
433  *
434  *	If task is queued to run, remove it from the queue.
435  *
436  *	Caller is _not_ guaranteed that the task is not running when
437  *	this is done.
438  *
439  *	Never sleeps.
440  */
441 void
442 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
443 {
444 	unsigned queue;
445 
446 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
447 
448 	while ((queue = task->queue) != USB_NUM_TASKQS) {
449 		struct usb_taskq *taskq = &usb_taskq[queue];
450 		mutex_enter(&taskq->lock);
451 		if (__predict_true(task->queue == queue)) {
452 			TAILQ_REMOVE(&taskq->tasks, task, next);
453 			task->queue = USB_NUM_TASKQS;
454 			mutex_exit(&taskq->lock);
455 			break;
456 		}
457 		mutex_exit(&taskq->lock);
458 	}
459 }
460 
461 /*
462  * usb_rem_task_wait(dev, task, queue, interlock)
463  *
464  *	If task is scheduled to run, remove it from the queue.  If it
465  *	may have already begun to run, drop interlock if not null, wait
466  *	for it to complete, and reacquire interlock if not null.
467  *	Return true if it successfully removed the task from the queue,
468  *	false if not.
469  *
470  *	Caller MUST guarantee that task will not be scheduled on a
471  *	_different_ queue, at least until after this returns.
472  *
473  *	If caller guarantees that task will not be scheduled on the
474  *	same queue before this returns, then caller is guaranteed that
475  *	the task is not running at all when this returns.
476  *
477  *	May sleep.
478  */
479 bool
480 usb_rem_task_wait(struct usbd_device *dev, struct usb_task *task, int queue,
481     kmutex_t *interlock)
482 {
483 	struct usb_taskq *taskq;
484 	int queue1;
485 	bool removed;
486 
487 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
488 	ASSERT_SLEEPABLE();
489 	KASSERT(0 <= queue);
490 	KASSERT(queue < USB_NUM_TASKQS);
491 
492 	taskq = &usb_taskq[queue];
493 	mutex_enter(&taskq->lock);
494 	queue1 = task->queue;
495 	if (queue1 == USB_NUM_TASKQS) {
496 		/*
497 		 * It is not on the queue.  It may be about to run, or
498 		 * it may have already finished running -- there is no
499 		 * stopping it now.  Wait for it if it is running.
500 		 */
501 		if (interlock)
502 			mutex_exit(interlock);
503 		while (taskq->current_task == task)
504 			cv_wait(&taskq->cv, &taskq->lock);
505 		removed = false;
506 	} else {
507 		/*
508 		 * It is still on the queue.  We can stop it before the
509 		 * task thread will run it.
510 		 */
511 		KASSERTMSG(queue1 == queue, "task %p on q%d expected on q%d",
512 		    task, queue1, queue);
513 		TAILQ_REMOVE(&taskq->tasks, task, next);
514 		task->queue = USB_NUM_TASKQS;
515 		removed = true;
516 	}
517 	mutex_exit(&taskq->lock);
518 
519 	/*
520 	 * If there's an interlock, and we dropped it to wait,
521 	 * reacquire it.
522 	 */
523 	if (interlock && !removed)
524 		mutex_enter(interlock);
525 
526 	return removed;
527 }
528 
529 void
530 usb_event_thread(void *arg)
531 {
532 	struct usb_softc *sc = arg;
533 	struct usbd_bus *bus = sc->sc_bus;
534 
535 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
536 
537 	/*
538 	 * In case this controller is a companion controller to an
539 	 * EHCI controller we need to wait until the EHCI controller
540 	 * has grabbed the port.
541 	 * XXX It would be nicer to do this with a tsleep(), but I don't
542 	 * know how to synchronize the creation of the threads so it
543 	 * will work.
544 	 */
545 	usb_delay_ms(bus, 500);
546 
547 	/* Make sure first discover does something. */
548 	mutex_enter(bus->ub_lock);
549 	sc->sc_bus->ub_needsexplore = 1;
550 	usb_discover(sc);
551 	mutex_exit(bus->ub_lock);
552 
553 	/* Drop the config_pending reference from attach. */
554 	config_pending_decr(bus->ub_usbctl);
555 
556 	mutex_enter(bus->ub_lock);
557 	while (!sc->sc_dying) {
558 #if 0 /* not yet */
559 		while (sc->sc_bus->ub_usepolling)
560 			kpause("usbpoll", true, hz, bus->ub_lock);
561 #endif
562 
563 		if (usb_noexplore < 2)
564 			usb_discover(sc);
565 
566 		cv_timedwait(&bus->ub_needsexplore_cv,
567 		    bus->ub_lock, usb_noexplore ? 0 : hz * 60);
568 
569 		DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
570 	}
571 	sc->sc_event_thread = NULL;
572 
573 	/* In case parent is waiting for us to exit. */
574 	cv_signal(&bus->ub_needsexplore_cv);
575 	mutex_exit(bus->ub_lock);
576 
577 	DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
578 	kthread_exit(0);
579 }
580 
581 void
582 usb_task_thread(void *arg)
583 {
584 	struct usb_task *task;
585 	struct usb_taskq *taskq;
586 	bool mpsafe;
587 
588 	taskq = arg;
589 
590 	USBHIST_FUNC();
591 	USBHIST_CALLARGS(usbdebug, "start taskq %#jx",
592 	    (uintptr_t)taskq, 0, 0, 0);
593 
594 	mutex_enter(&taskq->lock);
595 	for (;;) {
596 		task = TAILQ_FIRST(&taskq->tasks);
597 		if (task == NULL) {
598 			cv_wait(&taskq->cv, &taskq->lock);
599 			task = TAILQ_FIRST(&taskq->tasks);
600 		}
601 		DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
602 		if (task != NULL) {
603 			mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
604 			TAILQ_REMOVE(&taskq->tasks, task, next);
605 			task->queue = USB_NUM_TASKQS;
606 			taskq->current_task = task;
607 			mutex_exit(&taskq->lock);
608 
609 			if (!mpsafe)
610 				KERNEL_LOCK(1, curlwp);
611 			task->fun(task->arg);
612 			/* Can't dereference task after this point.  */
613 			if (!mpsafe)
614 				KERNEL_UNLOCK_ONE(curlwp);
615 
616 			mutex_enter(&taskq->lock);
617 			KASSERTMSG(taskq->current_task == task,
618 			    "somebody scribbled on usb taskq %p", taskq);
619 			taskq->current_task = NULL;
620 			cv_broadcast(&taskq->cv);
621 		}
622 	}
623 	mutex_exit(&taskq->lock);
624 }
625 
626 int
627 usbctlprint(void *aux, const char *pnp)
628 {
629 	/* only "usb"es can attach to host controllers */
630 	if (pnp)
631 		aprint_normal("usb at %s", pnp);
632 
633 	return UNCONF;
634 }
635 
636 int
637 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
638 {
639 	int unit = minor(dev);
640 	struct usb_softc *sc;
641 
642 	if (unit == USB_DEV_MINOR) {
643 		if (usb_dev_open)
644 			return EBUSY;
645 		usb_dev_open = 1;
646 		mutex_enter(proc_lock);
647 		usb_async_proc = 0;
648 		mutex_exit(proc_lock);
649 		return 0;
650 	}
651 
652 	sc = device_lookup_private(&usb_cd, unit);
653 	if (!sc)
654 		return ENXIO;
655 
656 	if (sc->sc_dying)
657 		return EIO;
658 
659 	return 0;
660 }
661 
662 int
663 usbread(dev_t dev, struct uio *uio, int flag)
664 {
665 	struct usb_event *ue;
666 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
667 	int useold = 0;
668 	int error, n;
669 
670 	if (minor(dev) != USB_DEV_MINOR)
671 		return ENXIO;
672 
673 	switch (uio->uio_resid) {
674 	case sizeof(struct usb_event_old):
675 		ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
676 		useold = 1;
677 		/* FALLTHROUGH */
678 	case sizeof(struct usb_event):
679 		ue = usb_alloc_event();
680 		break;
681 	default:
682 		return EINVAL;
683 	}
684 
685 	error = 0;
686 	mutex_enter(&usb_event_lock);
687 	for (;;) {
688 		n = usb_get_next_event(ue);
689 		if (n != 0)
690 			break;
691 		if (flag & IO_NDELAY) {
692 			error = EWOULDBLOCK;
693 			break;
694 		}
695 		error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
696 		if (error)
697 			break;
698 	}
699 	mutex_exit(&usb_event_lock);
700 	if (!error) {
701 		if (useold) { /* copy fields to old struct */
702 			MODULE_HOOK_CALL(usb_subr_copy_30_hook,
703 			    (ue, ueo, uio), enosys(), error);
704 			if (error == ENOSYS)
705 				error = EINVAL;
706 
707 			if (!error)
708 				error = uiomove((void *)ueo, sizeof(*ueo), uio);
709 		} else
710 			error = uiomove((void *)ue, sizeof(*ue), uio);
711 	}
712 	usb_free_event(ue);
713 	if (ueo)
714 		kmem_free(ueo, sizeof(struct usb_event_old));
715 
716 	return error;
717 }
718 
719 int
720 usbclose(dev_t dev, int flag, int mode,
721     struct lwp *l)
722 {
723 	int unit = minor(dev);
724 
725 	if (unit == USB_DEV_MINOR) {
726 		mutex_enter(proc_lock);
727 		usb_async_proc = 0;
728 		mutex_exit(proc_lock);
729 		usb_dev_open = 0;
730 	}
731 
732 	return 0;
733 }
734 
735 int
736 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
737 {
738 	struct usb_softc *sc;
739 	int unit = minor(devt);
740 
741 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "cmd %#jx", cmd, 0, 0, 0);
742 
743 	if (unit == USB_DEV_MINOR) {
744 		switch (cmd) {
745 		case FIONBIO:
746 			/* All handled in the upper FS layer. */
747 			return 0;
748 
749 		case FIOASYNC:
750 			mutex_enter(proc_lock);
751 			if (*(int *)data)
752 				usb_async_proc = l->l_proc;
753 			else
754 				usb_async_proc = 0;
755 			mutex_exit(proc_lock);
756 			return 0;
757 
758 		default:
759 			return EINVAL;
760 		}
761 	}
762 
763 	sc = device_lookup_private(&usb_cd, unit);
764 
765 	if (sc->sc_dying)
766 		return EIO;
767 
768 	int error = 0;
769 	switch (cmd) {
770 #ifdef USB_DEBUG
771 	case USB_SETDEBUG:
772 		if (!(flag & FWRITE))
773 			return EBADF;
774 		usbdebug  = ((*(int *)data) & 0x000000ff);
775 		break;
776 #endif /* USB_DEBUG */
777 	case USB_REQUEST:
778 	{
779 		struct usb_ctl_request *ur = (void *)data;
780 		int len = UGETW(ur->ucr_request.wLength);
781 		struct iovec iov;
782 		struct uio uio;
783 		void *ptr = 0;
784 		int addr = ur->ucr_addr;
785 		usbd_status err;
786 
787 		if (!(flag & FWRITE)) {
788 			error = EBADF;
789 			goto fail;
790 		}
791 
792 		DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
793 		if (len < 0 || len > 32768) {
794 			error = EINVAL;
795 			goto fail;
796 		}
797 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
798 			error = EINVAL;
799 			goto fail;
800 		}
801 		size_t dindex = usb_addr2dindex(addr);
802 		if (sc->sc_bus->ub_devices[dindex] == NULL) {
803 			error = EINVAL;
804 			goto fail;
805 		}
806 		if (len != 0) {
807 			iov.iov_base = (void *)ur->ucr_data;
808 			iov.iov_len = len;
809 			uio.uio_iov = &iov;
810 			uio.uio_iovcnt = 1;
811 			uio.uio_resid = len;
812 			uio.uio_offset = 0;
813 			uio.uio_rw =
814 				ur->ucr_request.bmRequestType & UT_READ ?
815 				UIO_READ : UIO_WRITE;
816 			uio.uio_vmspace = l->l_proc->p_vmspace;
817 			ptr = kmem_alloc(len, KM_SLEEP);
818 			if (uio.uio_rw == UIO_WRITE) {
819 				error = uiomove(ptr, len, &uio);
820 				if (error)
821 					goto ret;
822 			}
823 		}
824 		err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
825 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
826 			  USBD_DEFAULT_TIMEOUT);
827 		if (err) {
828 			error = EIO;
829 			goto ret;
830 		}
831 		if (len > ur->ucr_actlen)
832 			len = ur->ucr_actlen;
833 		if (len != 0) {
834 			if (uio.uio_rw == UIO_READ) {
835 				error = uiomove(ptr, len, &uio);
836 				if (error)
837 					goto ret;
838 			}
839 		}
840 	ret:
841 		if (ptr) {
842 			len = UGETW(ur->ucr_request.wLength);
843 			kmem_free(ptr, len);
844 		}
845 		break;
846 	}
847 
848 	case USB_DEVICEINFO:
849 	{
850 		struct usbd_device *dev;
851 		struct usb_device_info *di = (void *)data;
852 		int addr = di->udi_addr;
853 
854 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
855 			error = EINVAL;
856 			goto fail;
857 		}
858 		size_t dindex = usb_addr2dindex(addr);
859 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
860 			error = ENXIO;
861 			goto fail;
862 		}
863 		usbd_fill_deviceinfo(dev, di, 1);
864 		break;
865 	}
866 
867 	case USB_DEVICEINFO_OLD:
868 	{
869 		struct usbd_device *dev;
870 		struct usb_device_info_old *di = (void *)data;
871 		int addr = di->udi_addr;
872 
873 		if (addr < 1 || addr >= USB_MAX_DEVICES) {
874 			error = EINVAL;
875 			goto fail;
876 		}
877 		size_t dindex = usb_addr2dindex(addr);
878 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
879 			error = ENXIO;
880 			goto fail;
881 		}
882 		MODULE_HOOK_CALL(usb_subr_fill_30_hook,
883 		    (dev, di, 1, usbd_devinfo_vp, usbd_printBCD),
884 		    enosys(), error);
885 		if (error == ENOSYS)
886 			error = EINVAL;
887 		if (error)
888 			goto fail;
889 		break;
890 	}
891 
892 	case USB_DEVICESTATS:
893 		*(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
894 		break;
895 
896 	default:
897 		error = EINVAL;
898 	}
899 
900 fail:
901 
902 	DPRINTF("... done (error = %jd)", error, 0, 0, 0);
903 
904 	return error;
905 }
906 
907 int
908 usbpoll(dev_t dev, int events, struct lwp *l)
909 {
910 	int revents, mask;
911 
912 	if (minor(dev) == USB_DEV_MINOR) {
913 		revents = 0;
914 		mask = POLLIN | POLLRDNORM;
915 
916 		mutex_enter(&usb_event_lock);
917 		if (events & mask && usb_nevents > 0)
918 			revents |= events & mask;
919 		if (revents == 0 && events & mask)
920 			selrecord(l, &usb_selevent);
921 		mutex_exit(&usb_event_lock);
922 
923 		return revents;
924 	} else {
925 		return 0;
926 	}
927 }
928 
929 static void
930 filt_usbrdetach(struct knote *kn)
931 {
932 
933 	mutex_enter(&usb_event_lock);
934 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
935 	mutex_exit(&usb_event_lock);
936 }
937 
938 static int
939 filt_usbread(struct knote *kn, long hint)
940 {
941 
942 	if (usb_nevents == 0)
943 		return 0;
944 
945 	kn->kn_data = sizeof(struct usb_event);
946 	return 1;
947 }
948 
949 static const struct filterops usbread_filtops = {
950 	.f_isfd = 1,
951 	.f_attach = NULL,
952 	.f_detach = filt_usbrdetach,
953 	.f_event = filt_usbread,
954 };
955 
956 int
957 usbkqfilter(dev_t dev, struct knote *kn)
958 {
959 	struct klist *klist;
960 
961 	switch (kn->kn_filter) {
962 	case EVFILT_READ:
963 		if (minor(dev) != USB_DEV_MINOR)
964 			return 1;
965 		klist = &usb_selevent.sel_klist;
966 		kn->kn_fop = &usbread_filtops;
967 		break;
968 
969 	default:
970 		return EINVAL;
971 	}
972 
973 	kn->kn_hook = NULL;
974 
975 	mutex_enter(&usb_event_lock);
976 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
977 	mutex_exit(&usb_event_lock);
978 
979 	return 0;
980 }
981 
982 /* Explore device tree from the root. */
983 Static void
984 usb_discover(struct usb_softc *sc)
985 {
986 	struct usbd_bus *bus = sc->sc_bus;
987 
988 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
989 
990 	KASSERT(mutex_owned(bus->ub_lock));
991 
992 	if (usb_noexplore > 1)
993 		return;
994 
995 	/*
996 	 * We need mutual exclusion while traversing the device tree,
997 	 * but this is guaranteed since this function is only called
998 	 * from the event thread for the controller.
999 	 *
1000 	 * Also, we now have bus->ub_lock held, and in combination
1001 	 * with ub_exploring, avoids interferring with polling.
1002 	 */
1003 	while (bus->ub_needsexplore && !sc->sc_dying) {
1004 		bus->ub_needsexplore = 0;
1005 		mutex_exit(sc->sc_bus->ub_lock);
1006 		bus->ub_roothub->ud_hub->uh_explore(bus->ub_roothub);
1007 		mutex_enter(bus->ub_lock);
1008 	}
1009 }
1010 
1011 void
1012 usb_needs_explore(struct usbd_device *dev)
1013 {
1014 
1015 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1016 
1017 	mutex_enter(dev->ud_bus->ub_lock);
1018 	dev->ud_bus->ub_needsexplore = 1;
1019 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
1020 	mutex_exit(dev->ud_bus->ub_lock);
1021 }
1022 
1023 void
1024 usb_needs_reattach(struct usbd_device *dev)
1025 {
1026 
1027 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1028 
1029 	mutex_enter(dev->ud_bus->ub_lock);
1030 	dev->ud_powersrc->up_reattach = 1;
1031 	dev->ud_bus->ub_needsexplore = 1;
1032 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
1033 	mutex_exit(dev->ud_bus->ub_lock);
1034 }
1035 
1036 /* Called at with usb_event_lock held. */
1037 int
1038 usb_get_next_event(struct usb_event *ue)
1039 {
1040 	struct usb_event_q *ueq;
1041 
1042 	KASSERT(mutex_owned(&usb_event_lock));
1043 
1044 	if (usb_nevents <= 0)
1045 		return 0;
1046 	ueq = SIMPLEQ_FIRST(&usb_events);
1047 #ifdef DIAGNOSTIC
1048 	if (ueq == NULL) {
1049 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
1050 		usb_nevents = 0;
1051 		return 0;
1052 	}
1053 #endif
1054 	if (ue)
1055 		*ue = ueq->ue;
1056 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
1057 	usb_free_event((struct usb_event *)(void *)ueq);
1058 	usb_nevents--;
1059 	return 1;
1060 }
1061 
1062 void
1063 usbd_add_dev_event(int type, struct usbd_device *udev)
1064 {
1065 	struct usb_event *ue = usb_alloc_event();
1066 
1067 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
1068 	usb_add_event(type, ue);
1069 }
1070 
1071 void
1072 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
1073 {
1074 	struct usb_event *ue = usb_alloc_event();
1075 
1076 	ue->u.ue_driver.ue_cookie = udev->ud_cookie;
1077 	strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
1078 	    sizeof(ue->u.ue_driver.ue_devname));
1079 	usb_add_event(type, ue);
1080 }
1081 
1082 Static struct usb_event *
1083 usb_alloc_event(void)
1084 {
1085 	/* Yes, this is right; we allocate enough so that we can use it later */
1086 	return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
1087 }
1088 
1089 Static void
1090 usb_free_event(struct usb_event *uep)
1091 {
1092 	kmem_free(uep, sizeof(struct usb_event_q));
1093 }
1094 
1095 Static void
1096 usb_add_event(int type, struct usb_event *uep)
1097 {
1098 	struct usb_event_q *ueq;
1099 	struct timeval thetime;
1100 
1101 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1102 
1103 	microtime(&thetime);
1104 	/* Don't want to wait here with usb_event_lock held */
1105 	ueq = (struct usb_event_q *)(void *)uep;
1106 	ueq->ue = *uep;
1107 	ueq->ue.ue_type = type;
1108 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
1109 
1110 	mutex_enter(&usb_event_lock);
1111 	if (++usb_nevents >= USB_MAX_EVENTS) {
1112 		/* Too many queued events, drop an old one. */
1113 		DPRINTF("event dropped", 0, 0, 0, 0);
1114 		(void)usb_get_next_event(0);
1115 	}
1116 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
1117 	cv_signal(&usb_event_cv);
1118 	selnotify(&usb_selevent, 0, 0);
1119 	if (usb_async_proc != NULL) {
1120 		kpreempt_disable();
1121 		softint_schedule(usb_async_sih);
1122 		kpreempt_enable();
1123 	}
1124 	mutex_exit(&usb_event_lock);
1125 }
1126 
1127 Static void
1128 usb_async_intr(void *cookie)
1129 {
1130 	proc_t *proc;
1131 
1132 	mutex_enter(proc_lock);
1133 	if ((proc = usb_async_proc) != NULL)
1134 		psignal(proc, SIGIO);
1135 	mutex_exit(proc_lock);
1136 }
1137 
1138 Static void
1139 usb_soft_intr(void *arg)
1140 {
1141 	struct usbd_bus *bus = arg;
1142 
1143 	mutex_enter(bus->ub_lock);
1144 	bus->ub_methods->ubm_softint(bus);
1145 	mutex_exit(bus->ub_lock);
1146 }
1147 
1148 void
1149 usb_schedsoftintr(struct usbd_bus *bus)
1150 {
1151 
1152 	USBHIST_FUNC();
1153 	USBHIST_CALLARGS(usbdebug, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
1154 
1155 	/* In case the bus never finished setting up. */
1156 	if (__predict_false(bus->ub_soft == NULL))
1157 		return;
1158 
1159 	if (bus->ub_usepolling) {
1160 		bus->ub_methods->ubm_softint(bus);
1161 	} else {
1162 		kpreempt_disable();
1163 		softint_schedule(bus->ub_soft);
1164 		kpreempt_enable();
1165 	}
1166 }
1167 
1168 int
1169 usb_activate(device_t self, enum devact act)
1170 {
1171 	struct usb_softc *sc = device_private(self);
1172 
1173 	switch (act) {
1174 	case DVACT_DEACTIVATE:
1175 		sc->sc_dying = 1;
1176 		return 0;
1177 	default:
1178 		return EOPNOTSUPP;
1179 	}
1180 }
1181 
1182 void
1183 usb_childdet(device_t self, device_t child)
1184 {
1185 	int i;
1186 	struct usb_softc *sc = device_private(self);
1187 	struct usbd_device *dev;
1188 
1189 	if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
1190 		return;
1191 
1192 	for (i = 0; i < dev->ud_subdevlen; i++)
1193 		if (dev->ud_subdevs[i] == child)
1194 			dev->ud_subdevs[i] = NULL;
1195 }
1196 
1197 int
1198 usb_detach(device_t self, int flags)
1199 {
1200 	struct usb_softc *sc = device_private(self);
1201 	struct usb_event *ue;
1202 	int rc;
1203 
1204 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1205 
1206 	/* Make all devices disconnect. */
1207 	if (sc->sc_port.up_dev != NULL &&
1208 	    (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
1209 		return rc;
1210 
1211 	if (sc->sc_pmf_registered)
1212 		pmf_device_deregister(self);
1213 	/* Kill off event thread. */
1214 	sc->sc_dying = 1;
1215 	while (sc->sc_event_thread != NULL) {
1216 		mutex_enter(sc->sc_bus->ub_lock);
1217 		cv_signal(&sc->sc_bus->ub_needsexplore_cv);
1218 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
1219 		    sc->sc_bus->ub_lock, hz * 60);
1220 		mutex_exit(sc->sc_bus->ub_lock);
1221 	}
1222 	DPRINTF("event thread dead", 0, 0, 0, 0);
1223 
1224 	if (sc->sc_bus->ub_soft != NULL) {
1225 		softint_disestablish(sc->sc_bus->ub_soft);
1226 		sc->sc_bus->ub_soft = NULL;
1227 	}
1228 
1229 	ue = usb_alloc_event();
1230 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
1231 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1232 
1233 	cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
1234 
1235 	return 0;
1236 }
1237