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