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