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