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