iio: light: cm3232: fix remove ordering, locking and calibscale validation
Subsystems (3)
cover iio: light: cm3232: fix remove ordering, locking and calibscale validation
This series fixes three bugs in the cm3232 ambient light sensor driver, all present since the driver was added:
1. cm3232_remove() shuts the sensor down before unregistering the IIO device, so sysfs accesses in between can read a shut-down sensor or turn it back on.
2. Nothing serializes the read-modify-write of the cached command register (and calibscale) between read_raw(), write_raw(), suspend and resume.
3. in_illuminance_calibscale accepts zero, negative and fractional values, which give meaningless lux readings.
Patch 2 uses guard() and devm_mutex_init(), so older stable trees will need a manual backport of it.
The series is compile-tested only (W=1, sparse, checkpatch --strict); I don't have CM3232 hardware to test on.
Vineet Thakran (3):
iio: light: cm3232: unregister the device before powering it down
iio: light: cm3232: serialize access to the cached command register
iio: light: cm3232: reject invalid calibscale values
drivers/iio/light/cm3232.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
Signature · 1 line
2.43.0
PATCH 1/3 iio: light: cm3232: unregister the device before powering it down
cm3232_remove() shuts the sensor down before calling iio_device_unregister(), so the sysfs attributes stay live for a window after shutdown. A read of in_illuminance_input in that window reads the ALS register of a sensor that is shut down, and a write to in_illuminance_integration_time rewrites the command register from the cached regs_cmd, which does not have CM3232_CMD_ALS_DISABLE set, and so turns the sensor back on after remove has shut it down.
Unregister the IIO device first. Once iio_device_unregister() returns, the sysfs attributes are removed and no IIO callback can run, so the sensor stays shut down.
--- drivers/iio/light/cm3232.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/light/cm3232.c +2 -2
@@ -358,10 +358,10 @@ static void cm3232_remove(struct i2c_client *client) {struct iio_dev *indio_dev =i2c_get_clientdata (client); +iio_device_unregister (indio_dev); +i2c_smbus_write_byte_data (client,CM3232_REG_ADDR_CMD ,CM3232_CMD_ALS_DISABLE ); - -iio_device_unregister (indio_dev); }static const struct i2c_device_id cm3232_id[] = {
Signature · 1 line
2.43.0
PATCH 2/3 iio: light: cm3232: serialize access to the cached command register
In reply to Vineet ThakranThe driver keeps a copy of the command register in chip->regs_cmd. cm3232_write_als_it(), cm3232_suspend() and cm3232_resume() update it with a read-modify-write followed by an I2C write, and cm3232_get_lux() and cm3232_read_als_it() derive the integration time from it, but nothing serializes these paths. The IIO core does not serialize read_raw() and write_raw(), so concurrent sysfs accesses can
race: two integration time writes can leave regs_cmd out of sync with the value last written to the device, and a lux read that races with an integration time write can scale the ALS count of one integration time by the other. calibscale is updated and used in the same way.
Add a mutex that protects calibscale, regs_cmd, regs_als and the device register accesses, and take it in read_raw(), write_raw(), suspend and resume.
--- drivers/iio/light/cm3232.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
drivers/iio/light/cm3232.c +16 -0
@@ -8,8 +8,10 @@* IIO driver for CM3232 (7-bit I2C slave address 0x10). */ +#include <linux/cleanup.h> #include <linux/i2c.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h> #include <linux/init.h>
@@ -67,6 +69,8 @@ static const struct cm3232_als_info cm3232_als_info_default = {struct cm3232_chip {struct i2c_client *client;const struct cm3232_als_info *als_info; +/* Protects calibscale, regs_cmd, regs_als and the device registers */ +struct mutex lock;int calibscale;u8 regs_cmd;u16 regs_als;
@@ -237,6 +241,8 @@ static int cm3232_read_raw(struct iio_dev *indio_dev,struct cm3232_chip *chip =iio_priv (indio_dev);int ret; +guard (mutex)(&chip->lock); +switch (mask) {case IIO_CHAN_INFO_PROCESSED : ret =cm3232_get_lux (chip);
@@ -260,6 +266,8 @@ static int cm3232_write_raw(struct iio_dev *indio_dev, {struct cm3232_chip *chip =iio_priv (indio_dev); +guard (mutex)(&chip->lock); +switch (mask) {case IIO_CHAN_INFO_CALIBSCALE : chip->calibscale = val;
@@ -337,6 +345,10 @@ static int cm3232_probe(struct i2c_client *client) chip->client = client; chip->calibscale =CM3232_CALIBSCALE_DEFAULT ; + ret =devm_mutex_init (&client->dev, &chip->lock); +if (ret) +return ret; + indio_dev->channels = cm3232_channels; indio_dev->num_channels =ARRAY_SIZE (cm3232_channels); indio_dev->info = &cm3232_info;
@@ -377,6 +389,8 @@ static int cm3232_suspend(struct device *dev)struct i2c_client *client = chip->client;int ret; +guard (mutex)(&chip->lock); + chip->regs_cmd |=CM3232_CMD_ALS_DISABLE ; ret =i2c_smbus_write_byte_data (client,CM3232_REG_ADDR_CMD , chip->regs_cmd);
@@ -391,6 +405,8 @@ static int cm3232_resume(struct device *dev)struct i2c_client *client = chip->client;int ret; +guard (mutex)(&chip->lock); + chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE ; ret =i2c_smbus_write_byte_data (client,CM3232_REG_ADDR_CMD , chip->regs_cmd |CM3232_CMD_ALS_RESET );
Signature · 1 line
2.43.0
PATCH 3/3 iio: light: cm3232: reject invalid calibscale values
In reply to Vineet Thakrancm3232_write_raw() stores any value written to in_illuminance_calibscale and ignores the fractional part. A value of zero makes every lux reading zero. A negative value is converted to a large u64 when cm3232_get_lux() multiplies by it, so every non-zero reading is clamped to 0xffff. A fractional value such as 1.5 is silently truncated.
Accept only positive integers and return -EINVAL for anything else. No upper bound is needed: the largest product in cm3232_get_lux(), with the shortest integration time, a full-scale ALS count and INT_MAX as calibscale, still fits in a u64.
--- drivers/iio/light/cm3232.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/iio/light/cm3232.c +2 -0
@@ -270,6 +270,8 @@ static int cm3232_write_raw(struct iio_dev *indio_dev,switch (mask) {case IIO_CHAN_INFO_CALIBSCALE : +if (val <=0 || val2) +return -EINVAL ; chip->calibscale = val;return 0 ;case IIO_CHAN_INFO_INT_TIME :
Signature · 1 line
2.43.0