Skip to contentSkip to conversation

media: staging: atomisp: improve error handling in gc2235_detect()

PATCH In review [email protected] 2 participants 2 messages May 21, 2021 – May 24, 2021

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

PATCH 1/1 media: staging: atomisp: improve error handling in gc2235_detect()

From: Tom Rix <[email protected]>

Static analysis reports this representative problem

atomisp-gc2235.c:867:20: warning: The right operand
of '|' is a garbage value

        id = ((high << 8) | low);
                          ^ ~~~

When gc2235_read_reg() fails, its return val is never written.

For gc2235_detect(), high and low are or-ed and compared with GC2235_ID, 0x2235. Initialize both to 0 and skip checking the read returns, it's errors are not passed up, only -ENODEV is.

---
 drivers/staging/media/atomisp/i2c/atomisp-gc2235.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/media/atomisp/i2c/atomisp-gc2235.c +3 -10
index 38defa0f81513..3b6e02b1f45d1 100644
@@ -849,21 +849,14 @@ static int gc2235_get_fmt(struct v4l2_subdev *sd,
 static int gc2235_detect(struct i2c_client *client)
 {
 	struct i2c_adapter *adapter = client->adapter;
-	u16 high, low;
-	int ret;
+	u16 high = 0, low = 0;
 	u16 id;
 
 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
 		return -ENODEV;
 
-	ret = gc2235_read_reg(client, GC2235_8BIT,
-			      GC2235_SENSOR_ID_H, &high);
-	if (ret) {
-		dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
-		return -ENODEV;
-	}
-	ret = gc2235_read_reg(client, GC2235_8BIT,
-			      GC2235_SENSOR_ID_L, &low);
+	gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_H, &high);
+	gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_L, &low);
 	id = ((high << 8) | low);
 
 	if (id != GC2235_ID) {
Signature · 1 line
2.26.3
reviewer
#1 lore raw

Hi Tom,

Quoted 31 lines · On Fri, May 21, 2021 at 12:48:05PM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

Static analysis reports this representative problem

atomisp-gc2235.c:867:20: warning: The right operand
of '|' is a garbage value

        id = ((high << 8) | low);
                          ^ ~~~

When gc2235_read_reg() fails, its return val is never written.

For gc2235_detect(), high and low are or-ed and compared with GC2235_ID, 0x2235. Initialize both to 0 and skip checking the read returns, it's errors are not passed up, only -ENODEV is.

---
 drivers/staging/media/atomisp/i2c/atomisp-gc2235.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/media/atomisp/i2c/atomisp-gc2235.c +1 -2
index 38defa0f81513..3b6e02b1f45d1 100644
@@ -849,21 +849,14 @@ static int gc2235_get_fmt(struct v4l2_subdev *sd,
 static int gc2235_detect(struct i2c_client *client)
 {
 	struct i2c_adapter *adapter = client->adapter;
-	u16 high, low;
-	int ret;
+	u16 high = 0, low = 0;

drivers/staging/media/atomisp/i2c/atomisp-gc2235.c static int gc2235_get_fmt(struct v4l2_subdev *sd, · line 863

Quoted 10 lines
drivers/staging/media/atomisp/i2c/atomisp-gc2235.c +0 -5
 	u16 id;
 
 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
 		return -ENODEV;
 
-	ret = gc2235_read_reg(client, GC2235_8BIT,
-			      GC2235_SENSOR_ID_H, &high);
-	if (ret) {
-		dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
-		return -ENODEV;

It'd be nice if the function returned a different value for different errors, such as there was an I/O error and that the ID was wrong.

Quoted 8 lines
drivers/staging/media/atomisp/i2c/atomisp-gc2235.c +2 -3
-	}
-	ret = gc2235_read_reg(client, GC2235_8BIT,
-			      GC2235_SENSOR_ID_L, &low);
+	gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_H, &high);
+	gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_L, &low);
 	id = ((high << 8) | low);
 
 	if (id != GC2235_ID) {
Signature · 1 line
Sakari Ailus