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