Skip to contentSkip to conversation

button: gpio: handle broken controller

PATCH In review Caleb Connolly 2 participants 2 messages Nov 13, 2024 – Dec 6, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 button: gpio: handle broken controller

Avoid crashing U-Boot when the GPIO controller for a button is disabled or failed to probe. We also need to check the priv data for each button since even if a button fails to probe it will still be polled by the core code.

---
 drivers/button/button-gpio.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/button/button-gpio.c +8 -1
index 43b82d98aeb7..31b85c8150e8 100644
@@ -19,8 +19,11 @@ static enum button_state_t button_gpio_get_state(struct udevice *dev)
 {
 	struct button_gpio_priv *priv = dev_get_priv(dev);
 	int ret;
 
+	if (!priv)
+		return -ENODATA;
+
 	if (!dm_gpio_is_valid(&priv->gpio))
 		return -EREMOTEIO;
 	ret = dm_gpio_get_value(&priv->gpio);
 	if (ret < 0)
@@ -31,8 +34,10 @@ static enum button_state_t button_gpio_get_state(struct udevice *dev)
 
 static int button_gpio_get_code(struct udevice *dev)
 {
 	struct button_gpio_priv *priv = dev_get_priv(dev);
+	if (!priv)
+		return -ENODATA;
 	int code = priv->linux_code;
 
 	if (!code)
 		return -ENODATA;
@@ -50,9 +55,9 @@ static int button_gpio_probe(struct udevice *dev)
 	if (!uc_plat->label)
 		return 0;
 
 	ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_IN);
-	if (ret)
+	if (ret || !dm_gpio_is_valid(&priv->gpio))
 		return ret;
 
 	ret = dev_read_u32(dev, "linux,code", &priv->linux_code);
 
@@ -97,8 +102,10 @@ static int button_gpio_bind(struct udevice *parent)
 		if (ret)
 			return ret;
 		uc_plat = dev_get_uclass_plat(dev);
 		uc_plat->label = label;
+		debug("Button '%s' bound to driver '%s'\n", label,
+		      dev->driver->name);
 	}
 
 	return 0;
 }
Signature · 1 line
2.47.0
#1 lore raw
Quoted 6 lines · Caleb Connolly

Avoid crashing U-Boot when the GPIO controller for a button is disabled or failed to probe. We also need to check the priv data for each button since even if a button fails to probe it will still be polled by the core code.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom