Skip to contentSkip to conversation

iio: light: ltr501: claim direct mode during select raw reads

PATCH In review Alison Schofield 2 participants 2 messages Oct 11, 2016 – Oct 15, 2016 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: light: ltr501: claim direct mode during select raw reads

Driver was checking for direct mode but not locking it. Use claim/release helper functions to guarantee the device stays in direct mode during required raw read cases.

---
 drivers/iio/light/ltr501.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iio/light/ltr501.c +11 -5
index 3afc53a..c2ac3cd 100644
@@ -631,14 +631,16 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_PROCESSED:
-		if (iio_buffer_enabled(indio_dev))
-			return -EBUSY;
-
 		switch (chan->type) {
 		case IIO_LIGHT:
+			ret = iio_device_claim_direct_mode(indio_dev);
+			if (ret)
+				return ret;
+
 			mutex_lock(&data->lock_als);
 			ret = ltr501_read_als(data, buf);
 			mutex_unlock(&data->lock_als);
+			iio_device_release_direct_mode(indio_dev);
 			if (ret < 0)
 				return ret;
 			*val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
@@ -648,14 +650,16 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 		}
 	case IIO_CHAN_INFO_RAW:
-		if (iio_buffer_enabled(indio_dev))
-			return -EBUSY;
+		ret = iio_device_claim_direct_mode(indio_dev);
+		if (ret)
+			return ret;
 
 		switch (chan->type) {
 		case IIO_INTENSITY:
 			mutex_lock(&data->lock_als);
 			ret = ltr501_read_als(data, buf);
 			mutex_unlock(&data->lock_als);
+			iio_device_release_direct_mode(indio_dev);
 			if (ret < 0)
 				return ret;
 			*val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ?
@@ -665,11 +669,13 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 			mutex_lock(&data->lock_ps);
 			ret = ltr501_read_ps(data);
 			mutex_unlock(&data->lock_ps);
+			iio_device_release_direct_mode(indio_dev);
 			if (ret < 0)
 				return ret;
 			*val = ret & LTR501_PS_DATA_MASK;
 			return IIO_VAL_INT;
 		default:
+			iio_device_release_direct_mode(indio_dev);
 			return -EINVAL;
 		}
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
2.1.4
maintainer
#1 lore raw
Quoted 5 lines · Alison Schofield

Driver was checking for direct mode but not locking it. Use claim/release helper functions to guarantee the device stays in direct mode during required raw read cases.

The uneven levels of the claim and release in the second case are nasty, please rework so that doesn't occur.

It will work as it is, but I'm feeling fussy ;)

Jonathan

Quoted 34 lines
---
 drivers/iio/light/ltr501.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iio/light/ltr501.c +5 -5
index 3afc53a..c2ac3cd 100644
@@ -631,14 +631,16 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_PROCESSED:
-		if (iio_buffer_enabled(indio_dev))
-			return -EBUSY;
-
 		switch (chan->type) {
 		case IIO_LIGHT:
+			ret = iio_device_claim_direct_mode(indio_dev);
+			if (ret)
+				return ret;
+
 			mutex_lock(&data->lock_als);
 			ret = ltr501_read_als(data, buf);
 			mutex_unlock(&data->lock_als);
+			iio_device_release_direct_mode(indio_dev);
 			if (ret < 0)
 				return ret;
 			*val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
@@ -648,14 +650,16 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 		}
 	case IIO_CHAN_INFO_RAW:
-		if (iio_buffer_enabled(indio_dev))
-			return -EBUSY;

drivers/iio/light/ltr501.c static int ltr501_read_raw(struct iio_dev *indio_dev, · line 662

Quoted 10 lines
drivers/iio/light/ltr501.c +4 -0
+		ret = iio_device_claim_direct_mode(indio_dev);
+		if (ret)
+			return ret;
 
 		switch (chan->type) {
 		case IIO_INTENSITY:
 			mutex_lock(&data->lock_als);
 			ret = ltr501_read_als(data, buf);
 			mutex_unlock(&data->lock_als);
+			iio_device_release_direct_mode(indio_dev);

I don't like the releasing something in the case statement which was taken outside of it. Either bring the claim inside as you did above, or rework this to do the release after the switch for all paths.

Quoted 18 lines
drivers/iio/light/ltr501.c +2 -0
 			if (ret < 0)
 				return ret;
 			*val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ?
@@ -665,11 +669,13 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 			mutex_lock(&data->lock_ps);
 			ret = ltr501_read_ps(data);
 			mutex_unlock(&data->lock_ps);
+			iio_device_release_direct_mode(indio_dev);
 			if (ret < 0)
 				return ret;
 			*val = ret & LTR501_PS_DATA_MASK;
 			return IIO_VAL_INT;
 		default:
+			iio_device_release_direct_mode(indio_dev);
 			return -EINVAL;
 		}
 	case IIO_CHAN_INFO_SCALE: