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