Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.10
      1  1.10  augustss /*	$NetBSD: usb.c,v 1.10 1999/01/03 01:00:56 augustss Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7   1.5  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.5  augustss  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9   1.5  augustss  * Carlstedt Research & Technology.
     10   1.1  augustss  *
     11   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12   1.1  augustss  * modification, are permitted provided that the following conditions
     13   1.1  augustss  * are met:
     14   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19   1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     20   1.1  augustss  *    must display the following acknowledgement:
     21   1.1  augustss  *        This product includes software developed by the NetBSD
     22   1.1  augustss  *        Foundation, Inc. and its contributors.
     23   1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1  augustss  *    contributors may be used to endorse or promote products derived
     25   1.1  augustss  *    from this software without specific prior written permission.
     26   1.1  augustss  *
     27   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1  augustss  */
     39   1.1  augustss 
     40   1.1  augustss /*
     41   1.8  augustss  * USB specifications and other documentation can be found at
     42   1.8  augustss  * http://www.usb.org/developers/data/ and
     43   1.8  augustss  * http://www.usb.org/developers/index.html .
     44   1.1  augustss  */
     45   1.1  augustss 
     46   1.1  augustss #include <sys/param.h>
     47   1.1  augustss #include <sys/systm.h>
     48   1.1  augustss #include <sys/kernel.h>
     49   1.1  augustss #include <sys/malloc.h>
     50   1.7  augustss #if defined(__NetBSD__)
     51   1.1  augustss #include <sys/device.h>
     52   1.7  augustss #elif defined(__FreeBSD__)
     53   1.7  augustss #include <sys/module.h>
     54   1.7  augustss #include <sys/bus.h>
     55   1.7  augustss #include <sys/ioccom.h>
     56   1.7  augustss #include <sys/uio.h>
     57   1.7  augustss #include <sys/conf.h>
     58   1.7  augustss #endif
     59   1.1  augustss #include <sys/poll.h>
     60   1.1  augustss #include <sys/proc.h>
     61   1.1  augustss #include <sys/select.h>
     62   1.1  augustss 
     63   1.1  augustss #include <dev/usb/usb.h>
     64   1.1  augustss 
     65   1.7  augustss #if defined(__FreeBSD__)
     66   1.7  augustss MALLOC_DEFINE(M_USB, "USB", "USB");
     67   1.7  augustss MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
     68   1.7  augustss 
     69   1.7  augustss #include "usb_if.h"
     70   1.7  augustss #endif /* defined(__FreeBSD__) */
     71   1.7  augustss 
     72   1.1  augustss #include <dev/usb/usbdi.h>
     73   1.1  augustss #include <dev/usb/usbdivar.h>
     74   1.1  augustss #include <dev/usb/usb_quirks.h>
     75   1.1  augustss 
     76   1.1  augustss #ifdef USB_DEBUG
     77   1.1  augustss #define DPRINTF(x)	if (usbdebug) printf x
     78   1.1  augustss #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     79   1.1  augustss int	usbdebug = 0;
     80   1.1  augustss int	uhcidebug;
     81   1.1  augustss int	ohcidebug;
     82   1.1  augustss #else
     83   1.1  augustss #define DPRINTF(x)
     84   1.1  augustss #define DPRINTFN(n,x)
     85   1.1  augustss #endif
     86   1.1  augustss 
     87   1.1  augustss #define USBUNIT(dev) (minor(dev))
     88   1.1  augustss 
     89   1.1  augustss struct usb_softc {
     90   1.7  augustss 	bdevice sc_dev;			/* base device */
     91   1.1  augustss 	usbd_bus_handle sc_bus;		/* USB controller */
     92   1.1  augustss 	struct usbd_port sc_port;	/* dummy port for root hub */
     93   1.1  augustss 	char sc_running;
     94   1.1  augustss 	char sc_exploring;
     95   1.1  augustss 	struct selinfo sc_consel;	/* waiting for connect change */
     96   1.1  augustss };
     97   1.1  augustss 
     98   1.7  augustss #if defined(__NetBSD__)
     99   1.1  augustss int usbopen __P((dev_t, int, int, struct proc *));
    100   1.1  augustss int usbclose __P((dev_t, int, int, struct proc *));
    101   1.1  augustss int usbioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    102   1.1  augustss int usbpoll __P((dev_t, int, struct proc *));
    103   1.1  augustss 
    104   1.7  augustss #elif defined(__FreeBSD__)
    105   1.7  augustss static device_probe_t usb_match;
    106   1.7  augustss static device_attach_t usb_attach;
    107   1.7  augustss static bus_print_child_t usb_print_child;
    108   1.7  augustss 
    109   1.7  augustss d_open_t  usbopen;
    110   1.7  augustss d_close_t usbclose;
    111   1.7  augustss d_ioctl_t usbioctl;
    112   1.7  augustss int usbpoll __P((dev_t, int, struct proc *));
    113   1.7  augustss 
    114   1.7  augustss struct cdevsw usb_cdevsw = {
    115   1.7  augustss 	usbopen,     usbclose,    noread,         nowrite,
    116   1.7  augustss 	usbioctl,    nullstop,    nullreset,      nodevtotty,
    117   1.7  augustss 	usbpoll,     nommap,      nostrat,
    118   1.7  augustss 	"usb",        NULL,   -1
    119   1.7  augustss };
    120   1.7  augustss #endif
    121   1.7  augustss 
    122   1.1  augustss usbd_status usb_discover __P((struct usb_softc *));
    123   1.1  augustss 
    124   1.7  augustss USB_DECLARE_DRIVER_INIT(usb, DEVMETHOD(bus_print_child, usb_print_child));
    125   1.1  augustss 
    126   1.7  augustss USB_MATCH(usb)
    127   1.1  augustss {
    128   1.1  augustss 	DPRINTF(("usbd_match\n"));
    129   1.7  augustss 	return (UMATCH_GENERIC);
    130   1.1  augustss }
    131   1.1  augustss 
    132   1.7  augustss USB_ATTACH(usb)
    133   1.1  augustss {
    134   1.7  augustss #if defined(__NetBSD__)
    135   1.1  augustss 	struct usb_softc *sc = (struct usb_softc *)self;
    136   1.7  augustss #elif defined(__FreeBSD__)
    137   1.7  augustss 	struct usb_softc *sc = device_get_softc(device);
    138   1.7  augustss 	void *aux = device_get_ivars(device);
    139   1.7  augustss #endif
    140   1.1  augustss 	usbd_device_handle dev;
    141   1.1  augustss 	usbd_status r;
    142   1.1  augustss 
    143   1.7  augustss #if defined(__NetBSD__)
    144   1.4  augustss 	printf("\n");
    145   1.7  augustss #elif defined(__FreeBSD__)
    146   1.7  augustss 	sc->sc_dev = device;
    147   1.7  augustss #endif
    148   1.4  augustss 
    149   1.1  augustss 	DPRINTF(("usbd_attach\n"));
    150   1.4  augustss 	usbd_init();
    151   1.1  augustss 	sc->sc_bus = aux;
    152   1.1  augustss 	sc->sc_bus->usbctl = sc;
    153   1.1  augustss 	sc->sc_running = 1;
    154   1.3  augustss 	sc->sc_bus->use_polling = 1;
    155   1.1  augustss 	sc->sc_port.power = USB_MAX_POWER;
    156   1.1  augustss 	r = usbd_new_device(&sc->sc_dev, sc->sc_bus, 0, 0, 0, &sc->sc_port);
    157   1.7  augustss 
    158   1.1  augustss 	if (r == USBD_NORMAL_COMPLETION) {
    159   1.1  augustss 		dev = sc->sc_port.device;
    160   1.1  augustss 		if (!dev->hub) {
    161   1.1  augustss 			sc->sc_running = 0;
    162   1.7  augustss 			printf("%s: root device is not a hub\n",
    163   1.7  augustss 			       USBDEVNAME(sc->sc_dev));
    164   1.7  augustss 			USB_ATTACH_ERROR_RETURN;
    165   1.1  augustss 		}
    166   1.1  augustss 		sc->sc_bus->root_hub = dev;
    167   1.7  augustss 		dev->hub->explore(sc->sc_bus->root_hub);
    168   1.1  augustss 	} else {
    169   1.1  augustss 		printf("%s: root hub problem, error=%d\n",
    170   1.7  augustss 		       USBDEVNAME(sc->sc_dev), r);
    171   1.1  augustss 		sc->sc_running = 0;
    172   1.1  augustss 	}
    173   1.3  augustss 	sc->sc_bus->use_polling = 0;
    174   1.7  augustss 
    175   1.7  augustss 	USB_ATTACH_SUCCESS_RETURN;
    176   1.1  augustss }
    177   1.1  augustss 
    178   1.7  augustss #if defined(__NetBSD__)
    179   1.1  augustss int
    180   1.1  augustss usbctlprint(aux, pnp)
    181   1.1  augustss 	void *aux;
    182   1.1  augustss 	const char *pnp;
    183   1.1  augustss {
    184   1.1  augustss 	/* only "usb"es can attach to host controllers */
    185   1.1  augustss 	if (pnp)
    186   1.1  augustss 		printf("usb at %s", pnp);
    187   1.1  augustss 
    188   1.1  augustss 	return (UNCONF);
    189   1.1  augustss }
    190   1.1  augustss 
    191   1.7  augustss #elif defined(__FreeBSD__)
    192   1.7  augustss static void
    193   1.7  augustss usb_print_child(device_t parent, device_t child)
    194   1.7  augustss {
    195   1.7  augustss 	struct usb_softc *sc = device_get_softc(child);
    196   1.7  augustss 
    197   1.7  augustss 	printf(" at %s%d", device_get_name(parent), device_get_unit(parent));
    198   1.7  augustss 
    199   1.7  augustss 	/* How do we get to the usbd_device_handle???
    200   1.7  augustss 	usbd_device_handle dev = invalidadosch;
    201   1.7  augustss 
    202   1.7  augustss 	printf(" addr %d", dev->addr);
    203   1.7  augustss 
    204   1.7  augustss 	if (bootverbose) {
    205   1.7  augustss 		if (dev->lowspeed)
    206   1.7  augustss 			printf(", lowspeed");
    207   1.7  augustss 		if (dev->self_powered)
    208   1.7  augustss 			printf(", self powered");
    209   1.7  augustss 		else
    210   1.7  augustss 			printf(", %dmA", dev->power);
    211   1.7  augustss 		printf(", config %d", dev->config);
    212   1.7  augustss 	}
    213   1.7  augustss 	 */
    214   1.7  augustss }
    215   1.7  augustss 
    216   1.7  augustss /* Reconfigure all the USB busses in the system. */
    217   1.7  augustss int
    218   1.7  augustss usb_driver_load(module_t mod, int what, void *arg)
    219   1.7  augustss {
    220   1.7  augustss 	/* subroutine is there but inactive at the moment
    221   1.7  augustss 	 * the reconfiguration process has not been thought through yet.
    222   1.7  augustss 	 */
    223   1.7  augustss 	devclass_t ugen_devclass = devclass_find("ugen");
    224   1.7  augustss 	device_t *devlist;
    225   1.7  augustss 	int devcount;
    226   1.7  augustss 	int error;
    227   1.7  augustss 
    228   1.7  augustss 	switch (what) {
    229   1.7  augustss 	case MOD_LOAD:
    230   1.7  augustss 	case MOD_UNLOAD:
    231   1.7  augustss 		if (!usb_devclass)
    232   1.7  augustss 			return 0;	/* just ignore call */
    233   1.7  augustss 
    234   1.7  augustss 		if (ugen_devclass) {
    235   1.7  augustss 			/* detach devices from generic driver if possible
    236   1.7  augustss 			 */
    237   1.7  augustss 			error = devclass_get_devices(ugen_devclass, &devlist,
    238   1.7  augustss 						     &devcount);
    239   1.7  augustss 			if (!error)
    240   1.7  augustss 				for (devcount--; devcount >= 0; devcount--)
    241   1.7  augustss 					(void)DEVICE_DETACH(devlist[devcount]);
    242   1.7  augustss 		}
    243   1.7  augustss 
    244   1.7  augustss 		error = devclass_get_devices(usb_devclass, &devlist, &devcount);
    245   1.7  augustss 		if (error)
    246   1.7  augustss 			return 0;	/* XXX maybe transient, or error? */
    247   1.7  augustss 
    248   1.7  augustss 		for (devcount--; devcount >= 0; devcount--)
    249   1.7  augustss 			USB_RECONFIGURE(devlist[devcount]);
    250   1.7  augustss 
    251   1.7  augustss 		free(devlist, M_TEMP);
    252   1.7  augustss 		return 0;
    253   1.7  augustss 	}
    254   1.7  augustss 
    255   1.7  augustss 	return 0;			/* nothing to do by us */
    256   1.7  augustss }
    257   1.7  augustss 
    258   1.7  augustss /* Set the description of the device including a malloc and copy. */
    259   1.7  augustss void
    260   1.7  augustss usb_device_set_desc(device_t device, char *devinfo)
    261   1.7  augustss {
    262   1.7  augustss 	size_t l;
    263   1.7  augustss 	char *desc;
    264   1.7  augustss 
    265   1.7  augustss 	if ( devinfo ) {
    266   1.7  augustss 		l = strlen(devinfo);
    267   1.7  augustss 		desc = malloc(l+1, M_USB, M_NOWAIT);
    268   1.7  augustss 		if (desc)
    269   1.7  augustss 			memcpy(desc, devinfo, l+1);
    270   1.7  augustss 	} else
    271   1.7  augustss 		desc = NULL;
    272   1.7  augustss 
    273   1.7  augustss 	device_set_desc(device, desc);
    274   1.7  augustss }
    275   1.7  augustss 
    276   1.7  augustss /*
    277   1.7  augustss  * A static buffer is a loss if this routine is used from an interrupt,
    278   1.7  augustss  * but it's not fatal.
    279   1.7  augustss  */
    280   1.7  augustss char *
    281   1.7  augustss usb_devname(struct device *bdev)
    282   1.7  augustss {
    283   1.7  augustss 	static char buf[20];
    284   1.7  augustss 
    285   1.7  augustss 	sprintf(buf, "%s%d", device_get_name(*bdev), device_get_unit(*bdev));
    286   1.7  augustss 	return (buf);
    287   1.7  augustss }
    288   1.7  augustss 
    289   1.7  augustss #endif
    290   1.7  augustss 
    291   1.1  augustss int
    292   1.1  augustss usbopen(dev, flag, mode, p)
    293   1.1  augustss 	dev_t dev;
    294   1.1  augustss 	int flag, mode;
    295   1.1  augustss 	struct proc *p;
    296   1.1  augustss {
    297   1.7  augustss 	USB_GET_SC_OPEN(usb, USBUNIT(dev), sc);
    298   1.1  augustss 
    299   1.1  augustss 	if (sc == 0 || !sc->sc_running)
    300   1.1  augustss 		return (ENXIO);
    301   1.1  augustss 
    302   1.1  augustss 	return (0);
    303   1.1  augustss }
    304   1.1  augustss 
    305   1.1  augustss int
    306   1.1  augustss usbclose(dev, flag, mode, p)
    307   1.1  augustss 	dev_t dev;
    308   1.1  augustss 	int flag, mode;
    309   1.1  augustss 	struct proc *p;
    310   1.1  augustss {
    311   1.1  augustss 	return (0);
    312   1.1  augustss }
    313   1.1  augustss 
    314   1.1  augustss int
    315   1.1  augustss usbioctl(dev, cmd, data, flag, p)
    316   1.1  augustss 	dev_t dev;
    317   1.1  augustss 	u_long cmd;
    318   1.1  augustss 	caddr_t data;
    319   1.1  augustss 	int flag;
    320   1.1  augustss 	struct proc *p;
    321   1.1  augustss {
    322   1.7  augustss 	USB_GET_SC(usb, USBUNIT(dev), sc);
    323   1.1  augustss 
    324   1.1  augustss 	if (sc == 0 || !sc->sc_running)
    325   1.1  augustss 		return (ENXIO);
    326   1.1  augustss 	switch (cmd) {
    327   1.1  augustss #ifdef USB_DEBUG
    328   1.1  augustss 	case USB_SETDEBUG:
    329   1.1  augustss 		usbdebug = uhcidebug = ohcidebug = *(int *)data;
    330   1.1  augustss 		break;
    331   1.1  augustss #endif
    332   1.1  augustss 	case USB_DISCOVER:
    333   1.1  augustss 		usb_discover(sc);
    334   1.1  augustss 		break;
    335   1.1  augustss 	case USB_REQUEST:
    336   1.1  augustss 	{
    337   1.1  augustss 		struct usb_ctl_request *ur = (void *)data;
    338   1.1  augustss 		int len = UGETW(ur->request.wLength);
    339   1.1  augustss 		struct iovec iov;
    340   1.1  augustss 		struct uio uio;
    341   1.1  augustss 		void *ptr = 0;
    342   1.1  augustss 		int addr = ur->addr;
    343   1.1  augustss 		usbd_status r;
    344   1.1  augustss 		int error = 0;
    345   1.1  augustss 
    346   1.9  augustss 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    347   1.1  augustss 		if (len < 0 || len > 32768)
    348   1.9  augustss 			return (EINVAL);
    349   1.1  augustss 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    350   1.1  augustss 		    sc->sc_bus->devices[addr] == 0)
    351   1.9  augustss 			return (EINVAL);
    352   1.1  augustss 		if (len != 0) {
    353   1.1  augustss 			iov.iov_base = (caddr_t)ur->data;
    354   1.1  augustss 			iov.iov_len = len;
    355   1.1  augustss 			uio.uio_iov = &iov;
    356   1.1  augustss 			uio.uio_iovcnt = 1;
    357   1.1  augustss 			uio.uio_resid = len;
    358   1.1  augustss 			uio.uio_offset = 0;
    359   1.1  augustss 			uio.uio_segflg = UIO_USERSPACE;
    360   1.1  augustss 			uio.uio_rw =
    361   1.1  augustss 				ur->request.bmRequestType & UT_READ ?
    362   1.1  augustss 				UIO_READ : UIO_WRITE;
    363   1.1  augustss 			uio.uio_procp = p;
    364   1.1  augustss 			ptr = malloc(len, M_TEMP, M_WAITOK);
    365   1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
    366   1.1  augustss 				error = uiomove(ptr, len, &uio);
    367   1.1  augustss 				if (error)
    368   1.1  augustss 					goto ret;
    369   1.1  augustss 			}
    370   1.1  augustss 		}
    371   1.9  augustss 		r = usbd_do_request_flags(sc->sc_bus->devices[addr],
    372  1.10  augustss 					  &ur->request, ptr,
    373  1.10  augustss 					  ur->flags, &ur->actlen);
    374   1.1  augustss 		if (r) {
    375   1.1  augustss 			error = EIO;
    376   1.1  augustss 			goto ret;
    377   1.1  augustss 		}
    378   1.1  augustss 		if (len != 0) {
    379   1.1  augustss 			if (uio.uio_rw == UIO_READ) {
    380   1.1  augustss 				error = uiomove(ptr, len, &uio);
    381   1.1  augustss 				if (error)
    382   1.1  augustss 					goto ret;
    383   1.1  augustss 			}
    384   1.1  augustss 		}
    385   1.1  augustss 	ret:
    386   1.1  augustss 		if (ptr)
    387   1.1  augustss 			free(ptr, M_TEMP);
    388   1.1  augustss 		return (error);
    389   1.1  augustss 	}
    390   1.1  augustss 
    391   1.1  augustss 	case USB_DEVICEINFO:
    392   1.1  augustss 	{
    393   1.1  augustss 		struct usb_device_info *di = (void *)data;
    394   1.1  augustss 		int addr = di->addr;
    395   1.1  augustss 		usbd_device_handle dev;
    396   1.1  augustss 
    397   1.1  augustss 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    398   1.1  augustss 			return (EINVAL);
    399   1.1  augustss 		dev = sc->sc_bus->devices[addr];
    400   1.1  augustss 		if (dev == 0)
    401   1.1  augustss 			return (ENXIO);
    402   1.6  augustss 		usbd_fill_deviceinfo(dev, di);
    403   1.1  augustss 		break;
    404   1.1  augustss 	}
    405   1.2  augustss 
    406   1.2  augustss 	case USB_DEVICESTATS:
    407   1.2  augustss 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    408   1.2  augustss 		break;
    409   1.1  augustss 
    410   1.1  augustss 	default:
    411   1.1  augustss 		return (ENXIO);
    412   1.1  augustss 	}
    413   1.1  augustss 	return (0);
    414   1.1  augustss }
    415   1.1  augustss 
    416   1.1  augustss int
    417   1.1  augustss usbpoll(dev, events, p)
    418   1.1  augustss 	dev_t dev;
    419   1.1  augustss 	int events;
    420   1.1  augustss 	struct proc *p;
    421   1.1  augustss {
    422   1.1  augustss 	int revents, s;
    423   1.7  augustss 	USB_GET_SC(usb, USBUNIT(dev), sc);
    424   1.1  augustss 
    425   1.1  augustss 	DPRINTFN(2, ("usbpoll: sc=%p events=0x%x\n", sc, events));
    426   1.1  augustss 	s = splusb();
    427   1.1  augustss 	revents = 0;
    428   1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    429   1.1  augustss 		if (sc->sc_bus->needs_explore)
    430   1.1  augustss 			revents |= events & (POLLOUT | POLLWRNORM);
    431   1.1  augustss 	DPRINTFN(2, ("usbpoll: revents=0x%x\n", revents));
    432   1.1  augustss 	if (revents == 0) {
    433   1.1  augustss 		if (events & (POLLOUT | POLLWRNORM)) {
    434   1.1  augustss 			DPRINTFN(2, ("usbpoll: selrecord\n"));
    435   1.1  augustss 			selrecord(p, &sc->sc_consel);
    436   1.1  augustss 		}
    437   1.1  augustss 	}
    438   1.1  augustss 	splx(s);
    439   1.1  augustss 	return (revents);
    440   1.1  augustss }
    441   1.1  augustss 
    442   1.1  augustss int
    443   1.1  augustss usb_bus_count()
    444   1.1  augustss {
    445   1.1  augustss 	int i, n;
    446   1.1  augustss 
    447   1.1  augustss 	for (i = n = 0; i < usb_cd.cd_ndevs; i++)
    448   1.1  augustss 		if (usb_cd.cd_devs[i])
    449   1.1  augustss 			n++;
    450   1.1  augustss 	return (n);
    451   1.1  augustss }
    452   1.1  augustss 
    453   1.1  augustss usbd_status
    454   1.1  augustss usb_get_bus_handle(n, h)
    455   1.1  augustss 	int n;
    456   1.1  augustss 	usbd_bus_handle *h;
    457   1.1  augustss {
    458   1.1  augustss 	int i;
    459   1.1  augustss 
    460   1.1  augustss 	for (i = 0; i < usb_cd.cd_ndevs; i++)
    461   1.1  augustss 		if (usb_cd.cd_devs[i] && n-- == 0) {
    462   1.1  augustss 			*h = usb_cd.cd_devs[i];
    463   1.1  augustss 			return (USBD_NORMAL_COMPLETION);
    464   1.1  augustss 		}
    465   1.1  augustss 	return (USBD_INVAL);
    466   1.1  augustss }
    467   1.1  augustss 
    468   1.1  augustss usbd_status
    469   1.1  augustss usb_discover(sc)
    470   1.1  augustss 	struct usb_softc *sc;
    471   1.1  augustss {
    472   1.1  augustss 	int s;
    473   1.1  augustss 
    474   1.1  augustss 	/* Explore device tree from the root */
    475   1.1  augustss 	/* We need mutual exclusion while traversing the device tree. */
    476   1.1  augustss 	s = splusb();
    477   1.1  augustss 	while (sc->sc_exploring)
    478   1.1  augustss 		tsleep(&sc->sc_exploring, PRIBIO, "usbdis", 0);
    479   1.1  augustss 	sc->sc_exploring = 1;
    480   1.1  augustss 	sc->sc_bus->needs_explore = 0;
    481   1.1  augustss 	splx(s);
    482   1.1  augustss 
    483   1.7  augustss 	sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    484   1.1  augustss 
    485   1.1  augustss 	s = splusb();
    486   1.1  augustss 	sc->sc_exploring = 0;
    487   1.1  augustss 	wakeup(&sc->sc_exploring);
    488   1.1  augustss 	splx(s);
    489   1.1  augustss 	/* XXX should we start over if sc_needsexplore is set again? */
    490   1.1  augustss 	return (0);
    491   1.1  augustss }
    492   1.1  augustss 
    493   1.1  augustss void
    494   1.1  augustss usb_needs_explore(bus)
    495   1.1  augustss 	usbd_bus_handle bus;
    496   1.1  augustss {
    497   1.1  augustss 	bus->needs_explore = 1;
    498   1.1  augustss 	selwakeup(&bus->usbctl->sc_consel);
    499   1.1  augustss }
    500   1.7  augustss 
    501   1.7  augustss #if defined(__FreeBSD__)
    502   1.7  augustss int
    503   1.7  augustss usb_detach(device_t self)
    504   1.7  augustss {
    505   1.7  augustss 	struct usb_softc *sc = device_get_softc(self);
    506   1.7  augustss 	char *devinfo = (char *) device_get_desc(self);
    507   1.7  augustss 
    508   1.7  augustss 	if (devinfo) {
    509   1.7  augustss 		device_set_desc(self, NULL);
    510   1.7  augustss 		free(devinfo, M_USB);
    511   1.7  augustss 	}
    512   1.7  augustss 
    513   1.7  augustss 	return (0);
    514   1.7  augustss }
    515   1.7  augustss 
    516   1.7  augustss DRIVER_MODULE(usb, root, usb_driver, usb_devclass, 0, 0);
    517   1.7  augustss #endif
    518