2003-02-08 06:32:50 +03:00
|
|
|
/* $NetBSD: uhub.c,v 1.64 2003/02/08 03:32:51 ichiro Exp $ */
|
1999-11-19 02:32:25 +03:00
|
|
|
/* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $ */
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1998-11-26 01:32:04 +03:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2000-04-27 19:26:44 +04:00
|
|
|
* by Lennart Augustsson (lennart@augustsson.net) at
|
1998-11-26 01:32:04 +03:00
|
|
|
* Carlstedt Research & Technology.
|
1998-07-12 23:51:55 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1999-01-10 14:13:36 +03:00
|
|
|
/*
|
2003-02-08 06:32:50 +03:00
|
|
|
* USB spec: http://www.usb.org/developers/docs/usbspec.zip
|
1999-01-10 14:13:36 +03:00
|
|
|
*/
|
|
|
|
|
2001-11-13 09:24:53 +03:00
|
|
|
#include <sys/cdefs.h>
|
2003-02-08 06:32:50 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.64 2003/02/08 03:32:51 ichiro Exp $");
|
2001-11-13 09:24:53 +03:00
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
1999-08-17 20:06:20 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1998-07-12 23:51:55 +04:00
|
|
|
#include <sys/device.h>
|
1999-11-19 02:32:25 +03:00
|
|
|
#include <sys/proc.h>
|
1998-12-26 15:53:00 +03:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
1999-10-13 12:10:55 +04:00
|
|
|
#include "bus_if.h"
|
1998-12-26 15:53:00 +03:00
|
|
|
#endif
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-09-13 23:18:17 +04:00
|
|
|
#include <machine/bus.h>
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <dev/usb/usbdi_util.h>
|
|
|
|
#include <dev/usb/usbdivar.h>
|
|
|
|
|
2000-04-21 23:51:43 +04:00
|
|
|
#define UHUB_INTR_INTERVAL 255 /* ms */
|
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
#ifdef UHUB_DEBUG
|
1999-11-25 02:13:19 +03:00
|
|
|
#define DPRINTF(x) if (uhubdebug) logprintf x
|
|
|
|
#define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
|
2003-01-02 06:20:09 +03:00
|
|
|
int uhubdebug = 0;
|
1998-07-12 23:51:55 +04:00
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct uhub_softc {
|
1999-09-05 23:32:18 +04:00
|
|
|
USBBASEDEVICE sc_dev; /* base device */
|
1998-12-26 15:53:00 +03:00
|
|
|
usbd_device_handle sc_hub; /* USB device */
|
|
|
|
usbd_pipe_handle sc_ipipe; /* interrupt pipe */
|
|
|
|
u_int8_t sc_status[1]; /* XXX more ports */
|
|
|
|
u_char sc_running;
|
1998-07-12 23:51:55 +04:00
|
|
|
};
|
|
|
|
|
2000-06-01 18:28:57 +04:00
|
|
|
Static usbd_status uhub_explore(usbd_device_handle hub);
|
|
|
|
Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
#if defined(__FreeBSD__)
|
2000-03-27 16:33:53 +04:00
|
|
|
Static bus_child_detached_t uhub_child_detached;
|
1999-10-13 12:10:55 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-07-12 01:14:24 +04:00
|
|
|
/*
|
1999-10-13 12:10:55 +04:00
|
|
|
* We need two attachment points:
|
|
|
|
* hub to usb and hub to hub
|
|
|
|
* Every other driver only connects to hubs
|
|
|
|
*/
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-08-17 20:06:20 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1999-11-19 02:32:25 +03:00
|
|
|
USB_DECLARE_DRIVER(uhub);
|
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
/* Create the driver instance for the hub connected to hub case */
|
2002-10-01 05:25:25 +04:00
|
|
|
CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc),
|
2002-10-02 20:51:16 +04:00
|
|
|
uhub_match, uhub_attach, uhub_detach, uhub_activate);
|
1999-10-13 12:10:55 +04:00
|
|
|
#elif defined(__FreeBSD__)
|
1999-11-19 02:32:25 +03:00
|
|
|
USB_DECLARE_DRIVER_INIT(uhub,
|
|
|
|
DEVMETHOD(bus_child_detached, uhub_child_detached));
|
2002-07-12 01:14:24 +04:00
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
/* Create the driver instance for the hub connected to usb case. */
|
|
|
|
devclass_t uhubroot_devclass;
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static device_method_t uhubroot_methods[] = {
|
1999-10-13 12:10:55 +04:00
|
|
|
DEVMETHOD(device_probe, uhub_match),
|
|
|
|
DEVMETHOD(device_attach, uhub_attach),
|
|
|
|
|
|
|
|
/* detach is not allowed for a root hub */
|
|
|
|
{0,0}
|
|
|
|
};
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static driver_t uhubroot_driver = {
|
1999-10-13 12:10:55 +04:00
|
|
|
"uhub",
|
|
|
|
uhubroot_methods,
|
|
|
|
sizeof(struct uhub_softc)
|
|
|
|
};
|
1998-12-26 15:53:00 +03:00
|
|
|
#endif
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_MATCH(uhub)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_MATCH_START(uhub, uaa);
|
1998-07-12 23:51:55 +04:00
|
|
|
usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
|
2002-07-12 01:14:24 +04:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
DPRINTFN(5,("uhub_match, dd=%p\n", dd));
|
2002-07-12 01:14:24 +04:00
|
|
|
/*
|
1998-07-12 23:51:55 +04:00
|
|
|
* The subclass for hubs seems to be 0 for some and 1 for others,
|
|
|
|
* so we just ignore the subclass.
|
|
|
|
*/
|
2000-03-01 00:37:00 +03:00
|
|
|
if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
|
1998-07-12 23:51:55 +04:00
|
|
|
return (UMATCH_DEVCLASS_DEVSUBCLASS);
|
|
|
|
return (UMATCH_NONE);
|
|
|
|
}
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH(uhub)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_START(uhub, sc, uaa);
|
1998-07-12 23:51:55 +04:00
|
|
|
usbd_device_handle dev = uaa->device;
|
|
|
|
char devinfo[1024];
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-07-12 23:51:55 +04:00
|
|
|
struct usbd_hub *hub;
|
|
|
|
usb_device_request_t req;
|
|
|
|
usb_hub_descriptor_t hubdesc;
|
2000-04-21 20:05:50 +04:00
|
|
|
int p, port, nports, nremov, pwrdly;
|
1998-07-12 23:51:55 +04:00
|
|
|
usbd_interface_handle iface;
|
|
|
|
usb_endpoint_descriptor_t *ed;
|
2002-07-12 01:14:24 +04:00
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
DPRINTFN(1,("uhub_attach\n"));
|
|
|
|
sc->sc_hub = dev;
|
|
|
|
usbd_devinfo(dev, 1, devinfo);
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_SETUP;
|
|
|
|
printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_set_config_index(dev, 0, 1);
|
|
|
|
if (err) {
|
1999-08-19 23:51:36 +04:00
|
|
|
DPRINTF(("%s: configuration failed, error=%s\n",
|
1999-11-12 03:34:57 +03:00
|
|
|
USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_ERROR_RETURN;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->depth > USB_HUB_MAX_DEPTH) {
|
|
|
|
printf("%s: hub depth (%d) exceeded, hub ignored\n",
|
1998-12-26 15:53:00 +03:00
|
|
|
USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
|
|
|
|
USB_ATTACH_ERROR_RETURN;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get hub descriptor. */
|
|
|
|
req.bmRequestType = UT_READ_CLASS_DEVICE;
|
|
|
|
req.bRequest = UR_GET_DESCRIPTOR;
|
|
|
|
USETW(req.wValue, 0);
|
|
|
|
USETW(req.wIndex, 0);
|
|
|
|
USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
|
|
|
|
DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_do_request(dev, &req, &hubdesc);
|
1998-12-26 15:53:00 +03:00
|
|
|
nports = hubdesc.bNbrPorts;
|
1999-11-12 03:34:57 +03:00
|
|
|
if (!err && nports > 7) {
|
1998-12-26 15:53:00 +03:00
|
|
|
USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_do_request(dev, &req, &hubdesc);
|
1998-12-26 15:53:00 +03:00
|
|
|
}
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err) {
|
1999-08-19 23:51:36 +04:00
|
|
|
DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
|
1999-11-12 03:34:57 +03:00
|
|
|
USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_ERROR_RETURN;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
for (nremov = 0, port = 1; port <= nports; port++)
|
|
|
|
if (!UHD_NOT_REMOV(&hubdesc, port))
|
|
|
|
nremov++;
|
|
|
|
printf("%s: %d port%s with %d removable, %s powered\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
|
|
|
|
nremov, dev->self_powered ? "self" : "bus");
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
|
1999-06-30 10:44:22 +04:00
|
|
|
M_USBDEV, M_NOWAIT);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (hub == NULL)
|
1998-12-26 15:53:00 +03:00
|
|
|
USB_ATTACH_ERROR_RETURN;
|
1998-07-12 23:51:55 +04:00
|
|
|
dev->hub = hub;
|
1998-12-26 15:53:00 +03:00
|
|
|
dev->hub->hubsoftc = sc;
|
1998-07-12 23:51:55 +04:00
|
|
|
hub->explore = uhub_explore;
|
|
|
|
hub->hubdesc = hubdesc;
|
2002-07-12 01:14:24 +04:00
|
|
|
|
1998-12-09 22:24:28 +03:00
|
|
|
DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
|
|
|
|
"parent->selfpowered=%d\n",
|
1998-07-12 23:51:55 +04:00
|
|
|
dev->self_powered, dev->powersrc->parent,
|
2002-07-12 01:14:24 +04:00
|
|
|
dev->powersrc->parent ?
|
1998-07-12 23:51:55 +04:00
|
|
|
dev->powersrc->parent->self_powered : 0));
|
1999-10-13 12:10:55 +04:00
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
if (!dev->self_powered && dev->powersrc->parent != NULL &&
|
1998-07-12 23:51:55 +04:00
|
|
|
!dev->powersrc->parent->self_powered) {
|
1998-12-09 22:24:28 +03:00
|
|
|
printf("%s: bus powered hub connected to bus powered hub, "
|
1999-09-05 23:32:18 +04:00
|
|
|
"ignored\n", USBDEVNAME(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
goto bad;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up interrupt pipe. */
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_device2interface_handle(dev, 0, &iface);
|
|
|
|
if (err) {
|
1998-12-26 15:53:00 +03:00
|
|
|
printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
goto bad;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
ed = usbd_interface2endpoint_descriptor(iface, 0);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (ed == NULL) {
|
1998-12-26 15:53:00 +03:00
|
|
|
printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
goto bad;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
|
1998-12-26 15:53:00 +03:00
|
|
|
printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
goto bad;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
|
2002-07-12 01:14:24 +04:00
|
|
|
USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
|
2000-04-21 23:51:43 +04:00
|
|
|
sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err) {
|
2002-07-12 01:14:24 +04:00
|
|
|
printf("%s: cannot open interrupt pipe\n",
|
1998-12-26 15:53:00 +03:00
|
|
|
USBDEVNAME(sc->sc_dev));
|
1999-06-30 10:44:22 +04:00
|
|
|
goto bad;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
/* Wait with power off for a while. */
|
1998-12-30 21:06:25 +03:00
|
|
|
usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
|
1998-12-26 15:53:00 +03:00
|
|
|
|
2000-02-02 10:33:59 +03:00
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
|
|
|
|
|
2000-04-21 20:05:50 +04:00
|
|
|
/*
|
|
|
|
* To have the best chance of success we do things in the exact same
|
|
|
|
* order as Windoze98. This should not be necessary, but some
|
|
|
|
* devices do not follow the USB specs to the letter.
|
|
|
|
*
|
|
|
|
* These are the events on the bus when a hub is attached:
|
|
|
|
* Get device and config descriptors (see attach code)
|
|
|
|
* Get hub descriptor (see above)
|
|
|
|
* For all ports
|
|
|
|
* turn on power
|
|
|
|
* wait for power to become stable
|
|
|
|
* (all below happens in explore code)
|
|
|
|
* For all ports
|
|
|
|
* clear C_PORT_CONNECTION
|
|
|
|
* For all ports
|
|
|
|
* get port status
|
|
|
|
* if device connected
|
2001-11-20 19:08:37 +03:00
|
|
|
* wait 100 ms
|
2000-04-21 20:05:50 +04:00
|
|
|
* turn on reset
|
|
|
|
* wait
|
|
|
|
* clear C_PORT_RESET
|
|
|
|
* get port status
|
|
|
|
* proceed with device attachment
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Set up data structures */
|
1998-12-26 15:53:00 +03:00
|
|
|
for (p = 0; p < nports; p++) {
|
|
|
|
struct usbd_port *up = &hub->ports[p];
|
|
|
|
up->device = 0;
|
|
|
|
up->parent = dev;
|
|
|
|
up->portno = p+1;
|
2000-04-21 20:05:50 +04:00
|
|
|
if (dev->self_powered)
|
|
|
|
/* Self powered hub, give ports maximum current. */
|
|
|
|
up->power = USB_MAX_POWER;
|
|
|
|
else
|
|
|
|
up->power = USB_MIN_POWER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX should check for none, individual, or ganged power? */
|
|
|
|
|
|
|
|
pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
|
|
|
|
+ USB_EXTRA_POWER_UP_TIME;
|
|
|
|
for (port = 1; port <= nports; port++) {
|
|
|
|
/* Turn the power on. */
|
|
|
|
err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err)
|
2002-07-12 01:14:24 +04:00
|
|
|
printf("%s: port %d power on failed, %s\n",
|
2000-04-21 20:05:50 +04:00
|
|
|
USBDEVNAME(sc->sc_dev), port,
|
|
|
|
usbd_errstr(err));
|
|
|
|
DPRINTF(("usb_init_port: turn on port %d power\n", port));
|
|
|
|
/* Wait for stable power. */
|
|
|
|
usbd_delay_ms(dev, pwrdly);
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
2000-04-21 20:05:50 +04:00
|
|
|
|
|
|
|
/* The usual exploration will finish the setup. */
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
sc->sc_running = 1;
|
1998-12-26 15:53:00 +03:00
|
|
|
|
|
|
|
USB_ATTACH_SUCCESS_RETURN;
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
bad:
|
|
|
|
free(hub, M_USBDEV);
|
|
|
|
dev->hub = 0;
|
|
|
|
USB_ATTACH_ERROR_RETURN;
|
1998-12-26 15:53:00 +03:00
|
|
|
}
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
usbd_status
|
2000-06-01 18:28:57 +04:00
|
|
|
uhub_explore(usbd_device_handle dev)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
|
|
|
usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
|
1998-12-26 15:53:00 +03:00
|
|
|
struct uhub_softc *sc = dev->hub->hubsoftc;
|
1998-07-12 23:51:55 +04:00
|
|
|
struct usbd_port *up;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
2001-11-20 16:48:03 +03:00
|
|
|
int speed;
|
1998-07-12 23:51:55 +04:00
|
|
|
int port;
|
|
|
|
int change, status;
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
if (!sc->sc_running)
|
|
|
|
return (USBD_NOT_STARTED);
|
|
|
|
|
|
|
|
/* Ignore hubs that are too deep. */
|
|
|
|
if (dev->depth > USB_HUB_MAX_DEPTH)
|
|
|
|
return (USBD_TOO_DEEP);
|
|
|
|
|
|
|
|
for(port = 1; port <= hd->bNbrPorts; port++) {
|
|
|
|
up = &dev->hub->ports[port-1];
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usbd_get_port_status(dev, port, &up->status);
|
|
|
|
if (err) {
|
1998-12-09 22:24:28 +03:00
|
|
|
DPRINTF(("uhub_explore: get port status failed, "
|
1999-11-12 03:34:57 +03:00
|
|
|
"error=%s\n", usbd_errstr(err)));
|
1998-07-12 23:51:55 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
status = UGETW(up->status.wPortStatus);
|
|
|
|
change = UGETW(up->status.wPortChange);
|
2001-10-24 19:30:17 +04:00
|
|
|
DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port, status, change));
|
1998-12-26 15:53:00 +03:00
|
|
|
if (change & UPS_C_PORT_ENABLED) {
|
1999-11-25 02:13:19 +03:00
|
|
|
DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
|
1998-12-26 15:53:00 +03:00
|
|
|
usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
|
|
|
|
if (status & UPS_PORT_ENABLED) {
|
|
|
|
printf("%s: illegal enable change, port %d\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
|
|
|
} else {
|
|
|
|
/* Port error condition. */
|
2000-09-24 06:08:38 +04:00
|
|
|
if (up->restartcnt) /* no message first time */
|
1998-12-26 15:53:00 +03:00
|
|
|
printf("%s: port error, restarting "
|
|
|
|
"port %d\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
2000-09-24 06:08:38 +04:00
|
|
|
|
|
|
|
if (up->restartcnt++ < USBD_RESTART_MAX)
|
1998-12-26 15:53:00 +03:00
|
|
|
goto disco;
|
2000-09-24 06:08:38 +04:00
|
|
|
else
|
1998-12-26 15:53:00 +03:00
|
|
|
printf("%s: port error, giving up "
|
|
|
|
"port %d\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(change & UPS_C_CONNECT_STATUS)) {
|
1999-11-25 02:13:19 +03:00
|
|
|
DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
|
|
|
|
"STATUS\n", port));
|
1998-07-12 23:51:55 +04:00
|
|
|
/* No status change, just do recursive explore. */
|
2001-10-26 21:53:59 +04:00
|
|
|
if (up->device != NULL && up->device->hub != NULL)
|
1998-12-26 15:53:00 +03:00
|
|
|
up->device->hub->explore(up->device);
|
2001-10-26 21:53:59 +04:00
|
|
|
#if 0 && defined(DIAGNOSTIC)
|
2002-07-12 01:14:24 +04:00
|
|
|
if (up->device == NULL &&
|
2001-10-26 21:53:59 +04:00
|
|
|
(status & UPS_CURRENT_CONNECT_STATUS))
|
|
|
|
printf("%s: connected, no device\n",
|
|
|
|
USBDEVNAME(sc->sc_dev));
|
|
|
|
#endif
|
1998-07-12 23:51:55 +04:00
|
|
|
continue;
|
|
|
|
}
|
2000-04-21 20:05:50 +04:00
|
|
|
|
|
|
|
/* We have a connect status change, handle it. */
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
|
|
|
|
dev->address, port));
|
|
|
|
usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
|
2000-04-21 20:05:50 +04:00
|
|
|
/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
|
1998-07-12 23:51:55 +04:00
|
|
|
/*
|
|
|
|
* If there is already a device on the port the change status
|
|
|
|
* must mean that is has disconnected. Looking at the
|
|
|
|
* current connect status is not enough to figure this out
|
|
|
|
* since a new unit may have been connected before we handle
|
|
|
|
* the disconnect.
|
|
|
|
*/
|
1998-12-26 15:53:00 +03:00
|
|
|
disco:
|
1999-11-12 03:34:57 +03:00
|
|
|
if (up->device != NULL) {
|
1998-07-12 23:51:55 +04:00
|
|
|
/* Disconnected */
|
1999-11-25 02:13:19 +03:00
|
|
|
DPRINTF(("uhub_explore: device addr=%d disappeared "
|
1999-10-13 00:02:47 +04:00
|
|
|
"on port %d\n", up->device->address, port));
|
|
|
|
usb_disconnect_port(up, USBDEV(sc->sc_dev));
|
2002-07-12 01:14:24 +04:00
|
|
|
usbd_clear_port_feature(dev, port,
|
1998-07-12 23:51:55 +04:00
|
|
|
UHF_C_PORT_CONNECTION);
|
|
|
|
}
|
1999-11-25 02:13:19 +03:00
|
|
|
if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
|
2000-04-21 20:05:50 +04:00
|
|
|
/* Nothing connected, just ignore it. */
|
1999-11-25 02:13:19 +03:00
|
|
|
DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
|
|
|
|
"_STATUS\n", port));
|
1998-07-12 23:51:55 +04:00
|
|
|
continue;
|
1999-11-25 02:13:19 +03:00
|
|
|
}
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
/* Connected */
|
2000-04-21 20:05:50 +04:00
|
|
|
|
|
|
|
if (!(status & UPS_PORT_POWER))
|
|
|
|
printf("%s: strange, connected port %d has no power\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
/* Wait for maximum device power up time. */
|
1998-12-30 21:06:25 +03:00
|
|
|
usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
|
1998-12-26 15:53:00 +03:00
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
/* Reset port, which implies enabling it. */
|
1999-11-25 02:13:19 +03:00
|
|
|
if (usbd_reset_port(dev, port, &up->status)) {
|
2001-11-16 05:21:54 +03:00
|
|
|
printf("%s: port %d reset failed\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
1998-07-12 23:51:55 +04:00
|
|
|
continue;
|
1999-11-25 02:13:19 +03:00
|
|
|
}
|
2001-11-16 04:57:47 +03:00
|
|
|
/* Get port status again, it might have changed during reset */
|
|
|
|
err = usbd_get_port_status(dev, port, &up->status);
|
|
|
|
if (err) {
|
|
|
|
DPRINTF(("uhub_explore: get port status failed, "
|
|
|
|
"error=%s\n", usbd_errstr(err)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
status = UGETW(up->status.wPortStatus);
|
|
|
|
change = UGETW(up->status.wPortChange);
|
|
|
|
if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
|
|
|
|
/* Nothing connected, just ignore it. */
|
|
|
|
#ifdef DIAGNOSTIC
|
2001-11-20 16:48:03 +03:00
|
|
|
printf("%s: port %d, device disappeared after reset\n",
|
2001-11-16 04:57:47 +03:00
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
1998-07-12 23:51:55 +04:00
|
|
|
|
2001-11-20 16:48:03 +03:00
|
|
|
/* Figure out device speed */
|
|
|
|
if (status & UPS_HIGH_SPEED)
|
|
|
|
speed = USB_SPEED_HIGH;
|
|
|
|
else if (status & UPS_LOW_SPEED)
|
|
|
|
speed = USB_SPEED_LOW;
|
|
|
|
else
|
|
|
|
speed = USB_SPEED_FULL;
|
1998-07-12 23:51:55 +04:00
|
|
|
/* Get device info and set its address. */
|
2002-07-12 01:14:24 +04:00
|
|
|
err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
|
2001-11-20 16:48:03 +03:00
|
|
|
dev->depth + 1, speed, port, up);
|
1998-07-12 23:51:55 +04:00
|
|
|
/* XXX retry a few times? */
|
1999-11-12 03:34:57 +03:00
|
|
|
if (err) {
|
1998-12-09 22:24:28 +03:00
|
|
|
DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
|
1999-11-12 03:34:57 +03:00
|
|
|
"error=%s\n", usbd_errstr(err)));
|
1998-07-12 23:51:55 +04:00
|
|
|
/* Avoid addressing problems by disabling. */
|
|
|
|
/* usbd_reset_port(dev, port, &up->status); */
|
1999-10-11 13:15:34 +04:00
|
|
|
|
2002-07-12 01:14:24 +04:00
|
|
|
/*
|
1999-10-11 13:15:34 +04:00
|
|
|
* The unit refused to accept a new address, or had
|
|
|
|
* some other serious problem. Since we cannot leave
|
|
|
|
* at 0 we have to disable the port instead.
|
|
|
|
*/
|
|
|
|
printf("%s: device problem, disabling port %d\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
|
|
|
usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
|
1998-07-12 23:51:55 +04:00
|
|
|
} else {
|
2000-09-24 06:08:38 +04:00
|
|
|
/* The port set up succeeded, reset error count. */
|
|
|
|
up->restartcnt = 0;
|
|
|
|
|
1999-11-19 02:32:25 +03:00
|
|
|
if (up->device->hub)
|
1998-12-26 15:53:00 +03:00
|
|
|
up->device->hub->explore(up->device);
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (USBD_NORMAL_COMPLETION);
|
|
|
|
}
|
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1999-06-30 10:44:22 +04:00
|
|
|
int
|
2000-06-01 18:28:57 +04:00
|
|
|
uhub_activate(device_ptr_t self, enum devact act)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
1999-09-12 12:23:42 +04:00
|
|
|
struct uhub_softc *sc = (struct uhub_softc *)self;
|
2000-02-13 02:44:16 +03:00
|
|
|
struct usbd_hub *hub = sc->sc_hub->hub;
|
1999-11-19 02:32:25 +03:00
|
|
|
usbd_device_handle dev;
|
|
|
|
int nports, port, i;
|
1999-09-12 12:23:42 +04:00
|
|
|
|
1999-08-24 02:55:14 +04:00
|
|
|
switch (act) {
|
|
|
|
case DVACT_ACTIVATE:
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
|
|
|
case DVACT_DEACTIVATE:
|
2000-02-13 02:44:16 +03:00
|
|
|
if (hub == NULL) /* malfunctioning hub */
|
|
|
|
break;
|
|
|
|
nports = hub->hubdesc.bNbrPorts;
|
1999-11-19 02:32:25 +03:00
|
|
|
for(port = 0; port < nports; port++) {
|
2000-02-13 02:44:16 +03:00
|
|
|
dev = hub->ports[port].device;
|
2000-09-22 06:34:58 +04:00
|
|
|
if (dev != NULL && dev->subdevs != NULL) {
|
|
|
|
for (i = 0; dev->subdevs[i] != NULL; i++)
|
1999-09-12 12:23:42 +04:00
|
|
|
config_deactivate(dev->subdevs[i]);
|
|
|
|
}
|
|
|
|
}
|
1999-08-24 02:55:14 +04:00
|
|
|
break;
|
|
|
|
}
|
1999-06-30 10:44:22 +04:00
|
|
|
return (0);
|
|
|
|
}
|
1999-10-13 12:10:55 +04:00
|
|
|
#endif
|
1999-06-30 10:44:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from process context when the hub is gone.
|
|
|
|
* Detach all devices on active ports.
|
|
|
|
*/
|
1999-10-13 12:10:55 +04:00
|
|
|
USB_DETACH(uhub)
|
1999-06-30 10:44:22 +04:00
|
|
|
{
|
1999-10-13 12:10:55 +04:00
|
|
|
USB_DETACH_START(uhub, sc);
|
2000-02-13 02:44:16 +03:00
|
|
|
struct usbd_hub *hub = sc->sc_hub->hub;
|
1999-06-30 10:44:22 +04:00
|
|
|
struct usbd_port *rup;
|
1999-10-13 12:10:55 +04:00
|
|
|
int port, nports;
|
1999-06-30 10:44:22 +04:00
|
|
|
|
1999-10-13 12:10:55 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1999-06-30 10:44:22 +04:00
|
|
|
DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
|
1999-10-13 12:10:55 +04:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
DPRINTF(("uhub_detach: sc=%port\n", sc));
|
|
|
|
#endif
|
1999-06-30 10:44:22 +04:00
|
|
|
|
2000-02-13 02:44:16 +03:00
|
|
|
if (hub == NULL) /* Must be partially working */
|
1999-06-30 10:44:22 +04:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
usbd_abort_pipe(sc->sc_ipipe);
|
|
|
|
usbd_close_pipe(sc->sc_ipipe);
|
|
|
|
|
2000-02-13 02:44:16 +03:00
|
|
|
nports = hub->hubdesc.bNbrPorts;
|
1999-10-13 12:10:55 +04:00
|
|
|
for(port = 0; port < nports; port++) {
|
2000-02-13 02:44:16 +03:00
|
|
|
rup = &hub->ports[port];
|
1999-06-30 10:44:22 +04:00
|
|
|
if (rup->device)
|
1999-10-13 00:02:47 +04:00
|
|
|
usb_disconnect_port(rup, self);
|
1999-06-30 10:44:22 +04:00
|
|
|
}
|
2002-07-12 01:14:24 +04:00
|
|
|
|
2000-02-13 02:44:16 +03:00
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
|
|
|
|
USBDEV(sc->sc_dev));
|
2000-02-02 10:33:59 +03:00
|
|
|
|
2000-02-13 02:44:16 +03:00
|
|
|
free(hub, M_USBDEV);
|
|
|
|
sc->sc_hub->hub = NULL;
|
1999-06-30 10:44:22 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-11-19 02:32:25 +03:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
/* Called when a device has been detached from it */
|
2000-03-27 16:33:53 +04:00
|
|
|
Static void
|
2000-06-01 18:28:57 +04:00
|
|
|
uhub_child_detached(device_t self, device_t child)
|
1999-11-19 02:32:25 +03:00
|
|
|
{
|
|
|
|
struct uhub_softc *sc = device_get_softc(self);
|
|
|
|
usbd_device_handle devhub = sc->sc_hub;
|
|
|
|
usbd_device_handle dev;
|
|
|
|
int nports;
|
|
|
|
int port;
|
|
|
|
int i;
|
|
|
|
|
2002-07-12 01:14:24 +04:00
|
|
|
if (!devhub->hub)
|
1999-11-19 02:32:25 +03:00
|
|
|
/* should never happen; children are only created after init */
|
|
|
|
panic("hub not fully initialised, but child deleted?");
|
|
|
|
|
|
|
|
nports = devhub->hub->hubdesc.bNbrPorts;
|
|
|
|
for (port = 0; port < nports; port++) {
|
|
|
|
dev = devhub->hub->ports[port].device;
|
|
|
|
if (dev && dev->subdevs) {
|
|
|
|
for (i = 0; dev->subdevs[i]; i++) {
|
|
|
|
if (dev->subdevs[i] == child) {
|
|
|
|
dev->subdevs[i] = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
/*
|
|
|
|
* Hub interrupt.
|
|
|
|
* This an indication that some port has changed status.
|
|
|
|
* Notify the bus event handler thread that we need
|
|
|
|
* to be explored again.
|
|
|
|
*/
|
1998-07-12 23:51:55 +04:00
|
|
|
void
|
2000-06-01 18:28:57 +04:00
|
|
|
uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
|
|
|
struct uhub_softc *sc = addr;
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
|
2001-09-29 03:42:16 +04:00
|
|
|
if (status == USBD_STALLED)
|
1998-12-09 04:01:24 +03:00
|
|
|
usbd_clear_endpoint_stall_async(sc->sc_ipipe);
|
2001-09-29 03:42:16 +04:00
|
|
|
else if (status == USBD_NORMAL_COMPLETION)
|
|
|
|
usb_needs_explore(sc->sc_hub);
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
1998-12-26 15:53:00 +03:00
|
|
|
|
|
|
|
#if defined(__FreeBSD__)
|
1999-08-14 18:49:31 +04:00
|
|
|
DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
|
|
|
|
DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
|
1998-12-26 15:53:00 +03:00
|
|
|
#endif
|