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