From: Dumitru Ceclan <mitrutzceclan@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>,
Stefan Popa <stefan.popa@analog.com>,
Alexandru Tachici <alexandru.tachici@analog.com>
Cc: Dumitru Ceclan <mitrutzceclan@gmail.com>,
Jonathan Cameron <Jonathan.Cameron@huawei.com>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
Dumitru Ceclan <dumitru.ceclan@analog.com>
Subject: [PATCH 2/4] iio: adc: ad7124: fix config comparison
Date: Wed, 31 Jul 2024 15:37:23 +0300 [thread overview]
Message-ID: <20240731-ad7124-fix-v1-2-46a76aa4b9be@analog.com> (raw)
In-Reply-To: <20240731-ad7124-fix-v1-0-46a76aa4b9be@analog.com>
The ad7124_find_similar_live_cfg() computes the compare size by
substracting the address of the cfg struct from the address of the live
field. Because the live field is the first field in the struct, the
result is 0.
Also, the memcmp() call is made from the start of the cfg struct, which
includes the live and cfg_slot fields, which are not relevant for the
comparison.
Fix by grouping the relevant fields with struct_group() and use the
size of the group to compute the compare size; make the memcmp() call
from the address of the group.
Fixes: 7b8d045e497a ("iio: adc: ad7124: allow more than 8 channels")
Signed-off-by: Dumitru Ceclan <dumitru.ceclan@analog.com>
---
drivers/iio/adc/ad7124.c | 26 +++++++++++++++-----------
1 file changed, 15 insertions(+), 11 deletions(-)
diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
index 54d4c5597696..bd323c6bd756 100644
--- a/drivers/iio/adc/ad7124.c
+++ b/drivers/iio/adc/ad7124.c
@@ -147,15 +147,18 @@ struct ad7124_chip_info {
struct ad7124_channel_config {
bool live;
unsigned int cfg_slot;
- enum ad7124_ref_sel refsel;
- bool bipolar;
- bool buf_positive;
- bool buf_negative;
- unsigned int vref_mv;
- unsigned int pga_bits;
- unsigned int odr;
- unsigned int odr_sel_bits;
- unsigned int filter_type;
+ /* Following fields are used to compare equality. */
+ struct_group(config_props,
+ enum ad7124_ref_sel refsel;
+ bool bipolar;
+ bool buf_positive;
+ bool buf_negative;
+ unsigned int vref_mv;
+ unsigned int pga_bits;
+ unsigned int odr;
+ unsigned int odr_sel_bits;
+ unsigned int filter_type;
+ );
};
struct ad7124_channel {
@@ -334,11 +337,12 @@ static struct ad7124_channel_config *ad7124_find_similar_live_cfg(struct ad7124_
ptrdiff_t cmp_size;
int i;
- cmp_size = (u8 *)&cfg->live - (u8 *)cfg;
+ cmp_size = sizeof_field(struct ad7124_channel_config, config_props);
for (i = 0; i < st->num_channels; i++) {
cfg_aux = &st->channels[i].cfg;
- if (cfg_aux->live && !memcmp(cfg, cfg_aux, cmp_size))
+ if (cfg_aux->live &&
+ !memcmp(&cfg->config_props, &cfg_aux->config_props, cmp_size))
return cfg_aux;
}
--
2.43.0
next prev parent reply other threads:[~2024-07-31 12:37 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-31 12:37 [PATCH 0/4] ad7124 fixes and improvements Dumitru Ceclan
2024-07-31 12:37 ` [PATCH 1/4] iio: adc: ad7124: fix chip ID mismatch Dumitru Ceclan
2024-08-03 15:03 ` Jonathan Cameron
2024-07-31 12:37 ` Dumitru Ceclan [this message]
2024-08-03 15:04 ` [PATCH 2/4] iio: adc: ad7124: fix config comparison Jonathan Cameron
2024-07-31 12:37 ` [PATCH 3/4] iio: adc: ad7124: reduce the number of SPI transfers Dumitru Ceclan
2024-08-03 15:07 ` Jonathan Cameron
2024-07-31 12:37 ` [PATCH 4/4] iio: adc: ad7124: set initial ADC mode to idle Dumitru Ceclan
2024-08-01 8:36 ` [PATCH 0/4] ad7124 fixes and improvements Nuno Sá
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=20240731-ad7124-fix-v1-2-46a76aa4b9be@analog.com \
--to=mitrutzceclan@gmail.com \
--cc=Jonathan.Cameron@huawei.com \
--cc=alexandru.tachici@analog.com \
--cc=dumitru.ceclan@analog.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=stefan.popa@analog.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