xref: /netbsd-src/sys/dev/usb/usb.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: usb.c,v 1.46 2000/06/07 00:33:51 thorpej Exp $	*/
2 /*	$FreeBSD: src/sys/dev/usb/usb.c,v 1.20 1999/11/17 22:33:46 n_hibma Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * USB specifications and other documentation can be found at
43  * http://www.usb.org/developers/data/ and
44  * http://www.usb.org/developers/index.html .
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/device.h>
53 #include <sys/kthread.h>
54 #include <sys/proc.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <sys/filio.h>
59 #include <sys/uio.h>
60 #endif
61 #include <sys/conf.h>
62 #include <sys/poll.h>
63 #include <sys/select.h>
64 #include <sys/vnode.h>
65 #include <sys/signalvar.h>
66 
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdi_util.h>
70 
71 #define USB_DEV_MINOR 255
72 
73 #if defined(__FreeBSD__)
74 MALLOC_DEFINE(M_USB, "USB", "USB");
75 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
76 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
77 
78 #include "usb_if.h"
79 #endif /* defined(__FreeBSD__) */
80 
81 #include <machine/bus.h>
82 
83 #include <dev/usb/usbdivar.h>
84 #include <dev/usb/usb_quirks.h>
85 
86 #ifdef USB_DEBUG
87 #define DPRINTF(x)	if (usbdebug) logprintf x
88 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
89 int	usbdebug = 0;
90 #ifdef UHCI_DEBUG
91 int	uhcidebug;
92 #endif
93 #ifdef OHCI_DEBUG
94 int	ohcidebug;
95 #endif
96 /*
97  * 0  - do usual exploration
98  * 1  - do not use timeout exploration
99  * >1 - do no exploration
100  */
101 int	usb_noexplore = 0;
102 #else
103 #define DPRINTF(x)
104 #define DPRINTFN(n,x)
105 #endif
106 
107 struct usb_softc {
108 	USBBASEDEVICE	sc_dev;		/* base device */
109 	usbd_bus_handle sc_bus;		/* USB controller */
110 	struct usbd_port sc_port;	/* dummy port for root hub */
111 
112 #if defined(__FreeBSD__)
113 	/* This part should be deleted when kthreads is available */
114 	struct selinfo	sc_consel;	/* waiting for connect change */
115 #else
116 	struct proc    *sc_event_thread;
117 #endif
118 
119 	char		sc_dying;
120 };
121 
122 #if defined(__NetBSD__) || defined(__OpenBSD__)
123 cdev_decl(usb);
124 #elif defined(__FreeBSD__)
125 d_open_t  usbopen;
126 d_close_t usbclose;
127 d_read_t usbread;
128 d_ioctl_t usbioctl;
129 int usbpoll(dev_t, int, struct proc *);
130 
131 struct cdevsw usb_cdevsw = {
132 	/* open */      usbopen,
133 	/* close */     usbclose,
134 	/* read */      noread,
135 	/* write */     nowrite,
136 	/* ioctl */     usbioctl,
137 	/* poll */      usbpoll,
138 	/* mmap */      nommap,
139 	/* strategy */  nostrategy,
140 	/* name */      "usb",
141 	/* maj */       USB_CDEV_MAJOR,
142 	/* dump */      nodump,
143 	/* psize */     nopsize,
144 	/* flags */     0,
145 	/* bmaj */      -1
146 };
147 #endif
148 
149 Static usbd_status usb_discover(struct usb_softc *);
150 Static void	usb_create_event_thread(void *);
151 Static void	usb_event_thread(void *);
152 
153 #define USB_MAX_EVENTS 100
154 struct usb_event_q {
155 	struct usb_event ue;
156 	SIMPLEQ_ENTRY(usb_event_q) next;
157 };
158 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
159 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
160 Static int usb_nevents = 0;
161 Static struct selinfo usb_selevent;
162 Static struct proc *usb_async_proc;  /* process who wants USB SIGIO */
163 Static int usb_dev_open = 0;
164 Static void usb_add_event(int, struct usb_event *);
165 
166 Static int usb_get_next_event(struct usb_event *);
167 
168 #if defined(__NetBSD__) || defined(__OpenBSD__)
169 /* Flag to see if we are in the cold boot process. */
170 extern int cold;
171 #endif
172 
173 Static const char *usbrev_str[] = USBREV_STR;
174 
175 USB_DECLARE_DRIVER(usb);
176 
177 USB_MATCH(usb)
178 {
179 	DPRINTF(("usbd_match\n"));
180 	return (UMATCH_GENERIC);
181 }
182 
183 USB_ATTACH(usb)
184 {
185 #if defined(__NetBSD__) || defined(__OpenBSD__)
186 	struct usb_softc *sc = (struct usb_softc *)self;
187 #elif defined(__FreeBSD__)
188 	struct usb_softc *sc = device_get_softc(self);
189 	void *aux = device_get_ivars(self);
190 #endif
191 	usbd_device_handle dev;
192 	usbd_status err;
193 	int usbrev;
194 	struct usb_event ue;
195 
196 #if defined(__FreeBSD__)
197 	printf("%s", USBDEVNAME(sc->sc_dev));
198 	sc->sc_dev = self;
199 #endif
200 
201 	DPRINTF(("usbd_attach\n"));
202 
203 	usbd_init();
204 	sc->sc_bus = aux;
205 	sc->sc_bus->usbctl = sc;
206 	sc->sc_port.power = USB_MAX_POWER;
207 
208 	usbrev = sc->sc_bus->usbrev;
209 	printf(": USB revision %s", usbrev_str[usbrev]);
210 	if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
211 		printf(", not supported\n");
212 		USB_ATTACH_ERROR_RETURN;
213 	}
214 	printf("\n");
215 
216 	/* Make sure not to use tsleep() if we are cold booting. */
217 	if (cold)
218 		sc->sc_bus->use_polling++;
219 
220 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
221 	usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
222 
223 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
224 		  &sc->sc_port);
225 	if (!err) {
226 		dev = sc->sc_port.device;
227 		if (dev->hub == NULL) {
228 			sc->sc_dying = 1;
229 			printf("%s: root device is not a hub\n",
230 			       USBDEVNAME(sc->sc_dev));
231 			USB_ATTACH_ERROR_RETURN;
232 		}
233 		sc->sc_bus->root_hub = dev;
234 #if 1
235 		/*
236 		 * Turning this code off will delay attachment of USB devices
237 		 * until the USB event thread is running, which means that
238 		 * the keyboard will not work until after cold boot.
239 		 */
240 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
241 			dev->hub->explore(sc->sc_bus->root_hub);
242 #endif
243 	} else {
244 		printf("%s: root hub problem, error=%d\n",
245 		       USBDEVNAME(sc->sc_dev), err);
246 		sc->sc_dying = 1;
247 	}
248 	if (cold)
249 		sc->sc_bus->use_polling--;
250 
251 	config_pending_incr();
252 	usb_kthread_create(usb_create_event_thread, sc);
253 
254 #if defined(__FreeBSD__)
255 	make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
256 		 0644, "usb%d", device_get_unit(self));
257 #endif
258 
259 	USB_ATTACH_SUCCESS_RETURN;
260 }
261 
262 #if defined(__NetBSD__) || defined(__OpenBSD__)
263 void
264 usb_create_event_thread(void *arg)
265 {
266 	struct usb_softc *sc = arg;
267 
268 	if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
269 			   "%s", sc->sc_dev.dv_xname)) {
270 		printf("%s: unable to create event thread for\n",
271 		       sc->sc_dev.dv_xname);
272 		panic("usb_create_event_thread");
273 	}
274 }
275 
276 void
277 usb_event_thread(void *arg)
278 {
279 	struct usb_softc *sc = arg;
280 	int first = 1;
281 
282 	DPRINTF(("usb_event_thread: start\n"));
283 
284 	/* Make sure first discover does something. */
285 	sc->sc_bus->needs_explore = 1;
286 
287 	while (!sc->sc_dying) {
288 #ifdef USB_DEBUG
289 		if (usb_noexplore < 2)
290 #endif
291 		usb_discover(sc);
292 		if (first) {
293 			config_pending_decr();
294 			first = 0;
295 		}
296 #ifdef USB_DEBUG
297 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
298 		    usb_noexplore ? 0 : hz * 60);
299 #else
300 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
301 		    hz * 60);
302 #endif
303 		DPRINTFN(2,("usb_event_thread: woke up\n"));
304 	}
305 	sc->sc_event_thread = 0;
306 
307 	/* In case parent is waiting for us to exit. */
308 	wakeup(sc);
309 
310 	DPRINTF(("usb_event_thread: exit\n"));
311 	kthread_exit(0);
312 }
313 
314 int
315 usbctlprint(void *aux, const char *pnp)
316 {
317 	/* only "usb"es can attach to host controllers */
318 	if (pnp)
319 		printf("usb at %s", pnp);
320 
321 	return (UNCONF);
322 }
323 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
324 
325 int
326 usbopen(dev_t dev, int flag, int mode, struct proc *p)
327 {
328 	int unit = minor(dev);
329 	struct usb_softc *sc;
330 
331 	if (unit == USB_DEV_MINOR) {
332 		if (usb_dev_open)
333 			return (EBUSY);
334 		usb_dev_open = 1;
335 		usb_async_proc = 0;
336 		return (0);
337 	}
338 
339 	USB_GET_SC_OPEN(usb, unit, sc);
340 
341 	if (sc->sc_dying)
342 		return (EIO);
343 
344 	return (0);
345 }
346 
347 int
348 usbread(dev_t dev, struct uio *uio, int flag)
349 {
350 	struct usb_event ue;
351 	int s, error, n;
352 
353 	if (minor(dev) != USB_DEV_MINOR)
354 		return (ENXIO);
355 
356 	if (uio->uio_resid != sizeof(struct usb_event))
357 		return (EINVAL);
358 
359 	error = 0;
360 	s = splusb();
361 	for (;;) {
362 		n = usb_get_next_event(&ue);
363 		if (n != 0)
364 			break;
365 		if (flag & IO_NDELAY) {
366 			error = EWOULDBLOCK;
367 			break;
368 		}
369 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
370 		if (error)
371 			break;
372 	}
373 	splx(s);
374 	if (!error)
375 		error = uiomove((void *)&ue, uio->uio_resid, uio);
376 
377 	return (error);
378 }
379 
380 int
381 usbclose(dev_t dev, int flag, int mode, struct proc *p)
382 {
383 	int unit = minor(dev);
384 
385 	if (unit == USB_DEV_MINOR) {
386 		usb_async_proc = 0;
387 		usb_dev_open = 0;
388 	}
389 
390 	return (0);
391 }
392 
393 int
394 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
395 {
396 	struct usb_softc *sc;
397 	int unit = minor(devt);
398 
399 	if (unit == USB_DEV_MINOR) {
400 		switch (cmd) {
401 		case FIONBIO:
402 			/* All handled in the upper FS layer. */
403 			return (0);
404 
405 		case FIOASYNC:
406 			if (*(int *)data)
407 				usb_async_proc = p;
408 			else
409 				usb_async_proc = 0;
410 			return (0);
411 
412 		default:
413 			return (EINVAL);
414 		}
415 	}
416 
417 	USB_GET_SC(usb, unit, sc);
418 
419 	if (sc->sc_dying)
420 		return (EIO);
421 
422 	switch (cmd) {
423 #if defined(__FreeBSD__)
424 	/* This part should be deleted when kthreads is available */
425   	case USB_DISCOVER:
426   		usb_discover(sc);
427   		break;
428 #endif
429 #ifdef USB_DEBUG
430 	case USB_SETDEBUG:
431 		usbdebug  = ((*(int *)data) & 0x000000ff);
432 #ifdef UHCI_DEBUG
433 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
434 #endif
435 #ifdef OHCI_DEBUG
436 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
437 #endif
438 		break;
439 #endif
440 	case USB_REQUEST:
441 	{
442 		struct usb_ctl_request *ur = (void *)data;
443 		int len = UGETW(ur->request.wLength);
444 		struct iovec iov;
445 		struct uio uio;
446 		void *ptr = 0;
447 		int addr = ur->addr;
448 		usbd_status err;
449 		int error = 0;
450 
451 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
452 		if (len < 0 || len > 32768)
453 			return (EINVAL);
454 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
455 		    sc->sc_bus->devices[addr] == 0)
456 			return (EINVAL);
457 		if (len != 0) {
458 			iov.iov_base = (caddr_t)ur->data;
459 			iov.iov_len = len;
460 			uio.uio_iov = &iov;
461 			uio.uio_iovcnt = 1;
462 			uio.uio_resid = len;
463 			uio.uio_offset = 0;
464 			uio.uio_segflg = UIO_USERSPACE;
465 			uio.uio_rw =
466 				ur->request.bmRequestType & UT_READ ?
467 				UIO_READ : UIO_WRITE;
468 			uio.uio_procp = p;
469 			ptr = malloc(len, M_TEMP, M_WAITOK);
470 			if (uio.uio_rw == UIO_WRITE) {
471 				error = uiomove(ptr, len, &uio);
472 				if (error)
473 					goto ret;
474 			}
475 		}
476 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
477 			  &ur->request, ptr, ur->flags, &ur->actlen);
478 		if (err) {
479 			error = EIO;
480 			goto ret;
481 		}
482 		if (len != 0) {
483 			if (uio.uio_rw == UIO_READ) {
484 				error = uiomove(ptr, len, &uio);
485 				if (error)
486 					goto ret;
487 			}
488 		}
489 	ret:
490 		if (ptr)
491 			free(ptr, M_TEMP);
492 		return (error);
493 	}
494 
495 	case USB_DEVICEINFO:
496 	{
497 		struct usb_device_info *di = (void *)data;
498 		int addr = di->addr;
499 		usbd_device_handle dev;
500 
501 		if (addr < 1 || addr >= USB_MAX_DEVICES)
502 			return (EINVAL);
503 		dev = sc->sc_bus->devices[addr];
504 		if (dev == NULL)
505 			return (ENXIO);
506 		usbd_fill_deviceinfo(dev, di);
507 		break;
508 	}
509 
510 	case USB_DEVICESTATS:
511 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
512 		break;
513 
514 	default:
515 		return (EINVAL);
516 	}
517 	return (0);
518 }
519 
520 int
521 usbpoll(dev_t dev, int events, struct proc *p)
522 {
523 	int revents, mask, s;
524 
525 	if (minor(dev) == USB_DEV_MINOR) {
526 		revents = 0;
527 		mask = POLLIN | POLLRDNORM;
528 
529 		s = splusb();
530 		if (events & mask && usb_nevents > 0)
531 			revents |= events & mask;
532 		if (revents == 0 && events & mask)
533 			selrecord(p, &usb_selevent);
534 		splx(s);
535 
536 		return (revents);
537 	} else {
538 #if defined(__FreeBSD__)
539 		/* This part should be deleted when kthreads is available */
540 		struct usb_softc *sc;
541 		int unit = minor(dev);
542 
543 		USB_GET_SC(usb, unit, sc);
544 
545 		revents = 0;
546 		mask = POLLOUT | POLLRDNORM;
547 
548 		s = splusb();
549 		if (events & mask && sc->sc_bus->needs_explore)
550 			revents |= events & mask;
551 		if (revents == 0 && events & mask)
552 			selrecord(p, &sc->sc_consel);
553 		splx(s);
554 
555 		return (revents);
556 #else
557 		return (ENXIO);
558 #endif
559 	}
560 }
561 
562 /* Explore device tree from the root. */
563 usbd_status
564 usb_discover(struct usb_softc *sc)
565 {
566 #if defined(__FreeBSD__)
567 	/* The splxxx parts should be deleted when kthreads is available */
568 	int s;
569 #endif
570 
571 	/*
572 	 * We need mutual exclusion while traversing the device tree,
573 	 * but this is guaranteed since this function is only called
574 	 * from the event thread for the controller.
575 	 */
576 #if defined(__FreeBSD__)
577 	s = splusb();
578 #endif
579 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
580 		sc->sc_bus->needs_explore = 0;
581 #if defined(__FreeBSD__)
582 		splx(s);
583 #endif
584 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
585 #if defined(__FreeBSD__)
586 		s = splusb();
587 #endif
588 	}
589 #if defined(__FreeBSD__)
590 	splx(s);
591 #endif
592 
593 	return (USBD_NORMAL_COMPLETION);
594 }
595 
596 void
597 usb_needs_explore(usbd_bus_handle bus)
598 {
599 	bus->needs_explore = 1;
600 #if defined(__FreeBSD__)
601 	/* This part should be deleted when kthreads is available */
602 	selwakeup(&bus->usbctl->sc_consel);
603 #endif
604 	wakeup(&bus->needs_explore);
605 }
606 
607 /* Called at splusb() */
608 int
609 usb_get_next_event(struct usb_event *ue)
610 {
611 	struct usb_event_q *ueq;
612 
613 	if (usb_nevents <= 0)
614 		return (0);
615 	ueq = SIMPLEQ_FIRST(&usb_events);
616 	*ue = ueq->ue;
617 	SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
618 	free(ueq, M_USBDEV);
619 	usb_nevents--;
620 	return (1);
621 }
622 
623 void
624 usbd_add_dev_event(int type, usbd_device_handle udev)
625 {
626 	struct usb_event ue;
627 
628 	usbd_fill_deviceinfo(udev, &ue.u.ue_device);
629 	usb_add_event(type, &ue);
630 }
631 
632 void
633 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
634 {
635 	struct usb_event ue;
636 
637 	ue.u.ue_driver.ue_cookie = udev->cookie;
638 	strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
639 	    sizeof ue.u.ue_driver.ue_devname);
640 	usb_add_event(type, &ue);
641 }
642 
643 Static void
644 usb_add_event(int type, struct usb_event *uep)
645 {
646 	struct usb_event_q *ueq;
647 	struct usb_event ue;
648 	struct timeval thetime;
649 	int s;
650 
651 	microtime(&thetime);
652 	/* Don't want to wait here inside splusb() */
653 	ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
654 	ueq->ue = *uep;
655 	ueq->ue.ue_type = type;
656 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
657 
658 	s = splusb();
659 	if (++usb_nevents >= USB_MAX_EVENTS) {
660 		/* Too many queued events, drop an old one. */
661 		DPRINTFN(-1,("usb: event dropped\n"));
662 		(void)usb_get_next_event(&ue);
663 	}
664 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
665 	wakeup(&usb_events);
666 	selwakeup(&usb_selevent);
667 	if (usb_async_proc != NULL)
668 		psignal(usb_async_proc, SIGIO);
669 	splx(s);
670 }
671 void
672 usb_schedsoftintr(struct usbd_bus *bus)
673 {
674 	bus->methods->soft_intr(bus);
675 }
676 
677 #if defined(__NetBSD__) || defined(__OpenBSD__)
678 int
679 usb_activate(device_ptr_t self, enum devact act)
680 {
681 	struct usb_softc *sc = (struct usb_softc *)self;
682 	usbd_device_handle dev = sc->sc_port.device;
683 	int i, rv = 0;
684 
685 	switch (act) {
686 	case DVACT_ACTIVATE:
687 		return (EOPNOTSUPP);
688 		break;
689 
690 	case DVACT_DEACTIVATE:
691 		sc->sc_dying = 1;
692 		if (dev && dev->cdesc && dev->subdevs) {
693 			for (i = 0; dev->subdevs[i]; i++)
694 				rv |= config_deactivate(dev->subdevs[i]);
695 		}
696 		break;
697 	}
698 	return (rv);
699 }
700 
701 int
702 usb_detach(device_ptr_t self, int flags)
703 {
704 	struct usb_softc *sc = (struct usb_softc *)self;
705 	struct usb_event ue;
706 
707 	DPRINTF(("usb_detach: start\n"));
708 
709 	sc->sc_dying = 1;
710 
711 	/* Make all devices disconnect. */
712 	if (sc->sc_port.device)
713 		usb_disconnect_port(&sc->sc_port, self);
714 
715 	/* Kill off event thread. */
716 	if (sc->sc_event_thread) {
717 		wakeup(&sc->sc_bus->needs_explore);
718 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
719 			printf("%s: event thread didn't die\n",
720 			       USBDEVNAME(sc->sc_dev));
721 		DPRINTF(("usb_detach: event thread dead\n"));
722 	}
723 
724 	usbd_finish();
725 
726 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
727 	usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
728 
729 	return (0);
730 }
731 #elif defined(__FreeBSD__)
732 int
733 usb_detach(device_t self)
734 {
735 	DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
736 
737 	return (EINVAL);
738 }
739 #endif
740 
741 
742 #if defined(__FreeBSD__)
743 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
744 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
745 #endif
746