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