Skip to contentSkip to conversation

iio: imu: st_lsm6dsx: Fix selection of ST_LSM6DS3_ID

PATCH Applied Stephan Gerhold 4 participants 8 messages Dec 9, 2019 – Jan 14, 2020

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: imu: st_lsm6dsx: Fix selection of ST_LSM6DS3_ID

At the moment, attempting to probe a device with ST_LSM6DS3_ID (e.g. using the st,lsm6ds3 compatible) fails with:

    st_lsm6dsx_i2c 1-006b: unsupported whoami [69]

... even though 0x69 is the whoami listed for ST_LSM6DS3_ID.

This happens because st_lsm6dsx_check_whoami() also attempts to match unspecified (zero-initialized) entries in the "id" array. ST_LSM6DS3_ID = 0 will therefore match any entry in st_lsm6dsx_sensor_settings (here: the first), because none of them actually have all 12 entries listed in the "id" array.

Avoid this by additionally checking if "name" is set, which is only set for valid entries in the "id" array.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +2 -1
index a7d40c02ce6b..b921dd9e108f 100644
@@ -1301,7 +1301,8 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
 
 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
-			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
+			if (st_lsm6dsx_sensor_settings[i].id[j].name &&
+			    id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
 				break;
 		}
 		if (j < ST_LSM6DSX_MAX_ID)
Signature · 1 line
2.24.0
maintainer
#1 lore raw
Quoted 7 lines

At the moment, attempting to probe a device with ST_LSM6DS3_ID (e.g. using the st,lsm6ds3 compatible) fails with:

    st_lsm6dsx_i2c 1-006b: unsupported whoami [69]

... even though 0x69 is the whoami listed for ST_LSM6DS3_ID.

Hi Stephan,

thx for working on this. I guess we can skip 'void' iterations defining the array real size, do you agree?

Regards,
Lorenzo

Quoted 31 lines

This happens because st_lsm6dsx_check_whoami() also attempts to match unspecified (zero-initialized) entries in the "id" array. ST_LSM6DS3_ID = 0 will therefore match any entry in st_lsm6dsx_sensor_settings (here: the first), because none of them actually have all 12 entries listed in the "id" array.

Avoid this by additionally checking if "name" is set, which is only set for valid entries in the "id" array.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +2 -1
index a7d40c02ce6b..b921dd9e108f 100644
@@ -1301,7 +1301,8 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
 
 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
-			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
+			if (st_lsm6dsx_sensor_settings[i].id[j].name &&
+			    id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
 				break;
 		}
 		if (j < ST_LSM6DSX_MAX_ID)
Signature · 1 line
2.24.0
#2 lore raw
Quoted 5 lines · Lorenzo Bianconi
Quoted 5 lines

At the moment, attempting to probe a device with ST_LSM6DS3_ID (e.g. using the st,lsm6ds3 compatible) fails with:

    st_lsm6dsx_i2c 1-006b: unsupported whoami [69]
Quoted 8 lines
Quoted 2 lines

... even though 0x69 is the whoami listed for ST_LSM6DS3_ID.

Hi Stephan,

thx for working on this. I guess we can skip 'void' iterations defining the array real size, do you agree?

I'm not sure I understand you correctly.
Do you mean having something like:

struct st_lsm6dsx_settings {
	u8 wai;
	/* ... */
	struct {
		enum st_lsm6dsx_hw_id hw_id;
		const char *name;
	} id[ST_LSM6DSX_MAX_ID];
	int id_num; /* Add this field */
	/* ... */
};

And then change the loop to use .id_num instead?

I think it is pretty easy to forget to update "id_num"
when adding new entries. Right now there is no need to worry about that.

Thanks,
Stephan

Quoted 34 lines

Regards,
Lorenzo

Quoted 31 lines

This happens because st_lsm6dsx_check_whoami() also attempts to match unspecified (zero-initialized) entries in the "id" array. ST_LSM6DS3_ID = 0 will therefore match any entry in st_lsm6dsx_sensor_settings (here: the first), because none of them actually have all 12 entries listed in the "id" array.

Avoid this by additionally checking if "name" is set, which is only set for valid entries in the "id" array.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +2 -1
index a7d40c02ce6b..b921dd9e108f 100644
@@ -1301,7 +1301,8 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
 
 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
-			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
+			if (st_lsm6dsx_sensor_settings[i].id[j].name &&
+			    id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
 				break;
 		}
 		if (j < ST_LSM6DSX_MAX_ID)
Signature · 1 line
2.24.0
maintainer Acked-by
#3 lore raw
Quoted 29 lines · Lorenzo Bianconi
Quoted 5 lines
Quoted 5 lines

At the moment, attempting to probe a device with ST_LSM6DS3_ID (e.g. using the st,lsm6ds3 compatible) fails with:

    st_lsm6dsx_i2c 1-006b: unsupported whoami [69]
Quoted 8 lines
Quoted 2 lines

... even though 0x69 is the whoami listed for ST_LSM6DS3_ID.

Hi Stephan,

thx for working on this. I guess we can skip 'void' iterations defining the array real size, do you agree?

I'm not sure I understand you correctly.
Do you mean having something like:

struct st_lsm6dsx_settings {
	u8 wai;
	/* ... */
	struct {
		enum st_lsm6dsx_hw_id hw_id;
		const char *name;
	} id[ST_LSM6DSX_MAX_ID];
	int id_num; /* Add this field */
	/* ... */
};
Quoted 4 lines

And then change the loop to use .id_num instead?

I think it is pretty easy to forget to update "id_num"
when adding new entries. Right now there is no need to worry about that.

Uhm..this approach is even more safe if someone forgets to set name for a given device. So for the patch:

Regards,
Lorenzo

Quoted 40 lines

Thanks,
Stephan

Quoted 34 lines

Regards,
Lorenzo

Quoted 31 lines

This happens because st_lsm6dsx_check_whoami() also attempts to match unspecified (zero-initialized) entries in the "id" array. ST_LSM6DS3_ID = 0 will therefore match any entry in st_lsm6dsx_sensor_settings (here: the first), because none of them actually have all 12 entries listed in the "id" array.

Avoid this by additionally checking if "name" is set, which is only set for valid entries in the "id" array.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +2 -1
index a7d40c02ce6b..b921dd9e108f 100644
@@ -1301,7 +1301,8 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
 
 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
-			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
+			if (st_lsm6dsx_sensor_settings[i].id[j].name &&
+			    id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
 				break;
 		}
 		if (j < ST_LSM6DSX_MAX_ID)
Signature · 1 line
2.24.0
maintainer
#4 lore raw
In reply to Stephan Gerhold
Quoted 13 lines · On Mon, 9 Dec 2019 18:05:41 +0100 Stephan Gerhold <[email protected]> wrote:

At the moment, attempting to probe a device with ST_LSM6DS3_ID (e.g. using the st,lsm6ds3 compatible) fails with:

    st_lsm6dsx_i2c 1-006b: unsupported whoami [69]

... even though 0x69 is the whoami listed for ST_LSM6DS3_ID.

This happens because st_lsm6dsx_check_whoami() also attempts to match unspecified (zero-initialized) entries in the "id" array. ST_LSM6DS3_ID = 0 will therefore match any entry in st_lsm6dsx_sensor_settings (here: the first), because none of them actually have all 12 entries listed in the "id" array.

Quoted 4 lines

Avoid this by additionally checking if "name" is set, which is only set for valid entries in the "id" array.

Definitely sounds like this wants backporting.

If you can figure out a fixes tag that would be great!

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +2 -1
index a7d40c02ce6b..b921dd9e108f 100644
@@ -1301,7 +1301,8 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
 
 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
-			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
+			if (st_lsm6dsx_sensor_settings[i].id[j].name &&
+			    id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
 				break;
 		}
 		if (j < ST_LSM6DSX_MAX_ID)
maintainer Applied
#5 lore raw
Quoted 16 lines · On Sun, 15 Dec 2019 17:23:42 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 13 lines · On Mon, 9 Dec 2019 18:05:41 +0100 Stephan Gerhold <[email protected]> wrote:

At the moment, attempting to probe a device with ST_LSM6DS3_ID (e.g. using the st,lsm6ds3 compatible) fails with:

    st_lsm6dsx_i2c 1-006b: unsupported whoami [69]

... even though 0x69 is the whoami listed for ST_LSM6DS3_ID.

This happens because st_lsm6dsx_check_whoami() also attempts to match unspecified (zero-initialized) entries in the "id" array. ST_LSM6DS3_ID = 0 will therefore match any entry in st_lsm6dsx_sensor_settings (here: the first), because none of them actually have all 12 entries listed in the "id" array.

Quoted 7 lines
Quoted 4 lines

Avoid this by additionally checking if "name" is set, which is only set for valid entries in the "id" array.

Definitely sounds like this wants backporting.

If you can figure out a fixes tag that would be great!

I've taken a stab at working out when this got introduced and came up with:

81956a93b522 "iio: imu: st_lsm6dsx: get device name from st_lsm6dsx_sensor_settings"

If that's wrong please let me know asap.

Applied to the togreg branch of iio.git as we are near the merge window opening and marked for stable.

Thanks,

Jonathan

Quoted 24 lines

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +2 -1
index a7d40c02ce6b..b921dd9e108f 100644
@@ -1301,7 +1301,8 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
 
 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
-			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
+			if (st_lsm6dsx_sensor_settings[i].id[j].name &&
+			    id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
 				break;
 		}
 		if (j < ST_LSM6DSX_MAX_ID)  
#6 lore raw
Quoted 29 lines · Jonathan Cameron
Quoted 16 lines · On Sun, 15 Dec 2019 17:23:42 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 13 lines · On Mon, 9 Dec 2019 18:05:41 +0100 Stephan Gerhold <[email protected]> wrote:

At the moment, attempting to probe a device with ST_LSM6DS3_ID (e.g. using the st,lsm6ds3 compatible) fails with:

    st_lsm6dsx_i2c 1-006b: unsupported whoami [69]

... even though 0x69 is the whoami listed for ST_LSM6DS3_ID.

This happens because st_lsm6dsx_check_whoami() also attempts to match unspecified (zero-initialized) entries in the "id" array. ST_LSM6DS3_ID = 0 will therefore match any entry in st_lsm6dsx_sensor_settings (here: the first), because none of them actually have all 12 entries listed in the "id" array.

Quoted 7 lines
Quoted 4 lines

Avoid this by additionally checking if "name" is set, which is only set for valid entries in the "id" array.

Definitely sounds like this wants backporting.

If you can figure out a fixes tag that would be great!

I've taken a stab at working out when this got introduced and came up with:

Quoted 6 lines

81956a93b522 "iio: imu: st_lsm6dsx: get device name from st_lsm6dsx_sensor_settings"

If that's wrong please let me know asap.

Applied to the togreg branch of iio.git as we are near the merge window opening and marked for stable.

You have already applied the v2 I sent with the fixes/cc stable tags :) It's part of your "Second set of IIO fixes for the 5.5 cycle." pull request:
https://lore.kernel.org/linux-iio/20200105110051.445c9a95@archlinux/

Quoted 29 lines

Thanks,

Jonathan

Quoted 24 lines

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +2 -1
index a7d40c02ce6b..b921dd9e108f 100644
@@ -1301,7 +1301,8 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
 
 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
-			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
+			if (st_lsm6dsx_sensor_settings[i].id[j].name &&
+			    id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
 				break;
 		}
 		if (j < ST_LSM6DSX_MAX_ID)  
#7 lore raw
Quoted 32 lines · On Tue, 14 Jan 2020 10:06:52 +0100 Stephan Gerhold <[email protected]> wrote:
Quoted 31 lines · Jonathan Cameron
Quoted 16 lines · On Sun, 15 Dec 2019 17:23:42 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 13 lines · On Mon, 9 Dec 2019 18:05:41 +0100 Stephan Gerhold <[email protected]> wrote:

At the moment, attempting to probe a device with ST_LSM6DS3_ID (e.g. using the st,lsm6ds3 compatible) fails with:

    st_lsm6dsx_i2c 1-006b: unsupported whoami [69]

... even though 0x69 is the whoami listed for ST_LSM6DS3_ID.

This happens because st_lsm6dsx_check_whoami() also attempts to match unspecified (zero-initialized) entries in the "id" array. ST_LSM6DS3_ID = 0 will therefore match any entry in st_lsm6dsx_sensor_settings (here: the first), because none of them actually have all 12 entries listed in the "id" array.

Quoted 7 lines
Quoted 4 lines

Avoid this by additionally checking if "name" is set, which is only set for valid entries in the "id" array.

Definitely sounds like this wants backporting.

If you can figure out a fixes tag that would be great!

I've taken a stab at working out when this got introduced and came up with:

81956a93b522 "iio: imu: st_lsm6dsx: get device name from st_lsm6dsx_sensor_settings"

Quoted 8 lines
Quoted 4 lines

If that's wrong please let me know asap.

Applied to the togreg branch of iio.git as we are near the merge window opening and marked for stable.

You have already applied the v2 I sent with the fixes/cc stable tags :) It's part of your "Second set of IIO fixes for the 5.5 cycle." pull request:
https://lore.kernel.org/linux-iio/20200105110051.445c9a95@archlinux/

Gah! I clearly wasn't on top form last night. Will drop this one again.

Thanks.

Jonathan

Quoted 31 lines
Quoted 30 lines

Thanks,

Jonathan

Quoted 24 lines

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +2 -1
index a7d40c02ce6b..b921dd9e108f 100644
@@ -1301,7 +1301,8 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
 
 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
-			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
+			if (st_lsm6dsx_sensor_settings[i].id[j].name &&
+			    id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
 				break;
 		}
 		if (j < ST_LSM6DSX_MAX_ID)