Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.126.2.4
      1  1.126.2.4     skrll /*	$NetBSD: uhub.c,v 1.126.2.4 2014/12/05 09:37: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.1  augustss 
      4        1.1  augustss /*
      5       1.74   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      6        1.1  augustss  * All rights reserved.
      7        1.1  augustss  *
      8        1.6  augustss  * This code is derived from software contributed to The NetBSD Foundation
      9       1.44  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10        1.6  augustss  * Carlstedt Research & Technology.
     11        1.1  augustss  *
     12        1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13        1.1  augustss  * modification, are permitted provided that the following conditions
     14        1.1  augustss  * are met:
     15        1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16        1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17        1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18        1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19        1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20        1.1  augustss  *
     21        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22        1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23        1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24        1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25        1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26        1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27        1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28        1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29        1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30        1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31        1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     32       1.15  augustss  */
     33       1.15  augustss 
     34       1.15  augustss /*
     35       1.64    ichiro  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
     36        1.1  augustss  */
     37       1.53     lukem 
     38       1.53     lukem #include <sys/cdefs.h>
     39  1.126.2.4     skrll __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.126.2.4 2014/12/05 09:37:49 skrll Exp $");
     40        1.1  augustss 
     41        1.1  augustss #include <sys/param.h>
     42        1.1  augustss #include <sys/systm.h>
     43        1.1  augustss #include <sys/kernel.h>
     44  1.126.2.3     skrll #include <sys/kmem.h>
     45        1.1  augustss #include <sys/device.h>
     46       1.34  augustss #include <sys/proc.h>
     47       1.28  augustss 
     48       1.90        ad #include <sys/bus.h>
     49        1.1  augustss 
     50        1.1  augustss #include <dev/usb/usb.h>
     51        1.1  augustss #include <dev/usb/usbdi.h>
     52        1.1  augustss #include <dev/usb/usbdi_util.h>
     53        1.1  augustss #include <dev/usb/usbdivar.h>
     54        1.1  augustss 
     55       1.32  augustss #ifdef UHUB_DEBUG
     56      1.111    dyoung #define DPRINTF(x)	if (uhubdebug) printf x
     57      1.111    dyoung #define DPRINTFN(n,x)	if (uhubdebug>(n)) printf x
     58       1.63   dsainty int	uhubdebug = 0;
     59        1.1  augustss #else
     60        1.1  augustss #define DPRINTF(x)
     61        1.1  augustss #define DPRINTFN(n,x)
     62        1.1  augustss #endif
     63        1.1  augustss 
     64        1.1  augustss struct uhub_softc {
     65      1.111    dyoung 	device_t		sc_dev;		/* base device */
     66       1.11  augustss 	usbd_device_handle	sc_hub;		/* USB device */
     67       1.86  drochner 	int			sc_proto;	/* device protocol */
     68       1.11  augustss 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
     69       1.87  drochner 
     70       1.87  drochner 	/* XXX second buffer needed because we can't suspend pipes yet */
     71  1.126.2.1     skrll 	uint8_t			*sc_statusbuf;
     72  1.126.2.1     skrll 	uint8_t			*sc_status;
     73       1.87  drochner 	size_t			sc_statuslen;
     74       1.87  drochner 	int			sc_explorepending;
     75      1.101  drochner 	int		sc_isehciroothub; /* see comment in uhub_intr() */
     76       1.87  drochner 
     77       1.11  augustss 	u_char			sc_running;
     78        1.1  augustss };
     79       1.87  drochner 
     80       1.86  drochner #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB)
     81       1.86  drochner #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
     82        1.1  augustss 
     83       1.87  drochner #define PORTSTAT_ISSET(sc, port) \
     84       1.87  drochner 	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
     85       1.87  drochner 
     86       1.45  augustss Static usbd_status uhub_explore(usbd_device_handle hub);
     87       1.45  augustss Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
     88        1.1  augustss 
     89       1.32  augustss 
     90       1.58  augustss /*
     91       1.32  augustss  * We need two attachment points:
     92       1.32  augustss  * hub to usb and hub to hub
     93       1.32  augustss  * Every other driver only connects to hubs
     94       1.32  augustss  */
     95        1.1  augustss 
     96       1.98      cube int uhub_match(device_t, cfdata_t, void *);
     97       1.95    dyoung void uhub_attach(device_t, device_t, void *);
     98      1.103      kent int uhub_rescan(device_t, const char *, const int *);
     99       1.95    dyoung void uhub_childdet(device_t, device_t);
    100       1.95    dyoung int uhub_detach(device_t, int);
    101       1.95    dyoung extern struct cfdriver uhub_cd;
    102      1.104    dyoung CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
    103      1.108    dyoung     uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
    104      1.104    dyoung     DVF_DETACH_SHUTDOWN);
    105       1.99  drochner CFATTACH_DECL2_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
    106      1.108    dyoung     uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet);
    107        1.1  augustss 
    108      1.109     pooka /*
    109      1.109     pooka  * Setting this to 1 makes sure than an uhub attaches even at higher
    110      1.109     pooka  * priority than ugen when ugen_override is set to 1.  This allows to
    111      1.109     pooka  * probe the whole USB bus and attach functions with ugen.
    112      1.109     pooka  */
    113      1.109     pooka int uhub_ubermatch = 0;
    114      1.109     pooka 
    115      1.105    dyoung int
    116      1.105    dyoung uhub_match(device_t parent, cfdata_t match, void *aux)
    117        1.1  augustss {
    118      1.107    dyoung 	struct usb_attach_arg *uaa = aux;
    119      1.109     pooka 	int matchvalue;
    120      1.109     pooka 
    121      1.109     pooka 	if (uhub_ubermatch)
    122      1.109     pooka 		matchvalue = UMATCH_HIGHEST+1;
    123      1.109     pooka 	else
    124      1.109     pooka 		matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS;
    125       1.58  augustss 
    126       1.88  drochner 	DPRINTFN(5,("uhub_match, uaa=%p\n", uaa));
    127       1.58  augustss 	/*
    128        1.1  augustss 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    129        1.1  augustss 	 * so we just ignore the subclass.
    130        1.1  augustss 	 */
    131       1.86  drochner 	if (uaa->class == UDCLASS_HUB)
    132  1.126.2.4     skrll 		return matchvalue;
    133  1.126.2.4     skrll 	return UMATCH_NONE;
    134        1.1  augustss }
    135        1.1  augustss 
    136      1.105    dyoung void
    137      1.105    dyoung uhub_attach(device_t parent, device_t self, void *aux)
    138        1.1  augustss {
    139      1.107    dyoung 	struct uhub_softc *sc = device_private(self);
    140      1.107    dyoung 	struct usb_attach_arg *uaa = aux;
    141        1.1  augustss 	usbd_device_handle dev = uaa->device;
    142       1.76  augustss 	char *devinfop;
    143       1.33  augustss 	usbd_status err;
    144       1.70  augustss 	struct usbd_hub *hub = NULL;
    145        1.1  augustss 	usb_device_request_t req;
    146        1.1  augustss 	usb_hub_descriptor_t hubdesc;
    147       1.42  augustss 	int p, port, nports, nremov, pwrdly;
    148        1.1  augustss 	usbd_interface_handle iface;
    149        1.1  augustss 	usb_endpoint_descriptor_t *ed;
    150       1.83  drochner #if 0 /* notyet */
    151       1.70  augustss 	struct usbd_tt *tts = NULL;
    152       1.83  drochner #endif
    153       1.58  augustss 
    154        1.1  augustss 	DPRINTFN(1,("uhub_attach\n"));
    155       1.98      cube 	sc->sc_dev = self;
    156        1.1  augustss 	sc->sc_hub = dev;
    157       1.86  drochner 	sc->sc_proto = uaa->proto;
    158       1.76  augustss 
    159       1.76  augustss 	devinfop = usbd_devinfo_alloc(dev, 1);
    160       1.96  jmcneill 	aprint_naive("\n");
    161       1.96  jmcneill 	aprint_normal(": %s\n", devinfop);
    162       1.76  augustss 	usbd_devinfo_free(devinfop);
    163        1.1  augustss 
    164  1.126.2.2     skrll 	if (dev->ud_depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    165       1.98      cube 		aprint_normal_dev(self, "%s transaction translator%s\n",
    166       1.70  augustss 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    167       1.70  augustss 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    168       1.69  augustss 	}
    169       1.69  augustss 
    170       1.33  augustss 	err = usbd_set_config_index(dev, 0, 1);
    171       1.33  augustss 	if (err) {
    172       1.21  augustss 		DPRINTF(("%s: configuration failed, error=%s\n",
    173      1.105    dyoung 		    device_xname(sc->sc_dev), usbd_errstr(err)));
    174      1.107    dyoung 		return;
    175        1.1  augustss 	}
    176        1.1  augustss 
    177  1.126.2.2     skrll 	if (dev->ud_depth > USB_HUB_MAX_DEPTH) {
    178       1.98      cube 		aprint_error_dev(self,
    179       1.98      cube 		    "hub depth (%d) exceeded, hub ignored\n",
    180       1.98      cube 		    USB_HUB_MAX_DEPTH);
    181      1.107    dyoung 		return;
    182        1.1  augustss 	}
    183        1.1  augustss 
    184        1.1  augustss 	/* Get hub descriptor. */
    185        1.1  augustss 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    186        1.1  augustss 	req.bRequest = UR_GET_DESCRIPTOR;
    187       1.65    toshii 	USETW2(req.wValue, UDESC_HUB, 0);
    188        1.1  augustss 	USETW(req.wIndex, 0);
    189        1.1  augustss 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    190      1.119     skrll 	DPRINTFN(1,("%s: getting hub descriptor\n", __func__));
    191       1.33  augustss 	err = usbd_do_request(dev, &req, &hubdesc);
    192       1.11  augustss 	nports = hubdesc.bNbrPorts;
    193       1.33  augustss 	if (!err && nports > 7) {
    194       1.11  augustss 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    195       1.33  augustss 		err = usbd_do_request(dev, &req, &hubdesc);
    196       1.11  augustss 	}
    197       1.33  augustss 	if (err) {
    198       1.21  augustss 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
    199      1.105    dyoung 		    device_xname(sc->sc_dev), usbd_errstr(err)));
    200      1.107    dyoung 		return;
    201        1.1  augustss 	}
    202        1.1  augustss 
    203       1.11  augustss 	for (nremov = 0, port = 1; port <= nports; port++)
    204       1.11  augustss 		if (!UHD_NOT_REMOV(&hubdesc, port))
    205       1.11  augustss 			nremov++;
    206       1.98      cube 	aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
    207       1.98      cube 	    nports, nports != 1 ? "s" : "", nremov,
    208  1.126.2.2     skrll 	    dev->ud_selfpowered ? "self" : "bus");
    209       1.11  augustss 
    210       1.70  augustss 	if (nports == 0) {
    211       1.98      cube 		aprint_debug_dev(self, "no ports, hub ignored\n");
    212       1.70  augustss 		goto bad;
    213       1.70  augustss 	}
    214       1.70  augustss 
    215  1.126.2.3     skrll 	hub = kmem_alloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    216  1.126.2.3     skrll 	    KM_SLEEP);
    217       1.33  augustss 	if (hub == NULL)
    218      1.107    dyoung 		return;
    219  1.126.2.2     skrll 	dev->ud_hub = hub;
    220  1.126.2.2     skrll 	dev->ud_hub->uh_hubsoftc = sc;
    221  1.126.2.2     skrll 	hub->uh_explore = uhub_explore;
    222  1.126.2.2     skrll 	hub->uh_hubdesc = hubdesc;
    223       1.58  augustss 
    224        1.1  augustss 	/* Set up interrupt pipe. */
    225       1.33  augustss 	err = usbd_device2interface_handle(dev, 0, &iface);
    226       1.33  augustss 	if (err) {
    227       1.98      cube 		aprint_error_dev(self, "no interface handle\n");
    228       1.18  augustss 		goto bad;
    229        1.1  augustss 	}
    230       1.83  drochner 
    231       1.83  drochner 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
    232       1.83  drochner 		err = usbd_set_interface(iface, 1);
    233       1.83  drochner 		if (err)
    234       1.98      cube 			aprint_error_dev(self, "can't enable multiple TTs\n");
    235       1.83  drochner 	}
    236       1.83  drochner 
    237        1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    238       1.33  augustss 	if (ed == NULL) {
    239       1.98      cube 		aprint_error_dev(self, "no endpoint descriptor\n");
    240       1.18  augustss 		goto bad;
    241        1.1  augustss 	}
    242        1.1  augustss 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    243       1.98      cube 		aprint_error_dev(self, "bad interrupt endpoint\n");
    244       1.18  augustss 		goto bad;
    245        1.1  augustss 	}
    246        1.1  augustss 
    247       1.87  drochner 	sc->sc_statuslen = (nports + 1 + 7) / 8;
    248  1.126.2.3     skrll 	sc->sc_statusbuf = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
    249       1.87  drochner 	if (!sc->sc_statusbuf)
    250       1.87  drochner 		goto bad;
    251  1.126.2.3     skrll 	sc->sc_status = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
    252       1.84  drochner 	if (!sc->sc_status)
    253       1.84  drochner 		goto bad;
    254      1.101  drochner 	if (device_is_a(device_parent(device_parent(sc->sc_dev)), "ehci"))
    255      1.101  drochner 		sc->sc_isehciroothub = 1;
    256       1.84  drochner 
    257       1.87  drochner 	/* force initial scan */
    258       1.87  drochner 	memset(sc->sc_status, 0xff, sc->sc_statuslen);
    259       1.87  drochner 	sc->sc_explorepending = 1;
    260       1.87  drochner 
    261       1.33  augustss 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    262      1.122  jmcneill 		  USBD_SHORT_XFER_OK|USBD_MPSAFE, &sc->sc_ipipe, sc,
    263      1.122  jmcneill 		  sc->sc_statusbuf, sc->sc_statuslen,
    264      1.122  jmcneill 		  uhub_intr, USBD_DEFAULT_INTERVAL);
    265       1.33  augustss 	if (err) {
    266       1.98      cube 		aprint_error_dev(self, "cannot open interrupt pipe\n");
    267       1.18  augustss 		goto bad;
    268        1.1  augustss 	}
    269        1.1  augustss 
    270       1.11  augustss 	/* Wait with power off for a while. */
    271       1.13  augustss 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    272       1.11  augustss 
    273      1.105    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
    274       1.37  augustss 
    275       1.42  augustss 	/*
    276       1.42  augustss 	 * To have the best chance of success we do things in the exact same
    277      1.123  jakllsch 	 * order as Windows 98.  This should not be necessary, but some
    278       1.42  augustss 	 * devices do not follow the USB specs to the letter.
    279       1.42  augustss 	 *
    280       1.42  augustss 	 * These are the events on the bus when a hub is attached:
    281       1.42  augustss 	 *  Get device and config descriptors (see attach code)
    282       1.42  augustss 	 *  Get hub descriptor (see above)
    283       1.42  augustss 	 *  For all ports
    284       1.42  augustss 	 *     turn on power
    285       1.42  augustss 	 *     wait for power to become stable
    286       1.42  augustss 	 * (all below happens in explore code)
    287       1.42  augustss 	 *  For all ports
    288       1.42  augustss 	 *     clear C_PORT_CONNECTION
    289       1.42  augustss 	 *  For all ports
    290       1.42  augustss 	 *     get port status
    291       1.42  augustss 	 *     if device connected
    292       1.57  augustss 	 *        wait 100 ms
    293       1.42  augustss 	 *        turn on reset
    294       1.42  augustss 	 *        wait
    295       1.42  augustss 	 *        clear C_PORT_RESET
    296       1.42  augustss 	 *        get port status
    297       1.42  augustss 	 *        proceed with device attachment
    298       1.42  augustss 	 */
    299       1.42  augustss 
    300       1.83  drochner #if 0
    301       1.78  christos 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
    302  1.126.2.3     skrll 		tts = kmem_alloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    303  1.126.2.3     skrll 			     sizeof (struct usbd_tt), KM_SLEEP);
    304       1.70  augustss 		if (!tts)
    305       1.70  augustss 			goto bad;
    306       1.70  augustss 	}
    307       1.83  drochner #endif
    308       1.42  augustss 	/* Set up data structures */
    309       1.11  augustss 	for (p = 0; p < nports; p++) {
    310  1.126.2.2     skrll 		struct usbd_port *up = &hub->uh_ports[p];
    311  1.126.2.2     skrll 		up->up_dev = NULL;
    312  1.126.2.2     skrll 		up->up_parent = dev;
    313  1.126.2.2     skrll 		up->up_portno = p+1;
    314  1.126.2.2     skrll 		if (dev->ud_selfpowered)
    315       1.42  augustss 			/* Self powered hub, give ports maximum current. */
    316  1.126.2.2     skrll 			up->up_power = USB_MAX_POWER;
    317       1.42  augustss 		else
    318  1.126.2.2     skrll 			up->up_power = USB_MIN_POWER;
    319  1.126.2.2     skrll 		up->up_restartcnt = 0;
    320  1.126.2.2     skrll 		up->up_reattach = 0;
    321       1.83  drochner #if 0
    322       1.70  augustss 		if (UHUB_IS_HIGH_SPEED(sc)) {
    323       1.70  augustss 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
    324       1.70  augustss 			up->tt->hub = hub;
    325       1.70  augustss 		} else {
    326       1.70  augustss 			up->tt = NULL;
    327       1.70  augustss 		}
    328       1.83  drochner #endif
    329       1.42  augustss 	}
    330       1.42  augustss 
    331       1.42  augustss 	/* XXX should check for none, individual, or ganged power? */
    332       1.42  augustss 
    333  1.126.2.2     skrll 	pwrdly = dev->ud_hub->uh_hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    334       1.42  augustss 	    + USB_EXTRA_POWER_UP_TIME;
    335       1.42  augustss 	for (port = 1; port <= nports; port++) {
    336       1.42  augustss 		/* Turn the power on. */
    337       1.42  augustss 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    338       1.33  augustss 		if (err)
    339       1.98      cube 			aprint_error_dev(self, "port %d power on failed, %s\n",
    340       1.98      cube 			    port, usbd_errstr(err));
    341       1.42  augustss 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
    342       1.94  jmcneill 	}
    343       1.94  jmcneill 
    344       1.94  jmcneill 	/* Wait for stable power if we are not a root hub */
    345  1.126.2.2     skrll 	if (dev->ud_powersrc->up_parent != NULL)
    346       1.42  augustss 		usbd_delay_ms(dev, pwrdly);
    347       1.42  augustss 
    348       1.42  augustss 	/* The usual exploration will finish the setup. */
    349       1.42  augustss 
    350        1.1  augustss 	sc->sc_running = 1;
    351       1.11  augustss 
    352       1.92  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    353       1.92  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    354       1.92  jmcneill 
    355      1.107    dyoung 	return;
    356       1.11  augustss 
    357       1.18  augustss  bad:
    358       1.84  drochner 	if (sc->sc_status)
    359  1.126.2.3     skrll 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    360      1.101  drochner 	if (sc->sc_statusbuf)
    361  1.126.2.3     skrll 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    362       1.70  augustss 	if (hub)
    363  1.126.2.3     skrll 		kmem_free(hub,
    364  1.126.2.3     skrll 		    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
    365  1.126.2.2     skrll 	dev->ud_hub = NULL;
    366      1.107    dyoung 	return;
    367       1.11  augustss }
    368       1.11  augustss 
    369        1.1  augustss usbd_status
    370       1.45  augustss uhub_explore(usbd_device_handle dev)
    371        1.1  augustss {
    372  1.126.2.2     skrll 	usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
    373  1.126.2.2     skrll 	struct uhub_softc *sc = dev->ud_hub->uh_hubsoftc;
    374        1.1  augustss 	struct usbd_port *up;
    375       1.33  augustss 	usbd_status err;
    376       1.56  augustss 	int speed;
    377        1.1  augustss 	int port;
    378       1.72      joff 	int change, status, reconnect;
    379        1.1  augustss 
    380  1.126.2.2     skrll 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->ud_addr));
    381        1.1  augustss 
    382        1.1  augustss 	if (!sc->sc_running)
    383  1.126.2.4     skrll 		return USBD_NOT_STARTED;
    384        1.1  augustss 
    385        1.1  augustss 	/* Ignore hubs that are too deep. */
    386  1.126.2.2     skrll 	if (dev->ud_depth > USB_HUB_MAX_DEPTH)
    387  1.126.2.4     skrll 		return USBD_TOO_DEEP;
    388        1.1  augustss 
    389      1.101  drochner 	if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */
    390      1.101  drochner 		usb_hub_status_t hs;
    391      1.101  drochner 
    392      1.101  drochner 		err = usbd_get_hub_status(dev, &hs);
    393      1.101  drochner 		if (err) {
    394      1.101  drochner 			DPRINTF(("%s: get hub status failed, err=%d\n",
    395      1.101  drochner 				 device_xname(sc->sc_dev), err));
    396      1.101  drochner 		} else {
    397      1.101  drochner 			/* just acknowledge */
    398      1.101  drochner 			status = UGETW(hs.wHubStatus);
    399      1.101  drochner 			change = UGETW(hs.wHubChange);
    400      1.101  drochner 			if (change & UHS_LOCAL_POWER)
    401      1.101  drochner 				usbd_clear_hub_feature(dev,
    402      1.101  drochner 						       UHF_C_HUB_LOCAL_POWER);
    403      1.101  drochner 			if (change & UHS_OVER_CURRENT)
    404      1.101  drochner 				usbd_clear_hub_feature(dev,
    405      1.101  drochner 						       UHF_C_HUB_OVER_CURRENT);
    406      1.101  drochner 		}
    407      1.101  drochner 	}
    408      1.101  drochner 
    409       1.84  drochner 	for (port = 1; port <= hd->bNbrPorts; port++) {
    410  1.126.2.2     skrll 		up = &dev->ud_hub->uh_ports[port-1];
    411       1.87  drochner 
    412       1.87  drochner 		/* reattach is needed after firmware upload */
    413  1.126.2.2     skrll 		reconnect = up->up_reattach;
    414  1.126.2.2     skrll 		up->up_reattach = 0;
    415       1.87  drochner 
    416       1.87  drochner 		status = change = 0;
    417       1.87  drochner 
    418       1.87  drochner 		/* don't check if no change summary notification */
    419       1.87  drochner 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
    420  1.126.2.2     skrll 			err = usbd_get_port_status(dev, port, &up->up_status);
    421       1.87  drochner 			if (err) {
    422       1.87  drochner 				DPRINTF(("uhub_explore: get port stat failed, "
    423       1.87  drochner 					 "error=%s\n", usbd_errstr(err)));
    424       1.87  drochner 				continue;
    425       1.87  drochner 			}
    426  1.126.2.2     skrll 			status = UGETW(up->up_status.wPortStatus);
    427  1.126.2.2     skrll 			change = UGETW(up->up_status.wPortChange);
    428      1.101  drochner #if 0
    429      1.101  drochner 			printf("%s port %d: s/c=%x/%x\n",
    430      1.101  drochner 			       device_xname(sc->sc_dev), port, status, change);
    431      1.101  drochner #endif
    432       1.87  drochner 		}
    433       1.87  drochner 		if (!change && !reconnect) {
    434       1.87  drochner 			/* No status change, just do recursive explore. */
    435  1.126.2.2     skrll 			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
    436  1.126.2.2     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    437        1.1  augustss 			continue;
    438        1.1  augustss 		}
    439       1.87  drochner 
    440       1.11  augustss 		if (change & UPS_C_PORT_ENABLED) {
    441       1.35  augustss 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
    442       1.11  augustss 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    443       1.68   mycroft 			if (change & UPS_C_CONNECT_STATUS) {
    444       1.68   mycroft 				/* Ignore the port error if the device
    445       1.68   mycroft 				   vanished. */
    446       1.68   mycroft 			} else if (status & UPS_PORT_ENABLED) {
    447       1.98      cube 				aprint_error_dev(sc->sc_dev,
    448       1.98      cube 				    "illegal enable change, port %d\n", port);
    449       1.11  augustss 			} else {
    450       1.11  augustss 				/* Port error condition. */
    451  1.126.2.2     skrll 				if (up->up_restartcnt) /* no message first time */
    452       1.98      cube 					aprint_error_dev(sc->sc_dev,
    453       1.98      cube 					    "port error, restarting port %d\n",
    454       1.98      cube 					    port);
    455       1.47  augustss 
    456  1.126.2.2     skrll 				if (up->up_restartcnt++ < USBD_RESTART_MAX)
    457       1.11  augustss 					goto disco;
    458       1.47  augustss 				else
    459       1.98      cube 					aprint_error_dev(sc->sc_dev,
    460       1.98      cube 					    "port error, giving up port %d\n",
    461       1.98      cube 					    port);
    462       1.11  augustss 			}
    463       1.11  augustss 		}
    464       1.87  drochner 
    465       1.87  drochner 		/* XXX handle overcurrent and resume events! */
    466       1.87  drochner 
    467      1.116  jakllsch 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
    468      1.116  jakllsch 			/* No status change, just do recursive explore. */
    469  1.126.2.2     skrll 			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
    470  1.126.2.2     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    471        1.1  augustss 			continue;
    472      1.116  jakllsch 		}
    473       1.42  augustss 
    474       1.42  augustss 		/* We have a connect status change, handle it. */
    475       1.42  augustss 
    476        1.1  augustss 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    477  1.126.2.2     skrll 			 dev->ud_addr, port));
    478        1.1  augustss 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    479        1.1  augustss 		/*
    480        1.1  augustss 		 * If there is already a device on the port the change status
    481        1.1  augustss 		 * must mean that is has disconnected.  Looking at the
    482        1.1  augustss 		 * current connect status is not enough to figure this out
    483        1.1  augustss 		 * since a new unit may have been connected before we handle
    484        1.1  augustss 		 * the disconnect.
    485        1.1  augustss 		 */
    486       1.11  augustss 	disco:
    487  1.126.2.2     skrll 		if (up->up_dev != NULL) {
    488        1.1  augustss 			/* Disconnected */
    489       1.35  augustss 			DPRINTF(("uhub_explore: device addr=%d disappeared "
    490  1.126.2.2     skrll 				 "on port %d\n", up->up_dev->ud_addr, port));
    491      1.108    dyoung 			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
    492       1.58  augustss 			usbd_clear_port_feature(dev, port,
    493        1.1  augustss 						UHF_C_PORT_CONNECTION);
    494        1.1  augustss 		}
    495       1.35  augustss 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    496       1.42  augustss 			/* Nothing connected, just ignore it. */
    497       1.35  augustss 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
    498       1.35  augustss 				    "_STATUS\n", port));
    499        1.1  augustss 			continue;
    500       1.35  augustss 		}
    501        1.1  augustss 
    502        1.1  augustss 		/* Connected */
    503       1.42  augustss 
    504       1.42  augustss 		if (!(status & UPS_PORT_POWER))
    505       1.98      cube 			aprint_normal_dev(sc->sc_dev,
    506       1.98      cube 			    "strange, connected port %d has no power\n", port);
    507       1.42  augustss 
    508        1.1  augustss 		/* Wait for maximum device power up time. */
    509       1.13  augustss 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    510       1.11  augustss 
    511        1.1  augustss 		/* Reset port, which implies enabling it. */
    512  1.126.2.2     skrll 		if (usbd_reset_port(dev, port, &up->up_status)) {
    513       1.98      cube 			aprint_error_dev(sc->sc_dev,
    514       1.98      cube 			    "port %d reset failed\n", port);
    515       1.54  augustss 			continue;
    516       1.54  augustss 		}
    517       1.54  augustss 		/* Get port status again, it might have changed during reset */
    518  1.126.2.2     skrll 		err = usbd_get_port_status(dev, port, &up->up_status);
    519       1.54  augustss 		if (err) {
    520       1.54  augustss 			DPRINTF(("uhub_explore: get port status failed, "
    521       1.54  augustss 				 "error=%s\n", usbd_errstr(err)));
    522       1.54  augustss 			continue;
    523       1.54  augustss 		}
    524  1.126.2.2     skrll 		status = UGETW(up->up_status.wPortStatus);
    525  1.126.2.2     skrll 		change = UGETW(up->up_status.wPortChange);
    526       1.54  augustss 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    527       1.54  augustss 			/* Nothing connected, just ignore it. */
    528       1.54  augustss #ifdef DIAGNOSTIC
    529       1.98      cube 			aprint_debug_dev(sc->sc_dev,
    530       1.98      cube 			    "port %d, device disappeared after reset\n", port);
    531       1.54  augustss #endif
    532        1.1  augustss 			continue;
    533       1.35  augustss 		}
    534      1.110  kiyohara 		if (!(status & UPS_PORT_ENABLED)) {
    535      1.110  kiyohara 			/* Not allowed send/receive packet. */
    536      1.110  kiyohara #ifdef DIAGNOSTIC
    537      1.115  sborrill 			printf("%s: port %d, device not enabled\n",
    538      1.112    dyoung 			       device_xname(sc->sc_dev), port);
    539      1.110  kiyohara #endif
    540      1.110  kiyohara 			continue;
    541      1.110  kiyohara 		}
    542        1.1  augustss 
    543       1.56  augustss 		/* Figure out device speed */
    544      1.126     skrll #if 0
    545      1.125     skrll 		if (status & UPS_SUPER_SPEED)
    546      1.125     skrll 			speed = USB_SPEED_SUPER;
    547      1.126     skrll 		else
    548      1.126     skrll #endif
    549      1.126     skrll 		if (status & UPS_HIGH_SPEED)
    550       1.56  augustss 			speed = USB_SPEED_HIGH;
    551       1.56  augustss 		else if (status & UPS_LOW_SPEED)
    552       1.56  augustss 			speed = USB_SPEED_LOW;
    553       1.56  augustss 		else
    554       1.56  augustss 			speed = USB_SPEED_FULL;
    555        1.1  augustss 		/* Get device info and set its address. */
    556  1.126.2.2     skrll 		err = usbd_new_device(sc->sc_dev, dev->ud_bus,
    557  1.126.2.2     skrll 			  dev->ud_depth + 1, speed, port, up);
    558        1.1  augustss 		/* XXX retry a few times? */
    559       1.33  augustss 		if (err) {
    560      1.114      matt 			DPRINTFN(-1,("uhub_explore: usbd_new_device failed, "
    561       1.33  augustss 				     "error=%s\n", usbd_errstr(err)));
    562        1.1  augustss 			/* Avoid addressing problems by disabling. */
    563        1.1  augustss 			/* usbd_reset_port(dev, port, &up->status); */
    564       1.30  augustss 
    565       1.58  augustss 			/*
    566       1.30  augustss 			 * The unit refused to accept a new address, or had
    567       1.30  augustss 			 * some other serious problem.  Since we cannot leave
    568       1.30  augustss 			 * at 0 we have to disable the port instead.
    569       1.30  augustss 			 */
    570       1.98      cube 			aprint_error_dev(sc->sc_dev,
    571       1.98      cube 			    "device problem, disabling port %d\n", port);
    572       1.30  augustss 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    573        1.1  augustss 		} else {
    574       1.47  augustss 			/* The port set up succeeded, reset error count. */
    575  1.126.2.2     skrll 			up->up_restartcnt = 0;
    576       1.47  augustss 
    577  1.126.2.2     skrll 			if (up->up_dev->ud_hub)
    578  1.126.2.2     skrll 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    579        1.1  augustss 		}
    580        1.1  augustss 	}
    581       1.87  drochner 	/* enable status change notifications again */
    582      1.101  drochner 	if (!sc->sc_isehciroothub)
    583      1.101  drochner 		memset(sc->sc_status, 0, sc->sc_statuslen);
    584       1.87  drochner 	sc->sc_explorepending = 0;
    585  1.126.2.4     skrll 	return USBD_NORMAL_COMPLETION;
    586        1.1  augustss }
    587        1.1  augustss 
    588       1.18  augustss /*
    589       1.18  augustss  * Called from process context when the hub is gone.
    590       1.18  augustss  * Detach all devices on active ports.
    591       1.18  augustss  */
    592      1.105    dyoung int
    593      1.105    dyoung uhub_detach(device_t self, int flags)
    594       1.18  augustss {
    595      1.107    dyoung 	struct uhub_softc *sc = device_private(self);
    596  1.126.2.2     skrll 	struct usbd_hub *hub = sc->sc_hub->ud_hub;
    597       1.18  augustss 	struct usbd_port *rup;
    598      1.108    dyoung 	int nports, port, rc;
    599       1.18  augustss 
    600       1.18  augustss 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    601       1.18  augustss 
    602       1.39  augustss 	if (hub == NULL)		/* Must be partially working */
    603  1.126.2.4     skrll 		return 0;
    604       1.18  augustss 
    605      1.117       mrg 	/* XXXSMP usb */
    606      1.117       mrg 	KERNEL_LOCK(1, curlwp);
    607      1.117       mrg 
    608  1.126.2.2     skrll 	nports = hub->uh_hubdesc.bNbrPorts;
    609       1.32  augustss 	for(port = 0; port < nports; port++) {
    610  1.126.2.2     skrll 		rup = &hub->uh_ports[port];
    611  1.126.2.2     skrll 		if (rup->up_dev == NULL)
    612      1.108    dyoung 			continue;
    613      1.117       mrg 		if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
    614      1.117       mrg 			/* XXXSMP usb */
    615      1.117       mrg 			KERNEL_UNLOCK_ONE(curlwp);
    616      1.117       mrg 
    617      1.108    dyoung 			return rc;
    618      1.117       mrg 		}
    619       1.18  augustss 	}
    620       1.58  augustss 
    621      1.108    dyoung 	pmf_device_deregister(self);
    622      1.108    dyoung 	usbd_abort_pipe(sc->sc_ipipe);
    623      1.108    dyoung 	usbd_close_pipe(sc->sc_ipipe);
    624      1.108    dyoung 
    625      1.105    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
    626       1.37  augustss 
    627       1.83  drochner #if 0
    628       1.70  augustss 	if (hub->ports[0].tt)
    629  1.126.2.3     skrll 		kmem_free(hub->ports[0].tt,
    630  1.126.2.3     skrll 		    (UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    631  1.126.2.3     skrll 		    sizeof (struct usbd_tt));
    632       1.83  drochner #endif
    633  1.126.2.3     skrll 	kmem_free(hub,
    634  1.126.2.3     skrll 	    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
    635  1.126.2.2     skrll 	sc->sc_hub->ud_hub = NULL;
    636       1.84  drochner 	if (sc->sc_status)
    637  1.126.2.3     skrll 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    638      1.101  drochner 	if (sc->sc_statusbuf)
    639  1.126.2.3     skrll 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    640       1.18  augustss 
    641      1.117       mrg 	/* XXXSMP usb */
    642      1.117       mrg 	KERNEL_UNLOCK_ONE(curlwp);
    643      1.117       mrg 
    644  1.126.2.4     skrll 	return 0;
    645       1.18  augustss }
    646       1.34  augustss 
    647      1.103      kent int
    648      1.103      kent uhub_rescan(device_t self, const char *ifattr, const int *locators)
    649      1.103      kent {
    650      1.103      kent 	struct uhub_softc *sc = device_private(self);
    651  1.126.2.2     skrll 	struct usbd_hub *hub = sc->sc_hub->ud_hub;
    652      1.103      kent 	usbd_device_handle dev;
    653      1.124    martin 	int port;
    654      1.103      kent 
    655  1.126.2.2     skrll 	for (port = 0; port < hub->uh_hubdesc.bNbrPorts; port++) {
    656  1.126.2.2     skrll 		dev = hub->uh_ports[port].up_dev;
    657      1.103      kent 		if (dev == NULL)
    658      1.103      kent 			continue;
    659      1.124    martin 		usbd_reattach_device(sc->sc_dev, dev, port, locators);
    660      1.103      kent 	}
    661      1.103      kent 	return 0;
    662      1.103      kent }
    663      1.103      kent 
    664       1.34  augustss /* Called when a device has been detached from it */
    665       1.95    dyoung void
    666       1.95    dyoung uhub_childdet(device_t self, device_t child)
    667       1.34  augustss {
    668       1.95    dyoung 	struct uhub_softc *sc = device_private(self);
    669       1.95    dyoung 	usbd_device_handle devhub = sc->sc_hub;
    670       1.95    dyoung 	usbd_device_handle dev;
    671       1.95    dyoung 	int nports;
    672       1.95    dyoung 	int port;
    673       1.95    dyoung 	int i;
    674       1.95    dyoung 
    675  1.126.2.2     skrll 	if (!devhub->ud_hub)
    676       1.95    dyoung 		/* should never happen; children are only created after init */
    677       1.95    dyoung 		panic("hub not fully initialised, but child deleted?");
    678       1.95    dyoung 
    679  1.126.2.2     skrll 	nports = devhub->ud_hub->uh_hubdesc.bNbrPorts;
    680       1.95    dyoung 	for (port = 0; port < nports; port++) {
    681  1.126.2.2     skrll 		dev = devhub->ud_hub->uh_ports[port].up_dev;
    682  1.126.2.2     skrll 		if (!dev || dev->ud_subdevlen == 0)
    683       1.95    dyoung 			continue;
    684  1.126.2.2     skrll 		for (i = 0; i < dev->ud_subdevlen; i++) {
    685  1.126.2.2     skrll 			if (dev->ud_subdevs[i] == child) {
    686  1.126.2.2     skrll 				dev->ud_subdevs[i] = NULL;
    687  1.126.2.2     skrll 				dev->ud_nifaces_claimed--;
    688       1.95    dyoung 			}
    689       1.95    dyoung 		}
    690  1.126.2.2     skrll 		if (dev->ud_nifaces_claimed == 0) {
    691  1.126.2.3     skrll 			kmem_free(dev->ud_subdevs,
    692  1.126.2.3     skrll 			    dev->ud_subdevlen * sizeof(device_t));
    693  1.126.2.2     skrll 			dev->ud_subdevs = NULL;
    694  1.126.2.2     skrll 			dev->ud_subdevlen = 0;
    695      1.113  jmcneill 		}
    696       1.95    dyoung 	}
    697       1.34  augustss }
    698       1.34  augustss 
    699       1.18  augustss 
    700       1.18  augustss /*
    701       1.18  augustss  * Hub interrupt.
    702       1.18  augustss  * This an indication that some port has changed status.
    703       1.18  augustss  * Notify the bus event handler thread that we need
    704       1.18  augustss  * to be explored again.
    705       1.18  augustss  */
    706        1.1  augustss void
    707      1.105    dyoung uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    708        1.1  augustss {
    709        1.1  augustss 	struct uhub_softc *sc = addr;
    710        1.1  augustss 
    711       1.11  augustss 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    712       1.87  drochner 
    713       1.50  augustss 	if (status == USBD_STALLED)
    714        1.9  augustss 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    715       1.87  drochner 	else if (status == USBD_NORMAL_COMPLETION &&
    716       1.87  drochner 		 !sc->sc_explorepending) {
    717       1.87  drochner 		/*
    718       1.87  drochner 		 * Make sure the status is not overwritten in between.
    719       1.87  drochner 		 * XXX we should suspend the pipe instead
    720       1.87  drochner 		 */
    721       1.87  drochner 		memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
    722       1.87  drochner 		sc->sc_explorepending = 1;
    723       1.50  augustss 		usb_needs_explore(sc->sc_hub);
    724       1.87  drochner 	}
    725       1.89  drochner 	/*
    726       1.89  drochner 	 * XXX workaround for broken implementation of the interrupt
    727       1.89  drochner 	 * pipe in EHCI root hub emulation which doesn't resend
    728       1.89  drochner 	 * status change notifications until handled: force a rescan
    729       1.89  drochner 	 * of the ports we touched in the last run
    730       1.89  drochner 	 */
    731       1.89  drochner 	if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
    732      1.101  drochner 	    sc->sc_isehciroothub)
    733       1.89  drochner 		usb_needs_explore(sc->sc_hub);
    734        1.1  augustss }
    735