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