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