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