Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.30
      1 /*	$NetBSD: usb.c,v 1.30 1999/11/18 23:32:30 augustss 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 (augustss (at) carlstedt.se) 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 extern int uhcidebug;
     92 #endif
     93 #ifdef OHCI_DEBUG
     94 extern int ohcidebug;
     95 #endif
     96 int	usb_noexplore = 0;
     97 #else
     98 #define DPRINTF(x)
     99 #define DPRINTFN(n,x)
    100 #endif
    101 
    102 struct usb_softc {
    103 	USBBASEDEVICE	sc_dev;		/* base device */
    104 	usbd_bus_handle sc_bus;		/* USB controller */
    105 	struct usbd_port sc_port;	/* dummy port for root hub */
    106 
    107 #if defined(__FreeBSD__)
    108 	/* This part should be deleted when kthreads is available */
    109 	struct selinfo	sc_consel;	/* waiting for connect change */
    110 #else
    111 	struct proc    *sc_event_thread;
    112 #endif
    113 
    114 	char		sc_dying;
    115 };
    116 
    117 #if defined(__NetBSD__) || defined(__OpenBSD__)
    118 cdev_decl(usb);
    119 #elif defined(__FreeBSD__)
    120 d_open_t  usbopen;
    121 d_close_t usbclose;
    122 d_read_t usbread;
    123 d_ioctl_t usbioctl;
    124 int usbpoll __P((dev_t, int, struct proc *));
    125 
    126 struct cdevsw usb_cdevsw = {
    127 	/* open */      usbopen,
    128 	/* close */     usbclose,
    129 	/* read */      noread,
    130 	/* write */     nowrite,
    131 	/* ioctl */     usbioctl,
    132 	/* poll */      usbpoll,
    133 	/* mmap */      nommap,
    134 	/* strategy */  nostrategy,
    135 	/* name */      "usb",
    136 	/* maj */       USB_CDEV_MAJOR,
    137 	/* dump */      nodump,
    138 	/* psize */     nopsize,
    139 	/* flags */     0,
    140 	/* bmaj */      -1
    141 };
    142 #endif
    143 
    144 static usbd_status usb_discover __P((struct usb_softc *));
    145 static void	usb_create_event_thread __P((void *));
    146 static void	usb_event_thread __P((void *));
    147 
    148 #define USB_MAX_EVENTS 50
    149 struct usb_event_q {
    150 	struct usb_event ue;
    151 	SIMPLEQ_ENTRY(usb_event_q) next;
    152 };
    153 static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    154 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    155 static int usb_nevents = 0;
    156 static struct selinfo usb_selevent;
    157 static struct proc *usb_async_proc;  /* process who wants USB SIGIO */
    158 static int usb_dev_open = 0;
    159 
    160 static int usb_get_next_event __P((struct usb_event *));
    161 
    162 #if defined(__NetBSD__) || defined(__OpenBSD__)
    163 /* Flag to see if we are in the cold boot process. */
    164 extern int cold;
    165 #endif
    166 
    167 USB_DECLARE_DRIVER(usb);
    168 
    169 USB_MATCH(usb)
    170 {
    171 	DPRINTF(("usbd_match\n"));
    172 	return (UMATCH_GENERIC);
    173 }
    174 
    175 USB_ATTACH(usb)
    176 {
    177 #if defined(__NetBSD__) || defined(__OpenBSD__)
    178 	struct usb_softc *sc = (struct usb_softc *)self;
    179 #elif defined(__FreeBSD__)
    180 	struct usb_softc *sc = device_get_softc(self);
    181 	void *aux = device_get_ivars(self);
    182 #endif
    183 	usbd_device_handle dev;
    184 	usbd_status err;
    185 
    186 #if defined(__NetBSD__) || defined(__OpenBSD__)
    187 	printf("\n");
    188 #elif defined(__FreeBSD__)
    189 	sc->sc_dev = self;
    190 #endif
    191 
    192 	DPRINTF(("usbd_attach\n"));
    193 	usbd_init();
    194 	sc->sc_bus = aux;
    195 	sc->sc_bus->usbctl = sc;
    196 	sc->sc_port.power = USB_MAX_POWER;
    197 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
    198 		  &sc->sc_port);
    199 
    200 	if (!err) {
    201 		dev = sc->sc_port.device;
    202 		if (dev->hub == NULL) {
    203 			sc->sc_dying = 1;
    204 			printf("%s: root device is not a hub\n",
    205 			       USBDEVNAME(sc->sc_dev));
    206 			USB_ATTACH_ERROR_RETURN;
    207 		}
    208 		sc->sc_bus->root_hub = dev;
    209 #if 1
    210 		/*
    211 		 * Turning this code off will delay attachment of USB devices
    212 		 * until the USB event thread is running, which means that
    213 		 * the keyboard will not work until after cold boot.
    214 		 */
    215 		if (cold) {
    216 			sc->sc_bus->use_polling++;
    217 			dev->hub->explore(sc->sc_bus->root_hub);
    218 			sc->sc_bus->use_polling--;
    219 		}
    220 #endif
    221 	} else {
    222 		printf("%s: root hub problem, error=%d\n",
    223 		       USBDEVNAME(sc->sc_dev), err);
    224 		sc->sc_dying = 1;
    225 	}
    226 
    227 	kthread_create(usb_create_event_thread, sc);
    228 
    229 #if defined(__FreeBSD__)
    230 	make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
    231 		 0644, "usb%d", device_get_unit(self));
    232 #endif
    233 
    234 	USB_ATTACH_SUCCESS_RETURN;
    235 }
    236 
    237 #if defined(__NetBSD__) || defined(__OpenBSD__)
    238 void
    239 usb_create_event_thread(arg)
    240 	void *arg;
    241 {
    242 	struct usb_softc *sc = arg;
    243 
    244 	if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    245 			   "%s", sc->sc_dev.dv_xname)) {
    246 		printf("%s: unable to create event thread for\n",
    247 		       sc->sc_dev.dv_xname);
    248 		panic("usb_create_event_thread");
    249 	}
    250 }
    251 
    252 void
    253 usb_event_thread(arg)
    254 	void *arg;
    255 {
    256 	struct usb_softc *sc = arg;
    257 
    258 	DPRINTF(("usb_event_thread: start\n"));
    259 
    260 	while (!sc->sc_dying) {
    261 #ifdef USB_DEBUG
    262 		if (!usb_noexplore)
    263 #endif
    264 		usb_discover(sc);
    265 		(void)tsleep(&sc->sc_bus->needs_explore,
    266 			     PWAIT, "usbevt", hz*60);
    267 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    268 	}
    269 	sc->sc_event_thread = 0;
    270 
    271 	/* In case parent is waiting for us to exit. */
    272 	wakeup(sc);
    273 
    274 	DPRINTF(("usb_event_thread: exit\n"));
    275 	kthread_exit(0);
    276 }
    277 
    278 int
    279 usbctlprint(aux, pnp)
    280 	void *aux;
    281 	const char *pnp;
    282 {
    283 	/* only "usb"es can attach to host controllers */
    284 	if (pnp)
    285 		printf("usb at %s", pnp);
    286 
    287 	return (UNCONF);
    288 }
    289 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    290 
    291 int
    292 usbopen(dev, flag, mode, p)
    293 	dev_t dev;
    294 	int flag, mode;
    295 	struct proc *p;
    296 {
    297 	int unit = minor(dev);
    298 	struct usb_softc *sc;
    299 
    300 	if (unit == USB_DEV_MINOR) {
    301 		if (usb_dev_open)
    302 			return (EBUSY);
    303 		usb_dev_open = 1;
    304 		usb_async_proc = 0;
    305 		return (0);
    306 	}
    307 
    308 	USB_GET_SC_OPEN(usb, unit, sc);
    309 
    310 	if (sc->sc_dying)
    311 		return (EIO);
    312 
    313 	return (0);
    314 }
    315 
    316 int
    317 usbread(dev, uio, flag)
    318 	dev_t dev;
    319 	struct uio *uio;
    320 	int flag;
    321 {
    322 	struct usb_event ue;
    323 	int s, error, n;
    324 
    325 	if (minor(dev) != USB_DEV_MINOR)
    326 		return (ENXIO);
    327 
    328 	if (uio->uio_resid != sizeof(struct usb_event))
    329 		return (EINVAL);
    330 
    331 	error = 0;
    332 	s = splusb();
    333 	for (;;) {
    334 		n = usb_get_next_event(&ue);
    335 		if (n != 0)
    336 			break;
    337 		if (flag & IO_NDELAY) {
    338 			error = EWOULDBLOCK;
    339 			break;
    340 		}
    341 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    342 		if (error)
    343 			break;
    344 	}
    345 	splx(s);
    346 	if (!error)
    347 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    348 
    349 	return (error);
    350 }
    351 
    352 int
    353 usbclose(dev, flag, mode, p)
    354 	dev_t dev;
    355 	int flag, mode;
    356 	struct proc *p;
    357 {
    358 	int unit = minor(dev);
    359 
    360 	if (unit == USB_DEV_MINOR) {
    361 		usb_async_proc = 0;
    362 		usb_dev_open = 0;
    363 	}
    364 
    365 	return (0);
    366 }
    367 
    368 int
    369 usbioctl(devt, cmd, data, flag, p)
    370 	dev_t devt;
    371 	u_long cmd;
    372 	caddr_t data;
    373 	int flag;
    374 	struct proc *p;
    375 {
    376 	struct usb_softc *sc;
    377 	int unit = minor(devt);
    378 
    379 	if (unit == USB_DEV_MINOR) {
    380 		switch (cmd) {
    381 		case FIONBIO:
    382 			/* All handled in the upper FS layer. */
    383 			return (0);
    384 
    385 		case FIOASYNC:
    386 			if (*(int *)data)
    387 				usb_async_proc = p;
    388 			else
    389 				usb_async_proc = 0;
    390 			return (0);
    391 
    392 		default:
    393 			return (EINVAL);
    394 		}
    395 	}
    396 
    397 	USB_GET_SC(usb, unit, sc);
    398 
    399 	if (sc->sc_dying)
    400 		return (EIO);
    401 
    402 	switch (cmd) {
    403 #if defined(__FreeBSD__)
    404 	/* This part should be deleted when kthreads is available */
    405   	case USB_DISCOVER:
    406   		usb_discover(sc);
    407   		break;
    408 #endif
    409 #ifdef USB_DEBUG
    410 	case USB_SETDEBUG:
    411 		usbdebug  = ((*(int *)data) & 0x000000ff);
    412 #ifdef UHCI_DEBUG
    413 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    414 #endif
    415 #ifdef OHCI_DEBUG
    416 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    417 #endif
    418 		break;
    419 #endif
    420 	case USB_REQUEST:
    421 	{
    422 		struct usb_ctl_request *ur = (void *)data;
    423 		int len = UGETW(ur->request.wLength);
    424 		struct iovec iov;
    425 		struct uio uio;
    426 		void *ptr = 0;
    427 		int addr = ur->addr;
    428 		usbd_status err;
    429 		int error = 0;
    430 
    431 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    432 		if (len < 0 || len > 32768)
    433 			return (EINVAL);
    434 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    435 		    sc->sc_bus->devices[addr] == 0)
    436 			return (EINVAL);
    437 		if (len != 0) {
    438 			iov.iov_base = (caddr_t)ur->data;
    439 			iov.iov_len = len;
    440 			uio.uio_iov = &iov;
    441 			uio.uio_iovcnt = 1;
    442 			uio.uio_resid = len;
    443 			uio.uio_offset = 0;
    444 			uio.uio_segflg = UIO_USERSPACE;
    445 			uio.uio_rw =
    446 				ur->request.bmRequestType & UT_READ ?
    447 				UIO_READ : UIO_WRITE;
    448 			uio.uio_procp = p;
    449 			ptr = malloc(len, M_TEMP, M_WAITOK);
    450 			if (uio.uio_rw == UIO_WRITE) {
    451 				error = uiomove(ptr, len, &uio);
    452 				if (error)
    453 					goto ret;
    454 			}
    455 		}
    456 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    457 			  &ur->request, ptr, ur->flags, &ur->actlen);
    458 		if (err) {
    459 			error = EIO;
    460 			goto ret;
    461 		}
    462 		if (len != 0) {
    463 			if (uio.uio_rw == UIO_READ) {
    464 				error = uiomove(ptr, len, &uio);
    465 				if (error)
    466 					goto ret;
    467 			}
    468 		}
    469 	ret:
    470 		if (ptr)
    471 			free(ptr, M_TEMP);
    472 		return (error);
    473 	}
    474 
    475 	case USB_DEVICEINFO:
    476 	{
    477 		struct usb_device_info *di = (void *)data;
    478 		int addr = di->addr;
    479 		usbd_device_handle dev;
    480 
    481 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    482 			return (EINVAL);
    483 		dev = sc->sc_bus->devices[addr];
    484 		if (dev == NULL)
    485 			return (ENXIO);
    486 		usbd_fill_deviceinfo(dev, di);
    487 		break;
    488 	}
    489 
    490 	case USB_DEVICESTATS:
    491 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    492 		break;
    493 
    494 	default:
    495 		return (EINVAL);
    496 	}
    497 	return (0);
    498 }
    499 
    500 int
    501 usbpoll(dev, events, p)
    502 	dev_t dev;
    503 	int events;
    504 	struct proc *p;
    505 {
    506 	int revents, mask, s;
    507 
    508 	if (minor(dev) == USB_DEV_MINOR) {
    509 		revents = 0;
    510 		mask = POLLIN | POLLRDNORM;
    511 
    512 		s = splusb();
    513 		if (events & mask && usb_nevents > 0)
    514 			revents |= events & mask;
    515 		if (revents == 0 && events & mask)
    516 			selrecord(p, &usb_selevent);
    517 		splx(s);
    518 
    519 		return (revents);
    520 	} else {
    521 #if defined(__FreeBSD__)
    522 		/* This part should be deleted when kthreads is available */
    523 		struct usb_softc *sc;
    524 		int unit = minor(dev);
    525 
    526 		USB_GET_SC(usb, unit, sc);
    527 
    528 		revents = 0;
    529 		mask = POLLOUT | POLLRDNORM;
    530 
    531 		s = splusb();
    532 		if (events & mask && sc->sc_bus->needs_explore)
    533 			revents |= events & mask;
    534 		if (revents == 0 && events & mask)
    535 			selrecord(p, &sc->sc_consel);
    536 		splx(s);
    537 
    538 		return (revents);
    539 #else
    540 		return (ENXIO);
    541 #endif
    542 	}
    543 }
    544 
    545 /* Explore device tree from the root. */
    546 usbd_status
    547 usb_discover(sc)
    548 	struct usb_softc *sc;
    549 {
    550 #if defined(__FreeBSD__)
    551 	/* The splxxx parts should be deleted when kthreads is available */
    552 	int s;
    553 #endif
    554 
    555 	/*
    556 	 * We need mutual exclusion while traversing the device tree,
    557 	 * but this is guaranteed since this function is only called
    558 	 * from the event thread for the controller.
    559 	 */
    560 #if defined(__FreeBSD__)
    561 	s = splusb();
    562 #endif
    563 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    564 		sc->sc_bus->needs_explore = 0;
    565 #if defined(__FreeBSD__)
    566 		splx(s);
    567 #endif
    568 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    569 #if defined(__FreeBSD__)
    570 		s = splusb();
    571 #endif
    572 	}
    573 #if defined(__FreeBSD__)
    574 	splx(s);
    575 #endif
    576 
    577 	return (USBD_NORMAL_COMPLETION);
    578 }
    579 
    580 void
    581 usb_needs_explore(bus)
    582 	usbd_bus_handle bus;
    583 {
    584 	bus->needs_explore = 1;
    585 #if defined(__FreeBSD__)
    586 	/* This part should be deleted when kthreads is available */
    587 	selwakeup(&bus->usbctl->sc_consel);
    588 #endif
    589 	wakeup(&bus->needs_explore);
    590 }
    591 
    592 /* Called at splusb() */
    593 int
    594 usb_get_next_event(ue)
    595 	struct usb_event *ue;
    596 {
    597 	struct usb_event_q *ueq;
    598 
    599 	if (usb_nevents <= 0)
    600 		return (0);
    601 	ueq = SIMPLEQ_FIRST(&usb_events);
    602 	*ue = ueq->ue;
    603 	SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
    604 	free(ueq, M_USBDEV);
    605 	usb_nevents--;
    606 	return (1);
    607 }
    608 
    609 void
    610 usbd_add_event(type, dev)
    611 	int type;
    612 	usbd_device_handle dev;
    613 {
    614 	struct usb_event_q *ueq;
    615 	struct usb_event ue;
    616 	struct timeval thetime;
    617 	int s;
    618 
    619 	s = splusb();
    620 	if (++usb_nevents >= USB_MAX_EVENTS) {
    621 		/* Too many queued events, drop an old one. */
    622 		DPRINTFN(-1,("usb: event dropped\n"));
    623 		(void)usb_get_next_event(&ue);
    624 	}
    625 	/* Don't want to wait here inside splusb() */
    626 	ueq = malloc(sizeof *ueq, M_USBDEV, M_NOWAIT);
    627 	if (ueq == NULL) {
    628 		printf("usb: no memory, event dropped\n");
    629 		splx(s);
    630 		return;
    631 	}
    632 	ueq->ue.ue_type = type;
    633 	ueq->ue.ue_cookie = dev->cookie;
    634 	usbd_fill_deviceinfo(dev, &ueq->ue.ue_device);
    635 	microtime(&thetime);
    636 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    637 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    638 	wakeup(&usb_events);
    639 	selwakeup(&usb_selevent);
    640 	if (usb_async_proc != NULL)
    641 		psignal(usb_async_proc, SIGIO);
    642 	splx(s);
    643 }
    644 
    645 #if defined(__NetBSD__) || defined(__OpenBSD__)
    646 int
    647 usb_activate(self, act)
    648 	device_ptr_t self;
    649 	enum devact act;
    650 {
    651 	struct usb_softc *sc = (struct usb_softc *)self;
    652 	usbd_device_handle dev = sc->sc_port.device;
    653 	int i, rv = 0;
    654 
    655 	switch (act) {
    656 	case DVACT_ACTIVATE:
    657 		return (EOPNOTSUPP);
    658 		break;
    659 
    660 	case DVACT_DEACTIVATE:
    661 		sc->sc_dying = 1;
    662 		if (dev && dev->cdesc && dev->subdevs) {
    663 			for (i = 0; dev->subdevs[i]; i++)
    664 				rv |= config_deactivate(dev->subdevs[i]);
    665 		}
    666 		break;
    667 	}
    668 	return (rv);
    669 }
    670 
    671 int
    672 usb_detach(self, flags)
    673 	device_ptr_t self;
    674 	int flags;
    675 {
    676 	struct usb_softc *sc = (struct usb_softc *)self;
    677 
    678 	DPRINTF(("usb_detach: start\n"));
    679 
    680 	sc->sc_dying = 1;
    681 
    682 	/* Make all devices disconnect. */
    683 	if (sc->sc_port.device)
    684 		usb_disconnect_port(&sc->sc_port, self);
    685 
    686 	/* Kill off event thread. */
    687 	if (sc->sc_event_thread) {
    688 		wakeup(&sc->sc_bus->needs_explore);
    689 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    690 			printf("%s: event thread didn't die\n",
    691 			       USBDEVNAME(sc->sc_dev));
    692 		DPRINTF(("usb_detach: event thread dead\n"));
    693 	}
    694 
    695 	usbd_finish();
    696 	return (0);
    697 }
    698 #elif defined(__FreeBSD__)
    699 int
    700 usb_detach(device_t self)
    701 {
    702 	DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
    703 
    704 	return (EINVAL);
    705 }
    706 #endif
    707 
    708 
    709 #if defined(__FreeBSD__)
    710 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
    711 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
    712 #endif
    713