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