Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.171
      1 /*	$NetBSD: usb.c,v 1.171 2018/08/02 06:09:04 riastradh 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.171 2018/08/02 06:09:04 riastradh Exp $");
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_usb.h"
     44 #include "opt_compat_netbsd.h"
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/kmem.h>
     51 #include <sys/device.h>
     52 #include <sys/kthread.h>
     53 #include <sys/proc.h>
     54 #include <sys/conf.h>
     55 #include <sys/fcntl.h>
     56 #include <sys/poll.h>
     57 #include <sys/select.h>
     58 #include <sys/vnode.h>
     59 #include <sys/signalvar.h>
     60 #include <sys/intr.h>
     61 #include <sys/module.h>
     62 #include <sys/mutex.h>
     63 #include <sys/bus.h>
     64 #include <sys/once.h>
     65 #include <sys/atomic.h>
     66 #include <sys/sysctl.h>
     67 
     68 #include <dev/usb/usb.h>
     69 #include <dev/usb/usbdi.h>
     70 #include <dev/usb/usbdi_util.h>
     71 #include <dev/usb/usbdivar.h>
     72 #include <dev/usb/usb_verbose.h>
     73 #include <dev/usb/usb_quirks.h>
     74 #include <dev/usb/usbhist.h>
     75 
     76 #if defined(USB_DEBUG)
     77 
     78 #ifndef USBHIST_SIZE
     79 #define USBHIST_SIZE 50000
     80 #endif
     81 
     82 static struct kern_history_ent usbhistbuf[USBHIST_SIZE];
     83 USBHIST_DEFINE(usbhist) = KERNHIST_INITIALIZER(usbhist, usbhistbuf);
     84 
     85 #endif
     86 
     87 #define USB_DEV_MINOR 255
     88 
     89 #ifdef USB_DEBUG
     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 
     97 int	usbdebug = 0;
     98 SYSCTL_SETUP(sysctl_hw_usb_setup, "sysctl hw.usb setup")
     99 {
    100 	int err;
    101 	const struct sysctlnode *rnode;
    102 	const struct sysctlnode *cnode;
    103 
    104 	err = sysctl_createv(clog, 0, NULL, &rnode,
    105 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usb",
    106 	    SYSCTL_DESCR("usb global controls"),
    107 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    108 
    109 	if (err)
    110 		goto fail;
    111 
    112 	/* control debugging printfs */
    113 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    114 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    115 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    116 	    NULL, 0, &usbdebug, sizeof(usbdebug), CTL_CREATE, CTL_EOL);
    117 	if (err)
    118 		goto fail;
    119 
    120 	return;
    121 fail:
    122 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    123 }
    124 #else
    125 #define	usb_noexplore 0
    126 #endif
    127 
    128 #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usbdebug,FMT,A,B,C,D)
    129 #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
    130 
    131 struct usb_softc {
    132 #if 0
    133 	device_t	sc_dev;		/* base device */
    134 #endif
    135 	struct usbd_bus *sc_bus;		/* USB controller */
    136 	struct usbd_port sc_port;	/* dummy port for root hub */
    137 
    138 	struct lwp	*sc_event_thread;
    139 
    140 	char		sc_dying;
    141 };
    142 
    143 struct usb_taskq {
    144 	TAILQ_HEAD(, usb_task) tasks;
    145 	kmutex_t lock;
    146 	kcondvar_t cv;
    147 	struct lwp *task_thread_lwp;
    148 	const char *name;
    149 	struct usb_task *current_task;
    150 };
    151 
    152 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
    153 
    154 dev_type_open(usbopen);
    155 dev_type_close(usbclose);
    156 dev_type_read(usbread);
    157 dev_type_ioctl(usbioctl);
    158 dev_type_poll(usbpoll);
    159 dev_type_kqfilter(usbkqfilter);
    160 
    161 const struct cdevsw usb_cdevsw = {
    162 	.d_open = usbopen,
    163 	.d_close = usbclose,
    164 	.d_read = usbread,
    165 	.d_write = nowrite,
    166 	.d_ioctl = usbioctl,
    167 	.d_stop = nostop,
    168 	.d_tty = notty,
    169 	.d_poll = usbpoll,
    170 	.d_mmap = nommap,
    171 	.d_kqfilter = usbkqfilter,
    172 	.d_discard = nodiscard,
    173 	.d_flag = D_OTHER
    174 };
    175 
    176 Static void	usb_discover(struct usb_softc *);
    177 Static void	usb_create_event_thread(device_t);
    178 Static void	usb_event_thread(void *);
    179 Static void	usb_task_thread(void *);
    180 
    181 #define USB_MAX_EVENTS 100
    182 struct usb_event_q {
    183 	struct usb_event ue;
    184 	SIMPLEQ_ENTRY(usb_event_q) next;
    185 };
    186 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    187 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    188 Static int usb_nevents = 0;
    189 Static struct selinfo usb_selevent;
    190 Static kmutex_t usb_event_lock;
    191 Static kcondvar_t usb_event_cv;
    192 Static proc_t *usb_async_proc;  /* process that wants USB SIGIO */
    193 Static void *usb_async_sih;
    194 Static int usb_dev_open = 0;
    195 Static struct usb_event *usb_alloc_event(void);
    196 Static void usb_free_event(struct usb_event *);
    197 Static void usb_add_event(int, struct usb_event *);
    198 Static int usb_get_next_event(struct usb_event *);
    199 Static void usb_async_intr(void *);
    200 Static void usb_soft_intr(void *);
    201 
    202 #ifdef COMPAT_30
    203 Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
    204 #endif
    205 
    206 Static const char *usbrev_str[] = USBREV_STR;
    207 
    208 static int usb_match(device_t, cfdata_t, void *);
    209 static void usb_attach(device_t, device_t, void *);
    210 static int usb_detach(device_t, int);
    211 static int usb_activate(device_t, enum devact);
    212 static void usb_childdet(device_t, device_t);
    213 static int usb_once_init(void);
    214 static void usb_doattach(device_t);
    215 
    216 extern struct cfdriver usb_cd;
    217 
    218 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
    219     usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
    220     DVF_DETACH_SHUTDOWN);
    221 
    222 static const char *taskq_names[] = USB_TASKQ_NAMES;
    223 
    224 int
    225 usb_match(device_t parent, cfdata_t match, void *aux)
    226 {
    227 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    228 
    229 	return UMATCH_GENERIC;
    230 }
    231 
    232 void
    233 usb_attach(device_t parent, device_t self, void *aux)
    234 {
    235 	static ONCE_DECL(init_control);
    236 	struct usb_softc *sc = device_private(self);
    237 	int usbrev;
    238 
    239 	sc->sc_bus = aux;
    240 	usbrev = sc->sc_bus->ub_revision;
    241 
    242 	aprint_naive("\n");
    243 	aprint_normal(": USB revision %s", usbrev_str[usbrev]);
    244 	switch (usbrev) {
    245 	case USBREV_1_0:
    246 	case USBREV_1_1:
    247 	case USBREV_2_0:
    248 	case USBREV_3_0:
    249 	case USBREV_3_1:
    250 		break;
    251 	default:
    252 		aprint_error(", not supported\n");
    253 		sc->sc_dying = 1;
    254 		return;
    255 	}
    256 	aprint_normal("\n");
    257 
    258 	/* XXX we should have our own level */
    259 	sc->sc_bus->ub_soft = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
    260 	    usb_soft_intr, sc->sc_bus);
    261 	if (sc->sc_bus->ub_soft == NULL) {
    262 		aprint_error("%s: can't register softintr\n",
    263 			     device_xname(self));
    264 		sc->sc_dying = 1;
    265 		return;
    266 	}
    267 
    268 	sc->sc_bus->ub_methods->ubm_getlock(sc->sc_bus, &sc->sc_bus->ub_lock);
    269 	KASSERT(sc->sc_bus->ub_lock != NULL);
    270 
    271 	RUN_ONCE(&init_control, usb_once_init);
    272 	config_interrupts(self, usb_doattach);
    273 }
    274 
    275 static int
    276 usb_once_init(void)
    277 {
    278 	struct usb_taskq *taskq;
    279 	int i;
    280 
    281 	USBHIST_LINK_STATIC(usbhist);
    282 
    283 	selinit(&usb_selevent);
    284 	mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
    285 	cv_init(&usb_event_cv, "usbrea");
    286 
    287 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    288 		taskq = &usb_taskq[i];
    289 
    290 		TAILQ_INIT(&taskq->tasks);
    291 		/*
    292 		 * Since USB task methods usb_{add,rem}_task are callable
    293 		 * from any context, we have to make this lock a spinlock.
    294 		 */
    295 		mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
    296 		cv_init(&taskq->cv, "usbtsk");
    297 		taskq->name = taskq_names[i];
    298 		taskq->current_task = NULL;
    299 		if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    300 		    usb_task_thread, taskq, &taskq->task_thread_lwp,
    301 		    "%s", taskq->name)) {
    302 			printf("unable to create task thread: %s\n", taskq->name);
    303 			panic("usb_create_event_thread task");
    304 		}
    305 		/*
    306 		 * XXX we should make sure these threads are alive before
    307 		 * end up using them in usb_doattach().
    308 		 */
    309 	}
    310 	return 0;
    311 }
    312 
    313 static void
    314 usb_doattach(device_t self)
    315 {
    316 	struct usb_softc *sc = device_private(self);
    317 	struct usbd_device *dev;
    318 	usbd_status err;
    319 	int speed;
    320 	struct usb_event *ue;
    321 
    322 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    323 
    324 	sc->sc_bus->ub_usbctl = self;
    325 	sc->sc_port.up_power = USB_MAX_POWER;
    326 
    327 	switch (sc->sc_bus->ub_revision) {
    328 	case USBREV_1_0:
    329 	case USBREV_1_1:
    330 		speed = USB_SPEED_FULL;
    331 		break;
    332 	case USBREV_2_0:
    333 		speed = USB_SPEED_HIGH;
    334 		break;
    335 	case USBREV_3_0:
    336 		speed = USB_SPEED_SUPER;
    337 		break;
    338 	case USBREV_3_1:
    339 		speed = USB_SPEED_SUPER_PLUS;
    340 		break;
    341 	default:
    342 		panic("usb_doattach");
    343 	}
    344 
    345 	cv_init(&sc->sc_bus->ub_needsexplore_cv, "usbevt");
    346 
    347 	ue = usb_alloc_event();
    348 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
    349 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    350 
    351 	err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
    352 		  &sc->sc_port);
    353 	if (!err) {
    354 		dev = sc->sc_port.up_dev;
    355 		if (dev->ud_hub == NULL) {
    356 			sc->sc_dying = 1;
    357 			aprint_error("%s: root device is not a hub\n",
    358 				     device_xname(self));
    359 			return;
    360 		}
    361 		sc->sc_bus->ub_roothub = dev;
    362 		usb_create_event_thread(self);
    363 #if 1
    364 		/*
    365 		 * Turning this code off will delay attachment of USB devices
    366 		 * until the USB event thread is running, which means that
    367 		 * the keyboard will not work until after cold boot.
    368 		 */
    369 		if (cold && (device_cfdata(self)->cf_flags & 1))
    370 			dev->ud_hub->uh_explore(sc->sc_bus->ub_roothub);
    371 #endif
    372 	} else {
    373 		aprint_error("%s: root hub problem, error=%s\n",
    374 			     device_xname(self), usbd_errstr(err));
    375 		sc->sc_dying = 1;
    376 	}
    377 
    378 	config_pending_incr(self);
    379 
    380 	if (!pmf_device_register(self, NULL, NULL))
    381 		aprint_error_dev(self, "couldn't establish power handler\n");
    382 
    383 	usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    384 	   usb_async_intr, NULL);
    385 
    386 	return;
    387 }
    388 
    389 void
    390 usb_create_event_thread(device_t self)
    391 {
    392 	struct usb_softc *sc = device_private(self);
    393 
    394 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    395 	    usb_event_thread, sc, &sc->sc_event_thread,
    396 	    "%s", device_xname(self))) {
    397 		printf("%s: unable to create event thread for\n",
    398 		       device_xname(self));
    399 		panic("usb_create_event_thread");
    400 	}
    401 }
    402 
    403 /*
    404  * Add a task to be performed by the task thread.  This function can be
    405  * called from any context and the task will be executed in a process
    406  * context ASAP.
    407  */
    408 void
    409 usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
    410 {
    411 	struct usb_taskq *taskq;
    412 
    413 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    414 
    415 	KASSERT(0 <= queue);
    416 	KASSERT(queue < USB_NUM_TASKQS);
    417 	taskq = &usb_taskq[queue];
    418 	mutex_enter(&taskq->lock);
    419 	if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
    420 	    USB_NUM_TASKQS) {
    421 		DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
    422 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    423 		cv_signal(&taskq->cv);
    424 	} else {
    425 		DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
    426 	}
    427 	mutex_exit(&taskq->lock);
    428 }
    429 
    430 /*
    431  * usb_rem_task(dev, task)
    432  *
    433  *	If task is queued to run, remove it from the queue.
    434  *
    435  *	Caller is _not_ guaranteed that the task is not running when
    436  *	this is done.
    437  *
    438  *	Never sleeps.
    439  */
    440 void
    441 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
    442 {
    443 	unsigned queue;
    444 
    445 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    446 
    447 	while ((queue = task->queue) != USB_NUM_TASKQS) {
    448 		struct usb_taskq *taskq = &usb_taskq[queue];
    449 		mutex_enter(&taskq->lock);
    450 		if (__predict_true(task->queue == queue)) {
    451 			TAILQ_REMOVE(&taskq->tasks, task, next);
    452 			task->queue = USB_NUM_TASKQS;
    453 			mutex_exit(&taskq->lock);
    454 			break;
    455 		}
    456 		mutex_exit(&taskq->lock);
    457 	}
    458 }
    459 
    460 /*
    461  * usb_rem_task_wait(dev, task, queue, interlock)
    462  *
    463  *	If task is scheduled to run, remove it from the queue.  If it
    464  *	may have already begun to run, drop interlock if not null, wait
    465  *	for it to complete, and reacquire interlock if not null.
    466  *	Return true if it successfully removed the task from the queue,
    467  *	false if not.
    468  *
    469  *	Caller MUST guarantee that task will not be scheduled on a
    470  *	_different_ queue, at least until after this returns.
    471  *
    472  *	If caller guarantees that task will not be scheduled on the
    473  *	same queue before this returns, then caller is guaranteed that
    474  *	the task is not running at all when this returns.
    475  *
    476  *	May sleep.
    477  */
    478 bool
    479 usb_rem_task_wait(struct usbd_device *dev, struct usb_task *task, int queue,
    480     kmutex_t *interlock)
    481 {
    482 	struct usb_taskq *taskq;
    483 	int queue1;
    484 	bool removed;
    485 
    486 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    487 	ASSERT_SLEEPABLE();
    488 	KASSERT(0 <= queue);
    489 	KASSERT(queue < USB_NUM_TASKQS);
    490 
    491 	taskq = &usb_taskq[queue];
    492 	mutex_enter(&taskq->lock);
    493 	queue1 = task->queue;
    494 	if (queue1 == USB_NUM_TASKQS) {
    495 		/*
    496 		 * It is not on the queue.  It may be about to run, or
    497 		 * it may have already finished running -- there is no
    498 		 * stopping it now.  Wait for it if it is running.
    499 		 */
    500 		if (interlock)
    501 			mutex_exit(interlock);
    502 		while (taskq->current_task == task)
    503 			cv_wait(&taskq->cv, &taskq->lock);
    504 		removed = false;
    505 	} else {
    506 		/*
    507 		 * It is still on the queue.  We can stop it before the
    508 		 * task thread will run it.
    509 		 */
    510 		KASSERTMSG(queue1 == queue, "task %p on q%d expected on q%d",
    511 		    task, queue1, queue);
    512 		TAILQ_REMOVE(&taskq->tasks, task, next);
    513 		task->queue = USB_NUM_TASKQS;
    514 		removed = true;
    515 	}
    516 	mutex_exit(&taskq->lock);
    517 
    518 	/*
    519 	 * If there's an interlock, and we dropped it to wait,
    520 	 * reacquire it.
    521 	 */
    522 	if (interlock && !removed)
    523 		mutex_enter(interlock);
    524 
    525 	return removed;
    526 }
    527 
    528 void
    529 usb_event_thread(void *arg)
    530 {
    531 	struct usb_softc *sc = arg;
    532 
    533 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    534 
    535 	/*
    536 	 * In case this controller is a companion controller to an
    537 	 * EHCI controller we need to wait until the EHCI controller
    538 	 * has grabbed the port.
    539 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    540 	 * know how to synchronize the creation of the threads so it
    541 	 * will work.
    542 	 */
    543 	usb_delay_ms(sc->sc_bus, 500);
    544 
    545 	/* Make sure first discover does something. */
    546 	mutex_enter(sc->sc_bus->ub_lock);
    547 	sc->sc_bus->ub_needsexplore = 1;
    548 	usb_discover(sc);
    549 	mutex_exit(sc->sc_bus->ub_lock);
    550 	config_pending_decr(sc->sc_bus->ub_usbctl);
    551 
    552 	mutex_enter(sc->sc_bus->ub_lock);
    553 	while (!sc->sc_dying) {
    554 		if (usb_noexplore < 2)
    555 			usb_discover(sc);
    556 
    557 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
    558 		    sc->sc_bus->ub_lock, usb_noexplore ? 0 : hz * 60);
    559 
    560 		DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
    561 	}
    562 	sc->sc_event_thread = NULL;
    563 
    564 	/* In case parent is waiting for us to exit. */
    565 	cv_signal(&sc->sc_bus->ub_needsexplore_cv);
    566 	mutex_exit(sc->sc_bus->ub_lock);
    567 
    568 	DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
    569 	kthread_exit(0);
    570 }
    571 
    572 void
    573 usb_task_thread(void *arg)
    574 {
    575 	struct usb_task *task;
    576 	struct usb_taskq *taskq;
    577 	bool mpsafe;
    578 
    579 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    580 
    581 	taskq = arg;
    582 	DPRINTF("start taskq %#jx", (uintptr_t)taskq, 0, 0, 0);
    583 
    584 	mutex_enter(&taskq->lock);
    585 	for (;;) {
    586 		task = TAILQ_FIRST(&taskq->tasks);
    587 		if (task == NULL) {
    588 			cv_wait(&taskq->cv, &taskq->lock);
    589 			task = TAILQ_FIRST(&taskq->tasks);
    590 		}
    591 		DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
    592 		if (task != NULL) {
    593 			mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
    594 			TAILQ_REMOVE(&taskq->tasks, task, next);
    595 			task->queue = USB_NUM_TASKQS;
    596 			taskq->current_task = task;
    597 			mutex_exit(&taskq->lock);
    598 
    599 			if (!mpsafe)
    600 				KERNEL_LOCK(1, curlwp);
    601 			task->fun(task->arg);
    602 			/* Can't dereference task after this point.  */
    603 			if (!mpsafe)
    604 				KERNEL_UNLOCK_ONE(curlwp);
    605 
    606 			mutex_enter(&taskq->lock);
    607 			KASSERTMSG(taskq->current_task == task,
    608 			    "somebody scribbled on usb taskq %p", taskq);
    609 			taskq->current_task = NULL;
    610 			cv_broadcast(&taskq->cv);
    611 		}
    612 	}
    613 	mutex_exit(&taskq->lock);
    614 }
    615 
    616 int
    617 usbctlprint(void *aux, const char *pnp)
    618 {
    619 	/* only "usb"es can attach to host controllers */
    620 	if (pnp)
    621 		aprint_normal("usb at %s", pnp);
    622 
    623 	return UNCONF;
    624 }
    625 
    626 int
    627 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    628 {
    629 	int unit = minor(dev);
    630 	struct usb_softc *sc;
    631 
    632 	if (unit == USB_DEV_MINOR) {
    633 		if (usb_dev_open)
    634 			return EBUSY;
    635 		usb_dev_open = 1;
    636 		mutex_enter(proc_lock);
    637 		usb_async_proc = 0;
    638 		mutex_exit(proc_lock);
    639 		return 0;
    640 	}
    641 
    642 	sc = device_lookup_private(&usb_cd, unit);
    643 	if (!sc)
    644 		return ENXIO;
    645 
    646 	if (sc->sc_dying)
    647 		return EIO;
    648 
    649 	return 0;
    650 }
    651 
    652 int
    653 usbread(dev_t dev, struct uio *uio, int flag)
    654 {
    655 	struct usb_event *ue;
    656 #ifdef COMPAT_30
    657 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    658 	int useold = 0;
    659 #endif
    660 	int error, n;
    661 
    662 	if (minor(dev) != USB_DEV_MINOR)
    663 		return ENXIO;
    664 
    665 	switch (uio->uio_resid) {
    666 #ifdef COMPAT_30
    667 	case sizeof(struct usb_event_old):
    668 		ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
    669 		useold = 1;
    670 		/* FALLTHRU */
    671 #endif
    672 	case sizeof(struct usb_event):
    673 		ue = usb_alloc_event();
    674 		break;
    675 	default:
    676 		return EINVAL;
    677 	}
    678 
    679 	error = 0;
    680 	mutex_enter(&usb_event_lock);
    681 	for (;;) {
    682 		n = usb_get_next_event(ue);
    683 		if (n != 0)
    684 			break;
    685 		if (flag & IO_NDELAY) {
    686 			error = EWOULDBLOCK;
    687 			break;
    688 		}
    689 		error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
    690 		if (error)
    691 			break;
    692 	}
    693 	mutex_exit(&usb_event_lock);
    694 	if (!error) {
    695 #ifdef COMPAT_30
    696 		if (useold) { /* copy fields to old struct */
    697 			ueo->ue_type = ue->ue_type;
    698 			memcpy(&ueo->ue_time, &ue->ue_time,
    699 			      sizeof(struct timespec));
    700 			switch (ue->ue_type) {
    701 				case USB_EVENT_DEVICE_ATTACH:
    702 				case USB_EVENT_DEVICE_DETACH:
    703 					usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
    704 					break;
    705 
    706 				case USB_EVENT_CTRLR_ATTACH:
    707 				case USB_EVENT_CTRLR_DETACH:
    708 					ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
    709 					break;
    710 
    711 				case USB_EVENT_DRIVER_ATTACH:
    712 				case USB_EVENT_DRIVER_DETACH:
    713 					ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
    714 					memcpy(ueo->u.ue_driver.ue_devname,
    715 					       ue->u.ue_driver.ue_devname,
    716 					       sizeof(ue->u.ue_driver.ue_devname));
    717 					break;
    718 				default:
    719 					;
    720 			}
    721 
    722 			error = uiomove((void *)ueo, sizeof(*ueo), uio);
    723 		} else
    724 #endif
    725 			error = uiomove((void *)ue, sizeof(*ue), uio);
    726 	}
    727 	usb_free_event(ue);
    728 #ifdef COMPAT_30
    729 	if (useold)
    730 		kmem_free(ueo, sizeof(struct usb_event_old));
    731 #endif
    732 
    733 	return error;
    734 }
    735 
    736 int
    737 usbclose(dev_t dev, int flag, int mode,
    738     struct lwp *l)
    739 {
    740 	int unit = minor(dev);
    741 
    742 	if (unit == USB_DEV_MINOR) {
    743 		mutex_enter(proc_lock);
    744 		usb_async_proc = 0;
    745 		mutex_exit(proc_lock);
    746 		usb_dev_open = 0;
    747 	}
    748 
    749 	return 0;
    750 }
    751 
    752 int
    753 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    754 {
    755 	struct usb_softc *sc;
    756 	int unit = minor(devt);
    757 
    758 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    759 
    760 	if (unit == USB_DEV_MINOR) {
    761 		switch (cmd) {
    762 		case FIONBIO:
    763 			/* All handled in the upper FS layer. */
    764 			return 0;
    765 
    766 		case FIOASYNC:
    767 			mutex_enter(proc_lock);
    768 			if (*(int *)data)
    769 				usb_async_proc = l->l_proc;
    770 			else
    771 				usb_async_proc = 0;
    772 			mutex_exit(proc_lock);
    773 			return 0;
    774 
    775 		default:
    776 			return EINVAL;
    777 		}
    778 	}
    779 
    780 	sc = device_lookup_private(&usb_cd, unit);
    781 
    782 	if (sc->sc_dying)
    783 		return EIO;
    784 
    785 	int error = 0;
    786 	DPRINTF("cmd %#jx", cmd, 0, 0, 0);
    787 	switch (cmd) {
    788 #ifdef USB_DEBUG
    789 	case USB_SETDEBUG:
    790 		if (!(flag & FWRITE))
    791 			return EBADF;
    792 		usbdebug  = ((*(int *)data) & 0x000000ff);
    793 		break;
    794 #endif /* USB_DEBUG */
    795 	case USB_REQUEST:
    796 	{
    797 		struct usb_ctl_request *ur = (void *)data;
    798 		int len = UGETW(ur->ucr_request.wLength);
    799 		struct iovec iov;
    800 		struct uio uio;
    801 		void *ptr = 0;
    802 		int addr = ur->ucr_addr;
    803 		usbd_status err;
    804 
    805 		if (!(flag & FWRITE)) {
    806 			error = EBADF;
    807 			goto fail;
    808 		}
    809 
    810 		DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
    811 		if (len < 0 || len > 32768) {
    812 			error = EINVAL;
    813 			goto fail;
    814 		}
    815 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    816 			error = EINVAL;
    817 			goto fail;
    818 		}
    819 		size_t dindex = usb_addr2dindex(addr);
    820 		if (sc->sc_bus->ub_devices[dindex] == NULL) {
    821 			error = EINVAL;
    822 			goto fail;
    823 		}
    824 		if (len != 0) {
    825 			iov.iov_base = (void *)ur->ucr_data;
    826 			iov.iov_len = len;
    827 			uio.uio_iov = &iov;
    828 			uio.uio_iovcnt = 1;
    829 			uio.uio_resid = len;
    830 			uio.uio_offset = 0;
    831 			uio.uio_rw =
    832 				ur->ucr_request.bmRequestType & UT_READ ?
    833 				UIO_READ : UIO_WRITE;
    834 			uio.uio_vmspace = l->l_proc->p_vmspace;
    835 			ptr = kmem_alloc(len, KM_SLEEP);
    836 			if (uio.uio_rw == UIO_WRITE) {
    837 				error = uiomove(ptr, len, &uio);
    838 				if (error)
    839 					goto ret;
    840 			}
    841 		}
    842 		err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
    843 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    844 			  USBD_DEFAULT_TIMEOUT);
    845 		if (err) {
    846 			error = EIO;
    847 			goto ret;
    848 		}
    849 		if (len > ur->ucr_actlen)
    850 			len = ur->ucr_actlen;
    851 		if (len != 0) {
    852 			if (uio.uio_rw == UIO_READ) {
    853 				error = uiomove(ptr, len, &uio);
    854 				if (error)
    855 					goto ret;
    856 			}
    857 		}
    858 	ret:
    859 		if (ptr) {
    860 			len = UGETW(ur->ucr_request.wLength);
    861 			kmem_free(ptr, len);
    862 		}
    863 		break;
    864 	}
    865 
    866 	case USB_DEVICEINFO:
    867 	{
    868 		struct usbd_device *dev;
    869 		struct usb_device_info *di = (void *)data;
    870 		int addr = di->udi_addr;
    871 
    872 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    873 			error = EINVAL;
    874 			goto fail;
    875 		}
    876 		size_t dindex = usb_addr2dindex(addr);
    877 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    878 			error = ENXIO;
    879 			goto fail;
    880 		}
    881 		usbd_fill_deviceinfo(dev, di, 1);
    882 		break;
    883 	}
    884 
    885 #ifdef COMPAT_30
    886 	case USB_DEVICEINFO_OLD:
    887 	{
    888 		struct usbd_device *dev;
    889 		struct usb_device_info_old *di = (void *)data;
    890 		int addr = di->udi_addr;
    891 
    892 		if (addr < 1 || addr >= USB_MAX_DEVICES) {
    893 			error = EINVAL;
    894 			goto fail;
    895 		}
    896 		size_t dindex = usb_addr2dindex(addr);
    897 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    898 			error = ENXIO;
    899 			goto fail;
    900 		}
    901 		usbd_fill_deviceinfo_old(dev, di, 1);
    902 		break;
    903 	}
    904 #endif
    905 
    906 	case USB_DEVICESTATS:
    907 		*(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
    908 		break;
    909 
    910 	default:
    911 		error = EINVAL;
    912 	}
    913 
    914 fail:
    915 
    916 	DPRINTF("... done (error = %jd)", error, 0, 0, 0);
    917 
    918 	return error;
    919 }
    920 
    921 int
    922 usbpoll(dev_t dev, int events, struct lwp *l)
    923 {
    924 	int revents, mask;
    925 
    926 	if (minor(dev) == USB_DEV_MINOR) {
    927 		revents = 0;
    928 		mask = POLLIN | POLLRDNORM;
    929 
    930 		mutex_enter(&usb_event_lock);
    931 		if (events & mask && usb_nevents > 0)
    932 			revents |= events & mask;
    933 		if (revents == 0 && events & mask)
    934 			selrecord(l, &usb_selevent);
    935 		mutex_exit(&usb_event_lock);
    936 
    937 		return revents;
    938 	} else {
    939 		return 0;
    940 	}
    941 }
    942 
    943 static void
    944 filt_usbrdetach(struct knote *kn)
    945 {
    946 
    947 	mutex_enter(&usb_event_lock);
    948 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    949 	mutex_exit(&usb_event_lock);
    950 }
    951 
    952 static int
    953 filt_usbread(struct knote *kn, long hint)
    954 {
    955 
    956 	if (usb_nevents == 0)
    957 		return 0;
    958 
    959 	kn->kn_data = sizeof(struct usb_event);
    960 	return 1;
    961 }
    962 
    963 static const struct filterops usbread_filtops = {
    964 	.f_isfd = 1,
    965 	.f_attach = NULL,
    966 	.f_detach = filt_usbrdetach,
    967 	.f_event = filt_usbread,
    968 };
    969 
    970 int
    971 usbkqfilter(dev_t dev, struct knote *kn)
    972 {
    973 	struct klist *klist;
    974 
    975 	switch (kn->kn_filter) {
    976 	case EVFILT_READ:
    977 		if (minor(dev) != USB_DEV_MINOR)
    978 			return 1;
    979 		klist = &usb_selevent.sel_klist;
    980 		kn->kn_fop = &usbread_filtops;
    981 		break;
    982 
    983 	default:
    984 		return EINVAL;
    985 	}
    986 
    987 	kn->kn_hook = NULL;
    988 
    989 	mutex_enter(&usb_event_lock);
    990 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    991 	mutex_exit(&usb_event_lock);
    992 
    993 	return 0;
    994 }
    995 
    996 /* Explore device tree from the root. */
    997 Static void
    998 usb_discover(struct usb_softc *sc)
    999 {
   1000 
   1001 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1002 
   1003 	KASSERT(mutex_owned(sc->sc_bus->ub_lock));
   1004 
   1005 	if (usb_noexplore > 1)
   1006 		return;
   1007 	/*
   1008 	 * We need mutual exclusion while traversing the device tree,
   1009 	 * but this is guaranteed since this function is only called
   1010 	 * from the event thread for the controller.
   1011 	 *
   1012 	 * Also, we now have sc_bus->ub_lock held.
   1013 	 */
   1014 	while (sc->sc_bus->ub_needsexplore && !sc->sc_dying) {
   1015 		sc->sc_bus->ub_needsexplore = 0;
   1016 		mutex_exit(sc->sc_bus->ub_lock);
   1017 		sc->sc_bus->ub_roothub->ud_hub->uh_explore(sc->sc_bus->ub_roothub);
   1018 		mutex_enter(sc->sc_bus->ub_lock);
   1019 	}
   1020 }
   1021 
   1022 void
   1023 usb_needs_explore(struct usbd_device *dev)
   1024 {
   1025 
   1026 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1027 
   1028 	mutex_enter(dev->ud_bus->ub_lock);
   1029 	dev->ud_bus->ub_needsexplore = 1;
   1030 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1031 	mutex_exit(dev->ud_bus->ub_lock);
   1032 }
   1033 
   1034 void
   1035 usb_needs_reattach(struct usbd_device *dev)
   1036 {
   1037 
   1038 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1039 
   1040 	mutex_enter(dev->ud_bus->ub_lock);
   1041 	dev->ud_powersrc->up_reattach = 1;
   1042 	dev->ud_bus->ub_needsexplore = 1;
   1043 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1044 	mutex_exit(dev->ud_bus->ub_lock);
   1045 }
   1046 
   1047 /* Called at with usb_event_lock held. */
   1048 int
   1049 usb_get_next_event(struct usb_event *ue)
   1050 {
   1051 	struct usb_event_q *ueq;
   1052 
   1053 	KASSERT(mutex_owned(&usb_event_lock));
   1054 
   1055 	if (usb_nevents <= 0)
   1056 		return 0;
   1057 	ueq = SIMPLEQ_FIRST(&usb_events);
   1058 #ifdef DIAGNOSTIC
   1059 	if (ueq == NULL) {
   1060 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
   1061 		usb_nevents = 0;
   1062 		return 0;
   1063 	}
   1064 #endif
   1065 	if (ue)
   1066 		*ue = ueq->ue;
   1067 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
   1068 	usb_free_event((struct usb_event *)(void *)ueq);
   1069 	usb_nevents--;
   1070 	return 1;
   1071 }
   1072 
   1073 void
   1074 usbd_add_dev_event(int type, struct usbd_device *udev)
   1075 {
   1076 	struct usb_event *ue = usb_alloc_event();
   1077 
   1078 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
   1079 	usb_add_event(type, ue);
   1080 }
   1081 
   1082 void
   1083 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
   1084 {
   1085 	struct usb_event *ue = usb_alloc_event();
   1086 
   1087 	ue->u.ue_driver.ue_cookie = udev->ud_cookie;
   1088 	strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
   1089 	    sizeof(ue->u.ue_driver.ue_devname));
   1090 	usb_add_event(type, ue);
   1091 }
   1092 
   1093 Static struct usb_event *
   1094 usb_alloc_event(void)
   1095 {
   1096 	/* Yes, this is right; we allocate enough so that we can use it later */
   1097 	return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
   1098 }
   1099 
   1100 Static void
   1101 usb_free_event(struct usb_event *uep)
   1102 {
   1103 	kmem_free(uep, sizeof(struct usb_event_q));
   1104 }
   1105 
   1106 Static void
   1107 usb_add_event(int type, struct usb_event *uep)
   1108 {
   1109 	struct usb_event_q *ueq;
   1110 	struct timeval thetime;
   1111 
   1112 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1113 
   1114 	microtime(&thetime);
   1115 	/* Don't want to wait here with usb_event_lock held */
   1116 	ueq = (struct usb_event_q *)(void *)uep;
   1117 	ueq->ue = *uep;
   1118 	ueq->ue.ue_type = type;
   1119 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
   1120 
   1121 	mutex_enter(&usb_event_lock);
   1122 	if (++usb_nevents >= USB_MAX_EVENTS) {
   1123 		/* Too many queued events, drop an old one. */
   1124 		DPRINTF("event dropped", 0, 0, 0, 0);
   1125 		(void)usb_get_next_event(0);
   1126 	}
   1127 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
   1128 	cv_signal(&usb_event_cv);
   1129 	selnotify(&usb_selevent, 0, 0);
   1130 	if (usb_async_proc != NULL) {
   1131 		kpreempt_disable();
   1132 		softint_schedule(usb_async_sih);
   1133 		kpreempt_enable();
   1134 	}
   1135 	mutex_exit(&usb_event_lock);
   1136 }
   1137 
   1138 Static void
   1139 usb_async_intr(void *cookie)
   1140 {
   1141 	proc_t *proc;
   1142 
   1143 	mutex_enter(proc_lock);
   1144 	if ((proc = usb_async_proc) != NULL)
   1145 		psignal(proc, SIGIO);
   1146 	mutex_exit(proc_lock);
   1147 }
   1148 
   1149 Static void
   1150 usb_soft_intr(void *arg)
   1151 {
   1152 	struct usbd_bus *bus = arg;
   1153 
   1154 	mutex_enter(bus->ub_lock);
   1155 	bus->ub_methods->ubm_softint(bus);
   1156 	mutex_exit(bus->ub_lock);
   1157 }
   1158 
   1159 void
   1160 usb_schedsoftintr(struct usbd_bus *bus)
   1161 {
   1162 
   1163 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1164 
   1165 	DPRINTFN(10, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
   1166 
   1167 	if (bus->ub_usepolling) {
   1168 		bus->ub_methods->ubm_softint(bus);
   1169 	} else {
   1170 		kpreempt_disable();
   1171 		softint_schedule(bus->ub_soft);
   1172 		kpreempt_enable();
   1173 	}
   1174 }
   1175 
   1176 int
   1177 usb_activate(device_t self, enum devact act)
   1178 {
   1179 	struct usb_softc *sc = device_private(self);
   1180 
   1181 	switch (act) {
   1182 	case DVACT_DEACTIVATE:
   1183 		sc->sc_dying = 1;
   1184 		return 0;
   1185 	default:
   1186 		return EOPNOTSUPP;
   1187 	}
   1188 }
   1189 
   1190 void
   1191 usb_childdet(device_t self, device_t child)
   1192 {
   1193 	int i;
   1194 	struct usb_softc *sc = device_private(self);
   1195 	struct usbd_device *dev;
   1196 
   1197 	if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
   1198 		return;
   1199 
   1200 	for (i = 0; i < dev->ud_subdevlen; i++)
   1201 		if (dev->ud_subdevs[i] == child)
   1202 			dev->ud_subdevs[i] = NULL;
   1203 }
   1204 
   1205 int
   1206 usb_detach(device_t self, int flags)
   1207 {
   1208 	struct usb_softc *sc = device_private(self);
   1209 	struct usb_event *ue;
   1210 	int rc;
   1211 
   1212 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1213 
   1214 	/* Make all devices disconnect. */
   1215 	if (sc->sc_port.up_dev != NULL &&
   1216 	    (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
   1217 		return rc;
   1218 
   1219 	pmf_device_deregister(self);
   1220 	/* Kill off event thread. */
   1221 	sc->sc_dying = 1;
   1222 	while (sc->sc_event_thread != NULL) {
   1223 		mutex_enter(sc->sc_bus->ub_lock);
   1224 		cv_signal(&sc->sc_bus->ub_needsexplore_cv);
   1225 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
   1226 		    sc->sc_bus->ub_lock, hz * 60);
   1227 		mutex_exit(sc->sc_bus->ub_lock);
   1228 	}
   1229 	DPRINTF("event thread dead", 0, 0, 0, 0);
   1230 
   1231 	if (sc->sc_bus->ub_soft != NULL) {
   1232 		softint_disestablish(sc->sc_bus->ub_soft);
   1233 		sc->sc_bus->ub_soft = NULL;
   1234 	}
   1235 
   1236 	ue = usb_alloc_event();
   1237 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
   1238 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1239 
   1240 	cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
   1241 
   1242 	return 0;
   1243 }
   1244 
   1245 #ifdef COMPAT_30
   1246 Static void
   1247 usb_copy_old_devinfo(struct usb_device_info_old *uo,
   1248 		     const struct usb_device_info *ue)
   1249 {
   1250 	const unsigned char *p;
   1251 	unsigned char *q;
   1252 	int i, n;
   1253 
   1254 	uo->udi_bus = ue->udi_bus;
   1255 	uo->udi_addr = ue->udi_addr;
   1256 	uo->udi_cookie = ue->udi_cookie;
   1257 	for (i = 0, p = (const unsigned char *)ue->udi_product,
   1258 	     q = (unsigned char *)uo->udi_product;
   1259 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1260 		if (*p < 0x80)
   1261 			q[i++] = *p;
   1262 		else {
   1263 			q[i++] = '?';
   1264 			if ((*p & 0xe0) == 0xe0)
   1265 				p++;
   1266 			p++;
   1267 		}
   1268 	}
   1269 	q[i] = 0;
   1270 
   1271 	for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
   1272 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1273 		if (* p < 0x80)
   1274 			q[i++] = *p;
   1275 		else {
   1276 			q[i++] = '?';
   1277 			p++;
   1278 			if ((*p & 0xe0) == 0xe0)
   1279 				p++;
   1280 		}
   1281 	}
   1282 	q[i] = 0;
   1283 
   1284 	memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
   1285 
   1286 	uo->udi_productNo = ue->udi_productNo;
   1287 	uo->udi_vendorNo = ue->udi_vendorNo;
   1288 	uo->udi_releaseNo = ue->udi_releaseNo;
   1289 	uo->udi_class = ue->udi_class;
   1290 	uo->udi_subclass = ue->udi_subclass;
   1291 	uo->udi_protocol = ue->udi_protocol;
   1292 	uo->udi_config = ue->udi_config;
   1293 	uo->udi_speed = ue->udi_speed;
   1294 	uo->udi_power = ue->udi_power;
   1295 	uo->udi_nports = ue->udi_nports;
   1296 
   1297 	for (n=0; n<USB_MAX_DEVNAMES; n++)
   1298 		memcpy(uo->udi_devnames[n],
   1299 		       ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
   1300 	memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
   1301 }
   1302 #endif
   1303