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