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