Skip to content

Commit 2f6b92d

Browse files
ceclandumitrujic23
authored andcommitted
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 <[email protected]> Reviewed-by: Nuno Sa <[email protected]> Link: https://patch.msgid.link/[email protected] Cc: <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 96f9ab0 commit 2f6b92d

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
@@ -147,15 +147,18 @@ struct ad7124_chip_info {
147147
struct ad7124_channel_config {
148148
bool live;
149149
unsigned int cfg_slot;
150-
enum ad7124_ref_sel refsel;
151-
bool bipolar;
152-
bool buf_positive;
153-
bool buf_negative;
154-
unsigned int vref_mv;
155-
unsigned int pga_bits;
156-
unsigned int odr;
157-
unsigned int odr_sel_bits;
158-
unsigned int filter_type;
150+
/* Following fields are used to compare equality. */
151+
struct_group(config_props,
152+
enum ad7124_ref_sel refsel;
153+
bool bipolar;
154+
bool buf_positive;
155+
bool buf_negative;
156+
unsigned int vref_mv;
157+
unsigned int pga_bits;
158+
unsigned int odr;
159+
unsigned int odr_sel_bits;
160+
unsigned int filter_type;
161+
);
159162
};
160163

161164
struct ad7124_channel {
@@ -334,11 +337,12 @@ static struct ad7124_channel_config *ad7124_find_similar_live_cfg(struct ad7124_
334337
ptrdiff_t cmp_size;
335338
int i;
336339

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

341-
if (cfg_aux->live && !memcmp(cfg, cfg_aux, cmp_size))
344+
if (cfg_aux->live &&
345+
!memcmp(&cfg->config_props, &cfg_aux->config_props, cmp_size))
342346
return cfg_aux;
343347
}
344348

0 commit comments

Comments
 (0)