Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.136
      1  1.136       chs /*	$NetBSD: uhub.c,v 1.136 2017/06/01 02:45:12 chs Exp $	*/
      2   1.34  augustss /*	$FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $	*/
      3  1.133     skrll /*	$OpenBSD: uhub.c,v 1.86 2015/06/29 18:27:40 mpi Exp $ */
      4    1.1  augustss 
      5    1.1  augustss /*
      6   1.74   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      7    1.1  augustss  * All rights reserved.
      8    1.1  augustss  *
      9    1.6  augustss  * This code is derived from software contributed to The NetBSD Foundation
     10   1.44  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
     11    1.6  augustss  * Carlstedt Research & Technology.
     12    1.1  augustss  *
     13    1.1  augustss  * Redistribution and use in source and binary forms, with or without
     14    1.1  augustss  * modification, are permitted provided that the following conditions
     15    1.1  augustss  * are met:
     16    1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     17    1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     18    1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     19    1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     20    1.1  augustss  *    documentation and/or other materials provided with the distribution.
     21    1.1  augustss  *
     22    1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23    1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24    1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25    1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26    1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27    1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28    1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29    1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30    1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31    1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32    1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     33   1.15  augustss  */
     34   1.15  augustss 
     35   1.15  augustss /*
     36   1.64    ichiro  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
     37    1.1  augustss  */
     38   1.53     lukem 
     39   1.53     lukem #include <sys/cdefs.h>
     40  1.136       chs __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.136 2017/06/01 02:45:12 chs Exp $");
     41  1.135     skrll 
     42  1.135     skrll #ifdef _KERNEL_OPT
     43  1.135     skrll #include "opt_usb.h"
     44  1.135     skrll #endif
     45    1.1  augustss 
     46    1.1  augustss #include <sys/param.h>
     47  1.127     skrll 
     48  1.133     skrll #include <sys/bus.h>
     49  1.127     skrll #include <sys/device.h>
     50    1.1  augustss #include <sys/kernel.h>
     51  1.133     skrll #include <sys/kmem.h>
     52   1.34  augustss #include <sys/proc.h>
     53  1.127     skrll #include <sys/sysctl.h>
     54  1.133     skrll #include <sys/systm.h>
     55   1.28  augustss 
     56    1.1  augustss 
     57    1.1  augustss #include <dev/usb/usb.h>
     58    1.1  augustss #include <dev/usb/usbdi.h>
     59    1.1  augustss #include <dev/usb/usbdi_util.h>
     60    1.1  augustss #include <dev/usb/usbdivar.h>
     61  1.127     skrll #include <dev/usb/usbhist.h>
     62    1.1  augustss 
     63  1.127     skrll #ifdef USB_DEBUG
     64  1.127     skrll #ifndef UHUB_DEBUG
     65  1.127     skrll #define uhubdebug 0
     66    1.1  augustss #else
     67  1.127     skrll static int uhubdebug = 0;
     68  1.127     skrll 
     69  1.127     skrll SYSCTL_SETUP(sysctl_hw_uhub_setup, "sysctl hw.uhub setup")
     70  1.127     skrll {
     71  1.127     skrll 	int err;
     72  1.127     skrll 	const struct sysctlnode *rnode;
     73  1.127     skrll 	const struct sysctlnode *cnode;
     74  1.127     skrll 
     75  1.127     skrll 	err = sysctl_createv(clog, 0, NULL, &rnode,
     76  1.127     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "uhub",
     77  1.127     skrll 	    SYSCTL_DESCR("uhub global controls"),
     78  1.127     skrll 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     79  1.127     skrll 
     80  1.127     skrll 	if (err)
     81  1.127     skrll 		goto fail;
     82  1.127     skrll 
     83  1.127     skrll 	/* control debugging printfs */
     84  1.127     skrll 	err = sysctl_createv(clog, 0, &rnode, &cnode,
     85  1.127     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
     86  1.127     skrll 	    "debug", SYSCTL_DESCR("Enable debugging output"),
     87  1.127     skrll 	    NULL, 0, &uhubdebug, sizeof(uhubdebug), CTL_CREATE, CTL_EOL);
     88  1.127     skrll 	if (err)
     89  1.127     skrll 		goto fail;
     90  1.127     skrll 
     91  1.127     skrll 	return;
     92  1.127     skrll fail:
     93  1.127     skrll 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
     94  1.127     skrll }
     95  1.127     skrll 
     96  1.127     skrll #endif /* UHUB_DEBUG */
     97  1.127     skrll #endif /* USB_DEBUG */
     98  1.127     skrll 
     99  1.127     skrll #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(uhubdebug,1,FMT,A,B,C,D)
    100  1.127     skrll #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(uhubdebug,N,FMT,A,B,C,D)
    101  1.127     skrll #define UHUBHIST_FUNC() USBHIST_FUNC()
    102  1.127     skrll #define UHUBHIST_CALLED(name) USBHIST_CALLED(uhubdebug)
    103    1.1  augustss 
    104    1.1  augustss struct uhub_softc {
    105  1.133     skrll 	device_t		 sc_dev;	/* base device */
    106  1.133     skrll 	struct usbd_device	*sc_hub;	/* USB device */
    107  1.133     skrll 	int			 sc_proto;	/* device protocol */
    108  1.133     skrll 	struct usbd_pipe	*sc_ipipe;	/* interrupt pipe */
    109  1.133     skrll 
    110  1.133     skrll 	kmutex_t		 sc_lock;
    111  1.133     skrll 
    112  1.133     skrll 	uint8_t			*sc_statusbuf;
    113  1.133     skrll 	uint8_t			*sc_statuspend;
    114  1.133     skrll 	uint8_t			*sc_status;
    115  1.133     skrll 	size_t			 sc_statuslen;
    116  1.133     skrll 	int			 sc_explorepending;
    117   1.87  drochner 
    118  1.133     skrll 	u_char			 sc_running;
    119    1.1  augustss };
    120   1.87  drochner 
    121  1.133     skrll #define UHUB_IS_HIGH_SPEED(sc) \
    122  1.133     skrll     ((sc)->sc_proto == UDPROTO_HSHUBSTT || (sc)->sc_proto == UDPROTO_HSHUBMTT)
    123   1.86  drochner #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
    124    1.1  augustss 
    125   1.87  drochner #define PORTSTAT_ISSET(sc, port) \
    126   1.87  drochner 	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
    127   1.87  drochner 
    128  1.133     skrll Static usbd_status uhub_explore(struct usbd_device *);
    129  1.133     skrll Static void uhub_intr(struct usbd_xfer *, void *, usbd_status);
    130    1.1  augustss 
    131   1.32  augustss 
    132   1.58  augustss /*
    133   1.32  augustss  * We need two attachment points:
    134   1.32  augustss  * hub to usb and hub to hub
    135   1.32  augustss  * Every other driver only connects to hubs
    136   1.32  augustss  */
    137    1.1  augustss 
    138   1.98      cube int uhub_match(device_t, cfdata_t, void *);
    139   1.95    dyoung void uhub_attach(device_t, device_t, void *);
    140  1.103      kent int uhub_rescan(device_t, const char *, const int *);
    141   1.95    dyoung void uhub_childdet(device_t, device_t);
    142   1.95    dyoung int uhub_detach(device_t, int);
    143   1.95    dyoung extern struct cfdriver uhub_cd;
    144  1.104    dyoung CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
    145  1.108    dyoung     uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
    146  1.104    dyoung     DVF_DETACH_SHUTDOWN);
    147   1.99  drochner CFATTACH_DECL2_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
    148  1.108    dyoung     uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet);
    149    1.1  augustss 
    150  1.109     pooka /*
    151  1.109     pooka  * Setting this to 1 makes sure than an uhub attaches even at higher
    152  1.109     pooka  * priority than ugen when ugen_override is set to 1.  This allows to
    153  1.109     pooka  * probe the whole USB bus and attach functions with ugen.
    154  1.109     pooka  */
    155  1.109     pooka int uhub_ubermatch = 0;
    156  1.109     pooka 
    157  1.133     skrll static usbd_status
    158  1.133     skrll usbd_get_hub_desc(struct usbd_device *dev, usb_hub_descriptor_t *hd, int speed)
    159  1.133     skrll {
    160  1.133     skrll 	usb_device_request_t req;
    161  1.133     skrll 	usbd_status err;
    162  1.133     skrll 	int nports;
    163  1.133     skrll 
    164  1.133     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    165  1.133     skrll 
    166  1.133     skrll 	/* don't issue UDESC_HUB to SS hub, or it would stall */
    167  1.133     skrll 	if (dev->ud_depth != 0 && USB_IS_SS(dev->ud_speed)) {
    168  1.133     skrll 		usb_hub_ss_descriptor_t hssd;
    169  1.133     skrll 		int rmvlen;
    170  1.133     skrll 
    171  1.133     skrll 		memset(&hssd, 0, sizeof(hssd));
    172  1.133     skrll 		req.bmRequestType = UT_READ_CLASS_DEVICE;
    173  1.133     skrll 		req.bRequest = UR_GET_DESCRIPTOR;
    174  1.133     skrll 		USETW2(req.wValue, UDESC_SS_HUB, 0);
    175  1.133     skrll 		USETW(req.wIndex, 0);
    176  1.133     skrll 		USETW(req.wLength, USB_HUB_SS_DESCRIPTOR_SIZE);
    177  1.133     skrll 		DPRINTFN(1, "getting sshub descriptor", 0, 0, 0, 0);
    178  1.133     skrll 		err = usbd_do_request(dev, &req, &hssd);
    179  1.133     skrll 		nports = hssd.bNbrPorts;
    180  1.133     skrll 		if (dev->ud_depth != 0 && nports > UHD_SS_NPORTS_MAX) {
    181  1.133     skrll 			DPRINTF("num of ports %d exceeds maxports %d",
    182  1.133     skrll 			    nports, UHD_SS_NPORTS_MAX, 0, 0);
    183  1.133     skrll 			nports = hd->bNbrPorts = UHD_SS_NPORTS_MAX;
    184  1.133     skrll 		}
    185  1.133     skrll 		rmvlen = (nports + 7) / 8;
    186  1.133     skrll 		hd->bDescLength = USB_HUB_DESCRIPTOR_SIZE +
    187  1.133     skrll 		    (rmvlen > 1 ? rmvlen : 1) - 1;
    188  1.133     skrll 		memcpy(hd->DeviceRemovable, hssd.DeviceRemovable, rmvlen);
    189  1.133     skrll 		hd->bDescriptorType		= hssd.bDescriptorType;
    190  1.133     skrll 		hd->bNbrPorts			= hssd.bNbrPorts;
    191  1.133     skrll 		hd->wHubCharacteristics[0]	= hssd.wHubCharacteristics[0];
    192  1.133     skrll 		hd->wHubCharacteristics[1]	= hssd.wHubCharacteristics[1];
    193  1.133     skrll 		hd->bPwrOn2PwrGood		= hssd.bPwrOn2PwrGood;
    194  1.133     skrll 		hd->bHubContrCurrent		= hssd.bHubContrCurrent;
    195  1.133     skrll 	} else {
    196  1.133     skrll 		req.bmRequestType = UT_READ_CLASS_DEVICE;
    197  1.133     skrll 		req.bRequest = UR_GET_DESCRIPTOR;
    198  1.133     skrll 		USETW2(req.wValue, UDESC_HUB, 0);
    199  1.133     skrll 		USETW(req.wIndex, 0);
    200  1.133     skrll 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    201  1.133     skrll 		DPRINTFN(1, "getting hub descriptor", 0, 0, 0, 0);
    202  1.133     skrll 		err = usbd_do_request(dev, &req, hd);
    203  1.133     skrll 		nports = hd->bNbrPorts;
    204  1.133     skrll 		if (!err && nports > 7) {
    205  1.133     skrll 			USETW(req.wLength,
    206  1.133     skrll 			    USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    207  1.133     skrll 			err = usbd_do_request(dev, &req, hd);
    208  1.133     skrll 		}
    209  1.133     skrll 	}
    210  1.133     skrll 
    211  1.133     skrll 	return err;
    212  1.133     skrll }
    213  1.133     skrll 
    214  1.133     skrll static usbd_status
    215  1.133     skrll usbd_set_hub_depth(struct usbd_device *dev, int depth)
    216  1.133     skrll {
    217  1.133     skrll 	usb_device_request_t req;
    218  1.133     skrll 
    219  1.133     skrll 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    220  1.133     skrll 	req.bRequest = UR_SET_HUB_DEPTH;
    221  1.133     skrll 	USETW(req.wValue, depth);
    222  1.133     skrll 	USETW(req.wIndex, 0);
    223  1.133     skrll 	USETW(req.wLength, 0);
    224  1.133     skrll 	return usbd_do_request(dev, &req, 0);
    225  1.133     skrll }
    226  1.133     skrll 
    227  1.105    dyoung int
    228  1.105    dyoung uhub_match(device_t parent, cfdata_t match, void *aux)
    229    1.1  augustss {
    230  1.107    dyoung 	struct usb_attach_arg *uaa = aux;
    231  1.109     pooka 	int matchvalue;
    232  1.109     pooka 
    233  1.127     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    234  1.127     skrll 
    235  1.109     pooka 	if (uhub_ubermatch)
    236  1.109     pooka 		matchvalue = UMATCH_HIGHEST+1;
    237  1.109     pooka 	else
    238  1.109     pooka 		matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS;
    239   1.58  augustss 
    240  1.127     skrll 	DPRINTFN(5, "uaa=%p", uaa, 0, 0, 0);
    241   1.58  augustss 	/*
    242    1.1  augustss 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    243    1.1  augustss 	 * so we just ignore the subclass.
    244    1.1  augustss 	 */
    245  1.133     skrll 	if (uaa->uaa_class == UDCLASS_HUB)
    246  1.133     skrll 		return matchvalue;
    247  1.133     skrll 	return UMATCH_NONE;
    248    1.1  augustss }
    249    1.1  augustss 
    250  1.105    dyoung void
    251  1.105    dyoung uhub_attach(device_t parent, device_t self, void *aux)
    252    1.1  augustss {
    253  1.107    dyoung 	struct uhub_softc *sc = device_private(self);
    254  1.107    dyoung 	struct usb_attach_arg *uaa = aux;
    255  1.133     skrll 	struct usbd_device *dev = uaa->uaa_device;
    256   1.76  augustss 	char *devinfop;
    257   1.33  augustss 	usbd_status err;
    258   1.70  augustss 	struct usbd_hub *hub = NULL;
    259    1.1  augustss 	usb_hub_descriptor_t hubdesc;
    260   1.42  augustss 	int p, port, nports, nremov, pwrdly;
    261  1.133     skrll 	struct usbd_interface *iface;
    262    1.1  augustss 	usb_endpoint_descriptor_t *ed;
    263   1.70  augustss 	struct usbd_tt *tts = NULL;
    264   1.58  augustss 
    265  1.127     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    266  1.127     skrll 
    267   1.98      cube 	sc->sc_dev = self;
    268    1.1  augustss 	sc->sc_hub = dev;
    269  1.133     skrll 	sc->sc_proto = uaa->uaa_proto;
    270   1.76  augustss 
    271   1.76  augustss 	devinfop = usbd_devinfo_alloc(dev, 1);
    272   1.96  jmcneill 	aprint_naive("\n");
    273   1.96  jmcneill 	aprint_normal(": %s\n", devinfop);
    274   1.76  augustss 	usbd_devinfo_free(devinfop);
    275    1.1  augustss 
    276  1.133     skrll 	if (dev->ud_depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    277   1.98      cube 		aprint_normal_dev(self, "%s transaction translator%s\n",
    278   1.70  augustss 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    279   1.70  augustss 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    280   1.69  augustss 	}
    281   1.69  augustss 
    282   1.33  augustss 	err = usbd_set_config_index(dev, 0, 1);
    283   1.33  augustss 	if (err) {
    284  1.127     skrll 		DPRINTF("configuration failed, sc %p error %d", sc, err, 0, 0);
    285  1.107    dyoung 		return;
    286    1.1  augustss 	}
    287    1.1  augustss 
    288  1.133     skrll 	if (dev->ud_depth > USB_HUB_MAX_DEPTH) {
    289   1.98      cube 		aprint_error_dev(self,
    290   1.98      cube 		    "hub depth (%d) exceeded, hub ignored\n",
    291   1.98      cube 		    USB_HUB_MAX_DEPTH);
    292  1.107    dyoung 		return;
    293    1.1  augustss 	}
    294    1.1  augustss 
    295    1.1  augustss 	/* Get hub descriptor. */
    296  1.133     skrll 	memset(&hubdesc, 0, sizeof(hubdesc));
    297  1.133     skrll 	err = usbd_get_hub_desc(dev, &hubdesc, dev->ud_speed);
    298   1.11  augustss 	nports = hubdesc.bNbrPorts;
    299   1.33  augustss 	if (err) {
    300  1.133     skrll 		DPRINTF("getting hub descriptor failed, uhub%d error %d",
    301  1.128     skrll 		    device_unit(self), err, 0, 0);
    302  1.107    dyoung 		return;
    303    1.1  augustss 	}
    304    1.1  augustss 
    305   1.11  augustss 	for (nremov = 0, port = 1; port <= nports; port++)
    306   1.11  augustss 		if (!UHD_NOT_REMOV(&hubdesc, port))
    307   1.11  augustss 			nremov++;
    308   1.98      cube 	aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
    309   1.98      cube 	    nports, nports != 1 ? "s" : "", nremov,
    310  1.133     skrll 	    dev->ud_selfpowered ? "self" : "bus");
    311   1.11  augustss 
    312   1.70  augustss 	if (nports == 0) {
    313   1.98      cube 		aprint_debug_dev(self, "no ports, hub ignored\n");
    314   1.70  augustss 		goto bad;
    315   1.70  augustss 	}
    316   1.70  augustss 
    317  1.133     skrll 	hub = kmem_alloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    318  1.133     skrll 	    KM_SLEEP);
    319  1.133     skrll 	dev->ud_hub = hub;
    320  1.133     skrll 	dev->ud_hub->uh_hubsoftc = sc;
    321  1.133     skrll 	hub->uh_explore = uhub_explore;
    322  1.133     skrll 	hub->uh_hubdesc = hubdesc;
    323  1.133     skrll 
    324  1.133     skrll 	if (USB_IS_SS(dev->ud_speed) && dev->ud_depth != 0) {
    325  1.133     skrll 		aprint_debug_dev(self, "setting hub depth %u\n",
    326  1.134     skrll 		    dev->ud_depth - 1);
    327  1.133     skrll 		err = usbd_set_hub_depth(dev, dev->ud_depth - 1);
    328  1.133     skrll 		if (err) {
    329  1.133     skrll 			aprint_error_dev(self, "can't set depth\n");
    330  1.133     skrll 			goto bad;
    331  1.133     skrll 		}
    332  1.133     skrll 	}
    333   1.58  augustss 
    334    1.1  augustss 	/* Set up interrupt pipe. */
    335   1.33  augustss 	err = usbd_device2interface_handle(dev, 0, &iface);
    336   1.33  augustss 	if (err) {
    337   1.98      cube 		aprint_error_dev(self, "no interface handle\n");
    338   1.18  augustss 		goto bad;
    339    1.1  augustss 	}
    340   1.83  drochner 
    341   1.83  drochner 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
    342   1.83  drochner 		err = usbd_set_interface(iface, 1);
    343   1.83  drochner 		if (err)
    344   1.98      cube 			aprint_error_dev(self, "can't enable multiple TTs\n");
    345   1.83  drochner 	}
    346   1.83  drochner 
    347    1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    348   1.33  augustss 	if (ed == NULL) {
    349   1.98      cube 		aprint_error_dev(self, "no endpoint descriptor\n");
    350   1.18  augustss 		goto bad;
    351    1.1  augustss 	}
    352    1.1  augustss 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    353   1.98      cube 		aprint_error_dev(self, "bad interrupt endpoint\n");
    354   1.18  augustss 		goto bad;
    355    1.1  augustss 	}
    356    1.1  augustss 
    357   1.87  drochner 	sc->sc_statuslen = (nports + 1 + 7) / 8;
    358  1.133     skrll 	sc->sc_statusbuf = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
    359  1.133     skrll 	sc->sc_statuspend = kmem_zalloc(sc->sc_statuslen, KM_SLEEP);
    360  1.133     skrll 	sc->sc_status = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
    361  1.133     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    362  1.133     skrll 	memset(sc->sc_statuspend, 0, sc->sc_statuslen);
    363   1.84  drochner 
    364   1.87  drochner 	/* force initial scan */
    365   1.87  drochner 	memset(sc->sc_status, 0xff, sc->sc_statuslen);
    366   1.87  drochner 	sc->sc_explorepending = 1;
    367   1.87  drochner 
    368   1.33  augustss 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    369  1.122  jmcneill 		  USBD_SHORT_XFER_OK|USBD_MPSAFE, &sc->sc_ipipe, sc,
    370  1.122  jmcneill 		  sc->sc_statusbuf, sc->sc_statuslen,
    371  1.122  jmcneill 		  uhub_intr, USBD_DEFAULT_INTERVAL);
    372   1.33  augustss 	if (err) {
    373   1.98      cube 		aprint_error_dev(self, "cannot open interrupt pipe\n");
    374   1.18  augustss 		goto bad;
    375    1.1  augustss 	}
    376    1.1  augustss 
    377   1.11  augustss 	/* Wait with power off for a while. */
    378   1.13  augustss 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    379   1.11  augustss 
    380  1.105    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
    381   1.37  augustss 
    382   1.42  augustss 	/*
    383   1.42  augustss 	 * To have the best chance of success we do things in the exact same
    384  1.123  jakllsch 	 * order as Windows 98.  This should not be necessary, but some
    385   1.42  augustss 	 * devices do not follow the USB specs to the letter.
    386   1.42  augustss 	 *
    387   1.42  augustss 	 * These are the events on the bus when a hub is attached:
    388   1.42  augustss 	 *  Get device and config descriptors (see attach code)
    389   1.42  augustss 	 *  Get hub descriptor (see above)
    390   1.42  augustss 	 *  For all ports
    391   1.42  augustss 	 *     turn on power
    392   1.42  augustss 	 *     wait for power to become stable
    393   1.42  augustss 	 * (all below happens in explore code)
    394   1.42  augustss 	 *  For all ports
    395   1.42  augustss 	 *     clear C_PORT_CONNECTION
    396   1.42  augustss 	 *  For all ports
    397   1.42  augustss 	 *     get port status
    398   1.42  augustss 	 *     if device connected
    399   1.57  augustss 	 *        wait 100 ms
    400   1.42  augustss 	 *        turn on reset
    401   1.42  augustss 	 *        wait
    402   1.42  augustss 	 *        clear C_PORT_RESET
    403   1.42  augustss 	 *        get port status
    404   1.42  augustss 	 *        proceed with device attachment
    405   1.42  augustss 	 */
    406   1.42  augustss 
    407   1.78  christos 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
    408  1.133     skrll 		tts = kmem_alloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    409  1.133     skrll 			     sizeof(struct usbd_tt), KM_SLEEP);
    410   1.70  augustss 	}
    411   1.42  augustss 	/* Set up data structures */
    412   1.11  augustss 	for (p = 0; p < nports; p++) {
    413  1.133     skrll 		struct usbd_port *up = &hub->uh_ports[p];
    414  1.133     skrll 		up->up_dev = NULL;
    415  1.133     skrll 		up->up_parent = dev;
    416  1.133     skrll 		up->up_portno = p + 1;
    417  1.133     skrll 		if (dev->ud_selfpowered)
    418   1.42  augustss 			/* Self powered hub, give ports maximum current. */
    419  1.133     skrll 			up->up_power = USB_MAX_POWER;
    420   1.42  augustss 		else
    421  1.133     skrll 			up->up_power = USB_MIN_POWER;
    422  1.133     skrll 		up->up_restartcnt = 0;
    423  1.133     skrll 		up->up_reattach = 0;
    424   1.70  augustss 		if (UHUB_IS_HIGH_SPEED(sc)) {
    425  1.133     skrll 			up->up_tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
    426  1.133     skrll 			up->up_tt->utt_hub = hub;
    427   1.70  augustss 		} else {
    428  1.133     skrll 			up->up_tt = NULL;
    429   1.70  augustss 		}
    430   1.42  augustss 	}
    431   1.42  augustss 
    432   1.42  augustss 	/* XXX should check for none, individual, or ganged power? */
    433   1.42  augustss 
    434  1.133     skrll 	pwrdly = dev->ud_hub->uh_hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    435   1.42  augustss 	    + USB_EXTRA_POWER_UP_TIME;
    436   1.42  augustss 	for (port = 1; port <= nports; port++) {
    437   1.42  augustss 		/* Turn the power on. */
    438   1.42  augustss 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    439   1.33  augustss 		if (err)
    440   1.98      cube 			aprint_error_dev(self, "port %d power on failed, %s\n",
    441   1.98      cube 			    port, usbd_errstr(err));
    442  1.133     skrll 		DPRINTF("uhub%d turn on port %d power", device_unit(self),
    443  1.128     skrll 		    port, 0, 0);
    444   1.94  jmcneill 	}
    445   1.94  jmcneill 
    446   1.94  jmcneill 	/* Wait for stable power if we are not a root hub */
    447  1.133     skrll 	if (dev->ud_powersrc->up_parent != NULL)
    448   1.42  augustss 		usbd_delay_ms(dev, pwrdly);
    449   1.42  augustss 
    450   1.42  augustss 	/* The usual exploration will finish the setup. */
    451   1.42  augustss 
    452    1.1  augustss 	sc->sc_running = 1;
    453   1.11  augustss 
    454   1.92  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    455   1.92  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    456   1.92  jmcneill 
    457  1.107    dyoung 	return;
    458   1.11  augustss 
    459   1.18  augustss  bad:
    460   1.84  drochner 	if (sc->sc_status)
    461  1.133     skrll 		kmem_free(sc->sc_status, sc->sc_statuslen);
    462  1.133     skrll 	if (sc->sc_statuspend)
    463  1.133     skrll 		kmem_free(sc->sc_statuspend, sc->sc_statuslen);
    464  1.101  drochner 	if (sc->sc_statusbuf)
    465  1.133     skrll 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    466   1.70  augustss 	if (hub)
    467  1.133     skrll 		kmem_free(hub,
    468  1.133     skrll 		    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
    469  1.133     skrll 	dev->ud_hub = NULL;
    470  1.107    dyoung 	return;
    471   1.11  augustss }
    472   1.11  augustss 
    473    1.1  augustss usbd_status
    474  1.133     skrll uhub_explore(struct usbd_device *dev)
    475    1.1  augustss {
    476  1.133     skrll 	usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
    477  1.133     skrll 	struct uhub_softc *sc = dev->ud_hub->uh_hubsoftc;
    478    1.1  augustss 	struct usbd_port *up;
    479   1.33  augustss 	usbd_status err;
    480   1.56  augustss 	int speed;
    481    1.1  augustss 	int port;
    482   1.72      joff 	int change, status, reconnect;
    483    1.1  augustss 
    484  1.127     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    485  1.127     skrll 
    486  1.133     skrll 	DPRINTFN(10, "uhub%d dev=%p addr=%d speed=%u",
    487  1.133     skrll 	    device_unit(sc->sc_dev), dev, dev->ud_addr, dev->ud_speed);
    488    1.1  augustss 
    489    1.1  augustss 	if (!sc->sc_running)
    490  1.133     skrll 		return USBD_NOT_STARTED;
    491    1.1  augustss 
    492    1.1  augustss 	/* Ignore hubs that are too deep. */
    493  1.133     skrll 	if (dev->ud_depth > USB_HUB_MAX_DEPTH)
    494  1.133     skrll 		return USBD_TOO_DEEP;
    495    1.1  augustss 
    496  1.101  drochner 	if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */
    497  1.101  drochner 		usb_hub_status_t hs;
    498  1.101  drochner 
    499  1.101  drochner 		err = usbd_get_hub_status(dev, &hs);
    500  1.101  drochner 		if (err) {
    501  1.133     skrll 			DPRINTF("uhub%d get hub status failed, err %d",
    502  1.128     skrll 			    device_unit(sc->sc_dev), err, 0, 0);
    503  1.101  drochner 		} else {
    504  1.101  drochner 			/* just acknowledge */
    505  1.101  drochner 			status = UGETW(hs.wHubStatus);
    506  1.101  drochner 			change = UGETW(hs.wHubChange);
    507  1.133     skrll 			DPRINTF("uhub%d s/c=%x/%x", device_unit(sc->sc_dev),
    508  1.129     skrll 			    status, change, 0);
    509  1.129     skrll 
    510  1.101  drochner 			if (change & UHS_LOCAL_POWER)
    511  1.101  drochner 				usbd_clear_hub_feature(dev,
    512  1.101  drochner 						       UHF_C_HUB_LOCAL_POWER);
    513  1.101  drochner 			if (change & UHS_OVER_CURRENT)
    514  1.101  drochner 				usbd_clear_hub_feature(dev,
    515  1.101  drochner 						       UHF_C_HUB_OVER_CURRENT);
    516  1.101  drochner 		}
    517  1.101  drochner 	}
    518  1.101  drochner 
    519   1.84  drochner 	for (port = 1; port <= hd->bNbrPorts; port++) {
    520  1.133     skrll 		up = &dev->ud_hub->uh_ports[port - 1];
    521   1.87  drochner 
    522   1.87  drochner 		/* reattach is needed after firmware upload */
    523  1.133     skrll 		reconnect = up->up_reattach;
    524  1.133     skrll 		up->up_reattach = 0;
    525   1.87  drochner 
    526   1.87  drochner 		status = change = 0;
    527   1.87  drochner 
    528   1.87  drochner 		/* don't check if no change summary notification */
    529   1.87  drochner 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
    530  1.133     skrll 			err = usbd_get_port_status(dev, port, &up->up_status);
    531   1.87  drochner 			if (err) {
    532  1.133     skrll 				DPRINTF("uhub%d get port stat failed, err %d",
    533  1.128     skrll 				    device_unit(sc->sc_dev), err, 0, 0);
    534   1.87  drochner 				continue;
    535   1.87  drochner 			}
    536  1.133     skrll 			status = UGETW(up->up_status.wPortStatus);
    537  1.133     skrll 			change = UGETW(up->up_status.wPortChange);
    538  1.129     skrll 
    539  1.133     skrll 			DPRINTF("uhub%d port %d: s/c=%x/%x",
    540  1.129     skrll 			    device_unit(sc->sc_dev), port, status, change);
    541   1.87  drochner 		}
    542   1.87  drochner 		if (!change && !reconnect) {
    543   1.87  drochner 			/* No status change, just do recursive explore. */
    544  1.133     skrll 			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
    545  1.133     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    546    1.1  augustss 			continue;
    547    1.1  augustss 		}
    548   1.87  drochner 
    549   1.11  augustss 		if (change & UPS_C_PORT_ENABLED) {
    550  1.133     skrll 			DPRINTF("uhub%d port %d C_PORT_ENABLED",
    551  1.129     skrll 			    device_unit(sc->sc_dev), port, 0, 0);
    552   1.11  augustss 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    553   1.68   mycroft 			if (change & UPS_C_CONNECT_STATUS) {
    554   1.68   mycroft 				/* Ignore the port error if the device
    555   1.68   mycroft 				   vanished. */
    556   1.68   mycroft 			} else if (status & UPS_PORT_ENABLED) {
    557   1.98      cube 				aprint_error_dev(sc->sc_dev,
    558   1.98      cube 				    "illegal enable change, port %d\n", port);
    559   1.11  augustss 			} else {
    560   1.11  augustss 				/* Port error condition. */
    561  1.133     skrll 				if (up->up_restartcnt) /* no message first time */
    562   1.98      cube 					aprint_error_dev(sc->sc_dev,
    563   1.98      cube 					    "port error, restarting port %d\n",
    564   1.98      cube 					    port);
    565   1.47  augustss 
    566  1.133     skrll 				if (up->up_restartcnt++ < USBD_RESTART_MAX)
    567   1.11  augustss 					goto disco;
    568   1.47  augustss 				else
    569   1.98      cube 					aprint_error_dev(sc->sc_dev,
    570   1.98      cube 					    "port error, giving up port %d\n",
    571   1.98      cube 					    port);
    572   1.11  augustss 			}
    573   1.11  augustss 		}
    574  1.133     skrll 		if (change & UPS_C_PORT_RESET) {
    575  1.133     skrll 			/*
    576  1.133     skrll 			 * some xHCs set PortResetChange instead of CSC
    577  1.133     skrll 			 * when port is reset.
    578  1.133     skrll 			 */
    579  1.133     skrll 			if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
    580  1.133     skrll 				change |= UPS_C_CONNECT_STATUS;
    581  1.133     skrll 			}
    582  1.133     skrll 			usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    583  1.133     skrll 		}
    584  1.133     skrll 		if (change & UPS_C_BH_PORT_RESET) {
    585  1.133     skrll 			/*
    586  1.133     skrll 			 * some xHCs set WarmResetChange instead of CSC
    587  1.133     skrll 			 * when port is reset.
    588  1.133     skrll 			 */
    589  1.133     skrll 			if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
    590  1.133     skrll 				change |= UPS_C_CONNECT_STATUS;
    591  1.133     skrll 			}
    592  1.133     skrll 			usbd_clear_port_feature(dev, port,
    593  1.133     skrll 			    UHF_C_BH_PORT_RESET);
    594  1.133     skrll 		}
    595  1.133     skrll 		if (change & UPS_C_PORT_LINK_STATE)
    596  1.133     skrll 			usbd_clear_port_feature(dev, port,
    597  1.133     skrll 			    UHF_C_PORT_LINK_STATE);
    598  1.133     skrll 		if (change & UPS_C_PORT_CONFIG_ERROR)
    599  1.133     skrll 			usbd_clear_port_feature(dev, port,
    600  1.133     skrll 			    UHF_C_PORT_CONFIG_ERROR);
    601   1.87  drochner 
    602   1.87  drochner 		/* XXX handle overcurrent and resume events! */
    603   1.87  drochner 
    604  1.116  jakllsch 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
    605  1.116  jakllsch 			/* No status change, just do recursive explore. */
    606  1.133     skrll 			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
    607  1.133     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    608    1.1  augustss 			continue;
    609  1.116  jakllsch 		}
    610   1.42  augustss 
    611   1.42  augustss 		/* We have a connect status change, handle it. */
    612   1.42  augustss 
    613  1.133     skrll 		DPRINTF("uhub%d status change port %d", device_unit(sc->sc_dev),
    614  1.133     skrll 		    port, 0, 0);
    615    1.1  augustss 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    616    1.1  augustss 		/*
    617    1.1  augustss 		 * If there is already a device on the port the change status
    618    1.1  augustss 		 * must mean that is has disconnected.  Looking at the
    619    1.1  augustss 		 * current connect status is not enough to figure this out
    620    1.1  augustss 		 * since a new unit may have been connected before we handle
    621    1.1  augustss 		 * the disconnect.
    622    1.1  augustss 		 */
    623   1.11  augustss 	disco:
    624  1.133     skrll 		if (up->up_dev != NULL) {
    625    1.1  augustss 			/* Disconnected */
    626  1.133     skrll 			DPRINTF("uhub%d device addr=%d disappeared on port %d",
    627  1.133     skrll 			    device_unit(sc->sc_dev), up->up_dev->ud_addr, port,
    628  1.129     skrll 			    0);
    629  1.133     skrll 
    630  1.108    dyoung 			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
    631   1.58  augustss 			usbd_clear_port_feature(dev, port,
    632    1.1  augustss 						UHF_C_PORT_CONNECTION);
    633    1.1  augustss 		}
    634   1.35  augustss 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    635   1.42  augustss 			/* Nothing connected, just ignore it. */
    636  1.133     skrll 			DPRINTFN(3, "uhub%d port %d !CURRENT_CONNECT_STATUS",
    637  1.129     skrll 			    device_unit(sc->sc_dev), port, 0, 0);
    638  1.133     skrll 			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
    639  1.133     skrll 			usbd_clear_port_feature(dev, port,
    640  1.133     skrll 						UHF_C_PORT_CONNECTION);
    641    1.1  augustss 			continue;
    642   1.35  augustss 		}
    643    1.1  augustss 
    644    1.1  augustss 		/* Connected */
    645  1.129     skrll 		DPRINTF("unit %d dev->speed=%u dev->depth=%u",
    646  1.133     skrll 		    device_unit(sc->sc_dev), dev->ud_speed, dev->ud_depth, 0);
    647   1.42  augustss 
    648    1.1  augustss 		/* Wait for maximum device power up time. */
    649   1.13  augustss 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    650   1.11  augustss 
    651    1.1  augustss 		/* Reset port, which implies enabling it. */
    652  1.133     skrll 		if (usbd_reset_port(dev, port, &up->up_status)) {
    653   1.98      cube 			aprint_error_dev(sc->sc_dev,
    654   1.98      cube 			    "port %d reset failed\n", port);
    655   1.54  augustss 			continue;
    656   1.54  augustss 		}
    657  1.130     skrll #if 0
    658   1.54  augustss 		/* Get port status again, it might have changed during reset */
    659  1.133     skrll 		err = usbd_get_port_status(dev, port, &up->up_status);
    660   1.54  augustss 		if (err) {
    661  1.133     skrll 			DPRINTF("uhub%d port %d get port status failed, "
    662  1.129     skrll 			    "err %d", device_unit(sc->sc_dev), port, err, 0);
    663   1.54  augustss 			continue;
    664   1.54  augustss 		}
    665  1.130     skrll #endif
    666  1.133     skrll 		/*
    667  1.133     skrll 		 * Use the port status from the reset to check for the device
    668  1.133     skrll 		 * disappearing, the port enable status, and the port speed
    669  1.133     skrll 		 */
    670  1.133     skrll 		status = UGETW(up->up_status.wPortStatus);
    671  1.133     skrll 		change = UGETW(up->up_status.wPortChange);
    672  1.133     skrll 		DPRINTF("uhub%d port %d after reset: s/c=%x/%x",
    673  1.133     skrll 		    device_unit(sc->sc_dev), port, status, change);
    674  1.133     skrll 
    675   1.54  augustss 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    676   1.54  augustss 			/* Nothing connected, just ignore it. */
    677   1.54  augustss #ifdef DIAGNOSTIC
    678   1.98      cube 			aprint_debug_dev(sc->sc_dev,
    679   1.98      cube 			    "port %d, device disappeared after reset\n", port);
    680   1.54  augustss #endif
    681    1.1  augustss 			continue;
    682   1.35  augustss 		}
    683  1.110  kiyohara 		if (!(status & UPS_PORT_ENABLED)) {
    684  1.110  kiyohara 			/* Not allowed send/receive packet. */
    685  1.110  kiyohara #ifdef DIAGNOSTIC
    686  1.115  sborrill 			printf("%s: port %d, device not enabled\n",
    687  1.112    dyoung 			       device_xname(sc->sc_dev), port);
    688  1.110  kiyohara #endif
    689  1.110  kiyohara 			continue;
    690  1.110  kiyohara 		}
    691  1.133     skrll 		/* port reset may cause Warm Reset Change, drop it. */
    692  1.133     skrll 		if (change & UPS_C_BH_PORT_RESET)
    693  1.133     skrll 			usbd_clear_port_feature(dev, port,
    694  1.133     skrll 			    UHF_C_BH_PORT_RESET);
    695  1.133     skrll 
    696  1.133     skrll 		/*
    697  1.133     skrll 		 * Figure out device speed from power bit of port status.
    698  1.133     skrll 		 *  USB 2.0 ch 11.24.2.7.1
    699  1.133     skrll 		 *  USB 3.1 ch 10.16.2.6.1
    700  1.133     skrll 		 */
    701  1.133     skrll 		int sts = status;
    702  1.133     skrll 		if ((sts & UPS_PORT_POWER) == 0)
    703  1.133     skrll 			sts &= ~UPS_PORT_POWER_SS;
    704    1.1  augustss 
    705  1.133     skrll 		if (sts & UPS_HIGH_SPEED)
    706   1.56  augustss 			speed = USB_SPEED_HIGH;
    707  1.133     skrll 		else if (sts & UPS_LOW_SPEED)
    708   1.56  augustss 			speed = USB_SPEED_LOW;
    709  1.133     skrll 		else {
    710  1.133     skrll 			/*
    711  1.133     skrll 			 * If there is no power bit set, it is certainly
    712  1.133     skrll 			 * a Super Speed device, so use the speed of its
    713  1.133     skrll 			 * parent hub.
    714  1.133     skrll 			 */
    715  1.133     skrll 			if (sts & UPS_PORT_POWER)
    716  1.133     skrll 				speed = USB_SPEED_FULL;
    717  1.133     skrll 			else
    718  1.133     skrll 				speed = dev->ud_speed;
    719  1.133     skrll 		}
    720  1.133     skrll 
    721  1.133     skrll 		/*
    722  1.133     skrll 		 * Reduce the speed, otherwise we won't setup the proper
    723  1.133     skrll 		 * transfer methods.
    724  1.133     skrll 		 */
    725  1.133     skrll 		if (speed > dev->ud_speed)
    726  1.133     skrll 			speed = dev->ud_speed;
    727  1.129     skrll 
    728  1.133     skrll 		DPRINTF("uhub%d speed %u", device_unit(sc->sc_dev), speed, 0,
    729  1.129     skrll 		    0);
    730  1.129     skrll 
    731  1.133     skrll 		/*
    732  1.133     skrll 		 * To check whether port has power,
    733  1.133     skrll 		 *  check UPS_PORT_POWER_SS bit if port speed is SS, and
    734  1.133     skrll 		 *  check UPS_PORT_POWER bit if port speed is HS/FS/LS.
    735  1.133     skrll 		 */
    736  1.133     skrll 		if (USB_IS_SS(speed)) {
    737  1.133     skrll 			/* SS hub port */
    738  1.133     skrll 			if (!(status & UPS_PORT_POWER_SS))
    739  1.133     skrll 				aprint_normal_dev(sc->sc_dev,
    740  1.133     skrll 				    "strange, connected port %d has no power\n",
    741  1.133     skrll 				    port);
    742  1.133     skrll 		} else {
    743  1.133     skrll 			/* HS/FS/LS hub port */
    744  1.133     skrll 			if (!(status & UPS_PORT_POWER))
    745  1.133     skrll 				aprint_normal_dev(sc->sc_dev,
    746  1.133     skrll 				    "strange, connected port %d has no power\n",
    747  1.133     skrll 				    port);
    748  1.133     skrll 		}
    749  1.133     skrll 
    750    1.1  augustss 		/* Get device info and set its address. */
    751  1.133     skrll 		err = usbd_new_device(sc->sc_dev, dev->ud_bus,
    752  1.133     skrll 			  dev->ud_depth + 1, speed, port, up);
    753    1.1  augustss 		/* XXX retry a few times? */
    754   1.33  augustss 		if (err) {
    755  1.127     skrll 			DPRINTF("usbd_new_device failed, error %d", err, 0, 0,
    756  1.127     skrll 			    0);
    757    1.1  augustss 			/* Avoid addressing problems by disabling. */
    758    1.1  augustss 			/* usbd_reset_port(dev, port, &up->status); */
    759   1.30  augustss 
    760   1.58  augustss 			/*
    761   1.30  augustss 			 * The unit refused to accept a new address, or had
    762   1.30  augustss 			 * some other serious problem.  Since we cannot leave
    763   1.30  augustss 			 * at 0 we have to disable the port instead.
    764   1.30  augustss 			 */
    765   1.98      cube 			aprint_error_dev(sc->sc_dev,
    766   1.98      cube 			    "device problem, disabling port %d\n", port);
    767   1.30  augustss 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    768    1.1  augustss 		} else {
    769   1.47  augustss 			/* The port set up succeeded, reset error count. */
    770  1.133     skrll 			up->up_restartcnt = 0;
    771   1.47  augustss 
    772  1.133     skrll 			if (up->up_dev->ud_hub)
    773  1.133     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    774    1.1  augustss 		}
    775    1.1  augustss 	}
    776  1.133     skrll 	mutex_enter(&sc->sc_lock);
    777   1.87  drochner 	sc->sc_explorepending = 0;
    778  1.133     skrll 	for (int i = 0; i < sc->sc_statuslen; i++) {
    779  1.133     skrll 		if (sc->sc_statuspend[i] != 0) {
    780  1.133     skrll 			memcpy(sc->sc_status, sc->sc_statuspend,
    781  1.133     skrll 			    sc->sc_statuslen);
    782  1.133     skrll 			memset(sc->sc_statuspend, 0, sc->sc_statuslen);
    783  1.133     skrll 			usb_needs_explore(sc->sc_hub);
    784  1.133     skrll 			break;
    785  1.133     skrll 		}
    786  1.133     skrll 	}
    787  1.133     skrll 	mutex_exit(&sc->sc_lock);
    788  1.133     skrll 
    789  1.133     skrll 	return USBD_NORMAL_COMPLETION;
    790    1.1  augustss }
    791    1.1  augustss 
    792   1.18  augustss /*
    793   1.18  augustss  * Called from process context when the hub is gone.
    794   1.18  augustss  * Detach all devices on active ports.
    795   1.18  augustss  */
    796  1.105    dyoung int
    797  1.105    dyoung uhub_detach(device_t self, int flags)
    798   1.18  augustss {
    799  1.107    dyoung 	struct uhub_softc *sc = device_private(self);
    800  1.133     skrll 	struct usbd_hub *hub = sc->sc_hub->ud_hub;
    801   1.18  augustss 	struct usbd_port *rup;
    802  1.108    dyoung 	int nports, port, rc;
    803   1.18  augustss 
    804  1.127     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    805  1.127     skrll 
    806  1.133     skrll 	DPRINTF("uhub%d flags=%d", device_unit(self), flags, 0, 0);
    807   1.18  augustss 
    808   1.39  augustss 	if (hub == NULL)		/* Must be partially working */
    809  1.133     skrll 		return 0;
    810   1.18  augustss 
    811  1.117       mrg 	/* XXXSMP usb */
    812  1.117       mrg 	KERNEL_LOCK(1, curlwp);
    813  1.117       mrg 
    814  1.133     skrll 	nports = hub->uh_hubdesc.bNbrPorts;
    815  1.132     skrll 	for (port = 0; port < nports; port++) {
    816  1.133     skrll 		rup = &hub->uh_ports[port];
    817  1.133     skrll 		if (rup->up_dev == NULL)
    818  1.108    dyoung 			continue;
    819  1.117       mrg 		if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
    820  1.117       mrg 			/* XXXSMP usb */
    821  1.117       mrg 			KERNEL_UNLOCK_ONE(curlwp);
    822  1.117       mrg 
    823  1.108    dyoung 			return rc;
    824  1.117       mrg 		}
    825   1.18  augustss 	}
    826   1.58  augustss 
    827  1.108    dyoung 	pmf_device_deregister(self);
    828  1.108    dyoung 	usbd_abort_pipe(sc->sc_ipipe);
    829  1.108    dyoung 	usbd_close_pipe(sc->sc_ipipe);
    830  1.108    dyoung 
    831  1.105    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
    832   1.37  augustss 
    833  1.133     skrll 	if (hub->uh_ports[0].up_tt)
    834  1.133     skrll 		kmem_free(hub->uh_ports[0].up_tt,
    835  1.133     skrll 		    (UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    836  1.133     skrll 		    sizeof(struct usbd_tt));
    837  1.133     skrll 	kmem_free(hub,
    838  1.133     skrll 	    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
    839  1.133     skrll 	sc->sc_hub->ud_hub = NULL;
    840   1.84  drochner 	if (sc->sc_status)
    841  1.133     skrll 		kmem_free(sc->sc_status, sc->sc_statuslen);
    842  1.133     skrll 	if (sc->sc_statuspend)
    843  1.133     skrll 		kmem_free(sc->sc_statuspend, sc->sc_statuslen);
    844  1.101  drochner 	if (sc->sc_statusbuf)
    845  1.133     skrll 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    846  1.133     skrll 
    847  1.133     skrll 	mutex_destroy(&sc->sc_lock);
    848   1.18  augustss 
    849  1.117       mrg 	/* XXXSMP usb */
    850  1.117       mrg 	KERNEL_UNLOCK_ONE(curlwp);
    851  1.117       mrg 
    852  1.133     skrll 	return 0;
    853   1.18  augustss }
    854   1.34  augustss 
    855  1.103      kent int
    856  1.103      kent uhub_rescan(device_t self, const char *ifattr, const int *locators)
    857  1.103      kent {
    858  1.103      kent 	struct uhub_softc *sc = device_private(self);
    859  1.133     skrll 	struct usbd_hub *hub = sc->sc_hub->ud_hub;
    860  1.133     skrll 	struct usbd_device *dev;
    861  1.124    martin 	int port;
    862  1.103      kent 
    863  1.133     skrll 	for (port = 0; port < hub->uh_hubdesc.bNbrPorts; port++) {
    864  1.133     skrll 		dev = hub->uh_ports[port].up_dev;
    865  1.103      kent 		if (dev == NULL)
    866  1.103      kent 			continue;
    867  1.124    martin 		usbd_reattach_device(sc->sc_dev, dev, port, locators);
    868  1.103      kent 	}
    869  1.103      kent 	return 0;
    870  1.103      kent }
    871  1.103      kent 
    872   1.34  augustss /* Called when a device has been detached from it */
    873   1.95    dyoung void
    874   1.95    dyoung uhub_childdet(device_t self, device_t child)
    875   1.34  augustss {
    876   1.95    dyoung 	struct uhub_softc *sc = device_private(self);
    877  1.133     skrll 	struct usbd_device *devhub = sc->sc_hub;
    878  1.133     skrll 	struct usbd_device *dev;
    879   1.95    dyoung 	int nports;
    880   1.95    dyoung 	int port;
    881   1.95    dyoung 	int i;
    882   1.95    dyoung 
    883  1.133     skrll 	if (!devhub->ud_hub)
    884   1.95    dyoung 		/* should never happen; children are only created after init */
    885   1.95    dyoung 		panic("hub not fully initialised, but child deleted?");
    886   1.95    dyoung 
    887  1.133     skrll 	nports = devhub->ud_hub->uh_hubdesc.bNbrPorts;
    888   1.95    dyoung 	for (port = 0; port < nports; port++) {
    889  1.133     skrll 		dev = devhub->ud_hub->uh_ports[port].up_dev;
    890  1.133     skrll 		if (!dev || dev->ud_subdevlen == 0)
    891   1.95    dyoung 			continue;
    892  1.133     skrll 		for (i = 0; i < dev->ud_subdevlen; i++) {
    893  1.133     skrll 			if (dev->ud_subdevs[i] == child) {
    894  1.133     skrll 				dev->ud_subdevs[i] = NULL;
    895  1.133     skrll 				dev->ud_nifaces_claimed--;
    896   1.95    dyoung 			}
    897   1.95    dyoung 		}
    898  1.133     skrll 		if (dev->ud_nifaces_claimed == 0) {
    899  1.133     skrll 			kmem_free(dev->ud_subdevs,
    900  1.133     skrll 			    dev->ud_subdevlen * sizeof(device_t));
    901  1.133     skrll 			dev->ud_subdevs = NULL;
    902  1.133     skrll 			dev->ud_subdevlen = 0;
    903  1.113  jmcneill 		}
    904   1.95    dyoung 	}
    905   1.34  augustss }
    906   1.34  augustss 
    907   1.18  augustss 
    908   1.18  augustss /*
    909   1.18  augustss  * Hub interrupt.
    910   1.18  augustss  * This an indication that some port has changed status.
    911   1.18  augustss  * Notify the bus event handler thread that we need
    912   1.18  augustss  * to be explored again.
    913   1.18  augustss  */
    914    1.1  augustss void
    915  1.133     skrll uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
    916    1.1  augustss {
    917    1.1  augustss 	struct uhub_softc *sc = addr;
    918    1.1  augustss 
    919  1.127     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    920  1.127     skrll 
    921  1.133     skrll 	DPRINTFN(5, "uhub%d", device_unit(sc->sc_dev), 0, 0, 0);
    922   1.87  drochner 
    923   1.50  augustss 	if (status == USBD_STALLED)
    924    1.9  augustss 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    925  1.133     skrll 	else if (status == USBD_NORMAL_COMPLETION) {
    926  1.133     skrll 
    927  1.133     skrll 		mutex_enter(&sc->sc_lock);
    928  1.133     skrll 
    929  1.133     skrll 		DPRINTFN(5, "uhub%d: explore pending %d",
    930  1.133     skrll 		    device_unit(sc->sc_dev), sc->sc_explorepending, 0, 0);
    931  1.133     skrll 
    932  1.133     skrll 		/* merge port bitmap into pending interrupts list */
    933  1.133     skrll 		for (size_t i = 0; i < sc->sc_statuslen; i++) {
    934  1.133     skrll 			sc->sc_statuspend[i] |= sc->sc_statusbuf[i];
    935  1.133     skrll 
    936  1.133     skrll 			DPRINTFN(5, "uhub%d: pending/new ports "
    937  1.133     skrll 			    "[%d] %#x/%#x", device_unit(sc->sc_dev),
    938  1.133     skrll 			    i, sc->sc_statuspend[i], sc->sc_statusbuf[i]);
    939  1.133     skrll 		}
    940  1.133     skrll 
    941  1.133     skrll 		if (!sc->sc_explorepending) {
    942  1.133     skrll 			sc->sc_explorepending = 1;
    943  1.133     skrll 
    944  1.133     skrll 			memcpy(sc->sc_status, sc->sc_statuspend,
    945  1.133     skrll 			    sc->sc_statuslen);
    946  1.133     skrll 			memset(sc->sc_statuspend, 0, sc->sc_statuslen);
    947  1.133     skrll 
    948  1.133     skrll 			for (size_t i = 0; i < sc->sc_statuslen; i++) {
    949  1.133     skrll 				DPRINTFN(5, "uhub%d: exploring ports "
    950  1.133     skrll 				    "[%d] %#x", device_unit(sc->sc_dev),
    951  1.133     skrll 				    i, sc->sc_status[i], 0);
    952  1.133     skrll 			}
    953  1.133     skrll 
    954  1.133     skrll 			usb_needs_explore(sc->sc_hub);
    955  1.133     skrll 		}
    956  1.133     skrll 		mutex_exit(&sc->sc_lock);
    957   1.87  drochner 	}
    958    1.1  augustss }
    959