Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.154
      1  1.154  jmcneill /*	$NetBSD: uhub.c,v 1.154 2021/10/10 23:39:50 jmcneill 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.154  jmcneill __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.154 2021/10/10 23:39:50 jmcneill 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.145      maxv #include <sys/kcov.h>
     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.143       mrg #define UHUBHIST_FUNC()		USBHIST_FUNC()
    102  1.143       mrg #define UHUBHIST_CALLED(name)	USBHIST_CALLED(uhubdebug)
    103  1.143       mrg #define UHUBHIST_CALLARGS(FMT,A,B,C,D) \
    104  1.143       mrg 				USBHIST_CALLARGS(uhubdebug,FMT,A,B,C,D)
    105    1.1  augustss 
    106    1.1  augustss struct uhub_softc {
    107  1.133     skrll 	device_t		 sc_dev;	/* base device */
    108  1.133     skrll 	struct usbd_device	*sc_hub;	/* USB device */
    109  1.133     skrll 	int			 sc_proto;	/* device protocol */
    110  1.133     skrll 	struct usbd_pipe	*sc_ipipe;	/* interrupt pipe */
    111  1.133     skrll 
    112  1.133     skrll 	kmutex_t		 sc_lock;
    113  1.148  riastrad 	kcondvar_t		 sc_cv;
    114  1.133     skrll 
    115  1.133     skrll 	uint8_t			*sc_statusbuf;
    116  1.133     skrll 	uint8_t			*sc_statuspend;
    117  1.133     skrll 	uint8_t			*sc_status;
    118  1.133     skrll 	size_t			 sc_statuslen;
    119  1.139       mrg 	bool			 sc_explorepending;
    120  1.139       mrg 	bool			 sc_first_explore;
    121  1.139       mrg 	bool			 sc_running;
    122  1.152  riastrad 	bool			 sc_rescan;
    123  1.148  riastrad 
    124  1.148  riastrad 	struct lwp		*sc_exploring;
    125    1.1  augustss };
    126   1.87  drochner 
    127  1.154  jmcneill typedef __BITMAP_TYPE(, uint8_t, UHD_NPORTS_MAX + 1) usb_port_mask;
    128  1.154  jmcneill 
    129  1.133     skrll #define UHUB_IS_HIGH_SPEED(sc) \
    130  1.133     skrll     ((sc)->sc_proto == UDPROTO_HSHUBSTT || (sc)->sc_proto == UDPROTO_HSHUBMTT)
    131   1.86  drochner #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
    132    1.1  augustss 
    133   1.87  drochner #define PORTSTAT_ISSET(sc, port) \
    134   1.87  drochner 	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
    135   1.87  drochner 
    136  1.133     skrll Static usbd_status uhub_explore(struct usbd_device *);
    137  1.133     skrll Static void uhub_intr(struct usbd_xfer *, void *, usbd_status);
    138    1.1  augustss 
    139   1.32  augustss 
    140   1.58  augustss /*
    141   1.32  augustss  * We need two attachment points:
    142   1.32  augustss  * hub to usb and hub to hub
    143   1.32  augustss  * Every other driver only connects to hubs
    144   1.32  augustss  */
    145    1.1  augustss 
    146  1.144      maxv static int uhub_match(device_t, cfdata_t, void *);
    147  1.144      maxv static void uhub_attach(device_t, device_t, void *);
    148  1.144      maxv static int uhub_rescan(device_t, const char *, const int *);
    149  1.144      maxv static void uhub_childdet(device_t, device_t);
    150  1.144      maxv static int uhub_detach(device_t, int);
    151  1.142       mrg 
    152  1.104    dyoung CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
    153  1.108    dyoung     uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
    154  1.104    dyoung     DVF_DETACH_SHUTDOWN);
    155  1.138   msaitoh CFATTACH_DECL3_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
    156  1.138   msaitoh     uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
    157  1.138   msaitoh     DVF_DETACH_SHUTDOWN);
    158    1.1  augustss 
    159  1.109     pooka /*
    160  1.109     pooka  * Setting this to 1 makes sure than an uhub attaches even at higher
    161  1.109     pooka  * priority than ugen when ugen_override is set to 1.  This allows to
    162  1.109     pooka  * probe the whole USB bus and attach functions with ugen.
    163  1.109     pooka  */
    164  1.109     pooka int uhub_ubermatch = 0;
    165  1.109     pooka 
    166  1.133     skrll static usbd_status
    167  1.133     skrll usbd_get_hub_desc(struct usbd_device *dev, usb_hub_descriptor_t *hd, int speed)
    168  1.133     skrll {
    169  1.133     skrll 	usb_device_request_t req;
    170  1.133     skrll 	usbd_status err;
    171  1.133     skrll 	int nports;
    172  1.133     skrll 
    173  1.133     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    174  1.133     skrll 
    175  1.133     skrll 	/* don't issue UDESC_HUB to SS hub, or it would stall */
    176  1.133     skrll 	if (dev->ud_depth != 0 && USB_IS_SS(dev->ud_speed)) {
    177  1.133     skrll 		usb_hub_ss_descriptor_t hssd;
    178  1.133     skrll 		int rmvlen;
    179  1.133     skrll 
    180  1.133     skrll 		memset(&hssd, 0, sizeof(hssd));
    181  1.133     skrll 		req.bmRequestType = UT_READ_CLASS_DEVICE;
    182  1.133     skrll 		req.bRequest = UR_GET_DESCRIPTOR;
    183  1.133     skrll 		USETW2(req.wValue, UDESC_SS_HUB, 0);
    184  1.133     skrll 		USETW(req.wIndex, 0);
    185  1.133     skrll 		USETW(req.wLength, USB_HUB_SS_DESCRIPTOR_SIZE);
    186  1.133     skrll 		DPRINTFN(1, "getting sshub descriptor", 0, 0, 0, 0);
    187  1.133     skrll 		err = usbd_do_request(dev, &req, &hssd);
    188  1.133     skrll 		nports = hssd.bNbrPorts;
    189  1.133     skrll 		if (dev->ud_depth != 0 && nports > UHD_SS_NPORTS_MAX) {
    190  1.137  pgoyette 			DPRINTF("num of ports %jd exceeds maxports %jd",
    191  1.133     skrll 			    nports, UHD_SS_NPORTS_MAX, 0, 0);
    192  1.133     skrll 			nports = hd->bNbrPorts = UHD_SS_NPORTS_MAX;
    193  1.133     skrll 		}
    194  1.133     skrll 		rmvlen = (nports + 7) / 8;
    195  1.133     skrll 		hd->bDescLength = USB_HUB_DESCRIPTOR_SIZE +
    196  1.133     skrll 		    (rmvlen > 1 ? rmvlen : 1) - 1;
    197  1.133     skrll 		memcpy(hd->DeviceRemovable, hssd.DeviceRemovable, rmvlen);
    198  1.133     skrll 		hd->bDescriptorType		= hssd.bDescriptorType;
    199  1.133     skrll 		hd->bNbrPorts			= hssd.bNbrPorts;
    200  1.133     skrll 		hd->wHubCharacteristics[0]	= hssd.wHubCharacteristics[0];
    201  1.133     skrll 		hd->wHubCharacteristics[1]	= hssd.wHubCharacteristics[1];
    202  1.133     skrll 		hd->bPwrOn2PwrGood		= hssd.bPwrOn2PwrGood;
    203  1.133     skrll 		hd->bHubContrCurrent		= hssd.bHubContrCurrent;
    204  1.133     skrll 	} else {
    205  1.133     skrll 		req.bmRequestType = UT_READ_CLASS_DEVICE;
    206  1.133     skrll 		req.bRequest = UR_GET_DESCRIPTOR;
    207  1.133     skrll 		USETW2(req.wValue, UDESC_HUB, 0);
    208  1.133     skrll 		USETW(req.wIndex, 0);
    209  1.133     skrll 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    210  1.133     skrll 		DPRINTFN(1, "getting hub descriptor", 0, 0, 0, 0);
    211  1.133     skrll 		err = usbd_do_request(dev, &req, hd);
    212  1.133     skrll 		nports = hd->bNbrPorts;
    213  1.133     skrll 		if (!err && nports > 7) {
    214  1.133     skrll 			USETW(req.wLength,
    215  1.133     skrll 			    USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    216  1.133     skrll 			err = usbd_do_request(dev, &req, hd);
    217  1.133     skrll 		}
    218  1.133     skrll 	}
    219  1.133     skrll 
    220  1.133     skrll 	return err;
    221  1.133     skrll }
    222  1.133     skrll 
    223  1.133     skrll static usbd_status
    224  1.133     skrll usbd_set_hub_depth(struct usbd_device *dev, int depth)
    225  1.133     skrll {
    226  1.133     skrll 	usb_device_request_t req;
    227  1.133     skrll 
    228  1.133     skrll 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    229  1.133     skrll 	req.bRequest = UR_SET_HUB_DEPTH;
    230  1.133     skrll 	USETW(req.wValue, depth);
    231  1.133     skrll 	USETW(req.wIndex, 0);
    232  1.133     skrll 	USETW(req.wLength, 0);
    233  1.133     skrll 	return usbd_do_request(dev, &req, 0);
    234  1.133     skrll }
    235  1.133     skrll 
    236  1.144      maxv static int
    237  1.105    dyoung uhub_match(device_t parent, cfdata_t match, void *aux)
    238    1.1  augustss {
    239  1.107    dyoung 	struct usb_attach_arg *uaa = aux;
    240  1.109     pooka 	int matchvalue;
    241  1.109     pooka 
    242  1.127     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    243  1.127     skrll 
    244  1.109     pooka 	if (uhub_ubermatch)
    245  1.109     pooka 		matchvalue = UMATCH_HIGHEST+1;
    246  1.109     pooka 	else
    247  1.109     pooka 		matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS;
    248   1.58  augustss 
    249  1.137  pgoyette 	DPRINTFN(5, "uaa=%#jx", (uintptr_t)uaa, 0, 0, 0);
    250   1.58  augustss 	/*
    251    1.1  augustss 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    252    1.1  augustss 	 * so we just ignore the subclass.
    253    1.1  augustss 	 */
    254  1.133     skrll 	if (uaa->uaa_class == UDCLASS_HUB)
    255  1.133     skrll 		return matchvalue;
    256  1.133     skrll 	return UMATCH_NONE;
    257    1.1  augustss }
    258    1.1  augustss 
    259  1.144      maxv static void
    260  1.105    dyoung uhub_attach(device_t parent, device_t self, void *aux)
    261    1.1  augustss {
    262  1.107    dyoung 	struct uhub_softc *sc = device_private(self);
    263  1.107    dyoung 	struct usb_attach_arg *uaa = aux;
    264  1.133     skrll 	struct usbd_device *dev = uaa->uaa_device;
    265   1.76  augustss 	char *devinfop;
    266   1.33  augustss 	usbd_status err;
    267   1.70  augustss 	struct usbd_hub *hub = NULL;
    268    1.1  augustss 	usb_hub_descriptor_t hubdesc;
    269   1.42  augustss 	int p, port, nports, nremov, pwrdly;
    270  1.133     skrll 	struct usbd_interface *iface;
    271    1.1  augustss 	usb_endpoint_descriptor_t *ed;
    272   1.70  augustss 	struct usbd_tt *tts = NULL;
    273   1.58  augustss 
    274  1.153  riastrad 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    275  1.153  riastrad 
    276  1.153  riastrad 	KASSERT(usb_in_event_thread(parent));
    277  1.153  riastrad 
    278  1.139       mrg 	config_pending_incr(self);
    279  1.139       mrg 
    280   1.98      cube 	sc->sc_dev = self;
    281    1.1  augustss 	sc->sc_hub = dev;
    282  1.133     skrll 	sc->sc_proto = uaa->uaa_proto;
    283   1.76  augustss 
    284   1.76  augustss 	devinfop = usbd_devinfo_alloc(dev, 1);
    285   1.96  jmcneill 	aprint_naive("\n");
    286   1.96  jmcneill 	aprint_normal(": %s\n", devinfop);
    287   1.76  augustss 	usbd_devinfo_free(devinfop);
    288    1.1  augustss 
    289  1.133     skrll 	if (dev->ud_depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    290   1.98      cube 		aprint_normal_dev(self, "%s transaction translator%s\n",
    291   1.70  augustss 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    292   1.70  augustss 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    293   1.69  augustss 	}
    294   1.69  augustss 
    295   1.33  augustss 	err = usbd_set_config_index(dev, 0, 1);
    296   1.33  augustss 	if (err) {
    297  1.137  pgoyette 		DPRINTF("configuration failed, sc %#jx error %jd",
    298  1.137  pgoyette 		    (uintptr_t)sc, err, 0, 0);
    299  1.139       mrg 		goto bad2;
    300    1.1  augustss 	}
    301    1.1  augustss 
    302  1.133     skrll 	if (dev->ud_depth > USB_HUB_MAX_DEPTH) {
    303   1.98      cube 		aprint_error_dev(self,
    304   1.98      cube 		    "hub depth (%d) exceeded, hub ignored\n",
    305   1.98      cube 		    USB_HUB_MAX_DEPTH);
    306  1.139       mrg 		goto bad2;
    307    1.1  augustss 	}
    308    1.1  augustss 
    309    1.1  augustss 	/* Get hub descriptor. */
    310  1.133     skrll 	memset(&hubdesc, 0, sizeof(hubdesc));
    311  1.133     skrll 	err = usbd_get_hub_desc(dev, &hubdesc, dev->ud_speed);
    312   1.11  augustss 	nports = hubdesc.bNbrPorts;
    313   1.33  augustss 	if (err) {
    314  1.137  pgoyette 		DPRINTF("getting hub descriptor failed, uhub%jd error %jd",
    315  1.128     skrll 		    device_unit(self), err, 0, 0);
    316  1.139       mrg 		goto bad2;
    317    1.1  augustss 	}
    318    1.1  augustss 
    319   1.11  augustss 	for (nremov = 0, port = 1; port <= nports; port++)
    320   1.11  augustss 		if (!UHD_NOT_REMOV(&hubdesc, port))
    321   1.11  augustss 			nremov++;
    322   1.98      cube 	aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
    323   1.98      cube 	    nports, nports != 1 ? "s" : "", nremov,
    324  1.133     skrll 	    dev->ud_selfpowered ? "self" : "bus");
    325   1.11  augustss 
    326   1.70  augustss 	if (nports == 0) {
    327   1.98      cube 		aprint_debug_dev(self, "no ports, hub ignored\n");
    328   1.70  augustss 		goto bad;
    329   1.70  augustss 	}
    330   1.70  augustss 
    331  1.133     skrll 	hub = kmem_alloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    332  1.133     skrll 	    KM_SLEEP);
    333  1.133     skrll 	dev->ud_hub = hub;
    334  1.133     skrll 	dev->ud_hub->uh_hubsoftc = sc;
    335  1.133     skrll 	hub->uh_explore = uhub_explore;
    336  1.133     skrll 	hub->uh_hubdesc = hubdesc;
    337  1.133     skrll 
    338  1.133     skrll 	if (USB_IS_SS(dev->ud_speed) && dev->ud_depth != 0) {
    339  1.133     skrll 		aprint_debug_dev(self, "setting hub depth %u\n",
    340  1.134     skrll 		    dev->ud_depth - 1);
    341  1.133     skrll 		err = usbd_set_hub_depth(dev, dev->ud_depth - 1);
    342  1.133     skrll 		if (err) {
    343  1.133     skrll 			aprint_error_dev(self, "can't set depth\n");
    344  1.133     skrll 			goto bad;
    345  1.133     skrll 		}
    346  1.133     skrll 	}
    347   1.58  augustss 
    348    1.1  augustss 	/* Set up interrupt pipe. */
    349   1.33  augustss 	err = usbd_device2interface_handle(dev, 0, &iface);
    350   1.33  augustss 	if (err) {
    351   1.98      cube 		aprint_error_dev(self, "no interface handle\n");
    352   1.18  augustss 		goto bad;
    353    1.1  augustss 	}
    354   1.83  drochner 
    355   1.83  drochner 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
    356   1.83  drochner 		err = usbd_set_interface(iface, 1);
    357   1.83  drochner 		if (err)
    358   1.98      cube 			aprint_error_dev(self, "can't enable multiple TTs\n");
    359   1.83  drochner 	}
    360   1.83  drochner 
    361    1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    362   1.33  augustss 	if (ed == NULL) {
    363   1.98      cube 		aprint_error_dev(self, "no endpoint descriptor\n");
    364   1.18  augustss 		goto bad;
    365    1.1  augustss 	}
    366    1.1  augustss 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    367   1.98      cube 		aprint_error_dev(self, "bad interrupt endpoint\n");
    368   1.18  augustss 		goto bad;
    369    1.1  augustss 	}
    370    1.1  augustss 
    371   1.87  drochner 	sc->sc_statuslen = (nports + 1 + 7) / 8;
    372  1.133     skrll 	sc->sc_statusbuf = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
    373  1.133     skrll 	sc->sc_statuspend = kmem_zalloc(sc->sc_statuslen, KM_SLEEP);
    374  1.133     skrll 	sc->sc_status = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
    375  1.133     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    376  1.148  riastrad 	cv_init(&sc->sc_cv, "uhubex");
    377   1.84  drochner 
    378   1.87  drochner 	/* force initial scan */
    379   1.87  drochner 	memset(sc->sc_status, 0xff, sc->sc_statuslen);
    380  1.139       mrg 	sc->sc_explorepending = true;
    381   1.87  drochner 
    382   1.33  augustss 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    383  1.122  jmcneill 		  USBD_SHORT_XFER_OK|USBD_MPSAFE, &sc->sc_ipipe, sc,
    384  1.122  jmcneill 		  sc->sc_statusbuf, sc->sc_statuslen,
    385  1.122  jmcneill 		  uhub_intr, USBD_DEFAULT_INTERVAL);
    386   1.33  augustss 	if (err) {
    387   1.98      cube 		aprint_error_dev(self, "cannot open interrupt pipe\n");
    388   1.18  augustss 		goto bad;
    389    1.1  augustss 	}
    390    1.1  augustss 
    391   1.11  augustss 	/* Wait with power off for a while. */
    392   1.13  augustss 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    393   1.11  augustss 
    394  1.105    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
    395   1.37  augustss 
    396   1.42  augustss 	/*
    397   1.42  augustss 	 * To have the best chance of success we do things in the exact same
    398  1.123  jakllsch 	 * order as Windows 98.  This should not be necessary, but some
    399   1.42  augustss 	 * devices do not follow the USB specs to the letter.
    400   1.42  augustss 	 *
    401   1.42  augustss 	 * These are the events on the bus when a hub is attached:
    402   1.42  augustss 	 *  Get device and config descriptors (see attach code)
    403   1.42  augustss 	 *  Get hub descriptor (see above)
    404   1.42  augustss 	 *  For all ports
    405   1.42  augustss 	 *     turn on power
    406   1.42  augustss 	 *     wait for power to become stable
    407   1.42  augustss 	 * (all below happens in explore code)
    408   1.42  augustss 	 *  For all ports
    409   1.42  augustss 	 *     clear C_PORT_CONNECTION
    410   1.42  augustss 	 *  For all ports
    411   1.42  augustss 	 *     get port status
    412   1.42  augustss 	 *     if device connected
    413   1.57  augustss 	 *        wait 100 ms
    414   1.42  augustss 	 *        turn on reset
    415   1.42  augustss 	 *        wait
    416   1.42  augustss 	 *        clear C_PORT_RESET
    417   1.42  augustss 	 *        get port status
    418   1.42  augustss 	 *        proceed with device attachment
    419   1.42  augustss 	 */
    420   1.42  augustss 
    421   1.78  christos 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
    422  1.133     skrll 		tts = kmem_alloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    423  1.133     skrll 			     sizeof(struct usbd_tt), KM_SLEEP);
    424   1.70  augustss 	}
    425   1.42  augustss 	/* Set up data structures */
    426  1.141      manu 	for (p = 1; p <= nports; p++) {
    427  1.141      manu 		struct usbd_port *up = &hub->uh_ports[p - 1];
    428  1.133     skrll 		up->up_dev = NULL;
    429  1.133     skrll 		up->up_parent = dev;
    430  1.141      manu 		up->up_portno = p;
    431  1.133     skrll 		if (dev->ud_selfpowered)
    432   1.42  augustss 			/* Self powered hub, give ports maximum current. */
    433  1.133     skrll 			up->up_power = USB_MAX_POWER;
    434   1.42  augustss 		else
    435  1.133     skrll 			up->up_power = USB_MIN_POWER;
    436  1.133     skrll 		up->up_restartcnt = 0;
    437  1.133     skrll 		up->up_reattach = 0;
    438   1.70  augustss 		if (UHUB_IS_HIGH_SPEED(sc)) {
    439  1.141      manu 			up->up_tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p - 1];
    440  1.133     skrll 			up->up_tt->utt_hub = hub;
    441   1.70  augustss 		} else {
    442  1.133     skrll 			up->up_tt = NULL;
    443   1.70  augustss 		}
    444   1.42  augustss 	}
    445   1.42  augustss 
    446   1.42  augustss 	/* XXX should check for none, individual, or ganged power? */
    447   1.42  augustss 
    448  1.133     skrll 	pwrdly = dev->ud_hub->uh_hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    449   1.42  augustss 	    + USB_EXTRA_POWER_UP_TIME;
    450   1.42  augustss 	for (port = 1; port <= nports; port++) {
    451   1.42  augustss 		/* Turn the power on. */
    452   1.42  augustss 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    453   1.33  augustss 		if (err)
    454   1.98      cube 			aprint_error_dev(self, "port %d power on failed, %s\n",
    455   1.98      cube 			    port, usbd_errstr(err));
    456  1.137  pgoyette 		DPRINTF("uhub%jd turn on port %jd power", device_unit(self),
    457  1.128     skrll 		    port, 0, 0);
    458   1.94  jmcneill 	}
    459   1.94  jmcneill 
    460   1.94  jmcneill 	/* Wait for stable power if we are not a root hub */
    461  1.133     skrll 	if (dev->ud_powersrc->up_parent != NULL)
    462   1.42  augustss 		usbd_delay_ms(dev, pwrdly);
    463   1.42  augustss 
    464   1.42  augustss 	/* The usual exploration will finish the setup. */
    465  1.139       mrg 	sc->sc_running = true;
    466  1.139       mrg 	sc->sc_first_explore = true;
    467   1.11  augustss 
    468   1.92  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    469   1.92  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    470   1.92  jmcneill 
    471  1.107    dyoung 	return;
    472   1.11  augustss 
    473   1.18  augustss  bad:
    474   1.84  drochner 	if (sc->sc_status)
    475  1.133     skrll 		kmem_free(sc->sc_status, sc->sc_statuslen);
    476  1.133     skrll 	if (sc->sc_statuspend)
    477  1.133     skrll 		kmem_free(sc->sc_statuspend, sc->sc_statuslen);
    478  1.101  drochner 	if (sc->sc_statusbuf)
    479  1.133     skrll 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    480   1.70  augustss 	if (hub)
    481  1.133     skrll 		kmem_free(hub,
    482  1.133     skrll 		    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
    483  1.133     skrll 	dev->ud_hub = NULL;
    484  1.139       mrg  bad2:
    485  1.139       mrg 	config_pending_decr(self);
    486   1.11  augustss }
    487   1.11  augustss 
    488    1.1  augustss usbd_status
    489  1.133     skrll uhub_explore(struct usbd_device *dev)
    490    1.1  augustss {
    491  1.133     skrll 	usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
    492  1.133     skrll 	struct uhub_softc *sc = dev->ud_hub->uh_hubsoftc;
    493    1.1  augustss 	struct usbd_port *up;
    494  1.152  riastrad 	struct usbd_device *subdev;
    495   1.33  augustss 	usbd_status err;
    496   1.56  augustss 	int speed;
    497    1.1  augustss 	int port;
    498  1.152  riastrad 	int change, status, reconnect, rescan;
    499  1.154  jmcneill 	usb_port_mask powerup_port;
    500  1.154  jmcneill 	int powerup_port_count = 0;
    501    1.1  augustss 
    502  1.143       mrg 	UHUBHIST_FUNC();
    503  1.143       mrg 	UHUBHIST_CALLARGS("uhub%jd dev=%#jx addr=%jd speed=%ju",
    504  1.137  pgoyette 	    device_unit(sc->sc_dev), (uintptr_t)dev, dev->ud_addr,
    505  1.137  pgoyette 	    dev->ud_speed);
    506    1.1  augustss 
    507  1.153  riastrad 	KASSERT(usb_in_event_thread(sc->sc_dev));
    508  1.149  riastrad 
    509    1.1  augustss 	if (!sc->sc_running)
    510  1.133     skrll 		return USBD_NOT_STARTED;
    511    1.1  augustss 
    512    1.1  augustss 	/* Ignore hubs that are too deep. */
    513  1.133     skrll 	if (dev->ud_depth > USB_HUB_MAX_DEPTH)
    514  1.133     skrll 		return USBD_TOO_DEEP;
    515    1.1  augustss 
    516  1.152  riastrad 	/* Process rescan if requested.  */
    517  1.152  riastrad 	mutex_enter(&sc->sc_lock);
    518  1.152  riastrad 	rescan = sc->sc_rescan;
    519  1.152  riastrad 	sc->sc_rescan = false;
    520  1.152  riastrad 	mutex_exit(&sc->sc_lock);
    521  1.152  riastrad 	if (rescan) {
    522  1.152  riastrad 		for (port = 1; port <= hd->bNbrPorts; port++) {
    523  1.152  riastrad 			subdev = dev->ud_hub->uh_ports[port - 1].up_dev;
    524  1.152  riastrad 			if (subdev == NULL)
    525  1.152  riastrad 				continue;
    526  1.152  riastrad 			usbd_reattach_device(sc->sc_dev, subdev, port, NULL);
    527  1.152  riastrad 		}
    528  1.152  riastrad 	}
    529  1.148  riastrad 
    530  1.101  drochner 	if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */
    531  1.101  drochner 		usb_hub_status_t hs;
    532  1.101  drochner 
    533  1.101  drochner 		err = usbd_get_hub_status(dev, &hs);
    534  1.101  drochner 		if (err) {
    535  1.137  pgoyette 			DPRINTF("uhub%jd get hub status failed, err %jd",
    536  1.128     skrll 			    device_unit(sc->sc_dev), err, 0, 0);
    537  1.101  drochner 		} else {
    538  1.101  drochner 			/* just acknowledge */
    539  1.101  drochner 			status = UGETW(hs.wHubStatus);
    540  1.101  drochner 			change = UGETW(hs.wHubChange);
    541  1.137  pgoyette 			DPRINTF("uhub%jd s/c=%jx/%jx", device_unit(sc->sc_dev),
    542  1.129     skrll 			    status, change, 0);
    543  1.129     skrll 
    544  1.101  drochner 			if (change & UHS_LOCAL_POWER)
    545  1.101  drochner 				usbd_clear_hub_feature(dev,
    546  1.101  drochner 						       UHF_C_HUB_LOCAL_POWER);
    547  1.101  drochner 			if (change & UHS_OVER_CURRENT)
    548  1.101  drochner 				usbd_clear_hub_feature(dev,
    549  1.101  drochner 						       UHF_C_HUB_OVER_CURRENT);
    550  1.101  drochner 		}
    551  1.101  drochner 	}
    552  1.101  drochner 
    553  1.154  jmcneill 	__BITMAP_ZERO(&powerup_port);
    554  1.154  jmcneill 
    555   1.84  drochner 	for (port = 1; port <= hd->bNbrPorts; port++) {
    556  1.133     skrll 		up = &dev->ud_hub->uh_ports[port - 1];
    557   1.87  drochner 
    558   1.87  drochner 		/* reattach is needed after firmware upload */
    559  1.133     skrll 		reconnect = up->up_reattach;
    560  1.133     skrll 		up->up_reattach = 0;
    561   1.87  drochner 
    562   1.87  drochner 		status = change = 0;
    563   1.87  drochner 
    564   1.87  drochner 		/* don't check if no change summary notification */
    565   1.87  drochner 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
    566  1.133     skrll 			err = usbd_get_port_status(dev, port, &up->up_status);
    567   1.87  drochner 			if (err) {
    568  1.137  pgoyette 				DPRINTF("uhub%jd get port stat failed, err %jd",
    569  1.128     skrll 				    device_unit(sc->sc_dev), err, 0, 0);
    570   1.87  drochner 				continue;
    571   1.87  drochner 			}
    572  1.133     skrll 			status = UGETW(up->up_status.wPortStatus);
    573  1.133     skrll 			change = UGETW(up->up_status.wPortChange);
    574  1.129     skrll 
    575  1.137  pgoyette 			DPRINTF("uhub%jd port %jd: s/c=%jx/%jx",
    576  1.129     skrll 			    device_unit(sc->sc_dev), port, status, change);
    577   1.87  drochner 		}
    578   1.87  drochner 		if (!change && !reconnect) {
    579   1.87  drochner 			/* No status change, just do recursive explore. */
    580  1.133     skrll 			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
    581  1.133     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    582    1.1  augustss 			continue;
    583    1.1  augustss 		}
    584   1.87  drochner 
    585   1.11  augustss 		if (change & UPS_C_PORT_ENABLED) {
    586  1.137  pgoyette 			DPRINTF("uhub%jd port %jd C_PORT_ENABLED",
    587  1.129     skrll 			    device_unit(sc->sc_dev), port, 0, 0);
    588   1.11  augustss 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    589   1.68   mycroft 			if (change & UPS_C_CONNECT_STATUS) {
    590   1.68   mycroft 				/* Ignore the port error if the device
    591   1.68   mycroft 				   vanished. */
    592   1.68   mycroft 			} else if (status & UPS_PORT_ENABLED) {
    593   1.98      cube 				aprint_error_dev(sc->sc_dev,
    594   1.98      cube 				    "illegal enable change, port %d\n", port);
    595   1.11  augustss 			} else {
    596   1.11  augustss 				/* Port error condition. */
    597  1.133     skrll 				if (up->up_restartcnt) /* no message first time */
    598   1.98      cube 					aprint_error_dev(sc->sc_dev,
    599   1.98      cube 					    "port error, restarting port %d\n",
    600   1.98      cube 					    port);
    601   1.47  augustss 
    602  1.133     skrll 				if (up->up_restartcnt++ < USBD_RESTART_MAX)
    603   1.11  augustss 					goto disco;
    604   1.47  augustss 				else
    605   1.98      cube 					aprint_error_dev(sc->sc_dev,
    606   1.98      cube 					    "port error, giving up port %d\n",
    607   1.98      cube 					    port);
    608   1.11  augustss 			}
    609   1.11  augustss 		}
    610  1.133     skrll 		if (change & UPS_C_PORT_RESET) {
    611  1.133     skrll 			/*
    612  1.133     skrll 			 * some xHCs set PortResetChange instead of CSC
    613  1.133     skrll 			 * when port is reset.
    614  1.133     skrll 			 */
    615  1.133     skrll 			if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
    616  1.133     skrll 				change |= UPS_C_CONNECT_STATUS;
    617  1.133     skrll 			}
    618  1.133     skrll 			usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    619  1.133     skrll 		}
    620  1.133     skrll 		if (change & UPS_C_BH_PORT_RESET) {
    621  1.133     skrll 			/*
    622  1.133     skrll 			 * some xHCs set WarmResetChange instead of CSC
    623  1.133     skrll 			 * when port is reset.
    624  1.133     skrll 			 */
    625  1.133     skrll 			if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
    626  1.133     skrll 				change |= UPS_C_CONNECT_STATUS;
    627  1.133     skrll 			}
    628  1.133     skrll 			usbd_clear_port_feature(dev, port,
    629  1.133     skrll 			    UHF_C_BH_PORT_RESET);
    630  1.133     skrll 		}
    631  1.133     skrll 		if (change & UPS_C_PORT_LINK_STATE)
    632  1.133     skrll 			usbd_clear_port_feature(dev, port,
    633  1.133     skrll 			    UHF_C_PORT_LINK_STATE);
    634  1.133     skrll 		if (change & UPS_C_PORT_CONFIG_ERROR)
    635  1.133     skrll 			usbd_clear_port_feature(dev, port,
    636  1.133     skrll 			    UHF_C_PORT_CONFIG_ERROR);
    637   1.87  drochner 
    638   1.87  drochner 		/* XXX handle overcurrent and resume events! */
    639   1.87  drochner 
    640  1.116  jakllsch 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
    641  1.116  jakllsch 			/* No status change, just do recursive explore. */
    642  1.133     skrll 			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
    643  1.133     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    644    1.1  augustss 			continue;
    645  1.116  jakllsch 		}
    646   1.42  augustss 
    647   1.42  augustss 		/* We have a connect status change, handle it. */
    648   1.42  augustss 
    649  1.137  pgoyette 		DPRINTF("uhub%jd status change port %jd",
    650  1.137  pgoyette 		    device_unit(sc->sc_dev), port, 0, 0);
    651    1.1  augustss 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    652    1.1  augustss 		/*
    653    1.1  augustss 		 * If there is already a device on the port the change status
    654    1.1  augustss 		 * must mean that is has disconnected.  Looking at the
    655    1.1  augustss 		 * current connect status is not enough to figure this out
    656    1.1  augustss 		 * since a new unit may have been connected before we handle
    657    1.1  augustss 		 * the disconnect.
    658    1.1  augustss 		 */
    659   1.11  augustss 	disco:
    660  1.133     skrll 		if (up->up_dev != NULL) {
    661    1.1  augustss 			/* Disconnected */
    662  1.137  pgoyette 			DPRINTF("uhub%jd device addr=%jd disappeared on "
    663  1.137  pgoyette 			    "port %jd",
    664  1.133     skrll 			    device_unit(sc->sc_dev), up->up_dev->ud_addr, port,
    665  1.129     skrll 			    0);
    666  1.133     skrll 
    667  1.108    dyoung 			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
    668   1.58  augustss 			usbd_clear_port_feature(dev, port,
    669    1.1  augustss 						UHF_C_PORT_CONNECTION);
    670    1.1  augustss 		}
    671   1.35  augustss 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    672   1.42  augustss 			/* Nothing connected, just ignore it. */
    673  1.137  pgoyette 			DPRINTFN(3, "uhub%jd port %jd !CURRENT_CONNECT_STATUS",
    674  1.129     skrll 			    device_unit(sc->sc_dev), port, 0, 0);
    675  1.133     skrll 			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
    676  1.133     skrll 			usbd_clear_port_feature(dev, port,
    677  1.133     skrll 						UHF_C_PORT_CONNECTION);
    678    1.1  augustss 			continue;
    679   1.35  augustss 		}
    680    1.1  augustss 
    681    1.1  augustss 		/* Connected */
    682  1.137  pgoyette 		DPRINTF("unit %jd dev->speed=%ju dev->depth=%ju",
    683  1.133     skrll 		    device_unit(sc->sc_dev), dev->ud_speed, dev->ud_depth, 0);
    684   1.42  augustss 
    685  1.154  jmcneill 		__BITMAP_SET(port, &powerup_port);
    686  1.154  jmcneill 		powerup_port_count++;
    687  1.154  jmcneill 	}
    688  1.154  jmcneill 
    689  1.154  jmcneill 	if (powerup_port_count == 0) {
    690  1.154  jmcneill 		goto explore;
    691  1.154  jmcneill 	}
    692  1.154  jmcneill 
    693  1.154  jmcneill 	aprint_debug_dev(sc->sc_dev, "power up %u port(s)\n",
    694  1.154  jmcneill 	    powerup_port_count);
    695  1.154  jmcneill 
    696  1.154  jmcneill 	/* Wait for maximum device power up time. */
    697  1.154  jmcneill 	usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    698  1.154  jmcneill 
    699  1.154  jmcneill 	for (port = 1; port <= hd->bNbrPorts; port++) {
    700  1.154  jmcneill 		if (!__BITMAP_ISSET(port, &powerup_port)) {
    701  1.154  jmcneill 			continue;
    702  1.154  jmcneill 		}
    703   1.11  augustss 
    704    1.1  augustss 		/* Reset port, which implies enabling it. */
    705  1.133     skrll 		if (usbd_reset_port(dev, port, &up->up_status)) {
    706   1.98      cube 			aprint_error_dev(sc->sc_dev,
    707   1.98      cube 			    "port %d reset failed\n", port);
    708   1.54  augustss 			continue;
    709   1.54  augustss 		}
    710  1.130     skrll #if 0
    711   1.54  augustss 		/* Get port status again, it might have changed during reset */
    712  1.133     skrll 		err = usbd_get_port_status(dev, port, &up->up_status);
    713   1.54  augustss 		if (err) {
    714  1.137  pgoyette 			DPRINTF("uhub%jd port %jd get port status failed, "
    715  1.137  pgoyette 			    "err %jd", device_unit(sc->sc_dev), port, err, 0);
    716   1.54  augustss 			continue;
    717   1.54  augustss 		}
    718  1.130     skrll #endif
    719  1.133     skrll 		/*
    720  1.133     skrll 		 * Use the port status from the reset to check for the device
    721  1.133     skrll 		 * disappearing, the port enable status, and the port speed
    722  1.133     skrll 		 */
    723  1.133     skrll 		status = UGETW(up->up_status.wPortStatus);
    724  1.133     skrll 		change = UGETW(up->up_status.wPortChange);
    725  1.137  pgoyette 		DPRINTF("uhub%jd port %jd after reset: s/c=%jx/%jx",
    726  1.133     skrll 		    device_unit(sc->sc_dev), port, status, change);
    727  1.133     skrll 
    728   1.54  augustss 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    729   1.54  augustss 			/* Nothing connected, just ignore it. */
    730   1.54  augustss #ifdef DIAGNOSTIC
    731   1.98      cube 			aprint_debug_dev(sc->sc_dev,
    732   1.98      cube 			    "port %d, device disappeared after reset\n", port);
    733   1.54  augustss #endif
    734    1.1  augustss 			continue;
    735   1.35  augustss 		}
    736  1.110  kiyohara 		if (!(status & UPS_PORT_ENABLED)) {
    737  1.110  kiyohara 			/* Not allowed send/receive packet. */
    738  1.110  kiyohara #ifdef DIAGNOSTIC
    739  1.115  sborrill 			printf("%s: port %d, device not enabled\n",
    740  1.112    dyoung 			       device_xname(sc->sc_dev), port);
    741  1.110  kiyohara #endif
    742  1.110  kiyohara 			continue;
    743  1.110  kiyohara 		}
    744  1.133     skrll 		/* port reset may cause Warm Reset Change, drop it. */
    745  1.133     skrll 		if (change & UPS_C_BH_PORT_RESET)
    746  1.133     skrll 			usbd_clear_port_feature(dev, port,
    747  1.133     skrll 			    UHF_C_BH_PORT_RESET);
    748  1.133     skrll 
    749  1.133     skrll 		/*
    750  1.133     skrll 		 * Figure out device speed from power bit of port status.
    751  1.133     skrll 		 *  USB 2.0 ch 11.24.2.7.1
    752  1.133     skrll 		 *  USB 3.1 ch 10.16.2.6.1
    753  1.133     skrll 		 */
    754  1.133     skrll 		int sts = status;
    755  1.133     skrll 		if ((sts & UPS_PORT_POWER) == 0)
    756  1.133     skrll 			sts &= ~UPS_PORT_POWER_SS;
    757    1.1  augustss 
    758  1.133     skrll 		if (sts & UPS_HIGH_SPEED)
    759   1.56  augustss 			speed = USB_SPEED_HIGH;
    760  1.133     skrll 		else if (sts & UPS_LOW_SPEED)
    761   1.56  augustss 			speed = USB_SPEED_LOW;
    762  1.133     skrll 		else {
    763  1.133     skrll 			/*
    764  1.133     skrll 			 * If there is no power bit set, it is certainly
    765  1.133     skrll 			 * a Super Speed device, so use the speed of its
    766  1.133     skrll 			 * parent hub.
    767  1.133     skrll 			 */
    768  1.133     skrll 			if (sts & UPS_PORT_POWER)
    769  1.133     skrll 				speed = USB_SPEED_FULL;
    770  1.133     skrll 			else
    771  1.133     skrll 				speed = dev->ud_speed;
    772  1.133     skrll 		}
    773  1.133     skrll 
    774  1.133     skrll 		/*
    775  1.133     skrll 		 * Reduce the speed, otherwise we won't setup the proper
    776  1.133     skrll 		 * transfer methods.
    777  1.133     skrll 		 */
    778  1.133     skrll 		if (speed > dev->ud_speed)
    779  1.133     skrll 			speed = dev->ud_speed;
    780  1.129     skrll 
    781  1.137  pgoyette 		DPRINTF("uhub%jd speed %ju", device_unit(sc->sc_dev), speed, 0,
    782  1.129     skrll 		    0);
    783  1.129     skrll 
    784  1.133     skrll 		/*
    785  1.133     skrll 		 * To check whether port has power,
    786  1.133     skrll 		 *  check UPS_PORT_POWER_SS bit if port speed is SS, and
    787  1.133     skrll 		 *  check UPS_PORT_POWER bit if port speed is HS/FS/LS.
    788  1.133     skrll 		 */
    789  1.133     skrll 		if (USB_IS_SS(speed)) {
    790  1.133     skrll 			/* SS hub port */
    791  1.133     skrll 			if (!(status & UPS_PORT_POWER_SS))
    792  1.133     skrll 				aprint_normal_dev(sc->sc_dev,
    793  1.133     skrll 				    "strange, connected port %d has no power\n",
    794  1.133     skrll 				    port);
    795  1.133     skrll 		} else {
    796  1.133     skrll 			/* HS/FS/LS hub port */
    797  1.133     skrll 			if (!(status & UPS_PORT_POWER))
    798  1.133     skrll 				aprint_normal_dev(sc->sc_dev,
    799  1.133     skrll 				    "strange, connected port %d has no power\n",
    800  1.133     skrll 				    port);
    801  1.133     skrll 		}
    802  1.133     skrll 
    803  1.147      maxv 		if (dev->ud_bus->ub_hctype == USBHCTYPE_VHCI) {
    804  1.147      maxv 			kcov_remote_enter(KCOV_REMOTE_VHCI,
    805  1.147      maxv 			    KCOV_REMOTE_VHCI_ID(dev->ud_bus->ub_busnum, port));
    806  1.147      maxv 		}
    807  1.145      maxv 
    808    1.1  augustss 		/* Get device info and set its address. */
    809  1.133     skrll 		err = usbd_new_device(sc->sc_dev, dev->ud_bus,
    810  1.133     skrll 			  dev->ud_depth + 1, speed, port, up);
    811  1.145      maxv 
    812  1.147      maxv 		if (dev->ud_bus->ub_hctype == USBHCTYPE_VHCI) {
    813  1.147      maxv 			kcov_remote_leave(KCOV_REMOTE_VHCI,
    814  1.147      maxv 			    KCOV_REMOTE_VHCI_ID(dev->ud_bus->ub_busnum, port));
    815  1.147      maxv 		}
    816  1.145      maxv 
    817    1.1  augustss 		/* XXX retry a few times? */
    818   1.33  augustss 		if (err) {
    819  1.143       mrg 			DPRINTF("uhub%jd: usbd_new_device failed, error %jd",
    820  1.143       mrg 			    device_unit(sc->sc_dev), err, 0, 0);
    821    1.1  augustss 			/* Avoid addressing problems by disabling. */
    822    1.1  augustss 			/* usbd_reset_port(dev, port, &up->status); */
    823   1.30  augustss 
    824   1.58  augustss 			/*
    825   1.30  augustss 			 * The unit refused to accept a new address, or had
    826   1.30  augustss 			 * some other serious problem.  Since we cannot leave
    827   1.30  augustss 			 * at 0 we have to disable the port instead.
    828   1.30  augustss 			 */
    829   1.98      cube 			aprint_error_dev(sc->sc_dev,
    830   1.98      cube 			    "device problem, disabling port %d\n", port);
    831   1.30  augustss 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    832    1.1  augustss 		} else {
    833   1.47  augustss 			/* The port set up succeeded, reset error count. */
    834  1.133     skrll 			up->up_restartcnt = 0;
    835   1.47  augustss 
    836  1.133     skrll 			if (up->up_dev->ud_hub)
    837  1.133     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    838    1.1  augustss 		}
    839    1.1  augustss 	}
    840  1.154  jmcneill 
    841  1.154  jmcneill explore:
    842  1.133     skrll 	mutex_enter(&sc->sc_lock);
    843  1.139       mrg 	sc->sc_explorepending = false;
    844  1.133     skrll 	for (int i = 0; i < sc->sc_statuslen; i++) {
    845  1.133     skrll 		if (sc->sc_statuspend[i] != 0) {
    846  1.133     skrll 			memcpy(sc->sc_status, sc->sc_statuspend,
    847  1.133     skrll 			    sc->sc_statuslen);
    848  1.133     skrll 			memset(sc->sc_statuspend, 0, sc->sc_statuslen);
    849  1.133     skrll 			usb_needs_explore(sc->sc_hub);
    850  1.133     skrll 			break;
    851  1.133     skrll 		}
    852  1.133     skrll 	}
    853  1.133     skrll 	mutex_exit(&sc->sc_lock);
    854  1.139       mrg 	if (sc->sc_first_explore) {
    855  1.139       mrg 		config_pending_decr(sc->sc_dev);
    856  1.139       mrg 		sc->sc_first_explore = false;
    857  1.139       mrg 	}
    858  1.133     skrll 
    859  1.133     skrll 	return USBD_NORMAL_COMPLETION;
    860    1.1  augustss }
    861    1.1  augustss 
    862   1.18  augustss /*
    863   1.18  augustss  * Called from process context when the hub is gone.
    864   1.18  augustss  * Detach all devices on active ports.
    865   1.18  augustss  */
    866  1.144      maxv static int
    867  1.105    dyoung uhub_detach(device_t self, int flags)
    868   1.18  augustss {
    869  1.107    dyoung 	struct uhub_softc *sc = device_private(self);
    870  1.133     skrll 	struct usbd_hub *hub = sc->sc_hub->ud_hub;
    871   1.18  augustss 	struct usbd_port *rup;
    872  1.108    dyoung 	int nports, port, rc;
    873   1.18  augustss 
    874  1.127     skrll 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    875  1.127     skrll 
    876  1.137  pgoyette 	DPRINTF("uhub%jd flags=%jd", device_unit(self), flags, 0, 0);
    877   1.18  augustss 
    878   1.39  augustss 	if (hub == NULL)		/* Must be partially working */
    879  1.133     skrll 		return 0;
    880   1.18  augustss 
    881  1.117       mrg 	/* XXXSMP usb */
    882  1.117       mrg 	KERNEL_LOCK(1, curlwp);
    883  1.117       mrg 
    884  1.133     skrll 	nports = hub->uh_hubdesc.bNbrPorts;
    885  1.140      manu 	for (port = 1; port <= nports; port++) {
    886  1.140      manu 		rup = &hub->uh_ports[port - 1];
    887  1.133     skrll 		if (rup->up_dev == NULL)
    888  1.108    dyoung 			continue;
    889  1.117       mrg 		if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
    890  1.117       mrg 			/* XXXSMP usb */
    891  1.117       mrg 			KERNEL_UNLOCK_ONE(curlwp);
    892  1.117       mrg 
    893  1.108    dyoung 			return rc;
    894  1.117       mrg 		}
    895   1.18  augustss 	}
    896   1.58  augustss 
    897  1.108    dyoung 	pmf_device_deregister(self);
    898  1.108    dyoung 	usbd_abort_pipe(sc->sc_ipipe);
    899  1.108    dyoung 	usbd_close_pipe(sc->sc_ipipe);
    900  1.108    dyoung 
    901  1.105    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
    902   1.37  augustss 
    903  1.133     skrll 	if (hub->uh_ports[0].up_tt)
    904  1.133     skrll 		kmem_free(hub->uh_ports[0].up_tt,
    905  1.133     skrll 		    (UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    906  1.133     skrll 		    sizeof(struct usbd_tt));
    907  1.133     skrll 	kmem_free(hub,
    908  1.133     skrll 	    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
    909  1.133     skrll 	sc->sc_hub->ud_hub = NULL;
    910   1.84  drochner 	if (sc->sc_status)
    911  1.133     skrll 		kmem_free(sc->sc_status, sc->sc_statuslen);
    912  1.133     skrll 	if (sc->sc_statuspend)
    913  1.133     skrll 		kmem_free(sc->sc_statuspend, sc->sc_statuslen);
    914  1.101  drochner 	if (sc->sc_statusbuf)
    915  1.133     skrll 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    916  1.133     skrll 
    917  1.148  riastrad 	cv_destroy(&sc->sc_cv);
    918  1.133     skrll 	mutex_destroy(&sc->sc_lock);
    919   1.18  augustss 
    920  1.117       mrg 	/* XXXSMP usb */
    921  1.117       mrg 	KERNEL_UNLOCK_ONE(curlwp);
    922  1.117       mrg 
    923  1.133     skrll 	return 0;
    924   1.18  augustss }
    925   1.34  augustss 
    926  1.144      maxv static int
    927  1.103      kent uhub_rescan(device_t self, const char *ifattr, const int *locators)
    928  1.103      kent {
    929  1.103      kent 	struct uhub_softc *sc = device_private(self);
    930  1.103      kent 
    931  1.150  riastrad 	UHUBHIST_FUNC();
    932  1.150  riastrad 	UHUBHIST_CALLARGS("uhub%jd", device_unit(sc->sc_dev), 0, 0, 0);
    933  1.150  riastrad 
    934  1.149  riastrad 	KASSERT(KERNEL_LOCKED_P());
    935  1.149  riastrad 
    936  1.152  riastrad 	/* Trigger bus exploration.  */
    937  1.152  riastrad 	/* XXX locators */
    938  1.152  riastrad 	mutex_enter(&sc->sc_lock);
    939  1.152  riastrad 	sc->sc_rescan = true;
    940  1.152  riastrad 	mutex_exit(&sc->sc_lock);
    941  1.151  riastrad 	usb_needs_explore(sc->sc_hub);
    942  1.151  riastrad 
    943  1.103      kent 	return 0;
    944  1.103      kent }
    945  1.103      kent 
    946   1.34  augustss /* Called when a device has been detached from it */
    947  1.144      maxv static void
    948   1.95    dyoung uhub_childdet(device_t self, device_t child)
    949   1.34  augustss {
    950   1.95    dyoung 	struct uhub_softc *sc = device_private(self);
    951  1.133     skrll 	struct usbd_device *devhub = sc->sc_hub;
    952  1.133     skrll 	struct usbd_device *dev;
    953   1.95    dyoung 	int nports;
    954   1.95    dyoung 	int port;
    955   1.95    dyoung 	int i;
    956   1.95    dyoung 
    957  1.149  riastrad 	KASSERT(KERNEL_LOCKED_P());
    958  1.149  riastrad 
    959  1.133     skrll 	if (!devhub->ud_hub)
    960   1.95    dyoung 		/* should never happen; children are only created after init */
    961   1.95    dyoung 		panic("hub not fully initialised, but child deleted?");
    962   1.95    dyoung 
    963  1.133     skrll 	nports = devhub->ud_hub->uh_hubdesc.bNbrPorts;
    964  1.140      manu 	for (port = 1; port <= nports; port++) {
    965  1.140      manu 		dev = devhub->ud_hub->uh_ports[port - 1].up_dev;
    966  1.133     skrll 		if (!dev || dev->ud_subdevlen == 0)
    967   1.95    dyoung 			continue;
    968  1.133     skrll 		for (i = 0; i < dev->ud_subdevlen; i++) {
    969  1.133     skrll 			if (dev->ud_subdevs[i] == child) {
    970  1.133     skrll 				dev->ud_subdevs[i] = NULL;
    971  1.133     skrll 				dev->ud_nifaces_claimed--;
    972   1.95    dyoung 			}
    973   1.95    dyoung 		}
    974  1.133     skrll 		if (dev->ud_nifaces_claimed == 0) {
    975  1.133     skrll 			kmem_free(dev->ud_subdevs,
    976  1.133     skrll 			    dev->ud_subdevlen * sizeof(device_t));
    977  1.133     skrll 			dev->ud_subdevs = NULL;
    978  1.133     skrll 			dev->ud_subdevlen = 0;
    979  1.113  jmcneill 		}
    980   1.95    dyoung 	}
    981   1.34  augustss }
    982   1.34  augustss 
    983   1.18  augustss 
    984   1.18  augustss /*
    985   1.18  augustss  * Hub interrupt.
    986   1.18  augustss  * This an indication that some port has changed status.
    987   1.18  augustss  * Notify the bus event handler thread that we need
    988   1.18  augustss  * to be explored again.
    989   1.18  augustss  */
    990    1.1  augustss void
    991  1.133     skrll uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
    992    1.1  augustss {
    993    1.1  augustss 	struct uhub_softc *sc = addr;
    994    1.1  augustss 
    995  1.143       mrg 	UHUBHIST_FUNC(); UHUBHIST_CALLARGS("called! uhub%jd status=%jx",
    996  1.143       mrg 	    device_unit(sc->sc_dev), status, 0, 0);
    997   1.87  drochner 
    998   1.50  augustss 	if (status == USBD_STALLED)
    999    1.9  augustss 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
   1000  1.133     skrll 	else if (status == USBD_NORMAL_COMPLETION) {
   1001  1.133     skrll 
   1002  1.133     skrll 		mutex_enter(&sc->sc_lock);
   1003  1.133     skrll 
   1004  1.137  pgoyette 		DPRINTFN(5, "uhub%jd: explore pending %jd",
   1005  1.133     skrll 		    device_unit(sc->sc_dev), sc->sc_explorepending, 0, 0);
   1006  1.133     skrll 
   1007  1.133     skrll 		/* merge port bitmap into pending interrupts list */
   1008  1.133     skrll 		for (size_t i = 0; i < sc->sc_statuslen; i++) {
   1009  1.133     skrll 			sc->sc_statuspend[i] |= sc->sc_statusbuf[i];
   1010  1.133     skrll 
   1011  1.137  pgoyette 			DPRINTFN(5, "uhub%jd: pending/new ports "
   1012  1.137  pgoyette 			    "[%jd] %#jx/%#jx", device_unit(sc->sc_dev),
   1013  1.133     skrll 			    i, sc->sc_statuspend[i], sc->sc_statusbuf[i]);
   1014  1.133     skrll 		}
   1015  1.133     skrll 
   1016  1.133     skrll 		if (!sc->sc_explorepending) {
   1017  1.139       mrg 			sc->sc_explorepending = true;
   1018  1.133     skrll 
   1019  1.133     skrll 			memcpy(sc->sc_status, sc->sc_statuspend,
   1020  1.133     skrll 			    sc->sc_statuslen);
   1021  1.133     skrll 			memset(sc->sc_statuspend, 0, sc->sc_statuslen);
   1022  1.133     skrll 
   1023  1.133     skrll 			for (size_t i = 0; i < sc->sc_statuslen; i++) {
   1024  1.137  pgoyette 				DPRINTFN(5, "uhub%jd: exploring ports "
   1025  1.137  pgoyette 				    "[%jd] %#jx", device_unit(sc->sc_dev),
   1026  1.133     skrll 				    i, sc->sc_status[i], 0);
   1027  1.133     skrll 			}
   1028  1.133     skrll 
   1029  1.133     skrll 			usb_needs_explore(sc->sc_hub);
   1030  1.133     skrll 		}
   1031  1.133     skrll 		mutex_exit(&sc->sc_lock);
   1032   1.87  drochner 	}
   1033    1.1  augustss }
   1034