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