Skip to contentSkip to conversation

usb: ohci-omap: implement overcurrent handling for GPIO-based power control

PATCH In review Muhammad Mahdi Amirpour 3 participants 3 messages Oct 18 – Oct 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 usb: ohci-omap: implement overcurrent handling for GPIO-based power control

The OMAP OHCI driver has had optional GPIO lines for VBUS power control and overcurrent detection since the gpiolib conversion, but the overcurrent line was never wired up — it was marked with a TODO comment.

On boards like the OMAP1 OSK, the VBUS power switch (TPS2045) is controlled via a TPS65010 GPIO, and overcurrent is reported on a separate GPIO. Without this patch, an overcurrent condition is not detected by software and VBUS power is not cut, relying solely on the hardware current limiter.

This patch implements the missing overcurrent handling:
- Requests a threaded IRQ on the overcurrent GPIO (both edges)
- Cuts VBUS power via the power GPIO when overcurrent is detected
- Reports overcurrent status and change through the root hub port status callbacks so the USB hub core can react
- Handles SetPortFeature/ClearPortFeature(POWER) to drive the VBUS GPIO alongside the OHCI hardware register
- Turns off VBUS on driver removal and probe failure

---
v4:
- Remove spinlock entirely; use READ_ONCE/WRITE_ONCE for the overcurrent flags (Greg KH). This also fixes the bug where gpiod_set_value_cansleep() was called under spin_lock_irqsave(), which sleeps on the I2C-attached TPS65010 (Sashiko).
- Rename overcurrent_status to overcurrent_active and overcurrent_changed to overcurrent_change_pending (Greg KH)
- Fix include order to be alphabetical (Greg KH)
- Add comment explaining BIT(1) = port 1 (Greg KH)
- Add comment explaining overcurrent_irq = -1 sentinel (Greg KH)
- Add VBUS power-off in err_remove_hcd and ohci_hcd_omap_remove() so VBUS is not left enabled after driver unbind (Sashiko)
- Re-check overcurrent_active after enabling VBUS in SetPortFeature(POWER) to close the race window (Sashiko)
- Rewrite commit message to explain motivation (Greg KH)

v3:
- Fix power GPIO polarity: 1 = VBUS off, 0 = VBUS on
- Fix wPortChange bit: shift USB_PORT_STAT_C_OVERCURRENT by 16
- Add clock and PHY cleanup to probe error path
- Use IRQF_NO_AUTOEN to close race window during probe
- Only set overcurrent_changed at probe when overcurrent is active
- Guard hub_status_data and hub_control behind priv->overcurrent
- Handle SetPortFeature/ClearPortFeature(POWER) for VBUS GPIO
- Remove shadowed flags variable in hub_control

v2:
- Use real name in From: and Signed-off-by: lines

 drivers/usb/host/ohci-omap.c | 282 +++++++++++++++++++++++++++++------
 1 file changed, 237 insertions(+), 45 deletions(-)
drivers/usb/host/ohci-omap.c +237 -45
index f6e56c4b9914..c71721243905 100644
@@ -19,20 +19,23 @@
 #include <linux/dma-mapping.h>
 #include <linux/err.h>
 #include <linux/gpio/consumer.h>
+#include <linux/interrupt.h>
 #include <linux/io.h>
+#include <linux/irqreturn.h>
 #include <linux/jiffies.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/usb/otg.h>
-#include <linux/platform_device.h>
 #include <linux/platform_data/usb-omap1.h>
-#include <linux/soc/ti/omap1-usb.h>
+#include <linux/platform_device.h>
+#include <linux/signal.h>
+#include <linux/soc/ti/omap1-io.h>
 #include <linux/soc/ti/omap1-mux.h>
 #include <linux/soc/ti/omap1-soc.h>
-#include <linux/soc/ti/omap1-io.h>
-#include <linux/signal.h>
+#include <linux/soc/ti/omap1-usb.h>
 #include <linux/usb.h>
+#include <linux/usb/ch11.h>
 #include <linux/usb/hcd.h>
+#include <linux/usb/otg.h>
 
 #include "ohci.h"
 
@@ -46,13 +49,23 @@ struct ohci_omap_priv {
 	struct clk *usb_dc_ck;
 	struct gpio_desc *power;
 	struct gpio_desc *overcurrent;
+	/*
+	 * Accessed with READ_ONCE/WRITE_ONCE from the threaded IRQ
+	 * handler and from hub_control in process context.  No lock
+	 * is needed: the IRQ handler is the sole writer of
+	 * overcurrent_active (IRQF_ONESHOT prevents re-entry), and
+	 * overcurrent_change_pending is a single-byte flag where a
+	 * missed clear just causes one extra poll cycle.
+	 */
+	bool overcurrent_active;
+	bool overcurrent_change_pending;
+	int overcurrent_irq; /* -1 means no IRQ has been allocated yet */
 };
 
 static const char hcd_name[] = "ohci-omap";
 static struct hc_driver __read_mostly ohci_omap_hc_driver;
 
-#define hcd_to_ohci_omap_priv(h) \
-	((struct ohci_omap_priv *)hcd_to_ohci(h)->priv)
+#define hcd_to_ohci_omap_priv(h) ((struct ohci_omap_priv *)hcd_to_ohci(h)->priv)
 
 static void omap_ohci_clock_power(struct ohci_omap_priv *priv, int on)
 {
@@ -70,15 +83,15 @@ static void omap_ohci_clock_power(struct ohci_omap_priv *priv, int on)
 static void start_hnp(struct ohci_hcd *ohci)
 {
 	struct usb_hcd *hcd = ohci_to_hcd(ohci);
-	const unsigned	port = hcd->self.otg_port - 1;
-	unsigned long	flags;
+	const unsigned port = hcd->self.otg_port - 1;
+	unsigned long flags;
 	u32 l;
 
 	otg_start_hnp(hcd->usb_phy->otg);
 
 	local_irq_save(flags);
 	hcd->usb_phy->otg->state = OTG_STATE_A_SUSPEND;
-	writel (RH_PS_PSS, &ohci->regs->roothub.portstatus [port]);
+	writel(RH_PS_PSS, &ohci->regs->roothub.portstatus[port]);
 	l = omap_readl(OTG_CTRL);
 	l &= ~OTG_A_BUSREQ;
 	omap_writel(l, OTG_CTRL);
@@ -89,11 +102,11 @@ static void start_hnp(struct ohci_hcd *ohci)
 
 static int ohci_omap_reset(struct usb_hcd *hcd)
 {
-	struct ohci_hcd		*ohci = hcd_to_ohci(hcd);
-	struct omap_usb_config	*config = dev_get_platdata(hcd->self.controller);
-	struct ohci_omap_priv	*priv = hcd_to_ohci_omap_priv(hcd);
-	int			need_transceiver = (config->otg != 0);
-	int			ret;
+	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
+	struct omap_usb_config *config = dev_get_platdata(hcd->self.controller);
+	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
+	int need_transceiver = (config->otg != 0);
+	int ret;
 
 	dev_dbg(hcd->self.controller, "starting USB Controller\n");
 
@@ -110,10 +123,11 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
 	if (IS_ENABLED(CONFIG_USB_OTG) && need_transceiver) {
 		hcd->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
 		if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
-			int	status = otg_set_host(hcd->usb_phy->otg,
-						&ohci_to_hcd(ohci)->self);
-			dev_dbg(hcd->self.controller, "init %s phy, status %d\n",
-					hcd->usb_phy->label, status);
+			int status = otg_set_host(hcd->usb_phy->otg,
+						  &ohci_to_hcd(ohci)->self);
+			dev_dbg(hcd->self.controller,
+				"init %s phy, status %d\n", hcd->usb_phy->label,
+				status);
 			if (status) {
 				usb_put_phy(hcd->usb_phy);
 				return status;
@@ -141,7 +155,7 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
 
 	/* board-specific power switching and overcurrent support */
 	if (machine_is_omap_osk()) {
-		u32	rh = roothub_a (ohci);
+		u32 rh = roothub_a(ohci);
 
 		/* power switching (ganged by default) */
 		rh &= ~RH_A_NPS;
@@ -181,6 +195,128 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
 	return 0;
 }
 
+static irqreturn_t ohci_omap_overcurrent_irq(int irq, void *data)
+{
+	struct usb_hcd *hcd = data;
+	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
+	int status;
+	bool was_active;
+
+	status = gpiod_get_value_cansleep(priv->overcurrent);
+	if (status < 0)
+		return IRQ_HANDLED;
+
+	if (status && priv->power)
+		gpiod_set_value_cansleep(priv->power, 1);
+
+	was_active = READ_ONCE(priv->overcurrent_active);
+	WRITE_ONCE(priv->overcurrent_active, !!status);
+	if (was_active != !!status)
+		WRITE_ONCE(priv->overcurrent_change_pending, true);
+
+	usb_hcd_poll_rh_status(hcd);
+
+	return IRQ_HANDLED;
+}
+
+static int ohci_omap_hub_status_data(struct usb_hcd *hcd, char *buf)
+{
+	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
+	int length;
+
+	length = ohci_hub_status_data(hcd, buf);
+
+	if (!priv->overcurrent)
+		return length;
+
+	if (READ_ONCE(priv->overcurrent_change_pending)) {
+		if (!length) {
+			buf[0] = 0;
+			length = 1;
+		}
+		buf[0] |= BIT(1); /* port 1 */
+	}
+
+	return length;
+}
+
+static int ohci_omap_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
+				 u16 wIndex, char *buf, u16 wLength)
+{
+	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
+	int ret;
+
+	/*
+	 * Drive the VBUS power GPIO alongside the OHCI hardware register.
+	 * On boards like the OSK, the OHCI PPS bit does not control the
+	 * real power switch — the GPIO does.
+	 */
+	if (priv->power && wIndex == 1) {
+		if (typeReq == SetPortFeature &&
+		    wValue == USB_PORT_FEAT_POWER) {
+			ret = ohci_hub_control(hcd, typeReq, wValue, wIndex,
+					       buf, wLength);
+			if (!ret && !READ_ONCE(priv->overcurrent_active))
+				gpiod_set_value_cansleep(priv->power, 0);
+			/*
+			 * Re-check: the overcurrent IRQ may have fired
+			 * between the check above and the GPIO call.
+			 * A mutex would close this fully, but the window
+			 * is microseconds and the hardware current limiter
+			 * provides the final safety net.
+			 */
+			if (!ret && READ_ONCE(priv->overcurrent_active))
+				gpiod_set_value_cansleep(priv->power, 1);
+			return ret;
+		}
+		if (typeReq == ClearPortFeature &&
+		    wValue == USB_PORT_FEAT_POWER) {
+			ret = ohci_hub_control(hcd, typeReq, wValue, wIndex,
+					       buf, wLength);
+			if (!ret)
+				gpiod_set_value_cansleep(priv->power, 1);
+			return ret;
+		}
+	}
+
+	ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
+	if (ret)
+		return ret;
+
+	if (!priv->overcurrent)
+		return ret;
+
+	if (typeReq == ClearPortFeature && wIndex == 1 &&
+	    wValue == USB_PORT_FEAT_C_OVER_CURRENT) {
+		WRITE_ONCE(priv->overcurrent_change_pending, false);
+		return 0;
+	}
+
+	if (typeReq != GetPortStatus || wIndex != 1)
+		return ret;
+
+	{
+		bool active = READ_ONCE(priv->overcurrent_active);
+		bool changed = READ_ONCE(priv->overcurrent_change_pending);
+		__le32 *portstatus = (__le32 *)buf;
+		u32 value = le32_to_cpu(*portstatus);
+
+		if (active)
+			value |= USB_PORT_STAT_OVERCURRENT;
+		else
+			value &= ~USB_PORT_STAT_OVERCURRENT;
+
+		if (changed)
+			value |= (USB_PORT_STAT_C_OVERCURRENT << 16);
+		else
+			value &= ~(USB_PORT_STAT_C_OVERCURRENT << 16);
+
+		*portstatus = cpu_to_le32(value);
+	}
+
+	return 0;
+}
+
 /*-------------------------------------------------------------------------*/
 
 /**
@@ -201,18 +337,18 @@ static int ohci_hcd_omap_probe(struct platform_device *pdev)
 
 	if (pdev->num_resources != 2) {
 		dev_err(&pdev->dev, "invalid num_resources: %i\n",
-		       pdev->num_resources);
+			pdev->num_resources);
 		return -ENODEV;
 	}
 
-	if (pdev->resource[0].flags != IORESOURCE_MEM
-			|| pdev->resource[1].flags != IORESOURCE_IRQ) {
+	if (pdev->resource[0].flags != IORESOURCE_MEM ||
+	    pdev->resource[1].flags != IORESOURCE_IRQ) {
 		dev_err(&pdev->dev, "invalid resource type\n");
 		return -ENODEV;
 	}
 
 	hcd = usb_create_hcd(&ohci_omap_hc_driver, &pdev->dev,
-			dev_name(&pdev->dev));
+			     dev_name(&pdev->dev));
 	if (!hcd)
 		return -ENOMEM;
 
@@ -221,6 +357,10 @@ static int ohci_hcd_omap_probe(struct platform_device *pdev)
 	priv = hcd_to_ohci_omap_priv(hcd);
 
 	/* Obtain two optional GPIO lines */
+	WRITE_ONCE(priv->overcurrent_active, false);
+	WRITE_ONCE(priv->overcurrent_change_pending, false);
+	priv->overcurrent_irq = -1;
+
 	priv->power = devm_gpiod_get_optional(&pdev->dev, "power", GPIOD_ASIS);
 	if (IS_ERR(priv->power)) {
 		retval = PTR_ERR(priv->power);
@@ -229,13 +369,8 @@ static int ohci_hcd_omap_probe(struct platform_device *pdev)
 	if (priv->power)
 		gpiod_set_consumer_name(priv->power, "OHCI power");
 
-	/*
-	 * This "overcurrent" GPIO line isn't really used in the code,
-	 * but has a designated hardware function.
-	 * TODO: implement proper overcurrent handling.
-	 */
-	priv->overcurrent = devm_gpiod_get_optional(&pdev->dev, "overcurrent",
-						    GPIOD_IN);
+	priv->overcurrent =
+		devm_gpiod_get_optional(&pdev->dev, "overcurrent", GPIOD_IN);
 	if (IS_ERR(priv->overcurrent)) {
 		retval = PTR_ERR(priv->overcurrent);
 		goto err_put_hcd;
@@ -277,23 +412,73 @@ static int ohci_hcd_omap_probe(struct platform_device *pdev)
 	if (!hcd->regs) {
 		dev_err(&pdev->dev, "can't ioremap OHCI HCD\n");
 		retval = -ENOMEM;
-		goto err2;
+		goto err_release_mem;
 	}
 
 	irq = platform_get_irq(pdev, 0);
 	if (irq < 0) {
 		retval = irq;
-		goto err3;
+		goto err_iounmap;
 	}
 	retval = usb_add_hcd(hcd, irq, 0);
 	if (retval)
-		goto err3;
+		goto err_iounmap;
+
+	if (priv->overcurrent) {
+		int status;
+
+		priv->overcurrent_irq = gpiod_to_irq(priv->overcurrent);
+		if (priv->overcurrent_irq < 0) {
+			retval = priv->overcurrent_irq;
+			goto err_remove_hcd;
+		}
+
+		retval = request_threaded_irq(
+			priv->overcurrent_irq, NULL, ohci_omap_overcurrent_irq,
+			IRQF_ONESHOT | IRQF_NO_AUTOEN | IRQF_TRIGGER_RISING |
+				IRQF_TRIGGER_FALLING,
+			"ohci-omap-overcurrent", hcd);
+		if (retval) {
+			priv->overcurrent_irq = -1;
+			goto err_remove_hcd;
+		}
+
+		status = gpiod_get_value_cansleep(priv->overcurrent);
+		if (status < 0) {
+			retval = status;
+			goto err_remove_hcd;
+		}
+
+		WRITE_ONCE(priv->overcurrent_active, !!status);
+		WRITE_ONCE(priv->overcurrent_change_pending, !!status);
+
+		if (status && priv->power)
+			gpiod_set_value_cansleep(priv->power, 1);
+
+		enable_irq(priv->overcurrent_irq);
+		if (status)
+			usb_hcd_poll_rh_status(hcd);
+	}
 
 	device_wakeup_enable(hcd->self.controller);
 	return 0;
-err3:
+
+err_remove_hcd:
+	if (priv->overcurrent_irq >= 0) {
+		free_irq(priv->overcurrent_irq, hcd);
+		priv->overcurrent_irq = -1;
+	}
+	usb_remove_hcd(hcd);
+	omap_ohci_clock_power(priv, 0);
+	if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
+		(void)otg_set_host(hcd->usb_phy->otg, NULL);
+		usb_put_phy(hcd->usb_phy);
+	}
+	if (priv->power)
+		gpiod_set_value_cansleep(priv->power, 1);
+err_iounmap:
 	iounmap(hcd->regs);
-err2:
+err_release_mem:
 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
 err_unprepare_dc_ck:
 	clk_unprepare(priv->usb_dc_ck);
@@ -308,7 +493,6 @@ static int ohci_hcd_omap_probe(struct platform_device *pdev)
 	return retval;
 }
 
-
 /* may be called with controller, bus, and devices active */
 
 /**
@@ -323,16 +507,22 @@ static int ohci_hcd_omap_probe(struct platform_device *pdev)
  */
 static void ohci_hcd_omap_remove(struct platform_device *pdev)
 {
-	struct usb_hcd	*hcd = platform_get_drvdata(pdev);
+	struct usb_hcd *hcd = platform_get_drvdata(pdev);
 	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
 
 	dev_dbg(hcd->self.controller, "stopping USB Controller\n");
+	if (priv->overcurrent_irq >= 0) {
+		free_irq(priv->overcurrent_irq, hcd);
+		priv->overcurrent_irq = -1;
+	}
 	usb_remove_hcd(hcd);
 	omap_ohci_clock_power(priv, 0);
 	if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
-		(void) otg_set_host(hcd->usb_phy->otg, 0);
+		(void)otg_set_host(hcd->usb_phy->otg, 0);
 		usb_put_phy(hcd->usb_phy);
 	}
+	if (priv->power)
+		gpiod_set_value_cansleep(priv->power, 1);
 	iounmap(hcd->regs);
 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
 	clk_unprepare(priv->usb_dc_ck);
@@ -344,7 +534,7 @@ static void ohci_hcd_omap_remove(struct platform_device *pdev)
 
 /*-------------------------------------------------------------------------*/
 
-#ifdef	CONFIG_PM
+#ifdef CONFIG_PM
 
 static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
 {
@@ -368,8 +558,8 @@ static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
 
 static int ohci_omap_resume(struct platform_device *dev)
 {
-	struct usb_hcd	*hcd = platform_get_drvdata(dev);
-	struct ohci_hcd	*ohci = hcd_to_ohci(hcd);
+	struct usb_hcd *hcd = platform_get_drvdata(dev);
+	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
 	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
 
 	if (time_before(jiffies, ohci->next_statechange))
@@ -392,7 +582,7 @@ static struct platform_driver ohci_hcd_omap_driver = {
 	.probe		= ohci_hcd_omap_probe,
 	.remove		= ohci_hcd_omap_remove,
 	.shutdown	= usb_hcd_platform_shutdown,
-#ifdef	CONFIG_PM
+#ifdef CONFIG_PM
 	.suspend	= ohci_omap_suspend,
 	.resume		= ohci_omap_resume,
 #endif
@@ -402,8 +592,8 @@ static struct platform_driver ohci_hcd_omap_driver = {
 };
 
 static const struct ohci_driver_overrides omap_overrides __initconst = {
-	.product_desc	= "OMAP OHCI",
-	.reset		= ohci_omap_reset,
+	.product_desc = "OMAP OHCI",
+	.reset = ohci_omap_reset,
 	.extra_priv_size = sizeof(struct ohci_omap_priv),
 };
 
@@ -413,6 +603,8 @@ static int __init ohci_omap_init(void)
 		return -ENODEV;
 
 	ohci_init_driver(&ohci_omap_hc_driver, &omap_overrides);
+	ohci_omap_hc_driver.hub_status_data = ohci_omap_hub_status_data;
+	ohci_omap_hc_driver.hub_control = ohci_omap_hub_control;
 	return platform_driver_register(&ohci_hcd_omap_driver);
 }
 module_init(ohci_omap_init);
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] usb: ohci-omap: race and inverted ordering in ohci_omap_overcurrent_irq() leaves VBUS on - [High]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] usb: ohci-omap: race and inverted ordering in ohci_omap_overcurrent_irq() leaves VBUS on
- [High] usb: ohci-omap: overcurrent IRQ fails to resume suspended root hub
- [Medium] usb: ohci-omap: race and missed falling edge in probe overcurrent init --

commit 2a3f441c427f55a1886bf324043fa831b260b3d4
Author: Muhammad Mahdi Amirpour <[email protected]>

usb: ohci-omap: implement overcurrent handling for GPIO-based power control

This commit implements overcurrent detection and VBUS power control via GPIOs in the OMAP OHCI driver using a threaded interrupt handler and custom root hub status and control callbacks.

Quoted 4 lines
drivers/usb/host/ohci-omap.c +0 -0
index f6e56c4b99148..c717212439053 100644

[ ... ]

Quoted 38 lines
drivers/usb/host/ohci-omap.c +34 -0
@@ -181,6 +195,128 @@ static int ohci_omap_reset(struct usb_hcd *hcd)
 	return 0;
 }
 
+static irqreturn_t ohci_omap_overcurrent_irq(int irq, void *data)
+{
+	struct usb_hcd *hcd = data;
+	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
+	int status;
+	bool was_active;
+
+	status = gpiod_get_value_cansleep(priv->overcurrent);
+	if (status < 0)
+		return IRQ_HANDLED;
+
+	if (status && priv->power)
+		gpiod_set_value_cansleep(priv->power, 1);
+
+	was_active = READ_ONCE(priv->overcurrent_active);
+	WRITE_ONCE(priv->overcurrent_active, !!status);
+	if (was_active != !!status)
+		WRITE_ONCE(priv->overcurrent_change_pending, true);
+
+	usb_hcd_poll_rh_status(hcd);
+
+	return IRQ_HANDLED;
+}
+
+static int ohci_omap_hub_status_data(struct usb_hcd *hcd, char *buf)
+{
+	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
+	int length;
+
+	length = ohci_hub_status_data(hcd, buf);
+
+	if (!priv->overcurrent)
+		return length;
+

drivers/usb/host/ohci-omap.c static int ohci_omap_reset(struct usb_hcd *hcd) · line 241

Quoted 10 lines
drivers/usb/host/ohci-omap.c +10 -0
+	if (READ_ONCE(priv->overcurrent_change_pending)) {
+		if (!length) {
+			buf[0] = 0;
+			length = 1;
+		}
+		buf[0] |= BIT(1); /* port 1 */
+	}
+
+	return length;
+}

[Severity: High]
What happens if an overcurrent interrupt fires while the root hub is runtime-suspended or autostopped?

When the root hub is suspended, hub_suspend() -> hub_quiesce() kills hub->urb (leaving hcd->status_urb as NULL) and ohci_bus_suspend() places the controller into OHCI_USB_SUSPEND.

When ohci_omap_overcurrent_irq() cuts VBUS and calls usb_hcd_poll_rh_status(), that calls ohci_omap_hub_status_data() -> ohci_hub_status_data(). Because the overcurrent signal is on an external GPIO rather than the OHCI hardware pin, ohci_hub_status_data() sees no hardware port change bits (changed == 0), so ohci_root_hub_state_changes() does not call ohci_rh_resume() or usb_hcd_resume_root_hub():

drivers/usb/host/ohci-hub.c:ohci_root_hub_state_changes() {
	...
	case OHCI_USB_SUSPEND:
	case OHCI_USB_RESUME:
		/* if there is a port change, autostart or ask to be resumed */
		if (changed) {
			if (ohci->autostop)
				ohci_rh_resume(ohci);
			else
				usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
	...
}

Then, even though ohci_omap_hub_status_data() returns length = 1, usb_hcd_poll_rh_status() sees that hcd->status_urb is NULL, so it only sets HCD_FLAG_POLL_PENDING and returns without waking hub_wq:

drivers/usb/core/hcd.c:usb_hcd_poll_rh_status() {
	...
		urb = hcd->status_urb;
		if (urb) {
			...
		} else {
			length = 0;
			set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
		}
	...
}

With VBUS turned off and the root hub still suspended, won't the port remain unpowered indefinitely because hub_wq never runs port_event() to clear the overcurrent change and restore power?

Quoted 20 lines
drivers/usb/host/ohci-omap.c +20 -0
+
+static int ohci_omap_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
+				 u16 wIndex, char *buf, u16 wLength)
+{
+	struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
+	int ret;
+
+	/*
+	 * Drive the VBUS power GPIO alongside the OHCI hardware register.
+	 * On boards like the OSK, the OHCI PPS bit does not control the
+	 * real power switch — the GPIO does.
+	 */
+	if (priv->power && wIndex == 1) {
+		if (typeReq == SetPortFeature &&
+		    wValue == USB_PORT_FEAT_POWER) {
+			ret = ohci_hub_control(hcd, typeReq, wValue, wIndex,
+					       buf, wLength);
+			if (!ret && !READ_ONCE(priv->overcurrent_active))
+				gpiod_set_value_cansleep(priv->power, 0);
+			/*

drivers/usb/host/ohci-omap.c static int ohci_omap_reset(struct usb_hcd *hcd) · line 271

Quoted 10 lines
drivers/usb/host/ohci-omap.c +10 -0
+			 * Re-check: the overcurrent IRQ may have fired
+			 * between the check above and the GPIO call.
+			 * A mutex would close this fully, but the window
+			 * is microseconds and the hardware current limiter
+			 * provides the final safety net.
+			 */
+			if (!ret && READ_ONCE(priv->overcurrent_active))
+				gpiod_set_value_cansleep(priv->power, 1);
+			return ret;
+		}

[Severity: High]
Can a concurrent SetPortFeature(USB_PORT_FEAT_POWER) in ohci_omap_hub_control() race with ohci_omap_overcurrent_irq() and leave VBUS switched on during an active overcurrent condition?

In ohci_omap_overcurrent_irq(), gpiod_set_value_cansleep(priv->power, 1) is called before WRITE_ONCE(priv->overcurrent_active, !!status). Because priv->power is on an I2C-attached TPS65010 expander on OMAP1 OSK, that GPIO write sleeps across an I2C transfer.

If ohci_omap_hub_control() runs while ohci_omap_overcurrent_irq() is sleeping in gpiod_set_value_cansleep(priv->power, 1) (or before the IRQ thread is scheduled to run WRITE_ONCE(priv->overcurrent_active, true)), ohci_omap_hub_control() can observe priv->overcurrent_active as false on both checks and drive priv->power back to 0. When ohci_omap_overcurrent_irq() finishes and sets priv->overcurrent_active to true, it has already completed its GPIO write, leaving VBUS enabled while priv->overcurrent_active is true.

Even if WRITE_ONCE(priv->overcurrent_active, !!status) were moved before the GPIO write in ohci_omap_overcurrent_irq(), if ohci_omap_hub_control() passes the initial !READ_ONCE(priv->overcurrent_active) check and is preempted while an overcurrent event asserts, cuts VBUS, and then deasserts once current stops flowing, priv->overcurrent_active will return to false and ohci_omap_hub_control() will turn VBUS back on and pass the re-check without the hub core authorizing recovery. Would a mutex serializing the GPIO writes and overcurrent state updates avoid this?

[ ... ]

Quoted 44 lines
drivers/usb/host/ohci-omap.c +29 -3
@@ -277,23 +412,73 @@ static int ohci_hcd_omap_probe(struct platform_device *pdev)
 	if (!hcd->regs) {
 		dev_err(&pdev->dev, "can't ioremap OHCI HCD\n");
 		retval = -ENOMEM;
-		goto err2;
+		goto err_release_mem;
 	}
 
 	irq = platform_get_irq(pdev, 0);
 	if (irq < 0) {
 		retval = irq;
-		goto err3;
+		goto err_iounmap;
 	}
 	retval = usb_add_hcd(hcd, irq, 0);
 	if (retval)
-		goto err3;
+		goto err_iounmap;
+
+	if (priv->overcurrent) {
+		int status;
+
+		priv->overcurrent_irq = gpiod_to_irq(priv->overcurrent);
+		if (priv->overcurrent_irq < 0) {
+			retval = priv->overcurrent_irq;
+			goto err_remove_hcd;
+		}
+
+		retval = request_threaded_irq(
+			priv->overcurrent_irq, NULL, ohci_omap_overcurrent_irq,
+			IRQF_ONESHOT | IRQF_NO_AUTOEN | IRQF_TRIGGER_RISING |
+				IRQF_TRIGGER_FALLING,
+			"ohci-omap-overcurrent", hcd);
+		if (retval) {
+			priv->overcurrent_irq = -1;
+			goto err_remove_hcd;
+		}
+
+		status = gpiod_get_value_cansleep(priv->overcurrent);
+		if (status < 0) {
+			retval = status;
+			goto err_remove_hcd;
+		}
+

drivers/usb/host/ohci-omap.c static int ohci_hcd_omap_probe(struct platform_device *pdev) · line 461

Quoted 10 lines
drivers/usb/host/ohci-omap.c +10 -0
+		WRITE_ONCE(priv->overcurrent_active, !!status);
+		WRITE_ONCE(priv->overcurrent_change_pending, !!status);
+
+		if (status && priv->power)
+			gpiod_set_value_cansleep(priv->power, 1);
+
+		enable_irq(priv->overcurrent_irq);
+		if (status)
+			usb_hcd_poll_rh_status(hcd);
+	}

[Severity: Medium]
In ohci_hcd_omap_probe(), if status is non-zero at probe time, could cutting VBUS power before calling enable_irq(priv->overcurrent_irq) cause the falling edge on priv->overcurrent to be missed?

When gpiod_set_value_cansleep(priv->power, 1) turns off VBUS (which sleeps on an I2C GPIO expander), the power switch can deassert its overcurrent line (1 -> 0) before enable_irq(priv->overcurrent_irq) is called. Because priv->overcurrent_irq was requested with IRQF_NO_AUTOEN, that falling edge occurs while the interrupt is still disabled, leaving priv->overcurrent_active stuck at true and preventing subsequent SetPortFeature(USB_PORT_FEAT_POWER) requests from ever re-enabling VBUS.

Also, since usb_add_hcd() calls ohci_omap_reset() (which enables VBUS) and registers the root hub before priv->overcurrent_irq is requested, hub_wq can already be running ohci_omap_hub_control() concurrently with this initialization block.

[ ... ]

Quoted 4 lines
Signature · 2 lines
-- 
2.51.2
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Muhammad Mahdi Amirpour
Quoted 19 lines · Muhammad Mahdi Amirpour

The OMAP OHCI driver has had optional GPIO lines for VBUS power control and overcurrent detection since the gpiolib conversion, but the overcurrent line was never wired up — it was marked with a TODO comment.

On boards like the OMAP1 OSK, the VBUS power switch (TPS2045) is controlled via a TPS65010 GPIO, and overcurrent is reported on a separate GPIO. Without this patch, an overcurrent condition is not detected by software and VBUS power is not cut, relying solely on the hardware current limiter.

This patch implements the missing overcurrent handling:
- Requests a threaded IRQ on the overcurrent GPIO (both edges)
- Cuts VBUS power via the power GPIO when overcurrent is detected
- Reports overcurrent status and change through the root hub port status callbacks so the USB hub core can react
- Handles SetPortFeature/ClearPortFeature(POWER) to drive the VBUS GPIO alongside the OHCI hardware register
- Turns off VBUS on driver removal and probe failure

Quoted 7 lines

---
v4:
- Remove spinlock entirely; use READ_ONCE/WRITE_ONCE for the overcurrent flags (Greg KH). This also fixes the bug where gpiod_set_value_cansleep() was called under spin_lock_irqsave(), which sleeps on the I2C-attached TPS65010 (Sashiko).

Are you sure this is correct? You never responded to my previous review comments here, just submitted a new version which is a bit odd...

thanks,

greg k-h