usb.c revision 1.110 1 /* $NetBSD: usb.c,v 1.110 2008/03/30 15:37:49 ad 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.110 2008/03/30 15:37:49 ad 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;
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
205 static void
206 usb_doattach(device_t self)
207 {
208 static bool usb_selevent_init; /* XXX */
209 struct usb_softc *sc = device_private(self);
210 usbd_device_handle dev;
211 usbd_status err;
212 int speed;
213 struct usb_event *ue;
214
215 if (!usb_selevent_init) {
216 selinit(&usb_selevent);
217 usb_selevent_init = true;
218 }
219 DPRINTF(("usbd_doattach\n"));
220
221 sc->sc_bus->usbctl = self;
222 sc->sc_port.power = USB_MAX_POWER;
223
224 switch (sc->sc_bus->usbrev) {
225 case USBREV_1_0:
226 case USBREV_1_1:
227 speed = USB_SPEED_FULL;
228 break;
229 case USBREV_2_0:
230 speed = USB_SPEED_HIGH;
231 break;
232 default:
233 panic("usb_doattach");
234 }
235
236 ue = usb_alloc_event();
237 ue->u.ue_ctrlr.ue_bus = device_unit(self);
238 usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
239
240 #ifdef USB_USE_SOFTINTR
241 /* XXX we should have our own level */
242 sc->sc_bus->soft = softint_establish(SOFTINT_NET,
243 sc->sc_bus->methods->soft_intr, sc->sc_bus);
244 if (sc->sc_bus->soft == NULL) {
245 aprint_error("%s: can't register softintr\n",
246 device_xname(self));
247 sc->sc_dying = 1;
248 USB_ATTACH_ERROR_RETURN;
249 }
250 #endif
251
252 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
253 &sc->sc_port);
254 if (!err) {
255 dev = sc->sc_port.device;
256 if (dev->hub == NULL) {
257 sc->sc_dying = 1;
258 aprint_error("%s: root device is not a hub\n",
259 device_xname(self));
260 USB_ATTACH_ERROR_RETURN;
261 }
262 sc->sc_bus->root_hub = dev;
263 #if 1
264 /*
265 * Turning this code off will delay attachment of USB devices
266 * until the USB event thread is running, which means that
267 * the keyboard will not work until after cold boot.
268 */
269 if (cold && (device_cfdata(self)->cf_flags & 1))
270 dev->hub->explore(sc->sc_bus->root_hub);
271 #endif
272 } else {
273 aprint_error("%s: root hub problem, error=%d\n",
274 device_xname(self), err);
275 sc->sc_dying = 1;
276 }
277
278 config_pending_incr();
279 usb_create_event_thread(self);
280
281 if (!pmf_device_register(self, NULL, NULL))
282 aprint_error_dev(self, "couldn't establish power handler\n");
283
284 USB_ATTACH_SUCCESS_RETURN;
285 }
286
287 static const char *taskq_names[] = USB_TASKQ_NAMES;
288
289 #if defined(__NetBSD__) || defined(__OpenBSD__)
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 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
442
443 int
444 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
445 {
446 int unit = minor(dev);
447 struct usb_softc *sc;
448
449 if (unit == USB_DEV_MINOR) {
450 if (usb_dev_open)
451 return (EBUSY);
452 usb_dev_open = 1;
453 usb_async_proc = 0;
454 return (0);
455 }
456
457 sc = device_lookup_private(&usb_cd, unit);
458
459 if (sc->sc_dying)
460 return (EIO);
461
462 return (0);
463 }
464
465 int
466 usbread(dev_t dev, struct uio *uio, int flag)
467 {
468 struct usb_event *ue;
469 #ifdef COMPAT_30
470 struct usb_event_old *ueo = NULL; /* XXXGCC */
471 #endif
472 int s, error, n, useold;
473
474 if (minor(dev) != USB_DEV_MINOR)
475 return (ENXIO);
476
477 useold = 0;
478 switch (uio->uio_resid) {
479 #ifdef COMPAT_30
480 case sizeof(struct usb_event_old):
481 ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
482 M_WAITOK|M_ZERO);
483 useold = 1;
484 /* FALLTHRU */
485 #endif
486 case sizeof(struct usb_event):
487 ue = usb_alloc_event();
488 break;
489 default:
490 return (EINVAL);
491 }
492
493 error = 0;
494 s = splusb();
495 for (;;) {
496 n = usb_get_next_event(ue);
497 if (n != 0)
498 break;
499 if (flag & IO_NDELAY) {
500 error = EWOULDBLOCK;
501 break;
502 }
503 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
504 if (error)
505 break;
506 }
507 splx(s);
508 if (!error) {
509 #ifdef COMPAT_30
510 if (useold) { /* copy fields to old struct */
511 ueo->ue_type = ue->ue_type;
512 memcpy(&ueo->ue_time, &ue->ue_time,
513 sizeof(struct timespec));
514 switch (ue->ue_type) {
515 case USB_EVENT_DEVICE_ATTACH:
516 case USB_EVENT_DEVICE_DETACH:
517 usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
518 break;
519
520 case USB_EVENT_CTRLR_ATTACH:
521 case USB_EVENT_CTRLR_DETACH:
522 ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
523 break;
524
525 case USB_EVENT_DRIVER_ATTACH:
526 case USB_EVENT_DRIVER_DETACH:
527 ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
528 memcpy(ueo->u.ue_driver.ue_devname,
529 ue->u.ue_driver.ue_devname,
530 sizeof(ue->u.ue_driver.ue_devname));
531 break;
532 default:
533 ;
534 }
535
536 error = uiomove((void *)ueo, sizeof *ueo, uio);
537 } else
538 #endif
539 error = uiomove((void *)ue, sizeof *ue, uio);
540 }
541 usb_free_event(ue);
542 #ifdef COMPAT_30
543 if (useold)
544 free(ueo, M_USBDEV);
545 #endif
546
547 return (error);
548 }
549
550 int
551 usbclose(dev_t dev, int flag, int mode,
552 struct lwp *l)
553 {
554 int unit = minor(dev);
555
556 if (unit == USB_DEV_MINOR) {
557 usb_async_proc = 0;
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 if (*(int *)data)
578 usb_async_proc = l->l_proc;
579 else
580 usb_async_proc = 0;
581 return (0);
582
583 default:
584 return (EINVAL);
585 }
586 }
587
588 sc = device_lookup_private(&usb_cd, unit);
589
590 if (sc->sc_dying)
591 return (EIO);
592
593 switch (cmd) {
594 #ifdef USB_DEBUG
595 case USB_SETDEBUG:
596 if (!(flag & FWRITE))
597 return (EBADF);
598 usbdebug = ((*(int *)data) & 0x000000ff);
599 #if defined(UHCI_DEBUG) && NUHCI > 0
600 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
601 #endif
602 #if defined(OHCI_DEBUG) && NOHCI > 0
603 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
604 #endif
605 break;
606 #endif /* USB_DEBUG */
607 case USB_REQUEST:
608 {
609 struct usb_ctl_request *ur = (void *)data;
610 int len = UGETW(ur->ucr_request.wLength);
611 struct iovec iov;
612 struct uio uio;
613 void *ptr = 0;
614 int addr = ur->ucr_addr;
615 usbd_status err;
616 int error = 0;
617
618 if (!(flag & FWRITE))
619 return (EBADF);
620
621 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
622 if (len < 0 || len > 32768)
623 return (EINVAL);
624 if (addr < 0 || addr >= USB_MAX_DEVICES ||
625 sc->sc_bus->devices[addr] == 0)
626 return (EINVAL);
627 if (len != 0) {
628 iov.iov_base = (void *)ur->ucr_data;
629 iov.iov_len = len;
630 uio.uio_iov = &iov;
631 uio.uio_iovcnt = 1;
632 uio.uio_resid = len;
633 uio.uio_offset = 0;
634 uio.uio_rw =
635 ur->ucr_request.bmRequestType & UT_READ ?
636 UIO_READ : UIO_WRITE;
637 uio.uio_vmspace = l->l_proc->p_vmspace;
638 ptr = malloc(len, M_TEMP, M_WAITOK);
639 if (uio.uio_rw == UIO_WRITE) {
640 error = uiomove(ptr, len, &uio);
641 if (error)
642 goto ret;
643 }
644 }
645 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
646 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
647 USBD_DEFAULT_TIMEOUT);
648 if (err) {
649 error = EIO;
650 goto ret;
651 }
652 if (len > ur->ucr_actlen)
653 len = ur->ucr_actlen;
654 if (len != 0) {
655 if (uio.uio_rw == UIO_READ) {
656 error = uiomove(ptr, len, &uio);
657 if (error)
658 goto ret;
659 }
660 }
661 ret:
662 if (ptr)
663 free(ptr, M_TEMP);
664 return (error);
665 }
666
667 case USB_DEVICEINFO:
668 {
669 usbd_device_handle dev;
670 struct usb_device_info *di = (void *)data;
671 int addr = di->udi_addr;
672
673 if (addr < 1 || addr >= USB_MAX_DEVICES)
674 return EINVAL;
675 if ((dev = sc->sc_bus->devices[addr]) == NULL)
676 return ENXIO;
677 usbd_fill_deviceinfo(dev, di, 1);
678 break;
679 }
680
681 #ifdef COMPAT_30
682 case USB_DEVICEINFO_OLD:
683 {
684 usbd_device_handle dev;
685 struct usb_device_info_old *di = (void *)data;
686 int addr = di->udi_addr;
687
688 if (addr < 1 || addr >= USB_MAX_DEVICES)
689 return EINVAL;
690 if ((dev = sc->sc_bus->devices[addr]) == NULL)
691 return ENXIO;
692 usbd_fill_deviceinfo_old(dev, di, 1);
693 break;
694 }
695 #endif
696
697 case USB_DEVICESTATS:
698 *(struct usb_device_stats *)data = sc->sc_bus->stats;
699 break;
700
701 default:
702 return (EINVAL);
703 }
704 return (0);
705 }
706
707 int
708 usbpoll(dev_t dev, int events, struct lwp *l)
709 {
710 int revents, mask, s;
711
712 if (minor(dev) == USB_DEV_MINOR) {
713 revents = 0;
714 mask = POLLIN | POLLRDNORM;
715
716 s = splusb();
717 if (events & mask && usb_nevents > 0)
718 revents |= events & mask;
719 if (revents == 0 && events & mask)
720 selrecord(l, &usb_selevent);
721 splx(s);
722
723 return (revents);
724 } else {
725 return (0);
726 }
727 }
728
729 static void
730 filt_usbrdetach(struct knote *kn)
731 {
732 int s;
733
734 s = splusb();
735 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
736 splx(s);
737 }
738
739 static int
740 filt_usbread(struct knote *kn, long hint)
741 {
742
743 if (usb_nevents == 0)
744 return (0);
745
746 kn->kn_data = sizeof(struct usb_event);
747 return (1);
748 }
749
750 static const struct filterops usbread_filtops =
751 { 1, NULL, filt_usbrdetach, filt_usbread };
752
753 int
754 usbkqfilter(dev_t dev, struct knote *kn)
755 {
756 struct klist *klist;
757 int s;
758
759 switch (kn->kn_filter) {
760 case EVFILT_READ:
761 if (minor(dev) != USB_DEV_MINOR)
762 return (1);
763 klist = &usb_selevent.sel_klist;
764 kn->kn_fop = &usbread_filtops;
765 break;
766
767 default:
768 return (EINVAL);
769 }
770
771 kn->kn_hook = NULL;
772
773 s = splusb();
774 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
775 splx(s);
776
777 return (0);
778 }
779
780 /* Explore device tree from the root. */
781 Static void
782 usb_discover(struct usb_softc *sc)
783 {
784
785 DPRINTFN(2,("usb_discover\n"));
786 #ifdef USB_DEBUG
787 if (usb_noexplore > 1)
788 return;
789 #endif
790 /*
791 * We need mutual exclusion while traversing the device tree,
792 * but this is guaranteed since this function is only called
793 * from the event thread for the controller.
794 */
795 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
796 sc->sc_bus->needs_explore = 0;
797 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
798 }
799 }
800
801 void
802 usb_needs_explore(usbd_device_handle dev)
803 {
804 DPRINTFN(2,("usb_needs_explore\n"));
805 dev->bus->needs_explore = 1;
806 wakeup(&dev->bus->needs_explore);
807 }
808
809 void
810 usb_needs_reattach(usbd_device_handle dev)
811 {
812 DPRINTFN(2,("usb_needs_reattach\n"));
813 dev->powersrc->reattach = 1;
814 dev->bus->needs_explore = 1;
815 wakeup(&dev->bus->needs_explore);
816 }
817
818 /* Called at splusb() */
819 int
820 usb_get_next_event(struct usb_event *ue)
821 {
822 struct usb_event_q *ueq;
823
824 if (usb_nevents <= 0)
825 return (0);
826 ueq = SIMPLEQ_FIRST(&usb_events);
827 #ifdef DIAGNOSTIC
828 if (ueq == NULL) {
829 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
830 usb_nevents = 0;
831 return (0);
832 }
833 #endif
834 if (ue)
835 *ue = ueq->ue;
836 SIMPLEQ_REMOVE_HEAD(&usb_events, next);
837 usb_free_event((struct usb_event *)(void *)ueq);
838 usb_nevents--;
839 return (1);
840 }
841
842 void
843 usbd_add_dev_event(int type, usbd_device_handle udev)
844 {
845 struct usb_event *ue = usb_alloc_event();
846
847 usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
848 usb_add_event(type, ue);
849 }
850
851 void
852 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
853 {
854 struct usb_event *ue = usb_alloc_event();
855
856 ue->u.ue_driver.ue_cookie = udev->cookie;
857 strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
858 sizeof ue->u.ue_driver.ue_devname);
859 usb_add_event(type, ue);
860 }
861
862 Static struct usb_event *
863 usb_alloc_event(void)
864 {
865 /* Yes, this is right; we allocate enough so that we can use it later */
866 return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
867 }
868
869 Static void
870 usb_free_event(struct usb_event *uep)
871 {
872 free(uep, M_USBDEV);
873 }
874
875 Static void
876 usb_add_event(int type, struct usb_event *uep)
877 {
878 struct usb_event_q *ueq;
879 struct timeval thetime;
880 int s;
881
882 microtime(&thetime);
883 /* Don't want to wait here inside splusb() */
884 ueq = (struct usb_event_q *)(void *)uep;
885 ueq->ue = *uep;
886 ueq->ue.ue_type = type;
887 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
888
889 s = splusb();
890 if (++usb_nevents >= USB_MAX_EVENTS) {
891 /* Too many queued events, drop an old one. */
892 DPRINTFN(-1,("usb: event dropped\n"));
893 (void)usb_get_next_event(0);
894 }
895 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
896 wakeup(&usb_events);
897 selnotify(&usb_selevent, 0, 0);
898 if (usb_async_proc != NULL) {
899 mutex_enter(&proclist_mutex);
900 psignal(usb_async_proc, SIGIO);
901 mutex_exit(&proclist_mutex);
902 }
903 splx(s);
904 }
905
906 void
907 usb_schedsoftintr(usbd_bus_handle bus)
908 {
909 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
910 #ifdef USB_USE_SOFTINTR
911 if (bus->use_polling) {
912 bus->methods->soft_intr(bus);
913 } else {
914 softint_schedule(bus->soft);
915 }
916 #else
917 bus->methods->soft_intr(bus);
918 #endif /* USB_USE_SOFTINTR */
919 }
920
921 int
922 usb_activate(device_t self, enum devact act)
923 {
924 struct usb_softc *sc = device_private(self);
925 usbd_device_handle dev = sc->sc_port.device;
926 int i, rv = 0;
927
928 switch (act) {
929 case DVACT_ACTIVATE:
930 return (EOPNOTSUPP);
931
932 case DVACT_DEACTIVATE:
933 sc->sc_dying = 1;
934 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
935 for (i = 0; dev->subdevs[i]; i++)
936 rv |= config_deactivate(dev->subdevs[i]);
937 }
938 break;
939 }
940 return (rv);
941 }
942
943 void
944 usb_childdet(device_t self, device_t child)
945 {
946 int i, last = -1;
947 struct usb_softc *sc = device_private(self);
948 struct usbd_device *dev;
949
950 if ((dev = sc->sc_port.device) == NULL || dev->subdevs == NULL)
951 return;
952
953 for (i = 0; dev->subdevs[i] != NULL; i++)
954 last = i;
955
956 for (i = 0; dev->subdevs[i] != NULL; i++) {
957 if (dev->subdevs[i] == child) {
958 dev->subdevs[i] = dev->subdevs[last];
959 dev->subdevs[last] = NULL;
960 }
961 }
962 }
963
964 int
965 usb_detach(device_t self, int flags)
966 {
967 struct usb_softc *sc = device_private(self);
968 struct usb_event *ue;
969
970 DPRINTF(("usb_detach: start\n"));
971
972 pmf_device_deregister(self);
973 /* Kill off event thread. */
974 while (sc->sc_event_thread != NULL) {
975 wakeup(&sc->sc_bus->needs_explore);
976 tsleep(sc, PWAIT, "usbdet", hz * 60);
977 }
978 DPRINTF(("usb_detach: event thread dead\n"));
979
980 /* Make all devices disconnect. */
981 if (sc->sc_port.device != NULL)
982 usb_disconnect_port(&sc->sc_port, self);
983
984 #ifdef USB_USE_SOFTINTR
985 if (sc->sc_bus->soft != NULL) {
986 softint_disestablish(sc->sc_bus->soft);
987 sc->sc_bus->soft = NULL;
988 }
989 #endif
990
991 ue = usb_alloc_event();
992 ue->u.ue_ctrlr.ue_bus = device_unit(self);
993 usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
994
995 return (0);
996 }
997
998 #ifdef COMPAT_30
999 Static void
1000 usb_copy_old_devinfo(struct usb_device_info_old *uo,
1001 const struct usb_device_info *ue)
1002 {
1003 const unsigned char *p;
1004 unsigned char *q;
1005 int i, n;
1006
1007 uo->udi_bus = ue->udi_bus;
1008 uo->udi_addr = ue->udi_addr;
1009 uo->udi_cookie = ue->udi_cookie;
1010 for (i = 0, p = (const unsigned char *)ue->udi_product,
1011 q = (unsigned char *)uo->udi_product;
1012 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1013 if (*p < 0x80)
1014 q[i++] = *p;
1015 else {
1016 q[i++] = '?';
1017 if ((*p & 0xe0) == 0xe0)
1018 p++;
1019 p++;
1020 }
1021 }
1022 q[i] = 0;
1023
1024 for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
1025 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1026 if (* p < 0x80)
1027 q[i++] = *p;
1028 else {
1029 q[i++] = '?';
1030 p++;
1031 if ((*p & 0xe0) == 0xe0)
1032 p++;
1033 }
1034 }
1035 q[i] = 0;
1036
1037 memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
1038
1039 uo->udi_productNo = ue->udi_productNo;
1040 uo->udi_vendorNo = ue->udi_vendorNo;
1041 uo->udi_releaseNo = ue->udi_releaseNo;
1042 uo->udi_class = ue->udi_class;
1043 uo->udi_subclass = ue->udi_subclass;
1044 uo->udi_protocol = ue->udi_protocol;
1045 uo->udi_config = ue->udi_config;
1046 uo->udi_speed = ue->udi_speed;
1047 uo->udi_power = ue->udi_power;
1048 uo->udi_nports = ue->udi_nports;
1049
1050 for (n=0; n<USB_MAX_DEVNAMES; n++)
1051 memcpy(uo->udi_devnames[n],
1052 ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
1053 memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
1054 }
1055 #endif
1056