Skip to contentSkip to conversation

media: i2c: imx219: Check return value of devm_gpiod_get_optional() in imx219_probe()

PATCH In review Chen Ni 3 participants 4 messages Feb 2 – Feb 4

Subsystems (3)
lore
Share & tools
lore

Chen Ni

author
#0 lore raw

PATCH 1/1 media: i2c: imx219: Check return value of devm_gpiod_get_optional() in imx219_probe()

The devm_gpiod_get_optional() function may return an error pointer (ERR_PTR) in case of a genuine failure during GPIO acquisition, not just NULL which indicates the legitimate absence of an optional GPIO.

Add an IS_ERR() check after the function call to catch such errors and propagate them to the probe function, ensuring the driver fails to load safely rather than proceeding with an invalid pointer.

---
 drivers/media/i2c/imx219.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/i2c/imx219.c +3 -0
index fee63bc106d9..7da02ce5da15 100644
@@ -1218,6 +1218,9 @@ static int imx219_probe(struct i2c_client *client)
 	/* Request optional enable pin */
 	imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset",
 						     GPIOD_OUT_HIGH);
+	if (IS_ERR(imx219->reset_gpio))
+		return dev_err_probe(dev, PTR_ERR(imx219->reset_gpio),
+				     "failed to get reset gpio\n");
 
 	/*
 	 * The sensor must be powered for imx219_identify_module()
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Chen Ni

The devm_gpiod_get_optional() function may return an error pointer (ERR_PTR) in case of a genuine failure during GPIO acquisition, not just NULL which indicates the legitimate absence of an optional GPIO.

Add an IS_ERR() check after the function call to catch such errors and propagate them to the probe function, ensuring the driver fails to load safely rather than proceeding with an invalid pointer.

Thanks for the patch

(Apologies to those directly on the cc for the earlier HTML version of this R-b. Google appears to have updated the composition settings for me, and I hadn't noticed)

Quoted 21 lines
---
 drivers/media/i2c/imx219.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/i2c/imx219.c +3 -1
index fee63bc106d9..7da02ce5da15 100644
@@ -1218,6 +1218,9 @@ static int imx219_probe(struct i2c_client *client)
        /* Request optional enable pin */
        imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset",
                                                     GPIOD_OUT_HIGH);
+       if (IS_ERR(imx219->reset_gpio))
+               return dev_err_probe(dev, PTR_ERR(imx219->reset_gpio),
+                                    "failed to get reset gpio\n");
 
        /*
         * The sensor must be powered for imx219_identify_module()
--

2.25.1

Reviewed-by on v1 1/1
#2 lore raw
In reply to Chen Ni

Quoting Chen Ni (2026-02-02 08:10:24)

Quoted 8 lines

The devm_gpiod_get_optional() function may return an error pointer (ERR_PTR) in case of a genuine failure during GPIO acquisition, not just NULL which indicates the legitimate absence of an optional GPIO.

Add an IS_ERR() check after the function call to catch such errors and propagate them to the probe function, ensuring the driver fails to load safely rather than proceeding with an invalid pointer.

Good catch, maybe a fixes tag is appropriate so this gets backported to stable kernels.

Fixes 1283b3b8f82b ("media: i2c: Add driver for Sony IMX219 sensor")

with that,

Quoted 24 lines
---
 drivers/media/i2c/imx219.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/i2c/imx219.c +3 -0
index fee63bc106d9..7da02ce5da15 100644
@@ -1218,6 +1218,9 @@ static int imx219_probe(struct i2c_client *client)
        /* Request optional enable pin */
        imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset",
                                                     GPIOD_OUT_HIGH);
+       if (IS_ERR(imx219->reset_gpio))
+               return dev_err_probe(dev, PTR_ERR(imx219->reset_gpio),
+                                    "failed to get reset gpio\n");
 
        /*
         * The sensor must be powered for imx219_identify_module()
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH v2 1/1 media: i2c: imx219: Check return value of devm_gpiod_get_optional() in imx219_probe()

The devm_gpiod_get_optional() function may return an error pointer (ERR_PTR) in case of a genuine failure during GPIO acquisition, not just NULL which indicates the legitimate absence of an optional GPIO.

Add an IS_ERR() check after the function call to catch such errors and propagate them to the probe function, ensuring the driver fails to load safely rather than proceeding with an invalid pointer.

Fixes 1283b3b8f82b ("media: i2c: Add driver for Sony IMX219 sensor")

---
Changes in v2:
- Add Fixes tag.

---
 drivers/media/i2c/imx219.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/i2c/imx219.c +3 -0
index fee63bc106d9..7da02ce5da15 100644
@@ -1218,6 +1218,9 @@ static int imx219_probe(struct i2c_client *client)
 	/* Request optional enable pin */
 	imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset",
 						     GPIOD_OUT_HIGH);
+	if (IS_ERR(imx219->reset_gpio))
+		return dev_err_probe(dev, PTR_ERR(imx219->reset_gpio),
+				     "failed to get reset gpio\n");
 
 	/*
 	 * The sensor must be powered for imx219_identify_module()
Signature · 1 line
2.25.1