summaryrefslogtreecommitdiffstats
path: root/drivers/iio/humidity
diff options
context:
space:
mode:
authorHarald Geyer <harald@ccbib.org>2016-01-17 16:13:29 +0000
committerJonathan Cameron <jic23@kernel.org>2016-01-30 16:27:13 +0000
commit22acc120a141ce0a3b6e98799d15970ef687bc70 (patch)
tree5b12ebcfe799413050372677f77046b1f37cbc96 /drivers/iio/humidity
parentc984b9cbbd17d3eb602de3802e25d975182474fa (diff)
downloadlinux-22acc120a141ce0a3b6e98799d15970ef687bc70.tar.bz2
iio: dht11: Improve reliability - be more tolerant about missing start bits
Instead of guessing where the data starts, we now just try to decode from every possible start position. This causes no additional overhead if we properly received the full preamble and only costs a few extra CPU cycles in the case where the preamble is corrupted. This is much more efficient than to return an error to userspace and start over again. Signed-off-by: Harald Geyer <harald@ccbib.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio/humidity')
-rw-r--r--drivers/iio/humidity/dht11.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/drivers/iio/humidity/dht11.c b/drivers/iio/humidity/dht11.c
index 1165b1c4f9d6..1ca284aaacfc 100644
--- a/drivers/iio/humidity/dht11.c
+++ b/drivers/iio/humidity/dht11.c
@@ -161,7 +161,7 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
int *val, int *val2, long m)
{
struct dht11 *dht11 = iio_priv(iio_dev);
- int ret, timeres;
+ int ret, timeres, offset;
mutex_lock(&dht11->lock);
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
@@ -208,11 +208,14 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
if (ret < 0)
goto err;
- ret = dht11_decode(dht11,
- dht11->num_edges == DHT11_EDGES_PER_READ ?
- DHT11_EDGES_PREAMBLE :
- DHT11_EDGES_PREAMBLE - 2,
- timeres);
+ offset = DHT11_EDGES_PREAMBLE +
+ dht11->num_edges - DHT11_EDGES_PER_READ;
+ for (; offset >= 0; --offset) {
+ ret = dht11_decode(dht11, offset, timeres);
+ if (!ret)
+ break;
+ }
+
if (ret)
goto err;
}