libpayload: Add a generic driver for usb hubs

The current drivers for external usb hubs and root hubs all follow
the same pattern. Before adding another one with 90% of the same code,
extract the common parts and rewrite them with a simple interface.

This also adds debouncing of new attachments. Current drivers just
waited 100ms before they reset the device. However, we should check
if the device becomes disconnected and reconnected during this period.

Porting of the current hub drivers will take place in separate
commits (when I have time to test the older HCIs).

Change-Id: I0c0ce0ac1b1cc51fb4cd009b3f9fcd1b9d2ba8fe
Signed-off-by: Nico Huber <nico.huber@secunet.com>
Reviewed-on: http://review.coreboot.org/3450
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/payloads/libpayload/drivers/usb/generic_hub.h b/payloads/libpayload/drivers/usb/generic_hub.h
new file mode 100644
index 0000000..7be20e8
--- /dev/null
+++ b/payloads/libpayload/drivers/usb/generic_hub.h
@@ -0,0 +1,83 @@
+/*
+ * This file is part of the libpayload project.
+ *
+ * Copyright (C) 2013 secunet Security Networks AG
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
+ */
+
+#ifndef __USB_HUB_H
+#define __USB_HUB_H
+
+#include <usb/usb.h>
+
+typedef struct generic_hub_ops {
+	/* negative results denote an error */
+
+	/* returns 1 if the hub's status changed since the last call (optional) */
+	int (*hub_status_changed)(usbdev_t *);
+	/* returns 1 if the port's status changed since the last call */
+	int (*port_status_changed)(usbdev_t *, int port);
+	/* returns 1 if something is connected to the port */
+	int (*port_connected)(usbdev_t *, int port);
+	/* returns 1 if port is currently resetting */
+	int (*port_in_reset)(usbdev_t *, int port);
+	/* returns 1 if the port is enabled */
+	int (*port_enabled)(usbdev_t *, int port);
+	/* returns speed if port is enabled, negative value if not */
+	int (*port_speed)(usbdev_t *, int port);
+
+	/* enables (powers up) a port (optional) */
+	int (*enable_port)(usbdev_t *, int port);
+	/* disables (powers down) a port (optional) */
+	int (*disable_port)(usbdev_t *, int port);
+	/* starts a port reset (required if reset_port is set to a generic one from below) */
+	int (*start_port_reset)(usbdev_t *, int port);
+
+	/* performs a port reset (optional, generic implementations below) */
+	int (*reset_port)(usbdev_t *, int port);
+} generic_hub_ops_t;
+
+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
+
+	const generic_hub_ops_t *ops;
+
+	void *data;
+} generic_hub_t;
+
+void generic_hub_destroy(usbdev_t *);
+int  generic_hub_resetport(usbdev_t *, int port);
+int  generic_hub_rh_resetport(usbdev_t *, int port); /* root hubs have different timing requirements */
+int  generic_hub_scanport(usbdev_t *, int port);
+/* the provided generic_hub_ops struct has to be static */
+int generic_hub_init(usbdev_t *, int num_ports, const generic_hub_ops_t *);
+
+#define GEN_HUB(usbdev) ((generic_hub_t *)(usbdev)->data)
+
+#endif