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