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