Home | History | Annotate | Line # | Download | only in usb
usb_subr.c revision 1.6
      1 /*	$NetBSD: usb_subr.c,v 1.6 1998/08/01 20:11:39 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson <augustss (at) carlstedt.se>
      8  *         Carlstedt Research & Technology
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/malloc.h>
     43 #include <sys/device.h>
     44 #include <sys/proc.h>
     45 #include <sys/select.h>
     46 
     47 #include <dev/usb/usb.h>
     48 
     49 #include <dev/usb/usbdi.h>
     50 #include <dev/usb/usbdi_util.h>
     51 #include <dev/usb/usbdivar.h>
     52 #include <dev/usb/usbdevs.h>
     53 #include <dev/usb/usb_quirks.h>
     54 
     55 #include "opt_usbverbose.h"
     56 
     57 #ifdef USB_DEBUG
     58 #define DPRINTF(x)	if (usbdebug) printf x
     59 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     60 extern int usbdebug;
     61 #else
     62 #define DPRINTF(x)
     63 #define DPRINTFN(n,x)
     64 #endif
     65 
     66 char *usbd_get_string __P((usbd_device_handle, int, char *));
     67 usbd_status usbd_get_desc __P((usbd_device_handle dev, int type,
     68 			       int index, int len, void *desc));
     69 int usbd_getnewaddr __P((usbd_bus_handle bus));
     70 int usbd_print __P((void *aux, const char *pnp));
     71 int usbd_submatch __P((struct device *, struct cfdata *cf, void *));
     72 usb_interface_descriptor_t *usbd_find_idesc __P((usb_config_descriptor_t *cd,
     73 						 int ino, int ano));
     74 usbd_status usbd_fill_iface_data __P((usbd_device_handle dev, int i, int a));
     75 void usbd_free_iface_data __P((usbd_device_handle dev, int ifcno));
     76 void usbd_kill_pipe __P((usbd_pipe_handle));
     77 
     78 #ifdef USBVERBOSE
     79 typedef u_int16_t usb_vendor_id_t;
     80 typedef u_int16_t usb_product_id_t;
     81 
     82 /*
     83  * Descriptions of of known vendors and devices ("products").
     84  */
     85 struct usb_knowndev {
     86 	usb_vendor_id_t		vendor;
     87 	usb_product_id_t	product;
     88 	int			flags;
     89 	char			*vendorname, *productname;
     90 };
     91 #define	USB_KNOWNDEV_NOPROD	0x01		/* match on vendor only */
     92 
     93 #include <dev/usb/usbdevs_data.h>
     94 #endif /* USBVERBOSE */
     95 
     96 
     97 char *
     98 usbd_get_string(dev, si, buf)
     99 	usbd_device_handle dev;
    100 	int si;
    101 	char *buf;
    102 {
    103 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
    104 	usb_device_request_t req;
    105 	usb_string_descriptor_t us;
    106 	char *s;
    107 	int i, n;
    108 	u_int16_t c;
    109 	usbd_status r;
    110 
    111 	if (si == 0)
    112 		return 0;
    113 	req.bmRequestType = UT_READ_DEVICE;
    114 	req.bRequest = UR_GET_DESCRIPTOR;
    115 	USETW2(req.wValue, UDESC_STRING, si);
    116 	USETW(req.wIndex, 0);
    117 	USETW(req.wLength, 1);	/* only size byte first */
    118 	r = usbd_do_request(dev, &req, &us);
    119 	if (r != USBD_NORMAL_COMPLETION)
    120 		return 0;
    121 	USETW(req.wLength, us.bLength);	/* the whole string */
    122 	r = usbd_do_request(dev, &req, &us);
    123 	if (r != USBD_NORMAL_COMPLETION)
    124 		return 0;
    125 	s = buf;
    126 	n = us.bLength / 2 - 1;
    127 	for (i = 0; i < n; i++) {
    128 		c = UGETW(us.bString[i]);
    129 		/* Convert from Unicode, handle buggy strings. */
    130 		if ((c & 0xff00) == 0)
    131 			*s++ = c;
    132 		else if ((c & 0x00ff) == 0 && swap)
    133 			*s++ = c >> 8;
    134 		else
    135 			*s++ = '?';
    136 	}
    137 	*s++ = 0;
    138 	return buf;
    139 }
    140 
    141 void
    142 usbd_devinfo_vp(dev, v, p)
    143 	usbd_device_handle dev;
    144 	char *v, *p;
    145 {
    146 	usb_device_descriptor_t *udd = &dev->ddesc;
    147 	char *vendor = 0, *product = 0;
    148 #ifdef USBVERBOSE
    149 	struct usb_knowndev *kdp;
    150 #endif
    151 
    152 	vendor = usbd_get_string(dev, udd->iManufacturer, v);
    153 	product = usbd_get_string(dev, udd->iProduct, p);
    154 #ifdef USBVERBOSE
    155 	if (!vendor) {
    156 		for(kdp = usb_knowndevs;
    157 		    kdp->vendorname != NULL;
    158 		    kdp++) {
    159 			if (kdp->vendor == UGETW(udd->idVendor) &&
    160 			    (kdp->product == UGETW(udd->idProduct) ||
    161 			     (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
    162 				break;
    163 		}
    164 		if (kdp->vendorname == NULL)
    165 			vendor = product = NULL;
    166 		else {
    167 			vendor = kdp->vendorname;
    168 			product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
    169 				kdp->productname : NULL;
    170 		}
    171 	}
    172 #endif
    173 	if (vendor)
    174 		strcpy(v, vendor);
    175 	else
    176 		sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
    177 	if (product)
    178 		strcpy(p, product);
    179 	else
    180 		sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
    181 }
    182 
    183 int
    184 usbd_printBCD(cp, bcd)
    185 	char *cp;
    186 	int bcd;
    187 {
    188 	return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
    189 }
    190 
    191 void
    192 usbd_devinfo(dev, showclass, cp)
    193 	usbd_device_handle dev;
    194 	int showclass;
    195 	char *cp;
    196 {
    197 	usb_device_descriptor_t *udd = &dev->ddesc;
    198 	char vendor[USB_MAX_STRING_LEN];
    199 	char product[USB_MAX_STRING_LEN];
    200 	int bcdDevice, bcdUSB;
    201 
    202 	usbd_devinfo_vp(dev, vendor, product);
    203 	cp += sprintf(cp, "%s", vendor);
    204 	cp += sprintf(cp, " %s", product);
    205 	if (showclass)
    206 		cp += sprintf(cp, " (class %d/%d)",
    207 			      udd->bDeviceClass, udd->bDeviceSubClass);
    208 	bcdUSB = UGETW(udd->bcdUSB);
    209 	bcdDevice = UGETW(udd->bcdDevice);
    210 	cp += sprintf(cp, " (rev ");
    211 	cp += usbd_printBCD(cp, bcdUSB);
    212 	*cp++ = '/';
    213 	cp += usbd_printBCD(cp, bcdDevice);
    214 	*cp++ = ')';
    215 	cp += sprintf(cp, " (addr %d)", dev->address);
    216 }
    217 
    218 /* Delay for a certain number of ms */
    219 void
    220 usbd_delay_ms(bus, ms)
    221 	usbd_bus_handle bus;
    222 	int ms;
    223 {
    224 	/* Wait at least two clock ticks so we know the time has passed. */
    225 	if (bus->use_polling)
    226 		delay((ms+1) * 1000);
    227 	else
    228 		tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
    229 }
    230 
    231 usbd_status
    232 usbd_reset_port(dev, port, ps)
    233 	usbd_device_handle dev;
    234 	int port;
    235 	usb_port_status_t *ps;
    236 {
    237 	usb_device_request_t req;
    238 	usbd_status r;
    239 	int n;
    240 
    241 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    242 	req.bRequest = UR_SET_FEATURE;
    243 	USETW(req.wValue, UHF_PORT_RESET);
    244 	USETW(req.wIndex, port);
    245 	USETW(req.wLength, 0);
    246 	r = usbd_do_request(dev, &req, 0);
    247 	DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%d\n",
    248 		    port, r));
    249 	if (r != USBD_NORMAL_COMPLETION)
    250 		return (r);
    251 	n = 10;
    252 	do {
    253 		/* Wait for device to recover from reset. */
    254 		usbd_delay_ms(dev->bus, USB_PORT_RESET_DELAY);
    255 		r = usbd_get_port_status(dev, port, ps);
    256 		if (r != USBD_NORMAL_COMPLETION) {
    257 			DPRINTF(("usbd_reset_port: get status failed %d\n",r));
    258 			return (r);
    259 		}
    260 	} while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
    261 	if (n == 0) {
    262 		printf("usbd_reset_port: timeout\n");
    263 		return (USBD_IOERROR);
    264 	}
    265 	r = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    266 #ifdef USB_DEBUG
    267 	if (r != USBD_NORMAL_COMPLETION)
    268 		DPRINTF(("usbd_reset_port: clear port feature failed %d\n",r));
    269 #endif
    270 	return (r);
    271 }
    272 
    273 usb_interface_descriptor_t *
    274 usbd_find_idesc(cd, ino, ano)
    275 	usb_config_descriptor_t *cd;
    276 	int ino;
    277 	int ano;
    278 {
    279 	char *p = (char *)cd;
    280 	char *end = p + UGETW(cd->wTotalLength);
    281 	usb_interface_descriptor_t *d;
    282 
    283 	for (; p < end; p += d->bLength) {
    284 		d = (usb_interface_descriptor_t *)p;
    285 		if (p + d->bLength <= end &&
    286 		    d->bDescriptorType == UDESC_INTERFACE &&
    287 		    d->bInterfaceNumber == ino && d->bAlternateSetting == ano)
    288 			return (d);
    289 	}
    290 	return (0);
    291 }
    292 
    293 usbd_status
    294 usbd_fill_iface_data(dev, ino, ano)
    295 	usbd_device_handle dev;
    296 	int ino;
    297 	int ano;
    298 {
    299 	usbd_interface_handle ifc = &dev->ifaces[ino];
    300 	usb_endpoint_descriptor_t *ed;
    301 	char *p, *end;
    302 	int endpt, nendpt;
    303 	usbd_status r;
    304 
    305 	DPRINTFN(5,("usbd_fill_iface_data: ino=%d ano=%d\n", ino, ano));
    306 	ifc->device = dev;
    307 	ifc->state = USBD_INTERFACE_ACTIVE;
    308 	ifc->idesc = usbd_find_idesc(dev->cdesc, ino, ano);
    309 	if (ifc->idesc == 0)
    310 		return (USBD_INVAL);
    311 	nendpt = ifc->idesc->bNumEndpoints;
    312 	DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
    313 	if (nendpt != 0) {
    314 		ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
    315 					M_USB, M_NOWAIT);
    316 		if (ifc->endpoints == 0)
    317 			return (USBD_NOMEM);
    318 	} else
    319 		ifc->endpoints = 0;
    320 	ifc->priv = 0;
    321 	p = (char *)ifc->idesc + ifc->idesc->bLength;
    322 	end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
    323 	for (endpt = 0; endpt < nendpt; endpt++) {
    324 		DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
    325 		for (; p < end; p += ed->bLength) {
    326 			ed = (usb_endpoint_descriptor_t *)p;
    327 			DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p len=%d type=%d\n",
    328 				 p, end, ed->bLength, ed->bDescriptorType));
    329 			if (p + ed->bLength <= end &&
    330 			    ed->bDescriptorType == UDESC_ENDPOINT)
    331 				goto found;
    332 			if (ed->bDescriptorType == UDESC_INTERFACE)
    333 				break;
    334 		}
    335 		r = USBD_INVAL;
    336 		goto bad;
    337 	found:
    338 		ifc->endpoints[endpt].edesc = ed;
    339 		ifc->endpoints[endpt].state = USBD_ENDPOINT_ACTIVE;
    340 		ifc->endpoints[endpt].refcnt = 0;
    341 		ifc->endpoints[endpt].toggle = 0;
    342 	}
    343 	LIST_INIT(&ifc->pipes);
    344 	return (USBD_NORMAL_COMPLETION);
    345  bad:
    346 	free(ifc->endpoints, M_USB);
    347 	return (r);
    348 }
    349 
    350 void
    351 usbd_free_iface_data(dev, ifcno)
    352 	usbd_device_handle dev;
    353 	int ifcno;
    354 {
    355 	usbd_interface_handle ifc = &dev->ifaces[ifcno];
    356 	if (ifc->endpoints)
    357 		free(ifc->endpoints, M_USB);
    358 }
    359 
    360 usbd_status
    361 usbd_set_config_no(dev, no)
    362 	usbd_device_handle dev;
    363 	int no;
    364 {
    365 	usb_status_t ds;
    366 	usb_hub_status_t hs;
    367 	usb_config_descriptor_t cd, *cdp;
    368 	usbd_status r;
    369 	int ifcno, nifc, len, selfpowered, power;
    370 
    371 	DPRINTFN(5, ("usbd_set_config_no: dev=%p no=%d\n", dev, no));
    372 
    373 	/* XXX check that all interfaces are idle */
    374 	if (dev->config != 0) {
    375 		DPRINTF(("usbd_set_config_no: free old config\n"));
    376 		/* Free all configuration data structures. */
    377 		nifc = dev->cdesc->bNumInterface;
    378 		for (ifcno = 0; ifcno < nifc; ifcno++)
    379 			usbd_free_iface_data(dev, ifcno);
    380 		free(dev->ifaces, M_USB);
    381 		free(dev->cdesc, M_USB);
    382 		dev->ifaces = 0;
    383 		dev->cdesc = 0;
    384 		dev->config = 0;
    385 		dev->state = USBD_DEVICE_ADDRESSED;
    386 	}
    387 
    388 	/* Figure out what config number to use. */
    389 	r = usbd_get_config_desc(dev, no, &cd);
    390 	if (r != USBD_NORMAL_COMPLETION)
    391 		return (r);
    392 	len = UGETW(cd.wTotalLength);
    393 	cdp = malloc(len, M_USB, M_NOWAIT);
    394 	if (cdp == 0)
    395 		return (USBD_NOMEM);
    396 	r = usbd_get_desc(dev, UDESC_CONFIG, no, len, cdp);
    397 	if (r != USBD_NORMAL_COMPLETION)
    398 		goto bad;
    399 	selfpowered = 0;
    400 	if (cdp->bmAttributes & UC_SELF_POWERED) {
    401 		/* May be self powered. */
    402 		if (cdp->bmAttributes & UC_BUS_POWERED) {
    403 			/* Must ask device. */
    404 			if (dev->quirks->uq_flags & UQ_HUB_POWER) {
    405 				/* Buggy hub, use hub descriptor. */
    406 				r = usbd_get_hub_status(dev, &hs);
    407 				if (r == USBD_NORMAL_COMPLETION &&
    408 				    !(UGETW(hs.wHubStatus) & UHS_LOCAL_POWER))
    409 					selfpowered = 1;
    410 			} else {
    411 				r = usbd_get_device_status(dev, &ds);
    412 				if (r == USBD_NORMAL_COMPLETION &&
    413 				    (UGETW(ds.wStatus) & UDS_SELF_POWERED))
    414 					selfpowered = 1;
    415 			}
    416 			DPRINTF(("usbd_set_config_no: status=0x%04x, error=%d\n",
    417 				 UGETW(ds.wStatus), r));
    418 		} else
    419 			selfpowered = 1;
    420 	}
    421 	DPRINTF(("usbd_set_config_no: (addr %d) attr=0x%02x, selfpowered=%d, power=%d, powerquirk=%x\n",
    422 		 dev->address, cdp->bmAttributes,
    423 		 selfpowered, cdp->bMaxPower * 2,
    424 		 dev->quirks->uq_flags & UQ_HUB_POWER));
    425 #ifdef USB_DEBUG
    426 	if (!dev->powersrc) {
    427 		printf("usbd_set_config_no: No power source?\n");
    428 		return (EIO);
    429 	}
    430 #endif
    431 	power = cdp->bMaxPower * 2;
    432 	if (power > dev->powersrc->power) {
    433 		/* XXX print nicer message. */
    434 		printf("%s: device addr %d (config %d) exceeds power budget, %d mA > %d mA\n",
    435 		       dev->bus->bdev.dv_xname, dev->address,
    436 		       cdp->bConfigurationValue,
    437 		       power, dev->powersrc->power);
    438 		r = USBD_NO_POWER;
    439 		goto bad;
    440 	}
    441 	dev->power = power;
    442 	dev->self_powered = selfpowered;
    443 
    444 	r = usbd_set_config(dev, cdp->bConfigurationValue);
    445 	if (r != USBD_NORMAL_COMPLETION) {
    446 		DPRINTF(("usbd_set_config_no: setting config=%d failed, error=%d\n",
    447 			 cdp->bConfigurationValue, r));
    448 		goto bad;
    449 	}
    450 	DPRINTF(("usbd_set_config_no: setting new config %d\n",
    451 		 cdp->bConfigurationValue));
    452 	nifc = cdp->bNumInterface;
    453 	dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
    454 			     M_USB, M_NOWAIT);
    455 	if (dev->ifaces == 0) {
    456 		r = USBD_NOMEM;
    457 		goto bad;
    458 	}
    459 	DPRINTFN(5,("usbd_set_config_no: dev=%p cdesc=%p\n", dev, cdp));
    460 	dev->cdesc = cdp;
    461 	dev->config = cdp->bConfigurationValue;
    462 	dev->state = USBD_DEVICE_CONFIGURED;
    463 	for (ifcno = 0; ifcno < nifc; ifcno++) {
    464 		r = usbd_fill_iface_data(dev, ifcno, 0);
    465 		if (r != USBD_NORMAL_COMPLETION) {
    466 			while (--ifcno >= 0)
    467 				usbd_free_iface_data(dev, ifcno);
    468 			goto bad;
    469 		}
    470 	}
    471 
    472 	return (USBD_NORMAL_COMPLETION);
    473 
    474  bad:
    475 	free(cdp, M_USB);
    476 	return (r);
    477 }
    478 
    479 /* XXX add function for alternate settings */
    480 
    481 usbd_status
    482 usbd_setup_pipe(dev, iface, ep, pipe)
    483 	usbd_device_handle dev;
    484 	usbd_interface_handle iface;
    485 	struct usbd_endpoint *ep;
    486 	usbd_pipe_handle *pipe;
    487 {
    488 	usbd_pipe_handle p;
    489 	usbd_status r;
    490 
    491 	DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
    492 		    dev, iface, ep, pipe));
    493 	p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
    494 	if (p == 0)
    495 		return (USBD_NOMEM);
    496 	p->device = dev;
    497 	p->iface = iface;
    498 	p->state = USBD_PIPE_ACTIVE;
    499 	p->endpoint = ep;
    500 	ep->refcnt++;
    501 	p->refcnt = 1;
    502 	p->intrreqh = 0;
    503 	p->running = 0;
    504 	SIMPLEQ_INIT(&p->queue);
    505 	r = dev->bus->open_pipe(p);
    506 	if (r != USBD_NORMAL_COMPLETION) {
    507 		DPRINTF(("usbd_setup_pipe: endpoint=%d failed, error=%d\n",
    508 			 ep->edesc->bEndpointAddress, r));
    509 		free(p, M_USB);
    510 		return (r);
    511 	}
    512 	*pipe = p;
    513 	return (USBD_NORMAL_COMPLETION);
    514 }
    515 
    516 /* Abort the device control pipe. */
    517 void
    518 usbd_kill_pipe(pipe)
    519 	usbd_pipe_handle pipe;
    520 {
    521 	pipe->methods->close(pipe);
    522 	pipe->endpoint->refcnt--;
    523 	free(pipe, M_USB);
    524 }
    525 
    526 int
    527 usbd_getnewaddr(bus)
    528 	usbd_bus_handle bus;
    529 {
    530 	int i;
    531 
    532 	for (i = 1; i < USB_MAX_DEVICES; i++)
    533 		if (bus->devices[i] == 0)
    534 			return (i);
    535 	return (-1);
    536 }
    537 
    538 /*
    539  * Called when a new device has been put in the powered state,
    540  * but not yet in the addressed state.
    541  * Get initial descriptor, set the address, get full descriptor,
    542  * and attach a driver.
    543  */
    544 usbd_status
    545 usbd_new_device(parent, bus, depth, lowspeed, port, up)
    546 	struct device *parent;
    547 	usbd_bus_handle bus;
    548 	int depth;
    549 	int lowspeed;
    550 	int port;
    551 	struct usbd_port *up;
    552 {
    553 	usbd_device_handle dev;
    554 	usb_device_descriptor_t *d;
    555 	usbd_status r;
    556 	struct usb_attach_arg uaa;
    557 	int addr;
    558 	int found, i, confi;
    559 
    560 	DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
    561 		 bus, depth, lowspeed));
    562 	addr = usbd_getnewaddr(bus);
    563 	if (addr < 0) {
    564 		printf("%s: No free USB addresses, new device ignored.\n",
    565 		       bus->bdev.dv_xname);
    566 		return (USBD_NO_ADDR);
    567 	}
    568 
    569 	dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
    570 	if (dev == 0)
    571 		return (USBD_NOMEM);
    572 	memset(dev, 0, sizeof(*dev));
    573 
    574 	dev->bus = bus;
    575 
    576 	/* Set up default endpoint handle. */
    577 	dev->def_ep.edesc = &dev->def_ep_desc;
    578 	dev->def_ep.state = USBD_ENDPOINT_ACTIVE;
    579 	dev->def_ep.refcnt = 0;
    580 	dev->def_ep.toggle = 0;	/* XXX */
    581 
    582 	/* Set up default endpoint descriptor. */
    583 	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
    584 	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
    585 	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
    586 	dev->def_ep_desc.bmAttributes = UE_CONTROL;
    587 	USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
    588 	dev->def_ep_desc.bInterval = 0;
    589 
    590 	dev->state = USBD_DEVICE_DEFAULT;
    591 	dev->quirks = &usbd_no_quirk;
    592 	dev->address = USB_START_ADDR;
    593 	dev->ddesc.bMaxPacketSize = 0;
    594 	dev->lowspeed = lowspeed != 0;
    595 	dev->depth = depth;
    596 	dev->powersrc = up;
    597 
    598 	/* Establish the the default pipe. */
    599 	r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
    600 	if (r != USBD_NORMAL_COMPLETION)
    601 		goto bad1;
    602 
    603 	up->device = dev;
    604 	d = &dev->ddesc;
    605 	/* Try a few times in case the device is slow (i.e. outside specs.) */
    606 	for (i = 0; i < 5; i++) {
    607 		/* Get the first 8 bytes of the device descriptor. */
    608 		r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, d);
    609 		if (r == USBD_NORMAL_COMPLETION)
    610 			break;
    611 		usbd_delay_ms(dev->bus, 200);
    612 	}
    613 	if (r != USBD_NORMAL_COMPLETION) {
    614 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc failed\n",
    615 			      addr));
    616 		goto bad;
    617 	}
    618 
    619 	DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, subclass=%d, protocol=%d, maxpacket=%d, ls=%d\n",
    620 		 addr, UGETW(d->bcdUSB), d->bDeviceClass, d->bDeviceSubClass,
    621 		 d->bDeviceProtocol, d->bMaxPacketSize, dev->lowspeed));
    622 
    623 	USETW(dev->def_ep_desc.wMaxPacketSize, d->bMaxPacketSize);
    624 
    625 	/* Get the full device descriptor. */
    626 	r = usbd_get_device_desc(dev, d);
    627 	if (r != USBD_NORMAL_COMPLETION) {
    628 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc failed\n", addr));
    629 		goto bad;
    630 	}
    631 
    632 	/* Figure out what's wrong with this device. */
    633 	dev->quirks = usbd_find_quirk(d);
    634 
    635 	/* Set the address */
    636 	r = usbd_set_address(dev, addr);
    637 	if (r != USBD_NORMAL_COMPLETION) {
    638 		DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
    639 		r = USBD_SET_ADDR_FAILED;
    640 		goto bad;
    641 	}
    642 	dev->address = addr;	/* New device address now */
    643 	dev->state = USBD_DEVICE_ADDRESSED;
    644 	bus->devices[addr] = dev;
    645 
    646 	/* Assume 100mA bus powered for now. Changed when configured. */
    647 	dev->power = USB_MIN_POWER;
    648 	dev->self_powered = 0;
    649 
    650 	DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
    651 		 addr, dev, parent));
    652 
    653 	uaa.device = dev;
    654 	uaa.iface = 0;
    655 	uaa.usegeneric = 0;
    656 	uaa.port = port;
    657 	/* First try with device specific drivers. */
    658 	if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
    659 		return (USBD_NORMAL_COMPLETION);
    660 
    661 	DPRINTF(("usbd_new_device: no device driver found\n"));
    662 
    663 	/* Next try with interface drivers. */
    664 	for (confi = 0; confi < d->bNumConfigurations; confi++) {
    665 		r = usbd_set_config_no(dev, confi);
    666 		if (r != USBD_NORMAL_COMPLETION) {
    667 			printf("%s: set config at addr %d failed, error=%d\n",
    668 			       parent->dv_xname, addr, r);
    669 			goto bad;
    670 		}
    671 		for (found = i = 0; i < dev->cdesc->bNumInterface; i++) {
    672 			uaa.iface = &dev->ifaces[i];
    673 			if (config_found_sm(parent, &uaa, usbd_print,
    674 					    usbd_submatch))
    675 				found++;
    676 		}
    677 		if (found != 0)
    678 			return (USBD_NORMAL_COMPLETION);
    679 	}
    680 	/* No interfaces were attach in any of the configurations. */
    681 	if (d->bNumConfigurations > 0)
    682 		usbd_set_config_no(dev, 0);
    683 
    684 	DPRINTF(("usbd_new_device: no interface drivers found\n"));
    685 
    686 	/* Finally try the generic driver. */
    687 	uaa.iface = 0;
    688 	uaa.usegeneric = 1;
    689 	if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
    690 		return (USBD_NORMAL_COMPLETION);
    691 
    692 	DPRINTF(("usbd_new_device: generic attach failed\n"));
    693 
    694 	return (USBD_NORMAL_COMPLETION);
    695 
    696  bad:
    697 	usbd_kill_pipe(dev->default_pipe);
    698  bad1:
    699 	up->device = 0;
    700 	bus->devices[addr] = 0;
    701 	free(dev, M_USB);
    702 	return (r);
    703 }
    704 
    705 int
    706 usbd_print(aux, pnp)
    707 	void *aux;
    708 	const char *pnp;
    709 {
    710 	struct usb_attach_arg *uaa = aux;
    711 	char devinfo[1024];
    712 
    713 	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
    714 	if (pnp) {
    715 		if (!uaa->usegeneric)
    716 			return (QUIET);
    717 		usbd_devinfo(uaa->device, 1, devinfo);
    718 		printf("%s at %s", devinfo, pnp);
    719 	}
    720 	if (uaa->port != 0)
    721 		printf(" port %d", uaa->port);
    722 	return (UNCONF);
    723 }
    724 
    725 int
    726 usbd_submatch(parent, cf, aux)
    727 	struct device *parent;
    728 	struct cfdata *cf;
    729 	void *aux;
    730 {
    731 	struct usb_attach_arg *uaa = aux;
    732 
    733 	if (uaa->port != 0 &&
    734 	    cf->uhubcf_port != UHUB_UNK_PORT &&
    735 	    cf->uhubcf_port != uaa->port)
    736 		return 0;
    737 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    738 }
    739