Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.49.4.3
      1  1.49.4.3  jdolecek /*	$NetBSD: uhub.c,v 1.49.4.3 2002/10/10 18:42:39 jdolecek 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.1  augustss  * Copyright (c) 1998 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.48  augustss  * USB spec: http://www.usb.org/developers/data/usbspec.zip
     43       1.1  augustss  */
     44       1.1  augustss 
     45  1.49.4.1   thorpej #include <sys/cdefs.h>
     46  1.49.4.3  jdolecek __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.49.4.3 2002/10/10 18:42:39 jdolecek Exp $");
     47  1.49.4.1   thorpej 
     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.28  augustss #include <machine/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.43  augustss #define UHUB_INTR_INTERVAL 255	/* ms */
     69      1.43  augustss 
     70      1.32  augustss #ifdef UHUB_DEBUG
     71      1.35  augustss #define DPRINTF(x)	if (uhubdebug) logprintf x
     72      1.35  augustss #define DPRINTFN(n,x)	if (uhubdebug>(n)) logprintf x
     73      1.35  augustss int	uhubdebug;
     74       1.1  augustss #else
     75       1.1  augustss #define DPRINTF(x)
     76       1.1  augustss #define DPRINTFN(n,x)
     77       1.1  augustss #endif
     78       1.1  augustss 
     79       1.1  augustss struct uhub_softc {
     80      1.26  augustss 	USBBASEDEVICE		sc_dev;		/* base device */
     81      1.11  augustss 	usbd_device_handle	sc_hub;		/* USB device */
     82      1.11  augustss 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
     83      1.11  augustss 	u_int8_t		sc_status[1];	/* XXX more ports */
     84      1.11  augustss 	u_char			sc_running;
     85       1.1  augustss };
     86       1.1  augustss 
     87      1.45  augustss Static usbd_status uhub_explore(usbd_device_handle hub);
     88      1.45  augustss Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
     89       1.1  augustss 
     90      1.32  augustss #if defined(__FreeBSD__)
     91      1.41  augustss Static bus_child_detached_t uhub_child_detached;
     92      1.32  augustss #endif
     93      1.32  augustss 
     94      1.32  augustss 
     95  1.49.4.2  jdolecek /*
     96      1.32  augustss  * We need two attachment points:
     97      1.32  augustss  * hub to usb and hub to hub
     98      1.32  augustss  * Every other driver only connects to hubs
     99      1.32  augustss  */
    100       1.1  augustss 
    101      1.20  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    102      1.34  augustss USB_DECLARE_DRIVER(uhub);
    103      1.34  augustss 
    104      1.32  augustss /* Create the driver instance for the hub connected to hub case */
    105  1.49.4.3  jdolecek CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc),
    106  1.49.4.3  jdolecek     uhub_match, uhub_attach, uhub_detach, uhub_activate);
    107      1.32  augustss #elif defined(__FreeBSD__)
    108      1.34  augustss USB_DECLARE_DRIVER_INIT(uhub,
    109      1.34  augustss 			DEVMETHOD(bus_child_detached, uhub_child_detached));
    110  1.49.4.2  jdolecek 
    111      1.32  augustss /* Create the driver instance for the hub connected to usb case. */
    112      1.32  augustss devclass_t uhubroot_devclass;
    113      1.32  augustss 
    114      1.41  augustss Static device_method_t uhubroot_methods[] = {
    115      1.32  augustss 	DEVMETHOD(device_probe, uhub_match),
    116      1.32  augustss 	DEVMETHOD(device_attach, uhub_attach),
    117      1.32  augustss 
    118      1.32  augustss 	/* detach is not allowed for a root hub */
    119      1.32  augustss 	{0,0}
    120      1.32  augustss };
    121      1.32  augustss 
    122      1.41  augustss Static	driver_t uhubroot_driver = {
    123      1.32  augustss 	"uhub",
    124      1.32  augustss 	uhubroot_methods,
    125      1.32  augustss 	sizeof(struct uhub_softc)
    126      1.32  augustss };
    127      1.11  augustss #endif
    128       1.1  augustss 
    129      1.11  augustss USB_MATCH(uhub)
    130       1.1  augustss {
    131      1.11  augustss 	USB_MATCH_START(uhub, uaa);
    132       1.1  augustss 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
    133  1.49.4.2  jdolecek 
    134      1.11  augustss 	DPRINTFN(5,("uhub_match, dd=%p\n", dd));
    135  1.49.4.2  jdolecek 	/*
    136       1.1  augustss 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    137       1.1  augustss 	 * so we just ignore the subclass.
    138       1.1  augustss 	 */
    139      1.40  augustss 	if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
    140       1.1  augustss 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
    141       1.1  augustss 	return (UMATCH_NONE);
    142       1.1  augustss }
    143       1.1  augustss 
    144      1.11  augustss USB_ATTACH(uhub)
    145       1.1  augustss {
    146      1.11  augustss 	USB_ATTACH_START(uhub, sc, uaa);
    147       1.1  augustss 	usbd_device_handle dev = uaa->device;
    148       1.1  augustss 	char devinfo[1024];
    149      1.33  augustss 	usbd_status err;
    150       1.1  augustss 	struct usbd_hub *hub;
    151       1.1  augustss 	usb_device_request_t req;
    152       1.1  augustss 	usb_hub_descriptor_t hubdesc;
    153      1.42  augustss 	int p, port, nports, nremov, pwrdly;
    154       1.1  augustss 	usbd_interface_handle iface;
    155       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    156  1.49.4.2  jdolecek 
    157       1.1  augustss 	DPRINTFN(1,("uhub_attach\n"));
    158       1.1  augustss 	sc->sc_hub = dev;
    159       1.1  augustss 	usbd_devinfo(dev, 1, devinfo);
    160      1.11  augustss 	USB_ATTACH_SETUP;
    161      1.11  augustss 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    162       1.1  augustss 
    163      1.33  augustss 	err = usbd_set_config_index(dev, 0, 1);
    164      1.33  augustss 	if (err) {
    165      1.21  augustss 		DPRINTF(("%s: configuration failed, error=%s\n",
    166      1.33  augustss 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    167      1.11  augustss 		USB_ATTACH_ERROR_RETURN;
    168       1.1  augustss 	}
    169       1.1  augustss 
    170       1.1  augustss 	if (dev->depth > USB_HUB_MAX_DEPTH) {
    171       1.1  augustss 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
    172      1.11  augustss 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
    173      1.11  augustss 		USB_ATTACH_ERROR_RETURN;
    174       1.1  augustss 	}
    175       1.1  augustss 
    176       1.1  augustss 	/* Get hub descriptor. */
    177       1.1  augustss 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    178       1.1  augustss 	req.bRequest = UR_GET_DESCRIPTOR;
    179       1.1  augustss 	USETW(req.wValue, 0);
    180       1.1  augustss 	USETW(req.wIndex, 0);
    181       1.1  augustss 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    182       1.1  augustss 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
    183      1.33  augustss 	err = usbd_do_request(dev, &req, &hubdesc);
    184      1.11  augustss 	nports = hubdesc.bNbrPorts;
    185      1.33  augustss 	if (!err && nports > 7) {
    186      1.11  augustss 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    187      1.33  augustss 		err = usbd_do_request(dev, &req, &hubdesc);
    188      1.11  augustss 	}
    189      1.33  augustss 	if (err) {
    190      1.21  augustss 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
    191      1.33  augustss 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    192      1.11  augustss 		USB_ATTACH_ERROR_RETURN;
    193       1.1  augustss 	}
    194       1.1  augustss 
    195      1.11  augustss 	for (nremov = 0, port = 1; port <= nports; port++)
    196      1.11  augustss 		if (!UHD_NOT_REMOV(&hubdesc, port))
    197      1.11  augustss 			nremov++;
    198      1.11  augustss 	printf("%s: %d port%s with %d removable, %s powered\n",
    199      1.11  augustss 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
    200      1.11  augustss 	       nremov, dev->self_powered ? "self" : "bus");
    201      1.11  augustss 
    202       1.1  augustss 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    203      1.18  augustss 		     M_USBDEV, M_NOWAIT);
    204      1.33  augustss 	if (hub == NULL)
    205      1.11  augustss 		USB_ATTACH_ERROR_RETURN;
    206       1.1  augustss 	dev->hub = hub;
    207      1.11  augustss 	dev->hub->hubsoftc = sc;
    208       1.1  augustss 	hub->explore = uhub_explore;
    209       1.1  augustss 	hub->hubdesc = hubdesc;
    210  1.49.4.2  jdolecek 
    211      1.10  drochner 	DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
    212      1.10  drochner 		    "parent->selfpowered=%d\n",
    213       1.1  augustss 		 dev->self_powered, dev->powersrc->parent,
    214  1.49.4.2  jdolecek 		 dev->powersrc->parent ?
    215       1.1  augustss 		 dev->powersrc->parent->self_powered : 0));
    216      1.32  augustss 
    217      1.33  augustss 	if (!dev->self_powered && dev->powersrc->parent != NULL &&
    218       1.1  augustss 	    !dev->powersrc->parent->self_powered) {
    219      1.10  drochner 		printf("%s: bus powered hub connected to bus powered hub, "
    220      1.26  augustss 		       "ignored\n", USBDEVNAME(sc->sc_dev));
    221      1.18  augustss 		goto bad;
    222       1.1  augustss 	}
    223       1.1  augustss 
    224       1.1  augustss 	/* Set up interrupt pipe. */
    225      1.33  augustss 	err = usbd_device2interface_handle(dev, 0, &iface);
    226      1.33  augustss 	if (err) {
    227      1.11  augustss 		printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
    228      1.18  augustss 		goto bad;
    229       1.1  augustss 	}
    230       1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    231      1.33  augustss 	if (ed == NULL) {
    232      1.11  augustss 		printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
    233      1.18  augustss 		goto bad;
    234       1.1  augustss 	}
    235       1.1  augustss 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    236      1.11  augustss 		printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
    237      1.18  augustss 		goto bad;
    238       1.1  augustss 	}
    239       1.1  augustss 
    240      1.33  augustss 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    241  1.49.4.2  jdolecek 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
    242      1.43  augustss 		  sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
    243      1.33  augustss 	if (err) {
    244  1.49.4.2  jdolecek 		printf("%s: cannot open interrupt pipe\n",
    245      1.11  augustss 		       USBDEVNAME(sc->sc_dev));
    246      1.18  augustss 		goto bad;
    247       1.1  augustss 	}
    248       1.1  augustss 
    249      1.11  augustss 	/* Wait with power off for a while. */
    250      1.13  augustss 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    251      1.11  augustss 
    252      1.37  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
    253      1.37  augustss 
    254      1.42  augustss 	/*
    255      1.42  augustss 	 * To have the best chance of success we do things in the exact same
    256      1.42  augustss 	 * order as Windoze98.  This should not be necessary, but some
    257      1.42  augustss 	 * devices do not follow the USB specs to the letter.
    258      1.42  augustss 	 *
    259      1.42  augustss 	 * These are the events on the bus when a hub is attached:
    260      1.42  augustss 	 *  Get device and config descriptors (see attach code)
    261      1.42  augustss 	 *  Get hub descriptor (see above)
    262      1.42  augustss 	 *  For all ports
    263      1.42  augustss 	 *     turn on power
    264      1.42  augustss 	 *     wait for power to become stable
    265      1.42  augustss 	 * (all below happens in explore code)
    266      1.42  augustss 	 *  For all ports
    267      1.42  augustss 	 *     clear C_PORT_CONNECTION
    268      1.42  augustss 	 *  For all ports
    269      1.42  augustss 	 *     get port status
    270      1.42  augustss 	 *     if device connected
    271  1.49.4.1   thorpej 	 *        wait 100 ms
    272      1.42  augustss 	 *        turn on reset
    273      1.42  augustss 	 *        wait
    274      1.42  augustss 	 *        clear C_PORT_RESET
    275      1.42  augustss 	 *        get port status
    276      1.42  augustss 	 *        proceed with device attachment
    277      1.42  augustss 	 */
    278      1.42  augustss 
    279      1.42  augustss 	/* Set up data structures */
    280      1.11  augustss 	for (p = 0; p < nports; p++) {
    281      1.11  augustss 		struct usbd_port *up = &hub->ports[p];
    282      1.11  augustss 		up->device = 0;
    283      1.11  augustss 		up->parent = dev;
    284      1.11  augustss 		up->portno = p+1;
    285      1.42  augustss 		if (dev->self_powered)
    286      1.42  augustss 			/* Self powered hub, give ports maximum current. */
    287      1.42  augustss 			up->power = USB_MAX_POWER;
    288      1.42  augustss 		else
    289      1.42  augustss 			up->power = USB_MIN_POWER;
    290      1.42  augustss 	}
    291      1.42  augustss 
    292      1.42  augustss 	/* XXX should check for none, individual, or ganged power? */
    293      1.42  augustss 
    294      1.42  augustss 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    295      1.42  augustss 	    + USB_EXTRA_POWER_UP_TIME;
    296      1.42  augustss 	for (port = 1; port <= nports; port++) {
    297      1.42  augustss 		/* Turn the power on. */
    298      1.42  augustss 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    299      1.33  augustss 		if (err)
    300  1.49.4.2  jdolecek 			printf("%s: port %d power on failed, %s\n",
    301      1.42  augustss 			       USBDEVNAME(sc->sc_dev), port,
    302      1.42  augustss 			       usbd_errstr(err));
    303      1.42  augustss 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
    304      1.42  augustss 		/* Wait for stable power. */
    305      1.42  augustss 		usbd_delay_ms(dev, pwrdly);
    306       1.1  augustss 	}
    307      1.42  augustss 
    308      1.42  augustss 	/* The usual exploration will finish the setup. */
    309      1.42  augustss 
    310       1.1  augustss 	sc->sc_running = 1;
    311      1.11  augustss 
    312      1.11  augustss 	USB_ATTACH_SUCCESS_RETURN;
    313      1.11  augustss 
    314      1.18  augustss  bad:
    315      1.18  augustss 	free(hub, M_USBDEV);
    316      1.18  augustss 	dev->hub = 0;
    317      1.18  augustss 	USB_ATTACH_ERROR_RETURN;
    318      1.11  augustss }
    319      1.11  augustss 
    320       1.1  augustss usbd_status
    321      1.45  augustss uhub_explore(usbd_device_handle dev)
    322       1.1  augustss {
    323       1.1  augustss 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    324      1.11  augustss 	struct uhub_softc *sc = dev->hub->hubsoftc;
    325       1.1  augustss 	struct usbd_port *up;
    326      1.33  augustss 	usbd_status err;
    327  1.49.4.1   thorpej 	int speed;
    328       1.1  augustss 	int port;
    329       1.1  augustss 	int change, status;
    330       1.1  augustss 
    331      1.11  augustss 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    332       1.1  augustss 
    333       1.1  augustss 	if (!sc->sc_running)
    334       1.1  augustss 		return (USBD_NOT_STARTED);
    335       1.1  augustss 
    336       1.1  augustss 	/* Ignore hubs that are too deep. */
    337       1.1  augustss 	if (dev->depth > USB_HUB_MAX_DEPTH)
    338       1.1  augustss 		return (USBD_TOO_DEEP);
    339       1.1  augustss 
    340       1.1  augustss 	for(port = 1; port <= hd->bNbrPorts; port++) {
    341       1.1  augustss 		up = &dev->hub->ports[port-1];
    342      1.33  augustss 		err = usbd_get_port_status(dev, port, &up->status);
    343      1.33  augustss 		if (err) {
    344      1.10  drochner 			DPRINTF(("uhub_explore: get port status failed, "
    345      1.33  augustss 				 "error=%s\n", usbd_errstr(err)));
    346       1.1  augustss 			continue;
    347       1.1  augustss 		}
    348       1.1  augustss 		status = UGETW(up->status.wPortStatus);
    349       1.1  augustss 		change = UGETW(up->status.wPortChange);
    350  1.49.4.1   thorpej 		DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
    351  1.49.4.1   thorpej 			    USBDEVNAME(sc->sc_dev), port, status, change));
    352      1.11  augustss 		if (change & UPS_C_PORT_ENABLED) {
    353      1.35  augustss 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
    354      1.11  augustss 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    355      1.11  augustss 			if (status & UPS_PORT_ENABLED) {
    356      1.11  augustss 				printf("%s: illegal enable change, port %d\n",
    357      1.11  augustss 				       USBDEVNAME(sc->sc_dev), port);
    358      1.11  augustss 			} else {
    359      1.11  augustss 				/* Port error condition. */
    360      1.47  augustss 				if (up->restartcnt) /* no message first time */
    361      1.11  augustss 					printf("%s: port error, restarting "
    362      1.11  augustss 					       "port %d\n",
    363      1.11  augustss 					       USBDEVNAME(sc->sc_dev), port);
    364      1.47  augustss 
    365      1.47  augustss 				if (up->restartcnt++ < USBD_RESTART_MAX)
    366      1.11  augustss 					goto disco;
    367      1.47  augustss 				else
    368      1.11  augustss 					printf("%s: port error, giving up "
    369      1.11  augustss 					       "port %d\n",
    370      1.11  augustss 					       USBDEVNAME(sc->sc_dev), port);
    371      1.11  augustss 			}
    372      1.11  augustss 		}
    373      1.11  augustss 		if (!(change & UPS_C_CONNECT_STATUS)) {
    374      1.35  augustss 			DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
    375      1.35  augustss 				    "STATUS\n", port));
    376       1.1  augustss 			/* No status change, just do recursive explore. */
    377  1.49.4.1   thorpej 			if (up->device != NULL && up->device->hub != NULL)
    378      1.11  augustss 				up->device->hub->explore(up->device);
    379  1.49.4.1   thorpej #if 0 && defined(DIAGNOSTIC)
    380  1.49.4.2  jdolecek 			if (up->device == NULL &&
    381  1.49.4.1   thorpej 			    (status & UPS_CURRENT_CONNECT_STATUS))
    382  1.49.4.1   thorpej 				printf("%s: connected, no device\n",
    383  1.49.4.1   thorpej 				       USBDEVNAME(sc->sc_dev));
    384  1.49.4.1   thorpej #endif
    385       1.1  augustss 			continue;
    386       1.1  augustss 		}
    387      1.42  augustss 
    388      1.42  augustss 		/* We have a connect status change, handle it. */
    389      1.42  augustss 
    390       1.1  augustss 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    391       1.1  augustss 			 dev->address, port));
    392       1.1  augustss 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    393      1.42  augustss 		/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
    394       1.1  augustss 		/*
    395       1.1  augustss 		 * If there is already a device on the port the change status
    396       1.1  augustss 		 * must mean that is has disconnected.  Looking at the
    397       1.1  augustss 		 * current connect status is not enough to figure this out
    398       1.1  augustss 		 * since a new unit may have been connected before we handle
    399       1.1  augustss 		 * the disconnect.
    400       1.1  augustss 		 */
    401      1.11  augustss 	disco:
    402      1.33  augustss 		if (up->device != NULL) {
    403       1.1  augustss 			/* Disconnected */
    404      1.35  augustss 			DPRINTF(("uhub_explore: device addr=%d disappeared "
    405      1.31  augustss 				 "on port %d\n", up->device->address, port));
    406      1.31  augustss 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
    407  1.49.4.2  jdolecek 			usbd_clear_port_feature(dev, port,
    408       1.1  augustss 						UHF_C_PORT_CONNECTION);
    409       1.1  augustss 		}
    410      1.35  augustss 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    411      1.42  augustss 			/* Nothing connected, just ignore it. */
    412      1.35  augustss 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
    413      1.35  augustss 				    "_STATUS\n", port));
    414       1.1  augustss 			continue;
    415      1.35  augustss 		}
    416       1.1  augustss 
    417       1.1  augustss 		/* Connected */
    418      1.42  augustss 
    419      1.42  augustss 		if (!(status & UPS_PORT_POWER))
    420      1.42  augustss 			printf("%s: strange, connected port %d has no power\n",
    421      1.42  augustss 			       USBDEVNAME(sc->sc_dev), port);
    422      1.42  augustss 
    423       1.1  augustss 		/* Wait for maximum device power up time. */
    424      1.13  augustss 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    425      1.11  augustss 
    426       1.1  augustss 		/* Reset port, which implies enabling it. */
    427      1.35  augustss 		if (usbd_reset_port(dev, port, &up->status)) {
    428  1.49.4.1   thorpej 			printf("%s: port %d reset failed\n",
    429  1.49.4.1   thorpej 			       USBDEVNAME(sc->sc_dev), port);
    430  1.49.4.1   thorpej 			continue;
    431  1.49.4.1   thorpej 		}
    432  1.49.4.1   thorpej 		/* Get port status again, it might have changed during reset */
    433  1.49.4.1   thorpej 		err = usbd_get_port_status(dev, port, &up->status);
    434  1.49.4.1   thorpej 		if (err) {
    435  1.49.4.1   thorpej 			DPRINTF(("uhub_explore: get port status failed, "
    436  1.49.4.1   thorpej 				 "error=%s\n", usbd_errstr(err)));
    437  1.49.4.1   thorpej 			continue;
    438  1.49.4.1   thorpej 		}
    439  1.49.4.1   thorpej 		status = UGETW(up->status.wPortStatus);
    440  1.49.4.1   thorpej 		change = UGETW(up->status.wPortChange);
    441  1.49.4.1   thorpej 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    442  1.49.4.1   thorpej 			/* Nothing connected, just ignore it. */
    443  1.49.4.1   thorpej #ifdef DIAGNOSTIC
    444  1.49.4.1   thorpej 			printf("%s: port %d, device disappeared after reset\n",
    445  1.49.4.1   thorpej 			       USBDEVNAME(sc->sc_dev), port);
    446  1.49.4.1   thorpej #endif
    447       1.1  augustss 			continue;
    448      1.35  augustss 		}
    449       1.1  augustss 
    450  1.49.4.1   thorpej 		/* Figure out device speed */
    451  1.49.4.1   thorpej 		if (status & UPS_HIGH_SPEED)
    452  1.49.4.1   thorpej 			speed = USB_SPEED_HIGH;
    453  1.49.4.1   thorpej 		else if (status & UPS_LOW_SPEED)
    454  1.49.4.1   thorpej 			speed = USB_SPEED_LOW;
    455  1.49.4.1   thorpej 		else
    456  1.49.4.1   thorpej 			speed = USB_SPEED_FULL;
    457       1.1  augustss 		/* Get device info and set its address. */
    458  1.49.4.2  jdolecek 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
    459  1.49.4.1   thorpej 			  dev->depth + 1, speed, port, up);
    460       1.1  augustss 		/* XXX retry a few times? */
    461      1.33  augustss 		if (err) {
    462      1.10  drochner 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
    463      1.33  augustss 				     "error=%s\n", usbd_errstr(err)));
    464       1.1  augustss 			/* Avoid addressing problems by disabling. */
    465       1.1  augustss 			/* usbd_reset_port(dev, port, &up->status); */
    466      1.30  augustss 
    467  1.49.4.2  jdolecek 			/*
    468      1.30  augustss 			 * The unit refused to accept a new address, or had
    469      1.30  augustss 			 * some other serious problem.  Since we cannot leave
    470      1.30  augustss 			 * at 0 we have to disable the port instead.
    471      1.30  augustss 			 */
    472      1.30  augustss 			printf("%s: device problem, disabling port %d\n",
    473      1.30  augustss 			       USBDEVNAME(sc->sc_dev), port);
    474      1.30  augustss 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    475       1.1  augustss 		} else {
    476      1.47  augustss 			/* The port set up succeeded, reset error count. */
    477      1.47  augustss 			up->restartcnt = 0;
    478      1.47  augustss 
    479      1.34  augustss 			if (up->device->hub)
    480      1.11  augustss 				up->device->hub->explore(up->device);
    481       1.1  augustss 		}
    482       1.1  augustss 	}
    483       1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    484       1.1  augustss }
    485       1.1  augustss 
    486      1.32  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    487      1.18  augustss int
    488      1.45  augustss uhub_activate(device_ptr_t self, enum devact act)
    489      1.18  augustss {
    490      1.27  augustss 	struct uhub_softc *sc = (struct uhub_softc *)self;
    491      1.39  augustss 	struct usbd_hub *hub = sc->sc_hub->hub;
    492      1.34  augustss 	usbd_device_handle dev;
    493      1.34  augustss 	int nports, port, i;
    494      1.27  augustss 
    495      1.23  augustss 	switch (act) {
    496      1.23  augustss 	case DVACT_ACTIVATE:
    497      1.23  augustss 		return (EOPNOTSUPP);
    498      1.23  augustss 
    499      1.23  augustss 	case DVACT_DEACTIVATE:
    500      1.39  augustss 		if (hub == NULL) /* malfunctioning hub */
    501      1.39  augustss 			break;
    502      1.39  augustss 		nports = hub->hubdesc.bNbrPorts;
    503      1.34  augustss 		for(port = 0; port < nports; port++) {
    504      1.39  augustss 			dev = hub->ports[port].device;
    505      1.46  augustss 			if (dev != NULL && dev->subdevs != NULL) {
    506      1.46  augustss 				for (i = 0; dev->subdevs[i] != NULL; i++)
    507      1.27  augustss 					config_deactivate(dev->subdevs[i]);
    508      1.27  augustss 			}
    509      1.27  augustss 		}
    510      1.23  augustss 		break;
    511      1.23  augustss 	}
    512      1.18  augustss 	return (0);
    513      1.18  augustss }
    514      1.32  augustss #endif
    515      1.18  augustss 
    516      1.18  augustss /*
    517      1.18  augustss  * Called from process context when the hub is gone.
    518      1.18  augustss  * Detach all devices on active ports.
    519      1.18  augustss  */
    520      1.32  augustss USB_DETACH(uhub)
    521      1.18  augustss {
    522      1.32  augustss 	USB_DETACH_START(uhub, sc);
    523      1.39  augustss 	struct usbd_hub *hub = sc->sc_hub->hub;
    524      1.18  augustss 	struct usbd_port *rup;
    525      1.32  augustss 	int port, nports;
    526      1.18  augustss 
    527      1.32  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    528      1.18  augustss 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    529      1.32  augustss #elif defined(__FreeBSD__)
    530      1.32  augustss 	DPRINTF(("uhub_detach: sc=%port\n", sc));
    531      1.32  augustss #endif
    532      1.18  augustss 
    533      1.39  augustss 	if (hub == NULL)		/* Must be partially working */
    534      1.18  augustss 		return (0);
    535      1.18  augustss 
    536      1.18  augustss 	usbd_abort_pipe(sc->sc_ipipe);
    537      1.18  augustss 	usbd_close_pipe(sc->sc_ipipe);
    538      1.18  augustss 
    539      1.39  augustss 	nports = hub->hubdesc.bNbrPorts;
    540      1.32  augustss 	for(port = 0; port < nports; port++) {
    541      1.39  augustss 		rup = &hub->ports[port];
    542      1.18  augustss 		if (rup->device)
    543      1.31  augustss 			usb_disconnect_port(rup, self);
    544      1.18  augustss 	}
    545  1.49.4.2  jdolecek 
    546      1.39  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
    547      1.39  augustss 			   USBDEV(sc->sc_dev));
    548      1.37  augustss 
    549      1.39  augustss 	free(hub, M_USBDEV);
    550      1.39  augustss 	sc->sc_hub->hub = NULL;
    551      1.18  augustss 
    552      1.18  augustss 	return (0);
    553      1.18  augustss }
    554      1.34  augustss 
    555      1.34  augustss #if defined(__FreeBSD__)
    556      1.34  augustss /* Called when a device has been detached from it */
    557      1.41  augustss Static void
    558      1.45  augustss uhub_child_detached(device_t self, device_t child)
    559      1.34  augustss {
    560      1.34  augustss        struct uhub_softc *sc = device_get_softc(self);
    561      1.34  augustss        usbd_device_handle devhub = sc->sc_hub;
    562      1.34  augustss        usbd_device_handle dev;
    563      1.34  augustss        int nports;
    564      1.34  augustss        int port;
    565      1.34  augustss        int i;
    566      1.34  augustss 
    567  1.49.4.2  jdolecek        if (!devhub->hub)
    568      1.34  augustss                /* should never happen; children are only created after init */
    569      1.34  augustss                panic("hub not fully initialised, but child deleted?");
    570      1.34  augustss 
    571      1.34  augustss        nports = devhub->hub->hubdesc.bNbrPorts;
    572      1.34  augustss        for (port = 0; port < nports; port++) {
    573      1.34  augustss                dev = devhub->hub->ports[port].device;
    574      1.34  augustss                if (dev && dev->subdevs) {
    575      1.34  augustss                        for (i = 0; dev->subdevs[i]; i++) {
    576      1.34  augustss                                if (dev->subdevs[i] == child) {
    577      1.34  augustss                                        dev->subdevs[i] = NULL;
    578      1.34  augustss                                        return;
    579      1.34  augustss                                }
    580      1.34  augustss                        }
    581      1.34  augustss                }
    582      1.34  augustss        }
    583      1.34  augustss }
    584      1.34  augustss #endif
    585      1.34  augustss 
    586      1.18  augustss 
    587      1.18  augustss /*
    588      1.18  augustss  * Hub interrupt.
    589      1.18  augustss  * This an indication that some port has changed status.
    590      1.18  augustss  * Notify the bus event handler thread that we need
    591      1.18  augustss  * to be explored again.
    592      1.18  augustss  */
    593       1.1  augustss void
    594      1.45  augustss uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    595       1.1  augustss {
    596       1.1  augustss 	struct uhub_softc *sc = addr;
    597       1.1  augustss 
    598      1.11  augustss 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    599  1.49.4.1   thorpej 	if (status == USBD_STALLED)
    600       1.9  augustss 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    601  1.49.4.1   thorpej 	else if (status == USBD_NORMAL_COMPLETION)
    602  1.49.4.1   thorpej 		usb_needs_explore(sc->sc_hub);
    603       1.1  augustss }
    604      1.11  augustss 
    605      1.11  augustss #if defined(__FreeBSD__)
    606      1.19  augustss DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
    607      1.19  augustss DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
    608      1.11  augustss #endif
    609