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