Re: [PATCH] HC-SR04 ultrasonic ranger IIO driver

2016-06-01 Thread Daniel Baluta
On Wed, Jun 1, 2016 at 12:05 AM,   wrote:
> From: Johannes Thoma 
>
> The HC-SR04 is an ultrasonic distance sensor attached to two GPIO
> pins. The driver based on Industrial I/O (iio) subsystem and is
> controlled via configfs and sysfs. It supports an (in theory) unlimited
> number of HC-SR04 devices.
>
> Datasheet to the device can be found at:
>
> http://www.micropik.com/PDF/HCSR04.pdf
> Signed-off-by: Johannes Thoma 

Please send this to the linux-iio mailing list (linux-...@vger.kernel.org )

thanks,
Daniel.

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: [PATCH] HC-SR04 ultrasonic ranger IIO driver

2016-05-31 Thread Valdis . Kletnieks
On Tue, 31 May 2016 23:05:57 +0200, johan...@johannesthoma.com said:

Looks good overall, far from the ugliest driver I've seen.  I spotted one
locking bug, and a few small typos etc, noted inline...

> From: Johannes Thoma 
>
> The HC-SR04 is an ultrasonic distance sensor attached to two GPIO
> pins. The driver based on Industrial I/O (iio) subsystem and is

The driver is based on the Industrial...


> + * To configure a device do a
> + *
> + *mkdir /config/iio/triggers/hc-sr04/sensor0
> + *
> + * (you need to mount configfs to /config first)

Most distros seem to be using /sys/kernel/config as the mount point for this...


> + * Then you can measure distance with:
> + *
> + *cat /sys/devices/trigger0/measure

What are the units of the returned value? Inches? Hundredths of an inch?
inches.hundredths?   Other?

(Yes, I looked at the datasheet.. and your driver source is more helpful
than the sheed :)


> + struct gpio_desc *echo_desc;
> + /* Used to measure length of ECHO signal */

I was going to say "comments on same line", but that would result in *long*
lines, this is better


> +static int do_measurement(struct hc_sr04 *device,
> +   long long *usecs_elapsed)
> +{
(...)
> + if (!mutex_trylock(&device->measurement_mutex))
> + return -EBUSY;

OK... this is a potential problem, because...

> + irq = gpiod_to_irq(device->echo_desc);
> + if (irq < 0)
> + return -EIO;

Here you do a 'return' without unlocking.  This should probably be:

 if (irq < 0) {
ret = -EIO;
goto out_mutex;
}

I admit not knowing the GPIO or IIO stuff well enough to comment on those
details, but I didn't see anything obviously insane either

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: [PATCH] HC-SR04 ultrasonic ranger IIO driver

2016-05-31 Thread Greg KH
On Tue, May 31, 2016 at 11:05:57PM +0200, johan...@johannesthoma.com wrote:
> From: Johannes Thoma 
> 
> The HC-SR04 is an ultrasonic distance sensor attached to two GPIO
> pins. The driver based on Industrial I/O (iio) subsystem and is
> controlled via configfs and sysfs. It supports an (in theory) unlimited
> number of HC-SR04 devices.
> 
> Datasheet to the device can be found at:
> 
> http://www.micropik.com/PDF/HCSR04.pdf
> Signed-off-by: Johannes Thoma 
> ---
>  MAINTAINERS   |   7 +
>  drivers/iio/Kconfig   |   1 +
>  drivers/iio/Makefile  |   1 +
>  drivers/iio/ultrasonic-distance/Kconfig   |  16 ++
>  drivers/iio/ultrasonic-distance/Makefile  |   6 +
>  drivers/iio/ultrasonic-distance/hc-sr04.c | 460 
> ++
>  6 files changed, 491 insertions(+)
>  create mode 100644 drivers/iio/ultrasonic-distance/Kconfig
>  create mode 100644 drivers/iio/ultrasonic-distance/Makefile
>  create mode 100644 drivers/iio/ultrasonic-distance/hc-sr04.c

Hint, use scripts/get_maintainer.pl to find the correct mailing list and
developers to send this patch to, in order to ensure the correct people
who can apply it, receive it.

thanks,

greg k-h

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: [PATCH] HC-SR04 ultrasonic ranger IIO driver

2016-05-31 Thread Johannes Thoma
Dear List,

I've ported my hc-sr04 driver to IIO now and wanted to ask if it is ok 
to post it to the
main kernel list (or some other list?) like this (see original mail, 
sent with git send-mail).

Thanks a lot,

- Johannes

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


[PATCH] HC-SR04 ultrasonic ranger IIO driver

2016-05-31 Thread johannes
From: Johannes Thoma 

The HC-SR04 is an ultrasonic distance sensor attached to two GPIO
pins. The driver based on Industrial I/O (iio) subsystem and is
controlled via configfs and sysfs. It supports an (in theory) unlimited
number of HC-SR04 devices.

Datasheet to the device can be found at:

http://www.micropik.com/PDF/HCSR04.pdf
Signed-off-by: Johannes Thoma 
---
 MAINTAINERS   |   7 +
 drivers/iio/Kconfig   |   1 +
 drivers/iio/Makefile  |   1 +
 drivers/iio/ultrasonic-distance/Kconfig   |  16 ++
 drivers/iio/ultrasonic-distance/Makefile  |   6 +
 drivers/iio/ultrasonic-distance/hc-sr04.c | 460 ++
 6 files changed, 491 insertions(+)
 create mode 100644 drivers/iio/ultrasonic-distance/Kconfig
 create mode 100644 drivers/iio/ultrasonic-distance/Makefile
 create mode 100644 drivers/iio/ultrasonic-distance/hc-sr04.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 9c567a4..fabb338 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4988,6 +4988,13 @@ W:   
http://www.kernel.org/pub/linux/kernel/people/fseidel/hdaps/
 S: Maintained
 F: drivers/platform/x86/hdaps.c
 
+
+HC-SR04 ULTRASONIC DISTANCE SENSOR DRIVER
+M: Johannes Thoma 
+S: Maintained
+F: drivers/iio/ultrasonic-distance/hc-sr04.c
+
+
 HDPVR USB VIDEO ENCODER DRIVER
 M: Hans Verkuil 
 L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index 505e921..3c82aad 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -82,5 +82,6 @@ source "drivers/iio/potentiometer/Kconfig"
 source "drivers/iio/pressure/Kconfig"
 source "drivers/iio/proximity/Kconfig"
 source "drivers/iio/temperature/Kconfig"
+source "drivers/iio/ultrasonic-distance/Kconfig"
 
 endif # IIO
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index 20f6490..0f1c00c 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -32,3 +32,4 @@ obj-y += pressure/
 obj-y += proximity/
 obj-y += temperature/
 obj-y += trigger/
+obj-y += ultrasonic-distance/
diff --git a/drivers/iio/ultrasonic-distance/Kconfig 
b/drivers/iio/ultrasonic-distance/Kconfig
new file mode 100644
index 000..9d442dd
--- /dev/null
+++ b/drivers/iio/ultrasonic-distance/Kconfig
@@ -0,0 +1,16 @@
+#
+# Ultrasonic range sensors
+#
+
+menu "Ultrasonic ranger devices"
+
+config HC_SR04
+   tristate "HC-SR04 ultrasonic distance sensor on GPIO"
+   depends on GPIOLIB && SYSFS
+   help
+ Say Y here if you want to support the HC-SR04 ultrasonic distance
+  sensor which is attached on two runtime-configurable GPIO pins.
+
+ To compile this driver as a module, choose M here: the
+ module will be called hc-sr04.
+endmenu
diff --git a/drivers/iio/ultrasonic-distance/Makefile 
b/drivers/iio/ultrasonic-distance/Makefile
new file mode 100644
index 000..1f01d50c
--- /dev/null
+++ b/drivers/iio/ultrasonic-distance/Makefile
@@ -0,0 +1,6 @@
+#
+# Makefile for IIO proximity sensors
+#
+
+# When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_HC_SR04)  += hc-sr04.o
diff --git a/drivers/iio/ultrasonic-distance/hc-sr04.c 
b/drivers/iio/ultrasonic-distance/hc-sr04.c
new file mode 100644
index 000..e5af647
--- /dev/null
+++ b/drivers/iio/ultrasonic-distance/hc-sr04.c
@@ -0,0 +1,460 @@
+/*
+ * hc-sr04.c - Support for HC-SR04 ultrasonic range sensor
+ *
+ * Copyright (C) 2016 Johannes Thoma 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/* Precise measurements of time delta between sending a trigger signal
+ * to the HC-SR04 distance sensor and receiving the echo signal from
+ * the sensor back. This has to be precise in the usecs range. We
+ * use trigger interrupts to measure the signal, so no busy wait :)
+ *
+ * This supports an (in theory) unlimited number of HC-SR04 devices.
+ * It uses IIO software triggers to interface with userland.
+ *
+ * To configure a device do a
+ *
+ *mkdir /config/iio/triggers/hc-sr04/sensor0
+ *
+ * (you need to mount configfs to /config first)
+ *
+ * Then configure the ECHO and TRIG pins (this also accepts symbolic names
+ * configured in the device tree)
+ *
+ *echo 23 > /config/iio/triggers/hc-sr04/sensor0/trig_pin
+ *echo 24 > /config/iio/triggers/hc-sr04/sensor0/echo_pin
+ *
+ * Then you can measure distance with:
+ *
+ *cat /sys/devices/trigger0/measure
+ *
+ * (trigger0 is the device name as reported by
+ *  /config/iio/triggers/hc-sr04/sensor0/dev_name
+ *
+ * To convert to cent