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