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