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