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