Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.95.4.1
      1 /*	$NetBSD: usb.c,v 1.95.4.1 2007/05/22 14:57:46 itohy 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.95.4.1 2007/05/22 14:57:46 itohy Exp $");
     48 /* __FBSDID("$FreeBSD: src/sys/dev/usb/usb.c,v 1.111 2006/10/19 01:15:58 iedowse Exp $"); */
     49 
     50 #if defined(__NetBSD__)
     51 #include "opt_compat_netbsd.h"
     52 #include "ohci.h"
     53 #include "uhci.h"
     54 #endif
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/kernel.h>
     59 #include <sys/lock.h>
     60 #include <sys/malloc.h>
     61 #include <sys/fcntl.h>
     62 #if defined(__NetBSD__) || defined(__OpenBSD__)
     63 #include <sys/device.h>
     64 #include <sys/vnode.h>
     65 #elif defined(__FreeBSD__)
     66 #if __FreeBSD_version >= 500000
     67 #include <sys/mutex.h>
     68 #endif
     69 #include <sys/unistd.h>
     70 #include <sys/module.h>
     71 #include <sys/bus.h>
     72 #include <sys/filio.h>
     73 #include <sys/uio.h>
     74 #endif
     75 #include <sys/kthread.h>
     76 #include <sys/proc.h>
     77 #include <sys/conf.h>
     78 #include <sys/poll.h>
     79 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
     80 #include <sys/selinfo.h>
     81 #else
     82 #include <sys/select.h>
     83 #endif
     84 #include <sys/signalvar.h>
     85 #include <sys/sysctl.h>
     86 #include <sys/uio.h>
     87 
     88 #include <dev/usb/usb.h>
     89 #include <dev/usb/usbdi.h>
     90 #include <dev/usb/usbdi_util.h>
     91 
     92 #define USBUNIT(d)	(minor(d))	/* usb_discover device nodes, kthread */
     93 #define USB_DEV_MINOR	255		/* event queue device */
     94 
     95 MALLOC_DEFINE(M_USB, "USB", "USB");
     96 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
     97 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
     98 
     99 #if defined(__FreeBSD__)
    100 #include "usb_if.h"
    101 #endif /* defined(__FreeBSD__) */
    102 
    103 #include <machine/bus.h>
    104 
    105 #include <dev/usb/usbdivar.h>
    106 #include <dev/usb/usb_quirks.h>
    107 
    108 #ifdef USB_DEBUG
    109 #define DPRINTF(x)	if (usbdebug) logprintf x
    110 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
    111 int	usbdebug = 0;
    112 #if defined(UHCI_DEBUG) && NUHCI > 0
    113 extern int	uhcidebug;
    114 #endif
    115 #if defined(OHCI_DEBUG) && NOHCI > 0
    116 extern int	ohcidebug;
    117 #endif
    118 /*
    119  * 0  - do usual exploration
    120  * 1  - do not use timeout exploration
    121  * >1 - do no exploration
    122  */
    123 int	usb_noexplore = 0;
    124 #else
    125 #define DPRINTF(x)
    126 #define DPRINTFN(n,x)
    127 #endif
    128 
    129 #ifdef __FreeBSD__
    130 /* Define this unconditionally in case a kernel module is loaded that
    131  * has been compiled with debugging options.
    132  */
    133 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
    134 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
    135 	   &usbdebug, 0, "usb debug level");
    136 #endif
    137 
    138 struct usb_softc {
    139 	USBBASEDEVICE	sc_dev;		/* base device */
    140 #ifdef __FreeBSD__
    141 	struct cdev	*sc_usbdev;	/* /dev/usbN device */
    142 	TAILQ_ENTRY(usb_softc) sc_coldexplist; /* cold needs-explore list */
    143 #endif
    144 	usbd_bus_handle sc_bus;		/* USB controller */
    145 	struct usbd_port sc_port;	/* dummy port for root hub */
    146 
    147 	struct proc	*sc_event_thread;
    148 
    149 	char		sc_dying;
    150 };
    151 
    152 struct usb_taskq {
    153 	TAILQ_HEAD(, usb_task) tasks;
    154 	struct proc *task_thread_proc;
    155 	const char *name;
    156 	int taskcreated;	/* task thread exists. */
    157 };
    158 
    159 static struct usb_taskq usb_taskq[USB_NUM_TASKQS] = {
    160 	{ TAILQ_HEAD_INITIALIZER(usb_taskq[0].tasks), NULL, NULL, 0 },
    161 	{ TAILQ_HEAD_INITIALIZER(usb_taskq[1].tasks), NULL, NULL, 0 }
    162 };
    163 
    164 #if defined(__NetBSD__) || defined(__OpenBSD__)
    165 dev_type_open(usbopen);
    166 dev_type_close(usbclose);
    167 dev_type_read(usbread);
    168 dev_type_ioctl(usbioctl);
    169 dev_type_poll(usbpoll);
    170 dev_type_kqfilter(usbkqfilter);
    171 
    172 const struct cdevsw usb_cdevsw = {
    173 	usbopen, usbclose, usbread, nowrite, usbioctl,
    174 	nostop, notty, usbpoll, nommap, usbkqfilter, D_OTHER,
    175 };
    176 #elif defined(__FreeBSD__)
    177 d_open_t  usbopen;
    178 d_close_t usbclose;
    179 d_read_t usbread;
    180 d_ioctl_t usbioctl;
    181 d_poll_t usbpoll;
    182 
    183 struct cdevsw usb_cdevsw = {
    184 	.d_version =	D_VERSION,
    185 	.d_flags =	D_NEEDGIANT,
    186 	.d_open =	usbopen,
    187 	.d_close =	usbclose,
    188 	.d_read =	usbread,
    189 	.d_ioctl =	usbioctl,
    190 	.d_poll =	usbpoll,
    191 	.d_name =	"usb",
    192 #if __FreeBSD_version < 500014
    193 	.d_bmaj =	-1
    194 #endif
    195 };
    196 #endif
    197 
    198 Static void	usb_discover(void *);
    199 #ifdef __FreeBSD__
    200 Static bus_child_detached_t usb_child_detached;
    201 #endif
    202 Static void	usb_create_event_thread(void *);
    203 Static void	usb_event_thread(void *);
    204 Static void	usb_task_thread(void *);
    205 
    206 Static int usb_nbusses;			/* Number of /dev/usbN busses. */
    207 #ifdef __FreeBSD__
    208 Static struct cdev *usb_dev;		/* The /dev/usb device. */
    209 /* Busses to explore at the end of boot-time device configuration. */
    210 Static TAILQ_HEAD(, usb_softc) usb_coldexplist =
    211     TAILQ_HEAD_INITIALIZER(usb_coldexplist);
    212 #endif
    213 
    214 #define USB_MAX_EVENTS 100
    215 struct usb_event_q {
    216 	struct usb_event ue;
    217 	TAILQ_ENTRY(usb_event_q) next;
    218 };
    219 Static TAILQ_HEAD(, usb_event_q) usb_events =
    220 	TAILQ_HEAD_INITIALIZER(usb_events);
    221 Static int usb_nevents = 0;
    222 Static struct selinfo usb_selevent;
    223 Static usb_sigproc_ptr usb_async_proc;  /* process that wants USB SIGIO */
    224 Static int usb_dev_open = 0;
    225 Static struct usb_event *usb_alloc_event(void);
    226 Static void usb_free_event(struct usb_event *);
    227 Static void usb_add_event(int, struct usb_event *);
    228 
    229 Static int usb_get_next_event(struct usb_event *);
    230 
    231 #if defined(__NetBSD__) && defined(COMPAT_30)
    232 Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
    233 #endif
    234 
    235 Static const char *usbrev_str[] = USBREV_STR;
    236 
    237 #if defined(__NetBSD__) || defined(__OpenBSD__)
    238 USB_DECLARE_DRIVER(usb);
    239 #elif defined(__FreeBSD__)
    240 USB_DECLARE_DRIVER_INIT(usb,
    241 			DEVMETHOD(bus_child_detached, usb_child_detached),
    242 			DEVMETHOD(device_suspend, bus_generic_suspend),
    243 			DEVMETHOD(device_resume, bus_generic_resume),
    244 			DEVMETHOD(device_shutdown, bus_generic_shutdown)
    245 			);
    246 MODULE_VERSION(usb, 1);
    247 #endif
    248 
    249 USB_MATCH(usb)
    250 {
    251 	DPRINTF(("usbd_match\n"));
    252 	return (UMATCH_GENERIC);
    253 }
    254 
    255 USB_ATTACH(usb)
    256 {
    257 #if defined(__NetBSD__) || defined(__OpenBSD__)
    258 	struct usb_softc *sc = (struct usb_softc *)self;
    259 #elif defined(__FreeBSD__)
    260 	struct usb_softc *sc = device_get_softc(self);
    261 	void *aux = device_get_ivars(self);
    262 #endif
    263 	usbd_device_handle dev;
    264 	usbd_status err;
    265 	int usbrev;
    266 	int speed;
    267 	struct usb_event *ue;
    268 
    269 #ifdef __FreeBSD__
    270 	sc->sc_dev = self;
    271 #endif
    272 
    273 	DPRINTF(("usbd_attach\n"));
    274 
    275 	sc->sc_bus = aux;
    276 	sc->sc_bus->usbctl = sc;
    277 	sc->sc_port.power = USB_MAX_POWER;
    278 
    279 #if defined(__FreeBSD__)
    280 	printf("%s", USBDEVNAME(sc->sc_dev));
    281 #endif
    282 	usbrev = sc->sc_bus->usbrev;
    283 	printf(": USB revision %s", usbrev_str[usbrev]);
    284 	switch (usbrev) {
    285 	case USBREV_1_0:
    286 	case USBREV_1_1:
    287 		speed = USB_SPEED_FULL;
    288 		break;
    289 	case USBREV_2_0:
    290 		speed = USB_SPEED_HIGH;
    291 		break;
    292 	default:
    293 		printf(", not supported\n");
    294 		sc->sc_dying = 1;
    295 		USB_ATTACH_ERROR_RETURN;
    296 	}
    297 	printf("\n");
    298 
    299 	/* Make sure not to use tsleep() if we are cold booting. */
    300 	if (cold)
    301 		sc->sc_bus->use_polling++;
    302 
    303 	ue = usb_alloc_event();
    304 	ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    305 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    306 
    307 #ifdef USB_USE_SOFTINTR
    308 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    309 	/* XXX we should have our own level */
    310 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
    311 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
    312 	if (sc->sc_bus->soft == NULL) {
    313 		printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
    314 		sc->sc_dying = 1;
    315 		USB_ATTACH_ERROR_RETURN;
    316 	}
    317 #else
    318 	usb_callout_init(sc->sc_bus->softi);
    319 #endif
    320 #endif
    321 
    322 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
    323 		  &sc->sc_port);
    324 	if (!err) {
    325 		dev = sc->sc_port.device;
    326 		if (dev->hub == NULL) {
    327 			sc->sc_dying = 1;
    328 			printf("%s: root device is not a hub\n",
    329 			       USBDEVNAME(sc->sc_dev));
    330 			USB_ATTACH_ERROR_RETURN;
    331 		}
    332 		sc->sc_bus->root_hub = dev;
    333 #if 1
    334 		/*
    335 		 * Turning this code off will delay attachment of USB devices
    336 		 * until the USB event thread is running, which means that
    337 		 * the keyboard will not work until after cold boot.
    338 		 */
    339 #if defined(__FreeBSD__)
    340 		if (cold) {
    341 			/* Explore high-speed busses before others. */
    342 			if (speed == USB_SPEED_HIGH)
    343 				dev->hub->explore(sc->sc_bus->root_hub);
    344 			else
    345 				TAILQ_INSERT_TAIL(&usb_coldexplist, sc,
    346 				    sc_coldexplist);
    347 		}
    348 #else
    349 		if (cold && (device_cfdata(&sc->sc_dev)->cf_flags & 1))
    350 			dev->hub->explore(sc->sc_bus->root_hub);
    351 #endif
    352 #endif
    353 	} else {
    354 		printf("%s: root hub problem, error=%d\n",
    355 		       USBDEVNAME(sc->sc_dev), err);
    356 		sc->sc_dying = 1;
    357 	}
    358 	if (cold)
    359 		sc->sc_bus->use_polling--;
    360 
    361 	config_pending_incr();
    362 #if defined(__NetBSD__) || defined(__OpenBSD__)
    363 	usb_kthread_create(usb_create_event_thread, sc);
    364 #endif
    365 
    366 	usb_nbusses++;
    367 #if defined(__FreeBSD__)
    368 	usb_create_event_thread(sc);
    369 	/* The per controller devices (used for usb_discover) */
    370 	/* XXX This is redundant now, but old usbd's will want it */
    371 	sc->sc_usbdev = make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT,
    372 	    GID_OPERATOR, 0660, "usb%d", device_get_unit(self));
    373 	if (usb_nbusses == 1) {
    374 		/* The device spitting out events */
    375 		usb_dev = make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT,
    376 		    GID_OPERATOR, 0660, "usb");
    377 	}
    378 #endif
    379 
    380 	USB_ATTACH_SUCCESS_RETURN;
    381 }
    382 
    383 static const char *taskq_names[] = USB_TASKQ_NAMES;
    384 
    385 void
    386 usb_create_event_thread(void *arg)
    387 {
    388 	struct usb_softc *sc = arg;
    389 	struct usb_taskq *taskq;
    390 	int i;
    391 
    392 	if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    393 			   "%s", USBDEVNAME(sc->sc_dev))) {
    394 		printf("%s: unable to create event thread for\n",
    395 		       USBDEVNAME(sc->sc_dev));
    396 		panic("usb_create_event_thread");
    397 	}
    398 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    399 		taskq = &usb_taskq[i];
    400 
    401 		if (taskq->taskcreated == 0) {
    402 			taskq->taskcreated = 1;
    403 			taskq->name = taskq_names[i];
    404 			if (usb_kthread_create2(usb_task_thread, taskq,
    405 			    &taskq->task_thread_proc, taskq->name)) {
    406 				printf("unable to create task thread: %s\n",
    407 				    taskq->name);
    408 				panic("usb_create_event_thread task");
    409 			}
    410 		}
    411 	}
    412 }
    413 
    414 /*
    415  * Add a task to be performed by the task thread.  This function can be
    416  * called from any context and the task will be executed in a process
    417  * context ASAP.
    418  */
    419 void
    420 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
    421 {
    422 	struct usb_taskq *taskq;
    423 	int s;
    424 
    425 	s = splusb();
    426 	taskq = &usb_taskq[queue];
    427 	if (task->queue == -1) {
    428 		DPRINTFN(2,("usb_add_task: task=%p\n", task));
    429 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    430 		task->queue = queue;
    431 	} else {
    432 		DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
    433 	}
    434 	wakeup(&taskq->tasks);
    435 	splx(s);
    436 }
    437 
    438 void
    439 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
    440 {
    441 	struct usb_taskq *taskq;
    442 	int s;
    443 
    444 	s = splusb();
    445 	if (task->queue != -1) {
    446 		taskq = &usb_taskq[task->queue];
    447 		TAILQ_REMOVE(&taskq->tasks, task, next);
    448 		task->queue = -1;
    449 	}
    450 	splx(s);
    451 }
    452 
    453 void
    454 usb_event_thread(void *arg)
    455 {
    456 	static int newthread_wchan;
    457 	struct usb_softc *sc = arg;
    458 
    459 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
    460 	mtx_lock(&Giant);
    461 #endif
    462 
    463 	DPRINTF(("usb_event_thread: start\n"));
    464 
    465 	/*
    466 	 * In case this controller is a companion controller to an
    467 	 * EHCI controller we need to wait until the EHCI controller
    468 	 * has grabbed the port.  What we do here is wait until no new
    469 	 * USB threads have been created in a while. XXX we actually
    470 	 * just want to wait for the PCI slot to be fully scanned.
    471 	 *
    472 	 * Note that when you `kldload usb' it actually attaches the
    473 	 * devices in order that the drivers appear in the kld, not the
    474 	 * normal PCI order, since the addition of each driver within
    475 	 * usb.ko (ohci, ehci etc.) causes a separate PCI bus re-scan.
    476 	 */
    477 	wakeup(&newthread_wchan);
    478 	for (;;) {
    479 		if (tsleep(&newthread_wchan , PWAIT, "usbets", hz * 4) != 0)
    480 			break;
    481 	}
    482 
    483 	/* Make sure first discover does something. */
    484 	sc->sc_bus->needs_explore = 1;
    485 	usb_discover(sc);
    486 	config_pending_decr();
    487 
    488 	while (!sc->sc_dying) {
    489 #ifdef USB_DEBUG
    490 		if (usb_noexplore < 2)
    491 #endif
    492 		usb_discover(sc);
    493 #ifdef USB_DEBUG
    494 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    495 		    usb_noexplore ? 0 : hz * 60);
    496 #else
    497 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    498 		    hz * 60);
    499 #endif
    500 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    501 	}
    502 	sc->sc_event_thread = NULL;
    503 
    504 	/* In case parent is waiting for us to exit. */
    505 	wakeup(sc);
    506 
    507 	DPRINTF(("usb_event_thread: exit\n"));
    508 	kthread_exit(0);
    509 }
    510 
    511 void
    512 usb_task_thread(void *arg)
    513 {
    514 	struct usb_task *task;
    515 	struct usb_taskq *taskq;
    516 	int s;
    517 
    518 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
    519 	mtx_lock(&Giant);
    520 #endif
    521 
    522 	taskq = arg;
    523 	DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
    524 
    525 	s = splusb();
    526 
    527 	while (usb_nbusses > 0) {
    528 		task = TAILQ_FIRST(&taskq->tasks);
    529 		if (task == NULL) {
    530 			tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
    531 			task = TAILQ_FIRST(&taskq->tasks);
    532 		}
    533 		DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
    534 		if (task != NULL) {
    535 			TAILQ_REMOVE(&taskq->tasks, task, next);
    536 			task->queue = -1;
    537 			splx(s);
    538 			task->fun(task->arg);
    539 			s = splusb();
    540 		}
    541 	}
    542 
    543 	splx(s);
    544 
    545 	taskq->taskcreated = 0;
    546 	wakeup(&taskq->taskcreated);
    547 
    548 	DPRINTF(("usb_event_thread: exit\n"));
    549 	kthread_exit(0);
    550 }
    551 
    552 #if defined(__NetBSD__) || defined(__OpenBSD__)
    553 int
    554 usbctlprint(void *aux, const char *pnp)
    555 {
    556 	/* only "usb"es can attach to host controllers */
    557 	if (pnp)
    558 		aprint_normal("usb at %s", pnp);
    559 
    560 	return (UNCONF);
    561 }
    562 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    563 
    564 int
    565 usbopen(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
    566 {
    567 	int unit = USBUNIT(dev);
    568 	struct usb_softc *sc;
    569 
    570 	if (unit == USB_DEV_MINOR) {
    571 		if (usb_dev_open)
    572 			return (EBUSY);
    573 		usb_dev_open = 1;
    574 		usb_async_proc = 0;
    575 		return (0);
    576 	}
    577 
    578 	USB_GET_SC_OPEN(usb, unit, sc);
    579 
    580 	if (sc->sc_dying)
    581 		return (EIO);
    582 
    583 	return (0);
    584 }
    585 
    586 int
    587 usbread(usb_cdev_t dev, struct uio *uio, int flag)
    588 {
    589 	struct usb_event *ue;
    590 #if defined(__NetBSD__) && defined(COMPAT_30)
    591 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    592 	int useold = 0;
    593 #endif
    594 	int s, error, n;
    595 
    596 	if (USBUNIT(dev) != USB_DEV_MINOR)
    597 		return (ENXIO);
    598 
    599 	switch (uio->uio_resid) {
    600 #if defined(__NetBSD__) && defined(COMPAT_30)
    601 	case sizeof(struct usb_event_old):
    602 		ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
    603 			     M_WAITOK|M_ZERO);
    604 		useold = 1;
    605 		/* FALLTHRU */
    606 #endif
    607 	case sizeof(struct usb_event):
    608 		ue = usb_alloc_event();
    609 		break;
    610 	default:
    611 		return (EINVAL);
    612 	}
    613 
    614 	error = 0;
    615 	s = splusb();
    616 	for (;;) {
    617 		n = usb_get_next_event(ue);
    618 		if (n != 0)
    619 			break;
    620 		if (flag &
    621 #ifdef __FreeBSD__
    622 		    O_NONBLOCK
    623 #else
    624 		    IO_NDELAY
    625 #endif
    626 		    ) {
    627 			error = EWOULDBLOCK;
    628 			break;
    629 		}
    630 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    631 		if (error)
    632 			break;
    633 	}
    634 	splx(s);
    635 	if (!error) {
    636 #if defined(__NetBSD__) && defined(COMPAT_30)
    637 		if (useold) { /* copy fields to old struct */
    638 			ueo->ue_type = ue->ue_type;
    639 			memcpy(&ueo->ue_time, &ue->ue_time,
    640 			      sizeof(struct timespec));
    641 			switch (ue->ue_type) {
    642 				case USB_EVENT_DEVICE_ATTACH:
    643 				case USB_EVENT_DEVICE_DETACH:
    644 					usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
    645 					break;
    646 
    647 				case USB_EVENT_CTRLR_ATTACH:
    648 				case USB_EVENT_CTRLR_DETACH:
    649 					ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
    650 					break;
    651 
    652 				case USB_EVENT_DRIVER_ATTACH:
    653 				case USB_EVENT_DRIVER_DETACH:
    654 					ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
    655 					memcpy(ueo->u.ue_driver.ue_devname,
    656 					       ue->u.ue_driver.ue_devname,
    657 					       sizeof(ue->u.ue_driver.ue_devname));
    658 					break;
    659 				default:
    660 					;
    661 			}
    662 
    663 			error = uiomove((void *)ueo, uio->uio_resid, uio);
    664 		} else
    665 #endif
    666 			error = uiomove((void *)ue, uio->uio_resid, uio);
    667 	}
    668 	usb_free_event(ue);
    669 #if defined(__NetBSD__) && defined(COMPAT_30)
    670 	if (useold)
    671 		free(ueo, M_USBDEV);
    672 #endif
    673 
    674 	return (error);
    675 }
    676 
    677 int
    678 usbclose(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
    679 {
    680 	int unit = USBUNIT(dev);
    681 
    682 	if (unit == USB_DEV_MINOR) {
    683 		usb_async_proc = 0;
    684 		usb_dev_open = 0;
    685 	}
    686 
    687 	return (0);
    688 }
    689 
    690 int
    691 usbioctl(usb_cdev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
    692 {
    693 	struct usb_softc *sc;
    694 	int unit = USBUNIT(devt);
    695 
    696 	if (unit == USB_DEV_MINOR) {
    697 		switch (cmd) {
    698 		case FIONBIO:
    699 			/* All handled in the upper FS layer. */
    700 			return (0);
    701 
    702 		case FIOASYNC:
    703 			if (*(int *)data)
    704 #if defined(__NetBSD__) && __NetBSD_Version__ >= 399001400
    705 				usb_async_proc = p->l_proc;
    706 #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000
    707 				usb_async_proc = p->td_proc;
    708 #else
    709 				usb_async_proc = p;
    710 #endif
    711 			else
    712 				usb_async_proc = 0;
    713 			return (0);
    714 
    715 		default:
    716 			return (EINVAL);
    717 		}
    718 	}
    719 
    720 	USB_GET_SC(usb, unit, sc);
    721 
    722 	if (sc->sc_dying)
    723 		return (EIO);
    724 
    725 	switch (cmd) {
    726 #if defined(USB_DEBUG)
    727 	case USB_SETDEBUG:
    728 		if (!(flag & FWRITE))
    729 			return (EBADF);
    730 		usbdebug  = ((*(int *)data) & 0x000000ff);
    731 #if defined(UHCI_DEBUG) && NUHCI > 0
    732 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    733 #endif
    734 #if defined(OHCI_DEBUG) && NOHCI > 0
    735 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    736 #endif
    737 		break;
    738 #endif /* USB_DEBUG */
    739 #if defined(__FreeBSD__)
    740 	/* This part should be deleted */
    741   	case USB_DISCOVER:
    742   		break;
    743 #endif
    744 	case USB_REQUEST:
    745 	{
    746 		struct usb_ctl_request *ur = (void *)data;
    747 		int len = UGETW(ur->ucr_request.wLength);
    748 		struct iovec iov;
    749 		struct uio uio;
    750 		void *ptr = 0;
    751 		int addr = ur->ucr_addr;
    752 		usbd_status err;
    753 		int error = 0;
    754 
    755 		if (!(flag & FWRITE))
    756 			return (EBADF);
    757 
    758 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    759 		if (len < 0 || len > 32768)
    760 			return (EINVAL);
    761 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    762 		    sc->sc_bus->devices[addr] == 0)
    763 			return (EINVAL);
    764 		if (len != 0) {
    765 			iov.iov_base = (caddr_t)ur->ucr_data;
    766 			iov.iov_len = len;
    767 			uio.uio_iov = &iov;
    768 			uio.uio_iovcnt = 1;
    769 			uio.uio_resid = len;
    770 			uio.uio_offset = 0;
    771 			uio.uio_rw =
    772 				ur->ucr_request.bmRequestType & UT_READ ?
    773 				UIO_READ : UIO_WRITE;
    774 #ifdef __NetBSD__
    775 			uio.uio_vmspace = p->l_proc->p_vmspace;
    776 #endif
    777 #ifdef __FreeBSD__
    778 			uio.uio_segflg = UIO_USERSPACE;
    779 			uio.uio_td = p;
    780 #endif
    781 			ptr = malloc(len, M_TEMP, M_WAITOK);
    782 			if (uio.uio_rw == UIO_WRITE) {
    783 				error = uiomove(ptr, len, &uio);
    784 				if (error)
    785 					goto ret;
    786 			}
    787 		}
    788 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    789 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    790 			  USBD_DEFAULT_TIMEOUT);
    791 		if (err) {
    792 			error = EIO;
    793 			goto ret;
    794 		}
    795 		if (len != 0) {
    796 			if (uio.uio_rw == UIO_READ) {
    797 				error = uiomove(ptr, len, &uio);
    798 				if (error)
    799 					goto ret;
    800 			}
    801 		}
    802 	ret:
    803 		if (ptr)
    804 			free(ptr, M_TEMP);
    805 		return (error);
    806 	}
    807 
    808 	case USB_DEVICEINFO:
    809 	{
    810 		usbd_device_handle dev;
    811 		struct usb_device_info *di = (void *)data;
    812 		int addr = di->udi_addr;
    813 
    814 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    815 			return EINVAL;
    816 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    817 			return ENXIO;
    818 		usbd_fill_deviceinfo(dev, di, 1);
    819 		break;
    820 	}
    821 
    822 #if defined(__NetBSD__) && defined(COMPAT_30)
    823 	case USB_DEVICEINFO_OLD:
    824 	{
    825 		usbd_device_handle dev;
    826 		struct usb_device_info_old *di = (void *)data;
    827 		int addr = di->udi_addr;
    828 
    829 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    830 			return EINVAL;
    831 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    832 			return ENXIO;
    833 		usbd_fill_deviceinfo_old(dev, di, 1);
    834 		break;
    835 	}
    836 #endif
    837 
    838 	case USB_DEVICESTATS:
    839 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    840 		break;
    841 
    842 	default:
    843 		return (EINVAL);
    844 	}
    845 	return (0);
    846 }
    847 
    848 int
    849 usbpoll(usb_cdev_t dev, int events, usb_proc_ptr p)
    850 {
    851 	int revents, mask, s;
    852 	int unit = USBUNIT(dev);
    853 
    854 	if (unit == USB_DEV_MINOR) {
    855 		revents = 0;
    856 		mask = POLLIN | POLLRDNORM;
    857 
    858 		s = splusb();
    859 		if (events & mask && usb_nevents > 0)
    860 			revents |= events & mask;
    861 		if (revents == 0 && events & mask)
    862 			selrecord(p, &usb_selevent);
    863 		splx(s);
    864 
    865 		return (revents);
    866 	} else {
    867 		return (0);
    868 	}
    869 }
    870 
    871 #if defined(__NetBSD__) || defined(__OpenBSD__)
    872 static void
    873 filt_usbrdetach(struct knote *kn)
    874 {
    875 	int s;
    876 
    877 	s = splusb();
    878 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    879 	splx(s);
    880 }
    881 
    882 static int
    883 filt_usbread(struct knote *kn, long hint)
    884 {
    885 
    886 	if (usb_nevents == 0)
    887 		return (0);
    888 
    889 	kn->kn_data = sizeof(struct usb_event);
    890 	return (1);
    891 }
    892 
    893 static const struct filterops usbread_filtops =
    894 	{ 1, NULL, filt_usbrdetach, filt_usbread };
    895 
    896 int
    897 usbkqfilter(dev_t dev, struct knote *kn)
    898 {
    899 	struct klist *klist;
    900 	int s;
    901 
    902 	switch (kn->kn_filter) {
    903 	case EVFILT_READ:
    904 		if (minor(dev) != USB_DEV_MINOR)
    905 			return (1);
    906 		klist = &usb_selevent.sel_klist;
    907 		kn->kn_fop = &usbread_filtops;
    908 		break;
    909 
    910 	default:
    911 		return (1);
    912 	}
    913 
    914 	kn->kn_hook = NULL;
    915 
    916 	s = splusb();
    917 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    918 	splx(s);
    919 
    920 	return (0);
    921 }
    922 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    923 
    924 /* Explore device tree from the root. */
    925 Static void
    926 usb_discover(void *v)
    927 {
    928 	struct usb_softc *sc = v;
    929 	/* splxxx should be changed to mutexes for preemption safety some day */
    930 	int s;
    931 
    932 	DPRINTFN(2,("usb_discover\n"));
    933 #ifdef USB_DEBUG
    934 	if (usb_noexplore > 1)
    935 		return;
    936 #endif
    937 
    938 	/*
    939 	 * We need mutual exclusion while traversing the device tree,
    940 	 * but this is guaranteed since this function is only called
    941 	 * from the event thread for the controller.
    942 	 */
    943 	s = splusb();
    944 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    945 		sc->sc_bus->needs_explore = 0;
    946 		splx(s);
    947 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    948 		s = splusb();
    949 	}
    950 	splx(s);
    951 }
    952 
    953 void
    954 usb_needs_explore(usbd_device_handle dev)
    955 {
    956 	DPRINTFN(2,("usb_needs_explore\n"));
    957 	dev->bus->needs_explore = 1;
    958 	wakeup(&dev->bus->needs_explore);
    959 }
    960 
    961 void
    962 usb_needs_reattach(usbd_device_handle dev)
    963 {
    964 	DPRINTFN(2,("usb_needs_reattach\n"));
    965 	dev->powersrc->reattach = 1;
    966 	dev->bus->needs_explore = 1;
    967 	wakeup(&dev->bus->needs_explore);
    968 }
    969 
    970 /* Called at splusb() */
    971 int
    972 usb_get_next_event(struct usb_event *ue)
    973 {
    974 	struct usb_event_q *ueq;
    975 
    976 	if (usb_nevents <= 0)
    977 		return (0);
    978 	ueq = TAILQ_FIRST(&usb_events);
    979 #ifdef DIAGNOSTIC
    980 	if (ueq == NULL) {
    981 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
    982 		usb_nevents = 0;
    983 		return (0);
    984 	}
    985 #endif
    986 	if (ue)
    987 		*ue = ueq->ue;
    988 	TAILQ_REMOVE(&usb_events, ueq, next);
    989 	usb_free_event((struct usb_event *)(void *)ueq);
    990 	usb_nevents--;
    991 	return (1);
    992 }
    993 
    994 void
    995 usbd_add_dev_event(int type, usbd_device_handle udev)
    996 {
    997 	struct usb_event *ue = usb_alloc_event();
    998 
    999 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
   1000 	usb_add_event(type, ue);
   1001 }
   1002 
   1003 void
   1004 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
   1005 {
   1006 	struct usb_event *ue = usb_alloc_event();
   1007 
   1008 	ue->u.ue_driver.ue_cookie = udev->cookie;
   1009 	strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
   1010 	    sizeof ue->u.ue_driver.ue_devname);
   1011 	usb_add_event(type, ue);
   1012 }
   1013 
   1014 Static struct usb_event *
   1015 usb_alloc_event(void)
   1016 {
   1017 	/* Yes, this is right; we allocate enough so that we can use it later */
   1018 	return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
   1019 }
   1020 
   1021 Static void
   1022 usb_free_event(struct usb_event *uep)
   1023 {
   1024 	free(uep, M_USBDEV);
   1025 }
   1026 
   1027 Static void
   1028 usb_add_event(int type, struct usb_event *uep)
   1029 {
   1030 	struct usb_event_q *ueq;
   1031 	struct timeval thetime;
   1032 	int s;
   1033 
   1034 	microtime(&thetime);
   1035 	/* Don't want to wait here inside splusb() */
   1036 	ueq = (struct usb_event_q *)(void *)uep;
   1037 	ueq->ue = *uep;
   1038 	ueq->ue.ue_type = type;
   1039 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
   1040 
   1041 	s = splusb();
   1042 	if (USB_EVENT_IS_DETACH(type)) {
   1043 		struct usb_event_q *ueqi, *ueqi_next;
   1044 
   1045 		for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
   1046 			ueqi_next = TAILQ_NEXT(ueqi, next);
   1047 			if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
   1048 			    uep->u.ue_device.udi_cookie.cookie) {
   1049 				TAILQ_REMOVE(&usb_events, ueqi, next);
   1050 				free(ueqi, M_USBDEV);
   1051 				usb_nevents--;
   1052 				ueqi_next = TAILQ_FIRST(&usb_events);
   1053 			}
   1054 		}
   1055 	}
   1056 	if (usb_nevents >= USB_MAX_EVENTS) {
   1057 		/* Too many queued events, drop an old one. */
   1058 		DPRINTFN(-1,("usb: event dropped\n"));
   1059 		(void)usb_get_next_event(0);
   1060 	}
   1061 	TAILQ_INSERT_TAIL(&usb_events, ueq, next);
   1062 	usb_nevents++;
   1063 	wakeup(&usb_events);
   1064 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1065 	selnotify(&usb_selevent, 0);
   1066 #elif defined(__FreeBSD__)
   1067 	selwakeuppri(&usb_selevent, PZERO);
   1068 #endif
   1069 	if (usb_async_proc != NULL) {
   1070 		USB_PROC_LOCK(usb_async_proc);
   1071 		psignal(usb_async_proc, SIGIO);
   1072 		USB_PROC_UNLOCK(usb_async_proc);
   1073 	}
   1074 	splx(s);
   1075 }
   1076 
   1077 void
   1078 usb_schedsoftintr(usbd_bus_handle bus)
   1079 {
   1080 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
   1081 #ifdef USB_USE_SOFTINTR
   1082 	if (bus->use_polling) {
   1083 		bus->methods->soft_intr(bus);
   1084 	} else {
   1085 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1086 		softintr_schedule(bus->soft);
   1087 #else
   1088 		if (!callout_pending(&bus->softi))
   1089 			callout_reset(&bus->softi, 0, bus->methods->soft_intr,
   1090 			    bus);
   1091 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
   1092 	}
   1093 #else
   1094 	bus->methods->soft_intr(bus);
   1095 #endif /* USB_USE_SOFTINTR */
   1096 }
   1097 
   1098 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1099 int
   1100 usb_activate(device_ptr_t self, enum devact act)
   1101 {
   1102 	struct usb_softc *sc = (struct usb_softc *)self;
   1103 	usbd_device_handle dev = sc->sc_port.device;
   1104 	int i, rv = 0;
   1105 
   1106 	switch (act) {
   1107 	case DVACT_ACTIVATE:
   1108 		return (EOPNOTSUPP);
   1109 
   1110 	case DVACT_DEACTIVATE:
   1111 		sc->sc_dying = 1;
   1112 		if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
   1113 			for (i = 0; dev->subdevs[i]; i++)
   1114 				rv |= config_deactivate(dev->subdevs[i]);
   1115 		}
   1116 		break;
   1117 	}
   1118 	return (rv);
   1119 }
   1120 #endif
   1121 
   1122 USB_DETACH(usb)
   1123 {
   1124 	USB_DETACH_START(usb, sc);
   1125 	struct usb_event *ue;
   1126 	struct usb_taskq *taskq;
   1127 	int i;
   1128 
   1129 	DPRINTF(("usb_detach: start\n"));
   1130 
   1131 	sc->sc_dying = 1;
   1132 
   1133 	/* Make all devices disconnect. */
   1134 	if (sc->sc_port.device != NULL)
   1135 		usb_disconnect_port(&sc->sc_port, self);
   1136 
   1137 	/* Kill off event thread. */
   1138 	if (sc->sc_event_thread != NULL) {
   1139 		wakeup(&sc->sc_bus->needs_explore);
   1140 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
   1141 			printf("%s: event thread didn't die\n",
   1142 			       USBDEVNAME(sc->sc_dev));
   1143 		DPRINTF(("usb_detach: event thread dead\n"));
   1144 	}
   1145 
   1146 #ifdef __FreeBSD__
   1147 	destroy_dev(sc->sc_usbdev);
   1148 #endif
   1149 	if (--usb_nbusses == 0) {
   1150 #ifdef __FreeBSD__
   1151 		destroy_dev(usb_dev);
   1152 		usb_dev = NULL;
   1153 #endif
   1154 		for (i = 0; i < USB_NUM_TASKQS; i++) {
   1155 			taskq = &usb_taskq[i];
   1156 			wakeup(&taskq->tasks);
   1157 			if (tsleep(&taskq->taskcreated, PWAIT, "usbtdt",
   1158 			    hz * 60)) {
   1159 				printf("usb task thread %s didn't die\n",
   1160 				    taskq->name);
   1161 			}
   1162 		}
   1163 	}
   1164 
   1165 #ifdef USB_USE_SOFTINTR
   1166 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1167 	if (sc->sc_bus->soft != NULL) {
   1168 		softintr_disestablish(sc->sc_bus->soft);
   1169 		sc->sc_bus->soft = NULL;
   1170 	}
   1171 #else
   1172 	callout_stop(&sc->sc_bus->softi);
   1173 #endif
   1174 #endif
   1175 
   1176 	ue = usb_alloc_event();
   1177 	ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
   1178 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1179 
   1180 	return (0);
   1181 }
   1182 
   1183 #if defined(__NetBSD__) && defined(COMPAT_30)
   1184 Static void
   1185 usb_copy_old_devinfo(struct usb_device_info_old *uo,
   1186 		     const struct usb_device_info *ue)
   1187 {
   1188 	const unsigned char *p;
   1189 	unsigned char *q;
   1190 	int i, n;
   1191 
   1192 	uo->udi_bus = ue->udi_bus;
   1193 	uo->udi_addr = ue->udi_addr;
   1194 	uo->udi_cookie = ue->udi_cookie;
   1195 	for (i = 0, p = (const unsigned char *)ue->udi_product,
   1196 	     q = (unsigned char *)uo->udi_product;
   1197 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1198 		if (*p < 0x80)
   1199 			q[i++] = *p;
   1200 		else {
   1201 			q[i++] = '?';
   1202 			if ((*p & 0xe0) == 0xe0)
   1203 				p++;
   1204 			p++;
   1205 		}
   1206 	}
   1207 	q[i] = 0;
   1208 
   1209 	for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
   1210 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1211 		if (* p < 0x80)
   1212 			q[i++] = *p;
   1213 		else {
   1214 			q[i++] = '?';
   1215 			p++;
   1216 			if ((*p & 0xe0) == 0xe0)
   1217 				p++;
   1218 		}
   1219 	}
   1220 	q[i] = 0;
   1221 
   1222 	memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
   1223 
   1224 	uo->udi_productNo = ue->udi_productNo;
   1225 	uo->udi_vendorNo = ue->udi_vendorNo;
   1226 	uo->udi_releaseNo = ue->udi_releaseNo;
   1227 	uo->udi_class = ue->udi_class;
   1228 	uo->udi_subclass = ue->udi_subclass;
   1229 	uo->udi_protocol = ue->udi_protocol;
   1230 	uo->udi_config = ue->udi_config;
   1231 	uo->udi_speed = ue->udi_speed;
   1232 	uo->udi_power = ue->udi_power;
   1233 	uo->udi_nports = ue->udi_nports;
   1234 
   1235 	for (n=0; n<USB_MAX_DEVNAMES; n++)
   1236 		memcpy(uo->udi_devnames[n],
   1237 		       ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
   1238 	memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
   1239 }
   1240 #endif
   1241 
   1242 #if defined(__FreeBSD__)
   1243 Static void
   1244 usb_child_detached(device_t self, device_t child)
   1245 {
   1246 	struct usb_softc *sc = device_get_softc(self);
   1247 
   1248 	/* XXX, should check it is the right device. */
   1249 	sc->sc_port.device = NULL;
   1250 }
   1251 
   1252 /* Explore USB busses at the end of device configuration. */
   1253 Static void
   1254 usb_cold_explore(void *arg)
   1255 {
   1256 	struct usb_softc *sc;
   1257 
   1258 	USB_KASSERT2(cold || TAILQ_EMPTY(&usb_coldexplist),
   1259 	    ("usb_cold_explore: busses to explore when !cold"));
   1260 	while (!TAILQ_EMPTY(&usb_coldexplist)) {
   1261 		sc = TAILQ_FIRST(&usb_coldexplist);
   1262 		TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
   1263 
   1264 		sc->sc_bus->use_polling++;
   1265 		sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
   1266 		sc->sc_bus->use_polling--;
   1267 	}
   1268 }
   1269 
   1270 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
   1271 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
   1272 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);
   1273 DRIVER_MODULE(usb, slhci, usb_driver, usb_devclass, 0, 0);
   1274 SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
   1275     usb_cold_explore, NULL);
   1276 #endif
   1277