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