Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.55
      1 /*	$NetBSD: usb.c,v 1.55 2001/11/13 06:24:56 lukem 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/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.55 2001/11/13 06:24:56 lukem Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/kernel.h>
     52 #include <sys/malloc.h>
     53 #include <sys/device.h>
     54 #include <sys/kthread.h>
     55 #include <sys/proc.h>
     56 #include <sys/conf.h>
     57 #include <sys/poll.h>
     58 #include <sys/select.h>
     59 #include <sys/vnode.h>
     60 #include <sys/signalvar.h>
     61 
     62 #include <dev/usb/usb.h>
     63 #include <dev/usb/usbdi.h>
     64 #include <dev/usb/usbdi_util.h>
     65 
     66 #define USB_DEV_MINOR 255
     67 
     68 #include <machine/bus.h>
     69 
     70 #include <dev/usb/usbdivar.h>
     71 #include <dev/usb/usb_quirks.h>
     72 
     73 #ifdef USB_DEBUG
     74 #define DPRINTF(x)	if (usbdebug) logprintf x
     75 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     76 int	usbdebug = 0;
     77 #ifdef UHCI_DEBUG
     78 int	uhcidebug;
     79 #endif
     80 #ifdef OHCI_DEBUG
     81 int	ohcidebug;
     82 #endif
     83 /*
     84  * 0  - do usual exploration
     85  * 1  - do not use timeout exploration
     86  * >1 - do no exploration
     87  */
     88 int	usb_noexplore = 0;
     89 #else
     90 #define DPRINTF(x)
     91 #define DPRINTFN(n,x)
     92 #endif
     93 
     94 struct usb_softc {
     95 	USBBASEDEVICE	sc_dev;		/* base device */
     96 	usbd_bus_handle sc_bus;		/* USB controller */
     97 	struct usbd_port sc_port;	/* dummy port for root hub */
     98 
     99 	TAILQ_HEAD(, usb_task) sc_tasks;
    100 	struct proc    *sc_event_thread;
    101 
    102 	struct usb_task sc_exp_task;
    103 
    104 	char		sc_dying;
    105 };
    106 
    107 cdev_decl(usb);
    108 
    109 Static void	usb_discover(void *);
    110 Static void	usb_create_event_thread(void *);
    111 Static void	usb_event_thread(void *);
    112 
    113 #define USB_MAX_EVENTS 100
    114 struct usb_event_q {
    115 	struct usb_event ue;
    116 	SIMPLEQ_ENTRY(usb_event_q) next;
    117 };
    118 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    119 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    120 Static int usb_nevents = 0;
    121 Static struct selinfo usb_selevent;
    122 Static struct proc *usb_async_proc;  /* process that wants USB SIGIO */
    123 Static int usb_dev_open = 0;
    124 Static void usb_add_event(int, struct usb_event *);
    125 
    126 Static int usb_get_next_event(struct usb_event *);
    127 
    128 Static const char *usbrev_str[] = USBREV_STR;
    129 
    130 USB_DECLARE_DRIVER(usb);
    131 
    132 USB_MATCH(usb)
    133 {
    134 	DPRINTF(("usbd_match\n"));
    135 	return (UMATCH_GENERIC);
    136 }
    137 
    138 USB_ATTACH(usb)
    139 {
    140 	struct usb_softc *sc = (struct usb_softc *)self;
    141 	usbd_device_handle dev;
    142 	usbd_status err;
    143 	int usbrev;
    144 	struct usb_event ue;
    145 
    146 	DPRINTF(("usbd_attach\n"));
    147 
    148 	usbd_init();
    149 	sc->sc_bus = aux;
    150 	sc->sc_bus->usbctl = sc;
    151 	sc->sc_port.power = USB_MAX_POWER;
    152 	TAILQ_INIT(&sc->sc_tasks);
    153 
    154 	usb_init_task(&sc->sc_exp_task, usb_discover, sc);
    155 
    156 	usbrev = sc->sc_bus->usbrev;
    157 	printf(": USB revision %s", usbrev_str[usbrev]);
    158 	if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
    159 		printf(", not supported\n");
    160 		USB_ATTACH_ERROR_RETURN;
    161 	}
    162 	printf("\n");
    163 
    164 	/* Make sure not to use tsleep() if we are cold booting. */
    165 	if (cold)
    166 		sc->sc_bus->use_polling++;
    167 
    168 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    169 	usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
    170 
    171 #ifdef USB_USE_SOFTINTR
    172 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    173 	/* XXX we should have our own level */
    174 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
    175 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
    176 	if (sc->sc_bus->soft == NULL) {
    177 		printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
    178 		sc->sc_dying = 1;
    179 	}
    180 #else
    181 	callout_init(&sc->sc_bus->softi);
    182 #endif
    183 #endif
    184 
    185 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
    186 		  &sc->sc_port);
    187 	if (!err) {
    188 		dev = sc->sc_port.device;
    189 		if (dev->hub == NULL) {
    190 			sc->sc_dying = 1;
    191 			printf("%s: root device is not a hub\n",
    192 			       USBDEVNAME(sc->sc_dev));
    193 			USB_ATTACH_ERROR_RETURN;
    194 		}
    195 		sc->sc_bus->root_hub = dev;
    196 #if 1
    197 		/*
    198 		 * Turning this code off will delay attachment of USB devices
    199 		 * until the USB event thread is running, which means that
    200 		 * the keyboard will not work until after cold boot.
    201 		 */
    202 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
    203 			dev->hub->explore(sc->sc_bus->root_hub);
    204 #endif
    205 	} else {
    206 		printf("%s: root hub problem, error=%d\n",
    207 		       USBDEVNAME(sc->sc_dev), err);
    208 		sc->sc_dying = 1;
    209 	}
    210 	if (cold)
    211 		sc->sc_bus->use_polling--;
    212 
    213 	config_pending_incr();
    214 	usb_kthread_create(usb_create_event_thread, sc);
    215 
    216 	USB_ATTACH_SUCCESS_RETURN;
    217 }
    218 
    219 #if defined(__NetBSD__) || defined(__OpenBSD__)
    220 void
    221 usb_create_event_thread(void *arg)
    222 {
    223 	struct usb_softc *sc = arg;
    224 
    225 	if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    226 			   "%s", sc->sc_dev.dv_xname)) {
    227 		printf("%s: unable to create event thread for\n",
    228 		       sc->sc_dev.dv_xname);
    229 		panic("usb_create_event_thread");
    230 	}
    231 }
    232 
    233 /*
    234  * Add a task to be performed by the event thread.  This function can be
    235  * called from any context and the task will be executed in a process
    236  * context ASAP.
    237  */
    238 void
    239 usb_add_task(usbd_device_handle dev, struct usb_task *task)
    240 {
    241 	struct usb_softc *sc = dev->bus->usbctl;
    242 	int s;
    243 
    244 	s = splusb();
    245 	if (!task->onqueue) {
    246 		DPRINTFN(2,("usb_add_task: sc=%p task=%p\n", sc, task));
    247 		TAILQ_INSERT_TAIL(&sc->sc_tasks, task, next);
    248 		task->onqueue = 1;
    249 	} else
    250 		DPRINTFN(3,("usb_add_task: sc=%p task=%p on q\n", sc, task));
    251 	wakeup(&sc->sc_tasks);
    252 	splx(s);
    253 }
    254 
    255 void
    256 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
    257 {
    258 	struct usb_softc *sc = dev->bus->usbctl;
    259 	int s;
    260 
    261 	s = splusb();
    262 	if (task->onqueue) {
    263 		TAILQ_REMOVE(&sc->sc_tasks, task, next);
    264 		task->onqueue = 0;
    265 	}
    266 	splx(s);
    267 }
    268 
    269 void
    270 usb_event_thread(void *arg)
    271 {
    272 	struct usb_softc *sc = arg;
    273 	struct usb_task *task;
    274 	int s;
    275 
    276 	DPRINTF(("usb_event_thread: start\n"));
    277 
    278 	/* Make sure first discover does something. */
    279 	sc->sc_bus->needs_explore = 1;
    280 	usb_discover(sc);
    281 	config_pending_decr();
    282 
    283 	while (!sc->sc_dying) {
    284 		s = splusb();
    285 		task = TAILQ_FIRST(&sc->sc_tasks);
    286 		if (task == NULL) {
    287 			tsleep(&sc->sc_tasks, PWAIT, "usbevt", 0);
    288 			task = TAILQ_FIRST(&sc->sc_tasks);
    289 		}
    290 		DPRINTFN(2,("usb_event_thread: woke up task=%p\n", task));
    291 		if (task != NULL && !sc->sc_dying) {
    292 			TAILQ_REMOVE(&sc->sc_tasks, task, next);
    293 			task->onqueue = 0;
    294 			splx(s);
    295 			task->fun(task->arg);
    296 		} else
    297 			splx(s);
    298 	}
    299 	sc->sc_event_thread = NULL;
    300 
    301 	/* In case parent is waiting for us to exit. */
    302 	wakeup(sc);
    303 
    304 	DPRINTF(("usb_event_thread: exit\n"));
    305 	kthread_exit(0);
    306 }
    307 
    308 int
    309 usbctlprint(void *aux, const char *pnp)
    310 {
    311 	/* only "usb"es can attach to host controllers */
    312 	if (pnp)
    313 		printf("usb at %s", pnp);
    314 
    315 	return (UNCONF);
    316 }
    317 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    318 
    319 int
    320 usbopen(dev_t dev, int flag, int mode, struct proc *p)
    321 {
    322 	int unit = minor(dev);
    323 	struct usb_softc *sc;
    324 
    325 	if (unit == USB_DEV_MINOR) {
    326 		if (usb_dev_open)
    327 			return (EBUSY);
    328 		usb_dev_open = 1;
    329 		usb_async_proc = 0;
    330 		return (0);
    331 	}
    332 
    333 	USB_GET_SC_OPEN(usb, unit, sc);
    334 
    335 	if (sc->sc_dying)
    336 		return (EIO);
    337 
    338 	return (0);
    339 }
    340 
    341 int
    342 usbread(dev_t dev, struct uio *uio, int flag)
    343 {
    344 	struct usb_event ue;
    345 	int s, error, n;
    346 
    347 	if (minor(dev) != USB_DEV_MINOR)
    348 		return (ENXIO);
    349 
    350 	if (uio->uio_resid != sizeof(struct usb_event))
    351 		return (EINVAL);
    352 
    353 	error = 0;
    354 	s = splusb();
    355 	for (;;) {
    356 		n = usb_get_next_event(&ue);
    357 		if (n != 0)
    358 			break;
    359 		if (flag & IO_NDELAY) {
    360 			error = EWOULDBLOCK;
    361 			break;
    362 		}
    363 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    364 		if (error)
    365 			break;
    366 	}
    367 	splx(s);
    368 	if (!error)
    369 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    370 
    371 	return (error);
    372 }
    373 
    374 int
    375 usbclose(dev_t dev, int flag, int mode, struct proc *p)
    376 {
    377 	int unit = minor(dev);
    378 
    379 	if (unit == USB_DEV_MINOR) {
    380 		usb_async_proc = 0;
    381 		usb_dev_open = 0;
    382 	}
    383 
    384 	return (0);
    385 }
    386 
    387 int
    388 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
    389 {
    390 	struct usb_softc *sc;
    391 	int unit = minor(devt);
    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(dev_t dev, int events, struct proc *p)
    510 {
    511 	int revents, mask, s;
    512 
    513 	if (minor(dev) == 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 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
    628 #ifdef USB_USE_SOFTINTR
    629 	if (bus->use_polling) {
    630 		bus->methods->soft_intr(bus);
    631 	} else {
    632 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    633 		softintr_schedule(bus->soft);
    634 #else
    635 		if (!callout_pending(&bus->softi))
    636 			callout_reset(&bus->softi, 0, bus->methods->soft_intr,
    637 			    bus);
    638 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
    639 	}
    640 #else
    641 	bus->methods->soft_intr(bus);
    642 #endif
    643 }
    644 
    645 int
    646 usb_activate(device_ptr_t self, enum devact act)
    647 {
    648 	struct usb_softc *sc = (struct usb_softc *)self;
    649 	usbd_device_handle dev = sc->sc_port.device;
    650 	int i, rv = 0;
    651 
    652 	switch (act) {
    653 	case DVACT_ACTIVATE:
    654 		return (EOPNOTSUPP);
    655 		break;
    656 
    657 	case DVACT_DEACTIVATE:
    658 		sc->sc_dying = 1;
    659 		if (dev && dev->cdesc && dev->subdevs) {
    660 			for (i = 0; dev->subdevs[i]; i++)
    661 				rv |= config_deactivate(dev->subdevs[i]);
    662 		}
    663 		break;
    664 	}
    665 	return (rv);
    666 }
    667 
    668 int
    669 usb_detach(device_ptr_t self, int flags)
    670 {
    671 	struct usb_softc *sc = (struct usb_softc *)self;
    672 	struct usb_event ue;
    673 
    674 	DPRINTF(("usb_detach: start\n"));
    675 
    676 	sc->sc_dying = 1;
    677 
    678 	/* Make all devices disconnect. */
    679 	if (sc->sc_port.device)
    680 		usb_disconnect_port(&sc->sc_port, self);
    681 
    682 	/* Kill off event thread. */
    683 	if (sc->sc_event_thread) {
    684 		wakeup(&sc->sc_tasks);
    685 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    686 			printf("%s: event thread didn't die\n",
    687 			       USBDEVNAME(sc->sc_dev));
    688 		DPRINTF(("usb_detach: event thread dead\n"));
    689 	}
    690 
    691 	usbd_finish();
    692 
    693 #ifdef USB_USE_SOFTINTR
    694 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    695 	if (sc->sc_bus->soft != NULL) {
    696 		softintr_disestablish(sc->sc_bus->soft);
    697 		sc->sc_bus->soft = NULL;
    698 	}
    699 #else
    700 	callout_stop(&sc->sc_bus->softi);
    701 #endif
    702 #endif
    703 
    704 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    705 	usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
    706 
    707 	return (0);
    708 }
    709