Home | History | Annotate | Line # | Download | only in usb
usb_subr.c revision 1.131.8.1
      1  1.131.8.1      yamt /*	$NetBSD: usb_subr.c,v 1.131.8.1 2006/06/26 12:52:28 yamt 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.131.8.1      yamt __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.131.8.1 2006/06/26 12:52:28 yamt 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.129  drochner 			 const int *, 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.131.8.1      yamt 	char *vendor, *product;
    264        1.1  augustss 	int bcdDevice, bcdUSB;
    265      1.113    itojun 	char *ep;
    266      1.113    itojun 
    267  1.131.8.1      yamt 	vendor = malloc(USB_MAX_ENCODED_STRING_LEN * 2, M_USB, M_NOWAIT);
    268  1.131.8.1      yamt 	if (vendor == NULL) {
    269  1.131.8.1      yamt 		*cp = '\0';
    270  1.131.8.1      yamt 		return;
    271  1.131.8.1      yamt 	}
    272  1.131.8.1      yamt 	product = &vendor[USB_MAX_ENCODED_STRING_LEN];
    273  1.131.8.1      yamt 
    274      1.113    itojun 	ep = cp + l;
    275        1.1  augustss 
    276      1.123  augustss 	usbd_devinfo_vp(dev, vendor, product, 1);
    277      1.113    itojun 	cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
    278        1.1  augustss 	if (showclass)
    279      1.113    itojun 		cp += snprintf(cp, ep - cp, ", class %d/%d",
    280      1.113    itojun 		    udd->bDeviceClass, udd->bDeviceSubClass);
    281        1.1  augustss 	bcdUSB = UGETW(udd->bcdUSB);
    282        1.1  augustss 	bcdDevice = UGETW(udd->bcdDevice);
    283      1.113    itojun 	cp += snprintf(cp, ep - cp, ", rev ");
    284      1.113    itojun 	cp += usbd_printBCD(cp, ep - cp, bcdUSB);
    285        1.1  augustss 	*cp++ = '/';
    286      1.113    itojun 	cp += usbd_printBCD(cp, ep - cp, bcdDevice);
    287      1.113    itojun 	cp += snprintf(cp, ep - cp, ", addr %d", dev->address);
    288       1.10  augustss 	*cp = 0;
    289  1.131.8.1      yamt 	free(vendor, M_USB);
    290        1.1  augustss }
    291        1.1  augustss 
    292      1.124  augustss char *
    293      1.124  augustss usbd_devinfo_alloc(usbd_device_handle dev, int showclass)
    294      1.124  augustss {
    295      1.124  augustss 	char *devinfop;
    296      1.124  augustss 
    297      1.124  augustss 	devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK);
    298      1.124  augustss 	usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE);
    299      1.124  augustss 	return devinfop;
    300      1.124  augustss }
    301      1.124  augustss 
    302      1.124  augustss void
    303      1.124  augustss usbd_devinfo_free(char *devinfop)
    304      1.124  augustss {
    305      1.124  augustss 	free(devinfop, M_TEMP);
    306      1.124  augustss }
    307      1.124  augustss 
    308        1.1  augustss /* Delay for a certain number of ms */
    309        1.1  augustss void
    310       1.78  augustss usb_delay_ms(usbd_bus_handle bus, u_int ms)
    311        1.1  augustss {
    312        1.1  augustss 	/* Wait at least two clock ticks so we know the time has passed. */
    313       1.64  augustss 	if (bus->use_polling || cold)
    314        1.1  augustss 		delay((ms+1) * 1000);
    315        1.1  augustss 	else
    316        1.1  augustss 		tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
    317        1.1  augustss }
    318        1.1  augustss 
    319       1.23  augustss /* Delay given a device handle. */
    320       1.23  augustss void
    321       1.78  augustss usbd_delay_ms(usbd_device_handle dev, u_int ms)
    322       1.23  augustss {
    323       1.23  augustss 	usb_delay_ms(dev->bus, ms);
    324       1.23  augustss }
    325       1.23  augustss 
    326        1.1  augustss usbd_status
    327       1.78  augustss usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
    328        1.1  augustss {
    329        1.1  augustss 	usb_device_request_t req;
    330       1.53  augustss 	usbd_status err;
    331        1.1  augustss 	int n;
    332       1.99  augustss 
    333        1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    334        1.1  augustss 	req.bRequest = UR_SET_FEATURE;
    335        1.1  augustss 	USETW(req.wValue, UHF_PORT_RESET);
    336        1.1  augustss 	USETW(req.wIndex, port);
    337        1.1  augustss 	USETW(req.wLength, 0);
    338       1.53  augustss 	err = usbd_do_request(dev, &req, 0);
    339       1.39  augustss 	DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
    340       1.53  augustss 		    port, usbd_errstr(err)));
    341       1.53  augustss 	if (err)
    342       1.53  augustss 		return (err);
    343        1.1  augustss 	n = 10;
    344        1.1  augustss 	do {
    345        1.1  augustss 		/* Wait for device to recover from reset. */
    346       1.23  augustss 		usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
    347       1.53  augustss 		err = usbd_get_port_status(dev, port, ps);
    348       1.53  augustss 		if (err) {
    349       1.53  augustss 			DPRINTF(("usbd_reset_port: get status failed %d\n",
    350       1.53  augustss 				 err));
    351       1.53  augustss 			return (err);
    352        1.1  augustss 		}
    353       1.92  augustss 		/* If the device disappeared, just give up. */
    354       1.92  augustss 		if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
    355       1.92  augustss 			return (USBD_NORMAL_COMPLETION);
    356        1.1  augustss 	} while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
    357       1.73  augustss 	if (n == 0)
    358       1.73  augustss 		return (USBD_TIMEOUT);
    359       1.53  augustss 	err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    360        1.1  augustss #ifdef USB_DEBUG
    361       1.53  augustss 	if (err)
    362       1.53  augustss 		DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
    363       1.53  augustss 			 err));
    364        1.1  augustss #endif
    365       1.18  augustss 
    366       1.18  augustss 	/* Wait for the device to recover from reset. */
    367       1.23  augustss 	usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
    368       1.53  augustss 	return (err);
    369        1.1  augustss }
    370        1.1  augustss 
    371       1.12  augustss usb_interface_descriptor_t *
    372       1.78  augustss usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
    373       1.12  augustss {
    374       1.12  augustss 	char *p = (char *)cd;
    375       1.12  augustss 	char *end = p + UGETW(cd->wTotalLength);
    376       1.12  augustss 	usb_interface_descriptor_t *d;
    377       1.19  augustss 	int curidx, lastidx, curaidx = 0;
    378       1.12  augustss 
    379       1.19  augustss 	for (curidx = lastidx = -1; p < end; ) {
    380       1.12  augustss 		d = (usb_interface_descriptor_t *)p;
    381       1.18  augustss 		DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
    382       1.99  augustss 			    "type=%d\n",
    383       1.18  augustss 			    ifaceidx, curidx, altidx, curaidx,
    384       1.18  augustss 			    d->bLength, d->bDescriptorType));
    385       1.12  augustss 		if (d->bLength == 0) /* bad descriptor */
    386       1.12  augustss 			break;
    387       1.12  augustss 		p += d->bLength;
    388       1.12  augustss 		if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
    389       1.19  augustss 			if (d->bInterfaceNumber != lastidx) {
    390       1.19  augustss 				lastidx = d->bInterfaceNumber;
    391       1.12  augustss 				curidx++;
    392       1.12  augustss 				curaidx = 0;
    393       1.12  augustss 			} else
    394       1.12  augustss 				curaidx++;
    395       1.12  augustss 			if (ifaceidx == curidx && altidx == curaidx)
    396       1.12  augustss 				return (d);
    397       1.12  augustss 		}
    398       1.12  augustss 	}
    399       1.58  augustss 	return (NULL);
    400       1.12  augustss }
    401       1.12  augustss 
    402       1.12  augustss usb_endpoint_descriptor_t *
    403       1.99  augustss usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
    404       1.78  augustss 		int endptidx)
    405       1.12  augustss {
    406       1.12  augustss 	char *p = (char *)cd;
    407       1.12  augustss 	char *end = p + UGETW(cd->wTotalLength);
    408       1.12  augustss 	usb_interface_descriptor_t *d;
    409       1.12  augustss 	usb_endpoint_descriptor_t *e;
    410       1.12  augustss 	int curidx;
    411       1.12  augustss 
    412       1.12  augustss 	d = usbd_find_idesc(cd, ifaceidx, altidx);
    413       1.53  augustss 	if (d == NULL)
    414       1.58  augustss 		return (NULL);
    415       1.12  augustss 	if (endptidx >= d->bNumEndpoints) /* quick exit */
    416       1.58  augustss 		return (NULL);
    417       1.12  augustss 
    418       1.12  augustss 	curidx = -1;
    419       1.12  augustss 	for (p = (char *)d + d->bLength; p < end; ) {
    420       1.12  augustss 		e = (usb_endpoint_descriptor_t *)p;
    421       1.12  augustss 		if (e->bLength == 0) /* bad descriptor */
    422       1.12  augustss 			break;
    423       1.12  augustss 		p += e->bLength;
    424       1.12  augustss 		if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
    425       1.58  augustss 			return (NULL);
    426       1.12  augustss 		if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
    427       1.12  augustss 			curidx++;
    428       1.12  augustss 			if (curidx == endptidx)
    429       1.12  augustss 				return (e);
    430       1.12  augustss 		}
    431        1.1  augustss 	}
    432       1.58  augustss 	return (NULL);
    433        1.1  augustss }
    434        1.1  augustss 
    435        1.1  augustss usbd_status
    436       1.78  augustss usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
    437        1.1  augustss {
    438       1.12  augustss 	usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
    439       1.77  augustss 	usb_interface_descriptor_t *idesc;
    440        1.1  augustss 	char *p, *end;
    441        1.1  augustss 	int endpt, nendpt;
    442        1.1  augustss 
    443       1.18  augustss 	DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
    444       1.14  drochner 		    ifaceidx, altidx));
    445       1.77  augustss 	idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
    446       1.77  augustss 	if (idesc == NULL)
    447       1.77  augustss 		return (USBD_INVAL);
    448        1.1  augustss 	ifc->device = dev;
    449       1.77  augustss 	ifc->idesc = idesc;
    450       1.13  augustss 	ifc->index = ifaceidx;
    451       1.13  augustss 	ifc->altindex = altidx;
    452        1.1  augustss 	nendpt = ifc->idesc->bNumEndpoints;
    453       1.59  augustss 	DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
    454        1.1  augustss 	if (nendpt != 0) {
    455        1.1  augustss 		ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
    456        1.1  augustss 					M_USB, M_NOWAIT);
    457       1.58  augustss 		if (ifc->endpoints == NULL)
    458        1.1  augustss 			return (USBD_NOMEM);
    459        1.1  augustss 	} else
    460       1.58  augustss 		ifc->endpoints = NULL;
    461       1.58  augustss 	ifc->priv = NULL;
    462        1.1  augustss 	p = (char *)ifc->idesc + ifc->idesc->bLength;
    463        1.1  augustss 	end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
    464       1.19  augustss #define ed ((usb_endpoint_descriptor_t *)p)
    465        1.1  augustss 	for (endpt = 0; endpt < nendpt; endpt++) {
    466        1.1  augustss 		DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
    467        1.1  augustss 		for (; p < end; p += ed->bLength) {
    468       1.14  drochner 			DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
    469       1.14  drochner 				     "len=%d type=%d\n",
    470        1.1  augustss 				 p, end, ed->bLength, ed->bDescriptorType));
    471       1.24  augustss 			if (p + ed->bLength <= end && ed->bLength != 0 &&
    472        1.1  augustss 			    ed->bDescriptorType == UDESC_ENDPOINT)
    473       1.24  augustss 				goto found;
    474       1.59  augustss 			if (ed->bLength == 0 ||
    475       1.59  augustss 			    ed->bDescriptorType == UDESC_INTERFACE)
    476        1.1  augustss 				break;
    477        1.1  augustss 		}
    478       1.24  augustss 		/* passed end, or bad desc */
    479       1.95  augustss 		printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
    480       1.95  augustss 		       ed->bLength == 0 ? "0 length" :
    481       1.95  augustss 		       ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
    482       1.95  augustss 		       "out of data");
    483       1.24  augustss 		goto bad;
    484       1.24  augustss 	found:
    485        1.1  augustss 		ifc->endpoints[endpt].edesc = ed;
    486       1.95  augustss 		if (dev->speed == USB_SPEED_HIGH) {
    487       1.95  augustss 			u_int mps;
    488       1.95  augustss 			/* Control and bulk endpoints have max packet limits. */
    489       1.95  augustss 			switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
    490       1.95  augustss 			case UE_CONTROL:
    491       1.95  augustss 				mps = USB_2_MAX_CTRL_PACKET;
    492       1.95  augustss 				goto check;
    493       1.95  augustss 			case UE_BULK:
    494       1.95  augustss 				mps = USB_2_MAX_BULK_PACKET;
    495       1.95  augustss 			check:
    496       1.95  augustss 				if (UGETW(ed->wMaxPacketSize) != mps) {
    497       1.95  augustss 					USETW(ed->wMaxPacketSize, mps);
    498       1.95  augustss #ifdef DIAGNOSTIC
    499       1.95  augustss 					printf("usbd_fill_iface_data: bad max "
    500       1.95  augustss 					       "packet size\n");
    501       1.95  augustss #endif
    502       1.95  augustss 				}
    503       1.95  augustss 				break;
    504       1.95  augustss 			default:
    505       1.95  augustss 				break;
    506       1.95  augustss 			}
    507       1.95  augustss 		}
    508        1.1  augustss 		ifc->endpoints[endpt].refcnt = 0;
    509       1.24  augustss 		p += ed->bLength;
    510        1.1  augustss 	}
    511       1.19  augustss #undef ed
    512        1.1  augustss 	LIST_INIT(&ifc->pipes);
    513        1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    514       1.24  augustss 
    515        1.1  augustss  bad:
    516       1.77  augustss 	if (ifc->endpoints != NULL) {
    517       1.59  augustss 		free(ifc->endpoints, M_USB);
    518       1.77  augustss 		ifc->endpoints = NULL;
    519       1.77  augustss 	}
    520       1.24  augustss 	return (USBD_INVAL);
    521        1.1  augustss }
    522        1.1  augustss 
    523        1.1  augustss void
    524       1.78  augustss usbd_free_iface_data(usbd_device_handle dev, int ifcno)
    525        1.1  augustss {
    526        1.1  augustss 	usbd_interface_handle ifc = &dev->ifaces[ifcno];
    527        1.1  augustss 	if (ifc->endpoints)
    528        1.1  augustss 		free(ifc->endpoints, M_USB);
    529        1.1  augustss }
    530        1.1  augustss 
    531       1.69  augustss Static usbd_status
    532       1.78  augustss usbd_set_config(usbd_device_handle dev, int conf)
    533        1.7  augustss {
    534        1.7  augustss 	usb_device_request_t req;
    535        1.7  augustss 
    536        1.7  augustss 	req.bmRequestType = UT_WRITE_DEVICE;
    537        1.7  augustss 	req.bRequest = UR_SET_CONFIG;
    538        1.7  augustss 	USETW(req.wValue, conf);
    539        1.7  augustss 	USETW(req.wIndex, 0);
    540        1.7  augustss 	USETW(req.wLength, 0);
    541        1.7  augustss 	return (usbd_do_request(dev, &req, 0));
    542        1.7  augustss }
    543        1.7  augustss 
    544        1.1  augustss usbd_status
    545       1.78  augustss usbd_set_config_no(usbd_device_handle dev, int no, int msg)
    546        1.1  augustss {
    547       1.12  augustss 	int index;
    548       1.12  augustss 	usb_config_descriptor_t cd;
    549       1.53  augustss 	usbd_status err;
    550       1.12  augustss 
    551       1.75  augustss 	if (no == USB_UNCONFIG_NO)
    552       1.75  augustss 		return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
    553       1.75  augustss 
    554       1.12  augustss 	DPRINTFN(5,("usbd_set_config_no: %d\n", no));
    555       1.12  augustss 	/* Figure out what config index to use. */
    556       1.12  augustss 	for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
    557       1.53  augustss 		err = usbd_get_config_desc(dev, index, &cd);
    558       1.53  augustss 		if (err)
    559       1.53  augustss 			return (err);
    560       1.12  augustss 		if (cd.bConfigurationValue == no)
    561       1.12  augustss 			return (usbd_set_config_index(dev, index, msg));
    562       1.12  augustss 	}
    563       1.12  augustss 	return (USBD_INVAL);
    564       1.12  augustss }
    565       1.12  augustss 
    566       1.12  augustss usbd_status
    567       1.78  augustss usbd_set_config_index(usbd_device_handle dev, int index, int msg)
    568       1.12  augustss {
    569        1.1  augustss 	usb_status_t ds;
    570        1.1  augustss 	usb_config_descriptor_t cd, *cdp;
    571       1.53  augustss 	usbd_status err;
    572      1.103  augustss 	int i, ifcidx, nifc, len, selfpowered, power;
    573        1.1  augustss 
    574       1.12  augustss 	DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
    575        1.1  augustss 
    576        1.1  augustss 	/* XXX check that all interfaces are idle */
    577       1.75  augustss 	if (dev->config != USB_UNCONFIG_NO) {
    578       1.12  augustss 		DPRINTF(("usbd_set_config_index: free old config\n"));
    579        1.1  augustss 		/* Free all configuration data structures. */
    580        1.1  augustss 		nifc = dev->cdesc->bNumInterface;
    581       1.12  augustss 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
    582       1.12  augustss 			usbd_free_iface_data(dev, ifcidx);
    583        1.1  augustss 		free(dev->ifaces, M_USB);
    584        1.1  augustss 		free(dev->cdesc, M_USB);
    585       1.57  augustss 		dev->ifaces = NULL;
    586       1.57  augustss 		dev->cdesc = NULL;
    587       1.75  augustss 		dev->config = USB_UNCONFIG_NO;
    588       1.75  augustss 	}
    589       1.75  augustss 
    590       1.75  augustss 	if (index == USB_UNCONFIG_INDEX) {
    591       1.75  augustss 		/* We are unconfiguring the device, so leave unallocated. */
    592       1.75  augustss 		DPRINTF(("usbd_set_config_index: set config 0\n"));
    593       1.75  augustss 		err = usbd_set_config(dev, USB_UNCONFIG_NO);
    594       1.75  augustss 		if (err)
    595       1.75  augustss 			DPRINTF(("usbd_set_config_index: setting config=0 "
    596       1.75  augustss 				 "failed, error=%s\n", usbd_errstr(err)));
    597       1.75  augustss 		return (err);
    598        1.1  augustss 	}
    599        1.1  augustss 
    600       1.74  augustss 	/* Get the short descriptor. */
    601       1.53  augustss 	err = usbd_get_config_desc(dev, index, &cd);
    602       1.53  augustss 	if (err)
    603       1.53  augustss 		return (err);
    604        1.1  augustss 	len = UGETW(cd.wTotalLength);
    605        1.1  augustss 	cdp = malloc(len, M_USB, M_NOWAIT);
    606       1.53  augustss 	if (cdp == NULL)
    607        1.1  augustss 		return (USBD_NOMEM);
    608      1.103  augustss 
    609      1.103  augustss 	/* Get the full descriptor.  Try a few times for slow devices. */
    610      1.103  augustss 	for (i = 0; i < 3; i++) {
    611      1.103  augustss 		err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
    612      1.103  augustss 		if (!err)
    613      1.103  augustss 			break;
    614      1.103  augustss 		usbd_delay_ms(dev, 200);
    615      1.103  augustss 	}
    616       1.53  augustss 	if (err)
    617        1.1  augustss 		goto bad;
    618      1.103  augustss 
    619       1.18  augustss 	if (cdp->bDescriptorType != UDESC_CONFIG) {
    620       1.18  augustss 		DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
    621       1.18  augustss 			     cdp->bDescriptorType));
    622       1.53  augustss 		err = USBD_INVAL;
    623       1.18  augustss 		goto bad;
    624       1.18  augustss 	}
    625       1.74  augustss 
    626       1.74  augustss 	/* Figure out if the device is self or bus powered. */
    627        1.1  augustss 	selfpowered = 0;
    628       1.49  augustss 	if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
    629       1.74  augustss 	    (cdp->bmAttributes & UC_SELF_POWERED)) {
    630        1.1  augustss 		/* May be self powered. */
    631        1.1  augustss 		if (cdp->bmAttributes & UC_BUS_POWERED) {
    632        1.1  augustss 			/* Must ask device. */
    633       1.81  augustss 			if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
    634       1.81  augustss 				/*
    635       1.81  augustss 				 * Hub claims to be self powered, but isn't.
    636       1.81  augustss 				 * It seems that the power status can be
    637       1.81  augustss 				 * determined by the hub characteristics.
    638       1.81  augustss 				 */
    639       1.81  augustss 				usb_hub_descriptor_t hd;
    640       1.81  augustss 				usb_device_request_t req;
    641       1.81  augustss 				req.bmRequestType = UT_READ_CLASS_DEVICE;
    642       1.81  augustss 				req.bRequest = UR_GET_DESCRIPTOR;
    643       1.81  augustss 				USETW(req.wValue, 0);
    644       1.81  augustss 				USETW(req.wIndex, 0);
    645       1.81  augustss 				USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    646       1.81  augustss 				err = usbd_do_request(dev, &req, &hd);
    647       1.81  augustss 				if (!err &&
    648       1.81  augustss 				    (UGETW(hd.wHubCharacteristics) &
    649       1.81  augustss 				     UHD_PWR_INDIVIDUAL))
    650       1.81  augustss 					selfpowered = 1;
    651       1.81  augustss 				DPRINTF(("usbd_set_config_index: charac=0x%04x"
    652       1.81  augustss 				    ", error=%s\n",
    653       1.81  augustss 				    UGETW(hd.wHubCharacteristics),
    654       1.81  augustss 				    usbd_errstr(err)));
    655       1.81  augustss 			} else {
    656       1.81  augustss 				err = usbd_get_device_status(dev, &ds);
    657       1.81  augustss 				if (!err &&
    658       1.81  augustss 				    (UGETW(ds.wStatus) & UDS_SELF_POWERED))
    659       1.81  augustss 					selfpowered = 1;
    660       1.81  augustss 				DPRINTF(("usbd_set_config_index: status=0x%04x"
    661       1.81  augustss 				    ", error=%s\n",
    662       1.81  augustss 				    UGETW(ds.wStatus), usbd_errstr(err)));
    663       1.81  augustss 			}
    664        1.1  augustss 		} else
    665        1.1  augustss 			selfpowered = 1;
    666        1.1  augustss 	}
    667       1.81  augustss 	DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
    668       1.81  augustss 		 "selfpowered=%d, power=%d\n",
    669       1.99  augustss 		 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
    670       1.33  augustss 		 selfpowered, cdp->bMaxPower * 2));
    671       1.74  augustss 
    672       1.74  augustss 	/* Check if we have enough power. */
    673        1.1  augustss #ifdef USB_DEBUG
    674       1.53  augustss 	if (dev->powersrc == NULL) {
    675       1.44  augustss 		DPRINTF(("usbd_set_config_index: No power source?\n"));
    676       1.13  augustss 		return (USBD_IOERROR);
    677        1.1  augustss 	}
    678        1.1  augustss #endif
    679        1.1  augustss 	power = cdp->bMaxPower * 2;
    680        1.1  augustss 	if (power > dev->powersrc->power) {
    681       1.81  augustss 		DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
    682        1.1  augustss 		/* XXX print nicer message. */
    683        1.7  augustss 		if (msg)
    684       1.18  augustss 			printf("%s: device addr %d (config %d) exceeds power "
    685       1.18  augustss 				 "budget, %d mA > %d mA\n",
    686       1.99  augustss 			       USBDEVNAME(dev->bus->bdev), dev->address,
    687       1.99  augustss 			       cdp->bConfigurationValue,
    688        1.7  augustss 			       power, dev->powersrc->power);
    689       1.53  augustss 		err = USBD_NO_POWER;
    690        1.1  augustss 		goto bad;
    691        1.1  augustss 	}
    692        1.1  augustss 	dev->power = power;
    693        1.1  augustss 	dev->self_powered = selfpowered;
    694        1.1  augustss 
    695       1.74  augustss 	/* Set the actual configuration value. */
    696       1.18  augustss 	DPRINTF(("usbd_set_config_index: set config %d\n",
    697       1.18  augustss 		 cdp->bConfigurationValue));
    698       1.53  augustss 	err = usbd_set_config(dev, cdp->bConfigurationValue);
    699       1.53  augustss 	if (err) {
    700       1.14  drochner 		DPRINTF(("usbd_set_config_index: setting config=%d failed, "
    701       1.39  augustss 			 "error=%s\n",
    702       1.53  augustss 			 cdp->bConfigurationValue, usbd_errstr(err)));
    703        1.1  augustss 		goto bad;
    704        1.1  augustss 	}
    705       1.74  augustss 
    706       1.74  augustss 	/* Allocate and fill interface data. */
    707        1.1  augustss 	nifc = cdp->bNumInterface;
    708       1.99  augustss 	dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
    709        1.1  augustss 			     M_USB, M_NOWAIT);
    710       1.58  augustss 	if (dev->ifaces == NULL) {
    711       1.53  augustss 		err = USBD_NOMEM;
    712        1.1  augustss 		goto bad;
    713        1.1  augustss 	}
    714       1.12  augustss 	DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
    715        1.1  augustss 	dev->cdesc = cdp;
    716        1.1  augustss 	dev->config = cdp->bConfigurationValue;
    717       1.12  augustss 	for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
    718       1.53  augustss 		err = usbd_fill_iface_data(dev, ifcidx, 0);
    719       1.53  augustss 		if (err) {
    720       1.12  augustss 			while (--ifcidx >= 0)
    721       1.12  augustss 				usbd_free_iface_data(dev, ifcidx);
    722        1.1  augustss 			goto bad;
    723        1.1  augustss 		}
    724        1.1  augustss 	}
    725        1.1  augustss 
    726        1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    727        1.1  augustss 
    728        1.1  augustss  bad:
    729        1.1  augustss 	free(cdp, M_USB);
    730       1.53  augustss 	return (err);
    731        1.1  augustss }
    732        1.1  augustss 
    733        1.1  augustss /* XXX add function for alternate settings */
    734        1.1  augustss 
    735        1.1  augustss usbd_status
    736       1.78  augustss usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
    737       1.78  augustss 		struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
    738        1.1  augustss {
    739        1.1  augustss 	usbd_pipe_handle p;
    740       1.53  augustss 	usbd_status err;
    741        1.1  augustss 
    742        1.1  augustss 	DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
    743        1.1  augustss 		    dev, iface, ep, pipe));
    744        1.1  augustss 	p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
    745       1.53  augustss 	if (p == NULL)
    746        1.1  augustss 		return (USBD_NOMEM);
    747        1.1  augustss 	p->device = dev;
    748        1.1  augustss 	p->iface = iface;
    749        1.1  augustss 	p->endpoint = ep;
    750        1.1  augustss 	ep->refcnt++;
    751        1.1  augustss 	p->refcnt = 1;
    752       1.53  augustss 	p->intrxfer = 0;
    753        1.1  augustss 	p->running = 0;
    754       1.70  augustss 	p->aborting = 0;
    755       1.34  augustss 	p->repeat = 0;
    756       1.63  augustss 	p->interval = ival;
    757        1.1  augustss 	SIMPLEQ_INIT(&p->queue);
    758       1.53  augustss 	err = dev->bus->methods->open_pipe(p);
    759       1.53  augustss 	if (err) {
    760       1.39  augustss 		DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
    761       1.39  augustss 			 "%s\n",
    762       1.53  augustss 			 ep->edesc->bEndpointAddress, usbd_errstr(err)));
    763        1.1  augustss 		free(p, M_USB);
    764       1.53  augustss 		return (err);
    765        1.1  augustss 	}
    766        1.1  augustss 	*pipe = p;
    767        1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    768        1.1  augustss }
    769        1.1  augustss 
    770        1.1  augustss /* Abort the device control pipe. */
    771        1.1  augustss void
    772       1.78  augustss usbd_kill_pipe(usbd_pipe_handle pipe)
    773        1.1  augustss {
    774       1.89  augustss 	usbd_abort_pipe(pipe);
    775        1.1  augustss 	pipe->methods->close(pipe);
    776        1.1  augustss 	pipe->endpoint->refcnt--;
    777        1.1  augustss 	free(pipe, M_USB);
    778        1.1  augustss }
    779        1.1  augustss 
    780        1.1  augustss int
    781       1.78  augustss usbd_getnewaddr(usbd_bus_handle bus)
    782        1.1  augustss {
    783       1.18  augustss 	int addr;
    784        1.1  augustss 
    785       1.18  augustss 	for (addr = 1; addr < USB_MAX_DEVICES; addr++)
    786       1.18  augustss 		if (bus->devices[addr] == 0)
    787       1.18  augustss 			return (addr);
    788        1.1  augustss 	return (-1);
    789        1.1  augustss }
    790        1.1  augustss 
    791       1.18  augustss 
    792       1.18  augustss usbd_status
    793       1.78  augustss usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
    794       1.78  augustss 		      int port, int addr)
    795       1.18  augustss {
    796       1.18  augustss 	struct usb_attach_arg uaa;
    797       1.18  augustss 	usb_device_descriptor_t *dd = &dev->ddesc;
    798       1.53  augustss 	int found, i, confi, nifaces;
    799       1.53  augustss 	usbd_status err;
    800       1.43  augustss 	device_ptr_t dv;
    801  1.131.8.1      yamt 	usbd_interface_handle *ifaces;
    802       1.18  augustss 
    803       1.18  augustss #if defined(__FreeBSD__)
    804       1.99  augustss 	/*
    805       1.28  augustss 	 * XXX uaa is a static var. Not a problem as it _should_ be used only
    806       1.28  augustss 	 * during probe and attach. Should be changed however.
    807       1.28  augustss 	 */
    808       1.43  augustss 	device_t bdev;
    809       1.52  augustss 	bdev = device_add_child(parent, NULL, -1, &uaa);
    810       1.26  augustss 	if (!bdev) {
    811       1.18  augustss 	    printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
    812       1.18  augustss 	    return (USBD_INVAL);
    813       1.18  augustss 	}
    814       1.52  augustss 	device_quiet(bdev);
    815       1.18  augustss #endif
    816       1.18  augustss 
    817       1.18  augustss 	uaa.device = dev;
    818       1.72  augustss 	uaa.iface = NULL;
    819       1.72  augustss 	uaa.ifaces = NULL;
    820       1.20  augustss 	uaa.nifaces = 0;
    821       1.18  augustss 	uaa.usegeneric = 0;
    822       1.18  augustss 	uaa.port = port;
    823       1.18  augustss 	uaa.configno = UHUB_UNK_CONFIGURATION;
    824       1.18  augustss 	uaa.ifaceno = UHUB_UNK_INTERFACE;
    825       1.32  augustss 	uaa.vendor = UGETW(dd->idVendor);
    826       1.32  augustss 	uaa.product = UGETW(dd->idProduct);
    827       1.32  augustss 	uaa.release = UGETW(dd->bcdDevice);
    828       1.18  augustss 
    829       1.18  augustss 	/* First try with device specific drivers. */
    830       1.60  augustss 	DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
    831       1.34  augustss 	dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
    832       1.34  augustss 	if (dv) {
    833       1.34  augustss 		dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
    834       1.57  augustss 		if (dev->subdevs == NULL)
    835       1.34  augustss 			return (USBD_NOMEM);
    836       1.34  augustss 		dev->subdevs[0] = dv;
    837       1.34  augustss 		dev->subdevs[1] = 0;
    838       1.18  augustss 		return (USBD_NORMAL_COMPLETION);
    839       1.34  augustss 	}
    840       1.18  augustss 
    841       1.18  augustss 	DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
    842       1.18  augustss 
    843       1.60  augustss 	DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
    844       1.60  augustss 		 dd->bNumConfigurations));
    845       1.18  augustss 	/* Next try with interface drivers. */
    846       1.18  augustss 	for (confi = 0; confi < dd->bNumConfigurations; confi++) {
    847       1.18  augustss 		DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
    848       1.18  augustss 			    confi));
    849       1.53  augustss 		err = usbd_set_config_index(dev, confi, 1);
    850       1.53  augustss 		if (err) {
    851       1.18  augustss #ifdef USB_DEBUG
    852       1.18  augustss 			DPRINTF(("%s: port %d, set config at addr %d failed, "
    853       1.43  augustss 				 "error=%s\n", USBDEVPTRNAME(parent), port,
    854       1.53  augustss 				 addr, usbd_errstr(err)));
    855       1.18  augustss #else
    856       1.18  augustss 			printf("%s: port %d, set config at addr %d failed\n",
    857       1.43  augustss 			       USBDEVPTRNAME(parent), port, addr);
    858       1.18  augustss #endif
    859       1.27  augustss #if defined(__FreeBSD__)
    860       1.52  augustss 			device_delete_child(parent, bdev);
    861       1.27  augustss #endif
    862       1.52  augustss 
    863       1.53  augustss  			return (err);
    864       1.18  augustss 		}
    865       1.20  augustss 		nifaces = dev->cdesc->bNumInterface;
    866       1.23  augustss 		uaa.configno = dev->cdesc->bConfigurationValue;
    867  1.131.8.1      yamt 		ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT);
    868  1.131.8.1      yamt 		if (ifaces == NULL)
    869  1.131.8.1      yamt 			goto nomem;
    870       1.20  augustss 		for (i = 0; i < nifaces; i++)
    871       1.20  augustss 			ifaces[i] = &dev->ifaces[i];
    872       1.20  augustss 		uaa.ifaces = ifaces;
    873       1.20  augustss 		uaa.nifaces = nifaces;
    874       1.34  augustss 		dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
    875       1.57  augustss 		if (dev->subdevs == NULL) {
    876  1.131.8.1      yamt 			free(ifaces, M_USB);
    877  1.131.8.1      yamt nomem:
    878       1.52  augustss #if defined(__FreeBSD__)
    879       1.52  augustss 			device_delete_child(parent, bdev);
    880       1.52  augustss #endif
    881       1.34  augustss 			return (USBD_NOMEM);
    882       1.52  augustss 		}
    883       1.52  augustss 
    884       1.52  augustss 		found = 0;
    885       1.52  augustss 		for (i = 0; i < nifaces; i++) {
    886       1.53  augustss 			if (ifaces[i] == NULL)
    887       1.20  augustss 				continue; /* interface already claimed */
    888       1.20  augustss 			uaa.iface = ifaces[i];
    889       1.20  augustss 			uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
    890       1.34  augustss 			dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
    891       1.34  augustss 					   usbd_submatch);
    892       1.53  augustss 			if (dv != NULL) {
    893       1.34  augustss 				dev->subdevs[found++] = dv;
    894       1.34  augustss 				dev->subdevs[found] = 0;
    895       1.20  augustss 				ifaces[i] = 0; /* consumed */
    896       1.52  augustss 
    897       1.52  augustss #if defined(__FreeBSD__)
    898       1.52  augustss 				/* create another child for the next iface */
    899       1.52  augustss 				bdev = device_add_child(parent, NULL, -1,&uaa);
    900       1.52  augustss 				if (!bdev) {
    901       1.52  augustss 					printf("%s: Device creation failed\n",
    902       1.52  augustss 					USBDEVNAME(dev->bus->bdev));
    903  1.131.8.1      yamt 					free(ifaces, M_USB);
    904       1.52  augustss 					return (USBD_NORMAL_COMPLETION);
    905       1.52  augustss 				}
    906       1.52  augustss 				device_quiet(bdev);
    907       1.52  augustss #endif
    908       1.20  augustss 			}
    909       1.18  augustss 		}
    910       1.52  augustss 		if (found != 0) {
    911       1.52  augustss #if defined(__FreeBSD__)
    912       1.52  augustss 			/* remove the last created child again; it is unused */
    913       1.52  augustss 			device_delete_child(parent, bdev);
    914       1.52  augustss #endif
    915  1.131.8.1      yamt 			free(ifaces, M_USB);
    916       1.18  augustss 			return (USBD_NORMAL_COMPLETION);
    917       1.52  augustss 		}
    918  1.131.8.1      yamt 		free(ifaces, M_USB);
    919       1.34  augustss 		free(dev->subdevs, M_USB);
    920       1.34  augustss 		dev->subdevs = 0;
    921       1.18  augustss 	}
    922       1.21  augustss 	/* No interfaces were attached in any of the configurations. */
    923       1.34  augustss 
    924       1.34  augustss 	if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
    925       1.18  augustss 		usbd_set_config_index(dev, 0, 0);
    926       1.18  augustss 
    927       1.18  augustss 	DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
    928       1.18  augustss 
    929       1.18  augustss 	/* Finally try the generic driver. */
    930       1.72  augustss 	uaa.iface = NULL;
    931       1.18  augustss 	uaa.usegeneric = 1;
    932       1.18  augustss 	uaa.configno = UHUB_UNK_CONFIGURATION;
    933       1.18  augustss 	uaa.ifaceno = UHUB_UNK_INTERFACE;
    934       1.34  augustss 	dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
    935       1.53  augustss 	if (dv != NULL) {
    936       1.34  augustss 		dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
    937       1.34  augustss 		if (dev->subdevs == 0)
    938       1.34  augustss 			return (USBD_NOMEM);
    939       1.34  augustss 		dev->subdevs[0] = dv;
    940       1.34  augustss 		dev->subdevs[1] = 0;
    941       1.18  augustss 		return (USBD_NORMAL_COMPLETION);
    942       1.34  augustss 	}
    943       1.18  augustss 
    944       1.99  augustss 	/*
    945       1.18  augustss 	 * The generic attach failed, but leave the device as it is.
    946       1.18  augustss 	 * We just did not find any drivers, that's all.  The device is
    947       1.18  augustss 	 * fully operational and not harming anyone.
    948       1.18  augustss 	 */
    949       1.18  augustss 	DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
    950       1.27  augustss #if defined(__FreeBSD__)
    951       1.52  augustss 	device_delete_child(parent, bdev);
    952       1.27  augustss #endif
    953       1.27  augustss  	return (USBD_NORMAL_COMPLETION);
    954       1.18  augustss }
    955       1.18  augustss 
    956       1.18  augustss 
    957        1.1  augustss /*
    958        1.1  augustss  * Called when a new device has been put in the powered state,
    959        1.1  augustss  * but not yet in the addressed state.
    960        1.1  augustss  * Get initial descriptor, set the address, get full descriptor,
    961        1.1  augustss  * and attach a driver.
    962        1.1  augustss  */
    963        1.1  augustss usbd_status
    964       1.78  augustss usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
    965       1.94  augustss 		int speed, int port, struct usbd_port *up)
    966        1.1  augustss {
    967      1.120  augustss 	usbd_device_handle dev, adev;
    968       1.94  augustss 	struct usbd_device *hub;
    969       1.18  augustss 	usb_device_descriptor_t *dd;
    970      1.107  augustss 	usb_port_status_t ps;
    971       1.53  augustss 	usbd_status err;
    972        1.1  augustss 	int addr;
    973       1.18  augustss 	int i;
    974      1.120  augustss 	int p;
    975        1.1  augustss 
    976       1.94  augustss 	DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
    977       1.94  augustss 		 bus, port, depth, speed));
    978        1.1  augustss 	addr = usbd_getnewaddr(bus);
    979        1.1  augustss 	if (addr < 0) {
    980       1.99  augustss 		printf("%s: No free USB addresses, new device ignored.\n",
    981       1.18  augustss 		       USBDEVNAME(bus->bdev));
    982        1.1  augustss 		return (USBD_NO_ADDR);
    983        1.1  augustss 	}
    984        1.1  augustss 
    985       1.97   tsutsui 	dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
    986       1.53  augustss 	if (dev == NULL)
    987        1.1  augustss 		return (USBD_NOMEM);
    988        1.1  augustss 
    989        1.1  augustss 	dev->bus = bus;
    990        1.1  augustss 
    991        1.1  augustss 	/* Set up default endpoint handle. */
    992        1.1  augustss 	dev->def_ep.edesc = &dev->def_ep_desc;
    993        1.1  augustss 
    994        1.1  augustss 	/* Set up default endpoint descriptor. */
    995        1.1  augustss 	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
    996        1.1  augustss 	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
    997        1.1  augustss 	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
    998        1.1  augustss 	dev->def_ep_desc.bmAttributes = UE_CONTROL;
    999        1.1  augustss 	USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
   1000        1.1  augustss 	dev->def_ep_desc.bInterval = 0;
   1001        1.1  augustss 
   1002        1.1  augustss 	dev->quirks = &usbd_no_quirk;
   1003        1.1  augustss 	dev->address = USB_START_ADDR;
   1004        1.4  augustss 	dev->ddesc.bMaxPacketSize = 0;
   1005        1.1  augustss 	dev->depth = depth;
   1006        1.1  augustss 	dev->powersrc = up;
   1007       1.94  augustss 	dev->myhub = up->parent;
   1008      1.120  augustss 
   1009      1.120  augustss 	up->device = dev;
   1010      1.120  augustss 
   1011      1.120  augustss 	/* Locate port on upstream high speed hub */
   1012      1.120  augustss 	for (adev = dev, hub = up->parent;
   1013       1.94  augustss 	     hub != NULL && hub->speed != USB_SPEED_HIGH;
   1014      1.120  augustss 	     adev = hub, hub = hub->myhub)
   1015       1.94  augustss 		;
   1016      1.120  augustss 	if (hub) {
   1017      1.120  augustss 		for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
   1018      1.120  augustss 			if (hub->hub->ports[p].device == adev) {
   1019      1.120  augustss 				dev->myhsport = &hub->hub->ports[p];
   1020      1.120  augustss 				goto found;
   1021      1.120  augustss 			}
   1022      1.120  augustss 		}
   1023      1.120  augustss 		panic("usbd_new_device: cannot find HS port\n");
   1024      1.120  augustss 	found:
   1025      1.120  augustss 		DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
   1026      1.120  augustss 	} else {
   1027      1.120  augustss 		dev->myhsport = NULL;
   1028      1.120  augustss 	}
   1029       1.94  augustss 	dev->speed = speed;
   1030       1.22  augustss 	dev->langid = USBD_NOLANG;
   1031       1.50  augustss 	dev->cookie.cookie = ++usb_cookie_no;
   1032        1.1  augustss 
   1033       1.67     soren 	/* Establish the default pipe. */
   1034       1.63  augustss 	err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
   1035       1.63  augustss 			      &dev->default_pipe);
   1036       1.53  augustss 	if (err) {
   1037       1.18  augustss 		usbd_remove_device(dev, up);
   1038       1.53  augustss 		return (err);
   1039       1.18  augustss 	}
   1040        1.1  augustss 
   1041      1.111  augustss 	/* Set the address.  Do this early; some devices need that. */
   1042      1.111  augustss 	err = usbd_set_address(dev, addr);
   1043      1.111  augustss 	DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
   1044      1.111  augustss 	if (err) {
   1045      1.111  augustss 		DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
   1046      1.111  augustss 		err = USBD_SET_ADDR_FAILED;
   1047      1.111  augustss 		usbd_remove_device(dev, up);
   1048      1.111  augustss 		return (err);
   1049      1.111  augustss 	}
   1050      1.111  augustss 	/* Allow device time to set new address */
   1051      1.111  augustss 	usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
   1052      1.111  augustss 	dev->address = addr;	/* New device address now */
   1053      1.111  augustss 	bus->devices[addr] = dev;
   1054      1.111  augustss 
   1055       1.18  augustss 	dd = &dev->ddesc;
   1056        1.6  augustss 	/* Try a few times in case the device is slow (i.e. outside specs.) */
   1057      1.107  augustss 	for (i = 0; i < 10; i++) {
   1058        1.6  augustss 		/* Get the first 8 bytes of the device descriptor. */
   1059       1.53  augustss 		err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
   1060       1.53  augustss 		if (!err)
   1061        1.6  augustss 			break;
   1062       1.23  augustss 		usbd_delay_ms(dev, 200);
   1063      1.107  augustss 		if ((i & 3) == 3)
   1064      1.107  augustss 			usbd_reset_port(up->parent, port, &ps);
   1065        1.6  augustss 	}
   1066       1.53  augustss 	if (err) {
   1067       1.14  drochner 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
   1068       1.53  augustss 			      "failed\n", addr));
   1069       1.18  augustss 		usbd_remove_device(dev, up);
   1070       1.53  augustss 		return (err);
   1071       1.95  augustss 	}
   1072       1.95  augustss 
   1073       1.95  augustss 	if (speed == USB_SPEED_HIGH) {
   1074       1.95  augustss 		/* Max packet size must be 64 (sec 5.5.3). */
   1075       1.95  augustss 		if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
   1076       1.95  augustss #ifdef DIAGNOSTIC
   1077       1.95  augustss 			printf("usbd_new_device: addr=%d bad max packet size\n",
   1078       1.95  augustss 			       addr);
   1079       1.95  augustss #endif
   1080       1.95  augustss 			dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
   1081       1.95  augustss 		}
   1082       1.18  augustss 	}
   1083       1.18  augustss 
   1084       1.39  augustss 	DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
   1085       1.99  augustss 		 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
   1086       1.39  augustss 		 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
   1087       1.99  augustss 		 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
   1088       1.94  augustss 		 dev->speed));
   1089       1.39  augustss 
   1090       1.18  augustss 	if (dd->bDescriptorType != UDESC_DEVICE) {
   1091       1.18  augustss 		/* Illegal device descriptor */
   1092       1.18  augustss 		DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
   1093       1.18  augustss 			     dd->bDescriptorType));
   1094       1.18  augustss 		usbd_remove_device(dev, up);
   1095       1.18  augustss 		return (USBD_INVAL);
   1096        1.1  augustss 	}
   1097        1.1  augustss 
   1098       1.39  augustss 	if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
   1099       1.39  augustss 		DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
   1100       1.39  augustss 		usbd_remove_device(dev, up);
   1101       1.39  augustss 		return (USBD_INVAL);
   1102       1.39  augustss 	}
   1103        1.4  augustss 
   1104       1.18  augustss 	USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
   1105        1.1  augustss 
   1106       1.62  augustss 	err = usbd_reload_device_desc(dev);
   1107       1.53  augustss 	if (err) {
   1108       1.14  drochner 		DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
   1109       1.14  drochner 			      "failed\n", addr));
   1110       1.18  augustss 		usbd_remove_device(dev, up);
   1111       1.53  augustss 		return (err);
   1112        1.1  augustss 	}
   1113        1.1  augustss 
   1114        1.1  augustss 	/* Assume 100mA bus powered for now. Changed when configured. */
   1115        1.1  augustss 	dev->power = USB_MIN_POWER;
   1116        1.1  augustss 	dev->self_powered = 0;
   1117        1.1  augustss 
   1118       1.99  augustss 	DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
   1119        1.1  augustss 		 addr, dev, parent));
   1120       1.99  augustss 
   1121       1.65  augustss 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
   1122        1.1  augustss 
   1123       1.53  augustss 	err = usbd_probe_and_attach(parent, dev, port, addr);
   1124       1.53  augustss 	if (err) {
   1125       1.18  augustss 		usbd_remove_device(dev, up);
   1126       1.53  augustss 		return (err);
   1127       1.18  augustss   	}
   1128       1.65  augustss 
   1129       1.18  augustss   	return (USBD_NORMAL_COMPLETION);
   1130       1.62  augustss }
   1131       1.62  augustss 
   1132       1.62  augustss usbd_status
   1133       1.78  augustss usbd_reload_device_desc(usbd_device_handle dev)
   1134       1.62  augustss {
   1135       1.62  augustss 	usbd_status err;
   1136       1.62  augustss 
   1137       1.62  augustss 	/* Get the full device descriptor. */
   1138       1.62  augustss 	err = usbd_get_device_desc(dev, &dev->ddesc);
   1139       1.62  augustss 	if (err)
   1140       1.62  augustss 		return (err);
   1141       1.62  augustss 
   1142       1.62  augustss 	/* Figure out what's wrong with this device. */
   1143       1.62  augustss 	dev->quirks = usbd_find_quirk(&dev->ddesc);
   1144       1.62  augustss 
   1145       1.62  augustss 	return (USBD_NORMAL_COMPLETION);
   1146       1.18  augustss }
   1147        1.1  augustss 
   1148       1.18  augustss void
   1149       1.78  augustss usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
   1150       1.18  augustss {
   1151       1.18  augustss 	DPRINTF(("usbd_remove_device: %p\n", dev));
   1152       1.99  augustss 
   1153       1.53  augustss 	if (dev->default_pipe != NULL)
   1154       1.18  augustss 		usbd_kill_pipe(dev->default_pipe);
   1155      1.120  augustss 	up->device = NULL;
   1156      1.120  augustss 	dev->bus->devices[dev->address] = NULL;
   1157        1.1  augustss 
   1158        1.1  augustss 	free(dev, M_USB);
   1159        1.1  augustss }
   1160        1.1  augustss 
   1161       1.37  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
   1162        1.1  augustss int
   1163       1.78  augustss usbd_print(void *aux, const char *pnp)
   1164        1.1  augustss {
   1165        1.1  augustss 	struct usb_attach_arg *uaa = aux;
   1166        1.1  augustss 
   1167        1.1  augustss 	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
   1168        1.1  augustss 	if (pnp) {
   1169  1.131.8.1      yamt #define USB_DEVINFO 1024
   1170  1.131.8.1      yamt 		char *devinfo;
   1171        1.1  augustss 		if (!uaa->usegeneric)
   1172        1.1  augustss 			return (QUIET);
   1173  1.131.8.1      yamt 		devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
   1174  1.131.8.1      yamt 		usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
   1175      1.101   thorpej 		aprint_normal("%s, %s", devinfo, pnp);
   1176  1.131.8.1      yamt 		free(devinfo, M_TEMP);
   1177        1.1  augustss 	}
   1178        1.1  augustss 	if (uaa->port != 0)
   1179      1.101   thorpej 		aprint_normal(" port %d", uaa->port);
   1180       1.11  augustss 	if (uaa->configno != UHUB_UNK_CONFIGURATION)
   1181      1.101   thorpej 		aprint_normal(" configuration %d", uaa->configno);
   1182       1.11  augustss 	if (uaa->ifaceno != UHUB_UNK_INTERFACE)
   1183      1.101   thorpej 		aprint_normal(" interface %d", uaa->ifaceno);
   1184       1.32  augustss #if 0
   1185       1.99  augustss 	/*
   1186       1.32  augustss 	 * It gets very crowded with these locators on the attach line.
   1187       1.32  augustss 	 * They are not really needed since they are printed in the clear
   1188       1.32  augustss 	 * by each driver.
   1189       1.32  augustss 	 */
   1190       1.32  augustss 	if (uaa->vendor != UHUB_UNK_VENDOR)
   1191      1.101   thorpej 		aprint_normal(" vendor 0x%04x", uaa->vendor);
   1192       1.32  augustss 	if (uaa->product != UHUB_UNK_PRODUCT)
   1193      1.101   thorpej 		aprint_normal(" product 0x%04x", uaa->product);
   1194       1.32  augustss 	if (uaa->release != UHUB_UNK_RELEASE)
   1195      1.101   thorpej 		aprint_normal(" release 0x%04x", uaa->release);
   1196       1.32  augustss #endif
   1197        1.1  augustss 	return (UNCONF);
   1198        1.1  augustss }
   1199        1.1  augustss 
   1200       1.37  augustss #if defined(__NetBSD__)
   1201        1.1  augustss int
   1202      1.118  drochner usbd_submatch(struct device *parent, struct cfdata *cf,
   1203      1.129  drochner 	      const int *ldesc, void *aux)
   1204        1.1  augustss {
   1205       1.37  augustss #elif defined(__OpenBSD__)
   1206       1.37  augustss int
   1207       1.78  augustss usbd_submatch(struct device *parent, void *match, void *aux)
   1208       1.37  augustss {
   1209       1.37  augustss 	struct cfdata *cf = match;
   1210       1.37  augustss #endif
   1211        1.1  augustss 	struct usb_attach_arg *uaa = aux;
   1212        1.1  augustss 
   1213       1.72  augustss 	DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
   1214       1.72  augustss 	    "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
   1215       1.72  augustss 	    uaa->port, cf->uhubcf_port,
   1216       1.72  augustss 	    uaa->configno, cf->uhubcf_configuration,
   1217       1.72  augustss 	    uaa->ifaceno, cf->uhubcf_interface,
   1218       1.72  augustss 	    uaa->vendor, cf->uhubcf_vendor,
   1219       1.72  augustss 	    uaa->product, cf->uhubcf_product,
   1220       1.72  augustss 	    uaa->release, cf->uhubcf_release));
   1221       1.72  augustss 	if (uaa->port != 0 &&	/* root hub has port 0, it should match */
   1222      1.117  drochner 	    ((cf->uhubcf_port != UHUB_UNK_PORT &&
   1223       1.72  augustss 	      cf->uhubcf_port != uaa->port) ||
   1224       1.72  augustss 	     (uaa->configno != UHUB_UNK_CONFIGURATION &&
   1225       1.72  augustss 	      cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
   1226       1.72  augustss 	      cf->uhubcf_configuration != uaa->configno) ||
   1227       1.72  augustss 	     (uaa->ifaceno != UHUB_UNK_INTERFACE &&
   1228       1.72  augustss 	      cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
   1229       1.72  augustss 	      cf->uhubcf_interface != uaa->ifaceno) ||
   1230       1.72  augustss 	     (uaa->vendor != UHUB_UNK_VENDOR &&
   1231       1.72  augustss 	      cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
   1232       1.72  augustss 	      cf->uhubcf_vendor != uaa->vendor) ||
   1233       1.72  augustss 	     (uaa->product != UHUB_UNK_PRODUCT &&
   1234       1.72  augustss 	      cf->uhubcf_product != UHUB_UNK_PRODUCT &&
   1235       1.72  augustss 	      cf->uhubcf_product != uaa->product) ||
   1236       1.72  augustss 	     (uaa->release != UHUB_UNK_RELEASE &&
   1237       1.72  augustss 	      cf->uhubcf_release != UHUB_UNK_RELEASE &&
   1238       1.72  augustss 	      cf->uhubcf_release != uaa->release)
   1239       1.72  augustss 	     )
   1240       1.32  augustss 	   )
   1241        1.1  augustss 		return 0;
   1242      1.100   thorpej 	return (config_match(parent, cf, aux));
   1243       1.13  augustss }
   1244       1.18  augustss 
   1245       1.18  augustss #endif
   1246       1.13  augustss 
   1247       1.13  augustss void
   1248       1.82  augustss usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
   1249       1.82  augustss 		     int usedev)
   1250       1.13  augustss {
   1251       1.13  augustss 	struct usbd_port *p;
   1252       1.56  augustss 	int i, err, s;
   1253       1.13  augustss 
   1254       1.98  christos 	di->udi_bus = USBDEVUNIT(dev->bus->bdev);
   1255       1.98  christos 	di->udi_addr = dev->address;
   1256       1.98  christos 	di->udi_cookie = dev->cookie;
   1257      1.123  augustss 	usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
   1258      1.113    itojun 	usbd_printBCD(di->udi_release, sizeof(di->udi_release),
   1259      1.113    itojun 	    UGETW(dev->ddesc.bcdDevice));
   1260      1.123  augustss 	di->udi_serial[0] = 0;
   1261      1.123  augustss 	if (usedev)
   1262      1.123  augustss 		(void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
   1263      1.123  augustss 				      di->udi_serial);
   1264       1.98  christos 	di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
   1265       1.98  christos 	di->udi_productNo = UGETW(dev->ddesc.idProduct);
   1266       1.98  christos 	di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
   1267       1.98  christos 	di->udi_class = dev->ddesc.bDeviceClass;
   1268       1.98  christos 	di->udi_subclass = dev->ddesc.bDeviceSubClass;
   1269       1.98  christos 	di->udi_protocol = dev->ddesc.bDeviceProtocol;
   1270       1.98  christos 	di->udi_config = dev->config;
   1271       1.98  christos 	di->udi_power = dev->self_powered ? 0 : dev->power;
   1272       1.98  christos 	di->udi_speed = dev->speed;
   1273       1.65  augustss 
   1274       1.65  augustss 	if (dev->subdevs != NULL) {
   1275       1.65  augustss 		for (i = 0; dev->subdevs[i] &&
   1276       1.65  augustss 			     i < USB_MAX_DEVNAMES; i++) {
   1277       1.98  christos 			strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
   1278       1.65  augustss 				USB_MAX_DEVNAMELEN);
   1279       1.98  christos 			di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
   1280       1.65  augustss                 }
   1281       1.65  augustss         } else {
   1282       1.65  augustss                 i = 0;
   1283       1.65  augustss         }
   1284       1.65  augustss         for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
   1285       1.98  christos                 di->udi_devnames[i][0] = 0;                 /* empty */
   1286       1.65  augustss 
   1287       1.13  augustss 	if (dev->hub) {
   1288       1.99  augustss 		for (i = 0;
   1289       1.98  christos 		     i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
   1290       1.13  augustss 			     i < dev->hub->hubdesc.bNbrPorts;
   1291       1.13  augustss 		     i++) {
   1292       1.13  augustss 			p = &dev->hub->ports[i];
   1293       1.13  augustss 			if (p->device)
   1294       1.56  augustss 				err = p->device->address;
   1295       1.13  augustss 			else {
   1296       1.13  augustss 				s = UGETW(p->status.wPortStatus);
   1297       1.13  augustss 				if (s & UPS_PORT_ENABLED)
   1298       1.56  augustss 					err = USB_PORT_ENABLED;
   1299       1.13  augustss 				else if (s & UPS_SUSPEND)
   1300       1.56  augustss 					err = USB_PORT_SUSPENDED;
   1301       1.13  augustss 				else if (s & UPS_PORT_POWER)
   1302       1.56  augustss 					err = USB_PORT_POWERED;
   1303       1.13  augustss 				else
   1304       1.56  augustss 					err = USB_PORT_DISABLED;
   1305       1.13  augustss 			}
   1306       1.98  christos 			di->udi_ports[i] = err;
   1307       1.13  augustss 		}
   1308       1.98  christos 		di->udi_nports = dev->hub->hubdesc.bNbrPorts;
   1309       1.13  augustss 	} else
   1310       1.98  christos 		di->udi_nports = 0;
   1311       1.34  augustss }
   1312       1.34  augustss 
   1313       1.34  augustss void
   1314       1.78  augustss usb_free_device(usbd_device_handle dev)
   1315       1.34  augustss {
   1316       1.34  augustss 	int ifcidx, nifc;
   1317       1.34  augustss 
   1318       1.53  augustss 	if (dev->default_pipe != NULL)
   1319       1.34  augustss 		usbd_kill_pipe(dev->default_pipe);
   1320       1.53  augustss 	if (dev->ifaces != NULL) {
   1321       1.34  augustss 		nifc = dev->cdesc->bNumInterface;
   1322       1.34  augustss 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
   1323       1.34  augustss 			usbd_free_iface_data(dev, ifcidx);
   1324       1.34  augustss 		free(dev->ifaces, M_USB);
   1325       1.34  augustss 	}
   1326       1.53  augustss 	if (dev->cdesc != NULL)
   1327       1.34  augustss 		free(dev->cdesc, M_USB);
   1328       1.53  augustss 	if (dev->subdevs != NULL)
   1329       1.34  augustss 		free(dev->subdevs, M_USB);
   1330       1.34  augustss 	free(dev, M_USB);
   1331        1.1  augustss }
   1332       1.47  augustss 
   1333       1.47  augustss /*
   1334       1.47  augustss  * The general mechanism for detaching drivers works as follows: Each
   1335       1.47  augustss  * driver is responsible for maintaining a reference count on the
   1336       1.47  augustss  * number of outstanding references to its softc (e.g.  from
   1337       1.47  augustss  * processing hanging in a read or write).  The detach method of the
   1338       1.47  augustss  * driver decrements this counter and flags in the softc that the
   1339       1.47  augustss  * driver is dying and then wakes any sleepers.  It then sleeps on the
   1340       1.47  augustss  * softc.  Each place that can sleep must maintain the reference
   1341       1.47  augustss  * count.  When the reference count drops to -1 (0 is the normal value
   1342       1.47  augustss  * of the reference count) the a wakeup on the softc is performed
   1343       1.47  augustss  * signaling to the detach waiter that all references are gone.
   1344       1.47  augustss  */
   1345       1.47  augustss 
   1346       1.47  augustss /*
   1347       1.47  augustss  * Called from process context when we discover that a port has
   1348       1.47  augustss  * been disconnected.
   1349       1.47  augustss  */
   1350       1.47  augustss void
   1351       1.78  augustss usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
   1352       1.47  augustss {
   1353       1.47  augustss 	usbd_device_handle dev = up->device;
   1354       1.51  augustss 	char *hubname = USBDEVPTRNAME(parent);
   1355       1.47  augustss 	int i;
   1356       1.47  augustss 
   1357       1.99  augustss 	DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
   1358       1.47  augustss 		    up, dev, up->portno));
   1359       1.48  augustss 
   1360       1.48  augustss #ifdef DIAGNOSTIC
   1361       1.57  augustss 	if (dev == NULL) {
   1362       1.48  augustss 		printf("usb_disconnect_port: no device\n");
   1363       1.48  augustss 		return;
   1364       1.48  augustss 	}
   1365       1.48  augustss #endif
   1366       1.47  augustss 
   1367       1.55  augustss 	if (dev->subdevs != NULL) {
   1368       1.57  augustss 		DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
   1369       1.47  augustss 		for (i = 0; dev->subdevs[i]; i++) {
   1370       1.99  augustss 			printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
   1371       1.51  augustss 			       hubname);
   1372       1.51  augustss 			if (up->portno != 0)
   1373       1.51  augustss 				printf(" port %d", up->portno);
   1374       1.51  augustss 			printf(" (addr %d) disconnected\n", dev->address);
   1375       1.47  augustss 			config_detach(dev->subdevs[i], DETACH_FORCE);
   1376      1.110  drochner 			dev->subdevs[i] = 0;
   1377       1.47  augustss 		}
   1378       1.47  augustss 	}
   1379       1.47  augustss 
   1380       1.65  augustss 	usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
   1381       1.57  augustss 	dev->bus->devices[dev->address] = NULL;
   1382       1.57  augustss 	up->device = NULL;
   1383       1.47  augustss 	usb_free_device(dev);
   1384       1.47  augustss }
   1385       1.47  augustss 
   1386       1.54  augustss #ifdef __OpenBSD__
   1387       1.79  augustss void *usb_realloc(void *p, u_int size, int pool, int flags)
   1388       1.54  augustss {
   1389       1.54  augustss 	void *q;
   1390       1.54  augustss 
   1391       1.54  augustss 	q = malloc(size, pool, flags);
   1392       1.54  augustss 	if (q == NULL)
   1393       1.54  augustss 		return (NULL);
   1394       1.54  augustss 	bcopy(p, q, size);
   1395       1.54  augustss 	free(p, pool);
   1396       1.54  augustss 	return (q);
   1397       1.54  augustss }
   1398       1.54  augustss #endif
   1399