summaryrefslogtreecommitdiff
path: root/sys/dev/usb/uhub.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/dev/usb/uhub.c')
-rw-r--r--sys/dev/usb/uhub.c197
1 files changed, 120 insertions, 77 deletions
diff --git a/sys/dev/usb/uhub.c b/sys/dev/usb/uhub.c
index 517d48a29e3..4ba332ef49e 100644
--- a/sys/dev/usb/uhub.c
+++ b/sys/dev/usb/uhub.c
@@ -1,5 +1,5 @@
-/* $OpenBSD: uhub.c,v 1.6 1999/11/07 21:30:19 fgsch Exp $ */
-/* $NetBSD: uhub.c,v 1.32 1999/10/13 08:10:56 augustss Exp $ */
+/* $OpenBSD: uhub.c,v 1.7 2000/03/26 08:39:46 aaron Exp $ */
+/* $NetBSD: uhub.c,v 1.40 2000/02/29 21:37:01 augustss Exp $ */
/*
* Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -48,12 +48,12 @@
#include <sys/malloc.h>
#if defined(__NetBSD__) || defined(__OpenBSD__)
#include <sys/device.h>
+#include <sys/proc.h>
#elif defined(__FreeBSD__)
#include <sys/module.h>
#include <sys/bus.h>
#include "bus_if.h"
#endif
-#include <sys/proc.h>
#include <machine/bus.h>
@@ -63,9 +63,9 @@
#include <dev/usb/usbdivar.h>
#ifdef UHUB_DEBUG
-#define DPRINTF(x) if (usbdebug) logprintf x
-#define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
-extern int usbdebug;
+#define DPRINTF(x) if (uhubdebug) logprintf x
+#define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
+int uhubdebug;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
@@ -81,15 +81,12 @@ struct uhub_softc {
usbd_status uhub_init_port __P((struct usbd_port *));
usbd_status uhub_explore __P((usbd_device_handle hub));
-void uhub_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
+void uhub_intr __P((usbd_xfer_handle, usbd_private_handle, usbd_status));
#if defined(__FreeBSD__)
static bus_child_detached_t uhub_child_detached;
#endif
-USB_DECLARE_DRIVER_INIT(uhub,
- DEVMETHOD(bus_child_detached, uhub_child_detached));
-
/*
* We need two attachment points:
* hub to usb and hub to hub
@@ -97,12 +94,17 @@ USB_DECLARE_DRIVER_INIT(uhub,
*/
#if defined(__NetBSD__) || defined(__OpenBSD__)
+USB_DECLARE_DRIVER(uhub);
+
/* Create the driver instance for the hub connected to hub case */
struct cfattach uhub_uhub_ca = {
sizeof(struct uhub_softc), uhub_match, uhub_attach,
uhub_detach, uhub_activate
};
#elif defined(__FreeBSD__)
+USB_DECLARE_DRIVER_INIT(uhub.
+ DEVMETHOD(bus_child_detached, uhub_child_detached));
+
/* Create the driver instance for the hub connected to usb case. */
devclass_t uhubroot_devclass;
@@ -131,7 +133,7 @@ USB_MATCH(uhub)
* 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)
+ if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
return (UMATCH_DEVCLASS_DEVSUBCLASS);
return (UMATCH_NONE);
}
@@ -141,7 +143,7 @@ USB_ATTACH(uhub)
USB_ATTACH_START(uhub, sc, uaa);
usbd_device_handle dev = uaa->device;
char devinfo[1024];
- usbd_status r;
+ usbd_status err;
struct usbd_hub *hub;
usb_device_request_t req;
usb_hub_descriptor_t hubdesc;
@@ -155,10 +157,10 @@ USB_ATTACH(uhub)
USB_ATTACH_SETUP;
printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
- r = usbd_set_config_index(dev, 0, 1);
- if (r != USBD_NORMAL_COMPLETION) {
+ err = usbd_set_config_index(dev, 0, 1);
+ if (err) {
DPRINTF(("%s: configuration failed, error=%s\n",
- USBDEVNAME(sc->sc_dev), usbd_errstr(r)));
+ USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
USB_ATTACH_ERROR_RETURN;
}
@@ -175,15 +177,15 @@ USB_ATTACH(uhub)
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);
+ err = usbd_do_request(dev, &req, &hubdesc);
nports = hubdesc.bNbrPorts;
- if (r == USBD_NORMAL_COMPLETION && nports > 7) {
+ if (!err && nports > 7) {
USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
- r = usbd_do_request(dev, &req, &hubdesc);
+ err = usbd_do_request(dev, &req, &hubdesc);
}
- if (r != USBD_NORMAL_COMPLETION) {
+ if (err) {
DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
- USBDEVNAME(sc->sc_dev), usbd_errstr(r)));
+ USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
USB_ATTACH_ERROR_RETURN;
}
@@ -196,7 +198,7 @@ USB_ATTACH(uhub)
hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
M_USBDEV, M_NOWAIT);
- if (hub == 0)
+ if (hub == NULL)
USB_ATTACH_ERROR_RETURN;
dev->hub = hub;
dev->hub->hubsoftc = sc;
@@ -209,7 +211,7 @@ USB_ATTACH(uhub)
dev->powersrc->parent ?
dev->powersrc->parent->self_powered : 0));
- if (!dev->self_powered && dev->powersrc->parent &&
+ if (!dev->self_powered && dev->powersrc->parent != NULL &&
!dev->powersrc->parent->self_powered) {
printf("%s: bus powered hub connected to bus powered hub, "
"ignored\n", USBDEVNAME(sc->sc_dev));
@@ -217,13 +219,13 @@ USB_ATTACH(uhub)
}
/* Set up interrupt pipe. */
- r = usbd_device2interface_handle(dev, 0, &iface);
- if (r != USBD_NORMAL_COMPLETION) {
+ err = usbd_device2interface_handle(dev, 0, &iface);
+ if (err) {
printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
goto bad;
}
ed = usbd_interface2endpoint_descriptor(iface, 0);
- if (ed == 0) {
+ if (ed == NULL) {
printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
goto bad;
}
@@ -232,11 +234,10 @@ USB_ATTACH(uhub)
goto bad;
}
- 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) {
+ err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
+ USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
+ sizeof(sc->sc_status), uhub_intr, USBD_DEFAULT_INTERVAL);
+ if (err) {
printf("%s: cannot open interrupt pipe\n",
USBDEVNAME(sc->sc_dev));
goto bad;
@@ -245,15 +246,17 @@ USB_ATTACH(uhub)
/* Wait with power off for a while. */
usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
+ usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
+
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);
- if (r != USBD_NORMAL_COMPLETION)
+ err = uhub_init_port(up);
+ if (err)
printf("%s: init of port %d failed\n",
- USBDEVNAME(sc->sc_dev), up->portno);
+ USBDEVNAME(sc->sc_dev), up->portno);
}
sc->sc_running = 1;
@@ -271,12 +274,12 @@ uhub_init_port(up)
{
int port = up->portno;
usbd_device_handle dev = up->parent;
- usbd_status r;
+ usbd_status err;
u_int16_t pstatus;
- r = usbd_get_port_status(dev, port, &up->status);
- if (r != USBD_NORMAL_COMPLETION)
- return (r);
+ err = usbd_get_port_status(dev, port, &up->status);
+ if (err)
+ return (err);
pstatus = UGETW(up->status.wPortStatus);
DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x "
"change=0x%04x\n",
@@ -287,15 +290,10 @@ uhub_init_port(up)
/* First let the device go through a good power cycle, */
usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME);
-#if 0
-usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
-usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
-#endif
-
/* then turn the power on. */
- r = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
- if (r != USBD_NORMAL_COMPLETION)
- return (r);
+ err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
+ if (err)
+ return (err);
DPRINTF(("usb_init_port: turn on port %d power status=0x%04x "
"change=0x%04x\n",
port, UGETW(up->status.wPortStatus),
@@ -304,9 +302,9 @@ usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood *
UHD_PWRON_FACTOR);
/* Get the port status again. */
- r = usbd_get_port_status(dev, port, &up->status);
- if (r != USBD_NORMAL_COMPLETION)
- return (r);
+ err = usbd_get_port_status(dev, port, &up->status);
+ if (err)
+ return (err);
DPRINTF(("usb_init_port: after power on status=0x%04x "
"change=0x%04x\n",
UGETW(up->status.wPortStatus),
@@ -339,7 +337,7 @@ uhub_explore(dev)
usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
struct uhub_softc *sc = dev->hub->hubsoftc;
struct usbd_port *up;
- usbd_status r;
+ usbd_status err;
int port;
int change, status;
@@ -354,17 +352,16 @@ uhub_explore(dev)
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) {
+ err = usbd_get_port_status(dev, port, &up->status);
+ if (err) {
DPRINTF(("uhub_explore: get port status failed, "
- "error=%s\n",
- usbd_errstr(r)));
+ "error=%s\n", usbd_errstr(err)));
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));
+ DPRINTFN(3,("uhub_explore: port %d status 0x%04x 0x%04x\n",
+ port, status, change));
if (change & UPS_C_PORT_ENABLED) {
usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
if (status & UPS_PORT_ENABLED) {
@@ -385,6 +382,8 @@ uhub_explore(dev)
}
}
if (!(change & UPS_C_CONNECT_STATUS)) {
+ DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
+ "STATUS\n", port));
/* No status change, just do recursive explore. */
if (up->device && up->device->hub)
up->device->hub->explore(up->device);
@@ -402,16 +401,19 @@ uhub_explore(dev)
* the disconnect.
*/
disco:
- if (up->device) {
+ if (up->device != NULL) {
/* Disconnected */
- DPRINTF(("uhub_explore: device %d disappeared "
+ DPRINTF(("uhub_explore: device addr=%d disappeared "
"on port %d\n", up->device->address, port));
usb_disconnect_port(up, USBDEV(sc->sc_dev));
usbd_clear_port_feature(dev, port,
UHF_C_PORT_CONNECTION);
}
- if (!(status & UPS_CURRENT_CONNECT_STATUS))
+ if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
+ DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
+ "_STATUS\n", port));
continue;
+ }
/* Connected */
up->restartcnt = 0;
@@ -420,18 +422,20 @@ uhub_explore(dev)
usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
/* Reset port, which implies enabling it. */
- if (usbd_reset_port(dev, port, &up->status) !=
- USBD_NORMAL_COMPLETION)
+ if (usbd_reset_port(dev, port, &up->status)) {
+ DPRINTF(("uhub_explore: port=%d reset failed\n",
+ port));
continue;
+ }
/* Get device info and set its address. */
- r = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
+ err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
dev->depth + 1, status & UPS_LOW_SPEED,
port, up);
/* XXX retry a few times? */
- if (r != USBD_NORMAL_COMPLETION) {
+ if (err) {
DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
- "error=%s\n", usbd_errstr(r)));
+ "error=%s\n", usbd_errstr(err)));
/* Avoid addressing problems by disabling. */
/* usbd_reset_port(dev, port, &up->status); */
@@ -460,8 +464,9 @@ uhub_activate(self, act)
enum devact act;
{
struct uhub_softc *sc = (struct uhub_softc *)self;
- usbd_device_handle devhub = sc->sc_hub;
- int nports, p, i;
+ struct usbd_hub *hub = sc->sc_hub->hub;
+ usbd_device_handle dev;
+ int nports, port, i;
switch (act) {
case DVACT_ACTIVATE:
@@ -469,10 +474,12 @@ uhub_activate(self, act)
break;
case DVACT_DEACTIVATE:
- nports = devhub->hub->hubdesc.bNbrPorts;
- for(p = 0; p < nports; p++) {
- usbd_device_handle dev = devhub->hub->ports[p].device;
- if (dev) {
+ if (hub == NULL) /* malfunctioning hub */
+ break;
+ nports = hub->hubdesc.bNbrPorts;
+ for(port = 0; port < nports; port++) {
+ dev = hub->ports[port].device;
+ if (dev != NULL) {
for (i = 0; dev->subdevs[i]; i++)
config_deactivate(dev->subdevs[i]);
}
@@ -490,7 +497,7 @@ uhub_activate(self, act)
USB_DETACH(uhub)
{
USB_DETACH_START(uhub, sc);
- usbd_device_handle dev = sc->sc_hub;
+ struct usbd_hub *hub = sc->sc_hub->hub;
struct usbd_port *rup;
int port, nports;
@@ -500,25 +507,61 @@ USB_DETACH(uhub)
DPRINTF(("uhub_detach: sc=%port\n", sc));
#endif
- if (!dev->hub) /* Must be partially working */
+ if (hub == NULL) /* Must be partially working */
return (0);
usbd_abort_pipe(sc->sc_ipipe);
usbd_close_pipe(sc->sc_ipipe);
- nports = dev->hub->hubdesc.bNbrPorts;
+ nports = hub->hubdesc.bNbrPorts;
for(port = 0; port < nports; port++) {
- rup = &dev->hub->ports[port];
+ rup = &hub->ports[port];
if (rup->device)
usb_disconnect_port(rup, self);
}
+
+ usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
+ USBDEV(sc->sc_dev));
- free(dev->hub, M_USBDEV);
- dev->hub = 0;
+ free(hub, M_USBDEV);
+ sc->sc_hub->hub = NULL;
return (0);
}
+#if defined(__FreeBSD__)
+/* Called when a device has been detached from it */
+static void
+uhub_child_detached(self, child)
+ device_t self;
+ device_t child;
+{
+ 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;
+
+ if (!devhub->hub)
+ /* 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
+
/*
* Hub interrupt.
* This an indication that some port has changed status.
@@ -526,8 +569,8 @@ USB_DETACH(uhub)
* to be explored again.
*/
void
-uhub_intr(reqh, addr, status)
- usbd_request_handle reqh;
+uhub_intr(xfer, addr, status)
+ usbd_xfer_handle xfer;
usbd_private_handle addr;
usbd_status status;
{