Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.49
      1 /*	$NetBSD: usb.c,v 1.49 2001/01/21 02:39:53 augustss Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/usb.c,v 1.20 1999/11/17 22:33:46 n_hibma Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * USB specifications and other documentation can be found at
     43  * http://www.usb.org/developers/data/ and
     44  * http://www.usb.org/developers/index.html .
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/malloc.h>
     51 #if defined(__NetBSD__) || defined(__OpenBSD__)
     52 #include <sys/device.h>
     53 #include <sys/kthread.h>
     54 #include <sys/proc.h>
     55 #elif defined(__FreeBSD__)
     56 #include <sys/module.h>
     57 #include <sys/bus.h>
     58 #include <sys/filio.h>
     59 #include <sys/uio.h>
     60 #endif
     61 #include <sys/conf.h>
     62 #include <sys/poll.h>
     63 #include <sys/select.h>
     64 #include <sys/vnode.h>
     65 #include <sys/signalvar.h>
     66 
     67 #include <dev/usb/usb.h>
     68 #include <dev/usb/usbdi.h>
     69 #include <dev/usb/usbdi_util.h>
     70 
     71 #define USB_DEV_MINOR 255
     72 
     73 #if defined(__FreeBSD__)
     74 MALLOC_DEFINE(M_USB, "USB", "USB");
     75 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
     76 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
     77 
     78 #include "usb_if.h"
     79 #endif /* defined(__FreeBSD__) */
     80 
     81 #include <machine/bus.h>
     82 
     83 #include <dev/usb/usbdivar.h>
     84 #include <dev/usb/usb_quirks.h>
     85 
     86 #ifdef USB_DEBUG
     87 #define DPRINTF(x)	if (usbdebug) logprintf x
     88 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     89 int	usbdebug = 0;
     90 #ifdef UHCI_DEBUG
     91 int	uhcidebug;
     92 #endif
     93 #ifdef OHCI_DEBUG
     94 int	ohcidebug;
     95 #endif
     96 /*
     97  * 0  - do usual exploration
     98  * 1  - do not use timeout exploration
     99  * >1 - do no exploration
    100  */
    101 int	usb_noexplore = 0;
    102 #else
    103 #define DPRINTF(x)
    104 #define DPRINTFN(n,x)
    105 #endif
    106 
    107 struct usb_softc {
    108 	USBBASEDEVICE	sc_dev;		/* base device */
    109 	usbd_bus_handle sc_bus;		/* USB controller */
    110 	struct usbd_port sc_port;	/* dummy port for root hub */
    111 
    112 #if defined(__FreeBSD__)
    113 	/* This part should be deleted when kthreads is available */
    114 	struct selinfo	sc_consel;	/* waiting for connect change */
    115 #else
    116 	struct proc    *sc_event_thread;
    117 #endif
    118 
    119 	char		sc_dying;
    120 };
    121 
    122 #if defined(__NetBSD__) || defined(__OpenBSD__)
    123 cdev_decl(usb);
    124 #elif defined(__FreeBSD__)
    125 d_open_t  usbopen;
    126 d_close_t usbclose;
    127 d_read_t usbread;
    128 d_ioctl_t usbioctl;
    129 int usbpoll(dev_t, int, struct proc *);
    130 
    131 struct cdevsw usb_cdevsw = {
    132 	/* open */      usbopen,
    133 	/* close */     usbclose,
    134 	/* read */      noread,
    135 	/* write */     nowrite,
    136 	/* ioctl */     usbioctl,
    137 	/* poll */      usbpoll,
    138 	/* mmap */      nommap,
    139 	/* strategy */  nostrategy,
    140 	/* name */      "usb",
    141 	/* maj */       USB_CDEV_MAJOR,
    142 	/* dump */      nodump,
    143 	/* psize */     nopsize,
    144 	/* flags */     0,
    145 	/* bmaj */      -1
    146 };
    147 #endif
    148 
    149 Static usbd_status usb_discover(struct usb_softc *);
    150 Static void	usb_create_event_thread(void *);
    151 Static void	usb_event_thread(void *);
    152 
    153 #define USB_MAX_EVENTS 100
    154 struct usb_event_q {
    155 	struct usb_event ue;
    156 	SIMPLEQ_ENTRY(usb_event_q) next;
    157 };
    158 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    159 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    160 Static int usb_nevents = 0;
    161 Static struct selinfo usb_selevent;
    162 Static struct proc *usb_async_proc;  /* process that wants USB SIGIO */
    163 Static int usb_dev_open = 0;
    164 Static void usb_add_event(int, struct usb_event *);
    165 
    166 Static int usb_get_next_event(struct usb_event *);
    167 
    168 Static const char *usbrev_str[] = USBREV_STR;
    169 
    170 USB_DECLARE_DRIVER(usb);
    171 
    172 USB_MATCH(usb)
    173 {
    174 	DPRINTF(("usbd_match\n"));
    175 	return (UMATCH_GENERIC);
    176 }
    177 
    178 USB_ATTACH(usb)
    179 {
    180 #if defined(__NetBSD__) || defined(__OpenBSD__)
    181 	struct usb_softc *sc = (struct usb_softc *)self;
    182 #elif defined(__FreeBSD__)
    183 	struct usb_softc *sc = device_get_softc(self);
    184 	void *aux = device_get_ivars(self);
    185 #endif
    186 	usbd_device_handle dev;
    187 	usbd_status err;
    188 	int usbrev;
    189 	struct usb_event ue;
    190 
    191 #if defined(__FreeBSD__)
    192 	printf("%s", USBDEVNAME(sc->sc_dev));
    193 	sc->sc_dev = self;
    194 #endif
    195 
    196 	DPRINTF(("usbd_attach\n"));
    197 
    198 	usbd_init();
    199 	sc->sc_bus = aux;
    200 	sc->sc_bus->usbctl = sc;
    201 	sc->sc_port.power = USB_MAX_POWER;
    202 
    203 	usbrev = sc->sc_bus->usbrev;
    204 	printf(": USB revision %s", usbrev_str[usbrev]);
    205 	if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
    206 		printf(", not supported\n");
    207 		USB_ATTACH_ERROR_RETURN;
    208 	}
    209 	printf("\n");
    210 
    211 	/* Make sure not to use tsleep() if we are cold booting. */
    212 	if (cold)
    213 		sc->sc_bus->use_polling++;
    214 
    215 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    216 	usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
    217 
    218 #ifdef USB_USE_SOFTINTR
    219 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    220 	/* XXX we should have our own level */
    221 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
    222 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
    223 	if (sc->sc_bus->soft == NULL) {
    224 		printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
    225 		sc->sc_dying = 1;
    226 	}
    227 #else
    228 	callout_init(&sc->sc_bus->softi);
    229 #endif
    230 #endif
    231 
    232 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
    233 		  &sc->sc_port);
    234 	if (!err) {
    235 		dev = sc->sc_port.device;
    236 		if (dev->hub == NULL) {
    237 			sc->sc_dying = 1;
    238 			printf("%s: root device is not a hub\n",
    239 			       USBDEVNAME(sc->sc_dev));
    240 			USB_ATTACH_ERROR_RETURN;
    241 		}
    242 		sc->sc_bus->root_hub = dev;
    243 #if 1
    244 		/*
    245 		 * Turning this code off will delay attachment of USB devices
    246 		 * until the USB event thread is running, which means that
    247 		 * the keyboard will not work until after cold boot.
    248 		 */
    249 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
    250 			dev->hub->explore(sc->sc_bus->root_hub);
    251 #endif
    252 	} else {
    253 		printf("%s: root hub problem, error=%d\n",
    254 		       USBDEVNAME(sc->sc_dev), err);
    255 		sc->sc_dying = 1;
    256 	}
    257 	if (cold)
    258 		sc->sc_bus->use_polling--;
    259 
    260 	config_pending_incr();
    261 	usb_kthread_create(usb_create_event_thread, sc);
    262 
    263 #if defined(__FreeBSD__)
    264 	make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
    265 		 0644, "usb%d", device_get_unit(self));
    266 #endif
    267 
    268 	USB_ATTACH_SUCCESS_RETURN;
    269 }
    270 
    271 #if defined(__NetBSD__) || defined(__OpenBSD__)
    272 void
    273 usb_create_event_thread(void *arg)
    274 {
    275 	struct usb_softc *sc = arg;
    276 
    277 	if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    278 			   "%s", sc->sc_dev.dv_xname)) {
    279 		printf("%s: unable to create event thread for\n",
    280 		       sc->sc_dev.dv_xname);
    281 		panic("usb_create_event_thread");
    282 	}
    283 }
    284 
    285 void
    286 usb_event_thread(void *arg)
    287 {
    288 	struct usb_softc *sc = arg;
    289 	int first = 1;
    290 
    291 	DPRINTF(("usb_event_thread: start\n"));
    292 
    293 	/* Make sure first discover does something. */
    294 	sc->sc_bus->needs_explore = 1;
    295 
    296 	while (!sc->sc_dying) {
    297 #ifdef USB_DEBUG
    298 		if (usb_noexplore < 2)
    299 #endif
    300 		usb_discover(sc);
    301 		if (first) {
    302 			config_pending_decr();
    303 			first = 0;
    304 		}
    305 #ifdef USB_DEBUG
    306 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    307 		    usb_noexplore ? 0 : hz * 60);
    308 #else
    309 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    310 		    hz * 60);
    311 #endif
    312 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    313 	}
    314 	sc->sc_event_thread = 0;
    315 
    316 	/* In case parent is waiting for us to exit. */
    317 	wakeup(sc);
    318 
    319 	DPRINTF(("usb_event_thread: exit\n"));
    320 	kthread_exit(0);
    321 }
    322 
    323 int
    324 usbctlprint(void *aux, const char *pnp)
    325 {
    326 	/* only "usb"es can attach to host controllers */
    327 	if (pnp)
    328 		printf("usb at %s", pnp);
    329 
    330 	return (UNCONF);
    331 }
    332 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    333 
    334 int
    335 usbopen(dev_t dev, int flag, int mode, struct proc *p)
    336 {
    337 	int unit = minor(dev);
    338 	struct usb_softc *sc;
    339 
    340 	if (unit == USB_DEV_MINOR) {
    341 		if (usb_dev_open)
    342 			return (EBUSY);
    343 		usb_dev_open = 1;
    344 		usb_async_proc = 0;
    345 		return (0);
    346 	}
    347 
    348 	USB_GET_SC_OPEN(usb, unit, sc);
    349 
    350 	if (sc->sc_dying)
    351 		return (EIO);
    352 
    353 	return (0);
    354 }
    355 
    356 int
    357 usbread(dev_t dev, struct uio *uio, int flag)
    358 {
    359 	struct usb_event ue;
    360 	int s, error, n;
    361 
    362 	if (minor(dev) != USB_DEV_MINOR)
    363 		return (ENXIO);
    364 
    365 	if (uio->uio_resid != sizeof(struct usb_event))
    366 		return (EINVAL);
    367 
    368 	error = 0;
    369 	s = splusb();
    370 	for (;;) {
    371 		n = usb_get_next_event(&ue);
    372 		if (n != 0)
    373 			break;
    374 		if (flag & IO_NDELAY) {
    375 			error = EWOULDBLOCK;
    376 			break;
    377 		}
    378 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    379 		if (error)
    380 			break;
    381 	}
    382 	splx(s);
    383 	if (!error)
    384 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    385 
    386 	return (error);
    387 }
    388 
    389 int
    390 usbclose(dev_t dev, int flag, int mode, struct proc *p)
    391 {
    392 	int unit = minor(dev);
    393 
    394 	if (unit == USB_DEV_MINOR) {
    395 		usb_async_proc = 0;
    396 		usb_dev_open = 0;
    397 	}
    398 
    399 	return (0);
    400 }
    401 
    402 int
    403 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
    404 {
    405 	struct usb_softc *sc;
    406 	int unit = minor(devt);
    407 
    408 	if (unit == USB_DEV_MINOR) {
    409 		switch (cmd) {
    410 		case FIONBIO:
    411 			/* All handled in the upper FS layer. */
    412 			return (0);
    413 
    414 		case FIOASYNC:
    415 			if (*(int *)data)
    416 				usb_async_proc = p;
    417 			else
    418 				usb_async_proc = 0;
    419 			return (0);
    420 
    421 		default:
    422 			return (EINVAL);
    423 		}
    424 	}
    425 
    426 	USB_GET_SC(usb, unit, sc);
    427 
    428 	if (sc->sc_dying)
    429 		return (EIO);
    430 
    431 	switch (cmd) {
    432 #if defined(__FreeBSD__)
    433 	/* This part should be deleted when kthreads is available */
    434   	case USB_DISCOVER:
    435   		usb_discover(sc);
    436   		break;
    437 #endif
    438 #ifdef USB_DEBUG
    439 	case USB_SETDEBUG:
    440 		usbdebug  = ((*(int *)data) & 0x000000ff);
    441 #ifdef UHCI_DEBUG
    442 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    443 #endif
    444 #ifdef OHCI_DEBUG
    445 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    446 #endif
    447 		break;
    448 #endif
    449 	case USB_REQUEST:
    450 	{
    451 		struct usb_ctl_request *ur = (void *)data;
    452 		int len = UGETW(ur->request.wLength);
    453 		struct iovec iov;
    454 		struct uio uio;
    455 		void *ptr = 0;
    456 		int addr = ur->addr;
    457 		usbd_status err;
    458 		int error = 0;
    459 
    460 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    461 		if (len < 0 || len > 32768)
    462 			return (EINVAL);
    463 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    464 		    sc->sc_bus->devices[addr] == 0)
    465 			return (EINVAL);
    466 		if (len != 0) {
    467 			iov.iov_base = (caddr_t)ur->data;
    468 			iov.iov_len = len;
    469 			uio.uio_iov = &iov;
    470 			uio.uio_iovcnt = 1;
    471 			uio.uio_resid = len;
    472 			uio.uio_offset = 0;
    473 			uio.uio_segflg = UIO_USERSPACE;
    474 			uio.uio_rw =
    475 				ur->request.bmRequestType & UT_READ ?
    476 				UIO_READ : UIO_WRITE;
    477 			uio.uio_procp = p;
    478 			ptr = malloc(len, M_TEMP, M_WAITOK);
    479 			if (uio.uio_rw == UIO_WRITE) {
    480 				error = uiomove(ptr, len, &uio);
    481 				if (error)
    482 					goto ret;
    483 			}
    484 		}
    485 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    486 			  &ur->request, ptr, ur->flags, &ur->actlen);
    487 		if (err) {
    488 			error = EIO;
    489 			goto ret;
    490 		}
    491 		if (len != 0) {
    492 			if (uio.uio_rw == UIO_READ) {
    493 				error = uiomove(ptr, len, &uio);
    494 				if (error)
    495 					goto ret;
    496 			}
    497 		}
    498 	ret:
    499 		if (ptr)
    500 			free(ptr, M_TEMP);
    501 		return (error);
    502 	}
    503 
    504 	case USB_DEVICEINFO:
    505 	{
    506 		struct usb_device_info *di = (void *)data;
    507 		int addr = di->addr;
    508 		usbd_device_handle dev;
    509 
    510 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    511 			return (EINVAL);
    512 		dev = sc->sc_bus->devices[addr];
    513 		if (dev == NULL)
    514 			return (ENXIO);
    515 		usbd_fill_deviceinfo(dev, di, 1);
    516 		break;
    517 	}
    518 
    519 	case USB_DEVICESTATS:
    520 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    521 		break;
    522 
    523 	default:
    524 		return (EINVAL);
    525 	}
    526 	return (0);
    527 }
    528 
    529 int
    530 usbpoll(dev_t dev, int events, struct proc *p)
    531 {
    532 	int revents, mask, s;
    533 
    534 	if (minor(dev) == USB_DEV_MINOR) {
    535 		revents = 0;
    536 		mask = POLLIN | POLLRDNORM;
    537 
    538 		s = splusb();
    539 		if (events & mask && usb_nevents > 0)
    540 			revents |= events & mask;
    541 		if (revents == 0 && events & mask)
    542 			selrecord(p, &usb_selevent);
    543 		splx(s);
    544 
    545 		return (revents);
    546 	} else {
    547 #if defined(__FreeBSD__)
    548 		/* This part should be deleted when kthreads is available */
    549 		struct usb_softc *sc;
    550 		int unit = minor(dev);
    551 
    552 		USB_GET_SC(usb, unit, sc);
    553 
    554 		revents = 0;
    555 		mask = POLLOUT | POLLRDNORM;
    556 
    557 		s = splusb();
    558 		if (events & mask && sc->sc_bus->needs_explore)
    559 			revents |= events & mask;
    560 		if (revents == 0 && events & mask)
    561 			selrecord(p, &sc->sc_consel);
    562 		splx(s);
    563 
    564 		return (revents);
    565 #else
    566 		return (ENXIO);
    567 #endif
    568 	}
    569 }
    570 
    571 /* Explore device tree from the root. */
    572 usbd_status
    573 usb_discover(struct usb_softc *sc)
    574 {
    575 #if defined(__FreeBSD__)
    576 	/* The splxxx parts should be deleted when kthreads is available */
    577 	int s;
    578 #endif
    579 
    580 	/*
    581 	 * We need mutual exclusion while traversing the device tree,
    582 	 * but this is guaranteed since this function is only called
    583 	 * from the event thread for the controller.
    584 	 */
    585 #if defined(__FreeBSD__)
    586 	s = splusb();
    587 #endif
    588 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    589 		sc->sc_bus->needs_explore = 0;
    590 #if defined(__FreeBSD__)
    591 		splx(s);
    592 #endif
    593 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    594 #if defined(__FreeBSD__)
    595 		s = splusb();
    596 #endif
    597 	}
    598 #if defined(__FreeBSD__)
    599 	splx(s);
    600 #endif
    601 
    602 	return (USBD_NORMAL_COMPLETION);
    603 }
    604 
    605 void
    606 usb_needs_explore(usbd_bus_handle bus)
    607 {
    608 	bus->needs_explore = 1;
    609 #if defined(__FreeBSD__)
    610 	/* This part should be deleted when kthreads is available */
    611 	selwakeup(&bus->usbctl->sc_consel);
    612 #endif
    613 	wakeup(&bus->needs_explore);
    614 }
    615 
    616 /* Called at splusb() */
    617 int
    618 usb_get_next_event(struct usb_event *ue)
    619 {
    620 	struct usb_event_q *ueq;
    621 
    622 	if (usb_nevents <= 0)
    623 		return (0);
    624 	ueq = SIMPLEQ_FIRST(&usb_events);
    625 	*ue = ueq->ue;
    626 	SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
    627 	free(ueq, M_USBDEV);
    628 	usb_nevents--;
    629 	return (1);
    630 }
    631 
    632 void
    633 usbd_add_dev_event(int type, usbd_device_handle udev)
    634 {
    635 	struct usb_event ue;
    636 
    637 	usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
    638 	usb_add_event(type, &ue);
    639 }
    640 
    641 void
    642 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
    643 {
    644 	struct usb_event ue;
    645 
    646 	ue.u.ue_driver.ue_cookie = udev->cookie;
    647 	strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
    648 	    sizeof ue.u.ue_driver.ue_devname);
    649 	usb_add_event(type, &ue);
    650 }
    651 
    652 Static void
    653 usb_add_event(int type, struct usb_event *uep)
    654 {
    655 	struct usb_event_q *ueq;
    656 	struct usb_event ue;
    657 	struct timeval thetime;
    658 	int s;
    659 
    660 	microtime(&thetime);
    661 	/* Don't want to wait here inside splusb() */
    662 	ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
    663 	ueq->ue = *uep;
    664 	ueq->ue.ue_type = type;
    665 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    666 
    667 	s = splusb();
    668 	if (++usb_nevents >= USB_MAX_EVENTS) {
    669 		/* Too many queued events, drop an old one. */
    670 		DPRINTFN(-1,("usb: event dropped\n"));
    671 		(void)usb_get_next_event(&ue);
    672 	}
    673 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    674 	wakeup(&usb_events);
    675 	selwakeup(&usb_selevent);
    676 	if (usb_async_proc != NULL)
    677 		psignal(usb_async_proc, SIGIO);
    678 	splx(s);
    679 }
    680 void
    681 usb_schedsoftintr(usbd_bus_handle bus)
    682 {
    683 #ifdef USB_USE_SOFTINTR
    684 	if (bus->use_polling) {
    685 		bus->methods->soft_intr(bus);
    686 	} else {
    687 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    688 		softintr_schedule(bus->soft);
    689 #else
    690 		if (!callout_pending(&bus->softi))
    691 			callout_reset(&bus->softi, 0, bus->methods->soft_intr,
    692 			    bus);
    693 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
    694 	}
    695 #else
    696 	bus->methods->soft_intr(bus);
    697 #endif
    698 }
    699 
    700 #if defined(__NetBSD__) || defined(__OpenBSD__)
    701 int
    702 usb_activate(device_ptr_t self, enum devact act)
    703 {
    704 	struct usb_softc *sc = (struct usb_softc *)self;
    705 	usbd_device_handle dev = sc->sc_port.device;
    706 	int i, rv = 0;
    707 
    708 	switch (act) {
    709 	case DVACT_ACTIVATE:
    710 		return (EOPNOTSUPP);
    711 		break;
    712 
    713 	case DVACT_DEACTIVATE:
    714 		sc->sc_dying = 1;
    715 		if (dev && dev->cdesc && dev->subdevs) {
    716 			for (i = 0; dev->subdevs[i]; i++)
    717 				rv |= config_deactivate(dev->subdevs[i]);
    718 		}
    719 		break;
    720 	}
    721 	return (rv);
    722 }
    723 
    724 int
    725 usb_detach(device_ptr_t self, int flags)
    726 {
    727 	struct usb_softc *sc = (struct usb_softc *)self;
    728 	struct usb_event ue;
    729 
    730 	DPRINTF(("usb_detach: start\n"));
    731 
    732 	sc->sc_dying = 1;
    733 
    734 	/* Make all devices disconnect. */
    735 	if (sc->sc_port.device)
    736 		usb_disconnect_port(&sc->sc_port, self);
    737 
    738 	/* Kill off event thread. */
    739 	if (sc->sc_event_thread) {
    740 		wakeup(&sc->sc_bus->needs_explore);
    741 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    742 			printf("%s: event thread didn't die\n",
    743 			       USBDEVNAME(sc->sc_dev));
    744 		DPRINTF(("usb_detach: event thread dead\n"));
    745 	}
    746 
    747 	usbd_finish();
    748 
    749 #ifdef USB_USE_SOFTINTR
    750 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    751 	if (sc->sc_bus->soft != NULL) {
    752 		softintr_disestablish(sc->sc_bus->soft);
    753 		sc->sc_bus->soft = NULL;
    754 	}
    755 #else
    756 	callout_stop(&sc->sc_bus->softi);
    757 #endif
    758 #endif
    759 
    760 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    761 	usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
    762 
    763 	return (0);
    764 }
    765 #elif defined(__FreeBSD__)
    766 int
    767 usb_detach(device_t self)
    768 {
    769 	DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
    770 
    771 	return (EINVAL);
    772 }
    773 #endif
    774 
    775 
    776 #if defined(__FreeBSD__)
    777 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
    778 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
    779 #endif
    780