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