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