usb.c revision 1.168.2.1 1 /* $NetBSD: usb.c,v 1.168.2.1 2018/03/29 11:20:03 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.1 2018/03/29 11:20:03 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 };
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 Static const char *usbrev_str[] = USBREV_STR;
203
204 static int usb_match(device_t, cfdata_t, void *);
205 static void usb_attach(device_t, device_t, void *);
206 static int usb_detach(device_t, int);
207 static int usb_activate(device_t, enum devact);
208 static void usb_childdet(device_t, device_t);
209 static int usb_once_init(void);
210 static void usb_doattach(device_t);
211
212 extern struct cfdriver usb_cd;
213
214 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
215 usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
216 DVF_DETACH_SHUTDOWN);
217
218 static const char *taskq_names[] = USB_TASKQ_NAMES;
219
220 int
221 usb_match(device_t parent, cfdata_t match, void *aux)
222 {
223 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
224
225 return UMATCH_GENERIC;
226 }
227
228 void
229 usb_attach(device_t parent, device_t self, void *aux)
230 {
231 static ONCE_DECL(init_control);
232 struct usb_softc *sc = device_private(self);
233 int usbrev;
234
235 sc->sc_bus = aux;
236 usbrev = sc->sc_bus->ub_revision;
237
238 aprint_naive("\n");
239 aprint_normal(": USB revision %s", usbrev_str[usbrev]);
240 switch (usbrev) {
241 case USBREV_1_0:
242 case USBREV_1_1:
243 case USBREV_2_0:
244 case USBREV_3_0:
245 break;
246 default:
247 aprint_error(", not supported\n");
248 sc->sc_dying = 1;
249 return;
250 }
251 aprint_normal("\n");
252
253 /* XXX we should have our own level */
254 sc->sc_bus->ub_soft = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
255 usb_soft_intr, sc->sc_bus);
256 if (sc->sc_bus->ub_soft == NULL) {
257 aprint_error("%s: can't register softintr\n",
258 device_xname(self));
259 sc->sc_dying = 1;
260 return;
261 }
262
263 sc->sc_bus->ub_methods->ubm_getlock(sc->sc_bus, &sc->sc_bus->ub_lock);
264 KASSERT(sc->sc_bus->ub_lock != NULL);
265
266 RUN_ONCE(&init_control, usb_once_init);
267 config_interrupts(self, usb_doattach);
268 }
269
270 static int
271 usb_once_init(void)
272 {
273 struct usb_taskq *taskq;
274 int i;
275
276 USBHIST_LINK_STATIC(usbhist);
277
278 selinit(&usb_selevent);
279 mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
280 cv_init(&usb_event_cv, "usbrea");
281
282 for (i = 0; i < USB_NUM_TASKQS; i++) {
283 taskq = &usb_taskq[i];
284
285 TAILQ_INIT(&taskq->tasks);
286 /*
287 * Since USB task methods usb_{add,rem}_task are callable
288 * from any context, we have to make this lock a spinlock.
289 */
290 mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
291 cv_init(&taskq->cv, "usbtsk");
292 taskq->name = taskq_names[i];
293 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
294 usb_task_thread, taskq, &taskq->task_thread_lwp,
295 "%s", taskq->name)) {
296 printf("unable to create task thread: %s\n", taskq->name);
297 panic("usb_create_event_thread task");
298 }
299 /*
300 * XXX we should make sure these threads are alive before
301 * end up using them in usb_doattach().
302 */
303 }
304 return 0;
305 }
306
307 static void
308 usb_doattach(device_t self)
309 {
310 struct usb_softc *sc = device_private(self);
311 struct usbd_device *dev;
312 usbd_status err;
313 int speed;
314 struct usb_event *ue;
315
316 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
317
318 sc->sc_bus->ub_usbctl = self;
319 sc->sc_port.up_power = USB_MAX_POWER;
320
321 switch (sc->sc_bus->ub_revision) {
322 case USBREV_1_0:
323 case USBREV_1_1:
324 speed = USB_SPEED_FULL;
325 break;
326 case USBREV_2_0:
327 speed = USB_SPEED_HIGH;
328 break;
329 case USBREV_3_0:
330 speed = USB_SPEED_SUPER;
331 break;
332 default:
333 panic("usb_doattach");
334 }
335
336 cv_init(&sc->sc_bus->ub_needsexplore_cv, "usbevt");
337
338 ue = usb_alloc_event();
339 ue->u.ue_ctrlr.ue_bus = device_unit(self);
340 usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
341
342 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
343 &sc->sc_port);
344 if (!err) {
345 dev = sc->sc_port.up_dev;
346 if (dev->ud_hub == NULL) {
347 sc->sc_dying = 1;
348 aprint_error("%s: root device is not a hub\n",
349 device_xname(self));
350 return;
351 }
352 sc->sc_bus->ub_roothub = dev;
353 usb_create_event_thread(self);
354 #if 1
355 /*
356 * Turning this code off will delay attachment of USB devices
357 * until the USB event thread is running, which means that
358 * the keyboard will not work until after cold boot.
359 */
360 if (cold && (device_cfdata(self)->cf_flags & 1))
361 dev->ud_hub->uh_explore(sc->sc_bus->ub_roothub);
362 #endif
363 } else {
364 aprint_error("%s: root hub problem, error=%s\n",
365 device_xname(self), usbd_errstr(err));
366 sc->sc_dying = 1;
367 }
368
369 config_pending_incr(self);
370
371 if (!pmf_device_register(self, NULL, NULL))
372 aprint_error_dev(self, "couldn't establish power handler\n");
373
374 usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
375 usb_async_intr, NULL);
376
377 return;
378 }
379
380 void
381 usb_create_event_thread(device_t self)
382 {
383 struct usb_softc *sc = device_private(self);
384
385 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
386 usb_event_thread, sc, &sc->sc_event_thread,
387 "%s", device_xname(self))) {
388 printf("%s: unable to create event thread for\n",
389 device_xname(self));
390 panic("usb_create_event_thread");
391 }
392 }
393
394 /*
395 * Add a task to be performed by the task thread. This function can be
396 * called from any context and the task will be executed in a process
397 * context ASAP.
398 */
399 void
400 usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
401 {
402 struct usb_taskq *taskq;
403
404 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
405
406 KASSERT(0 <= queue);
407 KASSERT(queue < USB_NUM_TASKQS);
408 taskq = &usb_taskq[queue];
409 mutex_enter(&taskq->lock);
410 if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
411 USB_NUM_TASKQS) {
412 DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
413 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
414 cv_signal(&taskq->cv);
415 } else {
416 DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
417 }
418 mutex_exit(&taskq->lock);
419 }
420
421 /*
422 * XXX This does not wait for completion! Most uses need such an
423 * operation. Urgh...
424 */
425 void
426 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
427 {
428 unsigned queue;
429
430 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
431
432 while ((queue = task->queue) != USB_NUM_TASKQS) {
433 struct usb_taskq *taskq = &usb_taskq[queue];
434 mutex_enter(&taskq->lock);
435 if (__predict_true(task->queue == queue)) {
436 TAILQ_REMOVE(&taskq->tasks, task, next);
437 task->queue = USB_NUM_TASKQS;
438 mutex_exit(&taskq->lock);
439 break;
440 }
441 mutex_exit(&taskq->lock);
442 }
443 }
444
445 void
446 usb_event_thread(void *arg)
447 {
448 struct usb_softc *sc = arg;
449
450 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
451
452 /*
453 * In case this controller is a companion controller to an
454 * EHCI controller we need to wait until the EHCI controller
455 * has grabbed the port.
456 * XXX It would be nicer to do this with a tsleep(), but I don't
457 * know how to synchronize the creation of the threads so it
458 * will work.
459 */
460 usb_delay_ms(sc->sc_bus, 500);
461
462 /* Make sure first discover does something. */
463 mutex_enter(sc->sc_bus->ub_lock);
464 sc->sc_bus->ub_needsexplore = 1;
465 usb_discover(sc);
466 mutex_exit(sc->sc_bus->ub_lock);
467 config_pending_decr(sc->sc_bus->ub_usbctl);
468
469 mutex_enter(sc->sc_bus->ub_lock);
470 while (!sc->sc_dying) {
471 if (usb_noexplore < 2)
472 usb_discover(sc);
473
474 cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
475 sc->sc_bus->ub_lock, usb_noexplore ? 0 : hz * 60);
476
477 DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
478 }
479 sc->sc_event_thread = NULL;
480
481 /* In case parent is waiting for us to exit. */
482 cv_signal(&sc->sc_bus->ub_needsexplore_cv);
483 mutex_exit(sc->sc_bus->ub_lock);
484
485 DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
486 kthread_exit(0);
487 }
488
489 void
490 usb_task_thread(void *arg)
491 {
492 struct usb_task *task;
493 struct usb_taskq *taskq;
494 bool mpsafe;
495
496 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
497
498 taskq = arg;
499 DPRINTF("start taskq %#jx", (uintptr_t)taskq, 0, 0, 0);
500
501 mutex_enter(&taskq->lock);
502 for (;;) {
503 task = TAILQ_FIRST(&taskq->tasks);
504 if (task == NULL) {
505 cv_wait(&taskq->cv, &taskq->lock);
506 task = TAILQ_FIRST(&taskq->tasks);
507 }
508 DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
509 if (task != NULL) {
510 mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
511 TAILQ_REMOVE(&taskq->tasks, task, next);
512 task->queue = USB_NUM_TASKQS;
513 mutex_exit(&taskq->lock);
514
515 if (!mpsafe)
516 KERNEL_LOCK(1, curlwp);
517 task->fun(task->arg);
518 /* Can't dereference task after this point. */
519 if (!mpsafe)
520 KERNEL_UNLOCK_ONE(curlwp);
521
522 mutex_enter(&taskq->lock);
523 }
524 }
525 mutex_exit(&taskq->lock);
526 }
527
528 int
529 usbctlprint(void *aux, const char *pnp)
530 {
531 /* only "usb"es can attach to host controllers */
532 if (pnp)
533 aprint_normal("usb at %s", pnp);
534
535 return UNCONF;
536 }
537
538 int
539 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
540 {
541 int unit = minor(dev);
542 struct usb_softc *sc;
543
544 if (unit == USB_DEV_MINOR) {
545 if (usb_dev_open)
546 return EBUSY;
547 usb_dev_open = 1;
548 mutex_enter(proc_lock);
549 usb_async_proc = 0;
550 mutex_exit(proc_lock);
551 return 0;
552 }
553
554 sc = device_lookup_private(&usb_cd, unit);
555 if (!sc)
556 return ENXIO;
557
558 if (sc->sc_dying)
559 return EIO;
560
561 return 0;
562 }
563
564 int
565 usbread(dev_t dev, struct uio *uio, int flag)
566 {
567 struct usb_event *ue;
568 struct usb_event_old *ueo = NULL; /* XXXGCC */
569 int useold = 0;
570 int error, n;
571
572 if (minor(dev) != USB_DEV_MINOR)
573 return ENXIO;
574
575 switch (uio->uio_resid) {
576 case sizeof(struct usb_event_old):
577 ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
578 useold = 1;
579 /* FALLTHRU */
580 case sizeof(struct usb_event):
581 ue = usb_alloc_event();
582 break;
583 default:
584 return EINVAL;
585 }
586
587 error = 0;
588 mutex_enter(&usb_event_lock);
589 for (;;) {
590 n = usb_get_next_event(ue);
591 if (n != 0)
592 break;
593 if (flag & IO_NDELAY) {
594 error = EWOULDBLOCK;
595 break;
596 }
597 error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
598 if (error)
599 break;
600 }
601 mutex_exit(&usb_event_lock);
602 if (!error) {
603 if (useold) { /* copy fields to old struct */
604 error = (*usb30_copy_to_old)(ue, ueo, uio);
605 if (error == ENOSYS)
606 error = EINVAL;
607
608 if (!error)
609 error = uiomove((void *)ueo, sizeof(*ueo), uio);
610 } else
611 error = uiomove((void *)ue, sizeof(*ue), uio);
612 }
613 usb_free_event(ue);
614 if (ueo)
615 kmem_free(ueo, sizeof(struct usb_event_old));
616
617 return error;
618 }
619
620 int
621 usbclose(dev_t dev, int flag, int mode,
622 struct lwp *l)
623 {
624 int unit = minor(dev);
625
626 if (unit == USB_DEV_MINOR) {
627 mutex_enter(proc_lock);
628 usb_async_proc = 0;
629 mutex_exit(proc_lock);
630 usb_dev_open = 0;
631 }
632
633 return 0;
634 }
635
636 int
637 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
638 {
639 struct usb_softc *sc;
640 int unit = minor(devt);
641
642 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
643
644 if (unit == USB_DEV_MINOR) {
645 switch (cmd) {
646 case FIONBIO:
647 /* All handled in the upper FS layer. */
648 return 0;
649
650 case FIOASYNC:
651 mutex_enter(proc_lock);
652 if (*(int *)data)
653 usb_async_proc = l->l_proc;
654 else
655 usb_async_proc = 0;
656 mutex_exit(proc_lock);
657 return 0;
658
659 default:
660 return EINVAL;
661 }
662 }
663
664 sc = device_lookup_private(&usb_cd, unit);
665
666 if (sc->sc_dying)
667 return EIO;
668
669 int error = 0;
670 DPRINTF("cmd %#jx", cmd, 0, 0, 0);
671 switch (cmd) {
672 #ifdef USB_DEBUG
673 case USB_SETDEBUG:
674 if (!(flag & FWRITE))
675 return EBADF;
676 usbdebug = ((*(int *)data) & 0x000000ff);
677 break;
678 #endif /* USB_DEBUG */
679 case USB_REQUEST:
680 {
681 struct usb_ctl_request *ur = (void *)data;
682 int len = UGETW(ur->ucr_request.wLength);
683 struct iovec iov;
684 struct uio uio;
685 void *ptr = 0;
686 int addr = ur->ucr_addr;
687 usbd_status err;
688
689 if (!(flag & FWRITE)) {
690 error = EBADF;
691 goto fail;
692 }
693
694 DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
695 if (len < 0 || len > 32768) {
696 error = EINVAL;
697 goto fail;
698 }
699 if (addr < 0 || addr >= USB_MAX_DEVICES) {
700 error = EINVAL;
701 goto fail;
702 }
703 size_t dindex = usb_addr2dindex(addr);
704 if (sc->sc_bus->ub_devices[dindex] == NULL) {
705 error = EINVAL;
706 goto fail;
707 }
708 if (len != 0) {
709 iov.iov_base = (void *)ur->ucr_data;
710 iov.iov_len = len;
711 uio.uio_iov = &iov;
712 uio.uio_iovcnt = 1;
713 uio.uio_resid = len;
714 uio.uio_offset = 0;
715 uio.uio_rw =
716 ur->ucr_request.bmRequestType & UT_READ ?
717 UIO_READ : UIO_WRITE;
718 uio.uio_vmspace = l->l_proc->p_vmspace;
719 ptr = kmem_alloc(len, KM_SLEEP);
720 if (uio.uio_rw == UIO_WRITE) {
721 error = uiomove(ptr, len, &uio);
722 if (error)
723 goto ret;
724 }
725 }
726 err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
727 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
728 USBD_DEFAULT_TIMEOUT);
729 if (err) {
730 error = EIO;
731 goto ret;
732 }
733 if (len > ur->ucr_actlen)
734 len = ur->ucr_actlen;
735 if (len != 0) {
736 if (uio.uio_rw == UIO_READ) {
737 error = uiomove(ptr, len, &uio);
738 if (error)
739 goto ret;
740 }
741 }
742 ret:
743 if (ptr) {
744 len = UGETW(ur->ucr_request.wLength);
745 kmem_free(ptr, len);
746 }
747 break;
748 }
749
750 case USB_DEVICEINFO:
751 {
752 struct usbd_device *dev;
753 struct usb_device_info *di = (void *)data;
754 int addr = di->udi_addr;
755
756 if (addr < 0 || addr >= USB_MAX_DEVICES) {
757 error = EINVAL;
758 goto fail;
759 }
760 size_t dindex = usb_addr2dindex(addr);
761 if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
762 error = ENXIO;
763 goto fail;
764 }
765 usbd_fill_deviceinfo(dev, di, 1);
766 break;
767 }
768
769 case USB_DEVICEINFO_OLD:
770 {
771 struct usbd_device *dev;
772 struct usb_device_info_old *di = (void *)data;
773 int addr = di->udi_addr;
774
775 if (addr < 1 || addr >= USB_MAX_DEVICES) {
776 error = EINVAL;
777 goto fail;
778 }
779 size_t dindex = usb_addr2dindex(addr);
780 if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
781 error = ENXIO;
782 goto fail;
783 }
784 error = (*usbd30_fill_deviceinfo_old)(dev, di, 1);
785 if (error == ENOSYS)
786 error = EINVAL;
787 if (error)
788 goto fail;
789 }
790
791 case USB_DEVICESTATS:
792 *(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
793 break;
794
795 default:
796 error = EINVAL;
797 }
798
799 fail:
800
801 DPRINTF("... done (error = %jd)", error, 0, 0, 0);
802
803 return error;
804 }
805
806 int
807 usbpoll(dev_t dev, int events, struct lwp *l)
808 {
809 int revents, mask;
810
811 if (minor(dev) == USB_DEV_MINOR) {
812 revents = 0;
813 mask = POLLIN | POLLRDNORM;
814
815 mutex_enter(&usb_event_lock);
816 if (events & mask && usb_nevents > 0)
817 revents |= events & mask;
818 if (revents == 0 && events & mask)
819 selrecord(l, &usb_selevent);
820 mutex_exit(&usb_event_lock);
821
822 return revents;
823 } else {
824 return 0;
825 }
826 }
827
828 static void
829 filt_usbrdetach(struct knote *kn)
830 {
831
832 mutex_enter(&usb_event_lock);
833 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
834 mutex_exit(&usb_event_lock);
835 }
836
837 static int
838 filt_usbread(struct knote *kn, long hint)
839 {
840
841 if (usb_nevents == 0)
842 return 0;
843
844 kn->kn_data = sizeof(struct usb_event);
845 return 1;
846 }
847
848 static const struct filterops usbread_filtops = {
849 .f_isfd = 1,
850 .f_attach = NULL,
851 .f_detach = filt_usbrdetach,
852 .f_event = filt_usbread,
853 };
854
855 int
856 usbkqfilter(dev_t dev, struct knote *kn)
857 {
858 struct klist *klist;
859
860 switch (kn->kn_filter) {
861 case EVFILT_READ:
862 if (minor(dev) != USB_DEV_MINOR)
863 return 1;
864 klist = &usb_selevent.sel_klist;
865 kn->kn_fop = &usbread_filtops;
866 break;
867
868 default:
869 return EINVAL;
870 }
871
872 kn->kn_hook = NULL;
873
874 mutex_enter(&usb_event_lock);
875 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
876 mutex_exit(&usb_event_lock);
877
878 return 0;
879 }
880
881 /* Explore device tree from the root. */
882 Static void
883 usb_discover(struct usb_softc *sc)
884 {
885
886 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
887
888 KASSERT(mutex_owned(sc->sc_bus->ub_lock));
889
890 if (usb_noexplore > 1)
891 return;
892 /*
893 * We need mutual exclusion while traversing the device tree,
894 * but this is guaranteed since this function is only called
895 * from the event thread for the controller.
896 *
897 * Also, we now have sc_bus->ub_lock held.
898 */
899 while (sc->sc_bus->ub_needsexplore && !sc->sc_dying) {
900 sc->sc_bus->ub_needsexplore = 0;
901 mutex_exit(sc->sc_bus->ub_lock);
902 sc->sc_bus->ub_roothub->ud_hub->uh_explore(sc->sc_bus->ub_roothub);
903 mutex_enter(sc->sc_bus->ub_lock);
904 }
905 }
906
907 void
908 usb_needs_explore(struct usbd_device *dev)
909 {
910
911 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
912
913 mutex_enter(dev->ud_bus->ub_lock);
914 dev->ud_bus->ub_needsexplore = 1;
915 cv_signal(&dev->ud_bus->ub_needsexplore_cv);
916 mutex_exit(dev->ud_bus->ub_lock);
917 }
918
919 void
920 usb_needs_reattach(struct usbd_device *dev)
921 {
922
923 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
924
925 mutex_enter(dev->ud_bus->ub_lock);
926 dev->ud_powersrc->up_reattach = 1;
927 dev->ud_bus->ub_needsexplore = 1;
928 cv_signal(&dev->ud_bus->ub_needsexplore_cv);
929 mutex_exit(dev->ud_bus->ub_lock);
930 }
931
932 /* Called at with usb_event_lock held. */
933 int
934 usb_get_next_event(struct usb_event *ue)
935 {
936 struct usb_event_q *ueq;
937
938 KASSERT(mutex_owned(&usb_event_lock));
939
940 if (usb_nevents <= 0)
941 return 0;
942 ueq = SIMPLEQ_FIRST(&usb_events);
943 #ifdef DIAGNOSTIC
944 if (ueq == NULL) {
945 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
946 usb_nevents = 0;
947 return 0;
948 }
949 #endif
950 if (ue)
951 *ue = ueq->ue;
952 SIMPLEQ_REMOVE_HEAD(&usb_events, next);
953 usb_free_event((struct usb_event *)(void *)ueq);
954 usb_nevents--;
955 return 1;
956 }
957
958 void
959 usbd_add_dev_event(int type, struct usbd_device *udev)
960 {
961 struct usb_event *ue = usb_alloc_event();
962
963 usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
964 usb_add_event(type, ue);
965 }
966
967 void
968 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
969 {
970 struct usb_event *ue = usb_alloc_event();
971
972 ue->u.ue_driver.ue_cookie = udev->ud_cookie;
973 strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
974 sizeof(ue->u.ue_driver.ue_devname));
975 usb_add_event(type, ue);
976 }
977
978 Static struct usb_event *
979 usb_alloc_event(void)
980 {
981 /* Yes, this is right; we allocate enough so that we can use it later */
982 return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
983 }
984
985 Static void
986 usb_free_event(struct usb_event *uep)
987 {
988 kmem_free(uep, sizeof(struct usb_event_q));
989 }
990
991 Static void
992 usb_add_event(int type, struct usb_event *uep)
993 {
994 struct usb_event_q *ueq;
995 struct timeval thetime;
996
997 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
998
999 microtime(&thetime);
1000 /* Don't want to wait here with usb_event_lock held */
1001 ueq = (struct usb_event_q *)(void *)uep;
1002 ueq->ue = *uep;
1003 ueq->ue.ue_type = type;
1004 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
1005
1006 mutex_enter(&usb_event_lock);
1007 if (++usb_nevents >= USB_MAX_EVENTS) {
1008 /* Too many queued events, drop an old one. */
1009 DPRINTF("event dropped", 0, 0, 0, 0);
1010 (void)usb_get_next_event(0);
1011 }
1012 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
1013 cv_signal(&usb_event_cv);
1014 selnotify(&usb_selevent, 0, 0);
1015 if (usb_async_proc != NULL) {
1016 kpreempt_disable();
1017 softint_schedule(usb_async_sih);
1018 kpreempt_enable();
1019 }
1020 mutex_exit(&usb_event_lock);
1021 }
1022
1023 Static void
1024 usb_async_intr(void *cookie)
1025 {
1026 proc_t *proc;
1027
1028 mutex_enter(proc_lock);
1029 if ((proc = usb_async_proc) != NULL)
1030 psignal(proc, SIGIO);
1031 mutex_exit(proc_lock);
1032 }
1033
1034 Static void
1035 usb_soft_intr(void *arg)
1036 {
1037 struct usbd_bus *bus = arg;
1038
1039 mutex_enter(bus->ub_lock);
1040 bus->ub_methods->ubm_softint(bus);
1041 mutex_exit(bus->ub_lock);
1042 }
1043
1044 void
1045 usb_schedsoftintr(struct usbd_bus *bus)
1046 {
1047
1048 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1049
1050 DPRINTFN(10, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
1051
1052 if (bus->ub_usepolling) {
1053 bus->ub_methods->ubm_softint(bus);
1054 } else {
1055 kpreempt_disable();
1056 softint_schedule(bus->ub_soft);
1057 kpreempt_enable();
1058 }
1059 }
1060
1061 int
1062 usb_activate(device_t self, enum devact act)
1063 {
1064 struct usb_softc *sc = device_private(self);
1065
1066 switch (act) {
1067 case DVACT_DEACTIVATE:
1068 sc->sc_dying = 1;
1069 return 0;
1070 default:
1071 return EOPNOTSUPP;
1072 }
1073 }
1074
1075 void
1076 usb_childdet(device_t self, device_t child)
1077 {
1078 int i;
1079 struct usb_softc *sc = device_private(self);
1080 struct usbd_device *dev;
1081
1082 if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
1083 return;
1084
1085 for (i = 0; i < dev->ud_subdevlen; i++)
1086 if (dev->ud_subdevs[i] == child)
1087 dev->ud_subdevs[i] = NULL;
1088 }
1089
1090 int
1091 usb_detach(device_t self, int flags)
1092 {
1093 struct usb_softc *sc = device_private(self);
1094 struct usb_event *ue;
1095 int rc;
1096
1097 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1098
1099 /* Make all devices disconnect. */
1100 if (sc->sc_port.up_dev != NULL &&
1101 (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
1102 return rc;
1103
1104 pmf_device_deregister(self);
1105 /* Kill off event thread. */
1106 sc->sc_dying = 1;
1107 while (sc->sc_event_thread != NULL) {
1108 mutex_enter(sc->sc_bus->ub_lock);
1109 cv_signal(&sc->sc_bus->ub_needsexplore_cv);
1110 cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
1111 sc->sc_bus->ub_lock, hz * 60);
1112 mutex_exit(sc->sc_bus->ub_lock);
1113 }
1114 DPRINTF("event thread dead", 0, 0, 0, 0);
1115
1116 if (sc->sc_bus->ub_soft != NULL) {
1117 softint_disestablish(sc->sc_bus->ub_soft);
1118 sc->sc_bus->ub_soft = NULL;
1119 }
1120
1121 ue = usb_alloc_event();
1122 ue->u.ue_ctrlr.ue_bus = device_unit(self);
1123 usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1124
1125 cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
1126
1127 return 0;
1128 }
1129