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