Skip to content

Commit 12c259b

Browse files
committed
iio: adc: ad7124: fix config comparison
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: 7b8d045 ("iio: adc: ad7124: allow more than 8 channels") Signed-off-by: Dumitru Ceclan <dumitru.ceclan@analog.com>
1 parent e420633 commit 12c259b

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

drivers/iio/adc/ad7124.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,18 @@ struct ad7124_chip_info {
146146
struct ad7124_channel_config {
147147
bool live;
148148
unsigned int cfg_slot;
149-
enum ad7124_ref_sel refsel;
150-
bool bipolar;
151-
bool buf_positive;
152-
bool buf_negative;
153-
unsigned int vref_mv;
154-
unsigned int pga_bits;
155-
unsigned int odr;
156-
unsigned int odr_sel_bits;
157-
unsigned int filter_type;
149+
/* Following fields are used to compare equality. */
150+
struct_group(config_props,
151+
enum ad7124_ref_sel refsel;
152+
bool bipolar;
153+
bool buf_positive;
154+
bool buf_negative;
155+
unsigned int vref_mv;
156+
unsigned int pga_bits;
157+
unsigned int odr;
158+
unsigned int odr_sel_bits;
159+
unsigned int filter_type;
160+
);
158161
};
159162

160163
struct ad7124_channel {
@@ -333,11 +336,12 @@ static struct ad7124_channel_config *ad7124_find_similar_live_cfg(struct ad7124_
333336
ptrdiff_t cmp_size;
334337
int i;
335338

336-
cmp_size = (u8 *)&cfg->live - (u8 *)cfg;
339+
cmp_size = sizeof_field(struct ad7124_channel_config, config_props);
337340
for (i = 0; i < st->num_channels; i++) {
338341
cfg_aux = &st->channels[i].cfg;
339342

340-
if (cfg_aux->live && !memcmp(cfg, cfg_aux, cmp_size))
343+
if (cfg_aux->live &&
344+
!memcmp(&cfg->config_props, &cfg_aux->config_props, cmp_size))
341345
return cfg_aux;
342346
}
343347

0 commit comments

Comments
 (0)