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