Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.172
      1 /*	$NetBSD: usb.c,v 1.172 2018/09/16 20:21:56 mrg 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.172 2018/09/16 20:21:56 mrg 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 	/*
    379 	 * Drop this reference after the first set of attachments in the
    380 	 * event thread.
    381 	 */
    382 	config_pending_incr(self);
    383 
    384 	if (!pmf_device_register(self, NULL, NULL))
    385 		aprint_error_dev(self, "couldn't establish power handler\n");
    386 
    387 	usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    388 	   usb_async_intr, NULL);
    389 
    390 	return;
    391 }
    392 
    393 void
    394 usb_create_event_thread(device_t self)
    395 {
    396 	struct usb_softc *sc = device_private(self);
    397 
    398 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    399 	    usb_event_thread, sc, &sc->sc_event_thread,
    400 	    "%s", device_xname(self))) {
    401 		printf("%s: unable to create event thread for\n",
    402 		       device_xname(self));
    403 		panic("usb_create_event_thread");
    404 	}
    405 }
    406 
    407 /*
    408  * Add a task to be performed by the task thread.  This function can be
    409  * called from any context and the task will be executed in a process
    410  * context ASAP.
    411  */
    412 void
    413 usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
    414 {
    415 	struct usb_taskq *taskq;
    416 
    417 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    418 
    419 	KASSERT(0 <= queue);
    420 	KASSERT(queue < USB_NUM_TASKQS);
    421 	taskq = &usb_taskq[queue];
    422 	mutex_enter(&taskq->lock);
    423 	if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
    424 	    USB_NUM_TASKQS) {
    425 		DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
    426 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    427 		cv_signal(&taskq->cv);
    428 	} else {
    429 		DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
    430 	}
    431 	mutex_exit(&taskq->lock);
    432 }
    433 
    434 /*
    435  * usb_rem_task(dev, task)
    436  *
    437  *	If task is queued to run, remove it from the queue.
    438  *
    439  *	Caller is _not_ guaranteed that the task is not running when
    440  *	this is done.
    441  *
    442  *	Never sleeps.
    443  */
    444 void
    445 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
    446 {
    447 	unsigned queue;
    448 
    449 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    450 
    451 	while ((queue = task->queue) != USB_NUM_TASKQS) {
    452 		struct usb_taskq *taskq = &usb_taskq[queue];
    453 		mutex_enter(&taskq->lock);
    454 		if (__predict_true(task->queue == queue)) {
    455 			TAILQ_REMOVE(&taskq->tasks, task, next);
    456 			task->queue = USB_NUM_TASKQS;
    457 			mutex_exit(&taskq->lock);
    458 			break;
    459 		}
    460 		mutex_exit(&taskq->lock);
    461 	}
    462 }
    463 
    464 /*
    465  * usb_rem_task_wait(dev, task, queue, interlock)
    466  *
    467  *	If task is scheduled to run, remove it from the queue.  If it
    468  *	may have already begun to run, drop interlock if not null, wait
    469  *	for it to complete, and reacquire interlock if not null.
    470  *	Return true if it successfully removed the task from the queue,
    471  *	false if not.
    472  *
    473  *	Caller MUST guarantee that task will not be scheduled on a
    474  *	_different_ queue, at least until after this returns.
    475  *
    476  *	If caller guarantees that task will not be scheduled on the
    477  *	same queue before this returns, then caller is guaranteed that
    478  *	the task is not running at all when this returns.
    479  *
    480  *	May sleep.
    481  */
    482 bool
    483 usb_rem_task_wait(struct usbd_device *dev, struct usb_task *task, int queue,
    484     kmutex_t *interlock)
    485 {
    486 	struct usb_taskq *taskq;
    487 	int queue1;
    488 	bool removed;
    489 
    490 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    491 	ASSERT_SLEEPABLE();
    492 	KASSERT(0 <= queue);
    493 	KASSERT(queue < USB_NUM_TASKQS);
    494 
    495 	taskq = &usb_taskq[queue];
    496 	mutex_enter(&taskq->lock);
    497 	queue1 = task->queue;
    498 	if (queue1 == USB_NUM_TASKQS) {
    499 		/*
    500 		 * It is not on the queue.  It may be about to run, or
    501 		 * it may have already finished running -- there is no
    502 		 * stopping it now.  Wait for it if it is running.
    503 		 */
    504 		if (interlock)
    505 			mutex_exit(interlock);
    506 		while (taskq->current_task == task)
    507 			cv_wait(&taskq->cv, &taskq->lock);
    508 		removed = false;
    509 	} else {
    510 		/*
    511 		 * It is still on the queue.  We can stop it before the
    512 		 * task thread will run it.
    513 		 */
    514 		KASSERTMSG(queue1 == queue, "task %p on q%d expected on q%d",
    515 		    task, queue1, queue);
    516 		TAILQ_REMOVE(&taskq->tasks, task, next);
    517 		task->queue = USB_NUM_TASKQS;
    518 		removed = true;
    519 	}
    520 	mutex_exit(&taskq->lock);
    521 
    522 	/*
    523 	 * If there's an interlock, and we dropped it to wait,
    524 	 * reacquire it.
    525 	 */
    526 	if (interlock && !removed)
    527 		mutex_enter(interlock);
    528 
    529 	return removed;
    530 }
    531 
    532 void
    533 usb_event_thread(void *arg)
    534 {
    535 	struct usb_softc *sc = arg;
    536 	struct usbd_bus *bus = sc->sc_bus;
    537 
    538 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    539 
    540 	/*
    541 	 * In case this controller is a companion controller to an
    542 	 * EHCI controller we need to wait until the EHCI controller
    543 	 * has grabbed the port.
    544 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    545 	 * know how to synchronize the creation of the threads so it
    546 	 * will work.
    547 	 */
    548 	usb_delay_ms(bus, 500);
    549 
    550 	/* Make sure first discover does something. */
    551 	mutex_enter(bus->ub_lock);
    552 	sc->sc_bus->ub_needsexplore = 1;
    553 	usb_discover(sc);
    554 	mutex_exit(bus->ub_lock);
    555 
    556 	/* Drop the config_pending reference from attach. */
    557 	config_pending_decr(bus->ub_usbctl);
    558 
    559 	mutex_enter(bus->ub_lock);
    560 	while (!sc->sc_dying) {
    561 #if 0 /* not yet */
    562 		while (sc->sc_bus->ub_usepolling)
    563 			kpause("usbpoll", true, hz, bus->ub_lock);
    564 #endif
    565 
    566 		if (usb_noexplore < 2)
    567 			usb_discover(sc);
    568 
    569 		cv_timedwait(&bus->ub_needsexplore_cv,
    570 		    bus->ub_lock, usb_noexplore ? 0 : hz * 60);
    571 
    572 		DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
    573 	}
    574 	sc->sc_event_thread = NULL;
    575 
    576 	/* In case parent is waiting for us to exit. */
    577 	cv_signal(&bus->ub_needsexplore_cv);
    578 	mutex_exit(bus->ub_lock);
    579 
    580 	DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
    581 	kthread_exit(0);
    582 }
    583 
    584 void
    585 usb_task_thread(void *arg)
    586 {
    587 	struct usb_task *task;
    588 	struct usb_taskq *taskq;
    589 	bool mpsafe;
    590 
    591 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    592 
    593 	taskq = arg;
    594 	DPRINTF("start taskq %#jx", (uintptr_t)taskq, 0, 0, 0);
    595 
    596 	mutex_enter(&taskq->lock);
    597 	for (;;) {
    598 		task = TAILQ_FIRST(&taskq->tasks);
    599 		if (task == NULL) {
    600 			cv_wait(&taskq->cv, &taskq->lock);
    601 			task = TAILQ_FIRST(&taskq->tasks);
    602 		}
    603 		DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
    604 		if (task != NULL) {
    605 			mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
    606 			TAILQ_REMOVE(&taskq->tasks, task, next);
    607 			task->queue = USB_NUM_TASKQS;
    608 			taskq->current_task = task;
    609 			mutex_exit(&taskq->lock);
    610 
    611 			if (!mpsafe)
    612 				KERNEL_LOCK(1, curlwp);
    613 			task->fun(task->arg);
    614 			/* Can't dereference task after this point.  */
    615 			if (!mpsafe)
    616 				KERNEL_UNLOCK_ONE(curlwp);
    617 
    618 			mutex_enter(&taskq->lock);
    619 			KASSERTMSG(taskq->current_task == task,
    620 			    "somebody scribbled on usb taskq %p", taskq);
    621 			taskq->current_task = NULL;
    622 			cv_broadcast(&taskq->cv);
    623 		}
    624 	}
    625 	mutex_exit(&taskq->lock);
    626 }
    627 
    628 int
    629 usbctlprint(void *aux, const char *pnp)
    630 {
    631 	/* only "usb"es can attach to host controllers */
    632 	if (pnp)
    633 		aprint_normal("usb at %s", pnp);
    634 
    635 	return UNCONF;
    636 }
    637 
    638 int
    639 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    640 {
    641 	int unit = minor(dev);
    642 	struct usb_softc *sc;
    643 
    644 	if (unit == USB_DEV_MINOR) {
    645 		if (usb_dev_open)
    646 			return EBUSY;
    647 		usb_dev_open = 1;
    648 		mutex_enter(proc_lock);
    649 		usb_async_proc = 0;
    650 		mutex_exit(proc_lock);
    651 		return 0;
    652 	}
    653 
    654 	sc = device_lookup_private(&usb_cd, unit);
    655 	if (!sc)
    656 		return ENXIO;
    657 
    658 	if (sc->sc_dying)
    659 		return EIO;
    660 
    661 	return 0;
    662 }
    663 
    664 int
    665 usbread(dev_t dev, struct uio *uio, int flag)
    666 {
    667 	struct usb_event *ue;
    668 #ifdef COMPAT_30
    669 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    670 	int useold = 0;
    671 #endif
    672 	int error, n;
    673 
    674 	if (minor(dev) != USB_DEV_MINOR)
    675 		return ENXIO;
    676 
    677 	switch (uio->uio_resid) {
    678 #ifdef COMPAT_30
    679 	case sizeof(struct usb_event_old):
    680 		ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
    681 		useold = 1;
    682 		/* FALLTHRU */
    683 #endif
    684 	case sizeof(struct usb_event):
    685 		ue = usb_alloc_event();
    686 		break;
    687 	default:
    688 		return EINVAL;
    689 	}
    690 
    691 	error = 0;
    692 	mutex_enter(&usb_event_lock);
    693 	for (;;) {
    694 		n = usb_get_next_event(ue);
    695 		if (n != 0)
    696 			break;
    697 		if (flag & IO_NDELAY) {
    698 			error = EWOULDBLOCK;
    699 			break;
    700 		}
    701 		error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
    702 		if (error)
    703 			break;
    704 	}
    705 	mutex_exit(&usb_event_lock);
    706 	if (!error) {
    707 #ifdef COMPAT_30
    708 		if (useold) { /* copy fields to old struct */
    709 			ueo->ue_type = ue->ue_type;
    710 			memcpy(&ueo->ue_time, &ue->ue_time,
    711 			      sizeof(struct timespec));
    712 			switch (ue->ue_type) {
    713 				case USB_EVENT_DEVICE_ATTACH:
    714 				case USB_EVENT_DEVICE_DETACH:
    715 					usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
    716 					break;
    717 
    718 				case USB_EVENT_CTRLR_ATTACH:
    719 				case USB_EVENT_CTRLR_DETACH:
    720 					ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
    721 					break;
    722 
    723 				case USB_EVENT_DRIVER_ATTACH:
    724 				case USB_EVENT_DRIVER_DETACH:
    725 					ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
    726 					memcpy(ueo->u.ue_driver.ue_devname,
    727 					       ue->u.ue_driver.ue_devname,
    728 					       sizeof(ue->u.ue_driver.ue_devname));
    729 					break;
    730 				default:
    731 					;
    732 			}
    733 
    734 			error = uiomove((void *)ueo, sizeof(*ueo), uio);
    735 		} else
    736 #endif
    737 			error = uiomove((void *)ue, sizeof(*ue), uio);
    738 	}
    739 	usb_free_event(ue);
    740 #ifdef COMPAT_30
    741 	if (useold)
    742 		kmem_free(ueo, sizeof(struct usb_event_old));
    743 #endif
    744 
    745 	return error;
    746 }
    747 
    748 int
    749 usbclose(dev_t dev, int flag, int mode,
    750     struct lwp *l)
    751 {
    752 	int unit = minor(dev);
    753 
    754 	if (unit == USB_DEV_MINOR) {
    755 		mutex_enter(proc_lock);
    756 		usb_async_proc = 0;
    757 		mutex_exit(proc_lock);
    758 		usb_dev_open = 0;
    759 	}
    760 
    761 	return 0;
    762 }
    763 
    764 int
    765 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    766 {
    767 	struct usb_softc *sc;
    768 	int unit = minor(devt);
    769 
    770 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    771 
    772 	if (unit == USB_DEV_MINOR) {
    773 		switch (cmd) {
    774 		case FIONBIO:
    775 			/* All handled in the upper FS layer. */
    776 			return 0;
    777 
    778 		case FIOASYNC:
    779 			mutex_enter(proc_lock);
    780 			if (*(int *)data)
    781 				usb_async_proc = l->l_proc;
    782 			else
    783 				usb_async_proc = 0;
    784 			mutex_exit(proc_lock);
    785 			return 0;
    786 
    787 		default:
    788 			return EINVAL;
    789 		}
    790 	}
    791 
    792 	sc = device_lookup_private(&usb_cd, unit);
    793 
    794 	if (sc->sc_dying)
    795 		return EIO;
    796 
    797 	int error = 0;
    798 	DPRINTF("cmd %#jx", cmd, 0, 0, 0);
    799 	switch (cmd) {
    800 #ifdef USB_DEBUG
    801 	case USB_SETDEBUG:
    802 		if (!(flag & FWRITE))
    803 			return EBADF;
    804 		usbdebug  = ((*(int *)data) & 0x000000ff);
    805 		break;
    806 #endif /* USB_DEBUG */
    807 	case USB_REQUEST:
    808 	{
    809 		struct usb_ctl_request *ur = (void *)data;
    810 		int len = UGETW(ur->ucr_request.wLength);
    811 		struct iovec iov;
    812 		struct uio uio;
    813 		void *ptr = 0;
    814 		int addr = ur->ucr_addr;
    815 		usbd_status err;
    816 
    817 		if (!(flag & FWRITE)) {
    818 			error = EBADF;
    819 			goto fail;
    820 		}
    821 
    822 		DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
    823 		if (len < 0 || len > 32768) {
    824 			error = EINVAL;
    825 			goto fail;
    826 		}
    827 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    828 			error = EINVAL;
    829 			goto fail;
    830 		}
    831 		size_t dindex = usb_addr2dindex(addr);
    832 		if (sc->sc_bus->ub_devices[dindex] == NULL) {
    833 			error = EINVAL;
    834 			goto fail;
    835 		}
    836 		if (len != 0) {
    837 			iov.iov_base = (void *)ur->ucr_data;
    838 			iov.iov_len = len;
    839 			uio.uio_iov = &iov;
    840 			uio.uio_iovcnt = 1;
    841 			uio.uio_resid = len;
    842 			uio.uio_offset = 0;
    843 			uio.uio_rw =
    844 				ur->ucr_request.bmRequestType & UT_READ ?
    845 				UIO_READ : UIO_WRITE;
    846 			uio.uio_vmspace = l->l_proc->p_vmspace;
    847 			ptr = kmem_alloc(len, KM_SLEEP);
    848 			if (uio.uio_rw == UIO_WRITE) {
    849 				error = uiomove(ptr, len, &uio);
    850 				if (error)
    851 					goto ret;
    852 			}
    853 		}
    854 		err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
    855 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    856 			  USBD_DEFAULT_TIMEOUT);
    857 		if (err) {
    858 			error = EIO;
    859 			goto ret;
    860 		}
    861 		if (len > ur->ucr_actlen)
    862 			len = ur->ucr_actlen;
    863 		if (len != 0) {
    864 			if (uio.uio_rw == UIO_READ) {
    865 				error = uiomove(ptr, len, &uio);
    866 				if (error)
    867 					goto ret;
    868 			}
    869 		}
    870 	ret:
    871 		if (ptr) {
    872 			len = UGETW(ur->ucr_request.wLength);
    873 			kmem_free(ptr, len);
    874 		}
    875 		break;
    876 	}
    877 
    878 	case USB_DEVICEINFO:
    879 	{
    880 		struct usbd_device *dev;
    881 		struct usb_device_info *di = (void *)data;
    882 		int addr = di->udi_addr;
    883 
    884 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    885 			error = EINVAL;
    886 			goto fail;
    887 		}
    888 		size_t dindex = usb_addr2dindex(addr);
    889 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    890 			error = ENXIO;
    891 			goto fail;
    892 		}
    893 		usbd_fill_deviceinfo(dev, di, 1);
    894 		break;
    895 	}
    896 
    897 #ifdef COMPAT_30
    898 	case USB_DEVICEINFO_OLD:
    899 	{
    900 		struct usbd_device *dev;
    901 		struct usb_device_info_old *di = (void *)data;
    902 		int addr = di->udi_addr;
    903 
    904 		if (addr < 1 || addr >= USB_MAX_DEVICES) {
    905 			error = EINVAL;
    906 			goto fail;
    907 		}
    908 		size_t dindex = usb_addr2dindex(addr);
    909 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    910 			error = ENXIO;
    911 			goto fail;
    912 		}
    913 		usbd_fill_deviceinfo_old(dev, di, 1);
    914 		break;
    915 	}
    916 #endif
    917 
    918 	case USB_DEVICESTATS:
    919 		*(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
    920 		break;
    921 
    922 	default:
    923 		error = EINVAL;
    924 	}
    925 
    926 fail:
    927 
    928 	DPRINTF("... done (error = %jd)", error, 0, 0, 0);
    929 
    930 	return error;
    931 }
    932 
    933 int
    934 usbpoll(dev_t dev, int events, struct lwp *l)
    935 {
    936 	int revents, mask;
    937 
    938 	if (minor(dev) == USB_DEV_MINOR) {
    939 		revents = 0;
    940 		mask = POLLIN | POLLRDNORM;
    941 
    942 		mutex_enter(&usb_event_lock);
    943 		if (events & mask && usb_nevents > 0)
    944 			revents |= events & mask;
    945 		if (revents == 0 && events & mask)
    946 			selrecord(l, &usb_selevent);
    947 		mutex_exit(&usb_event_lock);
    948 
    949 		return revents;
    950 	} else {
    951 		return 0;
    952 	}
    953 }
    954 
    955 static void
    956 filt_usbrdetach(struct knote *kn)
    957 {
    958 
    959 	mutex_enter(&usb_event_lock);
    960 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    961 	mutex_exit(&usb_event_lock);
    962 }
    963 
    964 static int
    965 filt_usbread(struct knote *kn, long hint)
    966 {
    967 
    968 	if (usb_nevents == 0)
    969 		return 0;
    970 
    971 	kn->kn_data = sizeof(struct usb_event);
    972 	return 1;
    973 }
    974 
    975 static const struct filterops usbread_filtops = {
    976 	.f_isfd = 1,
    977 	.f_attach = NULL,
    978 	.f_detach = filt_usbrdetach,
    979 	.f_event = filt_usbread,
    980 };
    981 
    982 int
    983 usbkqfilter(dev_t dev, struct knote *kn)
    984 {
    985 	struct klist *klist;
    986 
    987 	switch (kn->kn_filter) {
    988 	case EVFILT_READ:
    989 		if (minor(dev) != USB_DEV_MINOR)
    990 			return 1;
    991 		klist = &usb_selevent.sel_klist;
    992 		kn->kn_fop = &usbread_filtops;
    993 		break;
    994 
    995 	default:
    996 		return EINVAL;
    997 	}
    998 
    999 	kn->kn_hook = NULL;
   1000 
   1001 	mutex_enter(&usb_event_lock);
   1002 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1003 	mutex_exit(&usb_event_lock);
   1004 
   1005 	return 0;
   1006 }
   1007 
   1008 /* Explore device tree from the root. */
   1009 Static void
   1010 usb_discover(struct usb_softc *sc)
   1011 {
   1012 	struct usbd_bus *bus = sc->sc_bus;
   1013 
   1014 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1015 
   1016 	KASSERT(mutex_owned(bus->ub_lock));
   1017 
   1018 	if (usb_noexplore > 1)
   1019 		return;
   1020 
   1021 	/*
   1022 	 * We need mutual exclusion while traversing the device tree,
   1023 	 * but this is guaranteed since this function is only called
   1024 	 * from the event thread for the controller.
   1025 	 *
   1026 	 * Also, we now have bus->ub_lock held, and in combination
   1027 	 * with ub_exploring, avoids interferring with polling.
   1028 	 */
   1029 	while (bus->ub_needsexplore && !sc->sc_dying) {
   1030 		bus->ub_needsexplore = 0;
   1031 		mutex_exit(sc->sc_bus->ub_lock);
   1032 		bus->ub_roothub->ud_hub->uh_explore(bus->ub_roothub);
   1033 		mutex_enter(bus->ub_lock);
   1034 	}
   1035 }
   1036 
   1037 void
   1038 usb_needs_explore(struct usbd_device *dev)
   1039 {
   1040 
   1041 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1042 
   1043 	mutex_enter(dev->ud_bus->ub_lock);
   1044 	dev->ud_bus->ub_needsexplore = 1;
   1045 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1046 	mutex_exit(dev->ud_bus->ub_lock);
   1047 }
   1048 
   1049 void
   1050 usb_needs_reattach(struct usbd_device *dev)
   1051 {
   1052 
   1053 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1054 
   1055 	mutex_enter(dev->ud_bus->ub_lock);
   1056 	dev->ud_powersrc->up_reattach = 1;
   1057 	dev->ud_bus->ub_needsexplore = 1;
   1058 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1059 	mutex_exit(dev->ud_bus->ub_lock);
   1060 }
   1061 
   1062 /* Called at with usb_event_lock held. */
   1063 int
   1064 usb_get_next_event(struct usb_event *ue)
   1065 {
   1066 	struct usb_event_q *ueq;
   1067 
   1068 	KASSERT(mutex_owned(&usb_event_lock));
   1069 
   1070 	if (usb_nevents <= 0)
   1071 		return 0;
   1072 	ueq = SIMPLEQ_FIRST(&usb_events);
   1073 #ifdef DIAGNOSTIC
   1074 	if (ueq == NULL) {
   1075 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
   1076 		usb_nevents = 0;
   1077 		return 0;
   1078 	}
   1079 #endif
   1080 	if (ue)
   1081 		*ue = ueq->ue;
   1082 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
   1083 	usb_free_event((struct usb_event *)(void *)ueq);
   1084 	usb_nevents--;
   1085 	return 1;
   1086 }
   1087 
   1088 void
   1089 usbd_add_dev_event(int type, struct usbd_device *udev)
   1090 {
   1091 	struct usb_event *ue = usb_alloc_event();
   1092 
   1093 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
   1094 	usb_add_event(type, ue);
   1095 }
   1096 
   1097 void
   1098 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
   1099 {
   1100 	struct usb_event *ue = usb_alloc_event();
   1101 
   1102 	ue->u.ue_driver.ue_cookie = udev->ud_cookie;
   1103 	strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
   1104 	    sizeof(ue->u.ue_driver.ue_devname));
   1105 	usb_add_event(type, ue);
   1106 }
   1107 
   1108 Static struct usb_event *
   1109 usb_alloc_event(void)
   1110 {
   1111 	/* Yes, this is right; we allocate enough so that we can use it later */
   1112 	return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
   1113 }
   1114 
   1115 Static void
   1116 usb_free_event(struct usb_event *uep)
   1117 {
   1118 	kmem_free(uep, sizeof(struct usb_event_q));
   1119 }
   1120 
   1121 Static void
   1122 usb_add_event(int type, struct usb_event *uep)
   1123 {
   1124 	struct usb_event_q *ueq;
   1125 	struct timeval thetime;
   1126 
   1127 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1128 
   1129 	microtime(&thetime);
   1130 	/* Don't want to wait here with usb_event_lock held */
   1131 	ueq = (struct usb_event_q *)(void *)uep;
   1132 	ueq->ue = *uep;
   1133 	ueq->ue.ue_type = type;
   1134 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
   1135 
   1136 	mutex_enter(&usb_event_lock);
   1137 	if (++usb_nevents >= USB_MAX_EVENTS) {
   1138 		/* Too many queued events, drop an old one. */
   1139 		DPRINTF("event dropped", 0, 0, 0, 0);
   1140 		(void)usb_get_next_event(0);
   1141 	}
   1142 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
   1143 	cv_signal(&usb_event_cv);
   1144 	selnotify(&usb_selevent, 0, 0);
   1145 	if (usb_async_proc != NULL) {
   1146 		kpreempt_disable();
   1147 		softint_schedule(usb_async_sih);
   1148 		kpreempt_enable();
   1149 	}
   1150 	mutex_exit(&usb_event_lock);
   1151 }
   1152 
   1153 Static void
   1154 usb_async_intr(void *cookie)
   1155 {
   1156 	proc_t *proc;
   1157 
   1158 	mutex_enter(proc_lock);
   1159 	if ((proc = usb_async_proc) != NULL)
   1160 		psignal(proc, SIGIO);
   1161 	mutex_exit(proc_lock);
   1162 }
   1163 
   1164 Static void
   1165 usb_soft_intr(void *arg)
   1166 {
   1167 	struct usbd_bus *bus = arg;
   1168 
   1169 	mutex_enter(bus->ub_lock);
   1170 	bus->ub_methods->ubm_softint(bus);
   1171 	mutex_exit(bus->ub_lock);
   1172 }
   1173 
   1174 void
   1175 usb_schedsoftintr(struct usbd_bus *bus)
   1176 {
   1177 
   1178 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1179 
   1180 	DPRINTFN(10, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
   1181 
   1182 	if (bus->ub_usepolling) {
   1183 		bus->ub_methods->ubm_softint(bus);
   1184 	} else {
   1185 		kpreempt_disable();
   1186 		softint_schedule(bus->ub_soft);
   1187 		kpreempt_enable();
   1188 	}
   1189 }
   1190 
   1191 int
   1192 usb_activate(device_t self, enum devact act)
   1193 {
   1194 	struct usb_softc *sc = device_private(self);
   1195 
   1196 	switch (act) {
   1197 	case DVACT_DEACTIVATE:
   1198 		sc->sc_dying = 1;
   1199 		return 0;
   1200 	default:
   1201 		return EOPNOTSUPP;
   1202 	}
   1203 }
   1204 
   1205 void
   1206 usb_childdet(device_t self, device_t child)
   1207 {
   1208 	int i;
   1209 	struct usb_softc *sc = device_private(self);
   1210 	struct usbd_device *dev;
   1211 
   1212 	if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
   1213 		return;
   1214 
   1215 	for (i = 0; i < dev->ud_subdevlen; i++)
   1216 		if (dev->ud_subdevs[i] == child)
   1217 			dev->ud_subdevs[i] = NULL;
   1218 }
   1219 
   1220 int
   1221 usb_detach(device_t self, int flags)
   1222 {
   1223 	struct usb_softc *sc = device_private(self);
   1224 	struct usb_event *ue;
   1225 	int rc;
   1226 
   1227 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1228 
   1229 	/* Make all devices disconnect. */
   1230 	if (sc->sc_port.up_dev != NULL &&
   1231 	    (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
   1232 		return rc;
   1233 
   1234 	pmf_device_deregister(self);
   1235 	/* Kill off event thread. */
   1236 	sc->sc_dying = 1;
   1237 	while (sc->sc_event_thread != NULL) {
   1238 		mutex_enter(sc->sc_bus->ub_lock);
   1239 		cv_signal(&sc->sc_bus->ub_needsexplore_cv);
   1240 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
   1241 		    sc->sc_bus->ub_lock, hz * 60);
   1242 		mutex_exit(sc->sc_bus->ub_lock);
   1243 	}
   1244 	DPRINTF("event thread dead", 0, 0, 0, 0);
   1245 
   1246 	if (sc->sc_bus->ub_soft != NULL) {
   1247 		softint_disestablish(sc->sc_bus->ub_soft);
   1248 		sc->sc_bus->ub_soft = NULL;
   1249 	}
   1250 
   1251 	ue = usb_alloc_event();
   1252 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
   1253 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1254 
   1255 	cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
   1256 
   1257 	return 0;
   1258 }
   1259 
   1260 #ifdef COMPAT_30
   1261 Static void
   1262 usb_copy_old_devinfo(struct usb_device_info_old *uo,
   1263 		     const struct usb_device_info *ue)
   1264 {
   1265 	const unsigned char *p;
   1266 	unsigned char *q;
   1267 	int i, n;
   1268 
   1269 	uo->udi_bus = ue->udi_bus;
   1270 	uo->udi_addr = ue->udi_addr;
   1271 	uo->udi_cookie = ue->udi_cookie;
   1272 	for (i = 0, p = (const unsigned char *)ue->udi_product,
   1273 	     q = (unsigned char *)uo->udi_product;
   1274 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1275 		if (*p < 0x80)
   1276 			q[i++] = *p;
   1277 		else {
   1278 			q[i++] = '?';
   1279 			if ((*p & 0xe0) == 0xe0)
   1280 				p++;
   1281 			p++;
   1282 		}
   1283 	}
   1284 	q[i] = 0;
   1285 
   1286 	for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
   1287 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1288 		if (* p < 0x80)
   1289 			q[i++] = *p;
   1290 		else {
   1291 			q[i++] = '?';
   1292 			p++;
   1293 			if ((*p & 0xe0) == 0xe0)
   1294 				p++;
   1295 		}
   1296 	}
   1297 	q[i] = 0;
   1298 
   1299 	memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
   1300 
   1301 	uo->udi_productNo = ue->udi_productNo;
   1302 	uo->udi_vendorNo = ue->udi_vendorNo;
   1303 	uo->udi_releaseNo = ue->udi_releaseNo;
   1304 	uo->udi_class = ue->udi_class;
   1305 	uo->udi_subclass = ue->udi_subclass;
   1306 	uo->udi_protocol = ue->udi_protocol;
   1307 	uo->udi_config = ue->udi_config;
   1308 	uo->udi_speed = ue->udi_speed;
   1309 	uo->udi_power = ue->udi_power;
   1310 	uo->udi_nports = ue->udi_nports;
   1311 
   1312 	for (n=0; n<USB_MAX_DEVNAMES; n++)
   1313 		memcpy(uo->udi_devnames[n],
   1314 		       ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
   1315 	memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
   1316 }
   1317 #endif
   1318