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