1999-09-02 22:13:50 +04:00
|
|
|
/* $NetBSD: uhub.c,v 1.25 1999/09/02 18:13:50 augustss 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
|
|
|
|
* by Lennart Augustsson (augustss@carlstedt.se) at
|
|
|
|
* 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
|
|
|
/*
|
|
|
|
* USB spec: http://www.usb.org/cgi-usb/mailmerge.cgi/home/usb/docs/developers/cgiform.tpl
|
|
|
|
*/
|
|
|
|
|
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>
|
1998-12-26 15:53:00 +03:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#endif
|
1998-07-12 23:51:55 +04:00
|
|
|
#include <sys/proc.h>
|
|
|
|
|
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <dev/usb/usbdi_util.h>
|
|
|
|
#include <dev/usb/usbdivar.h>
|
|
|
|
|
|
|
|
#ifdef USB_DEBUG
|
1999-08-14 18:49:31 +04:00
|
|
|
#define DPRINTF(x) if (usbdebug) logprintf x
|
|
|
|
#define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
|
1998-07-12 23:51:55 +04:00
|
|
|
extern int usbdebug;
|
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct uhub_softc {
|
1998-12-26 15:53:00 +03:00
|
|
|
bdevice sc_dev; /* base device */
|
|
|
|
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
|
|
|
};
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
usbd_status uhub_init_port __P((struct usbd_port *));
|
1999-06-30 10:44:22 +04:00
|
|
|
void uhub_disconnect_port __P((struct usbd_port *up));
|
1998-12-26 15:53:00 +03:00
|
|
|
usbd_status uhub_explore __P((usbd_device_handle hub));
|
1998-07-12 23:51:55 +04:00
|
|
|
void uhub_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
|
|
|
|
|
1999-01-08 14:58:25 +03:00
|
|
|
USB_DECLARE_DRIVER_NAME(usb, uhub);
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-08-17 20:06:20 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
1998-07-12 23:51:55 +04:00
|
|
|
struct cfattach uhub_uhub_ca = {
|
1999-06-30 10:44:22 +04:00
|
|
|
sizeof(struct uhub_softc), uhub_match, uhub_attach,
|
|
|
|
uhub_detach, uhub_activate
|
1998-07-12 23:51:55 +04:00
|
|
|
};
|
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);
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
DPRINTFN(5,("uhub_match, dd=%p\n", dd));
|
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.
|
|
|
|
*/
|
|
|
|
if (uaa->iface == 0 && dd->bDeviceClass == UCLASS_HUB)
|
|
|
|
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];
|
|
|
|
usbd_status r;
|
|
|
|
struct usbd_hub *hub;
|
|
|
|
usb_device_request_t req;
|
|
|
|
usb_hub_descriptor_t hubdesc;
|
1998-12-26 15:53:00 +03:00
|
|
|
int p, port, nports, nremov;
|
1998-07-12 23:51:55 +04:00
|
|
|
usbd_interface_handle iface;
|
|
|
|
usb_endpoint_descriptor_t *ed;
|
|
|
|
|
|
|
|
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
|
|
|
|
1998-12-08 18:48:18 +03:00
|
|
|
r = usbd_set_config_index(dev, 0, 1);
|
1998-07-12 23:51:55 +04:00
|
|
|
if (r != USBD_NORMAL_COMPLETION) {
|
1999-08-19 23:51:36 +04:00
|
|
|
DPRINTF(("%s: configuration failed, error=%s\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), usbd_errstr(r)));
|
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"));
|
|
|
|
r = usbd_do_request(dev, &req, &hubdesc);
|
1998-12-26 15:53:00 +03:00
|
|
|
nports = hubdesc.bNbrPorts;
|
|
|
|
if (r == USBD_NORMAL_COMPLETION && nports > 7) {
|
|
|
|
USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
|
|
|
|
r = usbd_do_request(dev, &req, &hubdesc);
|
|
|
|
}
|
1998-07-12 23:51:55 +04:00
|
|
|
if (r != USBD_NORMAL_COMPLETION) {
|
1999-08-19 23:51:36 +04:00
|
|
|
DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), usbd_errstr(r)));
|
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);
|
1998-07-12 23:51:55 +04:00
|
|
|
if (hub == 0)
|
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;
|
|
|
|
|
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,
|
|
|
|
dev->powersrc->parent ?
|
|
|
|
dev->powersrc->parent->self_powered : 0));
|
|
|
|
if (!dev->self_powered && dev->powersrc->parent &&
|
|
|
|
!dev->powersrc->parent->self_powered) {
|
1998-12-09 22:24:28 +03:00
|
|
|
printf("%s: bus powered hub connected to bus powered hub, "
|
|
|
|
"ignored\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
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up interrupt pipe. */
|
|
|
|
r = usbd_device2interface_handle(dev, 0, &iface);
|
|
|
|
if (r != USBD_NORMAL_COMPLETION) {
|
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);
|
|
|
|
if (ed == 0) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
r = usbd_open_pipe_intr(iface, ed->bEndpointAddress,USBD_SHORT_XFER_OK,
|
|
|
|
&sc->sc_ipipe, sc, sc->sc_status,
|
|
|
|
sizeof(sc->sc_status),
|
|
|
|
uhub_intr);
|
|
|
|
if (r != USBD_NORMAL_COMPLETION) {
|
1998-12-26 15:53:00 +03:00
|
|
|
printf("%s: cannot open interrupt pipe\n",
|
|
|
|
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
|
|
|
|
|
|
|
for (p = 0; p < nports; p++) {
|
|
|
|
struct usbd_port *up = &hub->ports[p];
|
|
|
|
up->device = 0;
|
|
|
|
up->parent = dev;
|
|
|
|
up->portno = p+1;
|
|
|
|
r = uhub_init_port(up);
|
1998-07-12 23:51:55 +04:00
|
|
|
if (r != USBD_NORMAL_COMPLETION)
|
|
|
|
printf("%s: init of port %d failed\n",
|
1998-12-26 15:53:00 +03:00
|
|
|
USBDEVNAME(sc->sc_dev), up->portno);
|
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
|
1998-12-26 15:53:00 +03:00
|
|
|
uhub_init_port(up)
|
|
|
|
struct usbd_port *up;
|
1998-07-12 23:51:55 +04:00
|
|
|
{
|
1998-12-26 15:53:00 +03:00
|
|
|
int port = up->portno;
|
|
|
|
usbd_device_handle dev = up->parent;
|
1998-07-12 23:51:55 +04:00
|
|
|
usbd_status r;
|
|
|
|
u_int16_t pstatus;
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
r = usbd_get_port_status(dev, port, &up->status);
|
1998-07-12 23:51:55 +04:00
|
|
|
if (r != USBD_NORMAL_COMPLETION)
|
1998-12-26 15:53:00 +03:00
|
|
|
return (r);
|
|
|
|
pstatus = UGETW(up->status.wPortStatus);
|
1998-12-09 22:24:28 +03:00
|
|
|
DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x "
|
|
|
|
"change=0x%04x\n",
|
1998-12-26 15:53:00 +03:00
|
|
|
port, pstatus, UGETW(up->status.wPortChange)));
|
1998-07-12 23:51:55 +04:00
|
|
|
if ((pstatus & UPS_PORT_POWER) == 0) {
|
|
|
|
/* Port lacks power, turn it on */
|
1998-12-28 05:23:25 +03:00
|
|
|
|
1998-12-08 18:48:18 +03:00
|
|
|
/* First let the device go through a good power cycle, */
|
1998-12-30 21:06:25 +03:00
|
|
|
usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME);
|
1998-12-28 05:23:25 +03:00
|
|
|
|
1999-08-14 18:49:31 +04:00
|
|
|
#if 0
|
|
|
|
usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
|
|
|
|
usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
|
|
|
|
#endif
|
|
|
|
|
1998-12-08 18:48:18 +03:00
|
|
|
/* then turn the power on. */
|
1998-07-12 23:51:55 +04:00
|
|
|
r = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
|
|
|
|
if (r != USBD_NORMAL_COMPLETION)
|
|
|
|
return (r);
|
1998-12-09 22:24:28 +03:00
|
|
|
DPRINTF(("usb_init_port: turn on port %d power status=0x%04x "
|
|
|
|
"change=0x%04x\n",
|
1998-12-26 15:53:00 +03:00
|
|
|
port, UGETW(up->status.wPortStatus),
|
|
|
|
UGETW(up->status.wPortChange)));
|
1998-07-12 23:51:55 +04:00
|
|
|
/* Wait for stable power. */
|
1998-12-30 21:06:25 +03:00
|
|
|
usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood *
|
|
|
|
UHD_PWRON_FACTOR);
|
1999-06-14 20:59:47 +04:00
|
|
|
/* Get the port status again. */
|
|
|
|
r = usbd_get_port_status(dev, port, &up->status);
|
|
|
|
if (r != USBD_NORMAL_COMPLETION)
|
|
|
|
return (r);
|
1999-08-14 18:49:31 +04:00
|
|
|
DPRINTF(("usb_init_port: after power on status=0x%04x "
|
|
|
|
"change=0x%04x\n",
|
|
|
|
UGETW(up->status.wPortStatus),
|
|
|
|
UGETW(up->status.wPortChange)));
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
|
|
|
|
usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
|
|
|
|
usbd_get_port_status(dev, port, &up->status);
|
|
|
|
#endif
|
|
|
|
|
1999-06-14 20:59:47 +04:00
|
|
|
pstatus = UGETW(up->status.wPortStatus);
|
|
|
|
if ((pstatus & UPS_PORT_POWER) == 0)
|
|
|
|
printf("%s: port %d did not power up\n",
|
|
|
|
USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port);
|
|
|
|
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
if (dev->self_powered)
|
|
|
|
/* Self powered hub, give ports maximum current. */
|
1998-12-26 15:53:00 +03:00
|
|
|
up->power = USB_MAX_POWER;
|
1998-07-12 23:51:55 +04:00
|
|
|
else
|
1998-12-26 15:53:00 +03:00
|
|
|
up->power = USB_MIN_POWER;
|
1998-07-12 23:51:55 +04:00
|
|
|
return (USBD_NORMAL_COMPLETION);
|
|
|
|
}
|
|
|
|
|
|
|
|
usbd_status
|
1998-12-26 15:53:00 +03:00
|
|
|
uhub_explore(dev)
|
1998-07-12 23:51:55 +04:00
|
|
|
usbd_device_handle dev;
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
usbd_status r;
|
|
|
|
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];
|
|
|
|
r = usbd_get_port_status(dev, port, &up->status);
|
|
|
|
if (r != USBD_NORMAL_COMPLETION) {
|
1998-12-09 22:24:28 +03:00
|
|
|
DPRINTF(("uhub_explore: get port status failed, "
|
1999-08-19 23:51:36 +04:00
|
|
|
"error=%s\n",
|
|
|
|
usbd_errstr(r)));
|
1998-07-12 23:51:55 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
status = UGETW(up->status.wPortStatus);
|
|
|
|
change = UGETW(up->status.wPortChange);
|
|
|
|
DPRINTFN(5, ("uhub_explore: port %d status 0x%04x 0x%04x\n",
|
|
|
|
port, status, change));
|
1998-12-26 15:53:00 +03:00
|
|
|
if (change & UPS_C_PORT_ENABLED) {
|
|
|
|
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. */
|
|
|
|
if (up->restartcnt++ < USBD_RESTART_MAX) {
|
|
|
|
printf("%s: port error, restarting "
|
|
|
|
"port %d\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
|
|
|
goto disco;
|
|
|
|
} else {
|
|
|
|
printf("%s: port error, giving up "
|
|
|
|
"port %d\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(change & UPS_C_CONNECT_STATUS)) {
|
1998-07-12 23:51:55 +04:00
|
|
|
/* No status change, just do recursive explore. */
|
|
|
|
if (up->device && 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
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
|
|
|
|
dev->address, port));
|
|
|
|
usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
|
|
|
|
usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
|
|
|
|
/*
|
|
|
|
* 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:
|
1998-07-12 23:51:55 +04:00
|
|
|
if (up->device) {
|
|
|
|
/* Disconnected */
|
1998-12-09 22:24:28 +03:00
|
|
|
DPRINTF(("uhub_explore: device %d disappeared "
|
|
|
|
"on port %d\n",
|
1998-07-12 23:51:55 +04:00
|
|
|
up->device->address, port));
|
1999-06-30 10:44:22 +04:00
|
|
|
uhub_disconnect_port(up);
|
1998-07-12 23:51:55 +04:00
|
|
|
usbd_clear_port_feature(dev, port,
|
|
|
|
UHF_C_PORT_CONNECTION);
|
|
|
|
}
|
|
|
|
if (!(status & UPS_CURRENT_CONNECT_STATUS))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Connected */
|
1998-12-26 15:53:00 +03:00
|
|
|
up->restartcnt = 0;
|
|
|
|
|
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. */
|
|
|
|
if (usbd_reset_port(dev, port, &up->status) !=
|
|
|
|
USBD_NORMAL_COMPLETION)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Get device info and set its address. */
|
1998-12-26 15:53:00 +03:00
|
|
|
r = usbd_new_device(&sc->sc_dev, dev->bus,
|
1998-07-12 23:51:55 +04:00
|
|
|
dev->depth + 1, status & UPS_LOW_SPEED,
|
|
|
|
port, up);
|
|
|
|
/* XXX retry a few times? */
|
|
|
|
if (r != USBD_NORMAL_COMPLETION) {
|
1998-12-09 22:24:28 +03:00
|
|
|
DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
|
1999-08-19 23:51:36 +04:00
|
|
|
"error=%s\n", usbd_errstr(r)));
|
1998-07-12 23:51:55 +04:00
|
|
|
/* Avoid addressing problems by disabling. */
|
|
|
|
/* usbd_reset_port(dev, port, &up->status); */
|
|
|
|
/* XXX
|
|
|
|
* What should we do. The device may or may not be at its
|
|
|
|
* assigned address. In any case we'd like to ignore it.
|
|
|
|
* Maybe the port should be disabled until the device is
|
|
|
|
* disconnected.
|
|
|
|
*/
|
|
|
|
if (r == USBD_SET_ADDR_FAILED || 1) {/* XXX */
|
|
|
|
/* The unit refused to accept a new
|
|
|
|
* address, and since we cannot leave
|
|
|
|
* at 0 we have to disable the port
|
|
|
|
* instead. */
|
1998-12-09 22:24:28 +03:00
|
|
|
printf("%s: device problem, disabling "
|
|
|
|
"port %d\n",
|
1998-12-26 15:53:00 +03:00
|
|
|
USBDEVNAME(sc->sc_dev), port);
|
1998-07-12 23:51:55 +04:00
|
|
|
usbd_clear_port_feature(dev, port,
|
|
|
|
UHF_PORT_ENABLE);
|
1998-12-26 15:53:00 +03:00
|
|
|
/* Make sure we don't try to restart it. */
|
|
|
|
up->restartcnt = USBD_RESTART_MAX;
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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-06-30 10:44:22 +04:00
|
|
|
/*
|
|
|
|
* The general mechanism for detaching drivers works as follows: Each
|
|
|
|
* driver is responsible for maintaining a reference count on the
|
|
|
|
* number of outstanding references to its softc (e.g. from
|
|
|
|
* processing hanging in a read or write). The detach method of the
|
|
|
|
* driver decrements this counter and flags in the softc that the
|
|
|
|
* driver is dying and then wakes any sleepers. It then sleeps on the
|
|
|
|
* softc. Each place that can sleep must maintain the reference
|
|
|
|
* count. When the reference count drops to -1 (0 is the normal value
|
|
|
|
* of the reference count) the a wakeup on the softc is performed
|
|
|
|
* signaling to the detach waiter that all references are gone.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from process context when we discover that a port has
|
|
|
|
* been disconnected.
|
|
|
|
*/
|
1998-07-12 23:51:55 +04:00
|
|
|
void
|
1999-06-30 10:44:22 +04:00
|
|
|
uhub_disconnect_port(up)
|
1998-07-12 23:51:55 +04:00
|
|
|
struct usbd_port *up;
|
|
|
|
{
|
|
|
|
usbd_device_handle dev = up->device;
|
1999-06-30 10:44:22 +04:00
|
|
|
char *hubname;
|
1998-12-26 15:53:00 +03:00
|
|
|
int i;
|
1998-07-12 23:51:55 +04:00
|
|
|
|
|
|
|
DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
|
1998-12-26 15:53:00 +03:00
|
|
|
up, dev, up->portno));
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
if (!dev->cdesc) {
|
|
|
|
/* Partially attached device, just drop it. */
|
|
|
|
dev->bus->devices[dev->address] = 0;
|
|
|
|
up->device = 0;
|
|
|
|
return;
|
|
|
|
}
|
1998-07-12 23:51:55 +04:00
|
|
|
|
1999-09-02 22:13:50 +04:00
|
|
|
if (dev->subdevs) {
|
|
|
|
hubname = USBDEVNAME(*up->parent->subdevs[0]);
|
|
|
|
for (i = 0; dev->subdevs[i]; i++) {
|
|
|
|
printf("%s: at %s port %d (addr %d) disconnected\n",
|
|
|
|
USBDEVNAME(*dev->subdevs[i]), hubname,
|
|
|
|
up->portno, dev->address);
|
|
|
|
config_detach(dev->subdevs[i], DETACH_FORCE);
|
|
|
|
}
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
dev->bus->devices[dev->address] = 0;
|
|
|
|
up->device = 0;
|
1999-06-30 10:44:22 +04:00
|
|
|
usb_free_device(dev);
|
|
|
|
|
1999-01-08 14:58:25 +03:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
device_delete_child(
|
|
|
|
device_get_parent(((struct softc *)dev->softc)->sc_dev),
|
|
|
|
((struct softc *)dev->softc)->sc_dev);
|
1998-12-26 15:53:00 +03:00
|
|
|
#endif
|
1998-07-12 23:51:55 +04:00
|
|
|
}
|
|
|
|
|
1999-06-30 10:44:22 +04:00
|
|
|
int
|
|
|
|
uhub_activate(self, act)
|
1999-08-29 01:42:35 +04:00
|
|
|
bdevice *self;
|
1999-06-30 10:44:22 +04:00
|
|
|
enum devact act;
|
|
|
|
{
|
1999-08-24 02:55:14 +04:00
|
|
|
switch (act) {
|
|
|
|
case DVACT_ACTIVATE:
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
break;
|
|
|
|
}
|
1999-06-30 10:44:22 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from process context when the hub is gone.
|
|
|
|
* Detach all devices on active ports.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
uhub_detach(self, flags)
|
1999-08-29 01:42:35 +04:00
|
|
|
bdevice *self;
|
1999-06-30 10:44:22 +04:00
|
|
|
int flags;
|
|
|
|
{
|
|
|
|
struct uhub_softc *sc = (struct uhub_softc *)self;
|
|
|
|
usbd_device_handle dev = sc->sc_hub;
|
|
|
|
struct usbd_port *rup;
|
|
|
|
int p, nports;
|
|
|
|
|
|
|
|
DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
|
|
|
|
|
|
|
|
if (!dev->hub) {
|
|
|
|
/* Must be partially working */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
usbd_abort_pipe(sc->sc_ipipe);
|
|
|
|
usbd_close_pipe(sc->sc_ipipe);
|
|
|
|
|
|
|
|
nports = dev->hub->hubdesc.bNbrPorts;
|
|
|
|
for(p = 0; p < nports; p++) {
|
|
|
|
rup = &dev->hub->ports[p];
|
|
|
|
if (rup->device)
|
|
|
|
uhub_disconnect_port(rup);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dev->hub, M_USBDEV);
|
|
|
|
dev->hub = 0;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
uhub_intr(reqh, addr, status)
|
|
|
|
usbd_request_handle reqh;
|
|
|
|
usbd_private_handle addr;
|
|
|
|
usbd_status status;
|
|
|
|
{
|
|
|
|
struct uhub_softc *sc = addr;
|
|
|
|
|
1998-12-26 15:53:00 +03:00
|
|
|
DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
|
1998-07-12 23:51:55 +04:00
|
|
|
if (status != USBD_NORMAL_COMPLETION)
|
1998-12-09 04:01:24 +03:00
|
|
|
usbd_clear_endpoint_stall_async(sc->sc_ipipe);
|
1999-06-30 10:44:22 +04:00
|
|
|
|
|
|
|
usb_needs_explore(sc->sc_hub->bus);
|
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
|