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