Skip to contentSkip to conversation

leds/lm3642: simplify error handling, remove user-triggerable errors

PATCH Pavel Machek 2 participants 2 messages Dec 27, 2018 – Dec 28, 2018 LED

lore
Share & tools
lore
#0 lore raw

Doing goto just to return is unneccessarily complex, simplify code a bit.

Drop dev_err()s that can be triggered by user.

drivers/leds/leds-lm3642.c +14 -23
index cada084..62fc752 100644
@@ -110,7 +110,7 @@ static int lm3642_control(struct lm3642_chip_data *chip,
 	ret = regmap_read(chip->regmap, REG_FLAG, &chip->last_flag);
 	if (ret < 0) {
 		dev_err(chip->dev, "Failed to read REG_FLAG Register\n");
-		goto out;
+		return ret;
 	}
 
 	if (chip->last_flag)
@@ -150,11 +150,11 @@ static int lm3642_control(struct lm3642_chip_data *chip,
 		break;
 
 	default:
-		return ret;
+		return -EINVAL;
 	}
 	if (ret < 0) {
 		dev_err(chip->dev, "Failed to write REG_I_CTRL Register\n");
-		goto out;
+		return ret;
 	}
 
 	if (chip->tx_pin)
@@ -163,13 +163,12 @@ static int lm3642_control(struct lm3642_chip_data *chip,
 	ret = regmap_update_bits(chip->regmap, REG_ENABLE,
 				 MODE_BITS_MASK << MODE_BITS_SHIFT,
 				 opmode << MODE_BITS_SHIFT);
-out:
 	return ret;
 }
 
 /* torch */
 
-/* torch pin config for lm3642*/
+/* torch pin config for lm3642 */
 static ssize_t lm3642_torch_pin_store(struct device *dev,
 				      struct device_attribute *attr,
 				      const char *buf, size_t size)
@@ -182,7 +181,7 @@ static ssize_t lm3642_torch_pin_store(struct device *dev,
 
 	ret = kstrtouint(buf, 10, &state);
 	if (ret)
-		goto out_strtoint;
+		return ret;
 	if (state != 0)
 		state = 0x01 << TORCH_PIN_EN_SHIFT;
 
@@ -190,16 +189,12 @@ static ssize_t lm3642_torch_pin_store(struct device *dev,
 	ret = regmap_update_bits(chip->regmap, REG_ENABLE,
 				 TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT,
 				 state);
-	if (ret < 0)
-		goto out;
+	if (ret < 0) {
+		dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
+		return ret;
+	}
 
 	return size;
-out:
-	dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
-	return ret;
-out_strtoint:
-	dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
-	return ret;
 }
 
 static DEVICE_ATTR(torch_pin, S_IWUSR, NULL, lm3642_torch_pin_store);
@@ -233,7 +228,7 @@ static ssize_t lm3642_strobe_pin_store(struct device *dev,
 
 	ret = kstrtouint(buf, 10, &state);
 	if (ret)
-		goto out_strtoint;
+		return ret;
 	if (state != 0)
 		state = 0x01 << STROBE_PIN_EN_SHIFT;
 
@@ -241,16 +236,12 @@ static ssize_t lm3642_strobe_pin_store(struct device *dev,
 	ret = regmap_update_bits(chip->regmap, REG_ENABLE,
 				 STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT,
 				 state);
-	if (ret < 0)
-		goto out;
+	if (ret < 0) {
+		dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
+		return ret;
+	}
 
 	return size;
-out:
-	dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
-	return ret;
-out_strtoint:
-	dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
-	return ret;
 }
 
 static DEVICE_ATTR(strobe_pin, S_IWUSR, NULL, lm3642_strobe_pin_store);
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#1 lore raw

Hi Pavel,

Thank you for the patch.

Quoted 36 lines · Pavel Machek

Doing goto just to return is unneccessarily complex, simplify code a bit.

Drop dev_err()s that can be triggered by user.

drivers/leds/leds-lm3642.c +3 -3
index cada084..62fc752 100644
@@ -110,7 +110,7 @@ static int lm3642_control(struct lm3642_chip_data *chip,
  	ret = regmap_read(chip->regmap, REG_FLAG, &chip->last_flag);
  	if (ret < 0) {
  		dev_err(chip->dev, "Failed to read REG_FLAG Register\n");
-		goto out;
+		return ret;
  	}
 
  	if (chip->last_flag)
@@ -150,11 +150,11 @@ static int lm3642_control(struct lm3642_chip_data *chip,
  		break;
 
  	default:
-		return ret;
+		return -EINVAL;
  	}
  	if (ret < 0) {
  		dev_err(chip->dev, "Failed to write REG_I_CTRL Register\n");
-		goto out;
+		return ret;
  	}
 
  	if (chip->tx_pin)

drivers/leds/leds-lm3642.c static int lm3642_control(struct lm3642_chip_data *chip, · line 165

Quoted 3 lines
drivers/leds/leds-lm3642.c +0 -0
@@ -163,13 +163,12 @@ static int lm3642_control(struct lm3642_chip_data *chip,
  	ret = regmap_update_bits(chip->regmap, REG_ENABLE,
  				 MODE_BITS_MASK << MODE_BITS_SHIFT,
  				 opmode << MODE_BITS_SHIFT);

s/ret =/return/

Quoted 62 lines
drivers/leds/leds-lm3642.c +11 -14
-out:
  	return ret;
  }
 
  /* torch */
 
-/* torch pin config for lm3642*/
+/* torch pin config for lm3642 */
  static ssize_t lm3642_torch_pin_store(struct device *dev,
  				      struct device_attribute *attr,
  				      const char *buf, size_t size)
@@ -182,7 +181,7 @@ static ssize_t lm3642_torch_pin_store(struct device *dev,
 
  	ret = kstrtouint(buf, 10, &state);
  	if (ret)
-		goto out_strtoint;
+		return ret;
  	if (state != 0)
  		state = 0x01 << TORCH_PIN_EN_SHIFT;
 
@@ -190,16 +189,12 @@ static ssize_t lm3642_torch_pin_store(struct device *dev,
  	ret = regmap_update_bits(chip->regmap, REG_ENABLE,
  				 TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT,
  				 state);
-	if (ret < 0)
-		goto out;
+	if (ret < 0) {
+		dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
+		return ret;
+	}
 
  	return size;
-out:
-	dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
-	return ret;
-out_strtoint:
-	dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
-	return ret;
  }
 
  static DEVICE_ATTR(torch_pin, S_IWUSR, NULL, lm3642_torch_pin_store);
@@ -233,7 +228,7 @@ static ssize_t lm3642_strobe_pin_store(struct device *dev,
 
  	ret = kstrtouint(buf, 10, &state);
  	if (ret)
-		goto out_strtoint;
+		return ret;
  	if (state != 0)
  		state = 0x01 << STROBE_PIN_EN_SHIFT;
 
@@ -241,16 +236,12 @@ static ssize_t lm3642_strobe_pin_store(struct device *dev,
  	ret = regmap_update_bits(chip->regmap, REG_ENABLE,
  				 STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT,
  				 state);
-	if (ret < 0)
-		goto out;
+	if (ret < 0) {
+		dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
+		return ret;
+	}
 

drivers/leds/leds-lm3642.c static ssize_t lm3642_strobe_pin_store(struct device *dev, · line 247

Quoted 10 lines
drivers/leds/leds-lm3642.c +0 -6
  	return size;
-out:
-	dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
-	return ret;
-out_strtoint:
-	dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
-	return ret;
  }
 
  static DEVICE_ATTR(strobe_pin, S_IWUSR, NULL, lm3642_strobe_pin_store);

While we are at it I'd also switch to device managed LED registration API.

Signature · 2 lines
Best regards,
Jacek Anaszewski