Skip to content

Commit 8f2273b

Browse files
commodojic23
authored andcommitted
iio: adc: ad7192: fix null pointer de-reference crash during probe
When the 'spi_device_id' table was removed, it omitted to cleanup/fix the assignment: 'indio_dev->name = spi_get_device_id(spi)->name;' After that patch 'spi_get_device_id(spi)' returns NULL, so this crashes during probe with null de-ref. This change fixes this by introducing an ad7192_chip_info struct, and defines all part-names [that should be assigned to indio_dev->name] in a 'ad7192_chip_info_tbl' table. With this change, the old 'st->devid' is also moved to be a 'chip_info->chip_id'. And the old 'ID_AD719X' macros have been renamed to 'CHIPID_AD719X'. Tld identifiers have been re-purposed to be enum/index values in the new 'ad7192_chip_info_tbl'. This should fix the bug, and maintain the ABI for the 'indio_dev->name' field. Fixes: 66614ab ("staging: iio: adc: ad7192: removed spi_device_id") Signed-off-by: Alexandru Ardelean <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent a074791 commit 8f2273b

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

drivers/iio/adc/ad7192.c

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@
125125
#define AD7193_CH_AINCOM 0x600 /* AINCOM - AINCOM */
126126

127127
/* ID Register Bit Designations (AD7192_REG_ID) */
128-
#define ID_AD7190 0x4
129-
#define ID_AD7192 0x0
130-
#define ID_AD7193 0x2
131-
#define ID_AD7195 0x6
128+
#define CHIPID_AD7190 0x4
129+
#define CHIPID_AD7192 0x0
130+
#define CHIPID_AD7193 0x2
131+
#define CHIPID_AD7195 0x6
132132
#define AD7192_ID_MASK 0x0F
133133

134134
/* GPOCON Register Bit Designations (AD7192_REG_GPOCON) */
@@ -161,7 +161,20 @@ enum {
161161
AD7192_SYSCALIB_FULL_SCALE,
162162
};
163163

164+
enum {
165+
ID_AD7190,
166+
ID_AD7192,
167+
ID_AD7193,
168+
ID_AD7195,
169+
};
170+
171+
struct ad7192_chip_info {
172+
unsigned int chip_id;
173+
const char *name;
174+
};
175+
164176
struct ad7192_state {
177+
const struct ad7192_chip_info *chip_info;
165178
struct regulator *avdd;
166179
struct regulator *dvdd;
167180
struct clk *mclk;
@@ -172,7 +185,6 @@ struct ad7192_state {
172185
u32 conf;
173186
u32 scale_avail[8][2];
174187
u8 gpocon;
175-
u8 devid;
176188
u8 clock_sel;
177189
struct mutex lock; /* protect sensor state */
178190
u8 syscalib_mode[8];
@@ -348,7 +360,7 @@ static int ad7192_setup(struct ad7192_state *st, struct device_node *np)
348360

349361
id &= AD7192_ID_MASK;
350362

351-
if (id != st->devid)
363+
if (id != st->chip_info->chip_id)
352364
dev_warn(&st->sd.spi->dev, "device ID query failed (0x%X)\n",
353365
id);
354366

@@ -363,7 +375,7 @@ static int ad7192_setup(struct ad7192_state *st, struct device_node *np)
363375
st->mode |= AD7192_MODE_REJ60;
364376

365377
refin2_en = of_property_read_bool(np, "adi,refin2-pins-enable");
366-
if (refin2_en && st->devid != ID_AD7195)
378+
if (refin2_en && st->chip_info->chip_id != CHIPID_AD7195)
367379
st->conf |= AD7192_CONF_REFSEL;
368380

369381
st->conf &= ~AD7192_CONF_CHOP;
@@ -859,12 +871,31 @@ static const struct iio_chan_spec ad7193_channels[] = {
859871
IIO_CHAN_SOFT_TIMESTAMP(14),
860872
};
861873

874+
static const struct ad7192_chip_info ad7192_chip_info_tbl[] = {
875+
[ID_AD7190] = {
876+
.chip_id = CHIPID_AD7190,
877+
.name = "ad7190",
878+
},
879+
[ID_AD7192] = {
880+
.chip_id = CHIPID_AD7192,
881+
.name = "ad7192",
882+
},
883+
[ID_AD7193] = {
884+
.chip_id = CHIPID_AD7193,
885+
.name = "ad7193",
886+
},
887+
[ID_AD7195] = {
888+
.chip_id = CHIPID_AD7195,
889+
.name = "ad7195",
890+
},
891+
};
892+
862893
static int ad7192_channels_config(struct iio_dev *indio_dev)
863894
{
864895
struct ad7192_state *st = iio_priv(indio_dev);
865896

866-
switch (st->devid) {
867-
case ID_AD7193:
897+
switch (st->chip_info->chip_id) {
898+
case CHIPID_AD7193:
868899
indio_dev->channels = ad7193_channels;
869900
indio_dev->num_channels = ARRAY_SIZE(ad7193_channels);
870901
break;
@@ -878,10 +909,10 @@ static int ad7192_channels_config(struct iio_dev *indio_dev)
878909
}
879910

880911
static const struct of_device_id ad7192_of_match[] = {
881-
{ .compatible = "adi,ad7190", .data = (void *)ID_AD7190 },
882-
{ .compatible = "adi,ad7192", .data = (void *)ID_AD7192 },
883-
{ .compatible = "adi,ad7193", .data = (void *)ID_AD7193 },
884-
{ .compatible = "adi,ad7195", .data = (void *)ID_AD7195 },
912+
{ .compatible = "adi,ad7190", .data = &ad7192_chip_info_tbl[ID_AD7190] },
913+
{ .compatible = "adi,ad7192", .data = &ad7192_chip_info_tbl[ID_AD7192] },
914+
{ .compatible = "adi,ad7193", .data = &ad7192_chip_info_tbl[ID_AD7193] },
915+
{ .compatible = "adi,ad7195", .data = &ad7192_chip_info_tbl[ID_AD7195] },
885916
{}
886917
};
887918
MODULE_DEVICE_TABLE(of, ad7192_of_match);
@@ -938,16 +969,16 @@ static int ad7192_probe(struct spi_device *spi)
938969
}
939970

940971
spi_set_drvdata(spi, indio_dev);
941-
st->devid = (unsigned long)of_device_get_match_data(&spi->dev);
972+
st->chip_info = of_device_get_match_data(&spi->dev);
942973
indio_dev->dev.parent = &spi->dev;
943-
indio_dev->name = spi_get_device_id(spi)->name;
974+
indio_dev->name = st->chip_info->name;
944975
indio_dev->modes = INDIO_DIRECT_MODE;
945976

946977
ret = ad7192_channels_config(indio_dev);
947978
if (ret < 0)
948979
goto error_disable_dvdd;
949980

950-
if (st->devid == ID_AD7195)
981+
if (st->chip_info->chip_id == CHIPID_AD7195)
951982
indio_dev->info = &ad7195_info;
952983
else
953984
indio_dev->info = &ad7192_info;

0 commit comments

Comments
 (0)