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