Home | History | Annotate | Line # | Download | only in usb
usb_subr.c revision 1.196.4.6
      1  1.196.4.6    martin /*	$NetBSD: usb_subr.c,v 1.196.4.6 2019/11/16 16:13:56 martin Exp $	*/
      2       1.56  augustss /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
      3        1.1  augustss 
      4        1.1  augustss /*
      5      1.121   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      6        1.1  augustss  * All rights reserved.
      7        1.1  augustss  *
      8        1.9  augustss  * This code is derived from software contributed to The NetBSD Foundation
      9       1.76  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10        1.9  augustss  * Carlstedt Research & Technology.
     11        1.1  augustss  *
     12        1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13        1.1  augustss  * modification, are permitted provided that the following conditions
     14        1.1  augustss  * are met:
     15        1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16        1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17        1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18        1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19        1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20        1.1  augustss  *
     21        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22        1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23        1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24        1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25        1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26        1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27        1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28        1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29        1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30        1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31        1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     32        1.1  augustss  */
     33       1.91     lukem 
     34       1.91     lukem #include <sys/cdefs.h>
     35  1.196.4.6    martin __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.196.4.6 2019/11/16 16:13:56 martin Exp $");
     36      1.104    martin 
     37      1.186  christos #ifdef _KERNEL_OPT
     38      1.140     pavel #include "opt_compat_netbsd.h"
     39  1.196.4.3       snj #include "opt_usb.h"
     40      1.104    martin #include "opt_usbverbose.h"
     41      1.186  christos #endif
     42        1.1  augustss 
     43        1.1  augustss #include <sys/param.h>
     44        1.1  augustss #include <sys/systm.h>
     45        1.1  augustss #include <sys/kernel.h>
     46  1.196.4.2       snj #include <sys/kmem.h>
     47        1.1  augustss #include <sys/device.h>
     48       1.56  augustss #include <sys/select.h>
     49        1.1  augustss #include <sys/proc.h>
     50       1.46  augustss 
     51      1.149        ad #include <sys/bus.h>
     52      1.169  pgoyette #include <sys/module.h>
     53        1.1  augustss 
     54        1.1  augustss #include <dev/usb/usb.h>
     55        1.1  augustss 
     56        1.1  augustss #include <dev/usb/usbdi.h>
     57        1.1  augustss #include <dev/usb/usbdi_util.h>
     58        1.1  augustss #include <dev/usb/usbdivar.h>
     59        1.1  augustss #include <dev/usb/usbdevs.h>
     60        1.1  augustss #include <dev/usb/usb_quirks.h>
     61      1.169  pgoyette #include <dev/usb/usb_verbose.h>
     62  1.196.4.3       snj #include <dev/usb/usbhist.h>
     63        1.1  augustss 
     64      1.158  drochner #include "locators.h"
     65      1.158  drochner 
     66  1.196.4.3       snj #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usbdebug,FMT,A,B,C,D)
     67  1.196.4.3       snj #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
     68        1.1  augustss 
     69  1.196.4.3       snj Static usbd_status usbd_set_config(struct usbd_device *, int);
     70  1.196.4.3       snj Static void usbd_devinfo(struct usbd_device *, int, char *, size_t);
     71  1.196.4.3       snj Static void usbd_devinfo_vp(struct usbd_device *, char *, size_t, char *, size_t,
     72      1.175  christos     int, int);
     73  1.196.4.3       snj Static int usbd_getnewaddr(struct usbd_bus *);
     74      1.118  drochner Static int usbd_print(void *, const char *);
     75      1.144  drochner Static int usbd_ifprint(void *, const char *);
     76  1.196.4.3       snj Static void usbd_free_iface_data(struct usbd_device *, int);
     77        1.1  augustss 
     78      1.193  jakllsch uint32_t usb_cookie_no = 0;
     79       1.27  augustss 
     80       1.84  jdolecek Static const char * const usbd_error_strs[] = {
     81       1.12  augustss 	"NORMAL_COMPLETION",
     82       1.12  augustss 	"IN_PROGRESS",
     83       1.12  augustss 	"PENDING_REQUESTS",
     84       1.12  augustss 	"NOT_STARTED",
     85       1.12  augustss 	"INVAL",
     86       1.12  augustss 	"NOMEM",
     87       1.12  augustss 	"CANCELLED",
     88       1.12  augustss 	"BAD_ADDRESS",
     89       1.12  augustss 	"IN_USE",
     90       1.12  augustss 	"NO_ADDR",
     91       1.12  augustss 	"SET_ADDR_FAILED",
     92       1.12  augustss 	"NO_POWER",
     93       1.12  augustss 	"TOO_DEEP",
     94       1.12  augustss 	"IOERROR",
     95       1.12  augustss 	"NOT_CONFIGURED",
     96       1.12  augustss 	"TIMEOUT",
     97       1.12  augustss 	"SHORT_XFER",
     98       1.12  augustss 	"STALLED",
     99       1.33  augustss 	"INTERRUPTED",
    100       1.12  augustss 	"XXX",
    101       1.12  augustss };
    102        1.1  augustss 
    103      1.171  pgoyette void usb_load_verbose(void);
    104      1.171  pgoyette 
    105      1.175  christos void get_usb_vendor_stub(char *, size_t, usb_vendor_id_t);
    106      1.175  christos void get_usb_product_stub(char *, size_t, usb_vendor_id_t, usb_product_id_t);
    107      1.171  pgoyette 
    108      1.175  christos void (*get_usb_vendor)(char *, size_t, usb_vendor_id_t) = get_usb_vendor_stub;
    109      1.175  christos void (*get_usb_product)(char *, size_t, usb_vendor_id_t, usb_product_id_t) =
    110      1.171  pgoyette 	get_usb_product_stub;
    111      1.171  pgoyette 
    112      1.171  pgoyette int usb_verbose_loaded = 0;
    113      1.171  pgoyette 
    114      1.171  pgoyette /*
    115      1.171  pgoyette  * Load the usbverbose module
    116      1.171  pgoyette  */
    117      1.171  pgoyette void usb_load_verbose(void)
    118      1.171  pgoyette {
    119      1.176  pgoyette 	if (usb_verbose_loaded == 0)
    120      1.173  pgoyette 		module_autoload("usbverbose", MODULE_CLASS_MISC);
    121      1.171  pgoyette }
    122      1.169  pgoyette 
    123      1.175  christos void get_usb_vendor_stub(char *v, size_t l, usb_vendor_id_t v_id)
    124      1.169  pgoyette {
    125      1.171  pgoyette 	usb_load_verbose();
    126      1.171  pgoyette 	if (usb_verbose_loaded)
    127      1.175  christos 		get_usb_vendor(v, l, v_id);
    128      1.169  pgoyette }
    129      1.169  pgoyette 
    130      1.175  christos void get_usb_product_stub(char *p, size_t l, usb_vendor_id_t v_id,
    131      1.175  christos     usb_product_id_t p_id)
    132      1.169  pgoyette {
    133      1.171  pgoyette 	usb_load_verbose();
    134      1.171  pgoyette 	if (usb_verbose_loaded)
    135      1.175  christos 		get_usb_product(p, l, v_id, p_id);
    136      1.169  pgoyette }
    137      1.169  pgoyette 
    138       1.42   thorpej const char *
    139       1.78  augustss usbd_errstr(usbd_status err)
    140       1.39  augustss {
    141       1.39  augustss 	static char buffer[5];
    142       1.39  augustss 
    143       1.42   thorpej 	if (err < USBD_ERROR_MAX) {
    144       1.39  augustss 		return usbd_error_strs[err];
    145       1.39  augustss 	} else {
    146  1.196.4.3       snj 		snprintf(buffer, sizeof(buffer), "%d", err);
    147       1.39  augustss 		return buffer;
    148       1.39  augustss 	}
    149       1.39  augustss }
    150       1.39  augustss 
    151       1.13  augustss usbd_status
    152  1.196.4.3       snj usbd_get_string_desc(struct usbd_device *dev, int sindex, int langid,
    153      1.114   mycroft 		     usb_string_descriptor_t *sdesc, int *sizep)
    154       1.13  augustss {
    155  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    156       1.13  augustss 	usb_device_request_t req;
    157      1.116   mycroft 	usbd_status err;
    158      1.116   mycroft 	int actlen;
    159       1.13  augustss 
    160  1.196.4.6    martin 	/*
    161  1.196.4.6    martin 	 * Pass a full-sized buffer to usbd_do_request_len().  At least
    162  1.196.4.6    martin 	 * one device has been seen returning additional data beyond the
    163  1.196.4.6    martin 	 * provided buffers (2-bytes written shortly after the request
    164  1.196.4.6    martin 	 * claims to have completed and returned the 2 byte header,
    165  1.196.4.6    martin 	 * corrupting other memory.)
    166  1.196.4.6    martin 	 */
    167       1.13  augustss 	req.bmRequestType = UT_READ_DEVICE;
    168       1.13  augustss 	req.bRequest = UR_GET_DESCRIPTOR;
    169       1.13  augustss 	USETW2(req.wValue, UDESC_STRING, sindex);
    170       1.13  augustss 	USETW(req.wIndex, langid);
    171      1.146  christos 	USETW(req.wLength, 2);	/* only size byte first */
    172  1.196.4.6    martin 	err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc,
    173  1.196.4.6    martin 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    174      1.116   mycroft 	if (err)
    175  1.196.4.3       snj 		return err;
    176      1.116   mycroft 
    177      1.146  christos 	if (actlen < 2)
    178  1.196.4.3       snj 		return USBD_SHORT_XFER;
    179      1.146  christos 
    180      1.146  christos 	USETW(req.wLength, sdesc->bLength);	/* the whole string */
    181  1.196.4.6    martin 	err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc,
    182  1.196.4.6    martin 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    183      1.146  christos 	if (err)
    184  1.196.4.3       snj 		return err;
    185      1.146  christos 
    186      1.116   mycroft 	if (actlen != sdesc->bLength) {
    187  1.196.4.3       snj 		DPRINTF("expected %d, got %d", sdesc->bLength, actlen, 0, 0);
    188      1.116   mycroft 	}
    189      1.116   mycroft 
    190      1.116   mycroft 	*sizep = actlen;
    191  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
    192       1.13  augustss }
    193       1.13  augustss 
    194      1.128     enami static void
    195      1.128     enami usbd_trim_spaces(char *p)
    196       1.83  augustss {
    197      1.128     enami 	char *q, *e;
    198       1.83  augustss 
    199      1.128     enami 	q = e = p;
    200      1.128     enami 	while (*q == ' ')		/* skip leading spaces */
    201       1.85  augustss 		q++;
    202      1.128     enami 	while ((*p = *q++))		/* copy string */
    203      1.128     enami 		if (*p++ != ' ')	/* remember last non-space */
    204      1.128     enami 			e = p;
    205      1.128     enami 	*e = '\0';			/* kill trailing spaces */
    206       1.83  augustss }
    207       1.83  augustss 
    208  1.196.4.2       snj static void
    209  1.196.4.2       snj usbd_get_device_string(struct usbd_device *ud, uByte index, char **buf)
    210  1.196.4.2       snj {
    211  1.196.4.2       snj 	char *b = kmem_alloc(USB_MAX_ENCODED_STRING_LEN, KM_SLEEP);
    212  1.196.4.2       snj 	if (b) {
    213  1.196.4.2       snj 		usbd_status err = usbd_get_string0(ud, index, b, true);
    214  1.196.4.2       snj 		if (err != USBD_NORMAL_COMPLETION) {
    215  1.196.4.2       snj 			kmem_free(b, USB_MAX_ENCODED_STRING_LEN);
    216  1.196.4.2       snj 			b = NULL;
    217  1.196.4.2       snj 		} else {
    218  1.196.4.2       snj 			usbd_trim_spaces(b);
    219  1.196.4.2       snj 		}
    220  1.196.4.2       snj 	}
    221  1.196.4.2       snj 	*buf = b;
    222  1.196.4.2       snj }
    223  1.196.4.2       snj 
    224  1.196.4.2       snj void
    225  1.196.4.2       snj usbd_get_device_strings(struct usbd_device *ud)
    226  1.196.4.2       snj {
    227  1.196.4.3       snj 	usb_device_descriptor_t *udd = &ud->ud_ddesc;
    228  1.196.4.2       snj 
    229  1.196.4.2       snj 	usbd_get_device_string(ud, udd->iManufacturer, &ud->ud_vendor);
    230  1.196.4.2       snj 	usbd_get_device_string(ud, udd->iProduct, &ud->ud_product);
    231  1.196.4.2       snj 	usbd_get_device_string(ud, udd->iSerialNumber, &ud->ud_serial);
    232  1.196.4.2       snj }
    233  1.196.4.2       snj 
    234  1.196.4.2       snj 
    235      1.123  augustss Static void
    236  1.196.4.3       snj usbd_devinfo_vp(struct usbd_device *dev, char *v, size_t vl, char *p,
    237      1.175  christos     size_t pl, int usedev, int useencoded)
    238        1.1  augustss {
    239  1.196.4.3       snj 	usb_device_descriptor_t *udd = &dev->ud_ddesc;
    240      1.175  christos 	if (dev == NULL)
    241      1.175  christos 		return;
    242        1.1  augustss 
    243      1.128     enami 	v[0] = p[0] = '\0';
    244       1.52  augustss 
    245       1.82  augustss 	if (usedev) {
    246      1.139     pavel 		if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
    247      1.128     enami 		    USBD_NORMAL_COMPLETION)
    248      1.128     enami 			usbd_trim_spaces(v);
    249      1.139     pavel 		if (usbd_get_string0(dev, udd->iProduct, p, useencoded) ==
    250      1.128     enami 		    USBD_NORMAL_COMPLETION)
    251      1.128     enami 			usbd_trim_spaces(p);
    252  1.196.4.2       snj 	} else {
    253  1.196.4.2       snj 		if (dev->ud_vendor) {
    254  1.196.4.2       snj 			strlcpy(v, dev->ud_vendor, vl);
    255  1.196.4.2       snj 		}
    256  1.196.4.2       snj 		if (dev->ud_product) {
    257  1.196.4.2       snj 			strlcpy(p, dev->ud_product, pl);
    258  1.196.4.2       snj 		}
    259       1.82  augustss 	}
    260      1.128     enami 	if (v[0] == '\0')
    261      1.175  christos 		get_usb_vendor(v, vl, UGETW(udd->idVendor));
    262      1.128     enami 	if (p[0] == '\0')
    263      1.175  christos 		get_usb_product(p, pl, UGETW(udd->idVendor),
    264      1.175  christos 		    UGETW(udd->idProduct));
    265        1.1  augustss }
    266        1.1  augustss 
    267        1.1  augustss int
    268      1.113    itojun usbd_printBCD(char *cp, size_t l, int bcd)
    269        1.1  augustss {
    270      1.175  christos 	return snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff);
    271        1.1  augustss }
    272        1.1  augustss 
    273      1.124  augustss Static void
    274  1.196.4.3       snj usbd_devinfo(struct usbd_device *dev, int showclass, char *cp, size_t l)
    275        1.1  augustss {
    276  1.196.4.3       snj 	usb_device_descriptor_t *udd = &dev->ud_ddesc;
    277      1.133  christos 	char *vendor, *product;
    278        1.1  augustss 	int bcdDevice, bcdUSB;
    279      1.113    itojun 	char *ep;
    280      1.113    itojun 
    281  1.196.4.3       snj 	vendor = kmem_alloc(USB_MAX_ENCODED_STRING_LEN * 2, KM_SLEEP);
    282      1.133  christos 	if (vendor == NULL) {
    283      1.133  christos 		*cp = '\0';
    284      1.133  christos 		return;
    285      1.133  christos 	}
    286      1.133  christos 	product = &vendor[USB_MAX_ENCODED_STRING_LEN];
    287      1.133  christos 
    288      1.113    itojun 	ep = cp + l;
    289        1.1  augustss 
    290      1.175  christos 	usbd_devinfo_vp(dev, vendor, USB_MAX_ENCODED_STRING_LEN,
    291  1.196.4.2       snj 	    product, USB_MAX_ENCODED_STRING_LEN, 0, 1);
    292      1.113    itojun 	cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
    293        1.1  augustss 	if (showclass)
    294      1.113    itojun 		cp += snprintf(cp, ep - cp, ", class %d/%d",
    295      1.113    itojun 		    udd->bDeviceClass, udd->bDeviceSubClass);
    296        1.1  augustss 	bcdUSB = UGETW(udd->bcdUSB);
    297        1.1  augustss 	bcdDevice = UGETW(udd->bcdDevice);
    298      1.113    itojun 	cp += snprintf(cp, ep - cp, ", rev ");
    299      1.113    itojun 	cp += usbd_printBCD(cp, ep - cp, bcdUSB);
    300        1.1  augustss 	*cp++ = '/';
    301      1.113    itojun 	cp += usbd_printBCD(cp, ep - cp, bcdDevice);
    302  1.196.4.3       snj 	cp += snprintf(cp, ep - cp, ", addr %d", dev->ud_addr);
    303       1.10  augustss 	*cp = 0;
    304  1.196.4.3       snj 	kmem_free(vendor, USB_MAX_ENCODED_STRING_LEN * 2);
    305        1.1  augustss }
    306        1.1  augustss 
    307      1.124  augustss char *
    308  1.196.4.3       snj usbd_devinfo_alloc(struct usbd_device *dev, int showclass)
    309      1.124  augustss {
    310      1.124  augustss 	char *devinfop;
    311      1.124  augustss 
    312  1.196.4.3       snj 	devinfop = kmem_alloc(DEVINFOSIZE, KM_SLEEP);
    313      1.124  augustss 	usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE);
    314      1.124  augustss 	return devinfop;
    315      1.124  augustss }
    316      1.124  augustss 
    317      1.124  augustss void
    318      1.124  augustss usbd_devinfo_free(char *devinfop)
    319      1.124  augustss {
    320  1.196.4.3       snj 	kmem_free(devinfop, DEVINFOSIZE);
    321      1.124  augustss }
    322      1.124  augustss 
    323        1.1  augustss /* Delay for a certain number of ms */
    324        1.1  augustss void
    325  1.196.4.3       snj usb_delay_ms_locked(struct usbd_bus *bus, u_int ms, kmutex_t *lock)
    326        1.1  augustss {
    327        1.1  augustss 	/* Wait at least two clock ticks so we know the time has passed. */
    328  1.196.4.3       snj 	if (bus->ub_usepolling || cold)
    329        1.1  augustss 		delay((ms+1) * 1000);
    330        1.1  augustss 	else
    331      1.182       mrg 		kpause("usbdly", false, (ms*hz+999)/1000 + 1, lock);
    332      1.182       mrg }
    333      1.182       mrg 
    334      1.182       mrg void
    335  1.196.4.3       snj usb_delay_ms(struct usbd_bus *bus, u_int ms)
    336      1.182       mrg {
    337      1.182       mrg 	usb_delay_ms_locked(bus, ms, NULL);
    338      1.182       mrg }
    339      1.182       mrg 
    340      1.182       mrg /* Delay given a device handle. */
    341      1.182       mrg void
    342  1.196.4.3       snj usbd_delay_ms_locked(struct usbd_device *dev, u_int ms, kmutex_t *lock)
    343      1.182       mrg {
    344  1.196.4.3       snj 	usb_delay_ms_locked(dev->ud_bus, ms, lock);
    345        1.1  augustss }
    346        1.1  augustss 
    347       1.23  augustss /* Delay given a device handle. */
    348       1.23  augustss void
    349  1.196.4.3       snj usbd_delay_ms(struct usbd_device *dev, u_int ms)
    350       1.23  augustss {
    351  1.196.4.3       snj 	usb_delay_ms_locked(dev->ud_bus, ms, NULL);
    352       1.23  augustss }
    353       1.23  augustss 
    354        1.1  augustss usbd_status
    355  1.196.4.3       snj usbd_reset_port(struct usbd_device *dev, int port, usb_port_status_t *ps)
    356        1.1  augustss {
    357  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    358        1.1  augustss 	usb_device_request_t req;
    359       1.53  augustss 	usbd_status err;
    360        1.1  augustss 	int n;
    361       1.99  augustss 
    362        1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    363        1.1  augustss 	req.bRequest = UR_SET_FEATURE;
    364        1.1  augustss 	USETW(req.wValue, UHF_PORT_RESET);
    365        1.1  augustss 	USETW(req.wIndex, port);
    366        1.1  augustss 	USETW(req.wLength, 0);
    367       1.53  augustss 	err = usbd_do_request(dev, &req, 0);
    368  1.196.4.3       snj 	DPRINTFN(1, "port %d reset done, error=%d", port, err, 0, 0);
    369       1.53  augustss 	if (err)
    370  1.196.4.3       snj 		return err;
    371        1.1  augustss 	n = 10;
    372        1.1  augustss 	do {
    373        1.1  augustss 		/* Wait for device to recover from reset. */
    374       1.23  augustss 		usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
    375       1.53  augustss 		err = usbd_get_port_status(dev, port, ps);
    376       1.53  augustss 		if (err) {
    377  1.196.4.3       snj 			DPRINTF("get status failed %d", err, 0, 0, 0);
    378  1.196.4.3       snj 			return err;
    379        1.1  augustss 		}
    380       1.92  augustss 		/* If the device disappeared, just give up. */
    381       1.92  augustss 		if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
    382  1.196.4.3       snj 			return USBD_NORMAL_COMPLETION;
    383        1.1  augustss 	} while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
    384       1.73  augustss 	if (n == 0)
    385  1.196.4.3       snj 		return USBD_TIMEOUT;
    386       1.53  augustss 	err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    387        1.1  augustss #ifdef USB_DEBUG
    388       1.53  augustss 	if (err)
    389  1.196.4.3       snj 		DPRINTF("clear port feature failed %d", err, 0, 0, 0);
    390        1.1  augustss #endif
    391       1.18  augustss 
    392       1.18  augustss 	/* Wait for the device to recover from reset. */
    393       1.23  augustss 	usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
    394  1.196.4.3       snj 	return err;
    395        1.1  augustss }
    396        1.1  augustss 
    397       1.12  augustss usb_interface_descriptor_t *
    398       1.78  augustss usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
    399       1.12  augustss {
    400  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    401       1.12  augustss 	char *p = (char *)cd;
    402       1.12  augustss 	char *end = p + UGETW(cd->wTotalLength);
    403       1.12  augustss 	usb_interface_descriptor_t *d;
    404       1.19  augustss 	int curidx, lastidx, curaidx = 0;
    405       1.12  augustss 
    406       1.19  augustss 	for (curidx = lastidx = -1; p < end; ) {
    407       1.12  augustss 		d = (usb_interface_descriptor_t *)p;
    408  1.196.4.3       snj 		DPRINTFN(4, "idx=%d(%d) altidx=%d(%d)", ifaceidx, curidx,
    409  1.196.4.3       snj 		    altidx, curaidx);
    410  1.196.4.3       snj 		DPRINTFN(4, "len=%d type=%d", d->bLength, d->bDescriptorType,
    411  1.196.4.3       snj 		    0, 0);
    412       1.12  augustss 		if (d->bLength == 0) /* bad descriptor */
    413       1.12  augustss 			break;
    414       1.12  augustss 		p += d->bLength;
    415       1.12  augustss 		if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
    416       1.19  augustss 			if (d->bInterfaceNumber != lastidx) {
    417       1.19  augustss 				lastidx = d->bInterfaceNumber;
    418       1.12  augustss 				curidx++;
    419       1.12  augustss 				curaidx = 0;
    420       1.12  augustss 			} else
    421       1.12  augustss 				curaidx++;
    422       1.12  augustss 			if (ifaceidx == curidx && altidx == curaidx)
    423  1.196.4.3       snj 				return d;
    424       1.12  augustss 		}
    425       1.12  augustss 	}
    426  1.196.4.3       snj 	return NULL;
    427       1.12  augustss }
    428       1.12  augustss 
    429       1.12  augustss usb_endpoint_descriptor_t *
    430       1.99  augustss usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
    431       1.78  augustss 		int endptidx)
    432       1.12  augustss {
    433       1.12  augustss 	char *p = (char *)cd;
    434       1.12  augustss 	char *end = p + UGETW(cd->wTotalLength);
    435       1.12  augustss 	usb_interface_descriptor_t *d;
    436       1.12  augustss 	usb_endpoint_descriptor_t *e;
    437       1.12  augustss 	int curidx;
    438       1.12  augustss 
    439       1.12  augustss 	d = usbd_find_idesc(cd, ifaceidx, altidx);
    440       1.53  augustss 	if (d == NULL)
    441  1.196.4.3       snj 		return NULL;
    442       1.12  augustss 	if (endptidx >= d->bNumEndpoints) /* quick exit */
    443  1.196.4.3       snj 		return NULL;
    444       1.12  augustss 
    445       1.12  augustss 	curidx = -1;
    446       1.12  augustss 	for (p = (char *)d + d->bLength; p < end; ) {
    447       1.12  augustss 		e = (usb_endpoint_descriptor_t *)p;
    448       1.12  augustss 		if (e->bLength == 0) /* bad descriptor */
    449       1.12  augustss 			break;
    450       1.12  augustss 		p += e->bLength;
    451       1.12  augustss 		if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
    452  1.196.4.3       snj 			return NULL;
    453       1.12  augustss 		if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
    454       1.12  augustss 			curidx++;
    455       1.12  augustss 			if (curidx == endptidx)
    456  1.196.4.3       snj 				return e;
    457       1.12  augustss 		}
    458        1.1  augustss 	}
    459  1.196.4.3       snj 	return NULL;
    460        1.1  augustss }
    461        1.1  augustss 
    462        1.1  augustss usbd_status
    463  1.196.4.3       snj usbd_fill_iface_data(struct usbd_device *dev, int ifaceidx, int altidx)
    464        1.1  augustss {
    465  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    466  1.196.4.3       snj 	struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx];
    467       1.77  augustss 	usb_interface_descriptor_t *idesc;
    468        1.1  augustss 	char *p, *end;
    469        1.1  augustss 	int endpt, nendpt;
    470        1.1  augustss 
    471  1.196.4.3       snj 	DPRINTFN(4, "ifaceidx=%d altidx=%d", ifaceidx, altidx, 0, 0);
    472  1.196.4.3       snj 	idesc = usbd_find_idesc(dev->ud_cdesc, ifaceidx, altidx);
    473       1.77  augustss 	if (idesc == NULL)
    474  1.196.4.3       snj 		return USBD_INVAL;
    475  1.196.4.3       snj 	ifc->ui_dev = dev;
    476  1.196.4.3       snj 	ifc->ui_idesc = idesc;
    477  1.196.4.3       snj 	ifc->ui_index = ifaceidx;
    478  1.196.4.3       snj 	ifc->ui_altindex = altidx;
    479  1.196.4.3       snj 	nendpt = ifc->ui_idesc->bNumEndpoints;
    480  1.196.4.3       snj 	DPRINTFN(4, "found idesc nendpt=%d", nendpt, 0, 0, 0);
    481        1.1  augustss 	if (nendpt != 0) {
    482  1.196.4.3       snj 		ifc->ui_endpoints = kmem_alloc(nendpt * sizeof(struct usbd_endpoint),
    483  1.196.4.3       snj 				KM_SLEEP);
    484  1.196.4.3       snj 		if (ifc->ui_endpoints == NULL)
    485  1.196.4.3       snj 			return USBD_NOMEM;
    486        1.1  augustss 	} else
    487  1.196.4.3       snj 		ifc->ui_endpoints = NULL;
    488  1.196.4.3       snj 	ifc->ui_priv = NULL;
    489  1.196.4.3       snj 	p = (char *)ifc->ui_idesc + ifc->ui_idesc->bLength;
    490  1.196.4.3       snj 	end = (char *)dev->ud_cdesc + UGETW(dev->ud_cdesc->wTotalLength);
    491       1.19  augustss #define ed ((usb_endpoint_descriptor_t *)p)
    492        1.1  augustss 	for (endpt = 0; endpt < nendpt; endpt++) {
    493  1.196.4.3       snj 		DPRINTFN(10, "endpt=%d", endpt, 0, 0, 0);
    494        1.1  augustss 		for (; p < end; p += ed->bLength) {
    495  1.196.4.3       snj 			DPRINTFN(10, "p=%p end=%p len=%d type=%d",
    496  1.196.4.3       snj 			    p, end, ed->bLength, ed->bDescriptorType);
    497       1.24  augustss 			if (p + ed->bLength <= end && ed->bLength != 0 &&
    498        1.1  augustss 			    ed->bDescriptorType == UDESC_ENDPOINT)
    499       1.24  augustss 				goto found;
    500       1.59  augustss 			if (ed->bLength == 0 ||
    501       1.59  augustss 			    ed->bDescriptorType == UDESC_INTERFACE)
    502        1.1  augustss 				break;
    503        1.1  augustss 		}
    504       1.24  augustss 		/* passed end, or bad desc */
    505       1.95  augustss 		printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
    506       1.95  augustss 		       ed->bLength == 0 ? "0 length" :
    507       1.95  augustss 		       ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
    508       1.95  augustss 		       "out of data");
    509       1.24  augustss 		goto bad;
    510       1.24  augustss 	found:
    511  1.196.4.3       snj 		ifc->ui_endpoints[endpt].ue_edesc = ed;
    512  1.196.4.3       snj 		if (dev->ud_speed == USB_SPEED_HIGH) {
    513       1.95  augustss 			u_int mps;
    514       1.95  augustss 			/* Control and bulk endpoints have max packet limits. */
    515       1.95  augustss 			switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
    516       1.95  augustss 			case UE_CONTROL:
    517       1.95  augustss 				mps = USB_2_MAX_CTRL_PACKET;
    518       1.95  augustss 				goto check;
    519       1.95  augustss 			case UE_BULK:
    520       1.95  augustss 				mps = USB_2_MAX_BULK_PACKET;
    521       1.95  augustss 			check:
    522       1.95  augustss 				if (UGETW(ed->wMaxPacketSize) != mps) {
    523       1.95  augustss 					USETW(ed->wMaxPacketSize, mps);
    524       1.95  augustss #ifdef DIAGNOSTIC
    525       1.95  augustss 					printf("usbd_fill_iface_data: bad max "
    526       1.95  augustss 					       "packet size\n");
    527       1.95  augustss #endif
    528       1.95  augustss 				}
    529       1.95  augustss 				break;
    530       1.95  augustss 			default:
    531       1.95  augustss 				break;
    532       1.95  augustss 			}
    533       1.95  augustss 		}
    534  1.196.4.3       snj 		ifc->ui_endpoints[endpt].ue_refcnt = 0;
    535  1.196.4.3       snj 		ifc->ui_endpoints[endpt].ue_toggle = 0;
    536       1.24  augustss 		p += ed->bLength;
    537        1.1  augustss 	}
    538       1.19  augustss #undef ed
    539  1.196.4.3       snj 	LIST_INIT(&ifc->ui_pipes);
    540  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
    541       1.24  augustss 
    542        1.1  augustss  bad:
    543  1.196.4.3       snj 	if (ifc->ui_endpoints != NULL) {
    544  1.196.4.3       snj 		kmem_free(ifc->ui_endpoints, nendpt * sizeof(struct usbd_endpoint));
    545  1.196.4.3       snj 		ifc->ui_endpoints = NULL;
    546       1.77  augustss 	}
    547  1.196.4.3       snj 	return USBD_INVAL;
    548        1.1  augustss }
    549        1.1  augustss 
    550        1.1  augustss void
    551  1.196.4.3       snj usbd_free_iface_data(struct usbd_device *dev, int ifcno)
    552        1.1  augustss {
    553  1.196.4.3       snj 	struct usbd_interface *ifc = &dev->ud_ifaces[ifcno];
    554  1.196.4.3       snj 	if (ifc->ui_endpoints) {
    555  1.196.4.3       snj 		int nendpt = ifc->ui_idesc->bNumEndpoints;
    556  1.196.4.3       snj 		size_t sz = nendpt * sizeof(struct usbd_endpoint);
    557  1.196.4.3       snj 		kmem_free(ifc->ui_endpoints, sz);
    558  1.196.4.3       snj 	}
    559        1.1  augustss }
    560        1.1  augustss 
    561       1.69  augustss Static usbd_status
    562  1.196.4.3       snj usbd_set_config(struct usbd_device *dev, int conf)
    563        1.7  augustss {
    564  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    565        1.7  augustss 	usb_device_request_t req;
    566        1.7  augustss 
    567  1.196.4.3       snj 	DPRINTFN(5, "dev %p conf %d", dev, conf, 0, 0);
    568  1.196.4.3       snj 
    569        1.7  augustss 	req.bmRequestType = UT_WRITE_DEVICE;
    570        1.7  augustss 	req.bRequest = UR_SET_CONFIG;
    571        1.7  augustss 	USETW(req.wValue, conf);
    572        1.7  augustss 	USETW(req.wIndex, 0);
    573        1.7  augustss 	USETW(req.wLength, 0);
    574  1.196.4.3       snj 	return usbd_do_request(dev, &req, 0);
    575        1.7  augustss }
    576        1.7  augustss 
    577        1.1  augustss usbd_status
    578  1.196.4.3       snj usbd_set_config_no(struct usbd_device *dev, int no, int msg)
    579        1.1  augustss {
    580  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    581       1.12  augustss 	usb_config_descriptor_t cd;
    582       1.53  augustss 	usbd_status err;
    583  1.196.4.3       snj 	int index;
    584       1.12  augustss 
    585       1.75  augustss 	if (no == USB_UNCONFIG_NO)
    586  1.196.4.3       snj 		return usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg);
    587       1.75  augustss 
    588  1.196.4.3       snj 	DPRINTFN(5, "%d", no, 0, 0, 0);
    589       1.12  augustss 	/* Figure out what config index to use. */
    590  1.196.4.3       snj 	for (index = 0; index < dev->ud_ddesc.bNumConfigurations; index++) {
    591       1.53  augustss 		err = usbd_get_config_desc(dev, index, &cd);
    592       1.53  augustss 		if (err)
    593  1.196.4.3       snj 			return err;
    594       1.12  augustss 		if (cd.bConfigurationValue == no)
    595  1.196.4.3       snj 			return usbd_set_config_index(dev, index, msg);
    596       1.12  augustss 	}
    597  1.196.4.3       snj 	return USBD_INVAL;
    598       1.12  augustss }
    599       1.12  augustss 
    600       1.12  augustss usbd_status
    601  1.196.4.3       snj usbd_set_config_index(struct usbd_device *dev, int index, int msg)
    602       1.12  augustss {
    603  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    604        1.1  augustss 	usb_config_descriptor_t cd, *cdp;
    605  1.196.4.3       snj 	usb_bos_descriptor_t *bdp = NULL;
    606       1.53  augustss 	usbd_status err;
    607      1.103  augustss 	int i, ifcidx, nifc, len, selfpowered, power;
    608        1.1  augustss 
    609  1.196.4.3       snj 	DPRINTFN(5, "dev=%p index=%d", dev, index, 0, 0);
    610        1.1  augustss 
    611  1.196.4.3       snj 	if (index >= dev->ud_ddesc.bNumConfigurations &&
    612      1.161  drochner 	    index != USB_UNCONFIG_INDEX) {
    613      1.145  drochner 		/* panic? */
    614      1.145  drochner 		printf("usbd_set_config_index: illegal index\n");
    615  1.196.4.3       snj 		return USBD_INVAL;
    616      1.145  drochner 	}
    617      1.145  drochner 
    618        1.1  augustss 	/* XXX check that all interfaces are idle */
    619  1.196.4.3       snj 	if (dev->ud_config != USB_UNCONFIG_NO) {
    620  1.196.4.3       snj 		DPRINTF("free old config", 0, 0, 0, 0);
    621        1.1  augustss 		/* Free all configuration data structures. */
    622  1.196.4.3       snj 		nifc = dev->ud_cdesc->bNumInterface;
    623       1.12  augustss 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
    624       1.12  augustss 			usbd_free_iface_data(dev, ifcidx);
    625  1.196.4.3       snj 		kmem_free(dev->ud_ifaces, nifc * sizeof(struct usbd_interface));
    626  1.196.4.3       snj 		kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
    627  1.196.4.3       snj 		if (dev->ud_bdesc != NULL)
    628  1.196.4.3       snj 			kmem_free(dev->ud_bdesc,
    629  1.196.4.3       snj 			    UGETW(dev->ud_bdesc->wTotalLength));
    630  1.196.4.3       snj 		dev->ud_ifaces = NULL;
    631  1.196.4.3       snj 		dev->ud_cdesc = NULL;
    632  1.196.4.3       snj 		dev->ud_bdesc = NULL;
    633  1.196.4.3       snj 		dev->ud_config = USB_UNCONFIG_NO;
    634       1.75  augustss 	}
    635       1.75  augustss 
    636       1.75  augustss 	if (index == USB_UNCONFIG_INDEX) {
    637       1.75  augustss 		/* We are unconfiguring the device, so leave unallocated. */
    638  1.196.4.3       snj 		DPRINTF("set config 0", 0, 0, 0, 0);
    639       1.75  augustss 		err = usbd_set_config(dev, USB_UNCONFIG_NO);
    640      1.136  christos 		if (err) {
    641  1.196.4.3       snj 			DPRINTF("setting config=0 failed, err = %d", err,
    642  1.196.4.3       snj 			    0, 0, 0);
    643      1.136  christos 		}
    644  1.196.4.3       snj 		return err;
    645        1.1  augustss 	}
    646        1.1  augustss 
    647       1.74  augustss 	/* Get the short descriptor. */
    648       1.53  augustss 	err = usbd_get_config_desc(dev, index, &cd);
    649      1.172  drochner 	if (err) {
    650  1.196.4.3       snj 		DPRINTF("get_config_desc=%d", err, 0, 0, 0);
    651  1.196.4.3       snj 		return err;
    652      1.172  drochner 	}
    653        1.1  augustss 	len = UGETW(cd.wTotalLength);
    654  1.196.4.4       snj 	if (len == 0) {
    655  1.196.4.4       snj 		DPRINTF("empty short descriptor", 0, 0, 0, 0);
    656  1.196.4.4       snj 		return USBD_INVAL;
    657  1.196.4.4       snj 	}
    658  1.196.4.3       snj 	cdp = kmem_alloc(len, KM_SLEEP);
    659       1.53  augustss 	if (cdp == NULL)
    660  1.196.4.3       snj 		return USBD_NOMEM;
    661      1.103  augustss 
    662      1.103  augustss 	/* Get the full descriptor.  Try a few times for slow devices. */
    663      1.103  augustss 	for (i = 0; i < 3; i++) {
    664      1.103  augustss 		err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
    665      1.103  augustss 		if (!err)
    666      1.103  augustss 			break;
    667      1.103  augustss 		usbd_delay_ms(dev, 200);
    668      1.103  augustss 	}
    669      1.172  drochner 	if (err) {
    670  1.196.4.3       snj 		DPRINTF("get_desc=%d", err, 0, 0, 0);
    671        1.1  augustss 		goto bad;
    672      1.172  drochner 	}
    673       1.18  augustss 	if (cdp->bDescriptorType != UDESC_CONFIG) {
    674  1.196.4.3       snj 		DPRINTF("bad desc %d", cdp->bDescriptorType, 0, 0, 0);
    675       1.53  augustss 		err = USBD_INVAL;
    676       1.18  augustss 		goto bad;
    677       1.18  augustss 	}
    678       1.74  augustss 
    679  1.196.4.3       snj 	if (USB_IS_SS(dev->ud_speed)) {
    680  1.196.4.3       snj 		usb_bos_descriptor_t bd;
    681  1.196.4.3       snj 
    682  1.196.4.3       snj 		/* get short bos desc */
    683  1.196.4.3       snj 		err = usbd_get_bos_desc(dev, index, &bd);
    684  1.196.4.3       snj 		if (!err) {
    685  1.196.4.3       snj 			int blen = UGETW(bd.wTotalLength);
    686  1.196.4.4       snj 			if (blen == 0) {
    687  1.196.4.4       snj 				DPRINTF("empty bos descriptor", 0, 0, 0, 0);
    688  1.196.4.4       snj 				err = USBD_INVAL;
    689  1.196.4.4       snj 				goto bad;
    690  1.196.4.4       snj 			}
    691  1.196.4.3       snj 			bdp = kmem_alloc(blen, KM_SLEEP);
    692  1.196.4.3       snj 			if (bdp == NULL) {
    693  1.196.4.3       snj 				err = USBD_NOMEM;
    694  1.196.4.3       snj 				goto bad;
    695  1.196.4.3       snj 			}
    696  1.196.4.3       snj 
    697  1.196.4.3       snj 			/* Get the full desc */
    698  1.196.4.3       snj 			for (i = 0; i < 3; i++) {
    699  1.196.4.3       snj 				err = usbd_get_desc(dev, UDESC_BOS, index, blen,
    700  1.196.4.3       snj 				    bdp);
    701  1.196.4.3       snj 				if (!err)
    702  1.196.4.3       snj 					break;
    703  1.196.4.3       snj 				usbd_delay_ms(dev, 200);
    704  1.196.4.3       snj 			}
    705  1.196.4.3       snj 			if (err || bdp->bDescriptorType != UDESC_BOS) {
    706  1.196.4.3       snj 				DPRINTF("error %d or bad desc %d", err,
    707  1.196.4.3       snj 				    bdp->bDescriptorType, 0, 0);
    708  1.196.4.3       snj 				kmem_free(bdp, blen);
    709  1.196.4.3       snj 				bdp = NULL;
    710  1.196.4.3       snj 			}
    711  1.196.4.3       snj 		}
    712  1.196.4.3       snj 	}
    713  1.196.4.3       snj 	dev->ud_bdesc = bdp;
    714  1.196.4.3       snj 
    715      1.142  drochner 	/*
    716      1.142  drochner 	 * Figure out if the device is self or bus powered.
    717      1.142  drochner 	 */
    718      1.142  drochner #if 0 /* XXX various devices don't report the power state correctly */
    719        1.1  augustss 	selfpowered = 0;
    720      1.141  drochner 	err = usbd_get_device_status(dev, &ds);
    721      1.141  drochner 	if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
    722      1.141  drochner 		selfpowered = 1;
    723      1.142  drochner #endif
    724      1.142  drochner 	/*
    725      1.142  drochner 	 * Use the power state in the configuration we are going
    726      1.142  drochner 	 * to set. This doesn't necessarily reflect the actual
    727      1.142  drochner 	 * power state of the device; the driver can control this
    728      1.142  drochner 	 * by choosing the appropriate configuration.
    729      1.142  drochner 	 */
    730      1.142  drochner 	selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
    731      1.141  drochner 
    732  1.196.4.3       snj 	DPRINTF("addr %d cno=%d attr=0x%02x, selfpowered=%d",
    733  1.196.4.3       snj 	    dev->ud_addr, cdp->bConfigurationValue, cdp->bmAttributes,
    734  1.196.4.3       snj 	    selfpowered);
    735  1.196.4.3       snj 	DPRINTF("max power=%d", cdp->bMaxPower * 2, 0, 0, 0);
    736       1.74  augustss 
    737       1.74  augustss 	/* Check if we have enough power. */
    738      1.142  drochner #if 0 /* this is a no-op, see above */
    739      1.141  drochner 	if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
    740      1.141  drochner 		if (msg)
    741      1.141  drochner 			printf("%s: device addr %d (config %d): "
    742      1.141  drochner 				 "can't set self powered configuration\n",
    743  1.196.4.3       snj 			       device_xname(dev->ud_bus->bdev), dev->ud_addr,
    744      1.141  drochner 			       cdp->bConfigurationValue);
    745      1.141  drochner 		err = USBD_NO_POWER;
    746      1.141  drochner 		goto bad;
    747      1.141  drochner 	}
    748      1.142  drochner #endif
    749        1.1  augustss #ifdef USB_DEBUG
    750  1.196.4.3       snj 	if (dev->ud_powersrc == NULL) {
    751  1.196.4.3       snj 		DPRINTF("No power source?", 0, 0, 0, 0);
    752      1.141  drochner 		err = USBD_IOERROR;
    753      1.141  drochner 		goto bad;
    754        1.1  augustss 	}
    755        1.1  augustss #endif
    756        1.1  augustss 	power = cdp->bMaxPower * 2;
    757  1.196.4.3       snj 	if (power > dev->ud_powersrc->up_power) {
    758  1.196.4.3       snj 		DPRINTF("power exceeded %d %d", power, dev->ud_powersrc->up_power,
    759  1.196.4.3       snj 		    0, 0);
    760        1.1  augustss 		/* XXX print nicer message. */
    761        1.7  augustss 		if (msg)
    762       1.18  augustss 			printf("%s: device addr %d (config %d) exceeds power "
    763       1.18  augustss 				 "budget, %d mA > %d mA\n",
    764  1.196.4.3       snj 			       device_xname(dev->ud_bus->ub_usbctl), dev->ud_addr,
    765       1.99  augustss 			       cdp->bConfigurationValue,
    766  1.196.4.3       snj 			       power, dev->ud_powersrc->up_power);
    767       1.53  augustss 		err = USBD_NO_POWER;
    768        1.1  augustss 		goto bad;
    769        1.1  augustss 	}
    770  1.196.4.3       snj 	dev->ud_power = power;
    771  1.196.4.3       snj 	dev->ud_selfpowered = selfpowered;
    772        1.1  augustss 
    773       1.74  augustss 	/* Set the actual configuration value. */
    774  1.196.4.3       snj 	DPRINTF("set config %d", cdp->bConfigurationValue, 0, 0, 0);
    775       1.53  augustss 	err = usbd_set_config(dev, cdp->bConfigurationValue);
    776       1.53  augustss 	if (err) {
    777  1.196.4.3       snj 		DPRINTF("setting config=%d failed, error=%d",
    778  1.196.4.3       snj 		    cdp->bConfigurationValue, err, 0, 0);
    779        1.1  augustss 		goto bad;
    780        1.1  augustss 	}
    781       1.74  augustss 
    782       1.74  augustss 	/* Allocate and fill interface data. */
    783        1.1  augustss 	nifc = cdp->bNumInterface;
    784  1.196.4.4       snj 	if (nifc == 0) {
    785  1.196.4.4       snj 		DPRINTF("no interfaces", 0, 0, 0, 0);
    786  1.196.4.4       snj 		err = USBD_INVAL;
    787  1.196.4.4       snj 		goto bad;
    788  1.196.4.4       snj 	}
    789  1.196.4.3       snj 	dev->ud_ifaces = kmem_alloc(nifc * sizeof(struct usbd_interface),
    790  1.196.4.3       snj 	    KM_SLEEP);
    791  1.196.4.3       snj 	if (dev->ud_ifaces == NULL) {
    792       1.53  augustss 		err = USBD_NOMEM;
    793        1.1  augustss 		goto bad;
    794        1.1  augustss 	}
    795  1.196.4.3       snj 	DPRINTFN(5, "dev=%p cdesc=%p", dev, cdp, 0, 0);
    796  1.196.4.3       snj 	dev->ud_cdesc = cdp;
    797  1.196.4.3       snj 	dev->ud_config = cdp->bConfigurationValue;
    798       1.12  augustss 	for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
    799       1.53  augustss 		err = usbd_fill_iface_data(dev, ifcidx, 0);
    800       1.53  augustss 		if (err) {
    801       1.12  augustss 			while (--ifcidx >= 0)
    802       1.12  augustss 				usbd_free_iface_data(dev, ifcidx);
    803        1.1  augustss 			goto bad;
    804        1.1  augustss 		}
    805        1.1  augustss 	}
    806        1.1  augustss 
    807  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
    808        1.1  augustss 
    809        1.1  augustss  bad:
    810  1.196.4.3       snj 	kmem_free(cdp, len);
    811  1.196.4.3       snj 	if (bdp != NULL) {
    812  1.196.4.3       snj 		kmem_free(bdp, UGETW(bdp->wTotalLength));
    813  1.196.4.3       snj 		dev->ud_bdesc = NULL;
    814  1.196.4.3       snj 	}
    815  1.196.4.3       snj 	return err;
    816        1.1  augustss }
    817        1.1  augustss 
    818        1.1  augustss /* XXX add function for alternate settings */
    819        1.1  augustss 
    820        1.1  augustss usbd_status
    821  1.196.4.3       snj usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface,
    822  1.196.4.3       snj 		struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe)
    823        1.1  augustss {
    824      1.188  jmcneill 	return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0);
    825      1.188  jmcneill }
    826      1.188  jmcneill 
    827      1.188  jmcneill usbd_status
    828  1.196.4.3       snj usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface,
    829  1.196.4.3       snj     struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags)
    830      1.188  jmcneill {
    831  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    832  1.196.4.3       snj 	struct usbd_pipe *p;
    833       1.53  augustss 	usbd_status err;
    834        1.1  augustss 
    835  1.196.4.3       snj 	p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP);
    836  1.196.4.3       snj 	DPRINTFN(1, "dev=%p addr=%d iface=%p ep=%p pipe=%p", dev, dev->ud_addr, iface, ep);
    837  1.196.4.3       snj 	if (p == NULL) {
    838  1.196.4.3       snj 		DPRINTFN(1, "(nomem)", 0, 0, 0, 0);
    839  1.196.4.3       snj 		return USBD_NOMEM;
    840  1.196.4.3       snj 	}
    841  1.196.4.3       snj 	p->up_dev = dev;
    842  1.196.4.3       snj 	p->up_iface = iface;
    843  1.196.4.3       snj 	p->up_endpoint = ep;
    844  1.196.4.3       snj 	ep->ue_refcnt++;
    845  1.196.4.3       snj 	p->up_intrxfer = NULL;
    846  1.196.4.3       snj 	p->up_running = 0;
    847  1.196.4.3       snj 	p->up_aborting = 0;
    848  1.196.4.3       snj 	p->up_serialise = true;
    849  1.196.4.3       snj 	p->up_repeat = 0;
    850  1.196.4.3       snj 	p->up_interval = ival;
    851  1.196.4.3       snj 	p->up_flags = flags;
    852  1.196.4.3       snj 	p->up_serialise = true;
    853  1.196.4.3       snj 	SIMPLEQ_INIT(&p->up_queue);
    854  1.196.4.3       snj 	err = dev->ud_bus->ub_methods->ubm_open(p);
    855       1.53  augustss 	if (err) {
    856  1.196.4.3       snj 		DPRINTF("endpoint=0x%x failed, error=%d",
    857  1.196.4.3       snj 		    ep->ue_edesc->bEndpointAddress, err, 0, 0);
    858  1.196.4.3       snj 		kmem_free(p, dev->ud_bus->ub_pipesize);
    859  1.196.4.3       snj 		return err;
    860        1.1  augustss 	}
    861  1.196.4.3       snj 
    862  1.196.4.3       snj 	KASSERT(p->up_methods->upm_start || p->up_serialise == false);
    863  1.196.4.3       snj 
    864  1.196.4.3       snj 	usb_init_task(&p->up_async_task, usbd_clear_endpoint_stall_task, p,
    865      1.188  jmcneill 	    USB_TASKQ_MPSAFE);
    866  1.196.4.3       snj 	DPRINTFN(1, "pipe=%p", p, 0, 0, 0);
    867        1.1  augustss 	*pipe = p;
    868  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
    869        1.1  augustss }
    870        1.1  augustss 
    871        1.1  augustss /* Abort the device control pipe. */
    872        1.1  augustss void
    873  1.196.4.3       snj usbd_kill_pipe(struct usbd_pipe *pipe)
    874        1.1  augustss {
    875       1.89  augustss 	usbd_abort_pipe(pipe);
    876      1.182       mrg 	usbd_lock_pipe(pipe);
    877  1.196.4.3       snj 	pipe->up_methods->upm_close(pipe);
    878      1.182       mrg 	usbd_unlock_pipe(pipe);
    879  1.196.4.5    martin 	usb_rem_task_wait(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER,
    880  1.196.4.5    martin 	    NULL);
    881  1.196.4.3       snj 	pipe->up_endpoint->ue_refcnt--;
    882  1.196.4.3       snj 	kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
    883        1.1  augustss }
    884        1.1  augustss 
    885        1.1  augustss int
    886  1.196.4.3       snj usbd_getnewaddr(struct usbd_bus *bus)
    887        1.1  augustss {
    888       1.18  augustss 	int addr;
    889        1.1  augustss 
    890  1.196.4.3       snj 	for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
    891  1.196.4.3       snj 		size_t dindex = usb_addr2dindex(addr);
    892  1.196.4.3       snj 		if (bus->ub_devices[dindex] == NULL)
    893  1.196.4.3       snj 			return addr;
    894  1.196.4.3       snj 	}
    895  1.196.4.3       snj 	return -1;
    896        1.1  augustss }
    897        1.1  augustss 
    898      1.155  drochner usbd_status
    899  1.196.4.3       snj usbd_attach_roothub(device_t parent, struct usbd_device *dev)
    900      1.155  drochner {
    901      1.155  drochner 	struct usb_attach_arg uaa;
    902  1.196.4.3       snj 	usb_device_descriptor_t *dd = &dev->ud_ddesc;
    903      1.155  drochner 	device_t dv;
    904      1.155  drochner 
    905  1.196.4.3       snj 	uaa.uaa_device = dev;
    906  1.196.4.3       snj 	uaa.uaa_usegeneric = 0;
    907  1.196.4.3       snj 	uaa.uaa_port = 0;
    908  1.196.4.3       snj 	uaa.uaa_vendor = UGETW(dd->idVendor);
    909  1.196.4.3       snj 	uaa.uaa_product = UGETW(dd->idProduct);
    910  1.196.4.3       snj 	uaa.uaa_release = UGETW(dd->bcdDevice);
    911  1.196.4.3       snj 	uaa.uaa_class = dd->bDeviceClass;
    912  1.196.4.3       snj 	uaa.uaa_subclass = dd->bDeviceSubClass;
    913  1.196.4.3       snj 	uaa.uaa_proto = dd->bDeviceProtocol;
    914      1.155  drochner 
    915      1.156  drochner 	dv = config_found_ia(parent, "usbroothubif", &uaa, 0);
    916      1.155  drochner 	if (dv) {
    917  1.196.4.3       snj 		dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
    918  1.196.4.3       snj 		if (dev->ud_subdevs == NULL)
    919  1.196.4.3       snj 			return USBD_NOMEM;
    920  1.196.4.3       snj 		dev->ud_subdevs[0] = dv;
    921  1.196.4.3       snj 		dev->ud_subdevlen = 1;
    922      1.155  drochner 	}
    923  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
    924      1.155  drochner }
    925       1.18  augustss 
    926  1.196.4.1   msaitoh static void
    927  1.196.4.3       snj usbd_serialnumber(device_t dv, struct usbd_device *dev)
    928  1.196.4.1   msaitoh {
    929  1.196.4.2       snj 	if (dev->ud_serial) {
    930  1.196.4.1   msaitoh 		prop_dictionary_set_cstring(device_properties(dv),
    931  1.196.4.2       snj 		    "serialnumber", dev->ud_serial);
    932  1.196.4.1   msaitoh 	}
    933  1.196.4.1   msaitoh }
    934  1.196.4.1   msaitoh 
    935      1.160  drochner static usbd_status
    936  1.196.4.3       snj usbd_attachwholedevice(device_t parent, struct usbd_device *dev, int port,
    937      1.160  drochner 	int usegeneric)
    938       1.18  augustss {
    939       1.18  augustss 	struct usb_attach_arg uaa;
    940  1.196.4.3       snj 	usb_device_descriptor_t *dd = &dev->ud_ddesc;
    941      1.160  drochner 	device_t dv;
    942      1.158  drochner 	int dlocs[USBDEVIFCF_NLOCS];
    943       1.18  augustss 
    944  1.196.4.3       snj 	uaa.uaa_device = dev;
    945  1.196.4.3       snj 	uaa.uaa_usegeneric = usegeneric;
    946  1.196.4.3       snj 	uaa.uaa_port = port;
    947  1.196.4.3       snj 	uaa.uaa_vendor = UGETW(dd->idVendor);
    948  1.196.4.3       snj 	uaa.uaa_product = UGETW(dd->idProduct);
    949  1.196.4.3       snj 	uaa.uaa_release = UGETW(dd->bcdDevice);
    950  1.196.4.3       snj 	uaa.uaa_class = dd->bDeviceClass;
    951  1.196.4.3       snj 	uaa.uaa_subclass = dd->bDeviceSubClass;
    952  1.196.4.3       snj 	uaa.uaa_proto = dd->bDeviceProtocol;
    953  1.196.4.3       snj 
    954  1.196.4.3       snj 	dlocs[USBDEVIFCF_PORT] = uaa.uaa_port;
    955  1.196.4.3       snj 	dlocs[USBDEVIFCF_VENDOR] = uaa.uaa_vendor;
    956  1.196.4.3       snj 	dlocs[USBDEVIFCF_PRODUCT] = uaa.uaa_product;
    957  1.196.4.3       snj 	dlocs[USBDEVIFCF_RELEASE] = uaa.uaa_release;
    958      1.158  drochner 	/* the rest is historical ballast */
    959      1.158  drochner 	dlocs[USBDEVIFCF_CONFIGURATION] = -1;
    960      1.158  drochner 	dlocs[USBDEVIFCF_INTERFACE] = -1;
    961      1.158  drochner 
    962  1.196.4.3       snj 	KERNEL_LOCK(1, NULL);
    963      1.158  drochner 	dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
    964      1.160  drochner 				 config_stdsubmatch);
    965  1.196.4.3       snj 	KERNEL_UNLOCK_ONE(NULL);
    966       1.34  augustss 	if (dv) {
    967  1.196.4.3       snj 		dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
    968  1.196.4.3       snj 		if (dev->ud_subdevs == NULL)
    969  1.196.4.3       snj 			return USBD_NOMEM;
    970  1.196.4.3       snj 		dev->ud_subdevs[0] = dv;
    971  1.196.4.3       snj 		dev->ud_subdevlen = 1;
    972  1.196.4.3       snj 		dev->ud_nifaces_claimed = 1; /* XXX */
    973  1.196.4.1   msaitoh 		usbd_serialnumber(dv, dev);
    974       1.34  augustss 	}
    975  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
    976      1.160  drochner }
    977       1.18  augustss 
    978      1.160  drochner static usbd_status
    979  1.196.4.3       snj usbd_attachinterfaces(device_t parent, struct usbd_device *dev,
    980      1.162      kent 		      int port, const int *locators)
    981      1.160  drochner {
    982  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    983      1.160  drochner 	struct usbif_attach_arg uiaa;
    984      1.162      kent 	int ilocs[USBIFIFCF_NLOCS];
    985  1.196.4.3       snj 	usb_device_descriptor_t *dd = &dev->ud_ddesc;
    986      1.160  drochner 	int nifaces;
    987  1.196.4.3       snj 	struct usbd_interface **ifaces;
    988      1.162      kent 	int i, j, loc;
    989      1.160  drochner 	device_t dv;
    990      1.160  drochner 
    991  1.196.4.3       snj 	nifaces = dev->ud_cdesc->bNumInterface;
    992  1.196.4.3       snj 	ifaces = kmem_zalloc(nifaces * sizeof(*ifaces), KM_SLEEP);
    993      1.160  drochner 	if (!ifaces)
    994  1.196.4.3       snj 		return USBD_NOMEM;
    995  1.196.4.3       snj 	for (i = 0; i < nifaces; i++) {
    996  1.196.4.3       snj 		if (!dev->ud_subdevs[i]) {
    997  1.196.4.3       snj 			ifaces[i] = &dev->ud_ifaces[i];
    998  1.196.4.3       snj 		}
    999  1.196.4.3       snj 		DPRINTF("interface %d %p", i, ifaces[i], 0, 0);
   1000  1.196.4.3       snj 	}
   1001  1.196.4.3       snj 
   1002  1.196.4.3       snj 
   1003  1.196.4.3       snj 	uiaa.uiaa_device = dev;
   1004  1.196.4.3       snj 	uiaa.uiaa_port = port;
   1005  1.196.4.3       snj 	uiaa.uiaa_vendor = UGETW(dd->idVendor);
   1006  1.196.4.3       snj 	uiaa.uiaa_product = UGETW(dd->idProduct);
   1007  1.196.4.3       snj 	uiaa.uiaa_release = UGETW(dd->bcdDevice);
   1008  1.196.4.3       snj 	uiaa.uiaa_configno = dev->ud_cdesc->bConfigurationValue;
   1009  1.196.4.3       snj 	uiaa.uiaa_ifaces = ifaces;
   1010  1.196.4.3       snj 	uiaa.uiaa_nifaces = nifaces;
   1011  1.196.4.3       snj 	ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port;
   1012  1.196.4.3       snj 	ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor;
   1013  1.196.4.3       snj 	ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product;
   1014  1.196.4.3       snj 	ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release;
   1015  1.196.4.3       snj 	ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno;
   1016      1.160  drochner 
   1017      1.160  drochner 	for (i = 0; i < nifaces; i++) {
   1018  1.196.4.3       snj 		if (!ifaces[i]) {
   1019  1.196.4.3       snj 			DPRINTF("interface %d claimed", i, 0, 0, 0);
   1020      1.160  drochner 			continue; /* interface already claimed */
   1021  1.196.4.3       snj 		}
   1022  1.196.4.3       snj 		uiaa.uiaa_iface = ifaces[i];
   1023  1.196.4.3       snj 		uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass;
   1024  1.196.4.3       snj 		uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass;
   1025  1.196.4.3       snj 		uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol;
   1026  1.196.4.3       snj 		uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber;
   1027  1.196.4.3       snj 
   1028  1.196.4.3       snj 		DPRINTF("searching for interface %d...", i, 0, 0, 0);
   1029  1.196.4.3       snj 		DPRINTF("class %x subclass %x proto %x ifaceno %d",
   1030  1.196.4.3       snj 		    uiaa.uiaa_class, uiaa.uiaa_subclass, uiaa.uiaa_proto,
   1031  1.196.4.3       snj 		    uiaa.uiaa_ifaceno);
   1032  1.196.4.3       snj 		ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno;
   1033      1.162      kent 		if (locators != NULL) {
   1034      1.162      kent 			loc = locators[USBIFIFCF_CONFIGURATION];
   1035      1.162      kent 			if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
   1036  1.196.4.3       snj 			    loc != uiaa.uiaa_configno)
   1037      1.162      kent 				continue;
   1038      1.162      kent 			loc = locators[USBIFIFCF_INTERFACE];
   1039  1.196.4.3       snj 			if (loc != USBIFIFCF_INTERFACE_DEFAULT &&
   1040  1.196.4.3       snj 			    loc != uiaa.uiaa_ifaceno)
   1041      1.162      kent 				continue;
   1042      1.162      kent 		}
   1043  1.196.4.3       snj 		KERNEL_LOCK(1, NULL);
   1044      1.160  drochner 		dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
   1045      1.160  drochner 					 usbd_ifprint, config_stdsubmatch);
   1046  1.196.4.3       snj 		KERNEL_UNLOCK_ONE(NULL);
   1047      1.160  drochner 		if (!dv)
   1048      1.160  drochner 			continue;
   1049  1.196.4.1   msaitoh 
   1050  1.196.4.1   msaitoh 		usbd_serialnumber(dv, dev);
   1051  1.196.4.1   msaitoh 
   1052  1.196.4.3       snj 		/* claim */
   1053  1.196.4.3       snj 		ifaces[i] = NULL;
   1054      1.160  drochner 		/* account for ifaces claimed by the driver behind our back */
   1055      1.160  drochner 		for (j = 0; j < nifaces; j++) {
   1056  1.196.4.3       snj 
   1057  1.196.4.3       snj 			if (!ifaces[j] && !dev->ud_subdevs[j]) {
   1058  1.196.4.3       snj 				DPRINTF("interface %d claimed behind our back",
   1059  1.196.4.3       snj 				    j, 0, 0, 0);
   1060  1.196.4.3       snj 				dev->ud_subdevs[j] = dv;
   1061  1.196.4.3       snj 				dev->ud_nifaces_claimed++;
   1062      1.160  drochner 			}
   1063      1.160  drochner 		}
   1064      1.160  drochner 	}
   1065      1.160  drochner 
   1066  1.196.4.3       snj 	kmem_free(ifaces, nifaces * sizeof(*ifaces));
   1067  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
   1068      1.160  drochner }
   1069      1.160  drochner 
   1070      1.160  drochner usbd_status
   1071  1.196.4.3       snj usbd_probe_and_attach(device_t parent, struct usbd_device *dev,
   1072      1.164    dyoung                       int port, int addr)
   1073      1.160  drochner {
   1074  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1075  1.196.4.3       snj 	usb_device_descriptor_t *dd = &dev->ud_ddesc;
   1076      1.160  drochner 	int confi, nifaces;
   1077      1.160  drochner 	usbd_status err;
   1078      1.160  drochner 
   1079      1.160  drochner 	/* First try with device specific drivers. */
   1080  1.196.4.3       snj 	DPRINTF("trying device specific drivers", 0, 0, 0, 0);
   1081      1.160  drochner 	err = usbd_attachwholedevice(parent, dev, port, 0);
   1082  1.196.4.3       snj 	if (dev->ud_nifaces_claimed || err)
   1083  1.196.4.3       snj 		return err;
   1084  1.196.4.3       snj 	DPRINTF("no device specific driver found", 0, 0, 0, 0);
   1085      1.158  drochner 
   1086  1.196.4.3       snj 	DPRINTF("looping over %d configurations", dd->bNumConfigurations,
   1087  1.196.4.3       snj 	    0, 0, 0);
   1088       1.18  augustss 	for (confi = 0; confi < dd->bNumConfigurations; confi++) {
   1089  1.196.4.3       snj 		DPRINTFN(1, "trying config idx=%d", confi, 0, 0, 0);
   1090       1.53  augustss 		err = usbd_set_config_index(dev, confi, 1);
   1091       1.53  augustss 		if (err) {
   1092  1.196.4.3       snj 			DPRINTF("port %d, set config at addr %d failed, "
   1093  1.196.4.3       snj 			    "error=%d", port, addr, err, 0);
   1094       1.18  augustss 			printf("%s: port %d, set config at addr %d failed\n",
   1095      1.164    dyoung 			    device_xname(parent), port, addr);
   1096  1.196.4.3       snj 			return err;
   1097       1.18  augustss 		}
   1098  1.196.4.3       snj 		nifaces = dev->ud_cdesc->bNumInterface;
   1099  1.196.4.3       snj 		dev->ud_subdevs = kmem_zalloc(nifaces * sizeof(device_t),
   1100  1.196.4.3       snj 		    KM_SLEEP);
   1101  1.196.4.3       snj 		if (dev->ud_subdevs == NULL)
   1102  1.196.4.3       snj 			return USBD_NOMEM;
   1103  1.196.4.3       snj 		dev->ud_subdevlen = nifaces;
   1104       1.52  augustss 
   1105      1.162      kent 		err = usbd_attachinterfaces(parent, dev, port, NULL);
   1106      1.160  drochner 
   1107  1.196.4.3       snj 		if (!dev->ud_nifaces_claimed) {
   1108  1.196.4.3       snj 			kmem_free(dev->ud_subdevs,
   1109  1.196.4.3       snj 			    dev->ud_subdevlen * sizeof(device_t));
   1110  1.196.4.3       snj 			dev->ud_subdevs = 0;
   1111  1.196.4.3       snj 			dev->ud_subdevlen = 0;
   1112       1.18  augustss 		}
   1113  1.196.4.3       snj 		if (dev->ud_nifaces_claimed || err)
   1114  1.196.4.3       snj 			return err;
   1115       1.18  augustss 	}
   1116       1.21  augustss 	/* No interfaces were attached in any of the configurations. */
   1117       1.34  augustss 
   1118       1.34  augustss 	if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
   1119       1.18  augustss 		usbd_set_config_index(dev, 0, 0);
   1120       1.18  augustss 
   1121  1.196.4.3       snj 	DPRINTF("no interface drivers found", 0, 0, 0, 0);
   1122       1.18  augustss 
   1123       1.18  augustss 	/* Finally try the generic driver. */
   1124      1.160  drochner 	err = usbd_attachwholedevice(parent, dev, port, 1);
   1125       1.18  augustss 
   1126       1.99  augustss 	/*
   1127       1.18  augustss 	 * The generic attach failed, but leave the device as it is.
   1128       1.18  augustss 	 * We just did not find any drivers, that's all.  The device is
   1129       1.18  augustss 	 * fully operational and not harming anyone.
   1130       1.18  augustss 	 */
   1131  1.196.4.3       snj 	DPRINTF("generic attach failed", 0, 0, 0, 0);
   1132  1.196.4.3       snj 
   1133  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
   1134       1.18  augustss }
   1135       1.18  augustss 
   1136      1.162      kent /**
   1137      1.162      kent  * Called from uhub_rescan().  usbd_new_device() for the target dev must be
   1138      1.162      kent  * called before calling this.
   1139      1.162      kent  */
   1140      1.162      kent usbd_status
   1141  1.196.4.3       snj usbd_reattach_device(device_t parent, struct usbd_device *dev,
   1142      1.164    dyoung                      int port, const int *locators)
   1143      1.162      kent {
   1144      1.162      kent 	int i, loc;
   1145      1.162      kent 
   1146      1.162      kent 	if (locators != NULL) {
   1147      1.162      kent 		loc = locators[USBIFIFCF_PORT];
   1148      1.162      kent 		if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
   1149      1.162      kent 			return USBD_NORMAL_COMPLETION;
   1150      1.162      kent 		loc = locators[USBIFIFCF_VENDOR];
   1151      1.162      kent 		if (loc != USBIFIFCF_VENDOR_DEFAULT &&
   1152  1.196.4.3       snj 		    loc != UGETW(dev->ud_ddesc.idVendor))
   1153      1.162      kent 			return USBD_NORMAL_COMPLETION;
   1154      1.162      kent 		loc = locators[USBIFIFCF_PRODUCT];
   1155      1.162      kent 		if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
   1156  1.196.4.3       snj 		    loc != UGETW(dev->ud_ddesc.idProduct))
   1157      1.162      kent 			return USBD_NORMAL_COMPLETION;
   1158      1.162      kent 		loc = locators[USBIFIFCF_RELEASE];
   1159      1.162      kent 		if (loc != USBIFIFCF_RELEASE_DEFAULT &&
   1160  1.196.4.3       snj 		    loc != UGETW(dev->ud_ddesc.bcdDevice))
   1161      1.162      kent 			return USBD_NORMAL_COMPLETION;
   1162      1.162      kent 	}
   1163  1.196.4.3       snj 	if (dev->ud_subdevlen == 0) {
   1164      1.162      kent 		/* XXX: check USBIFIFCF_CONFIGURATION and
   1165      1.162      kent 		 * USBIFIFCF_INTERFACE too */
   1166  1.196.4.3       snj 		return usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
   1167  1.196.4.3       snj 	} else if (dev->ud_subdevlen != dev->ud_cdesc->bNumInterface) {
   1168      1.162      kent 		/* device-specific or generic driver is already attached. */
   1169      1.162      kent 		return USBD_NORMAL_COMPLETION;
   1170      1.162      kent 	}
   1171      1.162      kent 	/* Does the device have unconfigured interfaces? */
   1172  1.196.4.3       snj 	for (i = 0; i < dev->ud_subdevlen; i++) {
   1173  1.196.4.3       snj 		if (dev->ud_subdevs[i] == NULL) {
   1174      1.162      kent 			break;
   1175      1.162      kent 		}
   1176      1.162      kent 	}
   1177  1.196.4.3       snj 	if (i >= dev->ud_subdevlen)
   1178      1.162      kent 		return USBD_NORMAL_COMPLETION;
   1179      1.162      kent 	return usbd_attachinterfaces(parent, dev, port, locators);
   1180      1.162      kent }
   1181       1.18  augustss 
   1182        1.1  augustss /*
   1183      1.172  drochner  * Get the first 8 bytes of the device descriptor.
   1184      1.172  drochner  * Do as Windows does: try to read 64 bytes -- there are devices which
   1185      1.172  drochner  * recognize the initial descriptor fetch (before the control endpoint's
   1186      1.172  drochner  * MaxPacketSize is known by the host) by exactly this length.
   1187      1.172  drochner  */
   1188      1.193  jakllsch usbd_status
   1189  1.196.4.3       snj usbd_get_initial_ddesc(struct usbd_device *dev, usb_device_descriptor_t *desc)
   1190      1.172  drochner {
   1191  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1192      1.172  drochner 	usb_device_request_t req;
   1193      1.172  drochner 	char buf[64];
   1194      1.172  drochner 	int res, actlen;
   1195      1.172  drochner 
   1196  1.196.4.3       snj 	DPRINTFN(5, "dev %p", dev, 0, 0, 0);
   1197  1.196.4.3       snj 
   1198      1.172  drochner 	req.bmRequestType = UT_READ_DEVICE;
   1199      1.172  drochner 	req.bRequest = UR_GET_DESCRIPTOR;
   1200      1.172  drochner 	USETW2(req.wValue, UDESC_DEVICE, 0);
   1201      1.172  drochner 	USETW(req.wIndex, 0);
   1202  1.196.4.6    martin 	USETW(req.wLength, 8);
   1203      1.172  drochner 	res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
   1204      1.172  drochner 		&actlen, USBD_DEFAULT_TIMEOUT);
   1205      1.172  drochner 	if (res)
   1206      1.172  drochner 		return res;
   1207      1.172  drochner 	if (actlen < 8)
   1208      1.172  drochner 		return USBD_SHORT_XFER;
   1209      1.172  drochner 	memcpy(desc, buf, 8);
   1210      1.172  drochner 	return USBD_NORMAL_COMPLETION;
   1211      1.172  drochner }
   1212      1.172  drochner 
   1213      1.172  drochner /*
   1214        1.1  augustss  * Called when a new device has been put in the powered state,
   1215        1.1  augustss  * but not yet in the addressed state.
   1216        1.1  augustss  * Get initial descriptor, set the address, get full descriptor,
   1217        1.1  augustss  * and attach a driver.
   1218        1.1  augustss  */
   1219        1.1  augustss usbd_status
   1220  1.196.4.3       snj usbd_new_device(device_t parent, struct usbd_bus *bus, int depth, int speed,
   1221  1.196.4.3       snj     int port, struct usbd_port *up)
   1222        1.1  augustss {
   1223  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1224  1.196.4.3       snj 	struct usbd_device *dev, *adev;
   1225       1.94  augustss 	struct usbd_device *hub;
   1226       1.18  augustss 	usb_device_descriptor_t *dd;
   1227      1.107  augustss 	usb_port_status_t ps;
   1228       1.53  augustss 	usbd_status err;
   1229        1.1  augustss 	int addr;
   1230       1.18  augustss 	int i;
   1231      1.120  augustss 	int p;
   1232        1.1  augustss 
   1233  1.196.4.3       snj 	DPRINTF("bus=%p port=%d depth=%d speed=%d", bus, port, depth, speed);
   1234      1.193  jakllsch 
   1235  1.196.4.3       snj 	if (bus->ub_methods->ubm_newdev != NULL)
   1236  1.196.4.3       snj 		return (bus->ub_methods->ubm_newdev)(parent, bus, depth, speed,
   1237      1.193  jakllsch 		    port, up);
   1238      1.193  jakllsch 
   1239        1.1  augustss 	addr = usbd_getnewaddr(bus);
   1240        1.1  augustss 	if (addr < 0) {
   1241       1.99  augustss 		printf("%s: No free USB addresses, new device ignored.\n",
   1242  1.196.4.3       snj 		       device_xname(bus->ub_usbctl));
   1243  1.196.4.3       snj 		return USBD_NO_ADDR;
   1244        1.1  augustss 	}
   1245        1.1  augustss 
   1246  1.196.4.3       snj 	dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
   1247       1.53  augustss 	if (dev == NULL)
   1248  1.196.4.3       snj 		return USBD_NOMEM;
   1249        1.1  augustss 
   1250  1.196.4.3       snj 	dev->ud_bus = bus;
   1251        1.1  augustss 
   1252        1.1  augustss 	/* Set up default endpoint handle. */
   1253  1.196.4.3       snj 	dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
   1254        1.1  augustss 
   1255        1.1  augustss 	/* Set up default endpoint descriptor. */
   1256  1.196.4.3       snj 	dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
   1257  1.196.4.3       snj 	dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
   1258  1.196.4.3       snj 	dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
   1259  1.196.4.3       snj 	dev->ud_ep0desc.bmAttributes = UE_CONTROL;
   1260      1.172  drochner 	/*
   1261      1.172  drochner 	 * temporary, will be fixed after first descriptor fetch
   1262      1.172  drochner 	 * (which uses 64 bytes so it shouldn't be less),
   1263      1.172  drochner 	 * highspeed devices must support 64 byte packets anyway
   1264      1.172  drochner 	 */
   1265      1.190     skrll 	if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL)
   1266  1.196.4.3       snj 		USETW(dev->ud_ep0desc.wMaxPacketSize, 64);
   1267      1.190     skrll 	else
   1268  1.196.4.3       snj 		USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
   1269      1.190     skrll 
   1270  1.196.4.3       snj 	dev->ud_ep0desc.bInterval = 0;
   1271        1.1  augustss 
   1272  1.196.4.3       snj 	/* doesn't matter, just don't leave it uninitialized */
   1273  1.196.4.3       snj 	dev->ud_ep0.ue_toggle = 0;
   1274      1.179  drochner 
   1275  1.196.4.3       snj 	dev->ud_quirks = &usbd_no_quirk;
   1276  1.196.4.3       snj 	dev->ud_addr = USB_START_ADDR;
   1277  1.196.4.3       snj 	dev->ud_ddesc.bMaxPacketSize = 0;
   1278  1.196.4.3       snj 	dev->ud_depth = depth;
   1279  1.196.4.3       snj 	dev->ud_powersrc = up;
   1280  1.196.4.3       snj 	dev->ud_myhub = up->up_parent;
   1281      1.120  augustss 
   1282  1.196.4.3       snj 	up->up_dev = dev;
   1283      1.120  augustss 
   1284      1.120  augustss 	/* Locate port on upstream high speed hub */
   1285  1.196.4.3       snj 	for (adev = dev, hub = up->up_parent;
   1286  1.196.4.3       snj 	     hub != NULL && hub->ud_speed != USB_SPEED_HIGH;
   1287  1.196.4.3       snj 	     adev = hub, hub = hub->ud_myhub)
   1288       1.94  augustss 		;
   1289      1.120  augustss 	if (hub) {
   1290  1.196.4.3       snj 		for (p = 0; p < hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
   1291  1.196.4.3       snj 			if (hub->ud_hub->uh_ports[p].up_dev == adev) {
   1292  1.196.4.3       snj 				dev->ud_myhsport = &hub->ud_hub->uh_ports[p];
   1293      1.120  augustss 				goto found;
   1294      1.120  augustss 			}
   1295      1.120  augustss 		}
   1296  1.196.4.3       snj 		panic("usbd_new_device: cannot find HS port");
   1297      1.120  augustss 	found:
   1298  1.196.4.3       snj 		DPRINTFN(1, "high speed port %d", p, 0, 0, 0);
   1299      1.120  augustss 	} else {
   1300  1.196.4.3       snj 		dev->ud_myhsport = NULL;
   1301      1.120  augustss 	}
   1302  1.196.4.3       snj 	dev->ud_speed = speed;
   1303  1.196.4.3       snj 	dev->ud_langid = USBD_NOLANG;
   1304  1.196.4.3       snj 	dev->ud_cookie.cookie = ++usb_cookie_no;
   1305        1.1  augustss 
   1306       1.67     soren 	/* Establish the default pipe. */
   1307  1.196.4.3       snj 	err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
   1308  1.196.4.3       snj 	    &dev->ud_pipe0, USBD_MPSAFE);
   1309       1.53  augustss 	if (err) {
   1310       1.18  augustss 		usbd_remove_device(dev, up);
   1311  1.196.4.3       snj 		return err;
   1312       1.18  augustss 	}
   1313        1.1  augustss 
   1314  1.196.4.3       snj 	dd = &dev->ud_ddesc;
   1315        1.6  augustss 	/* Try a few times in case the device is slow (i.e. outside specs.) */
   1316      1.107  augustss 	for (i = 0; i < 10; i++) {
   1317        1.6  augustss 		/* Get the first 8 bytes of the device descriptor. */
   1318      1.172  drochner 		err = usbd_get_initial_ddesc(dev, dd);
   1319       1.53  augustss 		if (!err)
   1320        1.6  augustss 			break;
   1321  1.196.4.3       snj 		/*
   1322  1.196.4.3       snj 		 * The root hub can never fail to give the initial descriptor,
   1323  1.196.4.3       snj 		 * but assert it just in case.
   1324  1.196.4.3       snj 		 */
   1325  1.196.4.3       snj 		KASSERT(up->up_parent);
   1326       1.23  augustss 		usbd_delay_ms(dev, 200);
   1327      1.107  augustss 		if ((i & 3) == 3)
   1328  1.196.4.3       snj 			usbd_reset_port(up->up_parent, port, &ps);
   1329        1.6  augustss 	}
   1330       1.53  augustss 	if (err) {
   1331  1.196.4.3       snj 		DPRINTF("addr=%d, getting first desc failed: %d", addr, err,
   1332  1.196.4.3       snj 		    0, 0);
   1333       1.18  augustss 		usbd_remove_device(dev, up);
   1334  1.196.4.3       snj 		return err;
   1335       1.95  augustss 	}
   1336       1.95  augustss 
   1337      1.174  drochner 	/* Windows resets the port here, do likewise */
   1338  1.196.4.3       snj 	if (up->up_parent)
   1339  1.196.4.3       snj 		usbd_reset_port(up->up_parent, port, &ps);
   1340      1.174  drochner 
   1341       1.95  augustss 	if (speed == USB_SPEED_HIGH) {
   1342       1.95  augustss 		/* Max packet size must be 64 (sec 5.5.3). */
   1343       1.95  augustss 		if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
   1344       1.95  augustss #ifdef DIAGNOSTIC
   1345      1.165     pooka 			printf("usbd_new_device: addr=%d bad max packet "
   1346      1.165     pooka 			    "size=%d. adjusting to %d.\n",
   1347      1.165     pooka 			    addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
   1348       1.95  augustss #endif
   1349       1.95  augustss 			dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
   1350       1.95  augustss 		}
   1351       1.18  augustss 	}
   1352       1.18  augustss 
   1353  1.196.4.3       snj 	DPRINTF("adding unit addr=%d, rev=%02x, class=%d, subclass=%d", addr,
   1354  1.196.4.3       snj 	    UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass);
   1355  1.196.4.3       snj 	DPRINTF("protocol=%d, maxpacket=%d, len=%d, speed=%d",
   1356  1.196.4.3       snj 	    dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->ud_speed);
   1357       1.39  augustss 
   1358       1.18  augustss 	if (dd->bDescriptorType != UDESC_DEVICE) {
   1359       1.18  augustss 		/* Illegal device descriptor */
   1360  1.196.4.3       snj 		DPRINTF("illegal descriptor %d", dd->bDescriptorType, 0, 0, 0);
   1361       1.18  augustss 		usbd_remove_device(dev, up);
   1362  1.196.4.3       snj 		return USBD_INVAL;
   1363        1.1  augustss 	}
   1364        1.1  augustss 
   1365       1.39  augustss 	if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
   1366  1.196.4.3       snj 		DPRINTF("bad length %d", dd->bLength, 0, 0, 0);
   1367       1.39  augustss 		usbd_remove_device(dev, up);
   1368  1.196.4.3       snj 		return USBD_INVAL;
   1369       1.39  augustss 	}
   1370        1.4  augustss 
   1371  1.196.4.3       snj 	USETW(dev->ud_ep0desc.wMaxPacketSize, dd->bMaxPacketSize);
   1372        1.1  augustss 
   1373      1.196     skrll 	/* Re-establish the default pipe with the new MPS. */
   1374  1.196.4.3       snj 	usbd_kill_pipe(dev->ud_pipe0);
   1375  1.196.4.3       snj 	err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
   1376  1.196.4.3       snj 	    &dev->ud_pipe0, USBD_MPSAFE);
   1377      1.196     skrll 	if (err) {
   1378  1.196.4.3       snj 		DPRINTF("setup default pipe failed err %d", err, 0, 0, 0);
   1379      1.196     skrll 		usbd_remove_device(dev, up);
   1380      1.196     skrll 		return err;
   1381      1.196     skrll 	}
   1382      1.196     skrll 
   1383      1.159  jmcneill 	/* Set the address */
   1384  1.196.4.3       snj 	DPRINTFN(5, "setting device address=%d", addr, 0, 0, 0);
   1385      1.159  jmcneill 	err = usbd_set_address(dev, addr);
   1386      1.159  jmcneill 	if (err) {
   1387  1.196.4.3       snj 		DPRINTF("set address %d failed, err = %d", addr, err, 0, 0);
   1388      1.159  jmcneill 		err = USBD_SET_ADDR_FAILED;
   1389      1.159  jmcneill 		usbd_remove_device(dev, up);
   1390      1.159  jmcneill 		return err;
   1391      1.159  jmcneill 	}
   1392      1.159  jmcneill 
   1393      1.159  jmcneill 	/* Allow device time to set new address */
   1394      1.159  jmcneill 	usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
   1395  1.196.4.3       snj 	dev->ud_addr = addr;	/* new device address now */
   1396  1.196.4.3       snj 	bus->ub_devices[usb_addr2dindex(addr)] = dev;
   1397      1.159  jmcneill 
   1398      1.159  jmcneill 	/* Re-establish the default pipe with the new address. */
   1399  1.196.4.3       snj 	usbd_kill_pipe(dev->ud_pipe0);
   1400  1.196.4.3       snj 	err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
   1401  1.196.4.3       snj 	    &dev->ud_pipe0, USBD_MPSAFE);
   1402      1.159  jmcneill 	if (err) {
   1403  1.196.4.3       snj 		DPRINTF("setup default pipe failed, err = %d", err, 0, 0, 0);
   1404      1.159  jmcneill 		usbd_remove_device(dev, up);
   1405      1.159  jmcneill 		return err;
   1406      1.159  jmcneill 	}
   1407      1.159  jmcneill 
   1408      1.192     skrll 	err = usbd_reload_device_desc(dev);
   1409      1.192     skrll 	if (err) {
   1410  1.196.4.3       snj 		DPRINTF("addr=%d, getting full desc failed, err = %d", addr,
   1411  1.196.4.3       snj 		    err, 0, 0);
   1412      1.192     skrll 		usbd_remove_device(dev, up);
   1413  1.196.4.3       snj 		return err;
   1414      1.192     skrll 	}
   1415      1.192     skrll 
   1416        1.1  augustss 	/* Assume 100mA bus powered for now. Changed when configured. */
   1417  1.196.4.3       snj 	dev->ud_power = USB_MIN_POWER;
   1418  1.196.4.3       snj 	dev->ud_selfpowered = 0;
   1419        1.1  augustss 
   1420  1.196.4.3       snj 	DPRINTF("new dev (addr %d), dev=%p, parent=%p", addr, dev, parent, 0);
   1421       1.99  augustss 
   1422  1.196.4.2       snj 	usbd_get_device_strings(dev);
   1423  1.196.4.2       snj 
   1424       1.65  augustss 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
   1425        1.1  augustss 
   1426      1.155  drochner 	if (port == 0) { /* root hub */
   1427      1.155  drochner 		KASSERT(addr == 1);
   1428      1.155  drochner 		usbd_attach_roothub(parent, dev);
   1429  1.196.4.3       snj 		return USBD_NORMAL_COMPLETION;
   1430      1.155  drochner 	}
   1431      1.155  drochner 
   1432       1.53  augustss 	err = usbd_probe_and_attach(parent, dev, port, addr);
   1433       1.53  augustss 	if (err) {
   1434       1.18  augustss 		usbd_remove_device(dev, up);
   1435  1.196.4.3       snj 		return err;
   1436      1.164    dyoung 	}
   1437       1.65  augustss 
   1438  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
   1439       1.62  augustss }
   1440       1.62  augustss 
   1441       1.62  augustss usbd_status
   1442  1.196.4.3       snj usbd_reload_device_desc(struct usbd_device *dev)
   1443       1.62  augustss {
   1444  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1445  1.196.4.3       snj 	usb_device_descriptor_t *udd = &dev->ud_ddesc;
   1446       1.62  augustss 	usbd_status err;
   1447       1.62  augustss 
   1448       1.62  augustss 	/* Get the full device descriptor. */
   1449  1.196.4.3       snj 	err = usbd_get_device_desc(dev, udd);
   1450       1.62  augustss 	if (err)
   1451  1.196.4.3       snj 		return err;
   1452  1.196.4.3       snj 
   1453  1.196.4.3       snj 	DPRINTFN(15, "bLength             %5u", udd->bLength, 0, 0, 0);
   1454  1.196.4.3       snj 	DPRINTFN(15, "bDescriptorType     %5u", udd->bDescriptorType, 0, 0, 0);
   1455  1.196.4.3       snj 	DPRINTFN(15, "bcdUSB              %2x.%02x", udd->bcdUSB[1],
   1456  1.196.4.3       snj 	    udd->bcdUSB[0], 0, 0);
   1457  1.196.4.3       snj 	DPRINTFN(15, "bDeviceClass        %5u", udd->bDeviceClass, 0, 0, 0);
   1458  1.196.4.3       snj 	DPRINTFN(15, "bDeviceSubClass     %5u", udd->bDeviceSubClass, 0, 0, 0);
   1459  1.196.4.3       snj 	DPRINTFN(15, "bDeviceProtocol     %5u", udd->bDeviceProtocol, 0, 0, 0);
   1460  1.196.4.3       snj 	DPRINTFN(15, "bMaxPacketSize0     %5u", udd->bMaxPacketSize, 0, 0, 0);
   1461  1.196.4.3       snj 	DPRINTFN(15, "idVendor           0x%04x", udd->idVendor, 0, 0, 0);
   1462  1.196.4.3       snj 	DPRINTFN(15, "idProduct          0x%04x", udd->idProduct, 0, 0, 0);
   1463  1.196.4.3       snj 	DPRINTFN(15, "bcdDevice           %2x.%02x", udd->bcdDevice[1],
   1464  1.196.4.3       snj 	    udd->bcdDevice[0], 0, 0);
   1465  1.196.4.3       snj 	DPRINTFN(15, "iManufacturer       %5u", udd->iManufacturer, 0, 0, 0);
   1466  1.196.4.3       snj 	DPRINTFN(15, "iProduct            %5u", udd->iProduct, 0, 0, 0);
   1467  1.196.4.3       snj 	DPRINTFN(15, "iSerial             %5u", udd->iSerialNumber, 0, 0, 0);
   1468  1.196.4.3       snj 	DPRINTFN(15, "bNumConfigurations  %5u", udd->bNumConfigurations, 0, 0,
   1469  1.196.4.3       snj 	    0);
   1470       1.62  augustss 
   1471       1.62  augustss 	/* Figure out what's wrong with this device. */
   1472  1.196.4.3       snj 	dev->ud_quirks = usbd_find_quirk(udd);
   1473       1.62  augustss 
   1474  1.196.4.3       snj 	return USBD_NORMAL_COMPLETION;
   1475       1.18  augustss }
   1476        1.1  augustss 
   1477       1.18  augustss void
   1478  1.196.4.3       snj usbd_remove_device(struct usbd_device *dev, struct usbd_port *up)
   1479       1.18  augustss {
   1480      1.182       mrg 
   1481  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1482  1.196.4.3       snj 
   1483  1.196.4.3       snj 	DPRINTF("dev %p up %p", dev, up, 0, 0);
   1484       1.99  augustss 
   1485  1.196.4.3       snj 	if (dev->ud_pipe0 != NULL)
   1486  1.196.4.3       snj 		usbd_kill_pipe(dev->ud_pipe0);
   1487  1.196.4.3       snj 	up->up_dev = NULL;
   1488  1.196.4.3       snj 	dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL;
   1489        1.1  augustss 
   1490  1.196.4.3       snj 	kmem_free(dev, sizeof(*dev));
   1491        1.1  augustss }
   1492        1.1  augustss 
   1493        1.1  augustss int
   1494       1.78  augustss usbd_print(void *aux, const char *pnp)
   1495        1.1  augustss {
   1496        1.1  augustss 	struct usb_attach_arg *uaa = aux;
   1497        1.1  augustss 
   1498        1.1  augustss 	if (pnp) {
   1499      1.134  christos #define USB_DEVINFO 1024
   1500      1.134  christos 		char *devinfo;
   1501  1.196.4.3       snj 		if (!uaa->uaa_usegeneric)
   1502  1.196.4.3       snj 			return QUIET;
   1503  1.196.4.3       snj 		devinfo = kmem_alloc(USB_DEVINFO, KM_SLEEP);
   1504  1.196.4.3       snj 		usbd_devinfo(uaa->uaa_device, 1, devinfo, USB_DEVINFO);
   1505      1.101   thorpej 		aprint_normal("%s, %s", devinfo, pnp);
   1506  1.196.4.3       snj 		kmem_free(devinfo, USB_DEVINFO);
   1507        1.1  augustss 	}
   1508  1.196.4.3       snj 	aprint_normal(" port %d", uaa->uaa_port);
   1509      1.144  drochner #if 0
   1510      1.144  drochner 	/*
   1511      1.144  drochner 	 * It gets very crowded with these locators on the attach line.
   1512      1.144  drochner 	 * They are not really needed since they are printed in the clear
   1513      1.144  drochner 	 * by each driver.
   1514      1.144  drochner 	 */
   1515  1.196.4.3       snj 	if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
   1516  1.196.4.3       snj 		aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
   1517  1.196.4.3       snj 	if (uaa->uaa_product != UHUB_UNK_PRODUCT)
   1518  1.196.4.3       snj 		aprint_normal(" product 0x%04x", uaa->uaa_product);
   1519  1.196.4.3       snj 	if (uaa->uaa_release != UHUB_UNK_RELEASE)
   1520  1.196.4.3       snj 		aprint_normal(" release 0x%04x", uaa->uaa_release);
   1521      1.144  drochner #endif
   1522  1.196.4.3       snj 	return UNCONF;
   1523      1.144  drochner }
   1524      1.144  drochner 
   1525      1.144  drochner int
   1526      1.144  drochner usbd_ifprint(void *aux, const char *pnp)
   1527      1.144  drochner {
   1528  1.196.4.3       snj 	struct usbif_attach_arg *uiaa = aux;
   1529      1.144  drochner 
   1530      1.144  drochner 	if (pnp)
   1531  1.196.4.3       snj 		return QUIET;
   1532  1.196.4.3       snj 	aprint_normal(" port %d", uiaa->uiaa_port);
   1533  1.196.4.3       snj 	aprint_normal(" configuration %d", uiaa->uiaa_configno);
   1534  1.196.4.3       snj 	aprint_normal(" interface %d", uiaa->uiaa_ifaceno);
   1535       1.32  augustss #if 0
   1536       1.99  augustss 	/*
   1537       1.32  augustss 	 * It gets very crowded with these locators on the attach line.
   1538       1.32  augustss 	 * They are not really needed since they are printed in the clear
   1539       1.32  augustss 	 * by each driver.
   1540       1.32  augustss 	 */
   1541  1.196.4.3       snj 	if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
   1542  1.196.4.3       snj 		aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
   1543  1.196.4.3       snj 	if (uaa->uaa_product != UHUB_UNK_PRODUCT)
   1544  1.196.4.3       snj 		aprint_normal(" product 0x%04x", uaa->uaa_product);
   1545  1.196.4.3       snj 	if (uaa->uaa_release != UHUB_UNK_RELEASE)
   1546  1.196.4.3       snj 		aprint_normal(" release 0x%04x", uaa->uaa_release);
   1547       1.32  augustss #endif
   1548  1.196.4.3       snj 	return UNCONF;
   1549        1.1  augustss }
   1550        1.1  augustss 
   1551       1.13  augustss void
   1552  1.196.4.3       snj usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di,
   1553       1.82  augustss 		     int usedev)
   1554       1.13  augustss {
   1555       1.13  augustss 	struct usbd_port *p;
   1556  1.196.4.3       snj 	int i, j, err;
   1557       1.13  augustss 
   1558  1.196.4.3       snj 	di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
   1559  1.196.4.3       snj 	di->udi_addr = dev->ud_addr;
   1560  1.196.4.3       snj 	di->udi_cookie = dev->ud_cookie;
   1561      1.175  christos 	usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
   1562      1.175  christos 	    di->udi_product, sizeof(di->udi_product), usedev, 1);
   1563      1.113    itojun 	usbd_printBCD(di->udi_release, sizeof(di->udi_release),
   1564  1.196.4.3       snj 	    UGETW(dev->ud_ddesc.bcdDevice));
   1565  1.196.4.2       snj 	if (usedev) {
   1566  1.196.4.2       snj 		usbd_status uerr = usbd_get_string(dev,
   1567  1.196.4.3       snj 		    dev->ud_ddesc.iSerialNumber, di->udi_serial);
   1568  1.196.4.2       snj 		if (uerr != USBD_NORMAL_COMPLETION) {
   1569  1.196.4.2       snj 			di->udi_serial[0] = '\0';
   1570  1.196.4.2       snj 		} else {
   1571  1.196.4.2       snj 			usbd_trim_spaces(di->udi_serial);
   1572  1.196.4.2       snj 		}
   1573  1.196.4.2       snj 	} else {
   1574  1.196.4.2       snj 		di->udi_serial[0] = '\0';
   1575  1.196.4.2       snj 		if (dev->ud_serial) {
   1576  1.196.4.2       snj 			strlcpy(di->udi_serial, dev->ud_serial,
   1577  1.196.4.2       snj 			    sizeof(di->udi_serial));
   1578  1.196.4.2       snj 		}
   1579  1.196.4.2       snj 	}
   1580       1.65  augustss 
   1581  1.196.4.3       snj 	di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
   1582  1.196.4.3       snj 	di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
   1583  1.196.4.3       snj 	di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
   1584  1.196.4.3       snj 	di->udi_class = dev->ud_ddesc.bDeviceClass;
   1585  1.196.4.3       snj 	di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
   1586  1.196.4.3       snj 	di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
   1587  1.196.4.3       snj 	di->udi_config = dev->ud_config;
   1588  1.196.4.3       snj 	di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
   1589  1.196.4.3       snj 	di->udi_speed = dev->ud_speed;
   1590  1.196.4.3       snj 
   1591  1.196.4.3       snj 	if (dev->ud_subdevlen > 0) {
   1592  1.196.4.3       snj 		for (i = 0, j = 0; i < dev->ud_subdevlen &&
   1593      1.155  drochner 			     j < USB_MAX_DEVNAMES; i++) {
   1594  1.196.4.3       snj 			if (!dev->ud_subdevs[i])
   1595      1.155  drochner 				continue;
   1596      1.164    dyoung 			strncpy(di->udi_devnames[j],
   1597  1.196.4.3       snj 			    device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
   1598      1.155  drochner 			di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
   1599      1.155  drochner 			j++;
   1600      1.164    dyoung 		}
   1601      1.164    dyoung 	} else {
   1602      1.164    dyoung 		j = 0;
   1603      1.164    dyoung 	}
   1604      1.164    dyoung 	for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
   1605      1.164    dyoung 		di->udi_devnames[j][0] = 0;                 /* empty */
   1606       1.65  augustss 
   1607  1.196.4.3       snj 	if (!dev->ud_hub) {
   1608       1.98  christos 		di->udi_nports = 0;
   1609  1.196.4.3       snj 		return;
   1610  1.196.4.3       snj 	}
   1611  1.196.4.3       snj 
   1612  1.196.4.3       snj 	const int nports = dev->ud_hub->uh_hubdesc.bNbrPorts;
   1613  1.196.4.3       snj 	for (i = 0; i < __arraycount(di->udi_ports) && i < nports; i++) {
   1614  1.196.4.3       snj 		p = &dev->ud_hub->uh_ports[i];
   1615  1.196.4.3       snj 		if (p->up_dev)
   1616  1.196.4.3       snj 			err = p->up_dev->ud_addr;
   1617  1.196.4.3       snj 		else {
   1618  1.196.4.3       snj 			const int s = UGETW(p->up_status.wPortStatus);
   1619  1.196.4.3       snj 			const bool sshub_p = USB_IS_SS(dev->ud_speed);
   1620  1.196.4.3       snj 			if (s & UPS_PORT_ENABLED)
   1621  1.196.4.3       snj 				err = USB_PORT_ENABLED;
   1622  1.196.4.3       snj 			else if (s & UPS_SUSPEND)
   1623  1.196.4.3       snj 				err = USB_PORT_SUSPENDED;
   1624  1.196.4.3       snj 			/*
   1625  1.196.4.3       snj 			 * Note: UPS_PORT_POWER_SS is available only
   1626  1.196.4.3       snj 			 * on 3.x, and UPS_PORT_POWER is available
   1627  1.196.4.3       snj 			 * only on 2.0 or 1.1.
   1628  1.196.4.3       snj 			 */
   1629  1.196.4.3       snj 			else if (sshub_p && (s & UPS_PORT_POWER_SS))
   1630  1.196.4.3       snj 				err = USB_PORT_POWERED;
   1631  1.196.4.3       snj 			else if (!sshub_p && (s & UPS_PORT_POWER))
   1632  1.196.4.3       snj 				err = USB_PORT_POWERED;
   1633  1.196.4.3       snj 			else
   1634  1.196.4.3       snj 				err = USB_PORT_DISABLED;
   1635  1.196.4.3       snj 		}
   1636  1.196.4.3       snj 		di->udi_ports[i] = err;
   1637  1.196.4.3       snj 	}
   1638  1.196.4.3       snj 	di->udi_nports = nports;
   1639       1.34  augustss }
   1640       1.34  augustss 
   1641      1.139     pavel #ifdef COMPAT_30
   1642      1.139     pavel void
   1643  1.196.4.3       snj usbd_fill_deviceinfo_old(struct usbd_device *dev, struct usb_device_info_old *di,
   1644      1.139     pavel                          int usedev)
   1645      1.139     pavel {
   1646      1.139     pavel 	struct usbd_port *p;
   1647  1.196.4.3       snj 	int i, j, err;
   1648      1.139     pavel 
   1649  1.196.4.3       snj 	di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
   1650  1.196.4.3       snj 	di->udi_addr = dev->ud_addr;
   1651  1.196.4.3       snj 	di->udi_cookie = dev->ud_cookie;
   1652      1.175  christos 	usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
   1653      1.175  christos 	    di->udi_product, sizeof(di->udi_product), usedev, 0);
   1654      1.139     pavel 	usbd_printBCD(di->udi_release, sizeof(di->udi_release),
   1655  1.196.4.3       snj 	    UGETW(dev->ud_ddesc.bcdDevice));
   1656  1.196.4.3       snj 	di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
   1657  1.196.4.3       snj 	di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
   1658  1.196.4.3       snj 	di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
   1659  1.196.4.3       snj 	di->udi_class = dev->ud_ddesc.bDeviceClass;
   1660  1.196.4.3       snj 	di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
   1661  1.196.4.3       snj 	di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
   1662  1.196.4.3       snj 	di->udi_config = dev->ud_config;
   1663  1.196.4.3       snj 	di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
   1664  1.196.4.3       snj 	di->udi_speed = dev->ud_speed;
   1665      1.139     pavel 
   1666  1.196.4.3       snj 	if (dev->ud_subdevlen > 0) {
   1667  1.196.4.3       snj 		for (i = 0, j = 0; i < dev->ud_subdevlen &&
   1668      1.155  drochner 			     j < USB_MAX_DEVNAMES; i++) {
   1669  1.196.4.3       snj 			if (!dev->ud_subdevs[i])
   1670      1.155  drochner 				continue;
   1671      1.164    dyoung 			strncpy(di->udi_devnames[j],
   1672  1.196.4.3       snj 			    device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
   1673      1.155  drochner 			di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
   1674      1.155  drochner 			j++;
   1675      1.139     pavel 		}
   1676      1.139     pavel 	} else {
   1677      1.155  drochner 		j = 0;
   1678      1.139     pavel 	}
   1679      1.155  drochner 	for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
   1680      1.155  drochner 		di->udi_devnames[j][0] = 0;		 /* empty */
   1681      1.139     pavel 
   1682  1.196.4.3       snj 	if (!dev->ud_hub) {
   1683      1.139     pavel 		di->udi_nports = 0;
   1684  1.196.4.3       snj 		return;
   1685  1.196.4.3       snj 	}
   1686  1.196.4.3       snj 
   1687  1.196.4.3       snj 	const int nports = dev->ud_hub->uh_hubdesc.bNbrPorts;
   1688  1.196.4.3       snj 	for (i = 0; i < __arraycount(di->udi_ports) && i < nports;
   1689  1.196.4.3       snj 	     i++) {
   1690  1.196.4.3       snj 		p = &dev->ud_hub->uh_ports[i];
   1691  1.196.4.3       snj 		if (p->up_dev)
   1692  1.196.4.3       snj 			err = p->up_dev->ud_addr;
   1693  1.196.4.3       snj 		else {
   1694  1.196.4.3       snj 			const int s = UGETW(p->up_status.wPortStatus);
   1695  1.196.4.3       snj 			if (s & UPS_PORT_ENABLED)
   1696  1.196.4.3       snj 				err = USB_PORT_ENABLED;
   1697  1.196.4.3       snj 			else if (s & UPS_SUSPEND)
   1698  1.196.4.3       snj 				err = USB_PORT_SUSPENDED;
   1699  1.196.4.3       snj 			else if (s & UPS_PORT_POWER)
   1700  1.196.4.3       snj 				err = USB_PORT_POWERED;
   1701  1.196.4.3       snj 			else
   1702  1.196.4.3       snj 				err = USB_PORT_DISABLED;
   1703  1.196.4.3       snj 		}
   1704  1.196.4.3       snj 		di->udi_ports[i] = err;
   1705  1.196.4.3       snj 	}
   1706  1.196.4.3       snj 	di->udi_nports = nports;
   1707      1.139     pavel }
   1708      1.139     pavel #endif
   1709      1.139     pavel 
   1710      1.139     pavel 
   1711       1.34  augustss void
   1712  1.196.4.3       snj usb_free_device(struct usbd_device *dev)
   1713       1.34  augustss {
   1714       1.34  augustss 	int ifcidx, nifc;
   1715       1.34  augustss 
   1716  1.196.4.3       snj 	if (dev->ud_pipe0 != NULL)
   1717  1.196.4.3       snj 		usbd_kill_pipe(dev->ud_pipe0);
   1718  1.196.4.3       snj 	if (dev->ud_ifaces != NULL) {
   1719  1.196.4.3       snj 		nifc = dev->ud_cdesc->bNumInterface;
   1720       1.34  augustss 		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
   1721       1.34  augustss 			usbd_free_iface_data(dev, ifcidx);
   1722  1.196.4.3       snj 		kmem_free(dev->ud_ifaces,
   1723  1.196.4.3       snj 		    nifc * sizeof(struct usbd_interface));
   1724       1.34  augustss 	}
   1725  1.196.4.3       snj 	if (dev->ud_cdesc != NULL)
   1726  1.196.4.3       snj 		kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
   1727  1.196.4.3       snj 	if (dev->ud_bdesc != NULL)
   1728  1.196.4.3       snj 		kmem_free(dev->ud_bdesc, UGETW(dev->ud_bdesc->wTotalLength));
   1729  1.196.4.3       snj 	if (dev->ud_subdevlen > 0) {
   1730  1.196.4.3       snj 		kmem_free(dev->ud_subdevs,
   1731  1.196.4.3       snj 		    dev->ud_subdevlen * sizeof(device_t));
   1732  1.196.4.3       snj 		dev->ud_subdevlen = 0;
   1733      1.155  drochner 	}
   1734  1.196.4.2       snj 	if (dev->ud_vendor) {
   1735  1.196.4.2       snj 		kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN);
   1736  1.196.4.2       snj 	}
   1737  1.196.4.2       snj 	if (dev->ud_product) {
   1738  1.196.4.2       snj 		kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN);
   1739  1.196.4.2       snj 	}
   1740  1.196.4.2       snj 	if (dev->ud_serial) {
   1741  1.196.4.2       snj 		kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN);
   1742  1.196.4.2       snj 	}
   1743  1.196.4.3       snj 	kmem_free(dev, sizeof(*dev));
   1744        1.1  augustss }
   1745       1.47  augustss 
   1746       1.47  augustss /*
   1747       1.47  augustss  * The general mechanism for detaching drivers works as follows: Each
   1748       1.47  augustss  * driver is responsible for maintaining a reference count on the
   1749       1.47  augustss  * number of outstanding references to its softc (e.g.  from
   1750       1.47  augustss  * processing hanging in a read or write).  The detach method of the
   1751       1.47  augustss  * driver decrements this counter and flags in the softc that the
   1752       1.47  augustss  * driver is dying and then wakes any sleepers.  It then sleeps on the
   1753       1.47  augustss  * softc.  Each place that can sleep must maintain the reference
   1754       1.47  augustss  * count.  When the reference count drops to -1 (0 is the normal value
   1755       1.47  augustss  * of the reference count) the a wakeup on the softc is performed
   1756       1.47  augustss  * signaling to the detach waiter that all references are gone.
   1757       1.47  augustss  */
   1758       1.47  augustss 
   1759       1.47  augustss /*
   1760       1.47  augustss  * Called from process context when we discover that a port has
   1761       1.47  augustss  * been disconnected.
   1762       1.47  augustss  */
   1763      1.167    dyoung int
   1764      1.167    dyoung usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
   1765       1.47  augustss {
   1766  1.196.4.3       snj 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1767  1.196.4.3       snj 	struct usbd_device *dev = up->up_dev;
   1768      1.167    dyoung 	device_t subdev;
   1769      1.167    dyoung 	char subdevname[16];
   1770      1.164    dyoung 	const char *hubname = device_xname(parent);
   1771      1.167    dyoung 	int i, rc;
   1772       1.47  augustss 
   1773  1.196.4.3       snj 	DPRINTFN(3, "up=%p dev=%p port=%d", up, dev, up->up_portno, 0);
   1774       1.48  augustss 
   1775      1.167    dyoung 	if (dev == NULL) {
   1776      1.167    dyoung 		return 0;
   1777       1.48  augustss 	}
   1778       1.47  augustss 
   1779  1.196.4.3       snj 	if (dev->ud_subdevlen > 0) {
   1780  1.196.4.3       snj 		DPRINTFN(3, "disconnect subdevs", 0, 0, 0, 0);
   1781  1.196.4.3       snj 		for (i = 0; i < dev->ud_subdevlen; i++) {
   1782  1.196.4.3       snj 			if ((subdev = dev->ud_subdevs[i]) == NULL)
   1783      1.155  drochner 				continue;
   1784      1.167    dyoung 			strlcpy(subdevname, device_xname(subdev),
   1785      1.167    dyoung 			    sizeof(subdevname));
   1786      1.167    dyoung 			if ((rc = config_detach(subdev, flags)) != 0)
   1787      1.167    dyoung 				return rc;
   1788      1.167    dyoung 			printf("%s: at %s", subdevname, hubname);
   1789  1.196.4.3       snj 			if (up->up_portno != 0)
   1790  1.196.4.3       snj 				printf(" port %d", up->up_portno);
   1791  1.196.4.3       snj 			printf(" (addr %d) disconnected\n", dev->ud_addr);
   1792       1.47  augustss 		}
   1793  1.196.4.3       snj 		KASSERT(!dev->ud_nifaces_claimed);
   1794       1.47  augustss 	}
   1795       1.47  augustss 
   1796  1.196.4.3       snj 	mutex_enter(dev->ud_bus->ub_lock);
   1797  1.196.4.3       snj 	dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL;
   1798  1.196.4.3       snj 	up->up_dev = NULL;
   1799  1.196.4.3       snj 	mutex_exit(dev->ud_bus->ub_lock);
   1800  1.196.4.3       snj 
   1801       1.65  augustss 	usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
   1802  1.196.4.3       snj 
   1803       1.47  augustss 	usb_free_device(dev);
   1804  1.196.4.3       snj 
   1805      1.167    dyoung 	return 0;
   1806       1.47  augustss }
   1807