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