Home | History | Annotate | Line # | Download | only in usb
usb_subr.c revision 1.4
      1 /*	$NetBSD: usb_subr.c,v 1.4 1998/07/23 01:46:27 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(ms)
    221 	int ms;
    222 {
    223 	/* Wait at least two clock ticks so we know the time has passed. */
    224 	if (usbd_use_polling)
    225 		delay((ms+1) * 1000);
    226 	else
    227 		tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
    228 }
    229 
    230 usbd_status
    231 usbd_reset_port(dev, port, ps)
    232 	usbd_device_handle dev;
    233 	int port;
    234 	usb_port_status_t *ps;
    235 {
    236 	usb_device_request_t req;
    237 	usbd_status r;
    238 	int n;
    239 
    240 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    241 	req.bRequest = UR_SET_FEATURE;
    242 	USETW(req.wValue, UHF_PORT_RESET);
    243 	USETW(req.wIndex, port);
    244 	USETW(req.wLength, 0);
    245 	r = usbd_do_request(dev, &req, 0);
    246 	DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%d\n",
    247 		    port, r));
    248 	if (r != USBD_NORMAL_COMPLETION)
    249 		return (r);
    250 	n = 10;
    251 	do {
    252 		/* Wait for device to recover from reset. */
    253 		usbd_delay_ms(USB_PORT_RESET_DELAY);
    254 		r = usbd_get_port_status(dev, port, ps);
    255 		if (r != USBD_NORMAL_COMPLETION) {
    256 			DPRINTF(("usbd_reset_port: get status failed %d\n",r));
    257 			return (r);
    258 		}
    259 	} while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
    260 	if (n == 0) {
    261 		printf("usbd_reset_port: timeout\n");
    262 		return (USBD_IOERROR);
    263 	}
    264 	r = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    265 #ifdef USB_DEBUG
    266 	if (r != USBD_NORMAL_COMPLETION)
    267 		DPRINTF(("usbd_reset_port: clear port feature failed %d\n",r));
    268 #endif
    269 	return (r);
    270 }
    271 
    272 usb_interface_descriptor_t *
    273 usbd_find_idesc(cd, ino, ano)
    274 	usb_config_descriptor_t *cd;
    275 	int ino;
    276 	int ano;
    277 {
    278 	char *p = (char *)cd;
    279 	char *end = p + UGETW(cd->wTotalLength);
    280 	usb_interface_descriptor_t *d;
    281 
    282 	for (; p < end; p += d->bLength) {
    283 		d = (usb_interface_descriptor_t *)p;
    284 		if (p + d->bLength <= end &&
    285 		    d->bDescriptorType == UDESC_INTERFACE &&
    286 		    d->bInterfaceNumber == ino && d->bAlternateSetting == ano)
    287 			return (d);
    288 	}
    289 	return (0);
    290 }
    291 
    292 usbd_status
    293 usbd_fill_iface_data(dev, ino, ano)
    294 	usbd_device_handle dev;
    295 	int ino;
    296 	int ano;
    297 {
    298 	usbd_interface_handle ifc = &dev->ifaces[ino];
    299 	usb_endpoint_descriptor_t *ed;
    300 	char *p, *end;
    301 	int endpt, nendpt;
    302 	usbd_status r;
    303 
    304 	DPRINTFN(5,("usbd_fill_iface_data: ino=%d ano=%d\n", ino, ano));
    305 	ifc->device = dev;
    306 	ifc->state = USBD_INTERFACE_ACTIVE;
    307 	ifc->idesc = usbd_find_idesc(dev->cdesc, ino, ano);
    308 	if (ifc->idesc == 0)
    309 		return (USBD_INVAL);
    310 	nendpt = ifc->idesc->bNumEndpoints;
    311 	DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
    312 	if (nendpt != 0) {
    313 		ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
    314 					M_USB, M_NOWAIT);
    315 		if (ifc->endpoints == 0)
    316 			return (USBD_NOMEM);
    317 	} else
    318 		ifc->endpoints = 0;
    319 	ifc->priv = 0;
    320 	p = (char *)ifc->idesc + ifc->idesc->bLength;
    321 	end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
    322 	for (endpt = 0; endpt < nendpt; endpt++) {
    323 		DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
    324 		for (; p < end; p += ed->bLength) {
    325 			ed = (usb_endpoint_descriptor_t *)p;
    326 			DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p len=%d type=%d\n",
    327 				 p, end, ed->bLength, ed->bDescriptorType));
    328 			if (p + ed->bLength <= end &&
    329 			    ed->bDescriptorType == UDESC_ENDPOINT)
    330 				goto found;
    331 			if (ed->bDescriptorType == UDESC_INTERFACE)
    332 				break;
    333 		}
    334 		r = USBD_INVAL;
    335 		goto bad;
    336 	found:
    337 		ifc->endpoints[endpt].edesc = ed;
    338 		ifc->endpoints[endpt].state = USBD_ENDPOINT_ACTIVE;
    339 		ifc->endpoints[endpt].refcnt = 0;
    340 		ifc->endpoints[endpt].toggle = 0;
    341 	}
    342 	LIST_INIT(&ifc->pipes);
    343 	return (USBD_NORMAL_COMPLETION);
    344  bad:
    345 	free(ifc->endpoints, M_USB);
    346 	return (r);
    347 }
    348 
    349 void
    350 usbd_free_iface_data(dev, ifcno)
    351 	usbd_device_handle dev;
    352 	int ifcno;
    353 {
    354 	usbd_interface_handle ifc = &dev->ifaces[ifcno];
    355 	if (ifc->endpoints)
    356 		free(ifc->endpoints, M_USB);
    357 }
    358 
    359 usbd_status
    360 usbd_set_config_no(dev, no)
    361 	usbd_device_handle dev;
    362 	int no;
    363 {
    364 	usb_status_t ds;
    365 	usb_hub_status_t hs;
    366 	usb_config_descriptor_t cd, *cdp;
    367 	usbd_status r;
    368 	int ifcno, nifc, len, selfpowered, power;
    369 
    370 	DPRINTFN(5, ("usbd_set_config_no: dev=%p no=%d\n", dev, no));
    371 
    372 	/* XXX check that all interfaces are idle */
    373 	if (dev->config != 0) {
    374 		DPRINTF(("usbd_set_config_no: free old config\n"));
    375 		/* Free all configuration data structures. */
    376 		nifc = dev->cdesc->bNumInterface;
    377 		for (ifcno = 0; ifcno < nifc; ifcno++)
    378 			usbd_free_iface_data(dev, ifcno);
    379 		free(dev->ifaces, M_USB);
    380 		free(dev->cdesc, M_USB);
    381 		dev->ifaces = 0;
    382 		dev->cdesc = 0;
    383 		dev->config = 0;
    384 		dev->state = USBD_DEVICE_ADDRESSED;
    385 	}
    386 
    387 	/* Figure out what config number to use. */
    388 	r = usbd_get_config_desc(dev, no, &cd);
    389 	if (r != USBD_NORMAL_COMPLETION)
    390 		return (r);
    391 	len = UGETW(cd.wTotalLength);
    392 	cdp = malloc(len, M_USB, M_NOWAIT);
    393 	if (cdp == 0)
    394 		return (USBD_NOMEM);
    395 	r = usbd_get_desc(dev, UDESC_CONFIG, no, len, cdp);
    396 	if (r != USBD_NORMAL_COMPLETION)
    397 		goto bad;
    398 	selfpowered = 0;
    399 	if (cdp->bmAttributes & UC_SELF_POWERED) {
    400 		/* May be self powered. */
    401 		if (cdp->bmAttributes & UC_BUS_POWERED) {
    402 			/* Must ask device. */
    403 			if (dev->quirks->uq_flags & UQ_HUB_POWER) {
    404 				/* Buggy hub, use hub descriptor. */
    405 				r = usbd_get_hub_status(dev, &hs);
    406 				if (r == USBD_NORMAL_COMPLETION &&
    407 				    !(UGETW(hs.wHubStatus) & UHS_LOCAL_POWER))
    408 					selfpowered = 1;
    409 			} else {
    410 				r = usbd_get_device_status(dev, &ds);
    411 				if (r == USBD_NORMAL_COMPLETION &&
    412 				    (UGETW(ds.wStatus) & UDS_SELF_POWERED))
    413 					selfpowered = 1;
    414 			}
    415 			DPRINTF(("usbd_set_config_no: status=0x%04x, error=%d\n",
    416 				 UGETW(ds.wStatus), r));
    417 		} else
    418 			selfpowered = 1;
    419 	}
    420 	DPRINTF(("usbd_set_config_no: (addr %d) attr=0x%02x, selfpowered=%d, power=%d, powerquirk=%x\n",
    421 		 dev->address, cdp->bmAttributes,
    422 		 selfpowered, cdp->bMaxPower * 2,
    423 		 dev->quirks->uq_flags & UQ_HUB_POWER));
    424 #ifdef USB_DEBUG
    425 	if (!dev->powersrc) {
    426 		printf("usbd_set_config_no: No power source?\n");
    427 		return (EIO);
    428 	}
    429 #endif
    430 	power = cdp->bMaxPower * 2;
    431 	if (power > dev->powersrc->power) {
    432 		/* XXX print nicer message. */
    433 		printf("usb: device addr %d (config %d) exceeds power budget, %d mA > %d mA\n",
    434 		       dev->address, cdp->bConfigurationValue,
    435 		       power, dev->powersrc->power);
    436 		r = USBD_NO_POWER;
    437 		goto bad;
    438 	}
    439 	dev->power = power;
    440 	dev->self_powered = selfpowered;
    441 
    442 	r = usbd_set_config(dev, cdp->bConfigurationValue);
    443 	if (r != USBD_NORMAL_COMPLETION) {
    444 		DPRINTF(("usbd_set_config_no: setting config=%d failed, error=%d\n",
    445 			 cdp->bConfigurationValue, r));
    446 		goto bad;
    447 	}
    448 	DPRINTF(("usbd_set_config_no: setting new config %d\n",
    449 		 cdp->bConfigurationValue));
    450 	nifc = cdp->bNumInterface;
    451 	dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
    452 			     M_USB, M_NOWAIT);
    453 	if (dev->ifaces == 0) {
    454 		r = USBD_NOMEM;
    455 		goto bad;
    456 	}
    457 	dev->cdesc = cdp;
    458 	dev->config = cdp->bConfigurationValue;
    459 	dev->state = USBD_DEVICE_CONFIGURED;
    460 	for (ifcno = 0; ifcno < nifc; ifcno++) {
    461 		r = usbd_fill_iface_data(dev, ifcno, 0);
    462 		if (r != USBD_NORMAL_COMPLETION) {
    463 			while (--ifcno >= 0)
    464 				usbd_free_iface_data(dev, ifcno);
    465 			goto bad;
    466 		}
    467 	}
    468 
    469 	return (USBD_NORMAL_COMPLETION);
    470 
    471  bad:
    472 	free(cdp, M_USB);
    473 	return (r);
    474 }
    475 
    476 /* XXX add function for alternate settings */
    477 
    478 usbd_status
    479 usbd_setup_pipe(dev, iface, ep, pipe)
    480 	usbd_device_handle dev;
    481 	usbd_interface_handle iface;
    482 	struct usbd_endpoint *ep;
    483 	usbd_pipe_handle *pipe;
    484 {
    485 	usbd_pipe_handle p;
    486 	usbd_status r;
    487 
    488 	DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
    489 		    dev, iface, ep, pipe));
    490 	p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
    491 	if (p == 0)
    492 		return (USBD_NOMEM);
    493 	p->device = dev;
    494 	p->iface = iface;
    495 	p->state = USBD_PIPE_ACTIVE;
    496 	p->endpoint = ep;
    497 	ep->refcnt++;
    498 	p->refcnt = 1;
    499 	p->intrreqh = 0;
    500 	p->running = 0;
    501 	SIMPLEQ_INIT(&p->queue);
    502 	r = dev->bus->open_pipe(p);
    503 	if (r != USBD_NORMAL_COMPLETION) {
    504 		DPRINTF(("usbd_setup_pipe: endpoint=%d failed, error=%d\n",
    505 			 ep->edesc->bEndpointAddress, r));
    506 		free(p, M_USB);
    507 		return (r);
    508 	}
    509 	*pipe = p;
    510 	return (USBD_NORMAL_COMPLETION);
    511 }
    512 
    513 /* Abort the device control pipe. */
    514 void
    515 usbd_kill_pipe(pipe)
    516 	usbd_pipe_handle pipe;
    517 {
    518 	pipe->methods->close(pipe);
    519 	pipe->endpoint->refcnt--;
    520 	free(pipe, M_USB);
    521 }
    522 
    523 int
    524 usbd_getnewaddr(bus)
    525 	usbd_bus_handle bus;
    526 {
    527 	int i;
    528 
    529 	for (i = 1; i < USB_MAX_DEVICES; i++)
    530 		if (bus->devices[i] == 0)
    531 			return (i);
    532 	return (-1);
    533 }
    534 
    535 /*
    536  * Called when a new device has been put in the powered state,
    537  * but not yet in the addressed state.
    538  * Get initial descriptor, set the address, get full descriptor,
    539  * and attach a driver.
    540  */
    541 usbd_status
    542 usbd_new_device(parent, bus, depth, lowspeed, port, up)
    543 	struct device *parent;
    544 	usbd_bus_handle bus;
    545 	int depth;
    546 	int lowspeed;
    547 	int port;
    548 	struct usbd_port *up;
    549 {
    550 	usbd_device_handle dev;
    551 	usb_device_descriptor_t *d;
    552 	usbd_status r;
    553 	struct usb_attach_arg uaa;
    554 	int addr;
    555 	int found, i, confi;
    556 
    557 	DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
    558 		 bus, depth, lowspeed));
    559 	addr = usbd_getnewaddr(bus);
    560 	if (addr < 0) {
    561 		printf("%s: No free USB addresses, new device ignored.\n",
    562 		       bus->bdev.dv_xname);
    563 		return (USBD_NO_ADDR);
    564 	}
    565 
    566 	dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
    567 	if (dev == 0)
    568 		return (USBD_NOMEM);
    569 	memset(dev, 0, sizeof(*dev));
    570 
    571 	dev->bus = bus;
    572 
    573 	/* Set up default endpoint handle. */
    574 	dev->def_ep.edesc = &dev->def_ep_desc;
    575 	dev->def_ep.state = USBD_ENDPOINT_ACTIVE;
    576 	dev->def_ep.refcnt = 0;
    577 	dev->def_ep.toggle = 0;	/* XXX */
    578 
    579 	/* Set up default endpoint descriptor. */
    580 	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
    581 	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
    582 	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
    583 	dev->def_ep_desc.bmAttributes = UE_CONTROL;
    584 	USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
    585 	dev->def_ep_desc.bInterval = 0;
    586 
    587 	dev->state = USBD_DEVICE_DEFAULT;
    588 	dev->quirks = &usbd_no_quirk;
    589 	dev->address = USB_START_ADDR;
    590 	dev->ddesc.bMaxPacketSize = 0;
    591 	dev->lowspeed = lowspeed != 0;
    592 	dev->depth = depth;
    593 	dev->powersrc = up;
    594 
    595 	/* Establish the the default pipe. */
    596 	r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
    597 	if (r != USBD_NORMAL_COMPLETION)
    598 		goto bad1;
    599 
    600 	up->device = dev;
    601 	d = &dev->ddesc;
    602 	/* Get the first 8 bytes of the device descriptor. */
    603 	r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, d);
    604 	if (r != USBD_NORMAL_COMPLETION) {
    605 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc failed\n", addr));
    606 		goto bad;
    607 	}
    608 
    609 	DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, subclass=%d, protocol=%d, maxpacket=%d, ls=%d\n",
    610 		 addr, UGETW(d->bcdUSB), d->bDeviceClass, d->bDeviceSubClass,
    611 		 d->bDeviceProtocol, d->bMaxPacketSize, dev->lowspeed));
    612 
    613 	USETW(dev->def_ep_desc.wMaxPacketSize, d->bMaxPacketSize);
    614 
    615 	/* Get the full device descriptor. */
    616 	r = usbd_get_device_desc(dev, d);
    617 	if (r != USBD_NORMAL_COMPLETION) {
    618 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc failed\n", addr));
    619 		goto bad;
    620 	}
    621 
    622 	/* Figure out what's wrong with this device. */
    623 	dev->quirks = usbd_find_quirk(d);
    624 
    625 	/* Set the address */
    626 	r = usbd_set_address(dev, addr);
    627 	if (r != USBD_NORMAL_COMPLETION) {
    628 		DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
    629 		r = USBD_SET_ADDR_FAILED;
    630 		goto bad;
    631 	}
    632 	dev->address = addr;	/* New device address now */
    633 	dev->state = USBD_DEVICE_ADDRESSED;
    634 	bus->devices[addr] = dev;
    635 
    636 	/* Assume 100mA bus powered for now. Changed when configured. */
    637 	dev->power = USB_MIN_POWER;
    638 	dev->self_powered = 0;
    639 
    640 	DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
    641 		 addr, dev, parent));
    642 
    643 	uaa.device = dev;
    644 	uaa.iface = 0;
    645 	uaa.usegeneric = 0;
    646 	uaa.port = port;
    647 	/* First try with device specific drivers. */
    648 	if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
    649 		return (USBD_NORMAL_COMPLETION);
    650 
    651 	DPRINTF(("usbd_new_device: no device driver found\n"));
    652 	/* Next try with interface drivers. */
    653 	/* XXX should we try all configurations? */
    654 
    655 	for (confi = 0; confi < d->bNumConfigurations; confi++) {
    656 		r = usbd_set_config_no(dev, confi);
    657 		if (r != USBD_NORMAL_COMPLETION) {
    658 			printf("%s: set config at addr %d failed, error=%d\n",
    659 			       parent->dv_xname, addr, r);
    660 			goto bad;
    661 		}
    662 		for (found = i = 0; i < dev->cdesc->bNumInterface; i++) {
    663 			uaa.iface = &dev->ifaces[i];
    664 			if (config_found_sm(parent, &uaa, usbd_print,
    665 					    usbd_submatch))
    666 				found++;
    667 		}
    668 		if (found != 0)
    669 			return (USBD_NORMAL_COMPLETION);
    670 	}
    671 	/* No interfaces were attach in any of the configurations. */
    672 	if (d->bNumConfigurations > 0)
    673 		usbd_set_config_no(dev, 0);
    674 
    675 	DPRINTF(("usbd_new_device: no interface drivers found\n"));
    676 
    677 	/* Finally try the generic driver. */
    678 	uaa.iface = 0;
    679 	uaa.usegeneric = 1;
    680 	if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
    681 		return (USBD_NORMAL_COMPLETION);
    682 
    683 	DPRINTF(("usbd_new_device: generic attach failed\n"));
    684 
    685 	return (USBD_NORMAL_COMPLETION);
    686 
    687  bad:
    688 	usbd_kill_pipe(dev->default_pipe);
    689  bad1:
    690 	up->device = 0;
    691 	bus->devices[addr] = 0;
    692 	free(dev, M_USB);
    693 	return (r);
    694 }
    695 
    696 int
    697 usbd_print(aux, pnp)
    698 	void *aux;
    699 	const char *pnp;
    700 {
    701 	struct usb_attach_arg *uaa = aux;
    702 	char devinfo[1024];
    703 
    704 	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
    705 	if (pnp) {
    706 		if (!uaa->usegeneric)
    707 			return (QUIET);
    708 		usbd_devinfo(uaa->device, 1, devinfo);
    709 		printf("%s at %s", devinfo, pnp);
    710 	}
    711 	if (uaa->port != 0)
    712 		printf(" port %d", uaa->port);
    713 	return (UNCONF);
    714 }
    715 
    716 int
    717 usbd_submatch(parent, cf, aux)
    718 	struct device *parent;
    719 	struct cfdata *cf;
    720 	void *aux;
    721 {
    722 	struct usb_attach_arg *uaa = aux;
    723 
    724 	if (uaa->port != 0 &&
    725 	    cf->uhubcf_port != UHUB_UNK_PORT &&
    726 	    cf->uhubcf_port != uaa->port)
    727 		return 0;
    728 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    729 }
    730