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