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