Home | History | Annotate | Line # | Download | only in usb
usb_subr.c revision 1.29.4.1
      1  1.29.4.1   thorpej /*	$NetBSD: usb_subr.c,v 1.29.4.1 1999/06/21 01:19:29 thorpej Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4       1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5       1.1  augustss  * All rights reserved.
      6       1.1  augustss  *
      7       1.9  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.9  augustss  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9       1.9  augustss  * Carlstedt Research & Technology.
     10       1.1  augustss  *
     11       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12       1.1  augustss  * modification, are permitted provided that the following conditions
     13       1.1  augustss  * are met:
     14       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19       1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     20       1.1  augustss  *    must display the following acknowledgement:
     21       1.1  augustss  *        This product includes software developed by the NetBSD
     22       1.1  augustss  *        Foundation, Inc. and its contributors.
     23       1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1  augustss  *    contributors may be used to endorse or promote products derived
     25       1.1  augustss  *    from this software without specific prior written permission.
     26       1.1  augustss  *
     27       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1  augustss  */
     39       1.1  augustss 
     40       1.1  augustss #include <sys/param.h>
     41       1.1  augustss #include <sys/systm.h>
     42       1.1  augustss #include <sys/kernel.h>
     43       1.1  augustss #include <sys/malloc.h>
     44      1.18  augustss #if defined(__NetBSD__)
     45       1.1  augustss #include <sys/device.h>
     46      1.18  augustss #elif defined(__FreeBSD__)
     47      1.18  augustss #include <sys/module.h>
     48      1.18  augustss #include <sys/bus.h>
     49      1.18  augustss #endif
     50       1.1  augustss #include <sys/proc.h>
     51       1.1  augustss #include <sys/select.h>
     52       1.1  augustss 
     53       1.1  augustss #include <dev/usb/usb.h>
     54       1.1  augustss 
     55       1.1  augustss #include <dev/usb/usbdi.h>
     56       1.1  augustss #include <dev/usb/usbdi_util.h>
     57       1.1  augustss #include <dev/usb/usbdivar.h>
     58       1.1  augustss #include <dev/usb/usbdevs.h>
     59       1.1  augustss #include <dev/usb/usb_quirks.h>
     60       1.1  augustss 
     61      1.27  augustss #if defined(__FreeBSD__)
     62      1.27  augustss #include <machine/clock.h>
     63      1.27  augustss #define delay(d)         DELAY(d)
     64      1.27  augustss #endif
     65      1.27  augustss 
     66       1.1  augustss #ifdef USB_DEBUG
     67       1.1  augustss #define DPRINTF(x)	if (usbdebug) printf x
     68       1.1  augustss #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     69       1.1  augustss extern int usbdebug;
     70       1.1  augustss #else
     71       1.1  augustss #define DPRINTF(x)
     72       1.1  augustss #define DPRINTFN(n,x)
     73       1.1  augustss #endif
     74       1.1  augustss 
     75       1.7  augustss static usbd_status	usbd_set_config __P((usbd_device_handle, int));
     76       1.1  augustss char *usbd_get_string __P((usbd_device_handle, int, char *));
     77       1.1  augustss int usbd_getnewaddr __P((usbd_bus_handle bus));
     78      1.27  augustss #if defined(__NetBSD__)
     79       1.1  augustss int usbd_print __P((void *aux, const char *pnp));
     80      1.27  augustss int usbd_submatch __P((bdevice *, struct cfdata *cf, void *));
     81      1.18  augustss #endif
     82       1.1  augustss void usbd_free_iface_data __P((usbd_device_handle dev, int ifcno));
     83       1.1  augustss void usbd_kill_pipe __P((usbd_pipe_handle));
     84      1.18  augustss usbd_status usbd_probe_and_attach
     85      1.18  augustss 	__P((bdevice *parent, usbd_device_handle dev, int port, int addr));
     86       1.1  augustss 
     87      1.27  augustss 
     88       1.1  augustss #ifdef USBVERBOSE
     89       1.1  augustss typedef u_int16_t usb_vendor_id_t;
     90       1.1  augustss typedef u_int16_t usb_product_id_t;
     91       1.1  augustss 
     92       1.1  augustss /*
     93       1.1  augustss  * Descriptions of of known vendors and devices ("products").
     94       1.1  augustss  */
     95       1.1  augustss struct usb_knowndev {
     96       1.1  augustss 	usb_vendor_id_t		vendor;
     97       1.1  augustss 	usb_product_id_t	product;
     98       1.1  augustss 	int			flags;
     99       1.1  augustss 	char			*vendorname, *productname;
    100       1.1  augustss };
    101       1.1  augustss #define	USB_KNOWNDEV_NOPROD	0x01		/* match on vendor only */
    102       1.1  augustss 
    103       1.1  augustss #include <dev/usb/usbdevs_data.h>
    104       1.1  augustss #endif /* USBVERBOSE */
    105       1.1  augustss 
    106      1.12  augustss #ifdef USB_DEBUG
    107      1.12  augustss char *usbd_error_strs[] = {
    108      1.12  augustss 	"NORMAL_COMPLETION",
    109      1.12  augustss 	"IN_PROGRESS",
    110      1.12  augustss 	"PENDING_REQUESTS",
    111      1.12  augustss 	"NOT_STARTED",
    112      1.12  augustss 	"INVAL",
    113      1.12  augustss 	"NOMEM",
    114      1.12  augustss 	"CANCELLED",
    115      1.12  augustss 	"BAD_ADDRESS",
    116      1.12  augustss 	"IN_USE",
    117      1.12  augustss 	"NO_ADDR",
    118      1.12  augustss 	"SET_ADDR_FAILED",
    119      1.12  augustss 	"NO_POWER",
    120      1.12  augustss 	"TOO_DEEP",
    121      1.12  augustss 	"IOERROR",
    122      1.12  augustss 	"NOT_CONFIGURED",
    123      1.12  augustss 	"TIMEOUT",
    124      1.12  augustss 	"SHORT_XFER",
    125      1.12  augustss 	"STALLED",
    126  1.29.4.1   thorpej 	"INTERRUPTED",
    127      1.12  augustss 	"XXX",
    128      1.12  augustss };
    129      1.12  augustss #endif
    130       1.1  augustss 
    131      1.13  augustss usbd_status
    132      1.13  augustss usbd_get_string_desc(dev, sindex, langid, sdesc)
    133      1.13  augustss 	usbd_device_handle dev;
    134      1.13  augustss 	int sindex;
    135      1.13  augustss 	int langid;
    136      1.13  augustss 	usb_string_descriptor_t *sdesc;
    137      1.13  augustss {
    138      1.13  augustss 	usb_device_request_t req;
    139      1.13  augustss 	usbd_status r;
    140      1.13  augustss 
    141      1.13  augustss 	req.bmRequestType = UT_READ_DEVICE;
    142      1.13  augustss 	req.bRequest = UR_GET_DESCRIPTOR;
    143      1.13  augustss 	USETW2(req.wValue, UDESC_STRING, sindex);
    144      1.13  augustss 	USETW(req.wIndex, langid);
    145      1.13  augustss 	USETW(req.wLength, 1);	/* only size byte first */
    146      1.13  augustss 	r = usbd_do_request(dev, &req, sdesc);
    147      1.13  augustss 	if (r != USBD_NORMAL_COMPLETION)
    148      1.13  augustss 		return (r);
    149      1.13  augustss 	USETW(req.wLength, sdesc->bLength);	/* the whole string */
    150      1.13  augustss 	return (usbd_do_request(dev, &req, sdesc));
    151      1.13  augustss }
    152      1.13  augustss 
    153       1.1  augustss char *
    154       1.1  augustss usbd_get_string(dev, si, buf)
    155       1.1  augustss 	usbd_device_handle dev;
    156       1.1  augustss 	int si;
    157       1.1  augustss 	char *buf;
    158       1.1  augustss {
    159       1.1  augustss 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
    160       1.1  augustss 	usb_string_descriptor_t us;
    161       1.1  augustss 	char *s;
    162       1.1  augustss 	int i, n;
    163       1.1  augustss 	u_int16_t c;
    164       1.1  augustss 	usbd_status r;
    165       1.1  augustss 
    166       1.1  augustss 	if (si == 0)
    167      1.11  augustss 		return (0);
    168      1.11  augustss 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
    169      1.11  augustss 		return (0);
    170      1.22  augustss 	if (dev->langid == USBD_NOLANG) {
    171      1.22  augustss 		/* Set up default language */
    172      1.22  augustss 		r = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us);
    173      1.22  augustss 		if (r != USBD_NORMAL_COMPLETION || us.bLength < 4) {
    174      1.22  augustss 			dev->langid = 0; /* Well, just pick English then */
    175      1.22  augustss 		} else {
    176      1.22  augustss 			/* Pick the first language as the default. */
    177      1.22  augustss 			dev->langid = UGETW(us.bString[0]);
    178      1.22  augustss 		}
    179      1.22  augustss 	}
    180      1.22  augustss 	r = usbd_get_string_desc(dev, si, dev->langid, &us);
    181       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    182      1.13  augustss 		return (0);
    183       1.1  augustss 	s = buf;
    184       1.1  augustss 	n = us.bLength / 2 - 1;
    185       1.1  augustss 	for (i = 0; i < n; i++) {
    186       1.1  augustss 		c = UGETW(us.bString[i]);
    187       1.1  augustss 		/* Convert from Unicode, handle buggy strings. */
    188       1.1  augustss 		if ((c & 0xff00) == 0)
    189       1.1  augustss 			*s++ = c;
    190       1.1  augustss 		else if ((c & 0x00ff) == 0 && swap)
    191       1.1  augustss 			*s++ = c >> 8;
    192       1.1  augustss 		else
    193       1.1  augustss 			*s++ = '?';
    194       1.1  augustss 	}
    195       1.1  augustss 	*s++ = 0;
    196       1.1  augustss 	return buf;
    197       1.1  augustss }
    198       1.1  augustss 
    199       1.1  augustss void
    200       1.1  augustss usbd_devinfo_vp(dev, v, p)
    201       1.1  augustss 	usbd_device_handle dev;
    202       1.1  augustss 	char *v, *p;
    203       1.1  augustss {
    204       1.1  augustss 	usb_device_descriptor_t *udd = &dev->ddesc;
    205       1.1  augustss 	char *vendor = 0, *product = 0;
    206       1.2        is #ifdef USBVERBOSE
    207       1.1  augustss 	struct usb_knowndev *kdp;
    208       1.2        is #endif
    209       1.1  augustss 
    210       1.1  augustss 	vendor = usbd_get_string(dev, udd->iManufacturer, v);
    211       1.1  augustss 	product = usbd_get_string(dev, udd->iProduct, p);
    212       1.1  augustss #ifdef USBVERBOSE
    213       1.1  augustss 	if (!vendor) {
    214       1.1  augustss 		for(kdp = usb_knowndevs;
    215       1.1  augustss 		    kdp->vendorname != NULL;
    216       1.1  augustss 		    kdp++) {
    217       1.1  augustss 			if (kdp->vendor == UGETW(udd->idVendor) &&
    218       1.1  augustss 			    (kdp->product == UGETW(udd->idProduct) ||
    219       1.1  augustss 			     (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
    220       1.1  augustss 				break;
    221       1.1  augustss 		}
    222       1.1  augustss 		if (kdp->vendorname == NULL)
    223       1.1  augustss 			vendor = product = NULL;
    224       1.1  augustss 		else {
    225       1.1  augustss 			vendor = kdp->vendorname;
    226       1.1  augustss 			product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
    227       1.1  augustss 				kdp->productname : NULL;
    228       1.1  augustss 		}
    229       1.1  augustss 	}
    230       1.1  augustss #endif
    231       1.1  augustss 	if (vendor)
    232       1.1  augustss 		strcpy(v, vendor);
    233       1.1  augustss 	else
    234       1.1  augustss 		sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
    235       1.1  augustss 	if (product)
    236       1.1  augustss 		strcpy(p, product);
    237       1.1  augustss 	else
    238       1.1  augustss 		sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
    239       1.1  augustss }
    240       1.1  augustss 
    241       1.1  augustss int
    242       1.1  augustss usbd_printBCD(cp, bcd)
    243       1.1  augustss 	char *cp;
    244       1.1  augustss 	int bcd;
    245       1.1  augustss {
    246       1.1  augustss 	return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
    247       1.1  augustss }
    248       1.1  augustss 
    249       1.1  augustss void
    250       1.1  augustss usbd_devinfo(dev, showclass, cp)
    251       1.1  augustss 	usbd_device_handle dev;
    252       1.1  augustss 	int showclass;
    253       1.1  augustss 	char *cp;
    254       1.1  augustss {
    255       1.1  augustss 	usb_device_descriptor_t *udd = &dev->ddesc;
    256       1.1  augustss 	char vendor[USB_MAX_STRING_LEN];
    257       1.1  augustss 	char product[USB_MAX_STRING_LEN];
    258       1.1  augustss 	int bcdDevice, bcdUSB;
    259       1.1  augustss 
    260       1.1  augustss 	usbd_devinfo_vp(dev, vendor, product);
    261      1.11  augustss 	cp += sprintf(cp, "%s %s", vendor, product);
    262       1.1  augustss 	if (showclass)
    263      1.12  augustss 		cp += sprintf(cp, ", class %d/%d",
    264       1.1  augustss 			      udd->bDeviceClass, udd->bDeviceSubClass);
    265       1.1  augustss 	bcdUSB = UGETW(udd->bcdUSB);
    266       1.1  augustss 	bcdDevice = UGETW(udd->bcdDevice);
    267      1.12  augustss 	cp += sprintf(cp, ", rev ");
    268       1.1  augustss 	cp += usbd_printBCD(cp, bcdUSB);
    269       1.1  augustss 	*cp++ = '/';
    270       1.1  augustss 	cp += usbd_printBCD(cp, bcdDevice);
    271      1.11  augustss 	cp += sprintf(cp, ", addr %d", dev->address);
    272      1.10  augustss 	*cp = 0;
    273       1.1  augustss }
    274       1.1  augustss 
    275       1.1  augustss /* Delay for a certain number of ms */
    276       1.1  augustss void
    277      1.23  augustss usb_delay_ms(bus, ms)
    278       1.5  augustss 	usbd_bus_handle bus;
    279      1.23  augustss 	u_int ms;
    280       1.1  augustss {
    281       1.1  augustss 	/* Wait at least two clock ticks so we know the time has passed. */
    282       1.5  augustss 	if (bus->use_polling)
    283       1.1  augustss 		delay((ms+1) * 1000);
    284       1.1  augustss 	else
    285       1.1  augustss 		tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
    286       1.1  augustss }
    287       1.1  augustss 
    288      1.23  augustss /* Delay given a device handle. */
    289      1.23  augustss void
    290      1.23  augustss usbd_delay_ms(dev, ms)
    291      1.23  augustss 	usbd_device_handle dev;
    292      1.23  augustss 	u_int ms;
    293      1.23  augustss {
    294      1.23  augustss 	usb_delay_ms(dev->bus, ms);
    295      1.23  augustss }
    296      1.23  augustss 
    297       1.1  augustss usbd_status
    298       1.1  augustss usbd_reset_port(dev, port, ps)
    299       1.1  augustss 	usbd_device_handle dev;
    300       1.1  augustss 	int port;
    301       1.1  augustss 	usb_port_status_t *ps;
    302       1.1  augustss {
    303       1.1  augustss 	usb_device_request_t req;
    304       1.1  augustss 	usbd_status r;
    305       1.1  augustss 	int n;
    306       1.1  augustss 
    307       1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    308       1.1  augustss 	req.bRequest = UR_SET_FEATURE;
    309       1.1  augustss 	USETW(req.wValue, UHF_PORT_RESET);
    310       1.1  augustss 	USETW(req.wIndex, port);
    311       1.1  augustss 	USETW(req.wLength, 0);
    312       1.1  augustss 	r = usbd_do_request(dev, &req, 0);
    313      1.12  augustss 	DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%d(%s)\n",
    314      1.12  augustss 		    port, r, usbd_error_strs[r]));
    315       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    316       1.1  augustss 		return (r);
    317       1.1  augustss 	n = 10;
    318       1.1  augustss 	do {
    319       1.1  augustss 		/* Wait for device to recover from reset. */
    320      1.23  augustss 		usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
    321       1.1  augustss 		r = usbd_get_port_status(dev, port, ps);
    322       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    323       1.1  augustss 			DPRINTF(("usbd_reset_port: get status failed %d\n",r));
    324       1.1  augustss 			return (r);
    325       1.1  augustss 		}
    326       1.1  augustss 	} while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
    327       1.1  augustss 	if (n == 0) {
    328       1.1  augustss 		printf("usbd_reset_port: timeout\n");
    329       1.1  augustss 		return (USBD_IOERROR);
    330       1.1  augustss 	}
    331       1.1  augustss 	r = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    332       1.1  augustss #ifdef USB_DEBUG
    333       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    334       1.1  augustss 		DPRINTF(("usbd_reset_port: clear port feature failed %d\n",r));
    335       1.1  augustss #endif
    336      1.18  augustss 
    337      1.18  augustss 	/* Wait for the device to recover from reset. */
    338      1.23  augustss 	usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
    339       1.1  augustss 	return (r);
    340       1.1  augustss }
    341       1.1  augustss 
    342      1.12  augustss usb_interface_descriptor_t *
    343      1.12  augustss usbd_find_idesc(cd, ifaceidx, altidx)
    344      1.12  augustss 	usb_config_descriptor_t *cd;
    345      1.12  augustss 	int ifaceidx;
    346      1.12  augustss 	int altidx;
    347      1.12  augustss {
    348      1.12  augustss 	char *p = (char *)cd;
    349      1.12  augustss 	char *end = p + UGETW(cd->wTotalLength);
    350      1.12  augustss 	usb_interface_descriptor_t *d;
    351      1.19  augustss 	int curidx, lastidx, curaidx = 0;
    352      1.12  augustss 
    353      1.19  augustss 	for (curidx = lastidx = -1; p < end; ) {
    354      1.12  augustss 		d = (usb_interface_descriptor_t *)p;
    355      1.18  augustss 		DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
    356      1.18  augustss 			    "type=%d\n",
    357      1.18  augustss 			    ifaceidx, curidx, altidx, curaidx,
    358      1.18  augustss 			    d->bLength, d->bDescriptorType));
    359      1.12  augustss 		if (d->bLength == 0) /* bad descriptor */
    360      1.12  augustss 			break;
    361      1.12  augustss 		p += d->bLength;
    362      1.12  augustss 		if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
    363      1.19  augustss 			if (d->bInterfaceNumber != lastidx) {
    364      1.19  augustss 				lastidx = d->bInterfaceNumber;
    365      1.12  augustss 				curidx++;
    366      1.12  augustss 				curaidx = 0;
    367      1.12  augustss 			} else
    368      1.12  augustss 				curaidx++;
    369      1.12  augustss 			if (ifaceidx == curidx && altidx == curaidx)
    370      1.12  augustss 				return (d);
    371      1.12  augustss 		}
    372      1.12  augustss 	}
    373      1.12  augustss 	return (0);
    374      1.12  augustss }
    375      1.12  augustss 
    376      1.12  augustss usb_endpoint_descriptor_t *
    377      1.12  augustss usbd_find_edesc(cd, ifaceidx, altidx, endptidx)
    378      1.12  augustss 	usb_config_descriptor_t *cd;
    379      1.12  augustss 	int ifaceidx;
    380      1.12  augustss 	int altidx;
    381      1.12  augustss 	int endptidx;
    382      1.12  augustss {
    383      1.12  augustss 	char *p = (char *)cd;
    384      1.12  augustss 	char *end = p + UGETW(cd->wTotalLength);
    385      1.12  augustss 	usb_interface_descriptor_t *d;
    386      1.12  augustss 	usb_endpoint_descriptor_t *e;
    387      1.12  augustss 	int curidx;
    388      1.12  augustss 
    389      1.12  augustss 	d = usbd_find_idesc(cd, ifaceidx, altidx);
    390      1.12  augustss 	if (!d)
    391      1.12  augustss 		return (0);
    392      1.12  augustss 	if (endptidx >= d->bNumEndpoints) /* quick exit */
    393      1.12  augustss 		return (0);
    394      1.12  augustss 
    395      1.12  augustss 	curidx = -1;
    396      1.12  augustss 	for (p = (char *)d + d->bLength; p < end; ) {
    397      1.12  augustss 		e = (usb_endpoint_descriptor_t *)p;
    398      1.12  augustss 		if (e->bLength == 0) /* bad descriptor */
    399      1.12  augustss 			break;
    400      1.12  augustss 		p += e->bLength;
    401      1.12  augustss 		if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
    402      1.12  augustss 			return (0);
    403      1.12  augustss 		if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
    404      1.12  augustss 			curidx++;
    405      1.12  augustss 			if (curidx == endptidx)
    406      1.12  augustss 				return (e);
    407      1.12  augustss 		}
    408       1.1  augustss 	}
    409       1.1  augustss 	return (0);
    410       1.1  augustss }
    411       1.1  augustss 
    412       1.1  augustss usbd_status
    413      1.13  augustss usbd_fill_iface_data(dev, ifaceidx, altidx)
    414       1.1  augustss 	usbd_device_handle dev;
    415      1.12  augustss 	int ifaceidx;
    416      1.13  augustss 	int altidx;
    417       1.1  augustss {
    418      1.12  augustss 	usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
    419       1.1  augustss 	char *p, *end;
    420       1.1  augustss 	int endpt, nendpt;
    421       1.1  augustss 
    422      1.18  augustss 	DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
    423      1.14  drochner 		    ifaceidx, altidx));
    424       1.1  augustss 	ifc->device = dev;
    425      1.13  augustss 	ifc->idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
    426      1.19  augustss 	if (ifc->idesc == 0)
    427      1.19  augustss 		return (USBD_INVAL);
    428      1.13  augustss 	ifc->index = ifaceidx;
    429      1.13  augustss 	ifc->altindex = altidx;
    430       1.1  augustss 	nendpt = ifc->idesc->bNumEndpoints;
    431       1.1  augustss 	DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
    432       1.1  augustss 	if (nendpt != 0) {
    433       1.1  augustss 		ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
    434       1.1  augustss 					M_USB, M_NOWAIT);
    435       1.1  augustss 		if (ifc->endpoints == 0)
    436       1.1  augustss 			return (USBD_NOMEM);
    437       1.1  augustss 	} else
    438       1.1  augustss 		ifc->endpoints = 0;
    439       1.1  augustss 	ifc->priv = 0;
    440       1.1  augustss 	p = (char *)ifc->idesc + ifc->idesc->bLength;
    441       1.1  augustss 	end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
    442      1.19  augustss #define ed ((usb_endpoint_descriptor_t *)p)
    443       1.1  augustss 	for (endpt = 0; endpt < nendpt; endpt++) {
    444       1.1  augustss 		DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
    445       1.1  augustss 		for (; p < end; p += ed->bLength) {
    446       1.1  augustss 			ed = (usb_endpoint_descriptor_t *)p;
    447      1.14  drochner 			DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
    448      1.14  drochner 				     "len=%d type=%d\n",
    449       1.1  augustss 				 p, end, ed->bLength, ed->bDescriptorType));
    450      1.24  augustss 			if (p + ed->bLength <= end && ed->bLength != 0 &&
    451       1.1  augustss 			    ed->bDescriptorType == UDESC_ENDPOINT)
    452      1.24  augustss 				goto found;
    453      1.24  augustss 			if (ed->bDescriptorType == UDESC_INTERFACE ||
    454      1.24  augustss 			    ed->bLength == 0)
    455       1.1  augustss 				break;
    456       1.1  augustss 		}
    457      1.24  augustss 		/* passed end, or bad desc */
    458      1.24  augustss 		goto bad;
    459      1.24  augustss 	found:
    460       1.1  augustss 		ifc->endpoints[endpt].edesc = ed;
    461       1.1  augustss 		ifc->endpoints[endpt].refcnt = 0;
    462       1.1  augustss 		ifc->endpoints[endpt].toggle = 0;
    463      1.24  augustss 		p += ed->bLength;
    464       1.1  augustss 	}
    465      1.19  augustss #undef ed
    466       1.1  augustss 	LIST_INIT(&ifc->pipes);
    467       1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    468      1.24  augustss 
    469       1.1  augustss  bad:
    470       1.1  augustss 	free(ifc->endpoints, M_USB);
    471      1.24  augustss 	return (USBD_INVAL);
    472       1.1  augustss }
    473       1.1  augustss 
    474       1.1  augustss void
    475       1.1  augustss usbd_free_iface_data(dev, ifcno)
    476       1.1  augustss 	usbd_device_handle dev;
    477       1.1  augustss 	int ifcno;
    478       1.1  augustss {
    479       1.1  augustss 	usbd_interface_handle ifc = &dev->ifaces[ifcno];
    480       1.1  augustss 	if (ifc->endpoints)
    481       1.1  augustss 		free(ifc->endpoints, M_USB);
    482       1.1  augustss }
    483       1.1  augustss 
    484       1.7  augustss static usbd_status
    485       1.7  augustss usbd_set_config(dev, conf)
    486       1.7  augustss 	usbd_device_handle dev;
    487       1.7  augustss 	int conf;
    488       1.7  augustss {
    489       1.7  augustss 	usb_device_request_t req;
    490       1.7  augustss 
    491       1.7  augustss 	req.bmRequestType = UT_WRITE_DEVICE;
    492       1.7  augustss 	req.bRequest = UR_SET_CONFIG;
    493       1.7  augustss 	USETW(req.wValue, conf);
    494       1.7  augustss 	USETW(req.wIndex, 0);
    495       1.7  augustss 	USETW(req.wLength, 0);
    496       1.7  augustss 	return (usbd_do_request(dev, &req, 0));
    497       1.7  augustss }
    498       1.7  augustss 
    499       1.1  augustss usbd_status
    500       1.7  augustss usbd_set_config_no(dev, no, msg)
    501       1.1  augustss 	usbd_device_handle dev;
    502       1.1  augustss 	int no;
    503       1.7  augustss 	int msg;
    504       1.1  augustss {
    505      1.12  augustss 	int index;
    506      1.12  augustss 	usb_config_descriptor_t cd;
    507      1.12  augustss 	usbd_status r;
    508      1.12  augustss 
    509      1.12  augustss 	DPRINTFN(5,("usbd_set_config_no: %d\n", no));
    510      1.12  augustss 	/* Figure out what config index to use. */
    511      1.12  augustss 	for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
    512      1.12  augustss 		r = usbd_get_config_desc(dev, index, &cd);
    513      1.12  augustss 		if (r != USBD_NORMAL_COMPLETION)
    514      1.12  augustss 			return (r);
    515      1.12  augustss 		if (cd.bConfigurationValue == no)
    516      1.12  augustss 			return (usbd_set_config_index(dev, index, msg));
    517      1.12  augustss 	}
    518      1.12  augustss 	return (USBD_INVAL);
    519      1.12  augustss }
    520      1.12  augustss 
    521      1.12  augustss usbd_status
    522      1.12  augustss usbd_set_config_index(dev, index, msg)
    523      1.12  augustss 	usbd_device_handle dev;
    524      1.12  augustss 	int index;
    525      1.12  augustss 	int msg;
    526      1.12  augustss {
    527       1.1  augustss 	usb_status_t ds;
    528       1.1  augustss 	usb_config_descriptor_t cd, *cdp;
    529       1.1  augustss 	usbd_status r;
    530      1.12  augustss 	int ifcidx, nifc, len, selfpowered, power;
    531       1.1  augustss 
    532      1.12  augustss 	DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
    533       1.1  augustss 
    534       1.1  augustss 	/* XXX check that all interfaces are idle */
    535       1.1  augustss 	if (dev->config != 0) {
    536      1.12  augustss 		DPRINTF(("usbd_set_config_index: free old config\n"));
    537       1.1  augustss 		/* Free all configuration data structures. */
    538       1.1  augustss 		nifc = dev->cdesc->bNumInterface;
    539      1.12  augustss 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
    540      1.12  augustss 			usbd_free_iface_data(dev, ifcidx);
    541       1.1  augustss 		free(dev->ifaces, M_USB);
    542       1.1  augustss 		free(dev->cdesc, M_USB);
    543       1.1  augustss 		dev->ifaces = 0;
    544       1.1  augustss 		dev->cdesc = 0;
    545       1.1  augustss 		dev->config = 0;
    546       1.1  augustss 	}
    547       1.1  augustss 
    548       1.1  augustss 	/* Figure out what config number to use. */
    549      1.12  augustss 	r = usbd_get_config_desc(dev, index, &cd);
    550       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    551       1.1  augustss 		return (r);
    552       1.1  augustss 	len = UGETW(cd.wTotalLength);
    553       1.1  augustss 	cdp = malloc(len, M_USB, M_NOWAIT);
    554       1.1  augustss 	if (cdp == 0)
    555       1.1  augustss 		return (USBD_NOMEM);
    556      1.12  augustss 	r = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
    557       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    558       1.1  augustss 		goto bad;
    559      1.18  augustss 	if (cdp->bDescriptorType != UDESC_CONFIG) {
    560      1.18  augustss 		DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
    561      1.18  augustss 			     cdp->bDescriptorType));
    562      1.18  augustss 		r = USBD_INVAL;
    563      1.18  augustss 		goto bad;
    564      1.18  augustss 	}
    565       1.1  augustss 	selfpowered = 0;
    566       1.1  augustss 	if (cdp->bmAttributes & UC_SELF_POWERED) {
    567       1.1  augustss 		/* May be self powered. */
    568       1.1  augustss 		if (cdp->bmAttributes & UC_BUS_POWERED) {
    569       1.1  augustss 			/* Must ask device. */
    570  1.29.4.1   thorpej 			r = usbd_get_device_status(dev, &ds);
    571  1.29.4.1   thorpej 			if (r == USBD_NORMAL_COMPLETION &&
    572  1.29.4.1   thorpej 			    (UGETW(ds.wStatus) & UDS_SELF_POWERED))
    573  1.29.4.1   thorpej 				selfpowered = 1;
    574      1.14  drochner 			DPRINTF(("usbd_set_config_index: status=0x%04x, "
    575      1.14  drochner 				 "error=%d(%s)\n",
    576      1.12  augustss 				 UGETW(ds.wStatus), r, usbd_error_strs[r]));
    577       1.1  augustss 		} else
    578       1.1  augustss 			selfpowered = 1;
    579       1.1  augustss 	}
    580      1.14  drochner 	DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, "
    581  1.29.4.1   thorpej 		 "selfpowered=%d, power=%d\n",
    582       1.1  augustss 		 dev->address, cdp->bmAttributes,
    583  1.29.4.1   thorpej 		 selfpowered, cdp->bMaxPower * 2));
    584       1.1  augustss #ifdef USB_DEBUG
    585       1.1  augustss 	if (!dev->powersrc) {
    586      1.12  augustss 		printf("usbd_set_config_index: No power source?\n");
    587      1.13  augustss 		return (USBD_IOERROR);
    588       1.1  augustss 	}
    589       1.1  augustss #endif
    590       1.1  augustss 	power = cdp->bMaxPower * 2;
    591       1.1  augustss 	if (power > dev->powersrc->power) {
    592       1.1  augustss 		/* XXX print nicer message. */
    593       1.7  augustss 		if (msg)
    594      1.18  augustss 			printf("%s: device addr %d (config %d) exceeds power "
    595      1.18  augustss 				 "budget, %d mA > %d mA\n",
    596      1.18  augustss 			       USBDEVNAME(dev->bus->bdev), dev->address,
    597       1.7  augustss 			       cdp->bConfigurationValue,
    598       1.7  augustss 			       power, dev->powersrc->power);
    599       1.1  augustss 		r = USBD_NO_POWER;
    600       1.1  augustss 		goto bad;
    601       1.1  augustss 	}
    602       1.1  augustss 	dev->power = power;
    603       1.1  augustss 	dev->self_powered = selfpowered;
    604       1.1  augustss 
    605      1.18  augustss 	DPRINTF(("usbd_set_config_index: set config %d\n",
    606      1.18  augustss 		 cdp->bConfigurationValue));
    607       1.1  augustss 	r = usbd_set_config(dev, cdp->bConfigurationValue);
    608       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    609      1.14  drochner 		DPRINTF(("usbd_set_config_index: setting config=%d failed, "
    610      1.14  drochner 			 "error=%d(%s)\n",
    611      1.12  augustss 			 cdp->bConfigurationValue, r, usbd_error_strs[r]));
    612       1.1  augustss 		goto bad;
    613       1.1  augustss 	}
    614      1.12  augustss 	DPRINTF(("usbd_set_config_index: setting new config %d\n",
    615       1.1  augustss 		 cdp->bConfigurationValue));
    616       1.1  augustss 	nifc = cdp->bNumInterface;
    617       1.1  augustss 	dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
    618       1.1  augustss 			     M_USB, M_NOWAIT);
    619       1.1  augustss 	if (dev->ifaces == 0) {
    620       1.1  augustss 		r = USBD_NOMEM;
    621       1.1  augustss 		goto bad;
    622       1.1  augustss 	}
    623      1.12  augustss 	DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
    624       1.1  augustss 	dev->cdesc = cdp;
    625       1.1  augustss 	dev->config = cdp->bConfigurationValue;
    626      1.12  augustss 	for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
    627      1.12  augustss 		r = usbd_fill_iface_data(dev, ifcidx, 0);
    628       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    629      1.12  augustss 			while (--ifcidx >= 0)
    630      1.12  augustss 				usbd_free_iface_data(dev, ifcidx);
    631       1.1  augustss 			goto bad;
    632       1.1  augustss 		}
    633       1.1  augustss 	}
    634       1.1  augustss 
    635       1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    636       1.1  augustss 
    637       1.1  augustss  bad:
    638       1.1  augustss 	free(cdp, M_USB);
    639       1.1  augustss 	return (r);
    640       1.1  augustss }
    641       1.1  augustss 
    642       1.1  augustss /* XXX add function for alternate settings */
    643       1.1  augustss 
    644       1.1  augustss usbd_status
    645       1.1  augustss usbd_setup_pipe(dev, iface, ep, pipe)
    646       1.1  augustss 	usbd_device_handle dev;
    647       1.1  augustss 	usbd_interface_handle iface;
    648       1.1  augustss 	struct usbd_endpoint *ep;
    649       1.1  augustss 	usbd_pipe_handle *pipe;
    650       1.1  augustss {
    651       1.1  augustss 	usbd_pipe_handle p;
    652       1.1  augustss 	usbd_status r;
    653       1.1  augustss 
    654       1.1  augustss 	DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
    655       1.1  augustss 		    dev, iface, ep, pipe));
    656       1.1  augustss 	p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
    657       1.1  augustss 	if (p == 0)
    658       1.1  augustss 		return (USBD_NOMEM);
    659       1.1  augustss 	p->device = dev;
    660       1.1  augustss 	p->iface = iface;
    661       1.1  augustss 	p->endpoint = ep;
    662       1.1  augustss 	ep->refcnt++;
    663       1.1  augustss 	p->refcnt = 1;
    664       1.1  augustss 	p->intrreqh = 0;
    665       1.1  augustss 	p->running = 0;
    666      1.29  augustss 	p->disco = 0;
    667      1.29  augustss 	p->discoarg = 0;
    668       1.1  augustss 	SIMPLEQ_INIT(&p->queue);
    669       1.1  augustss 	r = dev->bus->open_pipe(p);
    670       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    671      1.19  augustss 		DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error=%d"
    672      1.19  augustss 			 "(%s)\n",
    673      1.12  augustss 			 ep->edesc->bEndpointAddress, r, usbd_error_strs[r]));
    674       1.1  augustss 		free(p, M_USB);
    675       1.1  augustss 		return (r);
    676       1.1  augustss 	}
    677       1.1  augustss 	*pipe = p;
    678       1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    679       1.1  augustss }
    680       1.1  augustss 
    681       1.1  augustss /* Abort the device control pipe. */
    682       1.1  augustss void
    683       1.1  augustss usbd_kill_pipe(pipe)
    684       1.1  augustss 	usbd_pipe_handle pipe;
    685       1.1  augustss {
    686       1.1  augustss 	pipe->methods->close(pipe);
    687       1.1  augustss 	pipe->endpoint->refcnt--;
    688       1.1  augustss 	free(pipe, M_USB);
    689       1.1  augustss }
    690       1.1  augustss 
    691       1.1  augustss int
    692       1.1  augustss usbd_getnewaddr(bus)
    693       1.1  augustss 	usbd_bus_handle bus;
    694       1.1  augustss {
    695      1.18  augustss 	int addr;
    696       1.1  augustss 
    697      1.18  augustss 	for (addr = 1; addr < USB_MAX_DEVICES; addr++)
    698      1.18  augustss 		if (bus->devices[addr] == 0)
    699      1.18  augustss 			return (addr);
    700       1.1  augustss 	return (-1);
    701       1.1  augustss }
    702       1.1  augustss 
    703      1.18  augustss 
    704      1.18  augustss usbd_status
    705      1.18  augustss usbd_probe_and_attach(parent, dev, port, addr)
    706      1.18  augustss 	bdevice *parent;
    707      1.18  augustss 	usbd_device_handle dev;
    708      1.18  augustss 	int port;
    709      1.18  augustss 	int addr;
    710      1.18  augustss {
    711      1.18  augustss 	struct usb_attach_arg uaa;
    712      1.18  augustss 	usb_device_descriptor_t *dd = &dev->ddesc;
    713      1.20  augustss 	int r, found, i, confi, nifaces;
    714      1.20  augustss 	usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
    715      1.18  augustss 
    716      1.18  augustss #if defined(__FreeBSD__)
    717      1.28  augustss 	/*
    718      1.28  augustss 	 * XXX uaa is a static var. Not a problem as it _should_ be used only
    719      1.28  augustss 	 * during probe and attach. Should be changed however.
    720      1.28  augustss 	 */
    721      1.18  augustss 	bdevice bdev;
    722      1.18  augustss 	bdev = device_add_child(*parent, NULL, -1, &uaa);
    723      1.26  augustss 	if (!bdev) {
    724      1.18  augustss 	    printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
    725      1.18  augustss 	    return (USBD_INVAL);
    726      1.18  augustss 	}
    727      1.18  augustss #endif
    728      1.18  augustss 
    729      1.18  augustss 	uaa.device = dev;
    730      1.18  augustss 	uaa.iface = 0;
    731      1.20  augustss 	uaa.ifaces = 0;
    732      1.20  augustss 	uaa.nifaces = 0;
    733      1.18  augustss 	uaa.usegeneric = 0;
    734      1.18  augustss 	uaa.port = port;
    735      1.18  augustss 	uaa.configno = UHUB_UNK_CONFIGURATION;
    736      1.18  augustss 	uaa.ifaceno = UHUB_UNK_INTERFACE;
    737  1.29.4.1   thorpej 	uaa.vendor = UGETW(dd->idVendor);
    738  1.29.4.1   thorpej 	uaa.product = UGETW(dd->idProduct);
    739  1.29.4.1   thorpej 	uaa.release = UGETW(dd->bcdDevice);
    740      1.18  augustss 
    741      1.18  augustss 	/* First try with device specific drivers. */
    742      1.18  augustss 	if (USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch))
    743      1.18  augustss 		return (USBD_NORMAL_COMPLETION);
    744      1.18  augustss 
    745      1.18  augustss 	DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
    746      1.18  augustss 
    747      1.18  augustss 	/* Next try with interface drivers. */
    748      1.18  augustss 	for (confi = 0; confi < dd->bNumConfigurations; confi++) {
    749      1.18  augustss 		DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
    750      1.18  augustss 			    confi));
    751      1.18  augustss 		r = usbd_set_config_index(dev, confi, 1);
    752      1.18  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    753      1.18  augustss #ifdef USB_DEBUG
    754      1.18  augustss 			DPRINTF(("%s: port %d, set config at addr %d failed, "
    755      1.18  augustss 				 "error=%d(%s)\n", USBDEVNAME(*parent), port,
    756      1.18  augustss 				 addr, r, usbd_error_strs[r]));
    757      1.18  augustss #else
    758      1.18  augustss 			printf("%s: port %d, set config at addr %d failed\n",
    759      1.18  augustss 			       USBDEVNAME(*parent), port, addr);
    760      1.18  augustss #endif
    761      1.27  augustss #if defined(__FreeBSD__)
    762      1.27  augustss 			device_delete_child(*parent, bdev);
    763      1.27  augustss #endif
    764      1.27  augustss  			return (r);
    765      1.18  augustss 		}
    766      1.20  augustss 		nifaces = dev->cdesc->bNumInterface;
    767      1.23  augustss 		uaa.configno = dev->cdesc->bConfigurationValue;
    768      1.20  augustss 		for (i = 0; i < nifaces; i++)
    769      1.20  augustss 			ifaces[i] = &dev->ifaces[i];
    770      1.20  augustss 		uaa.ifaces = ifaces;
    771      1.20  augustss 		uaa.nifaces = nifaces;
    772      1.20  augustss 		for (found = i = 0; i < nifaces; i++) {
    773      1.20  augustss 			if (!ifaces[i])
    774      1.20  augustss 				continue; /* interface already claimed */
    775      1.20  augustss 			uaa.iface = ifaces[i];
    776      1.20  augustss 			uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
    777      1.18  augustss 			if (USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
    778      1.20  augustss 					  usbd_submatch)) {
    779      1.18  augustss 				found++;
    780      1.20  augustss 				ifaces[i] = 0; /* consumed */
    781      1.20  augustss 			}
    782      1.18  augustss 		}
    783      1.18  augustss 		if (found != 0)
    784      1.18  augustss 			return (USBD_NORMAL_COMPLETION);
    785      1.18  augustss 	}
    786      1.21  augustss 	/* No interfaces were attached in any of the configurations. */
    787      1.21  augustss 	if (dd->bNumConfigurations > 1)/* don't change if only 1 config */
    788      1.18  augustss 		usbd_set_config_index(dev, 0, 0);
    789      1.18  augustss 
    790      1.18  augustss 	DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
    791      1.18  augustss 
    792      1.18  augustss 	/* Finally try the generic driver. */
    793      1.18  augustss 	uaa.iface = 0;
    794      1.18  augustss 	uaa.usegeneric = 1;
    795      1.18  augustss 	uaa.configno = UHUB_UNK_CONFIGURATION;
    796      1.18  augustss 	uaa.ifaceno = UHUB_UNK_INTERFACE;
    797  1.29.4.1   thorpej 	uaa.vendor = UHUB_UNK_VENDOR;
    798  1.29.4.1   thorpej 	uaa.product = UHUB_UNK_PRODUCT;
    799  1.29.4.1   thorpej 	uaa.release = UHUB_UNK_RELEASE;
    800      1.18  augustss 	if (USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch))
    801      1.18  augustss 		return (USBD_NORMAL_COMPLETION);
    802      1.18  augustss 
    803      1.18  augustss 	/*
    804      1.18  augustss 	 * The generic attach failed, but leave the device as it is.
    805      1.18  augustss 	 * We just did not find any drivers, that's all.  The device is
    806      1.18  augustss 	 * fully operational and not harming anyone.
    807      1.18  augustss 	 */
    808      1.18  augustss 	DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
    809      1.27  augustss #if defined(__FreeBSD__)
    810      1.27  augustss /*
    811      1.27  augustss  * XXX should we delete the child again? Left for now to avoid dangling
    812      1.27  augustss  * references.
    813      1.27  augustss       device_delete_child(*parent, bdev);
    814      1.27  augustss */
    815      1.27  augustss #endif
    816      1.27  augustss  	return (USBD_NORMAL_COMPLETION);
    817      1.18  augustss }
    818      1.18  augustss 
    819      1.18  augustss 
    820      1.18  augustss 
    821       1.1  augustss /*
    822       1.1  augustss  * Called when a new device has been put in the powered state,
    823       1.1  augustss  * but not yet in the addressed state.
    824       1.1  augustss  * Get initial descriptor, set the address, get full descriptor,
    825       1.1  augustss  * and attach a driver.
    826       1.1  augustss  */
    827       1.1  augustss usbd_status
    828       1.1  augustss usbd_new_device(parent, bus, depth, lowspeed, port, up)
    829      1.18  augustss 	bdevice *parent;
    830       1.1  augustss 	usbd_bus_handle bus;
    831       1.1  augustss 	int depth;
    832       1.1  augustss 	int lowspeed;
    833       1.1  augustss 	int port;
    834       1.1  augustss 	struct usbd_port *up;
    835       1.1  augustss {
    836       1.1  augustss 	usbd_device_handle dev;
    837      1.18  augustss 	usb_device_descriptor_t *dd;
    838       1.1  augustss 	usbd_status r;
    839       1.1  augustss 	int addr;
    840      1.18  augustss 	int i;
    841       1.1  augustss 
    842       1.1  augustss 	DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
    843       1.1  augustss 		 bus, depth, lowspeed));
    844       1.1  augustss 	addr = usbd_getnewaddr(bus);
    845       1.1  augustss 	if (addr < 0) {
    846      1.18  augustss 		printf("%s: No free USB addresses, new device ignored.\n",
    847      1.18  augustss 		       USBDEVNAME(bus->bdev));
    848       1.1  augustss 		return (USBD_NO_ADDR);
    849       1.1  augustss 	}
    850       1.1  augustss 
    851       1.1  augustss 	dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
    852       1.1  augustss 	if (dev == 0)
    853       1.1  augustss 		return (USBD_NOMEM);
    854       1.1  augustss 	memset(dev, 0, sizeof(*dev));
    855       1.1  augustss 
    856       1.1  augustss 	dev->bus = bus;
    857       1.1  augustss 
    858       1.1  augustss 	/* Set up default endpoint handle. */
    859       1.1  augustss 	dev->def_ep.edesc = &dev->def_ep_desc;
    860       1.1  augustss 
    861       1.1  augustss 	/* Set up default endpoint descriptor. */
    862       1.1  augustss 	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
    863       1.1  augustss 	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
    864       1.1  augustss 	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
    865       1.1  augustss 	dev->def_ep_desc.bmAttributes = UE_CONTROL;
    866       1.1  augustss 	USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
    867       1.1  augustss 	dev->def_ep_desc.bInterval = 0;
    868       1.1  augustss 
    869       1.1  augustss 	dev->quirks = &usbd_no_quirk;
    870       1.1  augustss 	dev->address = USB_START_ADDR;
    871       1.4  augustss 	dev->ddesc.bMaxPacketSize = 0;
    872       1.1  augustss 	dev->lowspeed = lowspeed != 0;
    873       1.1  augustss 	dev->depth = depth;
    874       1.1  augustss 	dev->powersrc = up;
    875      1.22  augustss 	dev->langid = USBD_NOLANG;
    876       1.1  augustss 
    877       1.1  augustss 	/* Establish the the default pipe. */
    878       1.1  augustss 	r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
    879      1.18  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    880      1.18  augustss 		usbd_remove_device(dev, up);
    881      1.19  augustss 		return (r);
    882      1.18  augustss 	}
    883       1.1  augustss 
    884       1.1  augustss 	up->device = dev;
    885      1.18  augustss 	dd = &dev->ddesc;
    886       1.6  augustss 	/* Try a few times in case the device is slow (i.e. outside specs.) */
    887       1.6  augustss 	for (i = 0; i < 5; i++) {
    888       1.6  augustss 		/* Get the first 8 bytes of the device descriptor. */
    889      1.18  augustss 		r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
    890       1.6  augustss 		if (r == USBD_NORMAL_COMPLETION)
    891       1.6  augustss 			break;
    892      1.23  augustss 		usbd_delay_ms(dev, 200);
    893       1.6  augustss 	}
    894       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    895      1.14  drochner 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
    896      1.14  drochner 			      "failed\n",
    897       1.6  augustss 			      addr));
    898      1.18  augustss 		usbd_remove_device(dev, up);
    899      1.18  augustss 		return (r);
    900      1.18  augustss 	}
    901      1.18  augustss 
    902      1.18  augustss 	if (dd->bDescriptorType != UDESC_DEVICE) {
    903      1.18  augustss 		/* Illegal device descriptor */
    904      1.18  augustss 		DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
    905      1.18  augustss 			     dd->bDescriptorType));
    906      1.18  augustss 		usbd_remove_device(dev, up);
    907      1.18  augustss 		return (USBD_INVAL);
    908       1.1  augustss 	}
    909       1.1  augustss 
    910      1.14  drochner 	DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
    911      1.14  drochner 		 "subclass=%d, protocol=%d, maxpacket=%d, ls=%d\n",
    912      1.18  augustss 		 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
    913      1.18  augustss 		 dd->bDeviceProtocol, dd->bMaxPacketSize, dev->lowspeed));
    914       1.4  augustss 
    915      1.18  augustss 	USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
    916       1.1  augustss 
    917       1.1  augustss 	/* Get the full device descriptor. */
    918      1.18  augustss 	r = usbd_get_device_desc(dev, dd);
    919       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    920      1.14  drochner 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
    921      1.14  drochner 			      "failed\n", addr));
    922      1.18  augustss 		usbd_remove_device(dev, up);
    923      1.18  augustss 		return (r);
    924       1.1  augustss 	}
    925       1.1  augustss 
    926       1.1  augustss 	/* Figure out what's wrong with this device. */
    927      1.18  augustss 	dev->quirks = usbd_find_quirk(dd);
    928       1.1  augustss 
    929       1.1  augustss 	/* Set the address */
    930       1.1  augustss 	r = usbd_set_address(dev, addr);
    931       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    932       1.1  augustss 		DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
    933       1.1  augustss 		r = USBD_SET_ADDR_FAILED;
    934      1.18  augustss 		usbd_remove_device(dev, up);
    935      1.18  augustss 		return (r);
    936       1.1  augustss 	}
    937      1.18  augustss 	/* Allow device time to set new address */
    938      1.23  augustss 	usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
    939      1.18  augustss 
    940       1.1  augustss 	dev->address = addr;	/* New device address now */
    941       1.1  augustss 	bus->devices[addr] = dev;
    942       1.1  augustss 
    943       1.1  augustss 	/* Assume 100mA bus powered for now. Changed when configured. */
    944       1.1  augustss 	dev->power = USB_MIN_POWER;
    945       1.1  augustss 	dev->self_powered = 0;
    946       1.1  augustss 
    947       1.1  augustss 	DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
    948       1.1  augustss 		 addr, dev, parent));
    949       1.1  augustss 
    950      1.18  augustss 	r = usbd_probe_and_attach(parent, dev, port, addr);
    951      1.18  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    952      1.18  augustss 		usbd_remove_device(dev, up);
    953      1.18  augustss 		return (r);
    954      1.18  augustss   	}
    955      1.18  augustss 
    956      1.18  augustss   	return (USBD_NORMAL_COMPLETION);
    957      1.18  augustss }
    958       1.1  augustss 
    959      1.18  augustss void
    960      1.18  augustss usbd_remove_device(dev, up)
    961      1.18  augustss 	usbd_device_handle dev;
    962      1.18  augustss 	struct usbd_port *up;
    963      1.18  augustss {
    964      1.18  augustss 	DPRINTF(("usbd_remove_device: %p\n", dev));
    965      1.18  augustss 
    966      1.18  augustss 	if (dev->default_pipe)
    967      1.18  augustss 		usbd_kill_pipe(dev->default_pipe);
    968      1.18  augustss 	up->device = 0;
    969      1.18  augustss 	dev->bus->devices[dev->address] = 0;
    970       1.1  augustss 
    971       1.1  augustss 	free(dev, M_USB);
    972       1.1  augustss }
    973       1.1  augustss 
    974      1.18  augustss #if defined(__NetBSD__)
    975       1.1  augustss int
    976       1.1  augustss usbd_print(aux, pnp)
    977       1.1  augustss 	void *aux;
    978       1.1  augustss 	const char *pnp;
    979       1.1  augustss {
    980       1.1  augustss 	struct usb_attach_arg *uaa = aux;
    981       1.1  augustss 	char devinfo[1024];
    982       1.1  augustss 
    983       1.1  augustss 	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
    984       1.1  augustss 	if (pnp) {
    985       1.1  augustss 		if (!uaa->usegeneric)
    986       1.1  augustss 			return (QUIET);
    987       1.1  augustss 		usbd_devinfo(uaa->device, 1, devinfo);
    988      1.11  augustss 		printf("%s, %s", devinfo, pnp);
    989       1.1  augustss 	}
    990       1.1  augustss 	if (uaa->port != 0)
    991      1.11  augustss 		printf(" port %d", uaa->port);
    992      1.11  augustss 	if (uaa->configno != UHUB_UNK_CONFIGURATION)
    993      1.11  augustss 		printf(" configuration %d", uaa->configno);
    994      1.11  augustss 	if (uaa->ifaceno != UHUB_UNK_INTERFACE)
    995      1.11  augustss 		printf(" interface %d", uaa->ifaceno);
    996  1.29.4.1   thorpej #if 0
    997  1.29.4.1   thorpej 	/*
    998  1.29.4.1   thorpej 	 * It gets very crowded with these locators on the attach line.
    999  1.29.4.1   thorpej 	 * They are not really needed since they are printed in the clear
   1000  1.29.4.1   thorpej 	 * by each driver.
   1001  1.29.4.1   thorpej 	 */
   1002  1.29.4.1   thorpej 	if (uaa->vendor != UHUB_UNK_VENDOR)
   1003  1.29.4.1   thorpej 		printf(" vendor 0x%04x", uaa->vendor);
   1004  1.29.4.1   thorpej 	if (uaa->product != UHUB_UNK_PRODUCT)
   1005  1.29.4.1   thorpej 		printf(" product 0x%04x", uaa->product);
   1006  1.29.4.1   thorpej 	if (uaa->release != UHUB_UNK_RELEASE)
   1007  1.29.4.1   thorpej 		printf(" release 0x%04x", uaa->release);
   1008  1.29.4.1   thorpej #endif
   1009       1.1  augustss 	return (UNCONF);
   1010       1.1  augustss }
   1011       1.1  augustss 
   1012       1.1  augustss int
   1013       1.1  augustss usbd_submatch(parent, cf, aux)
   1014       1.1  augustss 	struct device *parent;
   1015       1.1  augustss 	struct cfdata *cf;
   1016       1.1  augustss 	void *aux;
   1017       1.1  augustss {
   1018       1.1  augustss 	struct usb_attach_arg *uaa = aux;
   1019       1.1  augustss 
   1020      1.11  augustss 	if ((uaa->port != 0 &&
   1021      1.11  augustss 	     cf->uhubcf_port != UHUB_UNK_PORT &&
   1022      1.11  augustss 	     cf->uhubcf_port != uaa->port) ||
   1023      1.11  augustss 	    (uaa->configno != UHUB_UNK_CONFIGURATION &&
   1024      1.11  augustss 	     cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
   1025      1.11  augustss 	     cf->uhubcf_configuration != uaa->configno) ||
   1026      1.11  augustss 	    (uaa->ifaceno != UHUB_UNK_INTERFACE &&
   1027      1.11  augustss 	     cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
   1028  1.29.4.1   thorpej 	     cf->uhubcf_interface != uaa->ifaceno) ||
   1029  1.29.4.1   thorpej 	    (uaa->vendor != UHUB_UNK_VENDOR &&
   1030  1.29.4.1   thorpej 	     cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
   1031  1.29.4.1   thorpej 	     cf->uhubcf_vendor != uaa->vendor) ||
   1032  1.29.4.1   thorpej 	    (uaa->product != UHUB_UNK_PRODUCT &&
   1033  1.29.4.1   thorpej 	     cf->uhubcf_product != UHUB_UNK_PRODUCT &&
   1034  1.29.4.1   thorpej 	     cf->uhubcf_product != uaa->product) ||
   1035  1.29.4.1   thorpej 	    (uaa->release != UHUB_UNK_RELEASE &&
   1036  1.29.4.1   thorpej 	     cf->uhubcf_release != UHUB_UNK_RELEASE &&
   1037  1.29.4.1   thorpej 	     cf->uhubcf_release != uaa->release)
   1038  1.29.4.1   thorpej 	   )
   1039       1.1  augustss 		return 0;
   1040       1.1  augustss 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
   1041      1.13  augustss }
   1042      1.18  augustss 
   1043      1.18  augustss #elif defined(__FreeBSD__)
   1044      1.18  augustss static void
   1045      1.18  augustss usbd_bus_print_child(device_t bus, device_t dev)
   1046      1.18  augustss {
   1047      1.18  augustss 	/* FIXME print the device address and the configuration used
   1048      1.18  augustss 	 */
   1049      1.18  augustss }
   1050      1.18  augustss #endif
   1051      1.19  augustss 
   1052      1.19  augustss usbd_status
   1053      1.19  augustss usb_insert_transfer(reqh)
   1054      1.19  augustss 	usbd_request_handle reqh;
   1055      1.19  augustss {
   1056      1.19  augustss 	usbd_pipe_handle pipe = reqh->pipe;
   1057      1.19  augustss 
   1058      1.19  augustss 	SIMPLEQ_INSERT_TAIL(&pipe->queue, reqh, next);
   1059      1.19  augustss 	if (pipe->running)
   1060      1.19  augustss 		return (USBD_IN_PROGRESS);
   1061      1.19  augustss 	pipe->running = 1;
   1062      1.19  augustss 	return (USBD_NORMAL_COMPLETION);
   1063      1.19  augustss }
   1064      1.19  augustss 
   1065      1.19  augustss void
   1066      1.19  augustss usb_start_next(pipe)
   1067      1.19  augustss 	usbd_pipe_handle pipe;
   1068      1.19  augustss {
   1069      1.19  augustss 	usbd_request_handle reqh;
   1070      1.19  augustss 	usbd_status r;
   1071      1.19  augustss 
   1072  1.29.4.1   thorpej 	DPRINTFN(10, ("usb_start_next: pipe=%p\n", pipe));
   1073  1.29.4.1   thorpej 
   1074      1.19  augustss #ifdef DIAGNOSTIC
   1075  1.29.4.1   thorpej 	if (!pipe) {
   1076  1.29.4.1   thorpej 		printf("usb_start_next: pipe == 0\n");
   1077  1.29.4.1   thorpej 		return;
   1078  1.29.4.1   thorpej 	}
   1079  1.29.4.1   thorpej 	if (!pipe->methods || !pipe->methods->start) {
   1080  1.29.4.1   thorpej 		printf("usb_start_next:  no start method\n");
   1081  1.29.4.1   thorpej 		return;
   1082  1.29.4.1   thorpej 	}
   1083      1.19  augustss 	if (SIMPLEQ_FIRST(&pipe->queue) == 0) {
   1084      1.19  augustss 		printf("usb_start_next: empty\n");
   1085      1.19  augustss 		return;
   1086      1.19  augustss 	}
   1087      1.19  augustss #endif
   1088      1.19  augustss 
   1089      1.19  augustss 	/* First remove remove old */
   1090      1.19  augustss 	SIMPLEQ_REMOVE_HEAD(&pipe->queue, SIMPLEQ_FIRST(&pipe->queue), next);
   1091      1.19  augustss 	reqh = SIMPLEQ_FIRST(&pipe->queue);
   1092      1.19  augustss 	DPRINTFN(5, ("usb_start_next: start reqh=%p\n", reqh));
   1093      1.19  augustss 	if (!reqh)
   1094      1.19  augustss 		pipe->running = 0;
   1095      1.19  augustss 	else {
   1096      1.19  augustss 		r = pipe->methods->start(reqh);
   1097      1.19  augustss 		if (r != USBD_IN_PROGRESS) {
   1098      1.19  augustss 			printf("usb_start_next: error=%d\n", r);
   1099      1.19  augustss 			pipe->running = 0;
   1100      1.19  augustss 			/* XXX do what? */
   1101      1.19  augustss 		}
   1102      1.19  augustss 	}
   1103      1.19  augustss }
   1104      1.13  augustss 
   1105      1.13  augustss void
   1106      1.13  augustss usbd_fill_deviceinfo(dev, di)
   1107      1.13  augustss 	usbd_device_handle dev;
   1108      1.13  augustss 	struct usb_device_info *di;
   1109      1.13  augustss {
   1110      1.13  augustss 	struct usbd_port *p;
   1111      1.13  augustss 	int i, r, s;
   1112      1.13  augustss 
   1113      1.13  augustss 	di->config = dev->config;
   1114      1.13  augustss 	usbd_devinfo_vp(dev, di->vendor, di->product);
   1115  1.29.4.1   thorpej 	usbd_printBCD(di->release, UGETW(dev->ddesc.bcdDevice));
   1116      1.21  augustss 	di->vendorNo = UGETW(dev->ddesc.idVendor);
   1117      1.21  augustss 	di->productNo = UGETW(dev->ddesc.idProduct);
   1118      1.13  augustss 	di->class = dev->ddesc.bDeviceClass;
   1119      1.13  augustss 	di->power = dev->self_powered ? 0 : dev->power;
   1120      1.13  augustss 	di->lowspeed = dev->lowspeed;
   1121      1.13  augustss 	di->addr = dev->address;
   1122      1.13  augustss 	if (dev->hub) {
   1123      1.13  augustss 		for (i = 0;
   1124      1.13  augustss 		     i < sizeof(di->ports) / sizeof(di->ports[0]) &&
   1125      1.13  augustss 			     i < dev->hub->hubdesc.bNbrPorts;
   1126      1.13  augustss 		     i++) {
   1127      1.13  augustss 			p = &dev->hub->ports[i];
   1128      1.13  augustss 			if (p->device)
   1129      1.13  augustss 				r = p->device->address;
   1130      1.13  augustss 			else {
   1131      1.13  augustss 				s = UGETW(p->status.wPortStatus);
   1132      1.13  augustss 				if (s & UPS_PORT_ENABLED)
   1133      1.13  augustss 					r = USB_PORT_ENABLED;
   1134      1.13  augustss 				else if (s & UPS_SUSPEND)
   1135      1.13  augustss 					r = USB_PORT_SUSPENDED;
   1136      1.13  augustss 				else if (s & UPS_PORT_POWER)
   1137      1.13  augustss 					r = USB_PORT_POWERED;
   1138      1.13  augustss 				else
   1139      1.13  augustss 					r = USB_PORT_DISABLED;
   1140      1.13  augustss 			}
   1141      1.13  augustss 			di->ports[i] = r;
   1142      1.13  augustss 		}
   1143      1.13  augustss 		di->nports = dev->hub->hubdesc.bNbrPorts;
   1144      1.13  augustss 	} else
   1145      1.13  augustss 		di->nports = 0;
   1146       1.1  augustss }
   1147