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