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