Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.34
      1 /*	$NetBSD: usb.c,v 1.34 1999/11/26 01:41:03 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 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 __P((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 __P((struct usb_softc *));
    150 static void	usb_create_event_thread __P((void *));
    151 static void	usb_event_thread __P((void *));
    152 
    153 #define USB_MAX_EVENTS 50
    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 
    165 static int usb_get_next_event __P((struct usb_event *));
    166 
    167 #if defined(__NetBSD__) || defined(__OpenBSD__)
    168 /* Flag to see if we are in the cold boot process. */
    169 extern int cold;
    170 #endif
    171 
    172 static const char *usbrev_str[] = USBREV_STR;
    173 
    174 USB_DECLARE_DRIVER(usb);
    175 
    176 USB_MATCH(usb)
    177 {
    178 	DPRINTF(("usbd_match\n"));
    179 	return (UMATCH_GENERIC);
    180 }
    181 
    182 USB_ATTACH(usb)
    183 {
    184 #if defined(__NetBSD__) || defined(__OpenBSD__)
    185 	struct usb_softc *sc = (struct usb_softc *)self;
    186 #elif defined(__FreeBSD__)
    187 	struct usb_softc *sc = device_get_softc(self);
    188 	void *aux = device_get_ivars(self);
    189 #endif
    190 	usbd_device_handle dev;
    191 	usbd_status err;
    192 	int usbrev;
    193 
    194 #if defined(__FreeBSD__)
    195 	printf("%s", USBDEVNAME(sc->sc_dev));
    196 	sc->sc_dev = self;
    197 #endif
    198 
    199 	DPRINTF(("usbd_attach\n"));
    200 
    201 	usbd_init();
    202 	sc->sc_bus = aux;
    203 	sc->sc_bus->usbctl = sc;
    204 	sc->sc_port.power = USB_MAX_POWER;
    205 
    206 	usbrev = sc->sc_bus->usbrev;
    207 	printf(": USB revision %s", usbrev_str[usbrev]);
    208 	if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
    209 		printf(", not supported\n");
    210 		USB_ATTACH_ERROR_RETURN;
    211 	}
    212 	printf("\n");
    213 
    214 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
    215 		  &sc->sc_port);
    216 	if (!err) {
    217 		dev = sc->sc_port.device;
    218 		if (dev->hub == NULL) {
    219 			sc->sc_dying = 1;
    220 			printf("%s: root device is not a hub\n",
    221 			       USBDEVNAME(sc->sc_dev));
    222 			USB_ATTACH_ERROR_RETURN;
    223 		}
    224 		sc->sc_bus->root_hub = dev;
    225 #if 1
    226 		/*
    227 		 * Turning this code off will delay attachment of USB devices
    228 		 * until the USB event thread is running, which means that
    229 		 * the keyboard will not work until after cold boot.
    230 		 */
    231 		if (cold) {
    232 			sc->sc_bus->use_polling++;
    233 			dev->hub->explore(sc->sc_bus->root_hub);
    234 			sc->sc_bus->use_polling--;
    235 		}
    236 #endif
    237 	} else {
    238 		printf("%s: root hub problem, error=%d\n",
    239 		       USBDEVNAME(sc->sc_dev), err);
    240 		sc->sc_dying = 1;
    241 	}
    242 
    243 	kthread_create(usb_create_event_thread, sc);
    244 
    245 #if defined(__FreeBSD__)
    246 	make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
    247 		 0644, "usb%d", device_get_unit(self));
    248 #endif
    249 
    250 	USB_ATTACH_SUCCESS_RETURN;
    251 }
    252 
    253 #if defined(__NetBSD__) || defined(__OpenBSD__)
    254 void
    255 usb_create_event_thread(arg)
    256 	void *arg;
    257 {
    258 	struct usb_softc *sc = arg;
    259 
    260 	if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    261 			   "%s", sc->sc_dev.dv_xname)) {
    262 		printf("%s: unable to create event thread for\n",
    263 		       sc->sc_dev.dv_xname);
    264 		panic("usb_create_event_thread");
    265 	}
    266 }
    267 
    268 void
    269 usb_event_thread(arg)
    270 	void *arg;
    271 {
    272 	struct usb_softc *sc = arg;
    273 
    274 	DPRINTF(("usb_event_thread: start\n"));
    275 
    276 	while (!sc->sc_dying) {
    277 #ifdef USB_DEBUG
    278 		if (usb_noexplore < 2)
    279 #endif
    280 		usb_discover(sc);
    281 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    282 #ifdef USB_DEBUG
    283 			     usb_noexplore ? 0 :
    284 #endif
    285 			     hz*60
    286 			);
    287 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    288 	}
    289 	sc->sc_event_thread = 0;
    290 
    291 	/* In case parent is waiting for us to exit. */
    292 	wakeup(sc);
    293 
    294 	DPRINTF(("usb_event_thread: exit\n"));
    295 	kthread_exit(0);
    296 }
    297 
    298 int
    299 usbctlprint(aux, pnp)
    300 	void *aux;
    301 	const char *pnp;
    302 {
    303 	/* only "usb"es can attach to host controllers */
    304 	if (pnp)
    305 		printf("usb at %s", pnp);
    306 
    307 	return (UNCONF);
    308 }
    309 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    310 
    311 int
    312 usbopen(dev, flag, mode, p)
    313 	dev_t dev;
    314 	int flag, mode;
    315 	struct proc *p;
    316 {
    317 	int unit = minor(dev);
    318 	struct usb_softc *sc;
    319 
    320 	if (unit == USB_DEV_MINOR) {
    321 		if (usb_dev_open)
    322 			return (EBUSY);
    323 		usb_dev_open = 1;
    324 		usb_async_proc = 0;
    325 		return (0);
    326 	}
    327 
    328 	USB_GET_SC_OPEN(usb, unit, sc);
    329 
    330 	if (sc->sc_dying)
    331 		return (EIO);
    332 
    333 	return (0);
    334 }
    335 
    336 int
    337 usbread(dev, uio, flag)
    338 	dev_t dev;
    339 	struct uio *uio;
    340 	int flag;
    341 {
    342 	struct usb_event ue;
    343 	int s, error, n;
    344 
    345 	if (minor(dev) != USB_DEV_MINOR)
    346 		return (ENXIO);
    347 
    348 	if (uio->uio_resid != sizeof(struct usb_event))
    349 		return (EINVAL);
    350 
    351 	error = 0;
    352 	s = splusb();
    353 	for (;;) {
    354 		n = usb_get_next_event(&ue);
    355 		if (n != 0)
    356 			break;
    357 		if (flag & IO_NDELAY) {
    358 			error = EWOULDBLOCK;
    359 			break;
    360 		}
    361 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    362 		if (error)
    363 			break;
    364 	}
    365 	splx(s);
    366 	if (!error)
    367 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    368 
    369 	return (error);
    370 }
    371 
    372 int
    373 usbclose(dev, flag, mode, p)
    374 	dev_t dev;
    375 	int flag, mode;
    376 	struct proc *p;
    377 {
    378 	int unit = minor(dev);
    379 
    380 	if (unit == USB_DEV_MINOR) {
    381 		usb_async_proc = 0;
    382 		usb_dev_open = 0;
    383 	}
    384 
    385 	return (0);
    386 }
    387 
    388 int
    389 usbioctl(devt, cmd, data, flag, p)
    390 	dev_t devt;
    391 	u_long cmd;
    392 	caddr_t data;
    393 	int flag;
    394 	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, events, p)
    522 	dev_t dev;
    523 	int events;
    524 	struct proc *p;
    525 {
    526 	int revents, mask, s;
    527 
    528 	if (minor(dev) == USB_DEV_MINOR) {
    529 		revents = 0;
    530 		mask = POLLIN | POLLRDNORM;
    531 
    532 		s = splusb();
    533 		if (events & mask && usb_nevents > 0)
    534 			revents |= events & mask;
    535 		if (revents == 0 && events & mask)
    536 			selrecord(p, &usb_selevent);
    537 		splx(s);
    538 
    539 		return (revents);
    540 	} else {
    541 #if defined(__FreeBSD__)
    542 		/* This part should be deleted when kthreads is available */
    543 		struct usb_softc *sc;
    544 		int unit = minor(dev);
    545 
    546 		USB_GET_SC(usb, unit, sc);
    547 
    548 		revents = 0;
    549 		mask = POLLOUT | POLLRDNORM;
    550 
    551 		s = splusb();
    552 		if (events & mask && sc->sc_bus->needs_explore)
    553 			revents |= events & mask;
    554 		if (revents == 0 && events & mask)
    555 			selrecord(p, &sc->sc_consel);
    556 		splx(s);
    557 
    558 		return (revents);
    559 #else
    560 		return (ENXIO);
    561 #endif
    562 	}
    563 }
    564 
    565 /* Explore device tree from the root. */
    566 usbd_status
    567 usb_discover(sc)
    568 	struct usb_softc *sc;
    569 {
    570 #if defined(__FreeBSD__)
    571 	/* The splxxx parts should be deleted when kthreads is available */
    572 	int s;
    573 #endif
    574 
    575 	/*
    576 	 * We need mutual exclusion while traversing the device tree,
    577 	 * but this is guaranteed since this function is only called
    578 	 * from the event thread for the controller.
    579 	 */
    580 #if defined(__FreeBSD__)
    581 	s = splusb();
    582 #endif
    583 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    584 		sc->sc_bus->needs_explore = 0;
    585 #if defined(__FreeBSD__)
    586 		splx(s);
    587 #endif
    588 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    589 #if defined(__FreeBSD__)
    590 		s = splusb();
    591 #endif
    592 	}
    593 #if defined(__FreeBSD__)
    594 	splx(s);
    595 #endif
    596 
    597 	return (USBD_NORMAL_COMPLETION);
    598 }
    599 
    600 void
    601 usb_needs_explore(bus)
    602 	usbd_bus_handle bus;
    603 {
    604 	bus->needs_explore = 1;
    605 #if defined(__FreeBSD__)
    606 	/* This part should be deleted when kthreads is available */
    607 	selwakeup(&bus->usbctl->sc_consel);
    608 #endif
    609 	wakeup(&bus->needs_explore);
    610 }
    611 
    612 /* Called at splusb() */
    613 int
    614 usb_get_next_event(ue)
    615 	struct usb_event *ue;
    616 {
    617 	struct usb_event_q *ueq;
    618 
    619 	if (usb_nevents <= 0)
    620 		return (0);
    621 	ueq = SIMPLEQ_FIRST(&usb_events);
    622 	*ue = ueq->ue;
    623 	SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
    624 	free(ueq, M_USBDEV);
    625 	usb_nevents--;
    626 	return (1);
    627 }
    628 
    629 void
    630 usbd_add_event(type, dev)
    631 	int type;
    632 	usbd_device_handle dev;
    633 {
    634 	struct usb_event_q *ueq;
    635 	struct usb_event ue;
    636 	struct timeval thetime;
    637 	int s;
    638 
    639 	s = splusb();
    640 	if (++usb_nevents >= USB_MAX_EVENTS) {
    641 		/* Too many queued events, drop an old one. */
    642 		DPRINTFN(-1,("usb: event dropped\n"));
    643 		(void)usb_get_next_event(&ue);
    644 	}
    645 	/* Don't want to wait here inside splusb() */
    646 	ueq = malloc(sizeof *ueq, M_USBDEV, M_NOWAIT);
    647 	if (ueq == NULL) {
    648 		printf("usb: no memory, event dropped\n");
    649 		splx(s);
    650 		return;
    651 	}
    652 	ueq->ue.ue_type = type;
    653 	ueq->ue.ue_cookie = dev->cookie;
    654 	usbd_fill_deviceinfo(dev, &ueq->ue.ue_device);
    655 	microtime(&thetime);
    656 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    657 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    658 	wakeup(&usb_events);
    659 	selwakeup(&usb_selevent);
    660 	if (usb_async_proc != NULL)
    661 		psignal(usb_async_proc, SIGIO);
    662 	splx(s);
    663 }
    664 
    665 #if defined(__NetBSD__) || defined(__OpenBSD__)
    666 int
    667 usb_activate(self, act)
    668 	device_ptr_t self;
    669 	enum devact act;
    670 {
    671 	struct usb_softc *sc = (struct usb_softc *)self;
    672 	usbd_device_handle dev = sc->sc_port.device;
    673 	int i, rv = 0;
    674 
    675 	switch (act) {
    676 	case DVACT_ACTIVATE:
    677 		return (EOPNOTSUPP);
    678 		break;
    679 
    680 	case DVACT_DEACTIVATE:
    681 		sc->sc_dying = 1;
    682 		if (dev && dev->cdesc && dev->subdevs) {
    683 			for (i = 0; dev->subdevs[i]; i++)
    684 				rv |= config_deactivate(dev->subdevs[i]);
    685 		}
    686 		break;
    687 	}
    688 	return (rv);
    689 }
    690 
    691 int
    692 usb_detach(self, flags)
    693 	device_ptr_t self;
    694 	int flags;
    695 {
    696 	struct usb_softc *sc = (struct usb_softc *)self;
    697 
    698 	DPRINTF(("usb_detach: start\n"));
    699 
    700 	sc->sc_dying = 1;
    701 
    702 	/* Make all devices disconnect. */
    703 	if (sc->sc_port.device)
    704 		usb_disconnect_port(&sc->sc_port, self);
    705 
    706 	/* Kill off event thread. */
    707 	if (sc->sc_event_thread) {
    708 		wakeup(&sc->sc_bus->needs_explore);
    709 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    710 			printf("%s: event thread didn't die\n",
    711 			       USBDEVNAME(sc->sc_dev));
    712 		DPRINTF(("usb_detach: event thread dead\n"));
    713 	}
    714 
    715 	usbd_finish();
    716 	return (0);
    717 }
    718 #elif defined(__FreeBSD__)
    719 int
    720 usb_detach(device_t self)
    721 {
    722 	DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
    723 
    724 	return (EINVAL);
    725 }
    726 #endif
    727 
    728 
    729 #if defined(__FreeBSD__)
    730 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
    731 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
    732 #endif
    733