usb: Clean up the generic hub code a little.
Change-Id: Ic54d2221d80d86f912ced28cc6355c59ee646642
diff --git a/src/libpayload/drivers/usb/dwc2_rh.c b/src/libpayload/drivers/usb/dwc2_rh.c
index 442438b..594373c 100644
--- a/src/libpayload/drivers/usb/dwc2_rh.c
+++ b/src/libpayload/drivers/usb/dwc2_rh.c
@@ -160,7 +160,7 @@
return 0;
}
-static const generic_hub_ops_t dwc2_rh_ops = {
+static const GenericHubOps dwc2_rh_ops = {
.hub_status_changed = NULL,
.port_status_changed = dwc2_rh_port_status_changed,
.port_connected = dwc2_rh_port_connected,
diff --git a/src/libpayload/drivers/usb/generic_hub.c b/src/libpayload/drivers/usb/generic_hub.c
index 071637c..e269038 100644
--- a/src/libpayload/drivers/usb/generic_hub.c
+++ b/src/libpayload/drivers/usb/generic_hub.c
@@ -33,41 +33,41 @@
#include "base/time.h"
-void
-generic_hub_destroy(UsbDev *const dev)
+enum {
+ GenericHub_NoDev = -1,
+};
+
+void generic_hub_destroy(UsbDev *const dev)
{
generic_hub_t *const hub = GEN_HUB(dev);
if (!hub)
return;
- /* First, detach all devices behind this hub */
- int port;
- for (port = 1; port <= hub->num_ports; ++port) {
+ // First, detach all devices behind this hub.
+ for (int port = 1; port <= hub->num_ports; port++) {
if (hub->ports[port] >= 0) {
usb_debug("generic_hub: Detachment at port %d\n", port);
usb_detach_device(dev->controller, hub->ports[port]);
- hub->ports[port] = NO_DEV;
+ hub->ports[port] = GenericHub_NoDev;
}
}
- /* Disable all ports */
- if (hub->ops->disable_port) {
- for (port = 1; port <= hub->num_ports; ++port)
+ // Disable all ports.
+ if (hub->ops->disable_port)
+ for (int port = 1; port <= hub->num_ports; port++)
hub->ops->disable_port(dev, port);
- }
free(hub->ports);
free(hub);
}
-static int
-generic_hub_debounce(UsbDev *const dev, const int port)
+static int generic_hub_debounce(UsbDev *const dev, const int port)
{
generic_hub_t *const hub = GEN_HUB(dev);
- const int step_ms = 1; /* linux uses 25ms, we're busy anyway */
- const int at_least_ms = 100; /* 100ms as in usb20 spec 9.1.2 */
- const int timeout_ms = 1500; /* linux uses this value */
+ const int step_ms = 1; // linux uses 25ms, we're busy anyway.
+ const int at_least_ms = 100; // 100ms as in usb20 spec 9.1.2.
+ const int timeout_ms = 1500; // linux uses this value.
int total_ms = 0;
int stable_ms = 0;
@@ -90,64 +90,60 @@
}
if (total_ms >= timeout_ms)
usb_debug("generic_hub: Debouncing timed out at %d\n", port);
- return 0; /* ignore timeouts, try to always go on */
+ return 0; // Ignore timeouts, try to always go on.
}
-int
-generic_hub_wait_for_port(UsbDev *const dev, const int port,
- const int wait_for,
- int (*const port_op)(UsbDev *, int),
- int timeout_steps, const int step_us)
+int generic_hub_wait_for_port(UsbDev *const dev, const int port,
+ const int wait_for,
+ int (*const port_op)(UsbDev *, int),
+ int timeout_steps, const int step_us)
{
- int state;
do {
- state = port_op(dev, port);
+ int state = port_op(dev, port);
if (state < 0)
return -1;
else if (!!state == wait_for)
return timeout_steps;
udelay(step_us);
- --timeout_steps;
- } while (timeout_steps);
+ timeout_steps--;
+ } while(timeout_steps);
+
return 0;
}
-int
-generic_hub_resetport(UsbDev *const dev, const int port)
+int generic_hub_resetport(UsbDev *const dev, const int port)
{
generic_hub_t *const hub = GEN_HUB(dev);
if (hub->ops->start_port_reset(dev, port) < 0)
return -1;
- /* wait for 10ms (usb20 spec 11.5.1.5: reset should take 10 to 20ms) */
+ // Wait for 10ms. (usb20 spec 11.5.1.5: reset should take 10 to 20ms)
mdelay(10);
- /* now wait 12ms for the hub to finish the reset */
+ // Now wait 12ms for the hub to finish the reset.
const int ret = generic_hub_wait_for_port(
- /* time out after 120 * 100us = 12ms */
+ // Time out after 120 * 100us = 12ms.
dev, port, 0, hub->ops->port_in_reset, 120, 100);
if (ret < 0)
return -1;
else if (!ret)
usb_debug("generic_hub: Reset timed out at port %d\n", port);
- return 0; /* ignore timeouts, try to always go on */
+ return 0; // Ignore timeouts, try to always go on.
}
-static int
-generic_hub_detach_dev(UsbDev *const dev, const int port)
+static int generic_hub_detach_dev(UsbDev *const dev, const int port)
{
generic_hub_t *const hub = GEN_HUB(dev);
usb_detach_device(dev->controller, hub->ports[port]);
- hub->ports[port] = NO_DEV;
+ hub->ports[port] = GenericHub_NoDev;
return 0;
}
-static int
-generic_hub_attach_dev(UsbDev *const dev, const int port)
+static int generic_hub_attach_dev(UsbDev *const dev, const int port)
{
generic_hub_t *const hub = GEN_HUB(dev);
@@ -157,9 +153,9 @@
if (hub->ops->reset_port) {
if (hub->ops->reset_port(dev, port) < 0)
return -1;
- /* after reset the port will be enabled automatically */
+ // After reset the port will be enabled automatically.
const int ret = generic_hub_wait_for_port(
- /* time out after 1,000 * 10us = 10ms */
+ // Time out after 1,000 * 10us = 10ms.
dev, port, 1, hub->ops->port_enabled, 1000, 10);
if (ret < 0)
return -1;
@@ -172,16 +168,14 @@
if (speed >= 0) {
usb_debug("generic_hub: Success at port %d\n", port);
if (hub->ops->reset_port)
- mdelay(10); /* Reset recovery time
- (usb20 spec 7.1.7.5) */
+ mdelay(10); // Reset recovery time. (usb20 spec 7.1.7.5)
hub->ports[port] = usb_attach_device(
dev->controller, dev->address, port, speed);
}
return 0;
}
-int
-generic_hub_scanport(UsbDev *const dev, const int port)
+int generic_hub_scanport(UsbDev *const dev, const int port)
{
generic_hub_t *const hub = GEN_HUB(dev);
@@ -202,8 +196,7 @@
return 0;
}
-static void
-generic_hub_poll(UsbDev *const dev)
+static void generic_hub_poll(UsbDev *const dev)
{
generic_hub_t *const hub = GEN_HUB(dev);
if (!hub)
@@ -213,8 +206,7 @@
hub->ops->hub_status_changed(dev) != 1)
return;
- int port;
- for (port = 1; port <= hub->num_ports; ++port) {
+ for (int port = 1; port <= hub->num_ports; port++) {
const int ret = hub->ops->port_status_changed(dev, port);
if (ret < 0) {
return;
@@ -226,12 +218,9 @@
}
}
-int
-generic_hub_init(UsbDev *const dev, const int num_ports,
- const generic_hub_ops_t *const ops)
+int generic_hub_init(UsbDev *const dev, const int num_ports,
+ const GenericHubOps *const ops)
{
- int port;
-
dev->destroy = generic_hub_destroy;
dev->poll = generic_hub_poll;
dev->data = malloc(sizeof(generic_hub_t));
@@ -250,14 +239,14 @@
dev->data = NULL;
return -1;
}
- for (port = 1; port <= num_ports; ++port)
- hub->ports[port] = NO_DEV;
+ for (int port = 1; port <= num_ports; ++port)
+ hub->ports[port] = GenericHub_NoDev;
- /* Enable all ports */
+ // Enable all ports.
if (ops->enable_port) {
- for (port = 1; port <= num_ports; ++port)
+ for (int port = 1; port <= num_ports; ++port)
ops->enable_port(dev, port);
- /* wait once for all ports */
+ // Wait once for all ports.
mdelay(20);
}
diff --git a/src/libpayload/drivers/usb/generic_hub.h b/src/libpayload/drivers/usb/generic_hub.h
index f36c3c0..c74e566 100644
--- a/src/libpayload/drivers/usb/generic_hub.h
+++ b/src/libpayload/drivers/usb/generic_hub.h
@@ -25,48 +25,46 @@
* SUCH DAMAGE.
*/
-#ifndef __USB_HUB_H
-#define __USB_HUB_H
+#ifndef __LIBPAYLOAD_DRIVERS_USB_GENERIC_HUB_H__
+#define __LIBPAYLOAD_DRIVERS_USB_GENERIC_HUB_H__
#include <usb/usb.h>
-typedef struct generic_hub_ops {
- /* negative results denote an error */
+typedef struct {
+ // Negative results denote an error.
- /* returns 1 if the hub's status changed since the last call (optional) */
+ // Returns 1 if the hub's status changed since the last call (optional).
int (*hub_status_changed)(UsbDev *);
- /* returns 1 if the port's status changed since the last call */
+ // Returns 1 if the port's status changed since the last call.
int (*port_status_changed)(UsbDev *, int port);
- /* returns 1 if something is connected to the port */
+ // Returns 1 if something is connected to the port.
int (*port_connected)(UsbDev *, int port);
- /* returns 1 if port is currently resetting */
+ // Returns 1 if port is currently resetting.
int (*port_in_reset)(UsbDev *, int port);
- /* returns 1 if the port is enabled */
+ // Returns 1 if the port is enabled.
int (*port_enabled)(UsbDev *, int port);
- /* returns speed if port is enabled, negative value if not */
+ // Returns speed if port is enabled, negative value if not.
UsbSpeed (*port_speed)(UsbDev *, int port);
- /* enables (powers up) a port (optional) */
+ // Enables (powers up) a port (optional).
int (*enable_port)(UsbDev *, int port);
- /* disables (powers down) a port (optional) */
+ // disables (powers down) a port (optional).
int (*disable_port)(UsbDev *, int port);
- /* starts a port reset (required if reset_port is set to a generic one from below) */
+ // Starts a port reset (required if reset_port is set to a generic
+ // one from below).
int (*start_port_reset)(UsbDev *, int port);
- /* performs a port reset (optional, generic implementations below) */
+ // Performs a port reset (optional, generic implementations below).
int (*reset_port)(UsbDev *, int port);
-} generic_hub_ops_t;
+} GenericHubOps;
typedef struct generic_hub {
int num_ports;
- /* port numbers are always 1 based,
- so we waste one int for convenience */
- int *ports; /* allocated to sizeof(*ports)*(num_ports+1) */
-#define NO_DEV -1
+ // Port numbers are always 1 based, so we waste one int for
+ // convenience.
+ int *ports; // Allocated to sizeof(*ports)*(num_ports+1).
- const generic_hub_ops_t *ops;
-
- void *data;
+ const GenericHubOps *ops;
} generic_hub_t;
void generic_hub_destroy(UsbDev *);
@@ -76,9 +74,9 @@
int timeout_steps, const int step_us);
int generic_hub_resetport(UsbDev *, int port);
int generic_hub_scanport(UsbDev *, int port);
-/* the provided generic_hub_ops struct has to be static */
-int generic_hub_init(UsbDev *, int num_ports, const generic_hub_ops_t *);
+// The provided generic_hub_ops struct has to be static.
+int generic_hub_init(UsbDev *, int num_ports, const GenericHubOps *);
#define GEN_HUB(usbdev) ((generic_hub_t *)(usbdev)->data)
-#endif
+#endif /* __LIBPAYLOAD_DRIVERS_USB_GENERIC_HUB_H__ */
diff --git a/src/libpayload/drivers/usb/usbhub.c b/src/libpayload/drivers/usb/usbhub.c
index 8ed887e..b365438 100644
--- a/src/libpayload/drivers/usb/usbhub.c
+++ b/src/libpayload/drivers/usb/usbhub.c
@@ -144,7 +144,7 @@
dr.wValue, dev->address, ret);
}
-static const generic_hub_ops_t usb_hub_ops = {
+static const GenericHubOps usb_hub_ops = {
.hub_status_changed = NULL,
.port_status_changed = usb_hub_port_status_changed,
.port_connected = usb_hub_port_connected,
diff --git a/src/libpayload/drivers/usb/xhci_rh.c b/src/libpayload/drivers/usb/xhci_rh.c
index 8349aea..069d9fb 100644
--- a/src/libpayload/drivers/usb/xhci_rh.c
+++ b/src/libpayload/drivers/usb/xhci_rh.c
@@ -117,7 +117,7 @@
return 0;
}
-static const generic_hub_ops_t xhci_rh_ops = {
+static const GenericHubOps xhci_rh_ops = {
.hub_status_changed = xhci_rh_hub_status_changed,
.port_status_changed = xhci_rh_port_status_changed,
.port_connected = xhci_rh_port_connected,