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