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