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