Home | History | Annotate | Line # | Download | only in usb
usb_subr.c revision 1.70
      1 /*	$NetBSD: usb_subr.c,v 1.70 2000/03/29 01:45:20 augustss Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (augustss (at) carlstedt.se) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/malloc.h>
     45 #if defined(__NetBSD__) || defined(__OpenBSD__)
     46 #include <sys/device.h>
     47 #include <sys/select.h>
     48 #elif defined(__FreeBSD__)
     49 #include <sys/module.h>
     50 #include <sys/bus.h>
     51 #endif
     52 #include <sys/proc.h>
     53 
     54 #include <machine/bus.h>
     55 
     56 #include <dev/usb/usb.h>
     57 
     58 #include <dev/usb/usbdi.h>
     59 #include <dev/usb/usbdi_util.h>
     60 #include <dev/usb/usbdivar.h>
     61 #include <dev/usb/usbdevs.h>
     62 #include <dev/usb/usb_quirks.h>
     63 
     64 #if defined(__FreeBSD__)
     65 #include <machine/clock.h>
     66 #define delay(d)         DELAY(d)
     67 #endif
     68 
     69 #ifdef USB_DEBUG
     70 #define DPRINTF(x)	if (usbdebug) logprintf x
     71 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     72 extern int usbdebug;
     73 #else
     74 #define DPRINTF(x)
     75 #define DPRINTFN(n,x)
     76 #endif
     77 
     78 Static usbd_status	usbd_set_config __P((usbd_device_handle, int));
     79 Static char *usbd_get_string __P((usbd_device_handle, int, char *));
     80 Static int usbd_getnewaddr __P((usbd_bus_handle bus));
     81 #if defined(__NetBSD__)
     82 Static int usbd_print __P((void *aux, const char *pnp));
     83 Static int usbd_submatch __P((device_ptr_t, struct cfdata *cf, void *));
     84 #elif defined(__OpenBSD__)
     85 Static int usbd_submatch __P((device_ptr_t, void *, void *));
     86 #endif
     87 Static void usbd_free_iface_data __P((usbd_device_handle dev, int ifcno));
     88 Static void usbd_kill_pipe __P((usbd_pipe_handle));
     89 Static usbd_status usbd_probe_and_attach
     90 	__P((device_ptr_t parent, usbd_device_handle dev, int port, int addr));
     91 
     92 Static u_int32_t usb_cookie_no = 0;
     93 
     94 #ifdef USBVERBOSE
     95 typedef u_int16_t usb_vendor_id_t;
     96 typedef u_int16_t usb_product_id_t;
     97 
     98 /*
     99  * Descriptions of of known vendors and devices ("products").
    100  */
    101 struct usb_knowndev {
    102 	usb_vendor_id_t		vendor;
    103 	usb_product_id_t	product;
    104 	int			flags;
    105 	char			*vendorname, *productname;
    106 };
    107 #define	USB_KNOWNDEV_NOPROD	0x01		/* match on vendor only */
    108 
    109 #include <dev/usb/usbdevs_data.h>
    110 #endif /* USBVERBOSE */
    111 
    112 Static const char *usbd_error_strs[] = {
    113 	"NORMAL_COMPLETION",
    114 	"IN_PROGRESS",
    115 	"PENDING_REQUESTS",
    116 	"NOT_STARTED",
    117 	"INVAL",
    118 	"NOMEM",
    119 	"CANCELLED",
    120 	"BAD_ADDRESS",
    121 	"IN_USE",
    122 	"NO_ADDR",
    123 	"SET_ADDR_FAILED",
    124 	"NO_POWER",
    125 	"TOO_DEEP",
    126 	"IOERROR",
    127 	"NOT_CONFIGURED",
    128 	"TIMEOUT",
    129 	"SHORT_XFER",
    130 	"STALLED",
    131 	"INTERRUPTED",
    132 	"XXX",
    133 };
    134 
    135 const char *
    136 usbd_errstr(err)
    137 	usbd_status err;
    138 {
    139 	static char buffer[5];
    140 
    141 	if (err < USBD_ERROR_MAX) {
    142 		return usbd_error_strs[err];
    143 	} else {
    144 		snprintf(buffer, sizeof buffer, "%d", err);
    145 		return buffer;
    146 	}
    147 }
    148 
    149 usbd_status
    150 usbd_get_string_desc(dev, sindex, langid, sdesc)
    151 	usbd_device_handle dev;
    152 	int sindex;
    153 	int langid;
    154 	usb_string_descriptor_t *sdesc;
    155 {
    156 	usb_device_request_t req;
    157 	usbd_status err;
    158 
    159 	req.bmRequestType = UT_READ_DEVICE;
    160 	req.bRequest = UR_GET_DESCRIPTOR;
    161 	USETW2(req.wValue, UDESC_STRING, sindex);
    162 	USETW(req.wIndex, langid);
    163 	USETW(req.wLength, 1);	/* only size byte first */
    164 	err = usbd_do_request(dev, &req, sdesc);
    165 	if (err)
    166 		return (err);
    167 	USETW(req.wLength, sdesc->bLength);	/* the whole string */
    168 	return (usbd_do_request(dev, &req, sdesc));
    169 }
    170 
    171 char *
    172 usbd_get_string(dev, si, buf)
    173 	usbd_device_handle dev;
    174 	int si;
    175 	char *buf;
    176 {
    177 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
    178 	usb_string_descriptor_t us;
    179 	char *s;
    180 	int i, n;
    181 	u_int16_t c;
    182 	usbd_status err;
    183 
    184 	if (si == 0)
    185 		return (0);
    186 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
    187 		return (0);
    188 	if (dev->langid == USBD_NOLANG) {
    189 		/* Set up default language */
    190 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us);
    191 		if (err || us.bLength < 4) {
    192 			dev->langid = 0; /* Well, just pick English then */
    193 		} else {
    194 			/* Pick the first language as the default. */
    195 			dev->langid = UGETW(us.bString[0]);
    196 		}
    197 	}
    198 	err = usbd_get_string_desc(dev, si, dev->langid, &us);
    199 	if (err)
    200 		return (0);
    201 	s = buf;
    202 	n = us.bLength / 2 - 1;
    203 	for (i = 0; i < n; i++) {
    204 		c = UGETW(us.bString[i]);
    205 		/* Convert from Unicode, handle buggy strings. */
    206 		if ((c & 0xff00) == 0)
    207 			*s++ = c;
    208 		else if ((c & 0x00ff) == 0 && swap)
    209 			*s++ = c >> 8;
    210 		else
    211 			*s++ = '?';
    212 	}
    213 	*s++ = 0;
    214 	return (buf);
    215 }
    216 
    217 void
    218 usbd_devinfo_vp(dev, v, p)
    219 	usbd_device_handle dev;
    220 	char *v, *p;
    221 {
    222 	usb_device_descriptor_t *udd = &dev->ddesc;
    223 	char *vendor = 0, *product = 0;
    224 #ifdef USBVERBOSE
    225 	struct usb_knowndev *kdp;
    226 #endif
    227 
    228 	if (dev == NULL) {
    229 		v[0] = p[0] = '\0';
    230 		return;
    231 	}
    232 
    233 	vendor = usbd_get_string(dev, udd->iManufacturer, v);
    234 	product = usbd_get_string(dev, udd->iProduct, p);
    235 #ifdef USBVERBOSE
    236 	if (vendor == NULL || product == NULL) {
    237 		for(kdp = usb_knowndevs;
    238 		    kdp->vendorname != NULL;
    239 		    kdp++) {
    240 			if (kdp->vendor == UGETW(udd->idVendor) &&
    241 			    (kdp->product == UGETW(udd->idProduct) ||
    242 			     (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
    243 				break;
    244 		}
    245 		if (kdp->vendorname != NULL) {
    246 			if (!vendor)
    247 			    vendor = kdp->vendorname;
    248 			if (!product)
    249 			    product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
    250 				kdp->productname : NULL;
    251 		}
    252 	}
    253 #endif
    254 	if (vendor != NULL)
    255 		strcpy(v, vendor);
    256 	else
    257 		sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
    258 	if (product != NULL)
    259 		strcpy(p, product);
    260 	else
    261 		sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
    262 }
    263 
    264 int
    265 usbd_printBCD(cp, bcd)
    266 	char *cp;
    267 	int bcd;
    268 {
    269 	return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
    270 }
    271 
    272 void
    273 usbd_devinfo(dev, showclass, cp)
    274 	usbd_device_handle dev;
    275 	int showclass;
    276 	char *cp;
    277 {
    278 	usb_device_descriptor_t *udd = &dev->ddesc;
    279 	char vendor[USB_MAX_STRING_LEN];
    280 	char product[USB_MAX_STRING_LEN];
    281 	int bcdDevice, bcdUSB;
    282 
    283 	usbd_devinfo_vp(dev, vendor, product);
    284 	cp += sprintf(cp, "%s %s", vendor, product);
    285 	if (showclass)
    286 		cp += sprintf(cp, ", class %d/%d",
    287 			      udd->bDeviceClass, udd->bDeviceSubClass);
    288 	bcdUSB = UGETW(udd->bcdUSB);
    289 	bcdDevice = UGETW(udd->bcdDevice);
    290 	cp += sprintf(cp, ", rev ");
    291 	cp += usbd_printBCD(cp, bcdUSB);
    292 	*cp++ = '/';
    293 	cp += usbd_printBCD(cp, bcdDevice);
    294 	cp += sprintf(cp, ", addr %d", dev->address);
    295 	*cp = 0;
    296 }
    297 
    298 /* Delay for a certain number of ms */
    299 void
    300 usb_delay_ms(bus, ms)
    301 	usbd_bus_handle bus;
    302 	u_int ms;
    303 {
    304 	extern int cold;
    305 
    306 	/* Wait at least two clock ticks so we know the time has passed. */
    307 	if (bus->use_polling || cold)
    308 		delay((ms+1) * 1000);
    309 	else
    310 		tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
    311 }
    312 
    313 /* Delay given a device handle. */
    314 void
    315 usbd_delay_ms(dev, ms)
    316 	usbd_device_handle dev;
    317 	u_int ms;
    318 {
    319 	usb_delay_ms(dev->bus, ms);
    320 }
    321 
    322 usbd_status
    323 usbd_reset_port(dev, port, ps)
    324 	usbd_device_handle dev;
    325 	int port;
    326 	usb_port_status_t *ps;
    327 {
    328 	usb_device_request_t req;
    329 	usbd_status err;
    330 	int n;
    331 
    332 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    333 	req.bRequest = UR_SET_FEATURE;
    334 	USETW(req.wValue, UHF_PORT_RESET);
    335 	USETW(req.wIndex, port);
    336 	USETW(req.wLength, 0);
    337 	err = usbd_do_request(dev, &req, 0);
    338 	DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
    339 		    port, usbd_errstr(err)));
    340 	if (err)
    341 		return (err);
    342 	n = 10;
    343 	do {
    344 		/* Wait for device to recover from reset. */
    345 		usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
    346 		err = usbd_get_port_status(dev, port, ps);
    347 		if (err) {
    348 			DPRINTF(("usbd_reset_port: get status failed %d\n",
    349 				 err));
    350 			return (err);
    351 		}
    352 	} while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
    353 	if (n == 0) {
    354 		printf("usbd_reset_port: timeout\n");
    355 		return (USBD_IOERROR);
    356 	}
    357 	err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    358 #ifdef USB_DEBUG
    359 	if (err)
    360 		DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
    361 			 err));
    362 #endif
    363 
    364 	/* Wait for the device to recover from reset. */
    365 	usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
    366 	return (err);
    367 }
    368 
    369 usb_interface_descriptor_t *
    370 usbd_find_idesc(cd, ifaceidx, altidx)
    371 	usb_config_descriptor_t *cd;
    372 	int ifaceidx;
    373 	int altidx;
    374 {
    375 	char *p = (char *)cd;
    376 	char *end = p + UGETW(cd->wTotalLength);
    377 	usb_interface_descriptor_t *d;
    378 	int curidx, lastidx, curaidx = 0;
    379 
    380 	for (curidx = lastidx = -1; p < end; ) {
    381 		d = (usb_interface_descriptor_t *)p;
    382 		DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
    383 			    "type=%d\n",
    384 			    ifaceidx, curidx, altidx, curaidx,
    385 			    d->bLength, d->bDescriptorType));
    386 		if (d->bLength == 0) /* bad descriptor */
    387 			break;
    388 		p += d->bLength;
    389 		if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
    390 			if (d->bInterfaceNumber != lastidx) {
    391 				lastidx = d->bInterfaceNumber;
    392 				curidx++;
    393 				curaidx = 0;
    394 			} else
    395 				curaidx++;
    396 			if (ifaceidx == curidx && altidx == curaidx)
    397 				return (d);
    398 		}
    399 	}
    400 	return (NULL);
    401 }
    402 
    403 usb_endpoint_descriptor_t *
    404 usbd_find_edesc(cd, ifaceidx, altidx, endptidx)
    405 	usb_config_descriptor_t *cd;
    406 	int ifaceidx;
    407 	int altidx;
    408 	int endptidx;
    409 {
    410 	char *p = (char *)cd;
    411 	char *end = p + UGETW(cd->wTotalLength);
    412 	usb_interface_descriptor_t *d;
    413 	usb_endpoint_descriptor_t *e;
    414 	int curidx;
    415 
    416 	d = usbd_find_idesc(cd, ifaceidx, altidx);
    417 	if (d == NULL)
    418 		return (NULL);
    419 	if (endptidx >= d->bNumEndpoints) /* quick exit */
    420 		return (NULL);
    421 
    422 	curidx = -1;
    423 	for (p = (char *)d + d->bLength; p < end; ) {
    424 		e = (usb_endpoint_descriptor_t *)p;
    425 		if (e->bLength == 0) /* bad descriptor */
    426 			break;
    427 		p += e->bLength;
    428 		if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
    429 			return (NULL);
    430 		if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
    431 			curidx++;
    432 			if (curidx == endptidx)
    433 				return (e);
    434 		}
    435 	}
    436 	return (NULL);
    437 }
    438 
    439 usbd_status
    440 usbd_fill_iface_data(dev, ifaceidx, altidx)
    441 	usbd_device_handle dev;
    442 	int ifaceidx;
    443 	int altidx;
    444 {
    445 	usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
    446 	char *p, *end;
    447 	int endpt, nendpt;
    448 
    449 	DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
    450 		    ifaceidx, altidx));
    451 	ifc->device = dev;
    452 	ifc->idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
    453 	if (ifc->idesc == 0)
    454 		return (USBD_INVAL);
    455 	ifc->index = ifaceidx;
    456 	ifc->altindex = altidx;
    457 	nendpt = ifc->idesc->bNumEndpoints;
    458 	DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
    459 	if (nendpt != 0) {
    460 		ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
    461 					M_USB, M_NOWAIT);
    462 		if (ifc->endpoints == NULL)
    463 			return (USBD_NOMEM);
    464 	} else
    465 		ifc->endpoints = NULL;
    466 	ifc->priv = NULL;
    467 	p = (char *)ifc->idesc + ifc->idesc->bLength;
    468 	end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
    469 #define ed ((usb_endpoint_descriptor_t *)p)
    470 	for (endpt = 0; endpt < nendpt; endpt++) {
    471 		DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
    472 		for (; p < end; p += ed->bLength) {
    473 			ed = (usb_endpoint_descriptor_t *)p;
    474 			DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
    475 				     "len=%d type=%d\n",
    476 				 p, end, ed->bLength, ed->bDescriptorType));
    477 			if (p + ed->bLength <= end && ed->bLength != 0 &&
    478 			    ed->bDescriptorType == UDESC_ENDPOINT)
    479 				goto found;
    480 			if (ed->bLength == 0 ||
    481 			    ed->bDescriptorType == UDESC_INTERFACE)
    482 				break;
    483 		}
    484 		/* passed end, or bad desc */
    485 		DPRINTF(("usbd_fill_iface_data: bad descriptor(s): %s\n",
    486 			 ed->bLength == 0 ? "0 length" :
    487 			 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
    488 			 "out of data"));
    489 		goto bad;
    490 	found:
    491 		ifc->endpoints[endpt].edesc = ed;
    492 		ifc->endpoints[endpt].refcnt = 0;
    493 		p += ed->bLength;
    494 	}
    495 #undef ed
    496 	LIST_INIT(&ifc->pipes);
    497 	return (USBD_NORMAL_COMPLETION);
    498 
    499  bad:
    500 	if (ifc->endpoints != NULL)
    501 		free(ifc->endpoints, M_USB);
    502 	return (USBD_INVAL);
    503 }
    504 
    505 void
    506 usbd_free_iface_data(dev, ifcno)
    507 	usbd_device_handle dev;
    508 	int ifcno;
    509 {
    510 	usbd_interface_handle ifc = &dev->ifaces[ifcno];
    511 	if (ifc->endpoints)
    512 		free(ifc->endpoints, M_USB);
    513 }
    514 
    515 Static usbd_status
    516 usbd_set_config(dev, conf)
    517 	usbd_device_handle dev;
    518 	int conf;
    519 {
    520 	usb_device_request_t req;
    521 
    522 	req.bmRequestType = UT_WRITE_DEVICE;
    523 	req.bRequest = UR_SET_CONFIG;
    524 	USETW(req.wValue, conf);
    525 	USETW(req.wIndex, 0);
    526 	USETW(req.wLength, 0);
    527 	return (usbd_do_request(dev, &req, 0));
    528 }
    529 
    530 usbd_status
    531 usbd_set_config_no(dev, no, msg)
    532 	usbd_device_handle dev;
    533 	int no;
    534 	int msg;
    535 {
    536 	int index;
    537 	usb_config_descriptor_t cd;
    538 	usbd_status err;
    539 
    540 	DPRINTFN(5,("usbd_set_config_no: %d\n", no));
    541 	/* Figure out what config index to use. */
    542 	for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
    543 		err = usbd_get_config_desc(dev, index, &cd);
    544 		if (err)
    545 			return (err);
    546 		if (cd.bConfigurationValue == no)
    547 			return (usbd_set_config_index(dev, index, msg));
    548 	}
    549 	return (USBD_INVAL);
    550 }
    551 
    552 usbd_status
    553 usbd_set_config_index(dev, index, msg)
    554 	usbd_device_handle dev;
    555 	int index;
    556 	int msg;
    557 {
    558 	usb_status_t ds;
    559 	usb_config_descriptor_t cd, *cdp;
    560 	usbd_status err;
    561 	int ifcidx, nifc, len, selfpowered, power;
    562 
    563 	DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
    564 
    565 	/* XXX check that all interfaces are idle */
    566 	if (dev->config != 0) {
    567 		DPRINTF(("usbd_set_config_index: free old config\n"));
    568 		/* Free all configuration data structures. */
    569 		nifc = dev->cdesc->bNumInterface;
    570 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
    571 			usbd_free_iface_data(dev, ifcidx);
    572 		free(dev->ifaces, M_USB);
    573 		free(dev->cdesc, M_USB);
    574 		dev->ifaces = NULL;
    575 		dev->cdesc = NULL;
    576 		dev->config = 0;
    577 	}
    578 
    579 	/* Figure out what config number to use. */
    580 	err = usbd_get_config_desc(dev, index, &cd);
    581 	if (err)
    582 		return (err);
    583 	len = UGETW(cd.wTotalLength);
    584 	cdp = malloc(len, M_USB, M_NOWAIT);
    585 	if (cdp == NULL)
    586 		return (USBD_NOMEM);
    587 	err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
    588 	if (err)
    589 		goto bad;
    590 	if (cdp->bDescriptorType != UDESC_CONFIG) {
    591 		DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
    592 			     cdp->bDescriptorType));
    593 		err = USBD_INVAL;
    594 		goto bad;
    595 	}
    596 	selfpowered = 0;
    597 	if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
    598 	    cdp->bmAttributes & UC_SELF_POWERED) {
    599 		/* May be self powered. */
    600 		if (cdp->bmAttributes & UC_BUS_POWERED) {
    601 			/* Must ask device. */
    602 			err = usbd_get_device_status(dev, &ds);
    603 			if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
    604 				selfpowered = 1;
    605 			DPRINTF(("usbd_set_config_index: status=0x%04x, "
    606 				 "error=%s\n",
    607 				 UGETW(ds.wStatus), usbd_errstr(err)));
    608 		} else
    609 			selfpowered = 1;
    610 	}
    611 	DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, "
    612 		 "selfpowered=%d, power=%d\n",
    613 		 dev->address, cdp->bmAttributes,
    614 		 selfpowered, cdp->bMaxPower * 2));
    615 #ifdef USB_DEBUG
    616 	if (dev->powersrc == NULL) {
    617 		DPRINTF(("usbd_set_config_index: No power source?\n"));
    618 		return (USBD_IOERROR);
    619 	}
    620 #endif
    621 	power = cdp->bMaxPower * 2;
    622 	if (power > dev->powersrc->power) {
    623 		/* XXX print nicer message. */
    624 		if (msg)
    625 			printf("%s: device addr %d (config %d) exceeds power "
    626 				 "budget, %d mA > %d mA\n",
    627 			       USBDEVNAME(dev->bus->bdev), dev->address,
    628 			       cdp->bConfigurationValue,
    629 			       power, dev->powersrc->power);
    630 		err = USBD_NO_POWER;
    631 		goto bad;
    632 	}
    633 	dev->power = power;
    634 	dev->self_powered = selfpowered;
    635 
    636 	DPRINTF(("usbd_set_config_index: set config %d\n",
    637 		 cdp->bConfigurationValue));
    638 	err = usbd_set_config(dev, cdp->bConfigurationValue);
    639 	if (err) {
    640 		DPRINTF(("usbd_set_config_index: setting config=%d failed, "
    641 			 "error=%s\n",
    642 			 cdp->bConfigurationValue, usbd_errstr(err)));
    643 		goto bad;
    644 	}
    645 	DPRINTF(("usbd_set_config_index: setting new config %d\n",
    646 		 cdp->bConfigurationValue));
    647 	nifc = cdp->bNumInterface;
    648 	dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
    649 			     M_USB, M_NOWAIT);
    650 	if (dev->ifaces == NULL) {
    651 		err = USBD_NOMEM;
    652 		goto bad;
    653 	}
    654 	DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
    655 	dev->cdesc = cdp;
    656 	dev->config = cdp->bConfigurationValue;
    657 	for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
    658 		err = usbd_fill_iface_data(dev, ifcidx, 0);
    659 		if (err) {
    660 			while (--ifcidx >= 0)
    661 				usbd_free_iface_data(dev, ifcidx);
    662 			goto bad;
    663 		}
    664 	}
    665 
    666 	return (USBD_NORMAL_COMPLETION);
    667 
    668  bad:
    669 	free(cdp, M_USB);
    670 	return (err);
    671 }
    672 
    673 /* XXX add function for alternate settings */
    674 
    675 usbd_status
    676 usbd_setup_pipe(dev, iface, ep, ival, pipe)
    677 	usbd_device_handle dev;
    678 	usbd_interface_handle iface;
    679 	struct usbd_endpoint *ep;
    680 	int ival;
    681 	usbd_pipe_handle *pipe;
    682 {
    683 	usbd_pipe_handle p;
    684 	usbd_status err;
    685 
    686 	DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
    687 		    dev, iface, ep, pipe));
    688 	p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
    689 	if (p == NULL)
    690 		return (USBD_NOMEM);
    691 	p->device = dev;
    692 	p->iface = iface;
    693 	p->endpoint = ep;
    694 	ep->refcnt++;
    695 	p->refcnt = 1;
    696 	p->intrxfer = 0;
    697 	p->running = 0;
    698 	p->aborting = 0;
    699 	p->repeat = 0;
    700 	p->interval = ival;
    701 	SIMPLEQ_INIT(&p->queue);
    702 	usb_callout_init(p->abort_handle);
    703 	err = dev->bus->methods->open_pipe(p);
    704 	if (err) {
    705 		DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
    706 			 "%s\n",
    707 			 ep->edesc->bEndpointAddress, usbd_errstr(err)));
    708 		free(p, M_USB);
    709 		return (err);
    710 	}
    711 	/* Clear any stall and make sure DATA0 toggle will be used next. */
    712 	if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
    713 		usbd_clear_endpoint_stall(p);
    714 	*pipe = p;
    715 	return (USBD_NORMAL_COMPLETION);
    716 }
    717 
    718 /* Abort the device control pipe. */
    719 void
    720 usbd_kill_pipe(pipe)
    721 	usbd_pipe_handle pipe;
    722 {
    723 	pipe->methods->close(pipe);
    724 	pipe->endpoint->refcnt--;
    725 	free(pipe, M_USB);
    726 }
    727 
    728 int
    729 usbd_getnewaddr(bus)
    730 	usbd_bus_handle bus;
    731 {
    732 	int addr;
    733 
    734 	for (addr = 1; addr < USB_MAX_DEVICES; addr++)
    735 		if (bus->devices[addr] == 0)
    736 			return (addr);
    737 	return (-1);
    738 }
    739 
    740 
    741 usbd_status
    742 usbd_probe_and_attach(parent, dev, port, addr)
    743 	device_ptr_t parent;
    744 	usbd_device_handle dev;
    745 	int port;
    746 	int addr;
    747 {
    748 	struct usb_attach_arg uaa;
    749 	usb_device_descriptor_t *dd = &dev->ddesc;
    750 	int found, i, confi, nifaces;
    751 	usbd_status err;
    752 	device_ptr_t dv;
    753 	usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
    754 
    755 #if defined(__FreeBSD__)
    756 	/*
    757 	 * XXX uaa is a static var. Not a problem as it _should_ be used only
    758 	 * during probe and attach. Should be changed however.
    759 	 */
    760 	device_t bdev;
    761 	bdev = device_add_child(parent, NULL, -1, &uaa);
    762 	if (!bdev) {
    763 	    printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
    764 	    return (USBD_INVAL);
    765 	}
    766 	device_quiet(bdev);
    767 #endif
    768 
    769 	uaa.device = dev;
    770 	uaa.iface = 0;
    771 	uaa.ifaces = 0;
    772 	uaa.nifaces = 0;
    773 	uaa.usegeneric = 0;
    774 	uaa.port = port;
    775 	uaa.configno = UHUB_UNK_CONFIGURATION;
    776 	uaa.ifaceno = UHUB_UNK_INTERFACE;
    777 	uaa.vendor = UGETW(dd->idVendor);
    778 	uaa.product = UGETW(dd->idProduct);
    779 	uaa.release = UGETW(dd->bcdDevice);
    780 
    781 	/* First try with device specific drivers. */
    782 	DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
    783 	dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
    784 	if (dv) {
    785 		dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
    786 		if (dev->subdevs == NULL)
    787 			return (USBD_NOMEM);
    788 		dev->subdevs[0] = dv;
    789 		dev->subdevs[1] = 0;
    790 		return (USBD_NORMAL_COMPLETION);
    791 	}
    792 
    793 	DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
    794 
    795 	DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
    796 		 dd->bNumConfigurations));
    797 	/* Next try with interface drivers. */
    798 	for (confi = 0; confi < dd->bNumConfigurations; confi++) {
    799 		DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
    800 			    confi));
    801 		err = usbd_set_config_index(dev, confi, 1);
    802 		if (err) {
    803 #ifdef USB_DEBUG
    804 			DPRINTF(("%s: port %d, set config at addr %d failed, "
    805 				 "error=%s\n", USBDEVPTRNAME(parent), port,
    806 				 addr, usbd_errstr(err)));
    807 #else
    808 			printf("%s: port %d, set config at addr %d failed\n",
    809 			       USBDEVPTRNAME(parent), port, addr);
    810 #endif
    811 #if defined(__FreeBSD__)
    812 			device_delete_child(parent, bdev);
    813 #endif
    814 
    815  			return (err);
    816 		}
    817 		nifaces = dev->cdesc->bNumInterface;
    818 		uaa.configno = dev->cdesc->bConfigurationValue;
    819 		for (i = 0; i < nifaces; i++)
    820 			ifaces[i] = &dev->ifaces[i];
    821 		uaa.ifaces = ifaces;
    822 		uaa.nifaces = nifaces;
    823 		dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
    824 		if (dev->subdevs == NULL) {
    825 #if defined(__FreeBSD__)
    826 			device_delete_child(parent, bdev);
    827 #endif
    828 			return (USBD_NOMEM);
    829 		}
    830 
    831 		found = 0;
    832 		for (i = 0; i < nifaces; i++) {
    833 			if (ifaces[i] == NULL)
    834 				continue; /* interface already claimed */
    835 			uaa.iface = ifaces[i];
    836 			uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
    837 			dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
    838 					   usbd_submatch);
    839 			if (dv != NULL) {
    840 				dev->subdevs[found++] = dv;
    841 				dev->subdevs[found] = 0;
    842 				ifaces[i] = 0; /* consumed */
    843 
    844 #if defined(__FreeBSD__)
    845 				/* create another child for the next iface */
    846 				bdev = device_add_child(parent, NULL, -1,&uaa);
    847 				if (!bdev) {
    848 					printf("%s: Device creation failed\n",
    849 					USBDEVNAME(dev->bus->bdev));
    850 					return (USBD_NORMAL_COMPLETION);
    851 				}
    852 				device_quiet(bdev);
    853 #endif
    854 			}
    855 		}
    856 		if (found != 0) {
    857 #if defined(__FreeBSD__)
    858 			/* remove the last created child again; it is unused */
    859 			device_delete_child(parent, bdev);
    860 #endif
    861 			return (USBD_NORMAL_COMPLETION);
    862 		}
    863 		free(dev->subdevs, M_USB);
    864 		dev->subdevs = 0;
    865 	}
    866 	/* No interfaces were attached in any of the configurations. */
    867 
    868 	if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
    869 		usbd_set_config_index(dev, 0, 0);
    870 
    871 	DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
    872 
    873 	/* Finally try the generic driver. */
    874 	uaa.iface = 0;
    875 	uaa.usegeneric = 1;
    876 	uaa.configno = UHUB_UNK_CONFIGURATION;
    877 	uaa.ifaceno = UHUB_UNK_INTERFACE;
    878 	uaa.vendor = UHUB_UNK_VENDOR;
    879 	uaa.product = UHUB_UNK_PRODUCT;
    880 	uaa.release = UHUB_UNK_RELEASE;
    881 	dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
    882 	if (dv != NULL) {
    883 		dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
    884 		if (dev->subdevs == 0)
    885 			return (USBD_NOMEM);
    886 		dev->subdevs[0] = dv;
    887 		dev->subdevs[1] = 0;
    888 		return (USBD_NORMAL_COMPLETION);
    889 	}
    890 
    891 	/*
    892 	 * The generic attach failed, but leave the device as it is.
    893 	 * We just did not find any drivers, that's all.  The device is
    894 	 * fully operational and not harming anyone.
    895 	 */
    896 	DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
    897 #if defined(__FreeBSD__)
    898 	device_delete_child(parent, bdev);
    899 #endif
    900  	return (USBD_NORMAL_COMPLETION);
    901 }
    902 
    903 
    904 /*
    905  * Called when a new device has been put in the powered state,
    906  * but not yet in the addressed state.
    907  * Get initial descriptor, set the address, get full descriptor,
    908  * and attach a driver.
    909  */
    910 usbd_status
    911 usbd_new_device(parent, bus, depth, lowspeed, port, up)
    912 	device_ptr_t parent;
    913 	usbd_bus_handle bus;
    914 	int depth;
    915 	int lowspeed;
    916 	int port;
    917 	struct usbd_port *up;
    918 {
    919 	usbd_device_handle dev;
    920 	usb_device_descriptor_t *dd;
    921 	usbd_status err;
    922 	int addr;
    923 	int i;
    924 
    925 	DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
    926 		 bus, depth, lowspeed));
    927 	addr = usbd_getnewaddr(bus);
    928 	if (addr < 0) {
    929 		printf("%s: No free USB addresses, new device ignored.\n",
    930 		       USBDEVNAME(bus->bdev));
    931 		return (USBD_NO_ADDR);
    932 	}
    933 
    934 	dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
    935 	if (dev == NULL)
    936 		return (USBD_NOMEM);
    937 	memset(dev, 0, sizeof(*dev));
    938 
    939 	dev->bus = bus;
    940 
    941 	/* Set up default endpoint handle. */
    942 	dev->def_ep.edesc = &dev->def_ep_desc;
    943 
    944 	/* Set up default endpoint descriptor. */
    945 	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
    946 	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
    947 	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
    948 	dev->def_ep_desc.bmAttributes = UE_CONTROL;
    949 	USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
    950 	dev->def_ep_desc.bInterval = 0;
    951 
    952 	dev->quirks = &usbd_no_quirk;
    953 	dev->address = USB_START_ADDR;
    954 	dev->ddesc.bMaxPacketSize = 0;
    955 	dev->lowspeed = lowspeed != 0;
    956 	dev->depth = depth;
    957 	dev->powersrc = up;
    958 	dev->langid = USBD_NOLANG;
    959 	dev->cookie.cookie = ++usb_cookie_no;
    960 
    961 	/* Establish the default pipe. */
    962 	err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
    963 			      &dev->default_pipe);
    964 	if (err) {
    965 		usbd_remove_device(dev, up);
    966 		return (err);
    967 	}
    968 
    969 	up->device = dev;
    970 	dd = &dev->ddesc;
    971 	/* Try a few times in case the device is slow (i.e. outside specs.) */
    972 	for (i = 0; i < 3; i++) {
    973 		/* Get the first 8 bytes of the device descriptor. */
    974 		err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
    975 		if (!err)
    976 			break;
    977 		usbd_delay_ms(dev, 200);
    978 	}
    979 	if (err) {
    980 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
    981 			      "failed\n", addr));
    982 		usbd_remove_device(dev, up);
    983 		return (err);
    984 	}
    985 
    986 	DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
    987 		 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, ls=%d\n",
    988 		 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
    989 		 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
    990 		 dev->lowspeed));
    991 
    992 	if (dd->bDescriptorType != UDESC_DEVICE) {
    993 		/* Illegal device descriptor */
    994 		DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
    995 			     dd->bDescriptorType));
    996 		usbd_remove_device(dev, up);
    997 		return (USBD_INVAL);
    998 	}
    999 
   1000 	if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
   1001 		DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
   1002 		usbd_remove_device(dev, up);
   1003 		return (USBD_INVAL);
   1004 	}
   1005 
   1006 	USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
   1007 
   1008 	err = usbd_reload_device_desc(dev);
   1009 	if (err) {
   1010 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
   1011 			      "failed\n", addr));
   1012 		usbd_remove_device(dev, up);
   1013 		return (err);
   1014 	}
   1015 
   1016 	/* Set the address */
   1017 	err = usbd_set_address(dev, addr);
   1018 	DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
   1019 	if (err) {
   1020 		DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
   1021 		err = USBD_SET_ADDR_FAILED;
   1022 		usbd_remove_device(dev, up);
   1023 		return (err);
   1024 	}
   1025 	/* Allow device time to set new address */
   1026 	usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
   1027 
   1028 	dev->address = addr;	/* New device address now */
   1029 	bus->devices[addr] = dev;
   1030 
   1031 	/* Assume 100mA bus powered for now. Changed when configured. */
   1032 	dev->power = USB_MIN_POWER;
   1033 	dev->self_powered = 0;
   1034 
   1035 	DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
   1036 		 addr, dev, parent));
   1037 
   1038 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
   1039 
   1040 	err = usbd_probe_and_attach(parent, dev, port, addr);
   1041 	if (err) {
   1042 		usbd_remove_device(dev, up);
   1043 		return (err);
   1044   	}
   1045 
   1046   	return (USBD_NORMAL_COMPLETION);
   1047 }
   1048 
   1049 usbd_status
   1050 usbd_reload_device_desc(dev)
   1051 	usbd_device_handle dev;
   1052 {
   1053 	usbd_status err;
   1054 
   1055 	/* Get the full device descriptor. */
   1056 	err = usbd_get_device_desc(dev, &dev->ddesc);
   1057 	if (err)
   1058 		return (err);
   1059 
   1060 	/* Figure out what's wrong with this device. */
   1061 	dev->quirks = usbd_find_quirk(&dev->ddesc);
   1062 
   1063 	return (USBD_NORMAL_COMPLETION);
   1064 }
   1065 
   1066 void
   1067 usbd_remove_device(dev, up)
   1068 	usbd_device_handle dev;
   1069 	struct usbd_port *up;
   1070 {
   1071 	DPRINTF(("usbd_remove_device: %p\n", dev));
   1072 
   1073 	if (dev->default_pipe != NULL)
   1074 		usbd_kill_pipe(dev->default_pipe);
   1075 	up->device = 0;
   1076 	dev->bus->devices[dev->address] = 0;
   1077 
   1078 	free(dev, M_USB);
   1079 }
   1080 
   1081 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1082 int
   1083 usbd_print(aux, pnp)
   1084 	void *aux;
   1085 	const char *pnp;
   1086 {
   1087 	struct usb_attach_arg *uaa = aux;
   1088 	char devinfo[1024];
   1089 
   1090 	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
   1091 	if (pnp) {
   1092 		if (!uaa->usegeneric)
   1093 			return (QUIET);
   1094 		usbd_devinfo(uaa->device, 1, devinfo);
   1095 		printf("%s, %s", devinfo, pnp);
   1096 	}
   1097 	if (uaa->port != 0)
   1098 		printf(" port %d", uaa->port);
   1099 	if (uaa->configno != UHUB_UNK_CONFIGURATION)
   1100 		printf(" configuration %d", uaa->configno);
   1101 	if (uaa->ifaceno != UHUB_UNK_INTERFACE)
   1102 		printf(" interface %d", uaa->ifaceno);
   1103 #if 0
   1104 	/*
   1105 	 * It gets very crowded with these locators on the attach line.
   1106 	 * They are not really needed since they are printed in the clear
   1107 	 * by each driver.
   1108 	 */
   1109 	if (uaa->vendor != UHUB_UNK_VENDOR)
   1110 		printf(" vendor 0x%04x", uaa->vendor);
   1111 	if (uaa->product != UHUB_UNK_PRODUCT)
   1112 		printf(" product 0x%04x", uaa->product);
   1113 	if (uaa->release != UHUB_UNK_RELEASE)
   1114 		printf(" release 0x%04x", uaa->release);
   1115 #endif
   1116 	return (UNCONF);
   1117 }
   1118 
   1119 #if defined(__NetBSD__)
   1120 int
   1121 usbd_submatch(parent, cf, aux)
   1122 	struct device *parent;
   1123 	struct cfdata *cf;
   1124 	void *aux;
   1125 {
   1126 #elif defined(__OpenBSD__)
   1127 int
   1128 usbd_submatch(parent, match, aux)
   1129 	struct device *parent;
   1130 	void *match;
   1131 	void *aux;
   1132 {
   1133 	struct cfdata *cf = match;
   1134 #endif
   1135 	struct usb_attach_arg *uaa = aux;
   1136 
   1137 	if ((uaa->port != 0 &&
   1138 	     cf->uhubcf_port != UHUB_UNK_PORT &&
   1139 	     cf->uhubcf_port != uaa->port) ||
   1140 	    (uaa->configno != UHUB_UNK_CONFIGURATION &&
   1141 	     cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
   1142 	     cf->uhubcf_configuration != uaa->configno) ||
   1143 	    (uaa->ifaceno != UHUB_UNK_INTERFACE &&
   1144 	     cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
   1145 	     cf->uhubcf_interface != uaa->ifaceno) ||
   1146 	    (uaa->vendor != UHUB_UNK_VENDOR &&
   1147 	     cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
   1148 	     cf->uhubcf_vendor != uaa->vendor) ||
   1149 	    (uaa->product != UHUB_UNK_PRODUCT &&
   1150 	     cf->uhubcf_product != UHUB_UNK_PRODUCT &&
   1151 	     cf->uhubcf_product != uaa->product) ||
   1152 	    (uaa->release != UHUB_UNK_RELEASE &&
   1153 	     cf->uhubcf_release != UHUB_UNK_RELEASE &&
   1154 	     cf->uhubcf_release != uaa->release)
   1155 	   )
   1156 		return 0;
   1157 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
   1158 }
   1159 
   1160 #endif
   1161 
   1162 void
   1163 usbd_fill_deviceinfo(dev, di)
   1164 	usbd_device_handle dev;
   1165 	struct usb_device_info *di;
   1166 {
   1167 	struct usbd_port *p;
   1168 	int i, err, s;
   1169 
   1170 	di->bus = USBDEVUNIT(dev->bus->bdev);
   1171 	di->addr = dev->address;
   1172 	di->cookie = dev->cookie;
   1173 	usbd_devinfo_vp(dev, di->vendor, di->product);
   1174 	usbd_printBCD(di->release, UGETW(dev->ddesc.bcdDevice));
   1175 	di->vendorNo = UGETW(dev->ddesc.idVendor);
   1176 	di->productNo = UGETW(dev->ddesc.idProduct);
   1177 	di->releaseNo = UGETW(dev->ddesc.bcdDevice);
   1178 	di->class = dev->ddesc.bDeviceClass;
   1179 	di->subclass = dev->ddesc.bDeviceSubClass;
   1180 	di->protocol = dev->ddesc.bDeviceProtocol;
   1181 	di->config = dev->config;
   1182 	di->power = dev->self_powered ? 0 : dev->power;
   1183 	di->lowspeed = dev->lowspeed;
   1184 
   1185 	if (dev->subdevs != NULL) {
   1186 		for (i = 0; dev->subdevs[i] &&
   1187 			     i < USB_MAX_DEVNAMES; i++) {
   1188 			strncpy(di->devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
   1189 				USB_MAX_DEVNAMELEN);
   1190 			di->devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
   1191                 }
   1192         } else {
   1193                 i = 0;
   1194         }
   1195         for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
   1196                 di->devnames[i][0] = 0;                 /* empty */
   1197 
   1198 	if (dev->hub) {
   1199 		for (i = 0;
   1200 		     i < sizeof(di->ports) / sizeof(di->ports[0]) &&
   1201 			     i < dev->hub->hubdesc.bNbrPorts;
   1202 		     i++) {
   1203 			p = &dev->hub->ports[i];
   1204 			if (p->device)
   1205 				err = p->device->address;
   1206 			else {
   1207 				s = UGETW(p->status.wPortStatus);
   1208 				if (s & UPS_PORT_ENABLED)
   1209 					err = USB_PORT_ENABLED;
   1210 				else if (s & UPS_SUSPEND)
   1211 					err = USB_PORT_SUSPENDED;
   1212 				else if (s & UPS_PORT_POWER)
   1213 					err = USB_PORT_POWERED;
   1214 				else
   1215 					err = USB_PORT_DISABLED;
   1216 			}
   1217 			di->ports[i] = err;
   1218 		}
   1219 		di->nports = dev->hub->hubdesc.bNbrPorts;
   1220 	} else
   1221 		di->nports = 0;
   1222 }
   1223 
   1224 void
   1225 usb_free_device(dev)
   1226 	usbd_device_handle dev;
   1227 {
   1228 	int ifcidx, nifc;
   1229 
   1230 	if (dev->default_pipe != NULL)
   1231 		usbd_kill_pipe(dev->default_pipe);
   1232 	if (dev->ifaces != NULL) {
   1233 		nifc = dev->cdesc->bNumInterface;
   1234 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
   1235 			usbd_free_iface_data(dev, ifcidx);
   1236 		free(dev->ifaces, M_USB);
   1237 	}
   1238 	if (dev->cdesc != NULL)
   1239 		free(dev->cdesc, M_USB);
   1240 	if (dev->subdevs != NULL)
   1241 		free(dev->subdevs, M_USB);
   1242 	free(dev, M_USB);
   1243 }
   1244 
   1245 /*
   1246  * The general mechanism for detaching drivers works as follows: Each
   1247  * driver is responsible for maintaining a reference count on the
   1248  * number of outstanding references to its softc (e.g.  from
   1249  * processing hanging in a read or write).  The detach method of the
   1250  * driver decrements this counter and flags in the softc that the
   1251  * driver is dying and then wakes any sleepers.  It then sleeps on the
   1252  * softc.  Each place that can sleep must maintain the reference
   1253  * count.  When the reference count drops to -1 (0 is the normal value
   1254  * of the reference count) the a wakeup on the softc is performed
   1255  * signaling to the detach waiter that all references are gone.
   1256  */
   1257 
   1258 /*
   1259  * Called from process context when we discover that a port has
   1260  * been disconnected.
   1261  */
   1262 void
   1263 usb_disconnect_port(up, parent)
   1264 	struct usbd_port *up;
   1265 	device_ptr_t parent;
   1266 {
   1267 	usbd_device_handle dev = up->device;
   1268 	char *hubname = USBDEVPTRNAME(parent);
   1269 	int i;
   1270 
   1271 	DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
   1272 		    up, dev, up->portno));
   1273 
   1274 #ifdef DIAGNOSTIC
   1275 	if (dev == NULL) {
   1276 		printf("usb_disconnect_port: no device\n");
   1277 		return;
   1278 	}
   1279 #endif
   1280 
   1281 	if (dev->subdevs != NULL) {
   1282 		DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
   1283 		for (i = 0; dev->subdevs[i]; i++) {
   1284 			printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
   1285 			       hubname);
   1286 			if (up->portno != 0)
   1287 				printf(" port %d", up->portno);
   1288 			printf(" (addr %d) disconnected\n", dev->address);
   1289 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1290 			config_detach(dev->subdevs[i], DETACH_FORCE);
   1291 #elif defined(__FreeBSD__)
   1292                         device_delete_child(device_get_parent(dev->subdevs[i]),
   1293 					    dev->subdevs[i]);
   1294 #endif
   1295 
   1296 		}
   1297 	}
   1298 
   1299 	usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
   1300 	dev->bus->devices[dev->address] = NULL;
   1301 	up->device = NULL;
   1302 	usb_free_device(dev);
   1303 }
   1304 
   1305 #ifdef __OpenBSD__
   1306 void *usb_realloc(p, size, pool, flags)
   1307 	void *p;
   1308 	u_int size;
   1309 	int pool;
   1310 	int flags;
   1311 {
   1312 	void *q;
   1313 
   1314 	q = malloc(size, pool, flags);
   1315 	if (q == NULL)
   1316 		return (NULL);
   1317 	bcopy(p, q, size);
   1318 	free(p, pool);
   1319 	return (q);
   1320 }
   1321 #endif
   1322