public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	linux-media@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Tianshu Qiu <tian.shu.qiu@intel.com>,
	Shawn Tu <shawnx.tu@intel.com>, Bingbu Cao <bingbu.cao@intel.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>
Subject: [PATCH v1 2/8] media: ov2740: Replace voodoo coding with understandle flow
Date: Tue, 26 Jul 2022 15:05:50 +0300	[thread overview]
Message-ID: <20220726120556.2881-2-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20220726120556.2881-1-andriy.shevchenko@linux.intel.com>

Besides not being understandable at the first glance, the code
might provoke a compiler or a static analyser tool to warn about
out-of-bound access (when len == 0).

Replace it with clear flow an understandable intention.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/media/i2c/ov2740.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/drivers/media/i2c/ov2740.c b/drivers/media/i2c/ov2740.c
index c975db1bbe8c..81c0ab220339 100644
--- a/drivers/media/i2c/ov2740.c
+++ b/drivers/media/i2c/ov2740.c
@@ -377,10 +377,10 @@ static int ov2740_read_reg(struct ov2740 *ov2740, u16 reg, u16 len, u32 *val)
 	struct i2c_client *client = v4l2_get_subdevdata(&ov2740->sd);
 	struct i2c_msg msgs[2];
 	u8 addr_buf[2];
-	u8 data_buf[4] = {0};
+	u8 data_buf[4];
 	int ret = 0;
 
-	if (len > sizeof(data_buf))
+	if (len > 4)
 		return -EINVAL;
 
 	put_unaligned_be16(reg, addr_buf);
@@ -391,13 +391,22 @@ static int ov2740_read_reg(struct ov2740 *ov2740, u16 reg, u16 len, u32 *val)
 	msgs[1].addr = client->addr;
 	msgs[1].flags = I2C_M_RD;
 	msgs[1].len = len;
-	msgs[1].buf = &data_buf[sizeof(data_buf) - len];
+	msgs[1].buf = data_buf;
 
 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
 	if (ret != ARRAY_SIZE(msgs))
 		return ret < 0 ? ret : -EIO;
 
-	*val = get_unaligned_be32(data_buf);
+	if (len == 4)
+		*val = get_unaligned_be32(data_buf);
+	else if (len == 3)
+		*val = get_unaligned_be24(data_buf);
+	else if (len == 2)
+		*val = get_unaligned_be16(data_buf);
+	else if (len == 1)
+		*val = data_buf[0];
+	else
+		return -EINVAL;
 
 	return 0;
 }
@@ -412,7 +421,16 @@ static int ov2740_write_reg(struct ov2740 *ov2740, u16 reg, u16 len, u32 val)
 		return -EINVAL;
 
 	put_unaligned_be16(reg, buf);
-	put_unaligned_be32(val << 8 * (4 - len), buf + 2);
+	if (len == 4)
+		put_unaligned_be32(val, buf + 2);
+	else if (len == 3)
+		put_unaligned_be24(val, buf + 2);
+	else if (len == 2)
+		put_unaligned_be16(val, buf + 2);
+	else if (len == 1)
+		buf[2] = val;
+	else
+		return -EINVAL;
 
 	ret = i2c_master_send(client, buf, len + 2);
 	if (ret != len + 2)
-- 
2.35.1


  reply	other threads:[~2022-07-26 12:06 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-26 12:05 [PATCH v1 1/8] media: ov2740: Remove duplicative pointer in struct nvm_data Andy Shevchenko
2022-07-26 12:05 ` Andy Shevchenko [this message]
2022-07-27  9:52   ` [PATCH v1 2/8] media: ov2740: Replace voodoo coding with understandle flow Cao, Bingbu
2022-11-11 15:02   ` Sakari Ailus
2022-11-11 15:30     ` Andy Shevchenko
2022-11-11 19:46       ` Sakari Ailus
2022-07-26 12:05 ` [PATCH v1 3/8] media: ov2740: Switch from __maybe_unused to pm_sleep_ptr() etc Andy Shevchenko
2022-07-26 12:05 ` [PATCH v1 4/8] media: ov2740: Remove duplicate check for NULL fwnode Andy Shevchenko
2022-07-27  9:41   ` Cao, Bingbu
2022-07-27 10:01   ` Cao, Bingbu
2022-07-26 12:05 ` [PATCH v1 5/8] media: ov2740: Drop redundant assignments of ret = 0 Andy Shevchenko
2022-07-26 12:05 ` [PATCH v1 6/8] media: ov2740: Switch to use dev_err_probe() Andy Shevchenko
2022-07-27 10:06   ` Cao, Bingbu
2022-07-26 12:05 ` [PATCH v1 7/8] media: ov2740: Add missed \n to the end of the messages Andy Shevchenko
2022-07-27 10:01   ` Cao, Bingbu
2022-07-26 12:05 ` [PATCH v1 8/8] media: ov2740: Use traditional pattern when checking error codes Andy Shevchenko
2022-08-23 14:10 ` [PATCH v1 1/8] media: ov2740: Remove duplicative pointer in struct nvm_data Andy Shevchenko
2022-11-11 12:05   ` Andy Shevchenko
2022-11-11 12:08     ` Andy Shevchenko
2022-11-11 14:58       ` Sakari Ailus
2022-11-11 15:29         ` Andy Shevchenko
2022-11-11 19:41           ` Sakari Ailus
2022-11-11 19:47             ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220726120556.2881-2-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=bingbu.cao@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=shawnx.tu@intel.com \
    --cc=tian.shu.qiu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox