Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.95.4.3
      1 /*	$NetBSD: usb.c,v 1.95.4.3 2007/06/22 10:12:24 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.3 2007/06/22 10:12:24 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 	USB_KASSERT(queue > USB_TASKQ_IDLE && queue <= USB_NUM_TASKQS);
    426 
    427 	s = splusb();
    428 	taskq = &usb_taskq[queue - 1];
    429 	if (task->queue == USB_TASKQ_IDLE) {
    430 		DPRINTFN(2,("usb_add_task: task=%p\n", task));
    431 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    432 		task->queue = queue;
    433 	} else {
    434 		DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
    435 	}
    436 	wakeup(&taskq->tasks);
    437 	splx(s);
    438 }
    439 
    440 void
    441 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
    442 {
    443 	struct usb_taskq *taskq;
    444 	int s;
    445 
    446 	s = splusb();
    447 	if (task->queue != USB_TASKQ_IDLE) {
    448 		taskq = &usb_taskq[task->queue - 1];
    449 		TAILQ_REMOVE(&taskq->tasks, task, next);
    450 		task->queue = USB_TASKQ_IDLE;
    451 	}
    452 	splx(s);
    453 }
    454 
    455 void
    456 usb_event_thread(void *arg)
    457 {
    458 	static int newthread_wchan;
    459 	struct usb_softc *sc = arg;
    460 
    461 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
    462 	mtx_lock(&Giant);
    463 #endif
    464 
    465 	DPRINTF(("usb_event_thread: start\n"));
    466 
    467 	/*
    468 	 * In case this controller is a companion controller to an
    469 	 * EHCI controller we need to wait until the EHCI controller
    470 	 * has grabbed the port.  What we do here is wait until no new
    471 	 * USB threads have been created in a while. XXX we actually
    472 	 * just want to wait for the PCI slot to be fully scanned.
    473 	 *
    474 	 * Note that when you `kldload usb' it actually attaches the
    475 	 * devices in order that the drivers appear in the kld, not the
    476 	 * normal PCI order, since the addition of each driver within
    477 	 * usb.ko (ohci, ehci etc.) causes a separate PCI bus re-scan.
    478 	 */
    479 	wakeup(&newthread_wchan);
    480 	for (;;) {
    481 		if (tsleep(&newthread_wchan , PWAIT, "usbets", hz * 4) != 0)
    482 			break;
    483 	}
    484 
    485 	/* Make sure first discover does something. */
    486 	sc->sc_bus->needs_explore = 1;
    487 	usb_discover(sc);
    488 	config_pending_decr();
    489 
    490 	while (!sc->sc_dying) {
    491 #ifdef USB_DEBUG
    492 		if (usb_noexplore < 2)
    493 #endif
    494 		usb_discover(sc);
    495 #ifdef USB_DEBUG
    496 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    497 		    usb_noexplore ? 0 : hz * 60);
    498 #else
    499 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    500 		    hz * 60);
    501 #endif
    502 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    503 	}
    504 	sc->sc_event_thread = NULL;
    505 
    506 	/* In case parent is waiting for us to exit. */
    507 	wakeup(sc);
    508 
    509 	DPRINTF(("usb_event_thread: exit\n"));
    510 	kthread_exit(0);
    511 }
    512 
    513 void
    514 usb_task_thread(void *arg)
    515 {
    516 	struct usb_task *task;
    517 	struct usb_taskq *taskq;
    518 	int s;
    519 
    520 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
    521 	mtx_lock(&Giant);
    522 #endif
    523 
    524 	taskq = arg;
    525 	DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
    526 
    527 	s = splusb();
    528 
    529 	while (usb_nbusses > 0) {
    530 		task = TAILQ_FIRST(&taskq->tasks);
    531 		if (task == NULL) {
    532 			tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
    533 			task = TAILQ_FIRST(&taskq->tasks);
    534 		}
    535 		DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
    536 		if (task != NULL) {
    537 			TAILQ_REMOVE(&taskq->tasks, task, next);
    538 			task->queue = USB_TASKQ_IDLE;
    539 			splx(s);
    540 			task->fun(task->arg);
    541 			s = splusb();
    542 		}
    543 	}
    544 
    545 	splx(s);
    546 
    547 	taskq->taskcreated = 0;
    548 	wakeup(&taskq->taskcreated);
    549 
    550 	DPRINTF(("usb_event_thread: exit\n"));
    551 	kthread_exit(0);
    552 }
    553 
    554 #if defined(__NetBSD__) || defined(__OpenBSD__)
    555 int
    556 usbctlprint(void *aux, const char *pnp)
    557 {
    558 	/* only "usb"es can attach to host controllers */
    559 	if (pnp)
    560 		aprint_normal("usb at %s", pnp);
    561 
    562 	return (UNCONF);
    563 }
    564 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    565 
    566 int
    567 usbopen(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
    568 {
    569 	int unit = USBUNIT(dev);
    570 	struct usb_softc *sc;
    571 
    572 	if (unit == USB_DEV_MINOR) {
    573 		if (usb_dev_open)
    574 			return (EBUSY);
    575 		usb_dev_open = 1;
    576 		usb_async_proc = 0;
    577 		return (0);
    578 	}
    579 
    580 	USB_GET_SC_OPEN(usb, unit, sc);
    581 
    582 	if (sc->sc_dying)
    583 		return (EIO);
    584 
    585 	return (0);
    586 }
    587 
    588 int
    589 usbread(usb_cdev_t dev, struct uio *uio, int flag)
    590 {
    591 	struct usb_event *ue;
    592 #if defined(__NetBSD__) && defined(COMPAT_30)
    593 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    594 	int useold = 0;
    595 #endif
    596 	int s, error, n;
    597 
    598 	if (USBUNIT(dev) != USB_DEV_MINOR)
    599 		return (ENXIO);
    600 
    601 	switch (uio->uio_resid) {
    602 #if defined(__NetBSD__) && defined(COMPAT_30)
    603 	case sizeof(struct usb_event_old):
    604 		ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
    605 			     M_WAITOK|M_ZERO);
    606 		useold = 1;
    607 		/* FALLTHRU */
    608 #endif
    609 	case sizeof(struct usb_event):
    610 		ue = usb_alloc_event();
    611 		break;
    612 	default:
    613 		return (EINVAL);
    614 	}
    615 
    616 	error = 0;
    617 	s = splusb();
    618 	for (;;) {
    619 		n = usb_get_next_event(ue);
    620 		if (n != 0)
    621 			break;
    622 		if (flag &
    623 #ifdef __FreeBSD__
    624 		    O_NONBLOCK
    625 #else
    626 		    IO_NDELAY
    627 #endif
    628 		    ) {
    629 			error = EWOULDBLOCK;
    630 			break;
    631 		}
    632 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    633 		if (error)
    634 			break;
    635 	}
    636 	splx(s);
    637 	if (!error) {
    638 #if defined(__NetBSD__) && defined(COMPAT_30)
    639 		if (useold) { /* copy fields to old struct */
    640 			ueo->ue_type = ue->ue_type;
    641 			memcpy(&ueo->ue_time, &ue->ue_time,
    642 			      sizeof(struct timespec));
    643 			switch (ue->ue_type) {
    644 				case USB_EVENT_DEVICE_ATTACH:
    645 				case USB_EVENT_DEVICE_DETACH:
    646 					usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
    647 					break;
    648 
    649 				case USB_EVENT_CTRLR_ATTACH:
    650 				case USB_EVENT_CTRLR_DETACH:
    651 					ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
    652 					break;
    653 
    654 				case USB_EVENT_DRIVER_ATTACH:
    655 				case USB_EVENT_DRIVER_DETACH:
    656 					ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
    657 					memcpy(ueo->u.ue_driver.ue_devname,
    658 					       ue->u.ue_driver.ue_devname,
    659 					       sizeof(ue->u.ue_driver.ue_devname));
    660 					break;
    661 				default:
    662 					;
    663 			}
    664 
    665 			error = uiomove((void *)ueo, uio->uio_resid, uio);
    666 		} else
    667 #endif
    668 			error = uiomove((void *)ue, uio->uio_resid, uio);
    669 	}
    670 	usb_free_event(ue);
    671 #if defined(__NetBSD__) && defined(COMPAT_30)
    672 	if (useold)
    673 		free(ueo, M_USBDEV);
    674 #endif
    675 
    676 	return (error);
    677 }
    678 
    679 int
    680 usbclose(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
    681 {
    682 	int unit = USBUNIT(dev);
    683 
    684 	if (unit == USB_DEV_MINOR) {
    685 		usb_async_proc = 0;
    686 		usb_dev_open = 0;
    687 	}
    688 
    689 	return (0);
    690 }
    691 
    692 int
    693 usbioctl(usb_cdev_t devt, u_long cmd, usb_ioctlarg_t data, int flag,
    694 	 usb_proc_ptr p)
    695 {
    696 	struct usb_softc *sc;
    697 	int unit = USBUNIT(devt);
    698 
    699 	if (unit == USB_DEV_MINOR) {
    700 		switch (cmd) {
    701 		case FIONBIO:
    702 			/* All handled in the upper FS layer. */
    703 			return (0);
    704 
    705 		case FIOASYNC:
    706 			if (*(int *)data)
    707 #if defined(__NetBSD__) && __NetBSD_Version__ >= 399001400
    708 				usb_async_proc = p->l_proc;
    709 #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000
    710 				usb_async_proc = p->td_proc;
    711 #else
    712 				usb_async_proc = p;
    713 #endif
    714 			else
    715 				usb_async_proc = 0;
    716 			return (0);
    717 
    718 		default:
    719 			return (EINVAL);
    720 		}
    721 	}
    722 
    723 	USB_GET_SC(usb, unit, sc);
    724 
    725 	if (sc->sc_dying)
    726 		return (EIO);
    727 
    728 	switch (cmd) {
    729 #if defined(USB_DEBUG)
    730 	case USB_SETDEBUG:
    731 		if (!(flag & FWRITE))
    732 			return (EBADF);
    733 		usbdebug  = ((*(int *)data) & 0x000000ff);
    734 #if defined(UHCI_DEBUG) && NUHCI > 0
    735 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    736 #endif
    737 #if defined(OHCI_DEBUG) && NOHCI > 0
    738 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    739 #endif
    740 		break;
    741 #endif /* USB_DEBUG */
    742 #if defined(__FreeBSD__)
    743 	/* This part should be deleted */
    744   	case USB_DISCOVER:
    745   		break;
    746 #endif
    747 	case USB_REQUEST:
    748 	{
    749 		struct usb_ctl_request *ur = (void *)data;
    750 		int len = UGETW(ur->ucr_request.wLength);
    751 		struct iovec iov;
    752 		struct uio uio;
    753 		void *ptr = 0;
    754 		int addr = ur->ucr_addr;
    755 		usbd_status err;
    756 		int error = 0;
    757 
    758 		if (!(flag & FWRITE))
    759 			return (EBADF);
    760 
    761 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    762 		if (len < 0 || len > 32768)
    763 			return (EINVAL);
    764 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    765 		    sc->sc_bus->devices[addr] == 0)
    766 			return (EINVAL);
    767 		if (len != 0) {
    768 			iov.iov_base = (void *)ur->ucr_data;
    769 			iov.iov_len = len;
    770 			uio.uio_iov = &iov;
    771 			uio.uio_iovcnt = 1;
    772 			uio.uio_resid = len;
    773 			uio.uio_offset = 0;
    774 			uio.uio_rw =
    775 				ur->ucr_request.bmRequestType & UT_READ ?
    776 				UIO_READ : UIO_WRITE;
    777 			USB_UIO_SET_PROC(&uio, p);
    778 			ptr = malloc(len, M_TEMP, M_WAITOK);
    779 			if (uio.uio_rw == UIO_WRITE) {
    780 				error = uiomove(ptr, len, &uio);
    781 				if (error)
    782 					goto ret;
    783 			}
    784 		}
    785 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    786 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    787 			  USBD_DEFAULT_TIMEOUT);
    788 		if (err) {
    789 			error = EIO;
    790 			goto ret;
    791 		}
    792 		if (len != 0) {
    793 			if (uio.uio_rw == UIO_READ) {
    794 				error = uiomove(ptr, len, &uio);
    795 				if (error)
    796 					goto ret;
    797 			}
    798 		}
    799 	ret:
    800 		if (ptr)
    801 			free(ptr, M_TEMP);
    802 		return (error);
    803 	}
    804 
    805 	case USB_DEVICEINFO:
    806 	{
    807 		usbd_device_handle dev;
    808 		struct usb_device_info *di = (void *)data;
    809 		int addr = di->udi_addr;
    810 
    811 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    812 			return EINVAL;
    813 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    814 			return ENXIO;
    815 		usbd_fill_deviceinfo(dev, di, 1);
    816 		break;
    817 	}
    818 
    819 #if defined(__NetBSD__) && defined(COMPAT_30)
    820 	case USB_DEVICEINFO_OLD:
    821 	{
    822 		usbd_device_handle dev;
    823 		struct usb_device_info_old *di = (void *)data;
    824 		int addr = di->udi_addr;
    825 
    826 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    827 			return EINVAL;
    828 		if ((dev = sc->sc_bus->devices[addr]) == NULL)
    829 			return ENXIO;
    830 		usbd_fill_deviceinfo_old(dev, di, 1);
    831 		break;
    832 	}
    833 #endif
    834 
    835 	case USB_DEVICESTATS:
    836 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    837 		break;
    838 
    839 	default:
    840 		return (EINVAL);
    841 	}
    842 	return (0);
    843 }
    844 
    845 int
    846 usbpoll(usb_cdev_t dev, int events, usb_proc_ptr p)
    847 {
    848 	int revents, mask, s;
    849 	int unit = USBUNIT(dev);
    850 
    851 	if (unit == USB_DEV_MINOR) {
    852 		revents = 0;
    853 		mask = POLLIN | POLLRDNORM;
    854 
    855 		s = splusb();
    856 		if (events & mask && usb_nevents > 0)
    857 			revents |= events & mask;
    858 		if (revents == 0 && events & mask)
    859 			selrecord(p, &usb_selevent);
    860 		splx(s);
    861 
    862 		return (revents);
    863 	} else {
    864 		return (0);
    865 	}
    866 }
    867 
    868 #if defined(__NetBSD__) || defined(__OpenBSD__)
    869 static void
    870 filt_usbrdetach(struct knote *kn)
    871 {
    872 	int s;
    873 
    874 	s = splusb();
    875 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    876 	splx(s);
    877 }
    878 
    879 static int
    880 filt_usbread(struct knote *kn, long hint)
    881 {
    882 
    883 	if (usb_nevents == 0)
    884 		return (0);
    885 
    886 	kn->kn_data = sizeof(struct usb_event);
    887 	return (1);
    888 }
    889 
    890 static const struct filterops usbread_filtops =
    891 	{ 1, NULL, filt_usbrdetach, filt_usbread };
    892 
    893 int
    894 usbkqfilter(dev_t dev, struct knote *kn)
    895 {
    896 	struct klist *klist;
    897 	int s;
    898 
    899 	switch (kn->kn_filter) {
    900 	case EVFILT_READ:
    901 		if (minor(dev) != USB_DEV_MINOR)
    902 			return (1);
    903 		klist = &usb_selevent.sel_klist;
    904 		kn->kn_fop = &usbread_filtops;
    905 		break;
    906 
    907 	default:
    908 		return (1);
    909 	}
    910 
    911 	kn->kn_hook = NULL;
    912 
    913 	s = splusb();
    914 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    915 	splx(s);
    916 
    917 	return (0);
    918 }
    919 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    920 
    921 /* Explore device tree from the root. */
    922 Static void
    923 usb_discover(void *v)
    924 {
    925 	struct usb_softc *sc = v;
    926 	/* splxxx should be changed to mutexes for preemption safety some day */
    927 	int s;
    928 
    929 	DPRINTFN(2,("usb_discover\n"));
    930 #ifdef USB_DEBUG
    931 	if (usb_noexplore > 1)
    932 		return;
    933 #endif
    934 
    935 	/*
    936 	 * We need mutual exclusion while traversing the device tree,
    937 	 * but this is guaranteed since this function is only called
    938 	 * from the event thread for the controller.
    939 	 */
    940 	s = splusb();
    941 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    942 		sc->sc_bus->needs_explore = 0;
    943 		splx(s);
    944 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    945 		s = splusb();
    946 	}
    947 	splx(s);
    948 }
    949 
    950 void
    951 usb_needs_explore(usbd_device_handle dev)
    952 {
    953 	DPRINTFN(2,("usb_needs_explore\n"));
    954 	dev->bus->needs_explore = 1;
    955 	wakeup(&dev->bus->needs_explore);
    956 }
    957 
    958 void
    959 usb_needs_reattach(usbd_device_handle dev)
    960 {
    961 	DPRINTFN(2,("usb_needs_reattach\n"));
    962 	dev->powersrc->reattach = 1;
    963 	dev->bus->needs_explore = 1;
    964 	wakeup(&dev->bus->needs_explore);
    965 }
    966 
    967 /* Called at splusb() */
    968 int
    969 usb_get_next_event(struct usb_event *ue)
    970 {
    971 	struct usb_event_q *ueq;
    972 
    973 	if (usb_nevents <= 0)
    974 		return (0);
    975 	ueq = TAILQ_FIRST(&usb_events);
    976 #ifdef DIAGNOSTIC
    977 	if (ueq == NULL) {
    978 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
    979 		usb_nevents = 0;
    980 		return (0);
    981 	}
    982 #endif
    983 	if (ue)
    984 		*ue = ueq->ue;
    985 	TAILQ_REMOVE(&usb_events, ueq, next);
    986 	usb_free_event((struct usb_event *)(void *)ueq);
    987 	usb_nevents--;
    988 	return (1);
    989 }
    990 
    991 void
    992 usbd_add_dev_event(int type, usbd_device_handle udev)
    993 {
    994 	struct usb_event *ue = usb_alloc_event();
    995 
    996 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
    997 	usb_add_event(type, ue);
    998 }
    999 
   1000 void
   1001 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
   1002 {
   1003 	struct usb_event *ue = usb_alloc_event();
   1004 
   1005 	ue->u.ue_driver.ue_cookie = udev->cookie;
   1006 	strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
   1007 	    sizeof ue->u.ue_driver.ue_devname);
   1008 	usb_add_event(type, ue);
   1009 }
   1010 
   1011 Static struct usb_event *
   1012 usb_alloc_event(void)
   1013 {
   1014 	/* Yes, this is right; we allocate enough so that we can use it later */
   1015 	return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
   1016 }
   1017 
   1018 Static void
   1019 usb_free_event(struct usb_event *uep)
   1020 {
   1021 	free(uep, M_USBDEV);
   1022 }
   1023 
   1024 Static void
   1025 usb_add_event(int type, struct usb_event *uep)
   1026 {
   1027 	struct usb_event_q *ueq;
   1028 	struct timeval thetime;
   1029 	int s;
   1030 
   1031 	microtime(&thetime);
   1032 	/* Don't want to wait here inside splusb() */
   1033 	ueq = (struct usb_event_q *)(void *)uep;
   1034 	ueq->ue = *uep;
   1035 	ueq->ue.ue_type = type;
   1036 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
   1037 
   1038 	s = splusb();
   1039 	if (USB_EVENT_IS_DETACH(type)) {
   1040 		struct usb_event_q *ueqi, *ueqi_next;
   1041 
   1042 		for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
   1043 			ueqi_next = TAILQ_NEXT(ueqi, next);
   1044 			if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
   1045 			    uep->u.ue_device.udi_cookie.cookie) {
   1046 				TAILQ_REMOVE(&usb_events, ueqi, next);
   1047 				free(ueqi, M_USBDEV);
   1048 				usb_nevents--;
   1049 				ueqi_next = TAILQ_FIRST(&usb_events);
   1050 			}
   1051 		}
   1052 	}
   1053 	if (usb_nevents >= USB_MAX_EVENTS) {
   1054 		/* Too many queued events, drop an old one. */
   1055 		DPRINTFN(-1,("usb: event dropped\n"));
   1056 		(void)usb_get_next_event(0);
   1057 	}
   1058 	TAILQ_INSERT_TAIL(&usb_events, ueq, next);
   1059 	usb_nevents++;
   1060 	wakeup(&usb_events);
   1061 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1062 	selnotify(&usb_selevent, 0);
   1063 #elif defined(__FreeBSD__)
   1064 	selwakeuppri(&usb_selevent, PZERO);
   1065 #endif
   1066 	if (usb_async_proc != NULL) {
   1067 		USB_PROC_LOCK(usb_async_proc);
   1068 		psignal(usb_async_proc, SIGIO);
   1069 		USB_PROC_UNLOCK(usb_async_proc);
   1070 	}
   1071 	splx(s);
   1072 }
   1073 
   1074 void
   1075 usb_schedsoftintr(usbd_bus_handle bus)
   1076 {
   1077 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
   1078 #ifdef USB_USE_SOFTINTR
   1079 	if (bus->use_polling) {
   1080 		bus->methods->soft_intr(bus);
   1081 	} else {
   1082 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1083 		softintr_schedule(bus->soft);
   1084 #else
   1085 		if (!callout_pending(&bus->softi))
   1086 			callout_reset(&bus->softi, 0, bus->methods->soft_intr,
   1087 			    bus);
   1088 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
   1089 	}
   1090 #else
   1091 	bus->methods->soft_intr(bus);
   1092 #endif /* USB_USE_SOFTINTR */
   1093 }
   1094 
   1095 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1096 int
   1097 usb_activate(device_ptr_t self, enum devact act)
   1098 {
   1099 	struct usb_softc *sc = (struct usb_softc *)self;
   1100 	usbd_device_handle dev = sc->sc_port.device;
   1101 	int i, rv = 0;
   1102 
   1103 	switch (act) {
   1104 	case DVACT_ACTIVATE:
   1105 		return (EOPNOTSUPP);
   1106 
   1107 	case DVACT_DEACTIVATE:
   1108 		sc->sc_dying = 1;
   1109 		if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
   1110 			for (i = 0; dev->subdevs[i]; i++)
   1111 				rv |= config_deactivate(dev->subdevs[i]);
   1112 		}
   1113 		break;
   1114 	}
   1115 	return (rv);
   1116 }
   1117 #endif
   1118 
   1119 USB_DETACH(usb)
   1120 {
   1121 	USB_DETACH_START(usb, sc);
   1122 	struct usb_event *ue;
   1123 	struct usb_taskq *taskq;
   1124 	int i;
   1125 
   1126 	DPRINTF(("usb_detach: start\n"));
   1127 
   1128 	sc->sc_dying = 1;
   1129 
   1130 	/* Make all devices disconnect. */
   1131 	if (sc->sc_port.device != NULL)
   1132 		usb_disconnect_port(&sc->sc_port, self);
   1133 
   1134 	/* Kill off event thread. */
   1135 	if (sc->sc_event_thread != NULL) {
   1136 		wakeup(&sc->sc_bus->needs_explore);
   1137 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
   1138 			printf("%s: event thread didn't die\n",
   1139 			       USBDEVNAME(sc->sc_dev));
   1140 		DPRINTF(("usb_detach: event thread dead\n"));
   1141 	}
   1142 
   1143 #ifdef __FreeBSD__
   1144 	destroy_dev(sc->sc_usbdev);
   1145 #endif
   1146 	if (--usb_nbusses == 0) {
   1147 #ifdef __FreeBSD__
   1148 		destroy_dev(usb_dev);
   1149 		usb_dev = NULL;
   1150 #endif
   1151 		for (i = 0; i < USB_NUM_TASKQS; i++) {
   1152 			taskq = &usb_taskq[i];
   1153 			wakeup(&taskq->tasks);
   1154 			if (tsleep(&taskq->taskcreated, PWAIT, "usbtdt",
   1155 			    hz * 60)) {
   1156 				printf("usb task thread %s didn't die\n",
   1157 				    taskq->name);
   1158 			}
   1159 		}
   1160 	}
   1161 
   1162 #ifdef USB_USE_SOFTINTR
   1163 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1164 	if (sc->sc_bus->soft != NULL) {
   1165 		softintr_disestablish(sc->sc_bus->soft);
   1166 		sc->sc_bus->soft = NULL;
   1167 	}
   1168 #else
   1169 	callout_stop(&sc->sc_bus->softi);
   1170 #endif
   1171 #endif
   1172 
   1173 	ue = usb_alloc_event();
   1174 	ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
   1175 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1176 
   1177 	return (0);
   1178 }
   1179 
   1180 #if defined(__NetBSD__) && defined(COMPAT_30)
   1181 Static void
   1182 usb_copy_old_devinfo(struct usb_device_info_old *uo,
   1183 		     const struct usb_device_info *ue)
   1184 {
   1185 	const unsigned char *p;
   1186 	unsigned char *q;
   1187 	int i, n;
   1188 
   1189 	uo->udi_bus = ue->udi_bus;
   1190 	uo->udi_addr = ue->udi_addr;
   1191 	uo->udi_cookie = ue->udi_cookie;
   1192 	for (i = 0, p = (const unsigned char *)ue->udi_product,
   1193 	     q = (unsigned char *)uo->udi_product;
   1194 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1195 		if (*p < 0x80)
   1196 			q[i++] = *p;
   1197 		else {
   1198 			q[i++] = '?';
   1199 			if ((*p & 0xe0) == 0xe0)
   1200 				p++;
   1201 			p++;
   1202 		}
   1203 	}
   1204 	q[i] = 0;
   1205 
   1206 	for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
   1207 	     *p && i < USB_MAX_STRING_LEN - 1; p++) {
   1208 		if (* p < 0x80)
   1209 			q[i++] = *p;
   1210 		else {
   1211 			q[i++] = '?';
   1212 			p++;
   1213 			if ((*p & 0xe0) == 0xe0)
   1214 				p++;
   1215 		}
   1216 	}
   1217 	q[i] = 0;
   1218 
   1219 	memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
   1220 
   1221 	uo->udi_productNo = ue->udi_productNo;
   1222 	uo->udi_vendorNo = ue->udi_vendorNo;
   1223 	uo->udi_releaseNo = ue->udi_releaseNo;
   1224 	uo->udi_class = ue->udi_class;
   1225 	uo->udi_subclass = ue->udi_subclass;
   1226 	uo->udi_protocol = ue->udi_protocol;
   1227 	uo->udi_config = ue->udi_config;
   1228 	uo->udi_speed = ue->udi_speed;
   1229 	uo->udi_power = ue->udi_power;
   1230 	uo->udi_nports = ue->udi_nports;
   1231 
   1232 	for (n=0; n<USB_MAX_DEVNAMES; n++)
   1233 		memcpy(uo->udi_devnames[n],
   1234 		       ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
   1235 	memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
   1236 }
   1237 #endif
   1238 
   1239 #if defined(__FreeBSD__)
   1240 Static void
   1241 usb_child_detached(device_t self, device_t child)
   1242 {
   1243 	struct usb_softc *sc = device_get_softc(self);
   1244 
   1245 	/* XXX, should check it is the right device. */
   1246 	sc->sc_port.device = NULL;
   1247 }
   1248 
   1249 /* Explore USB busses at the end of device configuration. */
   1250 Static void
   1251 usb_cold_explore(void *arg)
   1252 {
   1253 	struct usb_softc *sc;
   1254 
   1255 	USB_KASSERT2(cold || TAILQ_EMPTY(&usb_coldexplist),
   1256 	    ("usb_cold_explore: busses to explore when !cold"));
   1257 	while (!TAILQ_EMPTY(&usb_coldexplist)) {
   1258 		sc = TAILQ_FIRST(&usb_coldexplist);
   1259 		TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
   1260 
   1261 		sc->sc_bus->use_polling++;
   1262 		sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
   1263 		sc->sc_bus->use_polling--;
   1264 	}
   1265 }
   1266 
   1267 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
   1268 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
   1269 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);
   1270 DRIVER_MODULE(usb, slhci, usb_driver, usb_devclass, 0, 0);
   1271 SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
   1272     usb_cold_explore, NULL);
   1273 #endif
   1274