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