usb.c revision 1.86 1 /* $NetBSD: usb.c,v 1.86 2006/03/29 04:16:50 thorpej 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.86 2006/03/29 04:16:50 thorpej Exp $");
48
49 #include "ohci.h"
50 #include "uhci.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/device.h>
57 #include <sys/kthread.h>
58 #include <sys/proc.h>
59 #include <sys/conf.h>
60 #include <sys/fcntl.h>
61 #include <sys/poll.h>
62 #include <sys/select.h>
63 #include <sys/vnode.h>
64 #include <sys/signalvar.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69
70 #define USB_DEV_MINOR 255
71
72 #include <machine/bus.h>
73
74 #include <dev/usb/usbdivar.h>
75 #include <dev/usb/usb_quirks.h>
76
77 #ifdef USB_DEBUG
78 #define DPRINTF(x) if (usbdebug) logprintf x
79 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
80 int usbdebug = 0;
81 #if defined(UHCI_DEBUG) && NUHCI > 0
82 extern int uhcidebug;
83 #endif
84 #if defined(OHCI_DEBUG) && NOHCI > 0
85 extern int ohcidebug;
86 #endif
87 /*
88 * 0 - do usual exploration
89 * 1 - do not use timeout exploration
90 * >1 - do no exploration
91 */
92 int usb_noexplore = 0;
93 #else
94 #define DPRINTF(x)
95 #define DPRINTFN(n,x)
96 #endif
97
98 struct usb_softc {
99 USBBASEDEVICE sc_dev; /* base device */
100 usbd_bus_handle sc_bus; /* USB controller */
101 struct usbd_port sc_port; /* dummy port for root hub */
102
103 struct proc *sc_event_thread;
104
105 char sc_dying;
106 };
107
108 TAILQ_HEAD(, usb_task) usb_all_tasks;
109
110 dev_type_open(usbopen);
111 dev_type_close(usbclose);
112 dev_type_read(usbread);
113 dev_type_ioctl(usbioctl);
114 dev_type_poll(usbpoll);
115 dev_type_kqfilter(usbkqfilter);
116
117 const struct cdevsw usb_cdevsw = {
118 usbopen, usbclose, usbread, nowrite, usbioctl,
119 nostop, notty, usbpoll, nommap, usbkqfilter,
120 };
121
122 Static void usb_discover(void *);
123 Static void usb_create_event_thread(void *);
124 Static void usb_event_thread(void *);
125 Static void usb_task_thread(void *);
126 Static struct proc *usb_task_thread_proc = NULL;
127
128 #define USB_MAX_EVENTS 100
129 struct usb_event_q {
130 struct usb_event ue;
131 SIMPLEQ_ENTRY(usb_event_q) next;
132 };
133 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
134 SIMPLEQ_HEAD_INITIALIZER(usb_events);
135 Static int usb_nevents = 0;
136 Static struct selinfo usb_selevent;
137 Static usb_proc_ptr usb_async_proc; /* process that wants USB SIGIO */
138 Static int usb_dev_open = 0;
139 Static void usb_add_event(int, struct usb_event *);
140
141 Static int usb_get_next_event(struct usb_event *);
142
143 Static const char *usbrev_str[] = USBREV_STR;
144
145 USB_DECLARE_DRIVER(usb);
146
147 USB_MATCH(usb)
148 {
149 DPRINTF(("usbd_match\n"));
150 return (UMATCH_GENERIC);
151 }
152
153 USB_ATTACH(usb)
154 {
155 struct usb_softc *sc = (struct usb_softc *)self;
156 usbd_device_handle dev;
157 usbd_status err;
158 int usbrev;
159 int speed;
160 struct usb_event ue;
161
162 DPRINTF(("usbd_attach\n"));
163
164 usbd_init();
165 sc->sc_bus = aux;
166 sc->sc_bus->usbctl = sc;
167 sc->sc_port.power = USB_MAX_POWER;
168
169 usbrev = sc->sc_bus->usbrev;
170 printf(": USB revision %s", usbrev_str[usbrev]);
171 switch (usbrev) {
172 case USBREV_1_0:
173 case USBREV_1_1:
174 speed = USB_SPEED_FULL;
175 break;
176 case USBREV_2_0:
177 speed = USB_SPEED_HIGH;
178 break;
179 default:
180 printf(", not supported\n");
181 sc->sc_dying = 1;
182 USB_ATTACH_ERROR_RETURN;
183 }
184 printf("\n");
185
186 /* Make sure not to use tsleep() if we are cold booting. */
187 if (cold)
188 sc->sc_bus->use_polling++;
189
190 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
191 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
192
193 #ifdef USB_USE_SOFTINTR
194 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
195 /* XXX we should have our own level */
196 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
197 sc->sc_bus->methods->soft_intr, sc->sc_bus);
198 if (sc->sc_bus->soft == NULL) {
199 printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
200 sc->sc_dying = 1;
201 USB_ATTACH_ERROR_RETURN;
202 }
203 #else
204 usb_callout_init(sc->sc_bus->softi);
205 #endif
206 #endif
207
208 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
209 &sc->sc_port);
210 if (!err) {
211 dev = sc->sc_port.device;
212 if (dev->hub == NULL) {
213 sc->sc_dying = 1;
214 printf("%s: root device is not a hub\n",
215 USBDEVNAME(sc->sc_dev));
216 USB_ATTACH_ERROR_RETURN;
217 }
218 sc->sc_bus->root_hub = dev;
219 #if 1
220 /*
221 * Turning this code off will delay attachment of USB devices
222 * until the USB event thread is running, which means that
223 * the keyboard will not work until after cold boot.
224 */
225 if (cold && (device_cfdata(&sc->sc_dev)->cf_flags & 1))
226 dev->hub->explore(sc->sc_bus->root_hub);
227 #endif
228 } else {
229 printf("%s: root hub problem, error=%d\n",
230 USBDEVNAME(sc->sc_dev), err);
231 sc->sc_dying = 1;
232 }
233 if (cold)
234 sc->sc_bus->use_polling--;
235
236 config_pending_incr();
237 usb_kthread_create(usb_create_event_thread, sc);
238
239 USB_ATTACH_SUCCESS_RETURN;
240 }
241
242 #if defined(__NetBSD__) || defined(__OpenBSD__)
243 void
244 usb_create_event_thread(void *arg)
245 {
246 struct usb_softc *sc = arg;
247 static int created = 0;
248
249 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
250 "%s", sc->sc_dev.dv_xname)) {
251 printf("%s: unable to create event thread for\n",
252 sc->sc_dev.dv_xname);
253 panic("usb_create_event_thread");
254 }
255 if (!created) {
256 created = 1;
257 TAILQ_INIT(&usb_all_tasks);
258 if (usb_kthread_create1(usb_task_thread, NULL,
259 &usb_task_thread_proc, "usbtask")) {
260 printf("unable to create task thread\n");
261 panic("usb_create_event_thread task");
262 }
263 }
264 }
265
266 /*
267 * Add a task to be performed by the task thread. This function can be
268 * called from any context and the task will be executed in a process
269 * context ASAP.
270 */
271 void
272 usb_add_task(usbd_device_handle dev, struct usb_task *task)
273 {
274 int s;
275
276 s = splusb();
277 if (!task->onqueue) {
278 DPRINTFN(2,("usb_add_task: task=%p\n", task));
279 TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
280 task->onqueue = 1;
281 } else {
282 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
283 }
284 wakeup(&usb_all_tasks);
285 splx(s);
286 }
287
288 void
289 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
290 {
291 int s;
292
293 s = splusb();
294 if (task->onqueue) {
295 TAILQ_REMOVE(&usb_all_tasks, task, next);
296 task->onqueue = 0;
297 }
298 splx(s);
299 }
300
301 void
302 usb_event_thread(void *arg)
303 {
304 struct usb_softc *sc = arg;
305
306 DPRINTF(("usb_event_thread: start\n"));
307
308 /*
309 * In case this controller is a companion controller to an
310 * EHCI controller we need to wait until the EHCI controller
311 * has grabbed the port.
312 * XXX It would be nicer to do this with a tsleep(), but I don't
313 * know how to synchronize the creation of the threads so it
314 * will work.
315 */
316 usb_delay_ms(sc->sc_bus, 500);
317
318 /* Make sure first discover does something. */
319 sc->sc_bus->needs_explore = 1;
320 usb_discover(sc);
321 config_pending_decr();
322
323 while (!sc->sc_dying) {
324 #ifdef USB_DEBUG
325 if (usb_noexplore < 2)
326 #endif
327 usb_discover(sc);
328 #ifdef USB_DEBUG
329 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
330 usb_noexplore ? 0 : hz * 60);
331 #else
332 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
333 hz * 60);
334 #endif
335 DPRINTFN(2,("usb_event_thread: woke up\n"));
336 }
337 sc->sc_event_thread = NULL;
338
339 /* In case parent is waiting for us to exit. */
340 wakeup(sc);
341
342 DPRINTF(("usb_event_thread: exit\n"));
343 kthread_exit(0);
344 }
345
346 void
347 usb_task_thread(void *arg)
348 {
349 struct usb_task *task;
350 int s;
351
352 DPRINTF(("usb_task_thread: start\n"));
353
354 s = splusb();
355 for (;;) {
356 task = TAILQ_FIRST(&usb_all_tasks);
357 if (task == NULL) {
358 tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0);
359 task = TAILQ_FIRST(&usb_all_tasks);
360 }
361 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
362 if (task != NULL) {
363 TAILQ_REMOVE(&usb_all_tasks, task, next);
364 task->onqueue = 0;
365 splx(s);
366 task->fun(task->arg);
367 s = splusb();
368 }
369 }
370 }
371
372 int
373 usbctlprint(void *aux, const char *pnp)
374 {
375 /* only "usb"es can attach to host controllers */
376 if (pnp)
377 aprint_normal("usb at %s", pnp);
378
379 return (UNCONF);
380 }
381 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
382
383 int
384 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
385 {
386 int unit = minor(dev);
387 struct usb_softc *sc;
388
389 if (unit == USB_DEV_MINOR) {
390 if (usb_dev_open)
391 return (EBUSY);
392 usb_dev_open = 1;
393 usb_async_proc = 0;
394 return (0);
395 }
396
397 USB_GET_SC_OPEN(usb, unit, sc);
398
399 if (sc->sc_dying)
400 return (EIO);
401
402 return (0);
403 }
404
405 int
406 usbread(dev_t dev, struct uio *uio, int flag)
407 {
408 struct usb_event ue;
409 int s, error, n;
410
411 if (minor(dev) != USB_DEV_MINOR)
412 return (ENXIO);
413
414 if (uio->uio_resid != sizeof(struct usb_event))
415 return (EINVAL);
416
417 error = 0;
418 s = splusb();
419 for (;;) {
420 n = usb_get_next_event(&ue);
421 if (n != 0)
422 break;
423 if (flag & IO_NDELAY) {
424 error = EWOULDBLOCK;
425 break;
426 }
427 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
428 if (error)
429 break;
430 }
431 splx(s);
432 if (!error)
433 error = uiomove((void *)&ue, uio->uio_resid, uio);
434
435 return (error);
436 }
437
438 int
439 usbclose(dev_t dev, int flag, int mode, struct lwp *l)
440 {
441 int unit = minor(dev);
442
443 if (unit == USB_DEV_MINOR) {
444 usb_async_proc = 0;
445 usb_dev_open = 0;
446 }
447
448 return (0);
449 }
450
451 int
452 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct lwp *l)
453 {
454 struct usb_softc *sc;
455 int unit = minor(devt);
456
457 if (unit == USB_DEV_MINOR) {
458 switch (cmd) {
459 case FIONBIO:
460 /* All handled in the upper FS layer. */
461 return (0);
462
463 case FIOASYNC:
464 if (*(int *)data)
465 usb_async_proc = l->l_proc;
466 else
467 usb_async_proc = 0;
468 return (0);
469
470 default:
471 return (EINVAL);
472 }
473 }
474
475 USB_GET_SC(usb, unit, sc);
476
477 if (sc->sc_dying)
478 return (EIO);
479
480 switch (cmd) {
481 #ifdef USB_DEBUG
482 case USB_SETDEBUG:
483 if (!(flag & FWRITE))
484 return (EBADF);
485 usbdebug = ((*(int *)data) & 0x000000ff);
486 #if defined(UHCI_DEBUG) && NUHCI > 0
487 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
488 #endif
489 #if defined(OHCI_DEBUG) && NOHCI > 0
490 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
491 #endif
492 break;
493 #endif /* USB_DEBUG */
494 case USB_REQUEST:
495 {
496 struct usb_ctl_request *ur = (void *)data;
497 int len = UGETW(ur->ucr_request.wLength);
498 struct iovec iov;
499 struct uio uio;
500 void *ptr = 0;
501 int addr = ur->ucr_addr;
502 usbd_status err;
503 int error = 0;
504
505 if (!(flag & FWRITE))
506 return (EBADF);
507
508 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
509 if (len < 0 || len > 32768)
510 return (EINVAL);
511 if (addr < 0 || addr >= USB_MAX_DEVICES ||
512 sc->sc_bus->devices[addr] == 0)
513 return (EINVAL);
514 if (len != 0) {
515 iov.iov_base = (caddr_t)ur->ucr_data;
516 iov.iov_len = len;
517 uio.uio_iov = &iov;
518 uio.uio_iovcnt = 1;
519 uio.uio_resid = len;
520 uio.uio_offset = 0;
521 uio.uio_rw =
522 ur->ucr_request.bmRequestType & UT_READ ?
523 UIO_READ : UIO_WRITE;
524 uio.uio_vmspace = l->l_proc->p_vmspace;
525 ptr = malloc(len, M_TEMP, M_WAITOK);
526 if (uio.uio_rw == UIO_WRITE) {
527 error = uiomove(ptr, len, &uio);
528 if (error)
529 goto ret;
530 }
531 }
532 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
533 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
534 USBD_DEFAULT_TIMEOUT);
535 if (err) {
536 error = EIO;
537 goto ret;
538 }
539 if (len != 0) {
540 if (uio.uio_rw == UIO_READ) {
541 error = uiomove(ptr, len, &uio);
542 if (error)
543 goto ret;
544 }
545 }
546 ret:
547 if (ptr)
548 free(ptr, M_TEMP);
549 return (error);
550 }
551
552 case USB_DEVICEINFO:
553 {
554 struct usb_device_info *di = (void *)data;
555 int addr = di->udi_addr;
556 usbd_device_handle dev;
557
558 if (addr < 1 || addr >= USB_MAX_DEVICES)
559 return (EINVAL);
560 dev = sc->sc_bus->devices[addr];
561 if (dev == NULL)
562 return (ENXIO);
563 usbd_fill_deviceinfo(dev, di, 1);
564 break;
565 }
566
567 case USB_DEVICESTATS:
568 *(struct usb_device_stats *)data = sc->sc_bus->stats;
569 break;
570
571 default:
572 return (EINVAL);
573 }
574 return (0);
575 }
576
577 int
578 usbpoll(dev_t dev, int events, struct lwp *l)
579 {
580 int revents, mask, s;
581
582 if (minor(dev) == USB_DEV_MINOR) {
583 revents = 0;
584 mask = POLLIN | POLLRDNORM;
585
586 s = splusb();
587 if (events & mask && usb_nevents > 0)
588 revents |= events & mask;
589 if (revents == 0 && events & mask)
590 selrecord(l, &usb_selevent);
591 splx(s);
592
593 return (revents);
594 } else {
595 return (0);
596 }
597 }
598
599 static void
600 filt_usbrdetach(struct knote *kn)
601 {
602 int s;
603
604 s = splusb();
605 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
606 splx(s);
607 }
608
609 static int
610 filt_usbread(struct knote *kn, long hint)
611 {
612
613 if (usb_nevents == 0)
614 return (0);
615
616 kn->kn_data = sizeof(struct usb_event);
617 return (1);
618 }
619
620 static const struct filterops usbread_filtops =
621 { 1, NULL, filt_usbrdetach, filt_usbread };
622
623 int
624 usbkqfilter(dev_t dev, struct knote *kn)
625 {
626 struct klist *klist;
627 int s;
628
629 switch (kn->kn_filter) {
630 case EVFILT_READ:
631 if (minor(dev) != USB_DEV_MINOR)
632 return (1);
633 klist = &usb_selevent.sel_klist;
634 kn->kn_fop = &usbread_filtops;
635 break;
636
637 default:
638 return (1);
639 }
640
641 kn->kn_hook = NULL;
642
643 s = splusb();
644 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
645 splx(s);
646
647 return (0);
648 }
649
650 /* Explore device tree from the root. */
651 Static void
652 usb_discover(void *v)
653 {
654 struct usb_softc *sc = v;
655
656 DPRINTFN(2,("usb_discover\n"));
657 #ifdef USB_DEBUG
658 if (usb_noexplore > 1)
659 return;
660 #endif
661 /*
662 * We need mutual exclusion while traversing the device tree,
663 * but this is guaranteed since this function is only called
664 * from the event thread for the controller.
665 */
666 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
667 sc->sc_bus->needs_explore = 0;
668 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
669 }
670 }
671
672 void
673 usb_needs_explore(usbd_device_handle dev)
674 {
675 DPRINTFN(2,("usb_needs_explore\n"));
676 dev->bus->needs_explore = 1;
677 wakeup(&dev->bus->needs_explore);
678 }
679
680 void
681 usb_needs_reattach(usbd_device_handle dev)
682 {
683 DPRINTFN(2,("usb_needs_reattach\n"));
684 dev->powersrc->reattach = 1;
685 dev->bus->needs_explore = 1;
686 wakeup(&dev->bus->needs_explore);
687 }
688
689 /* Called at splusb() */
690 int
691 usb_get_next_event(struct usb_event *ue)
692 {
693 struct usb_event_q *ueq;
694
695 if (usb_nevents <= 0)
696 return (0);
697 ueq = SIMPLEQ_FIRST(&usb_events);
698 #ifdef DIAGNOSTIC
699 if (ueq == NULL) {
700 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
701 usb_nevents = 0;
702 return (0);
703 }
704 #endif
705 if (ue)
706 *ue = ueq->ue;
707 SIMPLEQ_REMOVE_HEAD(&usb_events, next);
708 free(ueq, M_USBDEV);
709 usb_nevents--;
710 return (1);
711 }
712
713 void
714 usbd_add_dev_event(int type, usbd_device_handle udev)
715 {
716 struct usb_event ue;
717
718 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
719 usb_add_event(type, &ue);
720 }
721
722 void
723 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
724 {
725 struct usb_event ue;
726
727 ue.u.ue_driver.ue_cookie = udev->cookie;
728 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
729 sizeof ue.u.ue_driver.ue_devname);
730 usb_add_event(type, &ue);
731 }
732
733 Static void
734 usb_add_event(int type, struct usb_event *uep)
735 {
736 struct usb_event_q *ueq;
737 struct timeval thetime;
738 int s;
739
740 microtime(&thetime);
741 /* Don't want to wait here inside splusb() */
742 ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
743 ueq->ue = *uep;
744 ueq->ue.ue_type = type;
745 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
746
747 s = splusb();
748 if (++usb_nevents >= USB_MAX_EVENTS) {
749 /* Too many queued events, drop an old one. */
750 DPRINTFN(-1,("usb: event dropped\n"));
751 (void)usb_get_next_event(0);
752 }
753 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
754 wakeup(&usb_events);
755 selnotify(&usb_selevent, 0);
756 if (usb_async_proc != NULL)
757 psignal(usb_async_proc, SIGIO);
758 splx(s);
759 }
760
761 void
762 usb_schedsoftintr(usbd_bus_handle bus)
763 {
764 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
765 #ifdef USB_USE_SOFTINTR
766 if (bus->use_polling) {
767 bus->methods->soft_intr(bus);
768 } else {
769 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
770 softintr_schedule(bus->soft);
771 #else
772 if (!callout_pending(&bus->softi))
773 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
774 bus);
775 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
776 }
777 #else
778 bus->methods->soft_intr(bus);
779 #endif /* USB_USE_SOFTINTR */
780 }
781
782 int
783 usb_activate(device_ptr_t self, enum devact act)
784 {
785 struct usb_softc *sc = (struct usb_softc *)self;
786 usbd_device_handle dev = sc->sc_port.device;
787 int i, rv = 0;
788
789 switch (act) {
790 case DVACT_ACTIVATE:
791 return (EOPNOTSUPP);
792
793 case DVACT_DEACTIVATE:
794 sc->sc_dying = 1;
795 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
796 for (i = 0; dev->subdevs[i]; i++)
797 rv |= config_deactivate(dev->subdevs[i]);
798 }
799 break;
800 }
801 return (rv);
802 }
803
804 int
805 usb_detach(device_ptr_t self, int flags)
806 {
807 struct usb_softc *sc = (struct usb_softc *)self;
808 struct usb_event ue;
809
810 DPRINTF(("usb_detach: start\n"));
811
812 sc->sc_dying = 1;
813
814 /* Make all devices disconnect. */
815 if (sc->sc_port.device != NULL)
816 usb_disconnect_port(&sc->sc_port, self);
817
818 /* Kill off event thread. */
819 if (sc->sc_event_thread != NULL) {
820 wakeup(&sc->sc_bus->needs_explore);
821 if (tsleep(sc, PWAIT, "usbdet", hz * 60))
822 printf("%s: event thread didn't die\n",
823 USBDEVNAME(sc->sc_dev));
824 DPRINTF(("usb_detach: event thread dead\n"));
825 }
826
827 usbd_finish();
828
829 #ifdef USB_USE_SOFTINTR
830 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
831 if (sc->sc_bus->soft != NULL) {
832 softintr_disestablish(sc->sc_bus->soft);
833 sc->sc_bus->soft = NULL;
834 }
835 #else
836 callout_stop(&sc->sc_bus->softi);
837 #endif
838 #endif
839
840 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
841 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
842
843 return (0);
844 }
845