From patchwork Thu Oct 26 12:17:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SF Markus Elfring X-Patchwork-Id: 10028031 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 586166032C for ; Thu, 26 Oct 2017 12:17:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3CCB328459 for ; Thu, 26 Oct 2017 12:17:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2FBC428DEE; Thu, 26 Oct 2017 12:17:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67BB528459 for ; Thu, 26 Oct 2017 12:17:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932185AbdJZMR5 (ORCPT ); Thu, 26 Oct 2017 08:17:57 -0400 Received: from mout.web.de ([212.227.17.12]:64088 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932162AbdJZMR4 (ORCPT ); Thu, 26 Oct 2017 08:17:56 -0400 Received: from [192.168.1.2] ([78.48.35.118]) by smtp.web.de (mrweb101 [213.165.67.124]) with ESMTPSA (Nemesis) id 0M8R26-1dCp3N0a5C-00vunm; Thu, 26 Oct 2017 14:17:35 +0200 To: linux-iio@vger.kernel.org, Andreas Dannenberg , Felipe Balbi , Hartmut Knaack , Javier Martinez Canillas , Jonathan Cameron , Lars-Peter Clausen , Peter Meerwald-Stadler From: SF Markus Elfring Subject: [PATCH] iio/light/opt3001: Use common error handling code in opt3001_get_lux() Cc: LKML , kernel-janitors@vger.kernel.org Message-ID: Date: Thu, 26 Oct 2017 14:17:30 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 Content-Language: en-GB X-Provags-ID: V03:K0:KOTj7pCWpPv0WWoYBCrhjRaPZ9HzLpy15OlYJd+9dUxja6An6bO uuciMLC6a6PpR0SlPGTQSygZUHc/mGlsKd1w5y5LCfyK355c58DdkQZVuos4MsptpXmL4XD xpYCFkI+Dh5wnY+kvze3Lz6Mkgf1hDP9ZYYsMW/C0Rh85nsN9DctCQSSVdfe8dn7W4sinTI YITJoA7duN1y7z/Kkeg/g== X-UI-Out-Filterresults: notjunk:1; V01:K0:EVZme+QF4eY=:K+DyJtLS5HFlC3Xut/1tXR /pYgVsg2ETxjrY6WNAFmlcR2LYoGRttVJi9GekYaGtG8/FCQ18Ltd24X0d/4TTKQKqq7Y5KUT eznj8Z57VcKtuqNtO8PTnufA3AsmzephU0AegY0Lq0/ai9QDN54HpUhWQ2hpWbk6YLZITiWZG GjV+kB6tcheXCzLIO5+c1hwX//aCw9M2VG2pjZkuQHpagR/Onck5dsgsf4nHeIAmInxjdsTX8 PvN2BPzUe98n6wONJPY+HJPe9oWmMGZY5hnLZOVloMoQ+uu2BHllJMFZLcEepnVH8pX+/Wm8u DOrf/ZNN7s8JzCWqXZ3wS5MkskjoV3KSk3B0tdDxNv0vtlwIUMjY6t1kJQ60esaQQ1wAZGqmP Qx0BozIkQoitOihxP6ntPiZ0Rqu7peDTVZSHHqOduxk1Ki2zYFYDF1opjxRYw1QQk/JogJ1tG ebWPMGsVPzk1h8JkIzFxHWfrsbJFd/ikUR7dS7In5+nwSNXXrj0Z4YY5PfobMCIIbh42uCl5p riXzeu+aPW78qm7M6FyimeUrvk0Zg6aibjEEgE/qbmFU2ddA49B773uhe9Y2TuhTyQrl1f37K Of2C2xlZMYAVaPWcjOwErJ5Q8b8LfMjE1EctWPXhOuuXUZPdBFm9WxtP/hiixfT9FBRl5P5cT NCujwNf3x/5EcmWptySyYMvef1GPb3XbUFfS9iQKWqzt9z10u2OWMc5LwUabMsQsIAPVnms+u mT4Qfl/AMocEEcs63ad/ro765mURfOpKOdFjFud0FT+Y+koubwVuX96frPU3dydeFohrnnNq2 PKngO0kVKU7d3yUfbOGKQo0kkax3oEF1Bi5JX/2/w+wXnRppW4= Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Markus Elfring Date: Thu, 26 Oct 2017 14:06:49 +0200 * Add jump targets so that two error messages are stored only once at the end of this function implementation. * Adjust condition checks. * Replace string literals by references to two global constant variables in eight functions. This issue was detected by using the Coccinelle software. Signed-off-by: Markus Elfring --- drivers/iio/light/opt3001.c | 88 +++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 52 deletions(-) diff --git a/drivers/iio/light/opt3001.c b/drivers/iio/light/opt3001.c index 54d88b60e303..23d7d4e6ae61 100644 --- a/drivers/iio/light/opt3001.c +++ b/drivers/iio/light/opt3001.c @@ -77,6 +77,9 @@ #define OPT3001_RESULT_READY_SHORT 150 #define OPT3001_RESULT_READY_LONG 1000 +static char const read_failure[] = "failed to read register %02x\n"; +static char const write_failure[] = "failed to write register %02x\n"; + struct opt3001 { struct i2c_client *client; struct device *dev; @@ -246,11 +249,8 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_LOW_LIMIT, OPT3001_LOW_LIMIT_EOC_ENABLE); - if (ret < 0) { - dev_err(opt->dev, "failed to write register %02x\n", - OPT3001_LOW_LIMIT); - return ret; - } + if (ret) + goto report_write_failure; /* Allow IRQ to access the device despite lock being set */ opt->ok_to_ignore_lock = true; @@ -261,20 +261,16 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) /* Configure for single-conversion mode and start a new conversion */ ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); - if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_CONFIGURATION); - goto err; - } + if (ret < 0) + goto report_read_failure; reg = ret; opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SINGLE); ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, reg); - if (ret < 0) { - dev_err(opt->dev, "failed to write register %02x\n", - OPT3001_CONFIGURATION); + if (ret) { + dev_err(opt->dev, write_failure, OPT3001_CONFIGURATION); goto err; } @@ -292,11 +288,8 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) /* Check result ready flag */ ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); - if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_CONFIGURATION); - goto err; - } + if (ret < 0) + goto report_read_failure; if (!(ret & OPT3001_CONFIGURATION_CRF)) { ret = -ETIMEDOUT; @@ -306,8 +299,7 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) /* Obtain value */ ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_RESULT); + dev_err(opt->dev, read_failure, OPT3001_RESULT); goto err; } opt->result = ret; @@ -336,11 +328,8 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_LOW_LIMIT, value); - if (ret < 0) { - dev_err(opt->dev, "failed to write register %02x\n", - OPT3001_LOW_LIMIT); - return ret; - } + if (ret) + goto report_write_failure; } exponent = OPT3001_REG_EXPONENT(opt->result); @@ -349,6 +338,14 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) opt3001_to_iio_ret(opt, exponent, mantissa, val, val2); return IIO_VAL_INT_PLUS_MICRO; + +report_read_failure: + dev_err(opt->dev, read_failure, OPT3001_CONFIGURATION); + goto err; + +report_write_failure: + dev_err(opt->dev, write_failure, OPT3001_LOW_LIMIT); + return ret; } static int opt3001_get_int_time(struct opt3001 *opt, int *val, int *val2) @@ -366,8 +363,7 @@ static int opt3001_set_int_time(struct opt3001 *opt, int time) ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_CONFIGURATION); + dev_err(opt->dev, read_failure, OPT3001_CONFIGURATION); return ret; } @@ -521,7 +517,7 @@ static int opt3001_write_event_value(struct iio_dev *iio, ret = i2c_smbus_write_word_swapped(opt->client, reg, value); if (ret < 0) { - dev_err(opt->dev, "failed to write register %02x\n", reg); + dev_err(opt->dev, write_failure, reg); goto err; } @@ -562,8 +558,7 @@ static int opt3001_write_event_config(struct iio_dev *iio, ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_CONFIGURATION); + dev_err(opt->dev, read_failure, OPT3001_CONFIGURATION); goto err; } @@ -573,8 +568,7 @@ static int opt3001_write_event_config(struct iio_dev *iio, ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, reg); if (ret < 0) { - dev_err(opt->dev, "failed to write register %02x\n", - OPT3001_CONFIGURATION); + dev_err(opt->dev, write_failure, OPT3001_CONFIGURATION); goto err; } @@ -602,8 +596,7 @@ static int opt3001_read_id(struct opt3001 *opt) ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_MANUFACTURER_ID); + dev_err(opt->dev, read_failure, OPT3001_MANUFACTURER_ID); return ret; } @@ -612,8 +605,7 @@ static int opt3001_read_id(struct opt3001 *opt) ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_DEVICE_ID); + dev_err(opt->dev, read_failure, OPT3001_DEVICE_ID); return ret; } @@ -632,8 +624,7 @@ static int opt3001_configure(struct opt3001 *opt) ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_CONFIGURATION); + dev_err(opt->dev, read_failure, OPT3001_CONFIGURATION); return ret; } @@ -661,15 +652,13 @@ static int opt3001_configure(struct opt3001 *opt) ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, reg); if (ret < 0) { - dev_err(opt->dev, "failed to write register %02x\n", - OPT3001_CONFIGURATION); + dev_err(opt->dev, write_failure, OPT3001_CONFIGURATION); return ret; } ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_LOW_LIMIT); + dev_err(opt->dev, read_failure, OPT3001_LOW_LIMIT); return ret; } @@ -678,8 +667,7 @@ static int opt3001_configure(struct opt3001 *opt) ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_HIGH_LIMIT); + dev_err(opt->dev, read_failure, OPT3001_HIGH_LIMIT); return ret; } @@ -700,8 +688,7 @@ static irqreturn_t opt3001_irq(int irq, void *_iio) ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_CONFIGURATION); + dev_err(opt->dev, read_failure, OPT3001_CONFIGURATION); goto out; } @@ -722,8 +709,7 @@ static irqreturn_t opt3001_irq(int irq, void *_iio) } else if (ret & OPT3001_CONFIGURATION_CRF) { ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_RESULT); + dev_err(opt->dev, read_failure, OPT3001_RESULT); goto out; } opt->result = ret; @@ -810,8 +796,7 @@ static int opt3001_remove(struct i2c_client *client) ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); if (ret < 0) { - dev_err(opt->dev, "failed to read register %02x\n", - OPT3001_CONFIGURATION); + dev_err(opt->dev, read_failure, OPT3001_CONFIGURATION); return ret; } @@ -821,8 +806,7 @@ static int opt3001_remove(struct i2c_client *client) ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, reg); if (ret < 0) { - dev_err(opt->dev, "failed to write register %02x\n", - OPT3001_CONFIGURATION); + dev_err(opt->dev, write_failure, OPT3001_CONFIGURATION); return ret; }