Home | History | Annotate | Line # | Download | only in usb
usb_subr.c revision 1.47
      1 /*	$NetBSD: usb_subr.c,v 1.47 1999/09/15 10:25:31 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 (cdp->bmAttributes & UC_SELF_POWERED) {
    582 		/* May be self powered. */
    583 		if (cdp->bmAttributes & UC_BUS_POWERED) {
    584 			/* Must ask device. */
    585 			r = usbd_get_device_status(dev, &ds);
    586 			if (r == USBD_NORMAL_COMPLETION &&
    587 			    (UGETW(ds.wStatus) & UDS_SELF_POWERED))
    588 				selfpowered = 1;
    589 			DPRINTF(("usbd_set_config_index: status=0x%04x, "
    590 				 "error=%s\n",
    591 				 UGETW(ds.wStatus), usbd_errstr(r)));
    592 		} else
    593 			selfpowered = 1;
    594 	}
    595 	DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, "
    596 		 "selfpowered=%d, power=%d\n",
    597 		 dev->address, cdp->bmAttributes,
    598 		 selfpowered, cdp->bMaxPower * 2));
    599 #ifdef USB_DEBUG
    600 	if (!dev->powersrc) {
    601 		DPRINTF(("usbd_set_config_index: No power source?\n"));
    602 		return (USBD_IOERROR);
    603 	}
    604 #endif
    605 	power = cdp->bMaxPower * 2;
    606 	if (power > dev->powersrc->power) {
    607 		/* XXX print nicer message. */
    608 		if (msg)
    609 			printf("%s: device addr %d (config %d) exceeds power "
    610 				 "budget, %d mA > %d mA\n",
    611 			       USBDEVNAME(dev->bus->bdev), dev->address,
    612 			       cdp->bConfigurationValue,
    613 			       power, dev->powersrc->power);
    614 		r = USBD_NO_POWER;
    615 		goto bad;
    616 	}
    617 	dev->power = power;
    618 	dev->self_powered = selfpowered;
    619 
    620 	DPRINTF(("usbd_set_config_index: set config %d\n",
    621 		 cdp->bConfigurationValue));
    622 	r = usbd_set_config(dev, cdp->bConfigurationValue);
    623 	if (r != USBD_NORMAL_COMPLETION) {
    624 		DPRINTF(("usbd_set_config_index: setting config=%d failed, "
    625 			 "error=%s\n",
    626 			 cdp->bConfigurationValue, usbd_errstr(r)));
    627 		goto bad;
    628 	}
    629 	DPRINTF(("usbd_set_config_index: setting new config %d\n",
    630 		 cdp->bConfigurationValue));
    631 	nifc = cdp->bNumInterface;
    632 	dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
    633 			     M_USB, M_NOWAIT);
    634 	if (dev->ifaces == 0) {
    635 		r = USBD_NOMEM;
    636 		goto bad;
    637 	}
    638 	DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
    639 	dev->cdesc = cdp;
    640 	dev->config = cdp->bConfigurationValue;
    641 	for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
    642 		r = usbd_fill_iface_data(dev, ifcidx, 0);
    643 		if (r != USBD_NORMAL_COMPLETION) {
    644 			while (--ifcidx >= 0)
    645 				usbd_free_iface_data(dev, ifcidx);
    646 			goto bad;
    647 		}
    648 	}
    649 
    650 	return (USBD_NORMAL_COMPLETION);
    651 
    652  bad:
    653 	free(cdp, M_USB);
    654 	return (r);
    655 }
    656 
    657 /* XXX add function for alternate settings */
    658 
    659 usbd_status
    660 usbd_setup_pipe(dev, iface, ep, pipe)
    661 	usbd_device_handle dev;
    662 	usbd_interface_handle iface;
    663 	struct usbd_endpoint *ep;
    664 	usbd_pipe_handle *pipe;
    665 {
    666 	usbd_pipe_handle p;
    667 	usbd_status r;
    668 
    669 	DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
    670 		    dev, iface, ep, pipe));
    671 	p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
    672 	if (p == 0)
    673 		return (USBD_NOMEM);
    674 	p->device = dev;
    675 	p->iface = iface;
    676 	p->endpoint = ep;
    677 	ep->refcnt++;
    678 	p->refcnt = 1;
    679 	p->intrreqh = 0;
    680 	p->running = 0;
    681 	p->repeat = 0;
    682 	SIMPLEQ_INIT(&p->queue);
    683 	r = dev->bus->methods->open_pipe(p);
    684 	if (r != USBD_NORMAL_COMPLETION) {
    685 		DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
    686 			 "%s\n",
    687 			 ep->edesc->bEndpointAddress, usbd_errstr(r)));
    688 		free(p, M_USB);
    689 		return (r);
    690 	}
    691 	/* Clear any stall and make sure DATA0 toggle will be used next. */
    692 	if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
    693 		usbd_clear_endpoint_stall(p);
    694 	*pipe = p;
    695 	return (USBD_NORMAL_COMPLETION);
    696 }
    697 
    698 /* Abort the device control pipe. */
    699 void
    700 usbd_kill_pipe(pipe)
    701 	usbd_pipe_handle pipe;
    702 {
    703 	pipe->methods->close(pipe);
    704 	pipe->endpoint->refcnt--;
    705 	free(pipe, M_USB);
    706 }
    707 
    708 int
    709 usbd_getnewaddr(bus)
    710 	usbd_bus_handle bus;
    711 {
    712 	int addr;
    713 
    714 	for (addr = 1; addr < USB_MAX_DEVICES; addr++)
    715 		if (bus->devices[addr] == 0)
    716 			return (addr);
    717 	return (-1);
    718 }
    719 
    720 
    721 usbd_status
    722 usbd_probe_and_attach(parent, dev, port, addr)
    723 	device_ptr_t parent;
    724 	usbd_device_handle dev;
    725 	int port;
    726 	int addr;
    727 {
    728 	struct usb_attach_arg uaa;
    729 	usb_device_descriptor_t *dd = &dev->ddesc;
    730 	int r, found, i, confi, nifaces;
    731 	device_ptr_t dv;
    732 	usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
    733 
    734 #if defined(__FreeBSD__)
    735 	/*
    736 	 * XXX uaa is a static var. Not a problem as it _should_ be used only
    737 	 * during probe and attach. Should be changed however.
    738 	 */
    739 	device_t bdev;
    740 	bdev = device_add_child(*parent, NULL, -1, &uaa);
    741 	if (!bdev) {
    742 	    printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
    743 	    return (USBD_INVAL);
    744 	}
    745 #endif
    746 
    747 	uaa.device = dev;
    748 	uaa.iface = 0;
    749 	uaa.ifaces = 0;
    750 	uaa.nifaces = 0;
    751 	uaa.usegeneric = 0;
    752 	uaa.port = port;
    753 	uaa.configno = UHUB_UNK_CONFIGURATION;
    754 	uaa.ifaceno = UHUB_UNK_INTERFACE;
    755 	uaa.vendor = UGETW(dd->idVendor);
    756 	uaa.product = UGETW(dd->idProduct);
    757 	uaa.release = UGETW(dd->bcdDevice);
    758 
    759 	/* First try with device specific drivers. */
    760 	dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
    761 	if (dv) {
    762 		dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
    763 		if (dev->subdevs == 0)
    764 			return (USBD_NOMEM);
    765 		dev->subdevs[0] = dv;
    766 		dev->subdevs[1] = 0;
    767 		return (USBD_NORMAL_COMPLETION);
    768 	}
    769 
    770 	DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
    771 
    772 	/* Next try with interface drivers. */
    773 	for (confi = 0; confi < dd->bNumConfigurations; confi++) {
    774 		DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
    775 			    confi));
    776 		r = usbd_set_config_index(dev, confi, 1);
    777 		if (r != USBD_NORMAL_COMPLETION) {
    778 #ifdef USB_DEBUG
    779 			DPRINTF(("%s: port %d, set config at addr %d failed, "
    780 				 "error=%s\n", USBDEVPTRNAME(parent), port,
    781 				 addr, usbd_errstr(r)));
    782 #else
    783 			printf("%s: port %d, set config at addr %d failed\n",
    784 			       USBDEVPTRNAME(parent), port, addr);
    785 #endif
    786 #if defined(__FreeBSD__)
    787 			device_delete_child(*parent, bdev);
    788 #endif
    789  			return (r);
    790 		}
    791 		nifaces = dev->cdesc->bNumInterface;
    792 		uaa.configno = dev->cdesc->bConfigurationValue;
    793 		for (i = 0; i < nifaces; i++)
    794 			ifaces[i] = &dev->ifaces[i];
    795 		uaa.ifaces = ifaces;
    796 		uaa.nifaces = nifaces;
    797 		dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
    798 		if (dev->subdevs == 0)
    799 			return (USBD_NOMEM);
    800 		for (found = i = 0; i < nifaces; i++) {
    801 			if (!ifaces[i])
    802 				continue; /* interface already claimed */
    803 			uaa.iface = ifaces[i];
    804 			uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
    805 			dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
    806 					   usbd_submatch);
    807 			if (dv) {
    808 				dev->subdevs[found++] = dv;
    809 				dev->subdevs[found] = 0;
    810 				ifaces[i] = 0; /* consumed */
    811 			}
    812 		}
    813 		if (found != 0)
    814 			return (USBD_NORMAL_COMPLETION);
    815 		free(dev->subdevs, M_USB);
    816 		dev->subdevs = 0;
    817 	}
    818 	/* No interfaces were attached in any of the configurations. */
    819 
    820 	if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
    821 		usbd_set_config_index(dev, 0, 0);
    822 
    823 	DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
    824 
    825 	/* Finally try the generic driver. */
    826 	uaa.iface = 0;
    827 	uaa.usegeneric = 1;
    828 	uaa.configno = UHUB_UNK_CONFIGURATION;
    829 	uaa.ifaceno = UHUB_UNK_INTERFACE;
    830 	uaa.vendor = UHUB_UNK_VENDOR;
    831 	uaa.product = UHUB_UNK_PRODUCT;
    832 	uaa.release = UHUB_UNK_RELEASE;
    833 	dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
    834 	if (dv) {
    835 		dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
    836 		if (dev->subdevs == 0)
    837 			return (USBD_NOMEM);
    838 		dev->subdevs[0] = dv;
    839 		dev->subdevs[1] = 0;
    840 		return (USBD_NORMAL_COMPLETION);
    841 	}
    842 
    843 	/*
    844 	 * The generic attach failed, but leave the device as it is.
    845 	 * We just did not find any drivers, that's all.  The device is
    846 	 * fully operational and not harming anyone.
    847 	 */
    848 	DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
    849 #if defined(__FreeBSD__)
    850 /*
    851  * XXX should we delete the child again? Left for now to avoid dangling
    852  * references.
    853       device_delete_child(*parent, bdev);
    854 */
    855 #endif
    856  	return (USBD_NORMAL_COMPLETION);
    857 }
    858 
    859 
    860 
    861 /*
    862  * Called when a new device has been put in the powered state,
    863  * but not yet in the addressed state.
    864  * Get initial descriptor, set the address, get full descriptor,
    865  * and attach a driver.
    866  */
    867 usbd_status
    868 usbd_new_device(parent, bus, depth, lowspeed, port, up)
    869 	device_ptr_t parent;
    870 	usbd_bus_handle bus;
    871 	int depth;
    872 	int lowspeed;
    873 	int port;
    874 	struct usbd_port *up;
    875 {
    876 	usbd_device_handle dev;
    877 	usb_device_descriptor_t *dd;
    878 	usbd_status r;
    879 	int addr;
    880 	int i;
    881 
    882 	DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
    883 		 bus, depth, lowspeed));
    884 	addr = usbd_getnewaddr(bus);
    885 	if (addr < 0) {
    886 		printf("%s: No free USB addresses, new device ignored.\n",
    887 		       USBDEVNAME(bus->bdev));
    888 		return (USBD_NO_ADDR);
    889 	}
    890 
    891 	dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
    892 	if (dev == 0)
    893 		return (USBD_NOMEM);
    894 	memset(dev, 0, sizeof(*dev));
    895 
    896 	dev->bus = bus;
    897 
    898 	/* Set up default endpoint handle. */
    899 	dev->def_ep.edesc = &dev->def_ep_desc;
    900 
    901 	/* Set up default endpoint descriptor. */
    902 	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
    903 	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
    904 	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
    905 	dev->def_ep_desc.bmAttributes = UE_CONTROL;
    906 	USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
    907 	dev->def_ep_desc.bInterval = 0;
    908 
    909 	dev->quirks = &usbd_no_quirk;
    910 	dev->address = USB_START_ADDR;
    911 	dev->ddesc.bMaxPacketSize = 0;
    912 	dev->lowspeed = lowspeed != 0;
    913 	dev->depth = depth;
    914 	dev->powersrc = up;
    915 	dev->langid = USBD_NOLANG;
    916 
    917 	/* Establish the the default pipe. */
    918 	r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
    919 	if (r != USBD_NORMAL_COMPLETION) {
    920 		usbd_remove_device(dev, up);
    921 		return (r);
    922 	}
    923 
    924 	up->device = dev;
    925 	dd = &dev->ddesc;
    926 	/* Try a few times in case the device is slow (i.e. outside specs.) */
    927 	for (i = 0; i < 5; i++) {
    928 		/* Get the first 8 bytes of the device descriptor. */
    929 		r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
    930 		if (r == USBD_NORMAL_COMPLETION)
    931 			break;
    932 		usbd_delay_ms(dev, 200);
    933 	}
    934 	if (r != USBD_NORMAL_COMPLETION) {
    935 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
    936 			      "failed\n",
    937 			      addr));
    938 		usbd_remove_device(dev, up);
    939 		return (r);
    940 	}
    941 
    942 	DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
    943 		 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, ls=%d\n",
    944 		 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
    945 		 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
    946 		 dev->lowspeed));
    947 
    948 	if (dd->bDescriptorType != UDESC_DEVICE) {
    949 		/* Illegal device descriptor */
    950 		DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
    951 			     dd->bDescriptorType));
    952 		usbd_remove_device(dev, up);
    953 		return (USBD_INVAL);
    954 	}
    955 
    956 	if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
    957 		DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
    958 		usbd_remove_device(dev, up);
    959 		return (USBD_INVAL);
    960 	}
    961 
    962 	USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
    963 
    964 	/* Get the full device descriptor. */
    965 	r = usbd_get_device_desc(dev, dd);
    966 	if (r != USBD_NORMAL_COMPLETION) {
    967 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
    968 			      "failed\n", addr));
    969 		usbd_remove_device(dev, up);
    970 		return (r);
    971 	}
    972 
    973 	/* Figure out what's wrong with this device. */
    974 	dev->quirks = usbd_find_quirk(dd);
    975 
    976 	/* Set the address */
    977 	r = usbd_set_address(dev, addr);
    978 	if (r != USBD_NORMAL_COMPLETION) {
    979 		DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
    980 		r = USBD_SET_ADDR_FAILED;
    981 		usbd_remove_device(dev, up);
    982 		return (r);
    983 	}
    984 	/* Allow device time to set new address */
    985 	usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
    986 
    987 	dev->address = addr;	/* New device address now */
    988 	bus->devices[addr] = dev;
    989 
    990 	/* Assume 100mA bus powered for now. Changed when configured. */
    991 	dev->power = USB_MIN_POWER;
    992 	dev->self_powered = 0;
    993 
    994 	DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
    995 		 addr, dev, parent));
    996 
    997 	r = usbd_probe_and_attach(parent, dev, port, addr);
    998 	if (r != USBD_NORMAL_COMPLETION) {
    999 		usbd_remove_device(dev, up);
   1000 		return (r);
   1001   	}
   1002 
   1003   	return (USBD_NORMAL_COMPLETION);
   1004 }
   1005 
   1006 void
   1007 usbd_remove_device(dev, up)
   1008 	usbd_device_handle dev;
   1009 	struct usbd_port *up;
   1010 {
   1011 	DPRINTF(("usbd_remove_device: %p\n", dev));
   1012 
   1013 	if (dev->default_pipe)
   1014 		usbd_kill_pipe(dev->default_pipe);
   1015 	up->device = 0;
   1016 	dev->bus->devices[dev->address] = 0;
   1017 
   1018 	free(dev, M_USB);
   1019 }
   1020 
   1021 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1022 int
   1023 usbd_print(aux, pnp)
   1024 	void *aux;
   1025 	const char *pnp;
   1026 {
   1027 	struct usb_attach_arg *uaa = aux;
   1028 	char devinfo[1024];
   1029 
   1030 	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
   1031 	if (pnp) {
   1032 		if (!uaa->usegeneric)
   1033 			return (QUIET);
   1034 		usbd_devinfo(uaa->device, 1, devinfo);
   1035 		printf("%s, %s", devinfo, pnp);
   1036 	}
   1037 	if (uaa->port != 0)
   1038 		printf(" port %d", uaa->port);
   1039 	if (uaa->configno != UHUB_UNK_CONFIGURATION)
   1040 		printf(" configuration %d", uaa->configno);
   1041 	if (uaa->ifaceno != UHUB_UNK_INTERFACE)
   1042 		printf(" interface %d", uaa->ifaceno);
   1043 #if 0
   1044 	/*
   1045 	 * It gets very crowded with these locators on the attach line.
   1046 	 * They are not really needed since they are printed in the clear
   1047 	 * by each driver.
   1048 	 */
   1049 	if (uaa->vendor != UHUB_UNK_VENDOR)
   1050 		printf(" vendor 0x%04x", uaa->vendor);
   1051 	if (uaa->product != UHUB_UNK_PRODUCT)
   1052 		printf(" product 0x%04x", uaa->product);
   1053 	if (uaa->release != UHUB_UNK_RELEASE)
   1054 		printf(" release 0x%04x", uaa->release);
   1055 #endif
   1056 	return (UNCONF);
   1057 }
   1058 
   1059 #if defined(__NetBSD__)
   1060 int
   1061 usbd_submatch(parent, cf, aux)
   1062 	struct device *parent;
   1063 	struct cfdata *cf;
   1064 	void *aux;
   1065 {
   1066 #elif defined(__OpenBSD__)
   1067 int
   1068 usbd_submatch(parent, match, aux)
   1069 	struct device *parent;
   1070 	void *match;
   1071 	void *aux;
   1072 {
   1073 	struct cfdata *cf = match;
   1074 #endif
   1075 	struct usb_attach_arg *uaa = aux;
   1076 
   1077 	if ((uaa->port != 0 &&
   1078 	     cf->uhubcf_port != UHUB_UNK_PORT &&
   1079 	     cf->uhubcf_port != uaa->port) ||
   1080 	    (uaa->configno != UHUB_UNK_CONFIGURATION &&
   1081 	     cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
   1082 	     cf->uhubcf_configuration != uaa->configno) ||
   1083 	    (uaa->ifaceno != UHUB_UNK_INTERFACE &&
   1084 	     cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
   1085 	     cf->uhubcf_interface != uaa->ifaceno) ||
   1086 	    (uaa->vendor != UHUB_UNK_VENDOR &&
   1087 	     cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
   1088 	     cf->uhubcf_vendor != uaa->vendor) ||
   1089 	    (uaa->product != UHUB_UNK_PRODUCT &&
   1090 	     cf->uhubcf_product != UHUB_UNK_PRODUCT &&
   1091 	     cf->uhubcf_product != uaa->product) ||
   1092 	    (uaa->release != UHUB_UNK_RELEASE &&
   1093 	     cf->uhubcf_release != UHUB_UNK_RELEASE &&
   1094 	     cf->uhubcf_release != uaa->release)
   1095 	   )
   1096 		return 0;
   1097 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
   1098 }
   1099 
   1100 #elif defined(__FreeBSD__)
   1101 static void
   1102 usbd_bus_print_child(device_t bus, device_t dev)
   1103 {
   1104 	/* FIXME print the device address and the configuration used
   1105 	 */
   1106 }
   1107 #endif
   1108 
   1109 void
   1110 usbd_fill_deviceinfo(dev, di)
   1111 	usbd_device_handle dev;
   1112 	struct usb_device_info *di;
   1113 {
   1114 	struct usbd_port *p;
   1115 	int i, r, s;
   1116 
   1117 	di->config = dev->config;
   1118 	usbd_devinfo_vp(dev, di->vendor, di->product);
   1119 	usbd_printBCD(di->release, UGETW(dev->ddesc.bcdDevice));
   1120 	di->vendorNo = UGETW(dev->ddesc.idVendor);
   1121 	di->productNo = UGETW(dev->ddesc.idProduct);
   1122 	di->class = dev->ddesc.bDeviceClass;
   1123 	di->power = dev->self_powered ? 0 : dev->power;
   1124 	di->lowspeed = dev->lowspeed;
   1125 	di->addr = dev->address;
   1126 	if (dev->hub) {
   1127 		for (i = 0;
   1128 		     i < sizeof(di->ports) / sizeof(di->ports[0]) &&
   1129 			     i < dev->hub->hubdesc.bNbrPorts;
   1130 		     i++) {
   1131 			p = &dev->hub->ports[i];
   1132 			if (p->device)
   1133 				r = p->device->address;
   1134 			else {
   1135 				s = UGETW(p->status.wPortStatus);
   1136 				if (s & UPS_PORT_ENABLED)
   1137 					r = USB_PORT_ENABLED;
   1138 				else if (s & UPS_SUSPEND)
   1139 					r = USB_PORT_SUSPENDED;
   1140 				else if (s & UPS_PORT_POWER)
   1141 					r = USB_PORT_POWERED;
   1142 				else
   1143 					r = USB_PORT_DISABLED;
   1144 			}
   1145 			di->ports[i] = r;
   1146 		}
   1147 		di->nports = dev->hub->hubdesc.bNbrPorts;
   1148 	} else
   1149 		di->nports = 0;
   1150 }
   1151 
   1152 void
   1153 usb_free_device(dev)
   1154 	usbd_device_handle dev;
   1155 {
   1156 	int ifcidx, nifc;
   1157 
   1158 	if (dev->default_pipe)
   1159 		usbd_kill_pipe(dev->default_pipe);
   1160 	if (dev->ifaces) {
   1161 		nifc = dev->cdesc->bNumInterface;
   1162 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
   1163 			usbd_free_iface_data(dev, ifcidx);
   1164 		free(dev->ifaces, M_USB);
   1165 	}
   1166 	if (dev->cdesc)
   1167 		free(dev->cdesc, M_USB);
   1168 	if (dev->subdevs)
   1169 		free(dev->subdevs, M_USB);
   1170 	free(dev, M_USB);
   1171 }
   1172 
   1173 /*
   1174  * The general mechanism for detaching drivers works as follows: Each
   1175  * driver is responsible for maintaining a reference count on the
   1176  * number of outstanding references to its softc (e.g.  from
   1177  * processing hanging in a read or write).  The detach method of the
   1178  * driver decrements this counter and flags in the softc that the
   1179  * driver is dying and then wakes any sleepers.  It then sleeps on the
   1180  * softc.  Each place that can sleep must maintain the reference
   1181  * count.  When the reference count drops to -1 (0 is the normal value
   1182  * of the reference count) the a wakeup on the softc is performed
   1183  * signaling to the detach waiter that all references are gone.
   1184  */
   1185 
   1186 /*
   1187  * Called from process context when we discover that a port has
   1188  * been disconnected.
   1189  */
   1190 void
   1191 usb_disconnect_port(up)
   1192 	struct usbd_port *up;
   1193 {
   1194 	usbd_device_handle dev = up->device;
   1195 	char *hubname;
   1196 	int i;
   1197 
   1198 	DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
   1199 		    up, dev, up->portno));
   1200 
   1201 	if (!dev->cdesc) {
   1202 		/* Partially attached device, just drop it. */
   1203 		dev->bus->devices[dev->address] = 0;
   1204 		up->device = 0;
   1205 		return;
   1206 	}
   1207 
   1208 	if (dev->subdevs) {
   1209 		hubname = USBDEVPTRNAME(up->parent->subdevs[0]);
   1210 		for (i = 0; dev->subdevs[i]; i++) {
   1211 			printf("%s: at %s port %d (addr %d) disconnected\n",
   1212 			       USBDEVPTRNAME(dev->subdevs[i]), hubname,
   1213 			       up->portno, dev->address);
   1214 			config_detach(dev->subdevs[i], DETACH_FORCE);
   1215 		}
   1216 	}
   1217 
   1218 	dev->bus->devices[dev->address] = 0;
   1219 	up->device = 0;
   1220 	usb_free_device(dev);
   1221 
   1222 #if defined(__FreeBSD__)
   1223       device_delete_child(
   1224 	  device_get_parent(((struct softc *)dev->softc)->sc_dev),
   1225 	  ((struct softc *)dev->softc)->sc_dev);
   1226 #endif
   1227 }
   1228 
   1229