Home | History | Annotate | Line # | Download | only in usb
usb_subr.c revision 1.162.12.1
      1  1.162.12.1      matt /*	$NetBSD: usb_subr.c,v 1.162.12.1 2013/11/05 18:36:31 matt Exp $	*/
      2        1.56  augustss /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
      3         1.1  augustss 
      4         1.1  augustss /*
      5       1.121   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      6         1.1  augustss  * All rights reserved.
      7         1.1  augustss  *
      8         1.9  augustss  * This code is derived from software contributed to The NetBSD Foundation
      9        1.76  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10         1.9  augustss  * Carlstedt Research & Technology.
     11         1.1  augustss  *
     12         1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13         1.1  augustss  * modification, are permitted provided that the following conditions
     14         1.1  augustss  * are met:
     15         1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16         1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17         1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18         1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19         1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20         1.1  augustss  *
     21         1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22         1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23         1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24         1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25         1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26         1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27         1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28         1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29         1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30         1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31         1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     32         1.1  augustss  */
     33        1.91     lukem 
     34        1.91     lukem #include <sys/cdefs.h>
     35  1.162.12.1      matt __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.162.12.1 2013/11/05 18:36:31 matt Exp $");
     36       1.104    martin 
     37       1.140     pavel #include "opt_compat_netbsd.h"
     38       1.104    martin #include "opt_usbverbose.h"
     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.1  augustss #include <sys/device.h>
     45        1.56  augustss #include <sys/select.h>
     46         1.1  augustss #include <sys/proc.h>
     47        1.46  augustss 
     48       1.149        ad #include <sys/bus.h>
     49         1.1  augustss 
     50         1.1  augustss #include <dev/usb/usb.h>
     51         1.1  augustss 
     52         1.1  augustss #include <dev/usb/usbdi.h>
     53         1.1  augustss #include <dev/usb/usbdi_util.h>
     54         1.1  augustss #include <dev/usb/usbdivar.h>
     55         1.1  augustss #include <dev/usb/usbdevs.h>
     56         1.1  augustss #include <dev/usb/usb_quirks.h>
     57         1.1  augustss 
     58       1.158  drochner #include "locators.h"
     59       1.158  drochner 
     60         1.1  augustss #ifdef USB_DEBUG
     61        1.35  augustss #define DPRINTF(x)	if (usbdebug) logprintf x
     62        1.35  augustss #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     63         1.1  augustss extern int usbdebug;
     64         1.1  augustss #else
     65         1.1  augustss #define DPRINTF(x)
     66         1.1  augustss #define DPRINTFN(n,x)
     67         1.1  augustss #endif
     68         1.1  augustss 
     69        1.82  augustss Static usbd_status usbd_set_config(usbd_device_handle, int);
     70       1.124  augustss Static void usbd_devinfo(usbd_device_handle, int, char *, size_t);
     71       1.123  augustss Static void usbd_devinfo_vp(usbd_device_handle dev,
     72       1.139     pavel 			    char *v,
     73       1.139     pavel 			    char *p, int usedev,
     74       1.139     pavel 			    int useencoded );
     75        1.78  augustss Static int usbd_getnewaddr(usbd_bus_handle bus);
     76       1.118  drochner Static int usbd_print(void *, const char *);
     77       1.144  drochner Static int usbd_ifprint(void *, const char *);
     78  1.162.12.1      matt Static void usbd_free_iface_data(usbd_device_handle, int);
     79       1.155  drochner usbd_status usbd_attach_roothub(device_t, usbd_device_handle);
     80         1.1  augustss 
     81  1.162.12.1      matt uint32_t usb_cookie_no = 0;
     82        1.27  augustss 
     83         1.1  augustss #ifdef USBVERBOSE
     84         1.1  augustss typedef u_int16_t usb_vendor_id_t;
     85         1.1  augustss typedef u_int16_t usb_product_id_t;
     86         1.1  augustss 
     87         1.1  augustss /*
     88         1.1  augustss  * Descriptions of of known vendors and devices ("products").
     89         1.1  augustss  */
     90       1.122   mycroft struct usb_vendor {
     91       1.122   mycroft 	usb_vendor_id_t		vendor;
     92       1.126  christos 	const char		*vendorname;
     93       1.122   mycroft };
     94       1.122   mycroft struct usb_product {
     95         1.1  augustss 	usb_vendor_id_t		vendor;
     96         1.1  augustss 	usb_product_id_t	product;
     97       1.126  christos 	const char		*productname;
     98         1.1  augustss };
     99         1.1  augustss 
    100         1.1  augustss #include <dev/usb/usbdevs_data.h>
    101         1.1  augustss #endif /* USBVERBOSE */
    102         1.1  augustss 
    103        1.84  jdolecek Static const char * const usbd_error_strs[] = {
    104        1.12  augustss 	"NORMAL_COMPLETION",
    105        1.12  augustss 	"IN_PROGRESS",
    106        1.12  augustss 	"PENDING_REQUESTS",
    107        1.12  augustss 	"NOT_STARTED",
    108        1.12  augustss 	"INVAL",
    109        1.12  augustss 	"NOMEM",
    110        1.12  augustss 	"CANCELLED",
    111        1.12  augustss 	"BAD_ADDRESS",
    112        1.12  augustss 	"IN_USE",
    113        1.12  augustss 	"NO_ADDR",
    114        1.12  augustss 	"SET_ADDR_FAILED",
    115        1.12  augustss 	"NO_POWER",
    116        1.12  augustss 	"TOO_DEEP",
    117        1.12  augustss 	"IOERROR",
    118        1.12  augustss 	"NOT_CONFIGURED",
    119        1.12  augustss 	"TIMEOUT",
    120        1.12  augustss 	"SHORT_XFER",
    121        1.12  augustss 	"STALLED",
    122        1.33  augustss 	"INTERRUPTED",
    123        1.12  augustss 	"XXX",
    124        1.12  augustss };
    125         1.1  augustss 
    126        1.42   thorpej const char *
    127        1.78  augustss usbd_errstr(usbd_status err)
    128        1.39  augustss {
    129        1.39  augustss 	static char buffer[5];
    130        1.39  augustss 
    131        1.42   thorpej 	if (err < USBD_ERROR_MAX) {
    132        1.39  augustss 		return usbd_error_strs[err];
    133        1.39  augustss 	} else {
    134        1.39  augustss 		snprintf(buffer, sizeof buffer, "%d", err);
    135        1.39  augustss 		return buffer;
    136        1.39  augustss 	}
    137        1.39  augustss }
    138        1.39  augustss 
    139        1.13  augustss usbd_status
    140        1.78  augustss usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
    141       1.114   mycroft 		     usb_string_descriptor_t *sdesc, int *sizep)
    142        1.13  augustss {
    143        1.13  augustss 	usb_device_request_t req;
    144       1.116   mycroft 	usbd_status err;
    145       1.116   mycroft 	int actlen;
    146        1.13  augustss 
    147        1.13  augustss 	req.bmRequestType = UT_READ_DEVICE;
    148        1.13  augustss 	req.bRequest = UR_GET_DESCRIPTOR;
    149        1.13  augustss 	USETW2(req.wValue, UDESC_STRING, sindex);
    150        1.13  augustss 	USETW(req.wIndex, langid);
    151       1.146  christos 	USETW(req.wLength, 2);	/* only size byte first */
    152       1.116   mycroft 	err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
    153       1.116   mycroft 		&actlen, USBD_DEFAULT_TIMEOUT);
    154       1.116   mycroft 	if (err)
    155       1.116   mycroft 		return (err);
    156       1.116   mycroft 
    157       1.146  christos 	if (actlen < 2)
    158       1.146  christos 		return (USBD_SHORT_XFER);
    159       1.146  christos 
    160       1.146  christos 	USETW(req.wLength, sdesc->bLength);	/* the whole string */
    161       1.146  christos  	err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
    162       1.146  christos  		&actlen, USBD_DEFAULT_TIMEOUT);
    163       1.146  christos 	if (err)
    164       1.146  christos 		return (err);
    165       1.146  christos 
    166       1.116   mycroft 	if (actlen != sdesc->bLength) {
    167       1.116   mycroft 		DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
    168       1.116   mycroft 		    sdesc->bLength, actlen));
    169       1.116   mycroft 	}
    170       1.116   mycroft 
    171       1.116   mycroft 	*sizep = actlen;
    172       1.116   mycroft 	return (USBD_NORMAL_COMPLETION);
    173        1.13  augustss }
    174        1.13  augustss 
    175       1.128     enami static void
    176       1.128     enami usbd_trim_spaces(char *p)
    177        1.83  augustss {
    178       1.128     enami 	char *q, *e;
    179        1.83  augustss 
    180       1.128     enami 	q = e = p;
    181       1.128     enami 	while (*q == ' ')		/* skip leading spaces */
    182        1.85  augustss 		q++;
    183       1.128     enami 	while ((*p = *q++))		/* copy string */
    184       1.128     enami 		if (*p++ != ' ')	/* remember last non-space */
    185       1.128     enami 			e = p;
    186       1.128     enami 	*e = '\0';			/* kill trailing spaces */
    187        1.83  augustss }
    188        1.83  augustss 
    189       1.123  augustss Static void
    190       1.139     pavel usbd_devinfo_vp(usbd_device_handle dev, char *v,
    191       1.139     pavel 		char *p, int usedev, int useencoded)
    192         1.1  augustss {
    193         1.1  augustss 	usb_device_descriptor_t *udd = &dev->ddesc;
    194         1.2        is #ifdef USBVERBOSE
    195       1.122   mycroft 	int n;
    196         1.2        is #endif
    197         1.1  augustss 
    198       1.128     enami 	v[0] = p[0] = '\0';
    199       1.128     enami 	if (dev == NULL)
    200        1.52  augustss 		return;
    201        1.52  augustss 
    202        1.82  augustss 	if (usedev) {
    203       1.139     pavel 		if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
    204       1.128     enami 		    USBD_NORMAL_COMPLETION)
    205       1.128     enami 			usbd_trim_spaces(v);
    206       1.139     pavel 		if (usbd_get_string0(dev, udd->iProduct, p, useencoded) ==
    207       1.128     enami 		    USBD_NORMAL_COMPLETION)
    208       1.128     enami 			usbd_trim_spaces(p);
    209        1.82  augustss 	}
    210       1.128     enami 	/* There is no need for strlcpy & snprintf below. */
    211         1.1  augustss #ifdef USBVERBOSE
    212       1.128     enami 	if (v[0] == '\0')
    213       1.122   mycroft 		for (n = 0; n < usb_nvendors; n++)
    214       1.128     enami 			if (usb_vendors[n].vendor == UGETW(udd->idVendor)) {
    215       1.128     enami 				strcpy(v, usb_vendors[n].vendorname);
    216       1.128     enami 				break;
    217       1.128     enami 			}
    218       1.128     enami 	if (p[0] == '\0')
    219       1.122   mycroft 		for (n = 0; n < usb_nproducts; n++)
    220       1.122   mycroft 			if (usb_products[n].vendor == UGETW(udd->idVendor) &&
    221       1.128     enami 			    usb_products[n].product == UGETW(udd->idProduct)) {
    222       1.128     enami 				strcpy(p, usb_products[n].productname);
    223       1.128     enami 				break;
    224       1.128     enami 			}
    225         1.1  augustss #endif
    226       1.128     enami 	if (v[0] == '\0')
    227       1.123  augustss 		sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
    228       1.128     enami 	if (p[0] == '\0')
    229       1.123  augustss 		sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
    230         1.1  augustss }
    231         1.1  augustss 
    232         1.1  augustss int
    233       1.113    itojun usbd_printBCD(char *cp, size_t l, int bcd)
    234         1.1  augustss {
    235       1.113    itojun 	return (snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff));
    236         1.1  augustss }
    237         1.1  augustss 
    238       1.124  augustss Static void
    239       1.113    itojun usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l)
    240         1.1  augustss {
    241         1.1  augustss 	usb_device_descriptor_t *udd = &dev->ddesc;
    242       1.133  christos 	char *vendor, *product;
    243         1.1  augustss 	int bcdDevice, bcdUSB;
    244       1.113    itojun 	char *ep;
    245       1.113    itojun 
    246       1.133  christos 	vendor = malloc(USB_MAX_ENCODED_STRING_LEN * 2, M_USB, M_NOWAIT);
    247       1.133  christos 	if (vendor == NULL) {
    248       1.133  christos 		*cp = '\0';
    249       1.133  christos 		return;
    250       1.133  christos 	}
    251       1.133  christos 	product = &vendor[USB_MAX_ENCODED_STRING_LEN];
    252       1.133  christos 
    253       1.113    itojun 	ep = cp + l;
    254         1.1  augustss 
    255       1.139     pavel 	usbd_devinfo_vp(dev, vendor, product, 1, 1);
    256       1.113    itojun 	cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
    257         1.1  augustss 	if (showclass)
    258       1.113    itojun 		cp += snprintf(cp, ep - cp, ", class %d/%d",
    259       1.113    itojun 		    udd->bDeviceClass, udd->bDeviceSubClass);
    260         1.1  augustss 	bcdUSB = UGETW(udd->bcdUSB);
    261         1.1  augustss 	bcdDevice = UGETW(udd->bcdDevice);
    262       1.113    itojun 	cp += snprintf(cp, ep - cp, ", rev ");
    263       1.113    itojun 	cp += usbd_printBCD(cp, ep - cp, bcdUSB);
    264         1.1  augustss 	*cp++ = '/';
    265       1.113    itojun 	cp += usbd_printBCD(cp, ep - cp, bcdDevice);
    266       1.113    itojun 	cp += snprintf(cp, ep - cp, ", addr %d", dev->address);
    267        1.10  augustss 	*cp = 0;
    268       1.133  christos 	free(vendor, M_USB);
    269         1.1  augustss }
    270         1.1  augustss 
    271       1.124  augustss char *
    272       1.124  augustss usbd_devinfo_alloc(usbd_device_handle dev, int showclass)
    273       1.124  augustss {
    274       1.124  augustss 	char *devinfop;
    275       1.124  augustss 
    276       1.124  augustss 	devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK);
    277       1.124  augustss 	usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE);
    278       1.124  augustss 	return devinfop;
    279       1.124  augustss }
    280       1.124  augustss 
    281       1.124  augustss void
    282       1.124  augustss usbd_devinfo_free(char *devinfop)
    283       1.124  augustss {
    284       1.124  augustss 	free(devinfop, M_TEMP);
    285       1.124  augustss }
    286       1.124  augustss 
    287         1.1  augustss /* Delay for a certain number of ms */
    288         1.1  augustss void
    289        1.78  augustss usb_delay_ms(usbd_bus_handle bus, u_int ms)
    290         1.1  augustss {
    291         1.1  augustss 	/* Wait at least two clock ticks so we know the time has passed. */
    292        1.64  augustss 	if (bus->use_polling || cold)
    293         1.1  augustss 		delay((ms+1) * 1000);
    294         1.1  augustss 	else
    295         1.1  augustss 		tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
    296         1.1  augustss }
    297         1.1  augustss 
    298        1.23  augustss /* Delay given a device handle. */
    299        1.23  augustss void
    300        1.78  augustss usbd_delay_ms(usbd_device_handle dev, u_int ms)
    301        1.23  augustss {
    302        1.23  augustss 	usb_delay_ms(dev->bus, ms);
    303        1.23  augustss }
    304        1.23  augustss 
    305         1.1  augustss usbd_status
    306        1.78  augustss usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
    307         1.1  augustss {
    308         1.1  augustss 	usb_device_request_t req;
    309        1.53  augustss 	usbd_status err;
    310         1.1  augustss 	int n;
    311        1.99  augustss 
    312         1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    313         1.1  augustss 	req.bRequest = UR_SET_FEATURE;
    314         1.1  augustss 	USETW(req.wValue, UHF_PORT_RESET);
    315         1.1  augustss 	USETW(req.wIndex, port);
    316         1.1  augustss 	USETW(req.wLength, 0);
    317        1.53  augustss 	err = usbd_do_request(dev, &req, 0);
    318        1.39  augustss 	DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
    319        1.53  augustss 		    port, usbd_errstr(err)));
    320        1.53  augustss 	if (err)
    321        1.53  augustss 		return (err);
    322         1.1  augustss 	n = 10;
    323         1.1  augustss 	do {
    324         1.1  augustss 		/* Wait for device to recover from reset. */
    325        1.23  augustss 		usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
    326        1.53  augustss 		err = usbd_get_port_status(dev, port, ps);
    327        1.53  augustss 		if (err) {
    328        1.53  augustss 			DPRINTF(("usbd_reset_port: get status failed %d\n",
    329        1.53  augustss 				 err));
    330        1.53  augustss 			return (err);
    331         1.1  augustss 		}
    332        1.92  augustss 		/* If the device disappeared, just give up. */
    333        1.92  augustss 		if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
    334        1.92  augustss 			return (USBD_NORMAL_COMPLETION);
    335         1.1  augustss 	} while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
    336        1.73  augustss 	if (n == 0)
    337        1.73  augustss 		return (USBD_TIMEOUT);
    338        1.53  augustss 	err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    339         1.1  augustss #ifdef USB_DEBUG
    340        1.53  augustss 	if (err)
    341        1.53  augustss 		DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
    342        1.53  augustss 			 err));
    343         1.1  augustss #endif
    344        1.18  augustss 
    345        1.18  augustss 	/* Wait for the device to recover from reset. */
    346        1.23  augustss 	usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
    347        1.53  augustss 	return (err);
    348         1.1  augustss }
    349         1.1  augustss 
    350        1.12  augustss usb_interface_descriptor_t *
    351        1.78  augustss usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
    352        1.12  augustss {
    353        1.12  augustss 	char *p = (char *)cd;
    354        1.12  augustss 	char *end = p + UGETW(cd->wTotalLength);
    355        1.12  augustss 	usb_interface_descriptor_t *d;
    356        1.19  augustss 	int curidx, lastidx, curaidx = 0;
    357        1.12  augustss 
    358        1.19  augustss 	for (curidx = lastidx = -1; p < end; ) {
    359        1.12  augustss 		d = (usb_interface_descriptor_t *)p;
    360        1.18  augustss 		DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
    361        1.99  augustss 			    "type=%d\n",
    362        1.18  augustss 			    ifaceidx, curidx, altidx, curaidx,
    363        1.18  augustss 			    d->bLength, d->bDescriptorType));
    364        1.12  augustss 		if (d->bLength == 0) /* bad descriptor */
    365        1.12  augustss 			break;
    366        1.12  augustss 		p += d->bLength;
    367        1.12  augustss 		if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
    368        1.19  augustss 			if (d->bInterfaceNumber != lastidx) {
    369        1.19  augustss 				lastidx = d->bInterfaceNumber;
    370        1.12  augustss 				curidx++;
    371        1.12  augustss 				curaidx = 0;
    372        1.12  augustss 			} else
    373        1.12  augustss 				curaidx++;
    374        1.12  augustss 			if (ifaceidx == curidx && altidx == curaidx)
    375        1.12  augustss 				return (d);
    376        1.12  augustss 		}
    377        1.12  augustss 	}
    378        1.58  augustss 	return (NULL);
    379        1.12  augustss }
    380        1.12  augustss 
    381        1.12  augustss usb_endpoint_descriptor_t *
    382        1.99  augustss usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
    383        1.78  augustss 		int endptidx)
    384        1.12  augustss {
    385        1.12  augustss 	char *p = (char *)cd;
    386        1.12  augustss 	char *end = p + UGETW(cd->wTotalLength);
    387        1.12  augustss 	usb_interface_descriptor_t *d;
    388        1.12  augustss 	usb_endpoint_descriptor_t *e;
    389        1.12  augustss 	int curidx;
    390        1.12  augustss 
    391        1.12  augustss 	d = usbd_find_idesc(cd, ifaceidx, altidx);
    392        1.53  augustss 	if (d == NULL)
    393        1.58  augustss 		return (NULL);
    394        1.12  augustss 	if (endptidx >= d->bNumEndpoints) /* quick exit */
    395        1.58  augustss 		return (NULL);
    396        1.12  augustss 
    397        1.12  augustss 	curidx = -1;
    398        1.12  augustss 	for (p = (char *)d + d->bLength; p < end; ) {
    399        1.12  augustss 		e = (usb_endpoint_descriptor_t *)p;
    400        1.12  augustss 		if (e->bLength == 0) /* bad descriptor */
    401        1.12  augustss 			break;
    402        1.12  augustss 		p += e->bLength;
    403        1.12  augustss 		if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
    404        1.58  augustss 			return (NULL);
    405        1.12  augustss 		if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
    406        1.12  augustss 			curidx++;
    407        1.12  augustss 			if (curidx == endptidx)
    408        1.12  augustss 				return (e);
    409        1.12  augustss 		}
    410         1.1  augustss 	}
    411        1.58  augustss 	return (NULL);
    412         1.1  augustss }
    413         1.1  augustss 
    414         1.1  augustss usbd_status
    415        1.78  augustss usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
    416         1.1  augustss {
    417        1.12  augustss 	usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
    418        1.77  augustss 	usb_interface_descriptor_t *idesc;
    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.77  augustss 	idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
    425        1.77  augustss 	if (idesc == NULL)
    426        1.77  augustss 		return (USBD_INVAL);
    427         1.1  augustss 	ifc->device = dev;
    428        1.77  augustss 	ifc->idesc = idesc;
    429        1.13  augustss 	ifc->index = ifaceidx;
    430        1.13  augustss 	ifc->altindex = altidx;
    431         1.1  augustss 	nendpt = ifc->idesc->bNumEndpoints;
    432        1.59  augustss 	DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
    433         1.1  augustss 	if (nendpt != 0) {
    434         1.1  augustss 		ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
    435         1.1  augustss 					M_USB, M_NOWAIT);
    436        1.58  augustss 		if (ifc->endpoints == NULL)
    437         1.1  augustss 			return (USBD_NOMEM);
    438         1.1  augustss 	} else
    439        1.58  augustss 		ifc->endpoints = NULL;
    440        1.58  augustss 	ifc->priv = NULL;
    441         1.1  augustss 	p = (char *)ifc->idesc + ifc->idesc->bLength;
    442         1.1  augustss 	end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
    443        1.19  augustss #define ed ((usb_endpoint_descriptor_t *)p)
    444         1.1  augustss 	for (endpt = 0; endpt < nendpt; endpt++) {
    445         1.1  augustss 		DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
    446         1.1  augustss 		for (; p < end; p += ed->bLength) {
    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.59  augustss 			if (ed->bLength == 0 ||
    454        1.59  augustss 			    ed->bDescriptorType == UDESC_INTERFACE)
    455         1.1  augustss 				break;
    456         1.1  augustss 		}
    457        1.24  augustss 		/* passed end, or bad desc */
    458        1.95  augustss 		printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
    459        1.95  augustss 		       ed->bLength == 0 ? "0 length" :
    460        1.95  augustss 		       ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
    461        1.95  augustss 		       "out of data");
    462        1.24  augustss 		goto bad;
    463        1.24  augustss 	found:
    464         1.1  augustss 		ifc->endpoints[endpt].edesc = ed;
    465        1.95  augustss 		if (dev->speed == USB_SPEED_HIGH) {
    466        1.95  augustss 			u_int mps;
    467        1.95  augustss 			/* Control and bulk endpoints have max packet limits. */
    468        1.95  augustss 			switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
    469        1.95  augustss 			case UE_CONTROL:
    470        1.95  augustss 				mps = USB_2_MAX_CTRL_PACKET;
    471        1.95  augustss 				goto check;
    472        1.95  augustss 			case UE_BULK:
    473        1.95  augustss 				mps = USB_2_MAX_BULK_PACKET;
    474        1.95  augustss 			check:
    475        1.95  augustss 				if (UGETW(ed->wMaxPacketSize) != mps) {
    476        1.95  augustss 					USETW(ed->wMaxPacketSize, mps);
    477        1.95  augustss #ifdef DIAGNOSTIC
    478        1.95  augustss 					printf("usbd_fill_iface_data: bad max "
    479        1.95  augustss 					       "packet size\n");
    480        1.95  augustss #endif
    481        1.95  augustss 				}
    482        1.95  augustss 				break;
    483        1.95  augustss 			default:
    484        1.95  augustss 				break;
    485        1.95  augustss 			}
    486        1.95  augustss 		}
    487         1.1  augustss 		ifc->endpoints[endpt].refcnt = 0;
    488        1.24  augustss 		p += ed->bLength;
    489         1.1  augustss 	}
    490        1.19  augustss #undef ed
    491         1.1  augustss 	LIST_INIT(&ifc->pipes);
    492         1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    493        1.24  augustss 
    494         1.1  augustss  bad:
    495        1.77  augustss 	if (ifc->endpoints != NULL) {
    496        1.59  augustss 		free(ifc->endpoints, M_USB);
    497        1.77  augustss 		ifc->endpoints = NULL;
    498        1.77  augustss 	}
    499        1.24  augustss 	return (USBD_INVAL);
    500         1.1  augustss }
    501         1.1  augustss 
    502         1.1  augustss void
    503        1.78  augustss usbd_free_iface_data(usbd_device_handle dev, int ifcno)
    504         1.1  augustss {
    505         1.1  augustss 	usbd_interface_handle ifc = &dev->ifaces[ifcno];
    506         1.1  augustss 	if (ifc->endpoints)
    507         1.1  augustss 		free(ifc->endpoints, M_USB);
    508         1.1  augustss }
    509         1.1  augustss 
    510        1.69  augustss Static usbd_status
    511        1.78  augustss usbd_set_config(usbd_device_handle dev, int conf)
    512         1.7  augustss {
    513         1.7  augustss 	usb_device_request_t req;
    514         1.7  augustss 
    515         1.7  augustss 	req.bmRequestType = UT_WRITE_DEVICE;
    516         1.7  augustss 	req.bRequest = UR_SET_CONFIG;
    517         1.7  augustss 	USETW(req.wValue, conf);
    518         1.7  augustss 	USETW(req.wIndex, 0);
    519         1.7  augustss 	USETW(req.wLength, 0);
    520         1.7  augustss 	return (usbd_do_request(dev, &req, 0));
    521         1.7  augustss }
    522         1.7  augustss 
    523         1.1  augustss usbd_status
    524        1.78  augustss usbd_set_config_no(usbd_device_handle dev, int no, int msg)
    525         1.1  augustss {
    526        1.12  augustss 	int index;
    527        1.12  augustss 	usb_config_descriptor_t cd;
    528        1.53  augustss 	usbd_status err;
    529        1.12  augustss 
    530        1.75  augustss 	if (no == USB_UNCONFIG_NO)
    531        1.75  augustss 		return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
    532        1.75  augustss 
    533        1.12  augustss 	DPRINTFN(5,("usbd_set_config_no: %d\n", no));
    534        1.12  augustss 	/* Figure out what config index to use. */
    535        1.12  augustss 	for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
    536        1.53  augustss 		err = usbd_get_config_desc(dev, index, &cd);
    537        1.53  augustss 		if (err)
    538        1.53  augustss 			return (err);
    539        1.12  augustss 		if (cd.bConfigurationValue == no)
    540        1.12  augustss 			return (usbd_set_config_index(dev, index, msg));
    541        1.12  augustss 	}
    542        1.12  augustss 	return (USBD_INVAL);
    543        1.12  augustss }
    544        1.12  augustss 
    545        1.12  augustss usbd_status
    546        1.78  augustss usbd_set_config_index(usbd_device_handle dev, int index, int msg)
    547        1.12  augustss {
    548         1.1  augustss 	usb_config_descriptor_t cd, *cdp;
    549        1.53  augustss 	usbd_status err;
    550       1.103  augustss 	int i, ifcidx, nifc, len, selfpowered, power;
    551         1.1  augustss 
    552        1.12  augustss 	DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
    553         1.1  augustss 
    554       1.145  drochner 	if (index >= dev->ddesc.bNumConfigurations &&
    555       1.161  drochner 	    index != USB_UNCONFIG_INDEX) {
    556       1.145  drochner 		/* panic? */
    557       1.145  drochner 		printf("usbd_set_config_index: illegal index\n");
    558       1.145  drochner 		return (USBD_INVAL);
    559       1.145  drochner 	}
    560       1.145  drochner 
    561         1.1  augustss 	/* XXX check that all interfaces are idle */
    562        1.75  augustss 	if (dev->config != USB_UNCONFIG_NO) {
    563        1.12  augustss 		DPRINTF(("usbd_set_config_index: free old config\n"));
    564         1.1  augustss 		/* Free all configuration data structures. */
    565         1.1  augustss 		nifc = dev->cdesc->bNumInterface;
    566        1.12  augustss 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
    567        1.12  augustss 			usbd_free_iface_data(dev, ifcidx);
    568         1.1  augustss 		free(dev->ifaces, M_USB);
    569         1.1  augustss 		free(dev->cdesc, M_USB);
    570        1.57  augustss 		dev->ifaces = NULL;
    571        1.57  augustss 		dev->cdesc = NULL;
    572        1.75  augustss 		dev->config = USB_UNCONFIG_NO;
    573        1.75  augustss 	}
    574        1.75  augustss 
    575        1.75  augustss 	if (index == USB_UNCONFIG_INDEX) {
    576        1.75  augustss 		/* We are unconfiguring the device, so leave unallocated. */
    577        1.75  augustss 		DPRINTF(("usbd_set_config_index: set config 0\n"));
    578        1.75  augustss 		err = usbd_set_config(dev, USB_UNCONFIG_NO);
    579       1.136  christos 		if (err) {
    580        1.75  augustss 			DPRINTF(("usbd_set_config_index: setting config=0 "
    581        1.75  augustss 				 "failed, error=%s\n", usbd_errstr(err)));
    582       1.136  christos 		}
    583        1.75  augustss 		return (err);
    584         1.1  augustss 	}
    585         1.1  augustss 
    586        1.74  augustss 	/* Get the short descriptor. */
    587        1.53  augustss 	err = usbd_get_config_desc(dev, index, &cd);
    588        1.53  augustss 	if (err)
    589        1.53  augustss 		return (err);
    590         1.1  augustss 	len = UGETW(cd.wTotalLength);
    591         1.1  augustss 	cdp = malloc(len, M_USB, M_NOWAIT);
    592        1.53  augustss 	if (cdp == NULL)
    593         1.1  augustss 		return (USBD_NOMEM);
    594       1.103  augustss 
    595       1.103  augustss 	/* Get the full descriptor.  Try a few times for slow devices. */
    596       1.103  augustss 	for (i = 0; i < 3; i++) {
    597       1.103  augustss 		err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
    598       1.103  augustss 		if (!err)
    599       1.103  augustss 			break;
    600       1.103  augustss 		usbd_delay_ms(dev, 200);
    601       1.103  augustss 	}
    602        1.53  augustss 	if (err)
    603         1.1  augustss 		goto bad;
    604       1.103  augustss 
    605        1.18  augustss 	if (cdp->bDescriptorType != UDESC_CONFIG) {
    606        1.18  augustss 		DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
    607        1.18  augustss 			     cdp->bDescriptorType));
    608        1.53  augustss 		err = USBD_INVAL;
    609        1.18  augustss 		goto bad;
    610        1.18  augustss 	}
    611        1.74  augustss 
    612       1.142  drochner 	/*
    613       1.142  drochner 	 * Figure out if the device is self or bus powered.
    614       1.142  drochner 	 */
    615       1.142  drochner #if 0 /* XXX various devices don't report the power state correctly */
    616         1.1  augustss 	selfpowered = 0;
    617       1.141  drochner 	err = usbd_get_device_status(dev, &ds);
    618       1.141  drochner 	if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
    619       1.141  drochner 		selfpowered = 1;
    620       1.142  drochner #endif
    621       1.142  drochner 	/*
    622       1.142  drochner 	 * Use the power state in the configuration we are going
    623       1.142  drochner 	 * to set. This doesn't necessarily reflect the actual
    624       1.142  drochner 	 * power state of the device; the driver can control this
    625       1.142  drochner 	 * by choosing the appropriate configuration.
    626       1.142  drochner 	 */
    627       1.142  drochner 	selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
    628       1.141  drochner 
    629        1.81  augustss 	DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
    630        1.81  augustss 		 "selfpowered=%d, power=%d\n",
    631        1.99  augustss 		 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
    632        1.33  augustss 		 selfpowered, cdp->bMaxPower * 2));
    633        1.74  augustss 
    634        1.74  augustss 	/* Check if we have enough power. */
    635       1.142  drochner #if 0 /* this is a no-op, see above */
    636       1.141  drochner 	if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
    637       1.141  drochner 		if (msg)
    638       1.141  drochner 			printf("%s: device addr %d (config %d): "
    639       1.141  drochner 				 "can't set self powered configuration\n",
    640       1.141  drochner 			       USBDEVNAME(dev->bus->bdev), dev->address,
    641       1.141  drochner 			       cdp->bConfigurationValue);
    642       1.141  drochner 		err = USBD_NO_POWER;
    643       1.141  drochner 		goto bad;
    644       1.141  drochner 	}
    645       1.142  drochner #endif
    646         1.1  augustss #ifdef USB_DEBUG
    647        1.53  augustss 	if (dev->powersrc == NULL) {
    648        1.44  augustss 		DPRINTF(("usbd_set_config_index: No power source?\n"));
    649       1.141  drochner 		err = USBD_IOERROR;
    650       1.141  drochner 		goto bad;
    651         1.1  augustss 	}
    652         1.1  augustss #endif
    653         1.1  augustss 	power = cdp->bMaxPower * 2;
    654         1.1  augustss 	if (power > dev->powersrc->power) {
    655        1.81  augustss 		DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
    656         1.1  augustss 		/* XXX print nicer message. */
    657         1.7  augustss 		if (msg)
    658        1.18  augustss 			printf("%s: device addr %d (config %d) exceeds power "
    659        1.18  augustss 				 "budget, %d mA > %d mA\n",
    660       1.151  drochner 			       device_xname(dev->bus->usbctl), dev->address,
    661        1.99  augustss 			       cdp->bConfigurationValue,
    662         1.7  augustss 			       power, dev->powersrc->power);
    663        1.53  augustss 		err = USBD_NO_POWER;
    664         1.1  augustss 		goto bad;
    665         1.1  augustss 	}
    666         1.1  augustss 	dev->power = power;
    667         1.1  augustss 	dev->self_powered = selfpowered;
    668         1.1  augustss 
    669        1.74  augustss 	/* Set the actual configuration value. */
    670        1.18  augustss 	DPRINTF(("usbd_set_config_index: set config %d\n",
    671        1.18  augustss 		 cdp->bConfigurationValue));
    672        1.53  augustss 	err = usbd_set_config(dev, cdp->bConfigurationValue);
    673        1.53  augustss 	if (err) {
    674        1.14  drochner 		DPRINTF(("usbd_set_config_index: setting config=%d failed, "
    675        1.39  augustss 			 "error=%s\n",
    676        1.53  augustss 			 cdp->bConfigurationValue, usbd_errstr(err)));
    677         1.1  augustss 		goto bad;
    678         1.1  augustss 	}
    679        1.74  augustss 
    680        1.74  augustss 	/* Allocate and fill interface data. */
    681         1.1  augustss 	nifc = cdp->bNumInterface;
    682        1.99  augustss 	dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
    683         1.1  augustss 			     M_USB, M_NOWAIT);
    684        1.58  augustss 	if (dev->ifaces == NULL) {
    685        1.53  augustss 		err = USBD_NOMEM;
    686         1.1  augustss 		goto bad;
    687         1.1  augustss 	}
    688        1.12  augustss 	DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
    689         1.1  augustss 	dev->cdesc = cdp;
    690         1.1  augustss 	dev->config = cdp->bConfigurationValue;
    691        1.12  augustss 	for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
    692        1.53  augustss 		err = usbd_fill_iface_data(dev, ifcidx, 0);
    693        1.53  augustss 		if (err) {
    694        1.12  augustss 			while (--ifcidx >= 0)
    695        1.12  augustss 				usbd_free_iface_data(dev, ifcidx);
    696         1.1  augustss 			goto bad;
    697         1.1  augustss 		}
    698         1.1  augustss 	}
    699         1.1  augustss 
    700         1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    701         1.1  augustss 
    702         1.1  augustss  bad:
    703         1.1  augustss 	free(cdp, M_USB);
    704        1.53  augustss 	return (err);
    705         1.1  augustss }
    706         1.1  augustss 
    707         1.1  augustss /* XXX add function for alternate settings */
    708         1.1  augustss 
    709         1.1  augustss usbd_status
    710        1.78  augustss usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
    711        1.78  augustss 		struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
    712         1.1  augustss {
    713         1.1  augustss 	usbd_pipe_handle p;
    714        1.53  augustss 	usbd_status err;
    715         1.1  augustss 
    716         1.1  augustss 	DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
    717         1.1  augustss 		    dev, iface, ep, pipe));
    718         1.1  augustss 	p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
    719        1.53  augustss 	if (p == NULL)
    720         1.1  augustss 		return (USBD_NOMEM);
    721         1.1  augustss 	p->device = dev;
    722         1.1  augustss 	p->iface = iface;
    723         1.1  augustss 	p->endpoint = ep;
    724         1.1  augustss 	ep->refcnt++;
    725         1.1  augustss 	p->refcnt = 1;
    726        1.53  augustss 	p->intrxfer = 0;
    727         1.1  augustss 	p->running = 0;
    728        1.70  augustss 	p->aborting = 0;
    729        1.34  augustss 	p->repeat = 0;
    730        1.63  augustss 	p->interval = ival;
    731         1.1  augustss 	SIMPLEQ_INIT(&p->queue);
    732        1.53  augustss 	err = dev->bus->methods->open_pipe(p);
    733        1.53  augustss 	if (err) {
    734        1.39  augustss 		DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
    735        1.39  augustss 			 "%s\n",
    736        1.53  augustss 			 ep->edesc->bEndpointAddress, usbd_errstr(err)));
    737         1.1  augustss 		free(p, M_USB);
    738        1.53  augustss 		return (err);
    739         1.1  augustss 	}
    740         1.1  augustss 	*pipe = p;
    741         1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    742         1.1  augustss }
    743         1.1  augustss 
    744         1.1  augustss /* Abort the device control pipe. */
    745         1.1  augustss void
    746        1.78  augustss usbd_kill_pipe(usbd_pipe_handle pipe)
    747         1.1  augustss {
    748        1.89  augustss 	usbd_abort_pipe(pipe);
    749         1.1  augustss 	pipe->methods->close(pipe);
    750         1.1  augustss 	pipe->endpoint->refcnt--;
    751         1.1  augustss 	free(pipe, M_USB);
    752         1.1  augustss }
    753         1.1  augustss 
    754         1.1  augustss int
    755        1.78  augustss usbd_getnewaddr(usbd_bus_handle bus)
    756         1.1  augustss {
    757        1.18  augustss 	int addr;
    758         1.1  augustss 
    759        1.18  augustss 	for (addr = 1; addr < USB_MAX_DEVICES; addr++)
    760        1.18  augustss 		if (bus->devices[addr] == 0)
    761        1.18  augustss 			return (addr);
    762         1.1  augustss 	return (-1);
    763         1.1  augustss }
    764         1.1  augustss 
    765       1.155  drochner usbd_status
    766       1.155  drochner usbd_attach_roothub(device_t parent, usbd_device_handle dev)
    767       1.155  drochner {
    768       1.155  drochner 	struct usb_attach_arg uaa;
    769       1.155  drochner 	usb_device_descriptor_t *dd = &dev->ddesc;
    770       1.155  drochner 	device_t dv;
    771       1.155  drochner 
    772       1.155  drochner 	uaa.device = dev;
    773       1.155  drochner 	uaa.usegeneric = 0;
    774       1.155  drochner 	uaa.port = 0;
    775       1.155  drochner 	uaa.vendor = UGETW(dd->idVendor);
    776       1.155  drochner 	uaa.product = UGETW(dd->idProduct);
    777       1.155  drochner 	uaa.release = UGETW(dd->bcdDevice);
    778       1.155  drochner 	uaa.class = dd->bDeviceClass;
    779       1.155  drochner 	uaa.subclass = dd->bDeviceSubClass;
    780       1.155  drochner 	uaa.proto = dd->bDeviceProtocol;
    781       1.155  drochner 
    782       1.156  drochner 	dv = config_found_ia(parent, "usbroothubif", &uaa, 0);
    783       1.155  drochner 	if (dv) {
    784       1.155  drochner 		dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
    785       1.155  drochner 		if (dev->subdevs == NULL)
    786       1.155  drochner 			return (USBD_NOMEM);
    787       1.155  drochner 		dev->subdevs[0] = dv;
    788       1.155  drochner 		dev->subdevlen = 1;
    789       1.155  drochner 	}
    790       1.155  drochner 	return (USBD_NORMAL_COMPLETION);
    791       1.155  drochner }
    792        1.18  augustss 
    793       1.160  drochner static usbd_status
    794       1.160  drochner usbd_attachwholedevice(device_t parent, usbd_device_handle dev, int port,
    795       1.160  drochner 	int usegeneric)
    796        1.18  augustss {
    797        1.18  augustss 	struct usb_attach_arg uaa;
    798        1.18  augustss 	usb_device_descriptor_t *dd = &dev->ddesc;
    799       1.160  drochner 	device_t dv;
    800       1.158  drochner 	int dlocs[USBDEVIFCF_NLOCS];
    801        1.18  augustss 
    802        1.18  augustss 	uaa.device = dev;
    803       1.160  drochner 	uaa.usegeneric = usegeneric;
    804        1.18  augustss 	uaa.port = port;
    805        1.32  augustss 	uaa.vendor = UGETW(dd->idVendor);
    806        1.32  augustss 	uaa.product = UGETW(dd->idProduct);
    807        1.32  augustss 	uaa.release = UGETW(dd->bcdDevice);
    808       1.144  drochner 	uaa.class = dd->bDeviceClass;
    809       1.144  drochner 	uaa.subclass = dd->bDeviceSubClass;
    810       1.144  drochner 	uaa.proto = dd->bDeviceProtocol;
    811        1.18  augustss 
    812       1.158  drochner 	dlocs[USBDEVIFCF_PORT] = uaa.port;
    813       1.158  drochner 	dlocs[USBDEVIFCF_VENDOR] = uaa.vendor;
    814       1.158  drochner 	dlocs[USBDEVIFCF_PRODUCT] = uaa.product;
    815       1.158  drochner 	dlocs[USBDEVIFCF_RELEASE] = uaa.release;
    816       1.158  drochner 	/* the rest is historical ballast */
    817       1.158  drochner 	dlocs[USBDEVIFCF_CONFIGURATION] = -1;
    818       1.158  drochner 	dlocs[USBDEVIFCF_INTERFACE] = -1;
    819       1.158  drochner 
    820       1.158  drochner 	dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
    821       1.160  drochner 				 config_stdsubmatch);
    822        1.34  augustss 	if (dv) {
    823       1.155  drochner 		dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
    824        1.57  augustss 		if (dev->subdevs == NULL)
    825        1.34  augustss 			return (USBD_NOMEM);
    826        1.34  augustss 		dev->subdevs[0] = dv;
    827       1.155  drochner 		dev->subdevlen = 1;
    828       1.160  drochner 		dev->nifaces_claimed = 1; /* XXX */
    829        1.34  augustss 	}
    830       1.160  drochner 	return (USBD_NORMAL_COMPLETION);
    831       1.160  drochner }
    832        1.18  augustss 
    833       1.160  drochner static usbd_status
    834       1.162      kent usbd_attachinterfaces(device_t parent, usbd_device_handle dev,
    835       1.162      kent 		      int port, const int *locators)
    836       1.160  drochner {
    837       1.160  drochner 	struct usbif_attach_arg uiaa;
    838       1.162      kent 	int ilocs[USBIFIFCF_NLOCS];
    839       1.160  drochner 	usb_device_descriptor_t *dd = &dev->ddesc;
    840       1.160  drochner 	int nifaces;
    841       1.160  drochner 	usbd_interface_handle *ifaces;
    842       1.162      kent 	int i, j, loc;
    843       1.160  drochner 	device_t dv;
    844       1.160  drochner 
    845       1.160  drochner 	nifaces = dev->cdesc->bNumInterface;
    846       1.160  drochner 	ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT|M_ZERO);
    847       1.160  drochner 	if (!ifaces)
    848       1.160  drochner 		return (USBD_NOMEM);
    849       1.160  drochner 	for (i = 0; i < nifaces; i++)
    850       1.160  drochner 		if (!dev->subdevs[i])
    851       1.160  drochner 			ifaces[i] = &dev->ifaces[i];
    852        1.18  augustss 
    853       1.144  drochner 	uiaa.device = dev;
    854       1.144  drochner 	uiaa.port = port;
    855       1.144  drochner 	uiaa.vendor = UGETW(dd->idVendor);
    856       1.144  drochner 	uiaa.product = UGETW(dd->idProduct);
    857       1.144  drochner 	uiaa.release = UGETW(dd->bcdDevice);
    858       1.160  drochner 	uiaa.configno = dev->cdesc->bConfigurationValue;
    859       1.160  drochner 	uiaa.ifaces = ifaces;
    860       1.160  drochner 	uiaa.nifaces = nifaces;
    861       1.158  drochner 	ilocs[USBIFIFCF_PORT] = uiaa.port;
    862       1.158  drochner 	ilocs[USBIFIFCF_VENDOR] = uiaa.vendor;
    863       1.158  drochner 	ilocs[USBIFIFCF_PRODUCT] = uiaa.product;
    864       1.158  drochner 	ilocs[USBIFIFCF_RELEASE] = uiaa.release;
    865       1.160  drochner 	ilocs[USBIFIFCF_CONFIGURATION] = uiaa.configno;
    866       1.160  drochner 
    867       1.160  drochner 	for (i = 0; i < nifaces; i++) {
    868       1.160  drochner 		if (!ifaces[i])
    869       1.160  drochner 			continue; /* interface already claimed */
    870       1.160  drochner 		uiaa.iface = ifaces[i];
    871       1.160  drochner 		uiaa.class = ifaces[i]->idesc->bInterfaceClass;
    872       1.160  drochner 		uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass;
    873       1.160  drochner 		uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol;
    874       1.160  drochner 		uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
    875       1.160  drochner 		ilocs[USBIFIFCF_INTERFACE] = uiaa.ifaceno;
    876       1.162      kent 		if (locators != NULL) {
    877       1.162      kent 			loc = locators[USBIFIFCF_CONFIGURATION];
    878       1.162      kent 			if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
    879       1.162      kent 			    loc != uiaa.configno)
    880       1.162      kent 				continue;
    881       1.162      kent 			loc = locators[USBIFIFCF_INTERFACE];
    882       1.162      kent 			if (loc != USBIFIFCF_INTERFACE && loc != uiaa.ifaceno)
    883       1.162      kent 				continue;
    884       1.162      kent 		}
    885       1.160  drochner 		dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
    886       1.160  drochner 					 usbd_ifprint, config_stdsubmatch);
    887       1.160  drochner 		if (!dv)
    888       1.160  drochner 			continue;
    889       1.160  drochner 		ifaces[i] = 0; /* claim */
    890       1.160  drochner 		/* account for ifaces claimed by the driver behind our back */
    891       1.160  drochner 		for (j = 0; j < nifaces; j++) {
    892       1.160  drochner 			if (!ifaces[j] && !dev->subdevs[j]) {
    893       1.160  drochner 				dev->subdevs[j] = dv;
    894       1.160  drochner 				dev->nifaces_claimed++;
    895       1.160  drochner 			}
    896       1.160  drochner 		}
    897       1.160  drochner 	}
    898       1.160  drochner 
    899       1.160  drochner 	free(ifaces, M_USB);
    900       1.160  drochner 	return (USBD_NORMAL_COMPLETION);
    901       1.160  drochner }
    902       1.160  drochner 
    903       1.160  drochner usbd_status
    904       1.160  drochner usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
    905       1.160  drochner 		      int port, int addr)
    906       1.160  drochner {
    907       1.160  drochner 	usb_device_descriptor_t *dd = &dev->ddesc;
    908       1.160  drochner 	int confi, nifaces;
    909       1.160  drochner 	usbd_status err;
    910       1.160  drochner 
    911       1.160  drochner 	/* First try with device specific drivers. */
    912       1.160  drochner 	DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
    913       1.160  drochner 	err = usbd_attachwholedevice(parent, dev, port, 0);
    914       1.160  drochner 	if (dev->nifaces_claimed || err)
    915       1.160  drochner 		return (err);
    916       1.160  drochner 	DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
    917       1.158  drochner 
    918        1.60  augustss 	DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
    919        1.60  augustss 		 dd->bNumConfigurations));
    920        1.18  augustss 	for (confi = 0; confi < dd->bNumConfigurations; confi++) {
    921        1.18  augustss 		DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
    922        1.18  augustss 			    confi));
    923        1.53  augustss 		err = usbd_set_config_index(dev, confi, 1);
    924        1.53  augustss 		if (err) {
    925        1.18  augustss #ifdef USB_DEBUG
    926        1.18  augustss 			DPRINTF(("%s: port %d, set config at addr %d failed, "
    927        1.43  augustss 				 "error=%s\n", USBDEVPTRNAME(parent), port,
    928        1.53  augustss 				 addr, usbd_errstr(err)));
    929        1.18  augustss #else
    930        1.18  augustss 			printf("%s: port %d, set config at addr %d failed\n",
    931        1.43  augustss 			       USBDEVPTRNAME(parent), port, addr);
    932        1.18  augustss #endif
    933       1.162      kent 			return (err);
    934        1.18  augustss 		}
    935        1.20  augustss 		nifaces = dev->cdesc->bNumInterface;
    936       1.160  drochner 		dev->subdevs = malloc(nifaces * sizeof(device_t), M_USB,
    937       1.155  drochner 				      M_NOWAIT|M_ZERO);
    938       1.160  drochner 		if (dev->subdevs == NULL)
    939        1.34  augustss 			return (USBD_NOMEM);
    940       1.155  drochner 		dev->subdevlen = nifaces;
    941        1.52  augustss 
    942       1.162      kent 		err = usbd_attachinterfaces(parent, dev, port, NULL);
    943       1.160  drochner 
    944       1.160  drochner 		if (!dev->nifaces_claimed) {
    945       1.160  drochner 			free(dev->subdevs, M_USB);
    946       1.160  drochner 			dev->subdevs = 0;
    947       1.160  drochner 			dev->subdevlen = 0;
    948        1.18  augustss 		}
    949       1.160  drochner 		if (dev->nifaces_claimed || err)
    950       1.160  drochner 			return (err);
    951        1.18  augustss 	}
    952        1.21  augustss 	/* No interfaces were attached in any of the configurations. */
    953        1.34  augustss 
    954        1.34  augustss 	if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
    955        1.18  augustss 		usbd_set_config_index(dev, 0, 0);
    956        1.18  augustss 
    957        1.18  augustss 	DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
    958        1.18  augustss 
    959        1.18  augustss 	/* Finally try the generic driver. */
    960       1.160  drochner 	err = usbd_attachwholedevice(parent, dev, port, 1);
    961        1.18  augustss 
    962        1.99  augustss 	/*
    963        1.18  augustss 	 * The generic attach failed, but leave the device as it is.
    964        1.18  augustss 	 * We just did not find any drivers, that's all.  The device is
    965        1.18  augustss 	 * fully operational and not harming anyone.
    966        1.18  augustss 	 */
    967        1.18  augustss 	DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
    968       1.162      kent 	return (USBD_NORMAL_COMPLETION);
    969        1.18  augustss }
    970        1.18  augustss 
    971       1.162      kent /**
    972       1.162      kent  * Called from uhub_rescan().  usbd_new_device() for the target dev must be
    973       1.162      kent  * called before calling this.
    974       1.162      kent  */
    975       1.162      kent usbd_status
    976       1.162      kent usbd_reattach_device(device_ptr_t parent, usbd_device_handle dev,
    977       1.162      kent 		     int port, const int *locators)
    978       1.162      kent {
    979       1.162      kent 	int i, loc;
    980       1.162      kent 
    981       1.162      kent 	if (locators != NULL) {
    982       1.162      kent 		loc = locators[USBIFIFCF_PORT];
    983       1.162      kent 		if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
    984       1.162      kent 			return USBD_NORMAL_COMPLETION;
    985       1.162      kent 		loc = locators[USBIFIFCF_VENDOR];
    986       1.162      kent 		if (loc != USBIFIFCF_VENDOR_DEFAULT &&
    987       1.162      kent 		    loc != UGETW(dev->ddesc.idVendor))
    988       1.162      kent 			return USBD_NORMAL_COMPLETION;
    989       1.162      kent 		loc = locators[USBIFIFCF_PRODUCT];
    990       1.162      kent 		if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
    991       1.162      kent 		    loc != UGETW(dev->ddesc.idProduct))
    992       1.162      kent 			return USBD_NORMAL_COMPLETION;
    993       1.162      kent 		loc = locators[USBIFIFCF_RELEASE];
    994       1.162      kent 		if (loc != USBIFIFCF_RELEASE_DEFAULT &&
    995       1.162      kent 		    loc != UGETW(dev->ddesc.bcdDevice))
    996       1.162      kent 			return USBD_NORMAL_COMPLETION;
    997       1.162      kent 	}
    998       1.162      kent 	if (dev->subdevlen == 0) {
    999       1.162      kent 		/* XXX: check USBIFIFCF_CONFIGURATION and
   1000       1.162      kent 		 * USBIFIFCF_INTERFACE too */
   1001       1.162      kent 		return usbd_probe_and_attach(parent, dev, port, dev->address);
   1002       1.162      kent 	} else if (dev->subdevlen != dev->cdesc->bNumInterface) {
   1003       1.162      kent 		/* device-specific or generic driver is already attached. */
   1004       1.162      kent 		return USBD_NORMAL_COMPLETION;
   1005       1.162      kent 	}
   1006       1.162      kent 	/* Does the device have unconfigured interfaces? */
   1007       1.162      kent 	for (i = 0; i < dev->subdevlen; i++) {
   1008       1.162      kent 		if (dev->subdevs[i] == NULL) {
   1009       1.162      kent 			break;
   1010       1.162      kent 		}
   1011       1.162      kent 	}
   1012       1.162      kent 	if (i >= dev->subdevlen)
   1013       1.162      kent 		return USBD_NORMAL_COMPLETION;
   1014       1.162      kent 	return usbd_attachinterfaces(parent, dev, port, locators);
   1015       1.162      kent }
   1016        1.18  augustss 
   1017         1.1  augustss /*
   1018  1.162.12.1      matt  * Get the first 8 bytes of the device descriptor.
   1019  1.162.12.1      matt  * Do as Windows does: try to read 64 bytes -- there are devices which
   1020  1.162.12.1      matt  * recognize the initial descriptor fetch (before the control endpoint's
   1021  1.162.12.1      matt  * MaxPacketSize is known by the host) by exactly this length.
   1022  1.162.12.1      matt  */
   1023  1.162.12.1      matt usbd_status
   1024  1.162.12.1      matt usbd_get_initial_ddesc(usbd_device_handle dev, usb_device_descriptor_t *desc)
   1025  1.162.12.1      matt {
   1026  1.162.12.1      matt 	usb_device_request_t req;
   1027  1.162.12.1      matt 	char buf[64];
   1028  1.162.12.1      matt 	int res, actlen;
   1029  1.162.12.1      matt 
   1030  1.162.12.1      matt 	req.bmRequestType = UT_READ_DEVICE;
   1031  1.162.12.1      matt 	req.bRequest = UR_GET_DESCRIPTOR;
   1032  1.162.12.1      matt 	USETW2(req.wValue, UDESC_DEVICE, 0);
   1033  1.162.12.1      matt 	USETW(req.wIndex, 0);
   1034  1.162.12.1      matt 	USETW(req.wLength, 64);
   1035  1.162.12.1      matt 	res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
   1036  1.162.12.1      matt 		&actlen, USBD_DEFAULT_TIMEOUT);
   1037  1.162.12.1      matt 	if (res)
   1038  1.162.12.1      matt 		return res;
   1039  1.162.12.1      matt 	if (actlen < 8)
   1040  1.162.12.1      matt 		return USBD_SHORT_XFER;
   1041  1.162.12.1      matt 	memcpy(desc, buf, 8);
   1042  1.162.12.1      matt 	return USBD_NORMAL_COMPLETION;
   1043  1.162.12.1      matt }
   1044  1.162.12.1      matt 
   1045  1.162.12.1      matt /*
   1046         1.1  augustss  * Called when a new device has been put in the powered state,
   1047         1.1  augustss  * but not yet in the addressed state.
   1048         1.1  augustss  * Get initial descriptor, set the address, get full descriptor,
   1049         1.1  augustss  * and attach a driver.
   1050         1.1  augustss  */
   1051         1.1  augustss usbd_status
   1052        1.78  augustss usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
   1053        1.94  augustss 		int speed, int port, struct usbd_port *up)
   1054         1.1  augustss {
   1055       1.120  augustss 	usbd_device_handle dev, adev;
   1056        1.94  augustss 	struct usbd_device *hub;
   1057        1.18  augustss 	usb_device_descriptor_t *dd;
   1058       1.107  augustss 	usb_port_status_t ps;
   1059        1.53  augustss 	usbd_status err;
   1060         1.1  augustss 	int addr;
   1061        1.18  augustss 	int i;
   1062       1.120  augustss 	int p;
   1063         1.1  augustss 
   1064        1.94  augustss 	DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
   1065        1.94  augustss 		 bus, port, depth, speed));
   1066  1.162.12.1      matt 
   1067  1.162.12.1      matt 	if (bus->methods->new_device != NULL)
   1068  1.162.12.1      matt 		return (bus->methods->new_device)(parent, bus, depth, speed,
   1069  1.162.12.1      matt 		    port, up);
   1070  1.162.12.1      matt 
   1071         1.1  augustss 	addr = usbd_getnewaddr(bus);
   1072         1.1  augustss 	if (addr < 0) {
   1073        1.99  augustss 		printf("%s: No free USB addresses, new device ignored.\n",
   1074       1.151  drochner 		       device_xname(bus->usbctl));
   1075         1.1  augustss 		return (USBD_NO_ADDR);
   1076         1.1  augustss 	}
   1077         1.1  augustss 
   1078        1.97   tsutsui 	dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
   1079        1.53  augustss 	if (dev == NULL)
   1080         1.1  augustss 		return (USBD_NOMEM);
   1081         1.1  augustss 
   1082         1.1  augustss 	dev->bus = bus;
   1083         1.1  augustss 
   1084         1.1  augustss 	/* Set up default endpoint handle. */
   1085         1.1  augustss 	dev->def_ep.edesc = &dev->def_ep_desc;
   1086         1.1  augustss 
   1087         1.1  augustss 	/* Set up default endpoint descriptor. */
   1088         1.1  augustss 	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
   1089         1.1  augustss 	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
   1090         1.1  augustss 	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
   1091         1.1  augustss 	dev->def_ep_desc.bmAttributes = UE_CONTROL;
   1092         1.1  augustss 	USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
   1093         1.1  augustss 	dev->def_ep_desc.bInterval = 0;
   1094         1.1  augustss 
   1095         1.1  augustss 	dev->quirks = &usbd_no_quirk;
   1096         1.1  augustss 	dev->address = USB_START_ADDR;
   1097         1.4  augustss 	dev->ddesc.bMaxPacketSize = 0;
   1098         1.1  augustss 	dev->depth = depth;
   1099         1.1  augustss 	dev->powersrc = up;
   1100        1.94  augustss 	dev->myhub = up->parent;
   1101       1.120  augustss 
   1102       1.120  augustss 	up->device = dev;
   1103       1.120  augustss 
   1104       1.120  augustss 	/* Locate port on upstream high speed hub */
   1105       1.120  augustss 	for (adev = dev, hub = up->parent;
   1106        1.94  augustss 	     hub != NULL && hub->speed != USB_SPEED_HIGH;
   1107       1.120  augustss 	     adev = hub, hub = hub->myhub)
   1108        1.94  augustss 		;
   1109       1.120  augustss 	if (hub) {
   1110       1.120  augustss 		for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
   1111       1.120  augustss 			if (hub->hub->ports[p].device == adev) {
   1112       1.120  augustss 				dev->myhsport = &hub->hub->ports[p];
   1113       1.120  augustss 				goto found;
   1114       1.120  augustss 			}
   1115       1.120  augustss 		}
   1116       1.120  augustss 		panic("usbd_new_device: cannot find HS port\n");
   1117       1.120  augustss 	found:
   1118       1.120  augustss 		DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
   1119       1.120  augustss 	} else {
   1120       1.120  augustss 		dev->myhsport = NULL;
   1121       1.120  augustss 	}
   1122        1.94  augustss 	dev->speed = speed;
   1123        1.22  augustss 	dev->langid = USBD_NOLANG;
   1124        1.50  augustss 	dev->cookie.cookie = ++usb_cookie_no;
   1125         1.1  augustss 
   1126        1.67     soren 	/* Establish the default pipe. */
   1127        1.63  augustss 	err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
   1128        1.63  augustss 			      &dev->default_pipe);
   1129        1.53  augustss 	if (err) {
   1130        1.18  augustss 		usbd_remove_device(dev, up);
   1131        1.53  augustss 		return (err);
   1132        1.18  augustss 	}
   1133         1.1  augustss 
   1134        1.18  augustss 	dd = &dev->ddesc;
   1135         1.6  augustss 	/* Try a few times in case the device is slow (i.e. outside specs.) */
   1136       1.107  augustss 	for (i = 0; i < 10; i++) {
   1137         1.6  augustss 		/* Get the first 8 bytes of the device descriptor. */
   1138        1.53  augustss 		err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
   1139        1.53  augustss 		if (!err)
   1140         1.6  augustss 			break;
   1141        1.23  augustss 		usbd_delay_ms(dev, 200);
   1142       1.107  augustss 		if ((i & 3) == 3)
   1143       1.107  augustss 			usbd_reset_port(up->parent, port, &ps);
   1144         1.6  augustss 	}
   1145        1.53  augustss 	if (err) {
   1146        1.14  drochner 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
   1147        1.53  augustss 			      "failed\n", addr));
   1148        1.18  augustss 		usbd_remove_device(dev, up);
   1149        1.53  augustss 		return (err);
   1150        1.95  augustss 	}
   1151        1.95  augustss 
   1152        1.95  augustss 	if (speed == USB_SPEED_HIGH) {
   1153        1.95  augustss 		/* Max packet size must be 64 (sec 5.5.3). */
   1154        1.95  augustss 		if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
   1155        1.95  augustss #ifdef DIAGNOSTIC
   1156        1.95  augustss 			printf("usbd_new_device: addr=%d bad max packet size\n",
   1157        1.95  augustss 			       addr);
   1158        1.95  augustss #endif
   1159        1.95  augustss 			dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
   1160        1.95  augustss 		}
   1161        1.18  augustss 	}
   1162        1.18  augustss 
   1163        1.39  augustss 	DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
   1164        1.99  augustss 		 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
   1165        1.39  augustss 		 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
   1166        1.99  augustss 		 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
   1167        1.94  augustss 		 dev->speed));
   1168        1.39  augustss 
   1169        1.18  augustss 	if (dd->bDescriptorType != UDESC_DEVICE) {
   1170        1.18  augustss 		/* Illegal device descriptor */
   1171        1.18  augustss 		DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
   1172        1.18  augustss 			     dd->bDescriptorType));
   1173        1.18  augustss 		usbd_remove_device(dev, up);
   1174        1.18  augustss 		return (USBD_INVAL);
   1175         1.1  augustss 	}
   1176         1.1  augustss 
   1177        1.39  augustss 	if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
   1178        1.39  augustss 		DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
   1179        1.39  augustss 		usbd_remove_device(dev, up);
   1180        1.39  augustss 		return (USBD_INVAL);
   1181        1.39  augustss 	}
   1182         1.4  augustss 
   1183        1.18  augustss 	USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
   1184         1.1  augustss 
   1185        1.62  augustss 	err = usbd_reload_device_desc(dev);
   1186        1.53  augustss 	if (err) {
   1187        1.14  drochner 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
   1188        1.14  drochner 			      "failed\n", addr));
   1189        1.18  augustss 		usbd_remove_device(dev, up);
   1190        1.53  augustss 		return (err);
   1191         1.1  augustss 	}
   1192         1.1  augustss 
   1193       1.159  jmcneill 	/* Set the address */
   1194       1.159  jmcneill 	DPRINTFN(5, ("usbd_new_device: setting device address=%d\n", addr));
   1195       1.159  jmcneill 	err = usbd_set_address(dev, addr);
   1196       1.159  jmcneill 	if (err) {
   1197       1.159  jmcneill 		DPRINTFN(-1, ("usbd_new_device: set address %d failed\n", addr));
   1198       1.159  jmcneill 		err = USBD_SET_ADDR_FAILED;
   1199       1.159  jmcneill 		usbd_remove_device(dev, up);
   1200       1.159  jmcneill 		return err;
   1201       1.159  jmcneill 	}
   1202       1.159  jmcneill 
   1203       1.159  jmcneill 	/* Allow device time to set new address */
   1204       1.159  jmcneill 	usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
   1205       1.159  jmcneill 	dev->address = addr;	/* new device address now */
   1206       1.159  jmcneill 	bus->devices[addr] = dev;
   1207       1.159  jmcneill 
   1208       1.159  jmcneill 	/* Re-establish the default pipe with the new address. */
   1209       1.159  jmcneill 	usbd_kill_pipe(dev->default_pipe);
   1210       1.159  jmcneill 	err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
   1211       1.159  jmcneill 	    &dev->default_pipe);
   1212       1.159  jmcneill 	if (err) {
   1213       1.159  jmcneill 		DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n"));
   1214       1.159  jmcneill 		usbd_remove_device(dev, up);
   1215       1.159  jmcneill 		return err;
   1216       1.159  jmcneill 	}
   1217       1.159  jmcneill 
   1218         1.1  augustss 	/* Assume 100mA bus powered for now. Changed when configured. */
   1219         1.1  augustss 	dev->power = USB_MIN_POWER;
   1220         1.1  augustss 	dev->self_powered = 0;
   1221         1.1  augustss 
   1222        1.99  augustss 	DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
   1223         1.1  augustss 		 addr, dev, parent));
   1224        1.99  augustss 
   1225        1.65  augustss 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
   1226         1.1  augustss 
   1227       1.155  drochner 	if (port == 0) { /* root hub */
   1228       1.155  drochner 		KASSERT(addr == 1);
   1229       1.155  drochner 		usbd_attach_roothub(parent, dev);
   1230       1.155  drochner 		return (USBD_NORMAL_COMPLETION);
   1231       1.155  drochner 	}
   1232       1.155  drochner 
   1233        1.53  augustss 	err = usbd_probe_and_attach(parent, dev, port, addr);
   1234        1.53  augustss 	if (err) {
   1235        1.18  augustss 		usbd_remove_device(dev, up);
   1236        1.53  augustss 		return (err);
   1237        1.18  augustss   	}
   1238        1.65  augustss 
   1239        1.18  augustss   	return (USBD_NORMAL_COMPLETION);
   1240        1.62  augustss }
   1241        1.62  augustss 
   1242        1.62  augustss usbd_status
   1243        1.78  augustss usbd_reload_device_desc(usbd_device_handle dev)
   1244        1.62  augustss {
   1245        1.62  augustss 	usbd_status err;
   1246        1.62  augustss 
   1247        1.62  augustss 	/* Get the full device descriptor. */
   1248        1.62  augustss 	err = usbd_get_device_desc(dev, &dev->ddesc);
   1249        1.62  augustss 	if (err)
   1250        1.62  augustss 		return (err);
   1251        1.62  augustss 
   1252        1.62  augustss 	/* Figure out what's wrong with this device. */
   1253        1.62  augustss 	dev->quirks = usbd_find_quirk(&dev->ddesc);
   1254        1.62  augustss 
   1255        1.62  augustss 	return (USBD_NORMAL_COMPLETION);
   1256        1.18  augustss }
   1257         1.1  augustss 
   1258        1.18  augustss void
   1259        1.78  augustss usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
   1260        1.18  augustss {
   1261        1.18  augustss 	DPRINTF(("usbd_remove_device: %p\n", dev));
   1262        1.99  augustss 
   1263        1.53  augustss 	if (dev->default_pipe != NULL)
   1264        1.18  augustss 		usbd_kill_pipe(dev->default_pipe);
   1265       1.120  augustss 	up->device = NULL;
   1266       1.120  augustss 	dev->bus->devices[dev->address] = NULL;
   1267         1.1  augustss 
   1268         1.1  augustss 	free(dev, M_USB);
   1269         1.1  augustss }
   1270         1.1  augustss 
   1271         1.1  augustss int
   1272        1.78  augustss usbd_print(void *aux, const char *pnp)
   1273         1.1  augustss {
   1274         1.1  augustss 	struct usb_attach_arg *uaa = aux;
   1275         1.1  augustss 
   1276         1.1  augustss 	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
   1277         1.1  augustss 	if (pnp) {
   1278       1.134  christos #define USB_DEVINFO 1024
   1279       1.134  christos 		char *devinfo;
   1280         1.1  augustss 		if (!uaa->usegeneric)
   1281         1.1  augustss 			return (QUIET);
   1282       1.134  christos 		devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
   1283       1.134  christos 		usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
   1284       1.101   thorpej 		aprint_normal("%s, %s", devinfo, pnp);
   1285       1.134  christos 		free(devinfo, M_TEMP);
   1286         1.1  augustss 	}
   1287       1.155  drochner 	aprint_normal(" port %d", uaa->port);
   1288       1.144  drochner #if 0
   1289       1.144  drochner 	/*
   1290       1.144  drochner 	 * It gets very crowded with these locators on the attach line.
   1291       1.144  drochner 	 * They are not really needed since they are printed in the clear
   1292       1.144  drochner 	 * by each driver.
   1293       1.144  drochner 	 */
   1294       1.144  drochner 	if (uaa->vendor != UHUB_UNK_VENDOR)
   1295       1.144  drochner 		aprint_normal(" vendor 0x%04x", uaa->vendor);
   1296       1.144  drochner 	if (uaa->product != UHUB_UNK_PRODUCT)
   1297       1.144  drochner 		aprint_normal(" product 0x%04x", uaa->product);
   1298       1.144  drochner 	if (uaa->release != UHUB_UNK_RELEASE)
   1299       1.144  drochner 		aprint_normal(" release 0x%04x", uaa->release);
   1300       1.144  drochner #endif
   1301       1.144  drochner 	return (UNCONF);
   1302       1.144  drochner }
   1303       1.144  drochner 
   1304       1.144  drochner int
   1305       1.144  drochner usbd_ifprint(void *aux, const char *pnp)
   1306       1.144  drochner {
   1307       1.144  drochner 	struct usbif_attach_arg *uaa = aux;
   1308       1.144  drochner 
   1309       1.144  drochner 	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
   1310       1.144  drochner 	if (pnp)
   1311       1.144  drochner 		return (QUIET);
   1312       1.155  drochner 	aprint_normal(" port %d", uaa->port);
   1313       1.156  drochner 	aprint_normal(" configuration %d", uaa->configno);
   1314       1.156  drochner 	aprint_normal(" interface %d", uaa->ifaceno);
   1315        1.32  augustss #if 0
   1316        1.99  augustss 	/*
   1317        1.32  augustss 	 * It gets very crowded with these locators on the attach line.
   1318        1.32  augustss 	 * They are not really needed since they are printed in the clear
   1319        1.32  augustss 	 * by each driver.
   1320        1.32  augustss 	 */
   1321        1.32  augustss 	if (uaa->vendor != UHUB_UNK_VENDOR)
   1322       1.101   thorpej 		aprint_normal(" vendor 0x%04x", uaa->vendor);
   1323        1.32  augustss 	if (uaa->product != UHUB_UNK_PRODUCT)
   1324       1.101   thorpej 		aprint_normal(" product 0x%04x", uaa->product);
   1325        1.32  augustss 	if (uaa->release != UHUB_UNK_RELEASE)
   1326       1.101   thorpej 		aprint_normal(" release 0x%04x", uaa->release);
   1327        1.32  augustss #endif
   1328         1.1  augustss 	return (UNCONF);
   1329         1.1  augustss }
   1330         1.1  augustss 
   1331        1.13  augustss void
   1332        1.82  augustss usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
   1333        1.82  augustss 		     int usedev)
   1334        1.13  augustss {
   1335        1.13  augustss 	struct usbd_port *p;
   1336       1.155  drochner 	int i, j, err, s;
   1337        1.13  augustss 
   1338       1.151  drochner 	di->udi_bus = device_unit(dev->bus->usbctl);
   1339        1.98  christos 	di->udi_addr = dev->address;
   1340        1.98  christos 	di->udi_cookie = dev->cookie;
   1341       1.139     pavel 	usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 1);
   1342       1.113    itojun 	usbd_printBCD(di->udi_release, sizeof(di->udi_release),
   1343       1.113    itojun 	    UGETW(dev->ddesc.bcdDevice));
   1344       1.123  augustss 	di->udi_serial[0] = 0;
   1345       1.123  augustss 	if (usedev)
   1346       1.123  augustss 		(void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
   1347       1.123  augustss 				      di->udi_serial);
   1348        1.98  christos 	di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
   1349        1.98  christos 	di->udi_productNo = UGETW(dev->ddesc.idProduct);
   1350        1.98  christos 	di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
   1351        1.98  christos 	di->udi_class = dev->ddesc.bDeviceClass;
   1352        1.98  christos 	di->udi_subclass = dev->ddesc.bDeviceSubClass;
   1353        1.98  christos 	di->udi_protocol = dev->ddesc.bDeviceProtocol;
   1354        1.98  christos 	di->udi_config = dev->config;
   1355        1.98  christos 	di->udi_power = dev->self_powered ? 0 : dev->power;
   1356        1.98  christos 	di->udi_speed = dev->speed;
   1357        1.65  augustss 
   1358       1.155  drochner 	if (dev->subdevlen > 0) {
   1359       1.155  drochner 		for (i = 0, j = 0; i < dev->subdevlen &&
   1360       1.155  drochner 			     j < USB_MAX_DEVNAMES; i++) {
   1361       1.155  drochner 			if (!dev->subdevs[i])
   1362       1.155  drochner 				continue;
   1363       1.155  drochner 			strncpy(di->udi_devnames[j], USBDEVPTRNAME(dev->subdevs[i]),
   1364        1.65  augustss 				USB_MAX_DEVNAMELEN);
   1365       1.155  drochner 			di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
   1366       1.155  drochner 			j++;
   1367        1.65  augustss                 }
   1368        1.65  augustss         } else {
   1369       1.155  drochner                 j = 0;
   1370        1.65  augustss         }
   1371       1.155  drochner         for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
   1372       1.155  drochner                 di->udi_devnames[j][0] = 0;                 /* empty */
   1373        1.65  augustss 
   1374        1.13  augustss 	if (dev->hub) {
   1375        1.99  augustss 		for (i = 0;
   1376        1.98  christos 		     i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
   1377        1.13  augustss 			     i < dev->hub->hubdesc.bNbrPorts;
   1378        1.13  augustss 		     i++) {
   1379        1.13  augustss 			p = &dev->hub->ports[i];
   1380        1.13  augustss 			if (p->device)
   1381        1.56  augustss 				err = p->device->address;
   1382        1.13  augustss 			else {
   1383        1.13  augustss 				s = UGETW(p->status.wPortStatus);
   1384        1.13  augustss 				if (s & UPS_PORT_ENABLED)
   1385        1.56  augustss 					err = USB_PORT_ENABLED;
   1386        1.13  augustss 				else if (s & UPS_SUSPEND)
   1387        1.56  augustss 					err = USB_PORT_SUSPENDED;
   1388        1.13  augustss 				else if (s & UPS_PORT_POWER)
   1389        1.56  augustss 					err = USB_PORT_POWERED;
   1390        1.13  augustss 				else
   1391        1.56  augustss 					err = USB_PORT_DISABLED;
   1392        1.13  augustss 			}
   1393        1.98  christos 			di->udi_ports[i] = err;
   1394        1.13  augustss 		}
   1395        1.98  christos 		di->udi_nports = dev->hub->hubdesc.bNbrPorts;
   1396        1.13  augustss 	} else
   1397        1.98  christos 		di->udi_nports = 0;
   1398        1.34  augustss }
   1399        1.34  augustss 
   1400       1.139     pavel #ifdef COMPAT_30
   1401       1.139     pavel void
   1402       1.139     pavel usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
   1403       1.139     pavel                          int usedev)
   1404       1.139     pavel {
   1405       1.139     pavel 	struct usbd_port *p;
   1406       1.155  drochner 	int i, j, err, s;
   1407       1.139     pavel 
   1408       1.151  drochner 	di->udi_bus = device_unit(dev->bus->usbctl);
   1409       1.139     pavel 	di->udi_addr = dev->address;
   1410       1.139     pavel 	di->udi_cookie = dev->cookie;
   1411       1.139     pavel 	usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 0);
   1412       1.139     pavel 	usbd_printBCD(di->udi_release, sizeof(di->udi_release),
   1413       1.139     pavel 	    UGETW(dev->ddesc.bcdDevice));
   1414       1.139     pavel 	di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
   1415       1.139     pavel 	di->udi_productNo = UGETW(dev->ddesc.idProduct);
   1416       1.139     pavel 	di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
   1417       1.139     pavel 	di->udi_class = dev->ddesc.bDeviceClass;
   1418       1.139     pavel 	di->udi_subclass = dev->ddesc.bDeviceSubClass;
   1419       1.139     pavel 	di->udi_protocol = dev->ddesc.bDeviceProtocol;
   1420       1.139     pavel 	di->udi_config = dev->config;
   1421       1.139     pavel 	di->udi_power = dev->self_powered ? 0 : dev->power;
   1422       1.139     pavel 	di->udi_speed = dev->speed;
   1423       1.139     pavel 
   1424       1.155  drochner 	if (dev->subdevlen > 0) {
   1425       1.155  drochner 		for (i = 0, j = 0; i < dev->subdevlen &&
   1426       1.155  drochner 			     j < USB_MAX_DEVNAMES; i++) {
   1427       1.155  drochner 			if (!dev->subdevs[i])
   1428       1.155  drochner 				continue;
   1429       1.155  drochner 			strncpy(di->udi_devnames[j], USBDEVPTRNAME(dev->subdevs[i]),
   1430       1.139     pavel 				USB_MAX_DEVNAMELEN);
   1431       1.155  drochner 			di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
   1432       1.155  drochner 			j++;
   1433       1.139     pavel 		}
   1434       1.139     pavel 	} else {
   1435       1.155  drochner 		j = 0;
   1436       1.139     pavel 	}
   1437       1.155  drochner 	for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
   1438       1.155  drochner 		di->udi_devnames[j][0] = 0;		 /* empty */
   1439       1.139     pavel 
   1440       1.139     pavel 	if (dev->hub) {
   1441       1.139     pavel 		for (i = 0;
   1442       1.139     pavel 		     i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
   1443       1.139     pavel 			     i < dev->hub->hubdesc.bNbrPorts;
   1444       1.139     pavel 		     i++) {
   1445       1.139     pavel 			p = &dev->hub->ports[i];
   1446       1.139     pavel 			if (p->device)
   1447       1.139     pavel 				err = p->device->address;
   1448       1.139     pavel 			else {
   1449       1.139     pavel 				s = UGETW(p->status.wPortStatus);
   1450       1.139     pavel 				if (s & UPS_PORT_ENABLED)
   1451       1.139     pavel 					err = USB_PORT_ENABLED;
   1452       1.139     pavel 				else if (s & UPS_SUSPEND)
   1453       1.139     pavel 					err = USB_PORT_SUSPENDED;
   1454       1.139     pavel 				else if (s & UPS_PORT_POWER)
   1455       1.139     pavel 					err = USB_PORT_POWERED;
   1456       1.139     pavel 				else
   1457       1.139     pavel 					err = USB_PORT_DISABLED;
   1458       1.139     pavel 			}
   1459       1.139     pavel 			di->udi_ports[i] = err;
   1460       1.139     pavel 		}
   1461       1.139     pavel 		di->udi_nports = dev->hub->hubdesc.bNbrPorts;
   1462       1.139     pavel 	} else
   1463       1.139     pavel 		di->udi_nports = 0;
   1464       1.139     pavel }
   1465       1.139     pavel #endif
   1466       1.139     pavel 
   1467       1.139     pavel 
   1468        1.34  augustss void
   1469        1.78  augustss usb_free_device(usbd_device_handle dev)
   1470        1.34  augustss {
   1471        1.34  augustss 	int ifcidx, nifc;
   1472        1.34  augustss 
   1473        1.53  augustss 	if (dev->default_pipe != NULL)
   1474        1.34  augustss 		usbd_kill_pipe(dev->default_pipe);
   1475        1.53  augustss 	if (dev->ifaces != NULL) {
   1476        1.34  augustss 		nifc = dev->cdesc->bNumInterface;
   1477        1.34  augustss 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
   1478        1.34  augustss 			usbd_free_iface_data(dev, ifcidx);
   1479        1.34  augustss 		free(dev->ifaces, M_USB);
   1480        1.34  augustss 	}
   1481        1.53  augustss 	if (dev->cdesc != NULL)
   1482        1.34  augustss 		free(dev->cdesc, M_USB);
   1483       1.155  drochner 	if (dev->subdevlen > 0) {
   1484        1.34  augustss 		free(dev->subdevs, M_USB);
   1485       1.155  drochner 		dev->subdevlen = 0;
   1486       1.155  drochner 	}
   1487        1.34  augustss 	free(dev, M_USB);
   1488         1.1  augustss }
   1489        1.47  augustss 
   1490        1.47  augustss /*
   1491        1.47  augustss  * The general mechanism for detaching drivers works as follows: Each
   1492        1.47  augustss  * driver is responsible for maintaining a reference count on the
   1493        1.47  augustss  * number of outstanding references to its softc (e.g.  from
   1494        1.47  augustss  * processing hanging in a read or write).  The detach method of the
   1495        1.47  augustss  * driver decrements this counter and flags in the softc that the
   1496        1.47  augustss  * driver is dying and then wakes any sleepers.  It then sleeps on the
   1497        1.47  augustss  * softc.  Each place that can sleep must maintain the reference
   1498        1.47  augustss  * count.  When the reference count drops to -1 (0 is the normal value
   1499        1.47  augustss  * of the reference count) the a wakeup on the softc is performed
   1500        1.47  augustss  * signaling to the detach waiter that all references are gone.
   1501        1.47  augustss  */
   1502        1.47  augustss 
   1503        1.47  augustss /*
   1504        1.47  augustss  * Called from process context when we discover that a port has
   1505        1.47  augustss  * been disconnected.
   1506        1.47  augustss  */
   1507        1.47  augustss void
   1508        1.78  augustss usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
   1509        1.47  augustss {
   1510        1.47  augustss 	usbd_device_handle dev = up->device;
   1511       1.152    cegger 	const char *hubname = USBDEVPTRNAME(parent);
   1512        1.47  augustss 	int i;
   1513        1.47  augustss 
   1514        1.99  augustss 	DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
   1515        1.47  augustss 		    up, dev, up->portno));
   1516        1.48  augustss 
   1517        1.48  augustss #ifdef DIAGNOSTIC
   1518        1.57  augustss 	if (dev == NULL) {
   1519        1.48  augustss 		printf("usb_disconnect_port: no device\n");
   1520        1.48  augustss 		return;
   1521        1.48  augustss 	}
   1522        1.48  augustss #endif
   1523        1.47  augustss 
   1524       1.155  drochner 	if (dev->subdevlen > 0) {
   1525        1.57  augustss 		DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
   1526       1.155  drochner 		for (i = 0; i < dev->subdevlen; i++) {
   1527       1.155  drochner 			if (!dev->subdevs[i])
   1528       1.155  drochner 				continue;
   1529        1.99  augustss 			printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
   1530        1.51  augustss 			       hubname);
   1531        1.51  augustss 			if (up->portno != 0)
   1532        1.51  augustss 				printf(" port %d", up->portno);
   1533        1.51  augustss 			printf(" (addr %d) disconnected\n", dev->address);
   1534        1.47  augustss 			config_detach(dev->subdevs[i], DETACH_FORCE);
   1535        1.47  augustss 		}
   1536       1.160  drochner 		KASSERT(!dev->nifaces_claimed);
   1537        1.47  augustss 	}
   1538        1.47  augustss 
   1539        1.65  augustss 	usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
   1540        1.57  augustss 	dev->bus->devices[dev->address] = NULL;
   1541        1.57  augustss 	up->device = NULL;
   1542        1.47  augustss 	usb_free_device(dev);
   1543        1.47  augustss }
   1544