Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.96
      1 /*	$NetBSD: usb.c,v 1.96 2007/03/04 06:02:50 christos 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/docs/ and
     43  * http://www.usb.org/developers/devclass_docs/
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.96 2007/03/04 06:02:50 christos Exp $");
     48 
     49 #include "opt_compat_netbsd.h"
     50 
     51 #include "ohci.h"
     52 #include "uhci.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/malloc.h>
     58 #include <sys/device.h>
     59 #include <sys/kthread.h>
     60 #include <sys/proc.h>
     61 #include <sys/conf.h>
     62 #include <sys/fcntl.h>
     63 #include <sys/poll.h>
     64 #include <sys/select.h>
     65 #include <sys/vnode.h>
     66 #include <sys/signalvar.h>
     67 
     68 #include <dev/usb/usb.h>
     69 #include <dev/usb/usbdi.h>
     70 #include <dev/usb/usbdi_util.h>
     71 
     72 #define USB_DEV_MINOR 255
     73 
     74 #include <machine/bus.h>
     75 
     76 #include <dev/usb/usbdivar.h>
     77 #include <dev/usb/usb_quirks.h>
     78 
     79 #ifdef USB_DEBUG
     80 #define DPRINTF(x)	if (usbdebug) logprintf x
     81 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     82 int	usbdebug = 0;
     83 #if defined(UHCI_DEBUG) && NUHCI > 0
     84 extern int	uhcidebug;
     85 #endif
     86 #if defined(OHCI_DEBUG) && NOHCI > 0
     87 extern int	ohcidebug;
     88 #endif
     89 /*
     90  * 0  - do usual exploration
     91  * 1  - do not use timeout exploration
     92  * >1 - do no exploration
     93  */
     94 int	usb_noexplore = 0;
     95 #else
     96 #define DPRINTF(x)
     97 #define DPRINTFN(n,x)
     98 #endif
     99 
    100 struct usb_softc {
    101 	USBBASEDEVICE	sc_dev;		/* base device */
    102 	usbd_bus_handle sc_bus;		/* USB controller */
    103 	struct usbd_port sc_port;	/* dummy port for root hub */
    104 
    105 	struct proc	*sc_event_thread;
    106 
    107 	char		sc_dying;
    108 };
    109 
    110 struct usb_taskq {
    111 	TAILQ_HEAD(, usb_task) tasks;
    112 	struct proc *task_thread_proc;
    113 	const char *name;
    114 	int taskcreated;	/* task thread exists. */
    115 };
    116 
    117 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
    118 
    119 dev_type_open(usbopen);
    120 dev_type_close(usbclose);
    121 dev_type_read(usbread);
    122 dev_type_ioctl(usbioctl);
    123 dev_type_poll(usbpoll);
    124 dev_type_kqfilter(usbkqfilter);
    125 
    126 const struct cdevsw usb_cdevsw = {
    127 	usbopen, usbclose, usbread, nowrite, usbioctl,
    128 	nostop, notty, usbpoll, nommap, usbkqfilter, D_OTHER,
    129 };
    130 
    131 Static void	usb_discover(void *);
    132 Static void	usb_create_event_thread(void *);
    133 Static void	usb_event_thread(void *);
    134 Static void	usb_task_thread(void *);
    135 
    136 #define USB_MAX_EVENTS 100
    137 struct usb_event_q {
    138 	struct usb_event ue;
    139 	SIMPLEQ_ENTRY(usb_event_q) next;
    140 };
    141 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    142 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    143 Static int usb_nevents = 0;
    144 Static struct selinfo usb_selevent;
    145 Static usb_proc_ptr usb_async_proc;  /* process that wants USB SIGIO */
    146 Static int usb_dev_open = 0;
    147 Static struct usb_event *usb_alloc_event(void);
    148 Static void usb_free_event(struct usb_event *);
    149 Static void usb_add_event(int, struct usb_event *);
    150 
    151 Static int usb_get_next_event(struct usb_event *);
    152 
    153 #ifdef COMPAT_30
    154 Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
    155 #endif
    156 
    157 Static const char *usbrev_str[] = USBREV_STR;
    158 
    159 USB_DECLARE_DRIVER(usb);
    160 
    161 USB_MATCH(usb)
    162 {
    163 	DPRINTF(("usbd_match\n"));
    164 	return (UMATCH_GENERIC);
    165 }
    166 
    167 USB_ATTACH(usb)
    168 {
    169 	struct usb_softc *sc = (struct usb_softc *)self;
    170 	usbd_device_handle dev;
    171 	usbd_status err;
    172 	int usbrev;
    173 	int speed;
    174 	struct usb_event *ue;
    175 
    176 	DPRINTF(("usbd_attach\n"));
    177 
    178 	sc->sc_bus = aux;
    179 	sc->sc_bus->usbctl = sc;
    180 	sc->sc_port.power = USB_MAX_POWER;
    181 
    182 	usbrev = sc->sc_bus->usbrev;
    183 	printf(": USB revision %s", usbrev_str[usbrev]);
    184 	switch (usbrev) {
    185 	case USBREV_1_0:
    186 	case USBREV_1_1:
    187 		speed = USB_SPEED_FULL;
    188 		break;
    189 	case USBREV_2_0:
    190 		speed = USB_SPEED_HIGH;
    191 		break;
    192 	default:
    193 		printf(", not supported\n");
    194 		sc->sc_dying = 1;
    195 		USB_ATTACH_ERROR_RETURN;
    196 	}
    197 	printf("\n");
    198 
    199 	/* Make sure not to use tsleep() if we are cold booting. */
    200 	if (cold)
    201 		sc->sc_bus->use_polling++;
    202 
    203 	ue = usb_alloc_event();
    204 	ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    205 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    206 
    207 #ifdef USB_USE_SOFTINTR
    208 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    209 	/* XXX we should have our own level */
    210 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
    211 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
    212 	if (sc->sc_bus->soft == NULL) {
    213 		printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
    214 		sc->sc_dying = 1;
    215 		USB_ATTACH_ERROR_RETURN;
    216 	}
    217 #else
    218 	usb_callout_init(sc->sc_bus->softi);
    219 #endif
    220 #endif
    221 
    222 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
    223 		  &sc->sc_port);
    224 	if (!err) {
    225 		dev = sc->sc_port.device;
    226 		if (dev->hub == NULL) {
    227 			sc->sc_dying = 1;
    228 			printf("%s: root device is not a hub\n",
    229 			       USBDEVNAME(sc->sc_dev));
    230 			USB_ATTACH_ERROR_RETURN;
    231 		}
    232 		sc->sc_bus->root_hub = dev;
    233 #if 1
    234 		/*
    235 		 * Turning this code off will delay attachment of USB devices
    236 		 * until the USB event thread is running, which means that
    237 		 * the keyboard will not work until after cold boot.
    238 		 */
    239 		if (cold && (device_cfdata(&sc->sc_dev)->cf_flags & 1))
    240 			dev->hub->explore(sc->sc_bus->root_hub);
    241 #endif
    242 	} else {
    243 		printf("%s: root hub problem, error=%d\n",
    244 		       USBDEVNAME(sc->sc_dev), err);
    245 		sc->sc_dying = 1;
    246 	}
    247 	if (cold)
    248 		sc->sc_bus->use_polling--;
    249 
    250 	config_pending_incr();
    251 	usb_kthread_create(usb_create_event_thread, sc);
    252 
    253 	USB_ATTACH_SUCCESS_RETURN;
    254 }
    255 
    256 static const char *taskq_names[] = USB_TASKQ_NAMES;
    257 
    258 #if defined(__NetBSD__) || defined(__OpenBSD__)
    259 void
    260 usb_create_event_thread(void *arg)
    261 {
    262 	struct usb_softc *sc = arg;
    263 	struct usb_taskq *taskq;
    264 	int i;
    265 
    266 	if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    267 			   "%s", sc->sc_dev.dv_xname)) {
    268 		printf("%s: unable to create event thread for\n",
    269 		       sc->sc_dev.dv_xname);
    270 		panic("usb_create_event_thread");
    271 	}
    272 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    273 		taskq = &usb_taskq[i];
    274 
    275 		if (taskq->taskcreated)
    276 			continue;
    277 
    278 		TAILQ_INIT(&taskq->tasks);
    279 		taskq->taskcreated = 1;
    280 		taskq->name = taskq_names[i];
    281 		if (usb_kthread_create1(usb_task_thread, taskq,
    282 					&taskq->task_thread_proc, taskq->name)) {
    283 			printf("unable to create task thread: %s\n", taskq->name);
    284 			panic("usb_create_event_thread task");
    285 		}
    286 	}
    287 }
    288 
    289 /*
    290  * Add a task to be performed by the task thread.  This function can be
    291  * called from any context and the task will be executed in a process
    292  * context ASAP.
    293  */
    294 void
    295 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
    296 {
    297 	struct usb_taskq *taskq;
    298 	int s;
    299 
    300 	taskq = &usb_taskq[queue];
    301 	s = splusb();
    302 	if (task->queue == -1) {
    303 		DPRINTFN(2,("usb_add_task: task=%p\n", task));
    304 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    305 		task->queue = queue;
    306 	} else {
    307 		DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
    308 	}
    309 	wakeup(&taskq->tasks);
    310 	splx(s);
    311 }
    312 
    313 void
    314 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
    315 {
    316 	struct usb_taskq *taskq;
    317 	int s;
    318 
    319 	taskq = &usb_taskq[task->queue];
    320 	s = splusb();
    321 	if (task->queue != -1) {
    322 		TAILQ_REMOVE(&taskq->tasks, task, next);
    323 		task->queue = -1;
    324 	}
    325 	splx(s);
    326 }
    327 
    328 void
    329 usb_event_thread(void *arg)
    330 {
    331 	struct usb_softc *sc = arg;
    332 
    333 	DPRINTF(("usb_event_thread: start\n"));
    334 
    335 	/*
    336 	 * In case this controller is a companion controller to an
    337 	 * EHCI controller we need to wait until the EHCI controller
    338 	 * has grabbed the port.
    339 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    340 	 * know how to synchronize the creation of the threads so it
    341 	 * will work.
    342 	 */
    343 	usb_delay_ms(sc->sc_bus, 500);
    344 
    345 	/* Make sure first discover does something. */
    346 	sc->sc_bus->needs_explore = 1;
    347 	usb_discover(sc);
    348 	config_pending_decr();
    349 
    350 	while (!sc->sc_dying) {
    351 #ifdef USB_DEBUG
    352 		if (usb_noexplore < 2)
    353 #endif
    354 		usb_discover(sc);
    355 #ifdef USB_DEBUG
    356 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    357 		    usb_noexplore ? 0 : hz * 60);
    358 #else
    359 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    360 		    hz * 60);
    361 #endif
    362 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    363 	}
    364 	sc->sc_event_thread = NULL;
    365 
    366 	/* In case parent is waiting for us to exit. */
    367 	wakeup(sc);
    368 
    369 	DPRINTF(("usb_event_thread: exit\n"));
    370 	kthread_exit(0);
    371 }
    372 
    373 void
    374 usb_task_thread(void *arg)
    375 {
    376 	struct usb_task *task;
    377 	struct usb_taskq *taskq;
    378 	int s;
    379 
    380 	taskq = arg;
    381 	DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
    382 
    383 	s = splusb();
    384 	for (;;) {
    385 		task = TAILQ_FIRST(&taskq->tasks);
    386 		if (task == NULL) {
    387 			tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
    388 			task = TAILQ_FIRST(&taskq->tasks);
    389 		}
    390 		DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
    391 		if (task != NULL) {
    392 			TAILQ_REMOVE(&taskq->tasks, task, next);
    393 			task->queue = -1;
    394 			splx(s);
    395 			task->fun(task->arg);
    396 			s = splusb();
    397 		}
    398 	}
    399 }
    400 
    401 int
    402 usbctlprint(void *aux, const char *pnp)
    403 {
    404 	/* only "usb"es can attach to host controllers */
    405 	if (pnp)
    406 		aprint_normal("usb at %s", pnp);
    407 
    408 	return (UNCONF);
    409 }
    410 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    411 
    412 int
    413 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    414 {
    415 	int unit = minor(dev);
    416 	struct usb_softc *sc;
    417 
    418 	if (unit == USB_DEV_MINOR) {
    419 		if (usb_dev_open)
    420 			return (EBUSY);
    421 		usb_dev_open = 1;
    422 		usb_async_proc = 0;
    423 		return (0);
    424 	}
    425 
    426 	USB_GET_SC_OPEN(usb, unit, sc);
    427 
    428 	if (sc->sc_dying)
    429 		return (EIO);
    430 
    431 	return (0);
    432 }
    433 
    434 int
    435 usbread(dev_t dev, struct uio *uio, int flag)
    436 {
    437 	struct usb_event *ue;
    438 #ifdef COMPAT_30
    439 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    440 #endif
    441 	int s, error, n, useold;
    442 
    443 	if (minor(dev) != USB_DEV_MINOR)
    444 		return (ENXIO);
    445 
    446 	useold = 0;
    447 	switch (uio->uio_resid) {
    448 #ifdef COMPAT_30
    449 	case sizeof(struct usb_event_old):
    450 		ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
    451 			     M_WAITOK|M_ZERO);
    452 		useold = 1;
    453 		/* FALLTHRU */
    454 #endif
    455 	case sizeof(struct usb_event):
    456 		ue = usb_alloc_event();
    457 		break;
    458 	default:
    459 		return (EINVAL);
    460 	}
    461 
    462 	error = 0;
    463 	s = splusb();
    464 	for (;;) {
    465 		n = usb_get_next_event(ue);
    466 		if (n != 0)
    467 			break;
    468 		if (flag & IO_NDELAY) {
    469 			error = EWOULDBLOCK;
    470 			break;
    471 		}
    472 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    473 		if (error)
    474 			break;
    475 	}
    476 	splx(s);
    477 	if (!error) {
    478 #ifdef COMPAT_30
    479 		if (useold) { /* copy fields to old struct */
    480 			ueo->ue_type = ue->ue_type;
    481 			memcpy(&ueo->ue_time, &ue->ue_time,
    482 			      sizeof(struct timespec));
    483 			switch (ue->ue_type) {
    484 				case USB_EVENT_DEVICE_ATTACH:
    485 				case USB_EVENT_DEVICE_DETACH:
    486 					usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
    487 					break;
    488 
    489 				case USB_EVENT_CTRLR_ATTACH:
    490 				case USB_EVENT_CTRLR_DETACH:
    491 					ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
    492 					break;
    493 
    494 				case USB_EVENT_DRIVER_ATTACH:
    495 				case USB_EVENT_DRIVER_DETACH:
    496 					ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
    497 					memcpy(ueo->u.ue_driver.ue_devname,
    498 					       ue->u.ue_driver.ue_devname,
    499 					       sizeof(ue->u.ue_driver.ue_devname));
    500 					break;
    501 				default:
    502 					;
    503 			}
    504 
    505 			error = uiomove((void *)ueo, uio->uio_resid, uio);
    506 		} else
    507 #endif
    508 			error = uiomove((void *)ue, uio->uio_resid, uio);
    509 	}
    510 	usb_free_event(ue);
    511 #ifdef COMPAT_30
    512 	if (useold)
    513 		free(ueo, M_USBDEV);
    514 #endif
    515 
    516 	return (error);
    517 }
    518 
    519 int
    520 usbclose(dev_t dev, int flag, int mode,
    521     struct lwp *l)
    522 {
    523 	int unit = minor(dev);
    524 
    525 	if (unit == USB_DEV_MINOR) {
    526 		usb_async_proc = 0;
    527 		usb_dev_open = 0;
    528 	}
    529 
    530 	return (0);
    531 }
    532 
    533 int
    534 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    535 {
    536 	struct usb_softc *sc;
    537 	int unit = minor(devt);
    538 
    539 	if (unit == USB_DEV_MINOR) {
    540 		switch (cmd) {
    541 		case FIONBIO:
    542 			/* All handled in the upper FS layer. */
    543 			return (0);
    544 
    545 		case FIOASYNC:
    546 			if (*(int *)data)
    547 				usb_async_proc = l->l_proc;
    548 			else
    549 				usb_async_proc = 0;
    550 			return (0);
    551 
    552 		default:
    553 			return (EINVAL);
    554 		}
    555 	}
    556 
    557 	USB_GET_SC(usb, unit, sc);
    558 
    559 	if (sc->sc_dying)
    560 		return (EIO);
    561 
    562 	switch (cmd) {
    563 #ifdef USB_DEBUG
    564 	case USB_SETDEBUG:
    565 		if (!(flag & FWRITE))
    566 			return (EBADF);
    567 		usbdebug  = ((*(int *)data) & 0x000000ff);
    568 #if defined(UHCI_DEBUG) && NUHCI > 0
    569 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    570 #endif
    571 #if defined(OHCI_DEBUG) && NOHCI > 0
    572 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    573 #endif
    574 		break;
    575 #endif /* USB_DEBUG */
    576 	case USB_REQUEST:
    577 	{
    578 		struct usb_ctl_request *ur = (void *)data;
    579 		int len = UGETW(ur->ucr_request.wLength);
    580 		struct iovec iov;
    581 		struct uio uio;
    582 		void *ptr = 0;
    583 		int addr = ur->ucr_addr;
    584 		usbd_status err;
    585 		int error = 0;
    586 
    587 		if (!(flag & FWRITE))
    588 			return (EBADF);
    589 
    590 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    591 		if (len < 0 || len > 32768)
    592 			return (EINVAL);
    593 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    594 		    sc->sc_bus->devices[addr] == 0)
    595 			return (EINVAL);
    596 		if (len != 0) {
    597 			iov.iov_base = (void *)ur->ucr_data;
    598 			iov.iov_len = len;
    599 			uio.uio_iov = &iov;
    600 			uio.uio_iovcnt = 1;
    601 			uio.uio_resid = len;
    602 			uio.uio_offset = 0;
    603 			uio.uio_rw =
    604 				ur->ucr_request.bmRequestType & UT_READ ?
    605 				UIO_READ : UIO_WRITE;
    606 			uio.uio_vmspace = l->l_proc->p_vmspace;
    607 			ptr = malloc(len, M_TEMP, M_WAITOK);
    608 			if (uio.uio_rw == UIO_WRITE) {
    609 				error = uiomove(ptr, len, &uio);
    610 				if (error)
    611 					goto ret;
    612 			}
    613 		}
    614 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    615 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    616 			  USBD_DEFAULT_TIMEOUT);
    617 		if (err) {
    618 			error = EIO;
    619 			goto ret;
    620 		}
    621 		if (len != 0) {
    622 			if (uio.uio_rw == UIO_READ) {
    623 				error = uiomove(ptr, len, &uio);
    624 				if (error)
    625 					goto ret;
    626 			}
    627 		}
    628 	ret:
    629 		if (ptr)
    630 			free(ptr, M_TEMP);
    631 		return (error);
    632 	}
    633 
    634 	case USB_DEVICEINFO:
    635 	{
    636 		usbd_device_handle dev;
    637 		struct usb_device_info *di = (void *)data;
    638 		int addr = di->udi_addr;
    639 
    640 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    641 			return EINVAL;
    642 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    643 			return ENXIO;
    644 		usbd_fill_deviceinfo(dev, di, 1);
    645 		break;
    646 	}
    647 
    648 #ifdef COMPAT_30
    649 	case USB_DEVICEINFO_OLD:
    650 	{
    651 		usbd_device_handle dev;
    652 		struct usb_device_info_old *di = (void *)data;
    653 		int addr = di->udi_addr;
    654 
    655 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    656 			return EINVAL;
    657 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    658 			return ENXIO;
    659 		usbd_fill_deviceinfo_old(dev, di, 1);
    660 		break;
    661 	}
    662 #endif
    663 
    664 	case USB_DEVICESTATS:
    665 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    666 		break;
    667 
    668 	default:
    669 		return (EINVAL);
    670 	}
    671 	return (0);
    672 }
    673 
    674 int
    675 usbpoll(dev_t dev, int events, struct lwp *l)
    676 {
    677 	int revents, mask, s;
    678 
    679 	if (minor(dev) == USB_DEV_MINOR) {
    680 		revents = 0;
    681 		mask = POLLIN | POLLRDNORM;
    682 
    683 		s = splusb();
    684 		if (events & mask && usb_nevents > 0)
    685 			revents |= events & mask;
    686 		if (revents == 0 && events & mask)
    687 			selrecord(l, &usb_selevent);
    688 		splx(s);
    689 
    690 		return (revents);
    691 	} else {
    692 		return (0);
    693 	}
    694 }
    695 
    696 static void
    697 filt_usbrdetach(struct knote *kn)
    698 {
    699 	int s;
    700 
    701 	s = splusb();
    702 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    703 	splx(s);
    704 }
    705 
    706 static int
    707 filt_usbread(struct knote *kn, long hint)
    708 {
    709 
    710 	if (usb_nevents == 0)
    711 		return (0);
    712 
    713 	kn->kn_data = sizeof(struct usb_event);
    714 	return (1);
    715 }
    716 
    717 static const struct filterops usbread_filtops =
    718 	{ 1, NULL, filt_usbrdetach, filt_usbread };
    719 
    720 int
    721 usbkqfilter(dev_t dev, struct knote *kn)
    722 {
    723 	struct klist *klist;
    724 	int s;
    725 
    726 	switch (kn->kn_filter) {
    727 	case EVFILT_READ:
    728 		if (minor(dev) != USB_DEV_MINOR)
    729 			return (1);
    730 		klist = &usb_selevent.sel_klist;
    731 		kn->kn_fop = &usbread_filtops;
    732 		break;
    733 
    734 	default:
    735 		return (1);
    736 	}
    737 
    738 	kn->kn_hook = NULL;
    739 
    740 	s = splusb();
    741 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    742 	splx(s);
    743 
    744 	return (0);
    745 }
    746 
    747 /* Explore device tree from the root. */
    748 Static void
    749 usb_discover(void *v)
    750 {
    751 	struct usb_softc *sc = v;
    752 
    753 	DPRINTFN(2,("usb_discover\n"));
    754 #ifdef USB_DEBUG
    755 	if (usb_noexplore > 1)
    756 		return;
    757 #endif
    758 	/*
    759 	 * We need mutual exclusion while traversing the device tree,
    760 	 * but this is guaranteed since this function is only called
    761 	 * from the event thread for the controller.
    762 	 */
    763 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    764 		sc->sc_bus->needs_explore = 0;
    765 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    766 	}
    767 }
    768 
    769 void
    770 usb_needs_explore(usbd_device_handle dev)
    771 {
    772 	DPRINTFN(2,("usb_needs_explore\n"));
    773 	dev->bus->needs_explore = 1;
    774 	wakeup(&dev->bus->needs_explore);
    775 }
    776 
    777 void
    778 usb_needs_reattach(usbd_device_handle dev)
    779 {
    780 	DPRINTFN(2,("usb_needs_reattach\n"));
    781 	dev->powersrc->reattach = 1;
    782 	dev->bus->needs_explore = 1;
    783 	wakeup(&dev->bus->needs_explore);
    784 }
    785 
    786 /* Called at splusb() */
    787 int
    788 usb_get_next_event(struct usb_event *ue)
    789 {
    790 	struct usb_event_q *ueq;
    791 
    792 	if (usb_nevents <= 0)
    793 		return (0);
    794 	ueq = SIMPLEQ_FIRST(&usb_events);
    795 #ifdef DIAGNOSTIC
    796 	if (ueq == NULL) {
    797 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
    798 		usb_nevents = 0;
    799 		return (0);
    800 	}
    801 #endif
    802 	if (ue)
    803 		*ue = ueq->ue;
    804 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
    805 	usb_free_event((struct usb_event *)(void *)ueq);
    806 	usb_nevents--;
    807 	return (1);
    808 }
    809 
    810 void
    811 usbd_add_dev_event(int type, usbd_device_handle udev)
    812 {
    813 	struct usb_event *ue = usb_alloc_event();
    814 
    815 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
    816 	usb_add_event(type, ue);
    817 }
    818 
    819 void
    820 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
    821 {
    822 	struct usb_event *ue = usb_alloc_event();
    823 
    824 	ue->u.ue_driver.ue_cookie = udev->cookie;
    825 	strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
    826 	    sizeof ue->u.ue_driver.ue_devname);
    827 	usb_add_event(type, ue);
    828 }
    829 
    830 Static struct usb_event *
    831 usb_alloc_event(void)
    832 {
    833 	/* Yes, this is right; we allocate enough so that we can use it later */
    834 	return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
    835 }
    836 
    837 Static void
    838 usb_free_event(struct usb_event *uep)
    839 {
    840 	free(uep, M_USBDEV);
    841 }
    842 
    843 Static void
    844 usb_add_event(int type, struct usb_event *uep)
    845 {
    846 	struct usb_event_q *ueq;
    847 	struct timeval thetime;
    848 	int s;
    849 
    850 	microtime(&thetime);
    851 	/* Don't want to wait here inside splusb() */
    852 	ueq = (struct usb_event_q *)(void *)uep;
    853 	ueq->ue = *uep;
    854 	ueq->ue.ue_type = type;
    855 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    856 
    857 	s = splusb();
    858 	if (++usb_nevents >= USB_MAX_EVENTS) {
    859 		/* Too many queued events, drop an old one. */
    860 		DPRINTFN(-1,("usb: event dropped\n"));
    861 		(void)usb_get_next_event(0);
    862 	}
    863 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    864 	wakeup(&usb_events);
    865 	selnotify(&usb_selevent, 0);
    866 	if (usb_async_proc != NULL) {
    867 		mutex_enter(&proclist_mutex);
    868 		psignal(usb_async_proc, SIGIO);
    869 		mutex_exit(&proclist_mutex);
    870 	}
    871 	splx(s);
    872 }
    873 
    874 void
    875 usb_schedsoftintr(usbd_bus_handle bus)
    876 {
    877 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
    878 #ifdef USB_USE_SOFTINTR
    879 	if (bus->use_polling) {
    880 		bus->methods->soft_intr(bus);
    881 	} else {
    882 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    883 		softintr_schedule(bus->soft);
    884 #else
    885 		if (!callout_pending(&bus->softi))
    886 			callout_reset(&bus->softi, 0, bus->methods->soft_intr,
    887 			    bus);
    888 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
    889 	}
    890 #else
    891 	bus->methods->soft_intr(bus);
    892 #endif /* USB_USE_SOFTINTR */
    893 }
    894 
    895 int
    896 usb_activate(device_ptr_t self, enum devact act)
    897 {
    898 	struct usb_softc *sc = (struct usb_softc *)self;
    899 	usbd_device_handle dev = sc->sc_port.device;
    900 	int i, rv = 0;
    901 
    902 	switch (act) {
    903 	case DVACT_ACTIVATE:
    904 		return (EOPNOTSUPP);
    905 
    906 	case DVACT_DEACTIVATE:
    907 		sc->sc_dying = 1;
    908 		if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
    909 			for (i = 0; dev->subdevs[i]; i++)
    910 				rv |= config_deactivate(dev->subdevs[i]);
    911 		}
    912 		break;
    913 	}
    914 	return (rv);
    915 }
    916 
    917 int
    918 usb_detach(device_ptr_t self, int flags)
    919 {
    920 	struct usb_softc *sc = (struct usb_softc *)self;
    921 	struct usb_event *ue;
    922 
    923 	DPRINTF(("usb_detach: start\n"));
    924 
    925 	sc->sc_dying = 1;
    926 
    927 	/* Make all devices disconnect. */
    928 	if (sc->sc_port.device != NULL)
    929 		usb_disconnect_port(&sc->sc_port, self);
    930 
    931 	/* Kill off event thread. */
    932 	if (sc->sc_event_thread != NULL) {
    933 		wakeup(&sc->sc_bus->needs_explore);
    934 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    935 			printf("%s: event thread didn't die\n",
    936 			       USBDEVNAME(sc->sc_dev));
    937 		DPRINTF(("usb_detach: event thread dead\n"));
    938 	}
    939 
    940 #ifdef USB_USE_SOFTINTR
    941 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    942 	if (sc->sc_bus->soft != NULL) {
    943 		softintr_disestablish(sc->sc_bus->soft);
    944 		sc->sc_bus->soft = NULL;
    945 	}
    946 #else
    947 	callout_stop(&sc->sc_bus->softi);
    948 #endif
    949 #endif
    950 
    951 	ue = usb_alloc_event();
    952 	ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    953 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
    954 
    955 	return (0);
    956 }
    957 
    958 #ifdef COMPAT_30
    959 Static void
    960 usb_copy_old_devinfo(struct usb_device_info_old *uo,
    961 		     const struct usb_device_info *ue)
    962 {
    963 	const unsigned char *p;
    964 	unsigned char *q;
    965 	int i, n;
    966 
    967 	uo->udi_bus = ue->udi_bus;
    968 	uo->udi_addr = ue->udi_addr;
    969 	uo->udi_cookie = ue->udi_cookie;
    970 	for (i = 0, p = (const unsigned char *)ue->udi_product,
    971 	     q = (unsigned char *)uo->udi_product;
    972 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
    973 		if (*p < 0x80)
    974 			q[i++] = *p;
    975 		else {
    976 			q[i++] = '?';
    977 			if ((*p & 0xe0) == 0xe0)
    978 				p++;
    979 			p++;
    980 		}
    981 	}
    982 	q[i] = 0;
    983 
    984 	for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
    985 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
    986 		if (* p < 0x80)
    987 			q[i++] = *p;
    988 		else {
    989 			q[i++] = '?';
    990 			p++;
    991 			if ((*p & 0xe0) == 0xe0)
    992 				p++;
    993 		}
    994 	}
    995 	q[i] = 0;
    996 
    997 	memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
    998 
    999 	uo->udi_productNo = ue->udi_productNo;
   1000 	uo->udi_vendorNo = ue->udi_vendorNo;
   1001 	uo->udi_releaseNo = ue->udi_releaseNo;
   1002 	uo->udi_class = ue->udi_class;
   1003 	uo->udi_subclass = ue->udi_subclass;
   1004 	uo->udi_protocol = ue->udi_protocol;
   1005 	uo->udi_config = ue->udi_config;
   1006 	uo->udi_speed = ue->udi_speed;
   1007 	uo->udi_power = ue->udi_power;
   1008 	uo->udi_nports = ue->udi_nports;
   1009 
   1010 	for (n=0; n<USB_MAX_DEVNAMES; n++)
   1011 		memcpy(uo->udi_devnames[n],
   1012 		       ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
   1013 	memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
   1014 }
   1015 #endif
   1016