Linux-IIO Archive on lore.kernel.org
 help / Atom feed
* [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
@ 2019-03-31 10:09 H. Nikolaus Schaller
  2019-04-07 12:30 ` Jonathan Cameron
  0 siblings, 1 reply; 16+ messages in thread
From: H. Nikolaus Schaller @ 2019-03-31 10:09 UTC (permalink / raw)
  To: Jonathan Cameron, Dmitry Torokhov
  Cc: Eric Piel, linux-input, letux-kernel, kernel, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-kernel,
	linux-iio, H. Nikolaus Schaller

Some user spaces (e.g. some Android devices) use /dev/input/event* for handling
the 3D position of the device with respect to the center of gravity (earth).
This can be used for gaming input, auto-rotation of screens etc.

This interface should be the standard for such use cases because it is an abstraction
of how orientation data is acquired from sensor chips. Sensor chips may be connected
through different interfaces and in different positions. They may also have different
parameters. And, if a chip is replaced by a different one, the values reported by
the device position interface should remain the same, provided the device tree reflects
the changed chip.

This did initially lead to input accelerometer drivers like drivers/input/misc/bma150.c
or drivers/misc/lis3lv02d/

But nowadays, new accelerometer chips mostly get iio drivers and rarely input drivers.

Therefore we need something like a protocol stack which bridges raw data and input devices.
It can be seen as a similar layering like TCP/IP vs. bare Ethernet. Or keyboard
input events vs. raw gpio or raw USB access.

This patch bridges the gap between raw iio data and the input device abstraction
so that accelerometer measurements can additionally be presented as X/Y/Z accelerometer
channels (INPUT_PROP_ACCELEROMETER) through /dev/input/event*.

There are no special requirements or changes needed for an iio driver.

There is no need to define a mapping (e.g. in device tree).

This driver simply collects the first 3 accelerometer channels as X, Y and Z.
If only 1 or 2 channels are available, they are used for X and Y only. Additional
channels are ignored.

Scaling is done automatically so that 1g is represented by value 256 and
range is assumed to be -511 .. +511 which gives a reasonable precision as an
input device.

If a mount-matrix is provided by the iio driver, it is also taken into account
so that the input event automatically gets the correct orientation with respect
to the device.

If this extension is not configured into the kernel it takes no resources (except
source code).

If it is configured, but there is no accelerometer, there is only a tiny penalty
for scanning for accelerometer channels once during probe of each iio device.

If it runs, the driver polls the device(s) once every 100 ms. A mode where the
iio device defines the update rate is not implemented and for further study.

If there is no user-space client, polling is not running.

The driver is capable to handle multiple iio accelerometers and they are
presented by unique /dev/input/event* files. The iio chip name is used to define
the input device name so that it can be identified (e.g. by udev rules or evtest).

Here is some example what you can expect from the driver (device:
arch/arm/boot/dts/omap3-gta04a5.dts):

root@letux:~# dmesg|fgrep iio
[    6.324584] input: iio-bridge: bmc150_accel as /devices/platform/68000000.ocp/48072000.i2c/i2c-1/1-0010/iio:device1/input/input5
[    6.516632] input: iio-bridge: bno055 as /devices/platform/68000000.ocp/48072000.i2c/i2c-1/1-0029/iio:device3/input/input7
root@letux:~# evtest /dev/input/event5 | head -19
Input driver version is 1.0.1
Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
Input device name: "iio-bridge: bmc150_accel"
Supported events:
  Event type 0 (EV_SYN)
  Event type 3 (EV_ABS)
    Event code 0 (ABS_X)
      Value      8
      Min     -511
      Max      511
    Event code 1 (ABS_Y)
      Value    -44
      Min     -511
      Max      511
    Event code 2 (ABS_Z)
      Value   -265
      Min     -511
      Max      511
Properties:
root@letux:~# evtest /dev/input/event7 | head -19
Input driver version is 1.0.1
Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
Input device name: "iio-bridge: bno055"
Supported events:
  Event type 0 (EV_SYN)
  Event type 3 (EV_ABS)
    Event code 0 (ABS_X)
      Value     -6
      Min     -511
      Max      511
    Event code 1 (ABS_Y)
      Value     17
      Min     -511
      Max      511
    Event code 2 (ABS_Z)
      Value   -250
      Min     -511
      Max      511
Properties:
root@letux:~# 

Although the sensor chips are mounted with different axis orientation,
the application of the mount matrix provides equivalent (despite noise
and precision) information on device orientation.

Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
---
 drivers/iio/Kconfig                    |   7 +
 drivers/iio/Makefile                   |   1 +
 drivers/iio/industrialio-core.c        |  12 +
 drivers/iio/industrialio-inputbridge.c | 295 +++++++++++++++++++++++++
 drivers/iio/industrialio-inputbridge.h |  28 +++
 5 files changed, 343 insertions(+)
 create mode 100644 drivers/iio/industrialio-inputbridge.c
 create mode 100644 drivers/iio/industrialio-inputbridge.h

diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index d08aeb41cd07..d85afe002613 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -68,6 +68,13 @@ config IIO_TRIGGERED_EVENT
 	help
 	  Provides helper functions for setting up triggered events.
 
+config IIO_INPUT_BRIDGE
+	bool "Enable accelerometer bridge to input driver"
+	help
+	  Provides a /dev/input/event* device for accelerometers
+	  to use as a 3D input device, e.g. for gaming or auto-rotation
+	  of screen contents.
+
 source "drivers/iio/accel/Kconfig"
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/afe/Kconfig"
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index cb5993251381..d695e5a27da5 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_IIO) += industrialio.o
 industrialio-y := industrialio-core.o industrialio-event.o inkern.o
 industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o
 industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
+industrialio-$(CONFIG_IIO_INPUT_BRIDGE) += industrialio-inputbridge.o
 
 obj-$(CONFIG_IIO_CONFIGFS) += industrialio-configfs.o
 obj-$(CONFIG_IIO_SW_DEVICE) += industrialio-sw-device.o
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 4700fd5d8c90..81f412b41a78 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -29,6 +29,7 @@
 #include <linux/iio/iio.h>
 #include "iio_core.h"
 #include "iio_core_trigger.h"
+#include "industrialio-inputbridge.h"
 #include <linux/iio/sysfs.h>
 #include <linux/iio/events.h>
 #include <linux/iio/buffer.h>
@@ -1723,6 +1724,15 @@ int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
 	if (ret < 0)
 		goto error_unreg_eventset;
 
+	ret = iio_device_register_inputbridge(indio_dev);
+	if (ret) {
+		dev_err(indio_dev->dev.parent,
+			"Failed to register as input driver\n");
+		device_del(&indio_dev->dev);
+
+		return ret;
+	}
+
 	return 0;
 
 error_unreg_eventset:
@@ -1745,6 +1755,8 @@ void iio_device_unregister(struct iio_dev *indio_dev)
 {
 	mutex_lock(&indio_dev->info_exist_lock);
 
+	iio_device_unregister_inputbridge(indio_dev);
+
 	cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
 
 	iio_device_unregister_debugfs(indio_dev);
diff --git a/drivers/iio/industrialio-inputbridge.c b/drivers/iio/industrialio-inputbridge.c
new file mode 100644
index 000000000000..dd672e25bc70
--- /dev/null
+++ b/drivers/iio/industrialio-inputbridge.c
@@ -0,0 +1,295 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * The Industrial I/O core, bridge to input devices
+ *
+ * Copyright (c) 2016-2019 Golden Delicious Computers GmbH&Co. KG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ */
+
+#include <linux/iio/consumer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/types.h>
+#include <linux/input.h>
+#include <linux/input-polldev.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include "industrialio-inputbridge.h"
+
+/* currently, only polling is implemented */
+#define POLLING_MSEC	100
+
+struct iio_input_map {
+	struct input_polled_dev *poll_dev;	/* the input device */
+	struct iio_channel channels[3];		/* x, y, z channels */
+	struct matrix {
+		int mxx, myx, mzx;	/* fixed point mount-matrix */
+		int mxy, myy, mzy;
+		int mxz, myz, mzz;
+	} matrix;
+};
+
+static inline struct iio_input_map *to_iio_input_map(
+		struct iio_channel *channel)
+{
+	return (struct iio_input_map *) channel->data;
+}
+
+/* minimum and maximum range we want to report */
+#define ABSMAX_ACC_VAL		(512 - 1)
+#define ABSMIN_ACC_VAL		-(ABSMAX_ACC_VAL)
+
+/* scale processed iio values so that 1g maps to ABSMAX_ACC_VAL / 2 */
+#define SCALE			((100 * ABSMAX_ACC_VAL) / (2 * 981))
+
+/*
+ * convert float string to scaled fixed point format, e.g.
+ *   1		-> 1000		(value passed as unit)
+ *   1.23	-> 1230
+ *   0.1234	->  123
+ *   -.01234	->  -12
+ */
+
+static int32_t atofix(const char *str, uint32_t unit)
+{
+	int32_t mantissa = 0;
+	bool sign = false;
+	bool decimal = false;
+	int32_t divisor = 1;
+
+	if (*str == '-')
+		sign = true, str++;
+	while (*str && divisor < unit) {
+		if (*str >= '0' && *str <= '9') {
+			mantissa = 10 * mantissa + (*str - '0');
+			if (decimal)
+				divisor *= 10;
+		} else if (*str == '.')
+			decimal = true;
+		else
+			return 0;	/* error */
+		str++;
+	}
+
+	mantissa = (mantissa * unit) / divisor;
+	if (sign)
+		mantissa = -mantissa;
+
+	return mantissa;
+}
+
+static void iio_apply_matrix(struct matrix *m, int *in, int *out, uint32_t unit)
+{
+	/* apply mount matrix */
+	out[0] = (m->mxx * in[0] + m->myx * in[1] + m->mzx * in[2]) / unit;
+	out[1] = (m->mxy * in[0] + m->myy * in[1] + m->mzy * in[2]) / unit;
+	out[2] = (m->mxz * in[0] + m->myz * in[1] + m->mzz * in[2]) / unit;
+}
+
+#define FIXED_POINT_UNIT	1000	/* seems reasonable for accelerometer input */
+
+static void iio_accel_poll(struct input_polled_dev *dev)
+{
+	struct iio_input_map *map = dev->private;
+	struct input_dev *input = dev->input;
+
+	int values[3];		/* values while processing */
+	int aligned_values[3];	/* mount matrix applied */
+
+	int cindex = 0;
+
+printk("%s: map=%px input=%px\n", __func__, map, input);
+
+	while (cindex < ARRAY_SIZE(values)) {
+		struct iio_channel *channel =
+			&map->channels[cindex];
+		int val;
+		int ret;
+
+		if (!channel->indio_dev) {
+			values[cindex] = 0;
+			continue;
+		}
+
+		ret = iio_read_channel_raw(channel, &val);
+
+		if (ret < 0) {
+			pr_err("%s(): channel read error %d\n",
+				__func__, cindex);
+			return;
+		}
+
+		ret = iio_convert_raw_to_processed(channel, val,
+				 &values[cindex], SCALE);
+
+		if (ret < 0) {
+			pr_err("%s(): channel processing error\n",
+				__func__);
+			return;
+		}
+
+		cindex++;
+	}
+
+	iio_apply_matrix(&map->matrix, values, aligned_values, FIXED_POINT_UNIT);
+
+	input_report_abs(input, ABS_X, aligned_values[0]);
+	input_report_abs(input, ABS_Y, aligned_values[1]);
+	input_report_abs(input, ABS_Z, aligned_values[2]);
+	input_sync(input);
+}
+
+static int dindex=0;	/* assign unique names to accel/input devices */
+
+static int iio_input_register_accel_channel(struct iio_dev *indio_dev,
+		 const struct iio_chan_spec *chan)
+{ /* we found some accelerometer channel */
+	int ret;
+	int cindex;
+	struct iio_input_map *map = iio_device_get_drvdata(indio_dev);
+
+printk("%s: map=%px\n", __func__, map);
+
+	if (!map) {
+		struct input_polled_dev *poll_dev;
+		const struct iio_chan_spec_ext_info *ext_info;
+
+		map = devm_kzalloc(&indio_dev->dev, sizeof(struct iio_input_map), GFP_KERNEL);
+		if (!map)
+			return -ENOMEM;
+
+		iio_device_set_drvdata(indio_dev, map);
+
+		poll_dev = devm_input_allocate_polled_device(&indio_dev->dev);
+		if (!poll_dev)
+			return -ENOMEM;
+
+		poll_dev->private = map;
+		poll_dev->poll = iio_accel_poll;
+		poll_dev->poll_interval = POLLING_MSEC;
+
+		poll_dev->input->name = kasprintf(GFP_KERNEL, "iio-bridge: %s",
+						    indio_dev->name);
+		poll_dev->input->phys = kasprintf(GFP_KERNEL, "accel/input%d",
+						    dindex++);
+
+// do we need something like this?
+//		poll_dev->input->id.bustype = BUS_IIO;
+//		poll_dev->input->id.vendor = 0x0001;
+//		poll_dev->input->id.product = 0x0001;
+//		poll_dev->input->id.version = 0x0001;
+
+		set_bit(INPUT_PROP_ACCELEROMETER, poll_dev->input->propbit);
+		poll_dev->input->evbit[0] = BIT_MASK(EV_ABS);
+		input_alloc_absinfo(poll_dev->input);
+		input_set_abs_params(poll_dev->input, ABS_X, ABSMIN_ACC_VAL,
+					ABSMAX_ACC_VAL, 0, 0);
+		input_set_abs_params(poll_dev->input, ABS_Y, ABSMIN_ACC_VAL,
+					ABSMAX_ACC_VAL, 0, 0);
+		input_set_abs_params(poll_dev->input, ABS_Z, ABSMIN_ACC_VAL,
+					ABSMAX_ACC_VAL, 0, 0);
+
+		map->poll_dev = poll_dev;
+
+		ret = input_register_polled_device(poll_dev);
+
+		if (ret < 0) {
+			kfree(poll_dev->input->name);
+			kfree(poll_dev->input->phys);
+			return ret;
+		}
+
+		/* assume all channels of a device share the same matrix */
+
+		ext_info = chan->ext_info;
+		for (; ext_info && ext_info->name; ext_info++) {
+			if (strcmp(ext_info->name, "mount_matrix") == 0)
+				break;
+		}
+
+		if (ext_info && ext_info->name) {
+			/* matrix found */
+			uintptr_t priv = ext_info->private;
+			const struct iio_mount_matrix *mtx;
+
+			mtx = ((iio_get_mount_matrix_t *) priv)(indio_dev,
+								chan);
+
+			map->matrix.mxx = atofix(mtx->rotation[0], FIXED_POINT_UNIT);
+			map->matrix.myx = atofix(mtx->rotation[1], FIXED_POINT_UNIT);
+			map->matrix.mzx = atofix(mtx->rotation[2], FIXED_POINT_UNIT);
+			map->matrix.mxy = atofix(mtx->rotation[3], FIXED_POINT_UNIT);
+			map->matrix.myy = atofix(mtx->rotation[4], FIXED_POINT_UNIT);
+			map->matrix.mzy = atofix(mtx->rotation[5], FIXED_POINT_UNIT);
+			map->matrix.mxz = atofix(mtx->rotation[6], FIXED_POINT_UNIT);
+			map->matrix.myz = atofix(mtx->rotation[7], FIXED_POINT_UNIT);
+			map->matrix.mzz = atofix(mtx->rotation[8], FIXED_POINT_UNIT);
+		} else {
+			map->matrix.mxx = FIXED_POINT_UNIT;
+			map->matrix.myx = 0;
+			map->matrix.mzx = 0;
+			map->matrix.mxy = 0;
+			map->matrix.myy = FIXED_POINT_UNIT;
+			map->matrix.mzy = 0;
+			map->matrix.mxz = 0;
+			map->matrix.myz = 0;
+			map->matrix.mzz = FIXED_POINT_UNIT;
+		}
+	}
+
+// brauchen wir das noch? Oder nehmen wir einfach an dass es 3 Kanäle gibt?
+
+	/* find free channel within this device */
+
+	for (cindex = 0; cindex < ARRAY_SIZE(map->channels); cindex++) {
+		if (!map->channels[cindex].indio_dev)
+			break;
+	}
+
+	/* check if we already have collected enough channels */
+	if (cindex == ARRAY_SIZE(map->channels))
+		return 0;	/* silently ignore */
+
+	map->channels[cindex].indio_dev = indio_dev;
+	map->channels[cindex].channel = chan;
+	map->channels[cindex].data = map;
+
+	return 0;
+}
+
+int iio_device_register_inputbridge(struct iio_dev *indio_dev)
+{
+	int i;
+
+	for (i = 0; i < indio_dev->num_channels; i++) {
+		const struct iio_chan_spec *chan =
+				&indio_dev->channels[i];
+
+		if (chan->type == IIO_ACCEL) {
+			int r = iio_input_register_accel_channel(indio_dev,
+								 chan);
+
+			if (r < 0)
+				return r;
+		}
+	}
+
+	return 0;
+}
+
+void iio_device_unregister_inputbridge(struct iio_dev *indio_dev)
+{
+	struct iio_input_map *map = iio_device_get_drvdata(indio_dev);
+	struct input_dev *input = map->poll_dev->input;
+
+	kfree(input->name);
+	kfree(input->phys);
+	input_unregister_polled_device(map->poll_dev);
+}
+
+MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
+MODULE_DESCRIPTION("Bridge to present Industrial I/O accelerometers as properly oriented Input devices");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/industrialio-inputbridge.h b/drivers/iio/industrialio-inputbridge.h
new file mode 100644
index 000000000000..1363b10ab3f7
--- /dev/null
+++ b/drivers/iio/industrialio-inputbridge.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * The Industrial I/O core, bridge to input devices
+ *
+ * Copyright (c) 2016-2019 Golden Delicious Computers GmbH&Co. KG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ */
+
+#if defined(CONFIG_IIO_INPUT_BRIDGE)
+
+extern int iio_device_register_inputbridge(struct iio_dev *indio_dev);
+extern void iio_device_unregister_inputbridge(struct iio_dev *indio_dev);
+
+#else
+
+static inline int iio_device_register_inputbridge(struct iio_dev *indio_dev)
+{
+	return 0;
+}
+
+static inline void iio_device_unregister_inputbridge(struct iio_dev *indio_dev)
+{
+}
+
+#endif
-- 
2.19.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-03-31 10:09 [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface H. Nikolaus Schaller
@ 2019-04-07 12:30 ` Jonathan Cameron
       [not found]   ` <CD44AFA0-6676-4842-9C80-61BB363DD556@goldelico.com>
  0 siblings, 1 reply; 16+ messages in thread
From: Jonathan Cameron @ 2019-04-07 12:30 UTC (permalink / raw)
  To: H. Nikolaus Schaller
  Cc: Dmitry Torokhov, Eric Piel, linux-input, letux-kernel, kernel,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-kernel, linux-iio

On Sun, 31 Mar 2019 12:09:46 +0200
"H. Nikolaus Schaller" <hns@goldelico.com> wrote:

Hi Nikolaus,

I'm probably going to repeat a few things I sent for v1 as the audience has
expanded somewhat!

Good to see this moving forwards though as there has been at least some demand
for it going way back to the early days of IIO.

> Some user spaces (e.g. some Android devices) use /dev/input/event* for handling
> the 3D position of the device with respect to the center of gravity (earth).
> This can be used for gaming input, auto-rotation of screens etc.
> 
> This interface should be the standard for such use cases because it is an abstraction
> of how orientation data is acquired from sensor chips. Sensor chips may be connected
> through different interfaces and in different positions. They may also have different
> parameters. And, if a chip is replaced by a different one, the values reported by
> the device position interface should remain the same, provided the device tree reflects
> the changed chip.
> 
> This did initially lead to input accelerometer drivers like drivers/input/misc/bma150.c
> or drivers/misc/lis3lv02d/
> 
> But nowadays, new accelerometer chips mostly get iio drivers and rarely input drivers.
> 
> Therefore we need something like a protocol stack which bridges raw data and input devices.
> It can be seen as a similar layering like TCP/IP vs. bare Ethernet. Or keyboard
> input events vs. raw gpio or raw USB access.
> 
> This patch bridges the gap between raw iio data and the input device abstraction
> so that accelerometer measurements can additionally be presented as X/Y/Z accelerometer
> channels (INPUT_PROP_ACCELEROMETER) through /dev/input/event*.
> 
> There are no special requirements or changes needed for an iio driver.
> 
> There is no need to define a mapping (e.g. in device tree).
This worries me, as it inherently means we end up with this interface being
registered in cases where it makes no sense.  A lot of generic distros get
used across widely differing use cases.

I think we need some deliberate userspace interaction to instantiate
one of these rather than 'always doing it'.

As I mentioned in V1, look at the possibility of a configfs based method
to build the map.  It's easy for userspace to work out what makes sense to
map in principle.  There may be some missing info that we also need to
look to expose.

In general, userspace created channel maps would be very useful for
other things such as maker type boards where they can plug all sorts
of odd things into ADC channels for example.

> 
> This driver simply collects the first 3 accelerometer channels as X, Y and Z.
> If only 1 or 2 channels are available, they are used for X and Y only. Additional
> channels are ignored.
> 
> Scaling is done automatically so that 1g is represented by value 256 and
> range is assumed to be -511 .. +511 which gives a reasonable precision as an
> input device.

Why do we do this, rather than letting input deal with it?  Input is used
to widely differing scales IIRC

> 
> If a mount-matrix is provided by the iio driver, it is also taken into account
> so that the input event automatically gets the correct orientation with respect
> to the device.
> 
> If this extension is not configured into the kernel it takes no resources (except
> source code).
> 
> If it is configured, but there is no accelerometer, there is only a tiny penalty
> for scanning for accelerometer channels once during probe of each iio device.
> 
> If it runs, the driver polls the device(s) once every 100 ms. A mode where the
> iio device defines the update rate is not implemented and for further study.
> 
> If there is no user-space client, polling is not running.
> 
> The driver is capable to handle multiple iio accelerometers and they are
> presented by unique /dev/input/event* files. The iio chip name is used to define
> the input device name so that it can be identified (e.g. by udev rules or evtest).
> 
> Here is some example what you can expect from the driver (device:
> arch/arm/boot/dts/omap3-gta04a5.dts):
> 
> root@letux:~# dmesg|fgrep iio
> [    6.324584] input: iio-bridge: bmc150_accel as /devices/platform/68000000.ocp/48072000.i2c/i2c-1/1-0010/iio:device1/input/input5
> [    6.516632] input: iio-bridge: bno055 as /devices/platform/68000000.ocp/48072000.i2c/i2c-1/1-0029/iio:device3/input/input7
> root@letux:~# evtest /dev/input/event5 | head -19
> Input driver version is 1.0.1
> Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
> Input device name: "iio-bridge: bmc150_accel"
> Supported events:
>   Event type 0 (EV_SYN)
>   Event type 3 (EV_ABS)
>     Event code 0 (ABS_X)
>       Value      8
>       Min     -511
>       Max      511
>     Event code 1 (ABS_Y)
>       Value    -44
>       Min     -511
>       Max      511
>     Event code 2 (ABS_Z)
>       Value   -265
>       Min     -511
>       Max      511
> Properties:
> root@letux:~# evtest /dev/input/event7 | head -19
> Input driver version is 1.0.1
> Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
> Input device name: "iio-bridge: bno055"
> Supported events:
>   Event type 0 (EV_SYN)
>   Event type 3 (EV_ABS)
>     Event code 0 (ABS_X)
>       Value     -6
>       Min     -511
>       Max      511
>     Event code 1 (ABS_Y)
>       Value     17
>       Min     -511
>       Max      511
>     Event code 2 (ABS_Z)
>       Value   -250
>       Min     -511
>       Max      511
> Properties:
> root@letux:~# 
> 
> Although the sensor chips are mounted with different axis orientation,
> the application of the mount matrix provides equivalent (despite noise
> and precision) information on device orientation.
> 
> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
> ---
>  drivers/iio/Kconfig                    |   7 +
>  drivers/iio/Makefile                   |   1 +
>  drivers/iio/industrialio-core.c        |  12 +
>  drivers/iio/industrialio-inputbridge.c | 295 +++++++++++++++++++++++++
>  drivers/iio/industrialio-inputbridge.h |  28 +++
>  5 files changed, 343 insertions(+)
>  create mode 100644 drivers/iio/industrialio-inputbridge.c
>  create mode 100644 drivers/iio/industrialio-inputbridge.h
> 
> diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
> index d08aeb41cd07..d85afe002613 100644
> --- a/drivers/iio/Kconfig
> +++ b/drivers/iio/Kconfig
> @@ -68,6 +68,13 @@ config IIO_TRIGGERED_EVENT
>  	help
>  	  Provides helper functions for setting up triggered events.
>  
> +config IIO_INPUT_BRIDGE
> +	bool "Enable accelerometer bridge to input driver"

Dependency on input?

> +	help
> +	  Provides a /dev/input/event* device for accelerometers
> +	  to use as a 3D input device, e.g. for gaming or auto-rotation
> +	  of screen contents.
> +
>  source "drivers/iio/accel/Kconfig"
>  source "drivers/iio/adc/Kconfig"
>  source "drivers/iio/afe/Kconfig"
> diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
> index cb5993251381..d695e5a27da5 100644
> --- a/drivers/iio/Makefile
> +++ b/drivers/iio/Makefile
> @@ -7,6 +7,7 @@ obj-$(CONFIG_IIO) += industrialio.o
>  industrialio-y := industrialio-core.o industrialio-event.o inkern.o
>  industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o
>  industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
> +industrialio-$(CONFIG_IIO_INPUT_BRIDGE) += industrialio-inputbridge.o
>  
>  obj-$(CONFIG_IIO_CONFIGFS) += industrialio-configfs.o
>  obj-$(CONFIG_IIO_SW_DEVICE) += industrialio-sw-device.o
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 4700fd5d8c90..81f412b41a78 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -29,6 +29,7 @@
>  #include <linux/iio/iio.h>
>  #include "iio_core.h"
>  #include "iio_core_trigger.h"
> +#include "industrialio-inputbridge.h"
>  #include <linux/iio/sysfs.h>
>  #include <linux/iio/events.h>
>  #include <linux/iio/buffer.h>
> @@ -1723,6 +1724,15 @@ int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
>  	if (ret < 0)
>  		goto error_unreg_eventset;
>  
> +	ret = iio_device_register_inputbridge(indio_dev);
> +	if (ret) {
> +		dev_err(indio_dev->dev.parent,
> +			"Failed to register as input driver\n");
> +		device_del(&indio_dev->dev);
> +
This doesn't look like balanced error handling given the goto in the previous
case.  If we are treating this as an error we need to unwind the whole
of this function properly.

> +		return ret;
> +	}
> +
>  	return 0;
>  
>  error_unreg_eventset:
> @@ -1745,6 +1755,8 @@ void iio_device_unregister(struct iio_dev *indio_dev)
>  {
>  	mutex_lock(&indio_dev->info_exist_lock);
>  
> +	iio_device_unregister_inputbridge(indio_dev);
> +
>  	cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
>  
>  	iio_device_unregister_debugfs(indio_dev);
> diff --git a/drivers/iio/industrialio-inputbridge.c b/drivers/iio/industrialio-inputbridge.c
> new file mode 100644
> index 000000000000..dd672e25bc70
> --- /dev/null
> +++ b/drivers/iio/industrialio-inputbridge.c
> @@ -0,0 +1,295 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * The Industrial I/O core, bridge to input devices
> + *
> + * Copyright (c) 2016-2019 Golden Delicious Computers GmbH&Co. KG
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU General Public License version 2 as published by
> + * the Free Software Foundation.
No need for the boiler plate if you have SPDX.  That is one of the
advantages!

> + */
> +
> +#include <linux/iio/consumer.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/types.h>
> +#include <linux/input.h>
> +#include <linux/input-polldev.h>
> +#include <linux/module.h>
> +#include <linux/slab.h>
> +
> +#include "industrialio-inputbridge.h"
> +
> +/* currently, only polling is implemented */
> +#define POLLING_MSEC	100
> +
> +struct iio_input_map {
> +	struct input_polled_dev *poll_dev;	/* the input device */
> +	struct iio_channel channels[3];		/* x, y, z channels */
> +	struct matrix {
> +		int mxx, myx, mzx;	/* fixed point mount-matrix */
> +		int mxy, myy, mzy;
> +		int mxz, myz, mzz;
> +	} matrix;
> +};
> +
> +static inline struct iio_input_map *to_iio_input_map(
> +		struct iio_channel *channel)
> +{
> +	return (struct iio_input_map *) channel->data;
> +}
> +
> +/* minimum and maximum range we want to report */
> +#define ABSMAX_ACC_VAL		(512 - 1)
> +#define ABSMIN_ACC_VAL		-(ABSMAX_ACC_VAL)
> +
> +/* scale processed iio values so that 1g maps to ABSMAX_ACC_VAL / 2 */
> +#define SCALE			((100 * ABSMAX_ACC_VAL) / (2 * 981))
> +
> +/*
> + * convert float string to scaled fixed point format, e.g.
> + *   1		-> 1000		(value passed as unit)
> + *   1.23	-> 1230
> + *   0.1234	->  123
> + *   -.01234	->  -12
> + */
> +
> +static int32_t atofix(const char *str, uint32_t unit)
> +{
> +	int32_t mantissa = 0;
> +	bool sign = false;
> +	bool decimal = false;
> +	int32_t divisor = 1;
> +
> +	if (*str == '-')
> +		sign = true, str++;
> +	while (*str && divisor < unit) {
> +		if (*str >= '0' && *str <= '9') {
> +			mantissa = 10 * mantissa + (*str - '0');
> +			if (decimal)
> +				divisor *= 10;
> +		} else if (*str == '.')
> +			decimal = true;
> +		else
> +			return 0;	/* error */
> +		str++;
> +	}
> +
> +	mantissa = (mantissa * unit) / divisor;
> +	if (sign)
> +		mantissa = -mantissa;
> +
> +	return mantissa;
> +}
> +
> +static void iio_apply_matrix(struct matrix *m, int *in, int *out, uint32_t unit)
> +{
> +	/* apply mount matrix */
> +	out[0] = (m->mxx * in[0] + m->myx * in[1] + m->mzx * in[2]) / unit;
> +	out[1] = (m->mxy * in[0] + m->myy * in[1] + m->mzy * in[2]) / unit;
> +	out[2] = (m->mxz * in[0] + m->myz * in[1] + m->mzz * in[2]) / unit;
> +}
> +
> +#define FIXED_POINT_UNIT	1000	/* seems reasonable for accelerometer input */
> +
> +static void iio_accel_poll(struct input_polled_dev *dev)
> +{
> +	struct iio_input_map *map = dev->private;
> +	struct input_dev *input = dev->input;
> +
> +	int values[3];		/* values while processing */
> +	int aligned_values[3];	/* mount matrix applied */
> +
> +	int cindex = 0;
> +
> +printk("%s: map=%px input=%px\n", __func__, map, input);
Remember to tidy these debug statements up at some point.

> +
> +	while (cindex < ARRAY_SIZE(values)) {
> +		struct iio_channel *channel =
> +			&map->channels[cindex];
> +		int val;
> +		int ret;
> +
> +		if (!channel->indio_dev) {
> +			values[cindex] = 0;
> +			continue;
> +		}
> +
> +		ret = iio_read_channel_raw(channel, &val);
> +
> +		if (ret < 0) {
> +			pr_err("%s(): channel read error %d\n",
> +				__func__, cindex);
> +			return;
> +		}
> +
> +		ret = iio_convert_raw_to_processed(channel, val,
> +				 &values[cindex], SCALE);
> +
> +		if (ret < 0) {
> +			pr_err("%s(): channel processing error\n",
> +				__func__);
> +			return;
> +		}
> +
> +		cindex++;
> +	}
> +
> +	iio_apply_matrix(&map->matrix, values, aligned_values, FIXED_POINT_UNIT);
> +
> +	input_report_abs(input, ABS_X, aligned_values[0]);
> +	input_report_abs(input, ABS_Y, aligned_values[1]);
> +	input_report_abs(input, ABS_Z, aligned_values[2]);
> +	input_sync(input);
> +}
> +
> +static int dindex=0;	/* assign unique names to accel/input devices */
Build something from the iio device IDA perhaps?  Those are unique
as well.  Useful to know which one this is linked to.

> +
> +static int iio_input_register_accel_channel(struct iio_dev *indio_dev,
> +		 const struct iio_chan_spec *chan)
> +{ /* we found some accelerometer channel */
> +	int ret;
> +	int cindex;
> +	struct iio_input_map *map = iio_device_get_drvdata(indio_dev);

Don't do that.   That is in the domain of the device driver and so
will sometimes already be in use. 

> +
> +printk("%s: map=%px\n", __func__, map);
> +
> +	if (!map) {
> +		struct input_polled_dev *poll_dev;
> +		const struct iio_chan_spec_ext_info *ext_info;
> +
> +		map = devm_kzalloc(&indio_dev->dev, sizeof(struct iio_input_map), GFP_KERNEL);
> +		if (!map)
> +			return -ENOMEM;
> +
> +		iio_device_set_drvdata(indio_dev, map);
> +
> +		poll_dev = devm_input_allocate_polled_device(&indio_dev->dev);
> +		if (!poll_dev)
> +			return -ENOMEM;
> +
> +		poll_dev->private = map;
> +		poll_dev->poll = iio_accel_poll;
> +		poll_dev->poll_interval = POLLING_MSEC;
> +
> +		poll_dev->input->name = kasprintf(GFP_KERNEL, "iio-bridge: %s",
> +						    indio_dev->name);
> +		poll_dev->input->phys = kasprintf(GFP_KERNEL, "accel/input%d",
> +						    dindex++);
> +
> +// do we need something like this?
> +//		poll_dev->input->id.bustype = BUS_IIO;
> +//		poll_dev->input->id.vendor = 0x0001;
> +//		poll_dev->input->id.product = 0x0001;
> +//		poll_dev->input->id.version = 0x0001;
> +
> +		set_bit(INPUT_PROP_ACCELEROMETER, poll_dev->input->propbit);
> +		poll_dev->input->evbit[0] = BIT_MASK(EV_ABS);
> +		input_alloc_absinfo(poll_dev->input);
> +		input_set_abs_params(poll_dev->input, ABS_X, ABSMIN_ACC_VAL,
> +					ABSMAX_ACC_VAL, 0, 0);
> +		input_set_abs_params(poll_dev->input, ABS_Y, ABSMIN_ACC_VAL,
> +					ABSMAX_ACC_VAL, 0, 0);
> +		input_set_abs_params(poll_dev->input, ABS_Z, ABSMIN_ACC_VAL,
> +					ABSMAX_ACC_VAL, 0, 0);
> +
> +		map->poll_dev = poll_dev;
> +
> +		ret = input_register_polled_device(poll_dev);
> +
> +		if (ret < 0) {
> +			kfree(poll_dev->input->name);
> +			kfree(poll_dev->input->phys);
> +			return ret;
> +		}
> +
> +		/* assume all channels of a device share the same matrix */
> +
> +		ext_info = chan->ext_info;
> +		for (; ext_info && ext_info->name; ext_info++) {
> +			if (strcmp(ext_info->name, "mount_matrix") == 0)
> +				break;
> +		}
> +
> +		if (ext_info && ext_info->name) {
> +			/* matrix found */
> +			uintptr_t priv = ext_info->private;
> +			const struct iio_mount_matrix *mtx;
> +
> +			mtx = ((iio_get_mount_matrix_t *) priv)(indio_dev,
> +								chan);
> +
> +			map->matrix.mxx = atofix(mtx->rotation[0], FIXED_POINT_UNIT);
> +			map->matrix.myx = atofix(mtx->rotation[1], FIXED_POINT_UNIT);
> +			map->matrix.mzx = atofix(mtx->rotation[2], FIXED_POINT_UNIT);
> +			map->matrix.mxy = atofix(mtx->rotation[3], FIXED_POINT_UNIT);
> +			map->matrix.myy = atofix(mtx->rotation[4], FIXED_POINT_UNIT);
> +			map->matrix.mzy = atofix(mtx->rotation[5], FIXED_POINT_UNIT);
> +			map->matrix.mxz = atofix(mtx->rotation[6], FIXED_POINT_UNIT);
> +			map->matrix.myz = atofix(mtx->rotation[7], FIXED_POINT_UNIT);
> +			map->matrix.mzz = atofix(mtx->rotation[8], FIXED_POINT_UNIT);
> +		} else {
> +			map->matrix.mxx = FIXED_POINT_UNIT;
> +			map->matrix.myx = 0;
> +			map->matrix.mzx = 0;
> +			map->matrix.mxy = 0;
> +			map->matrix.myy = FIXED_POINT_UNIT;
> +			map->matrix.mzy = 0;
> +			map->matrix.mxz = 0;
> +			map->matrix.myz = 0;
> +			map->matrix.mzz = FIXED_POINT_UNIT;
> +		}
> +	}
> +
> +// brauchen wir das noch? Oder nehmen wir einfach an dass es 3 Kanäle gibt?
> +
> +	/* find free channel within this device */
> +
> +	for (cindex = 0; cindex < ARRAY_SIZE(map->channels); cindex++) {
> +		if (!map->channels[cindex].indio_dev)
> +			break;
> +	}
> +
> +	/* check if we already have collected enough channels */
> +	if (cindex == ARRAY_SIZE(map->channels))
> +		return 0;	/* silently ignore */
> +
> +	map->channels[cindex].indio_dev = indio_dev;
> +	map->channels[cindex].channel = chan;
> +	map->channels[cindex].data = map;
> +
> +	return 0;
> +}
> +
> +int iio_device_register_inputbridge(struct iio_dev *indio_dev)
> +{
> +	int i;
> +
> +	for (i = 0; i < indio_dev->num_channels; i++) {
> +		const struct iio_chan_spec *chan =
> +				&indio_dev->channels[i];
> +
> +		if (chan->type == IIO_ACCEL) {
> +			int r = iio_input_register_accel_channel(indio_dev,
> +								 chan);
It would be cleaner (and safer) to go find all the necessary channels then
set up the map in one go, rather that iterating and trying to build it
in a sequential fashion.

So move the search loop inside and have something like.

iio_input_find_accel_channel(indio_dev, chan, &numchans);
iio_input_register_device(indio_dev, chan, numchans);
 
> +
> +			if (r < 0)
> +				return r;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +void iio_device_unregister_inputbridge(struct iio_dev *indio_dev)
> +{
> +	struct iio_input_map *map = iio_device_get_drvdata(indio_dev);
> +	struct input_dev *input = map->poll_dev->input;
> +
> +	kfree(input->name);
> +	kfree(input->phys);
> +	input_unregister_polled_device(map->poll_dev);
> +}
> +
> +MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
> +MODULE_DESCRIPTION("Bridge to present Industrial I/O accelerometers as properly oriented Input devices");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/iio/industrialio-inputbridge.h b/drivers/iio/industrialio-inputbridge.h
> new file mode 100644
> index 000000000000..1363b10ab3f7
> --- /dev/null
> +++ b/drivers/iio/industrialio-inputbridge.h
> @@ -0,0 +1,28 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * The Industrial I/O core, bridge to input devices
> + *
> + * Copyright (c) 2016-2019 Golden Delicious Computers GmbH&Co. KG
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU General Public License version 2 as published by
> + * the Free Software Foundation.
> + */
> +
> +#if defined(CONFIG_IIO_INPUT_BRIDGE)
> +
> +extern int iio_device_register_inputbridge(struct iio_dev *indio_dev);
> +extern void iio_device_unregister_inputbridge(struct iio_dev *indio_dev);
> +
> +#else
> +
> +static inline int iio_device_register_inputbridge(struct iio_dev *indio_dev)
> +{
> +	return 0;
> +}
> +
> +static inline void iio_device_unregister_inputbridge(struct iio_dev *indio_dev)
> +{
> +}
> +
> +#endif


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
       [not found]   ` <CD44AFA0-6676-4842-9C80-61BB363DD556@goldelico.com>
@ 2019-04-14 11:40     ` Jonathan Cameron
  2019-04-14 16:26       ` Roderick Colenbrander
       [not found]       ` <CD6219BE-61FF-4C38-9532-054C60A77F89@goldelico.com>
  0 siblings, 2 replies; 16+ messages in thread
From: Jonathan Cameron @ 2019-04-14 11:40 UTC (permalink / raw)
  To: H. Nikolaus Schaller
  Cc: Dmitry Torokhov, Eric Piel, linux-input, letux-kernel, kernel,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-kernel, linux-iio

On Mon, 8 Apr 2019 15:15:56 +0200
H. Nikolaus Schaller <hns@goldelico.com> wrote:

> Hi Jonathan,
> 
> > Am 07.04.2019 um 14:30 schrieb Jonathan Cameron <jic23@kernel.org>:
> > 
> > On Sun, 31 Mar 2019 12:09:46 +0200
> > "H. Nikolaus Schaller" <hns@goldelico.com> wrote:
> > 
> > Hi Nikolaus,
> > 
> > I'm probably going to repeat a few things I sent for v1 as the audience has
> > expanded somewhat!
> > 
> > Good to see this moving forwards though as there has been at least some demand
> > for it going way back to the early days of IIO.
> >   
> >> Some user spaces (e.g. some Android devices) use /dev/input/event* for handling
> >> the 3D position of the device with respect to the center of gravity (earth).
> >> This can be used for gaming input, auto-rotation of screens etc.
> >> 
> >> This interface should be the standard for such use cases because it is an abstraction
> >> of how orientation data is acquired from sensor chips. Sensor chips may be connected
> >> through different interfaces and in different positions. They may also have different
> >> parameters. And, if a chip is replaced by a different one, the values reported by
> >> the device position interface should remain the same, provided the device tree reflects
> >> the changed chip.
> >> 
> >> This did initially lead to input accelerometer drivers like drivers/input/misc/bma150.c
> >> or drivers/misc/lis3lv02d/
> >> 
> >> But nowadays, new accelerometer chips mostly get iio drivers and rarely input drivers.
> >> 
> >> Therefore we need something like a protocol stack which bridges raw data and input devices.
> >> It can be seen as a similar layering like TCP/IP vs. bare Ethernet. Or keyboard
> >> input events vs. raw gpio or raw USB access.
> >> 
> >> This patch bridges the gap between raw iio data and the input device abstraction
> >> so that accelerometer measurements can additionally be presented as X/Y/Z accelerometer
> >> channels (INPUT_PROP_ACCELEROMETER) through /dev/input/event*.
> >> 
> >> There are no special requirements or changes needed for an iio driver.
> >> 
> >> There is no need to define a mapping (e.g. in device tree).  
> > This worries me, as it inherently means we end up with this interface being
> > registered in cases where it makes no sense.  A lot of generic distros get
> > used across widely differing use cases.  
> 
> I still do not fully understand what is worrying you here.

> 
> Do you worry about functionality, flexibility or resources or something else?

Two main things:
1) Lack of generality of the approach. 
   This is a single use trick for input devices. Why does it make sense for
   input devices?  There are lots of other in kernel users and potential
   ones in the future.  The ability to register additional IIO consumers like
   this is useful, lets make it useful to everyone.

2) To much generality of the specific usecase.  I don't want to put an Input
   interface on accelerometers where it makes no sense.  The rule of it has
   2-3 axis so it must make sense isn't good enough to my mind.  How
   does userspace know which accelerometer to use (more and more devices have
   multiple?)  You could do something like looking at the location info from
   DT / ACPI in your driver and pick the 'best' but that's policy. Should be
   in userspace.  Sure you can just use the right input driver, but the moment
   we do that, we need aware userspace, if that's the case why not make it
   aware from the start.

Believe me I've been round this one a good few times and thought about it
a lot.  I'll take a lot of convincing that this isn't a problem that
should be pushed into userspace.

> 
> I think having them mapped always does not need much resources (except a handful of bytes
> in memory and some µs during probing) unless the event device is opened and really used.
> Only then it starts e.g. I2C traffic to read the sensors.

The bytes don't really mater. The userspace ABI additions do.

> 
> So it is just some unused file sitting around in /dev. Or 2 or could be even 100.
> For devices which have no iio accelerometers configured, there will be no /dev/input
> file. So we are discussing the rare case of devices with more than one or two accelerometers.

Well they aren't exactly rare in IIO using systems ;)

> 
> Now, on every system there are many interfaces and files that are not used because it makes
> no sense to look at them. If I check on one of my systems, I find for example a lot of
> /dev/tty and only a very small portion is used and generic distros have no issue with it.
> 
> There is even /dev/iio:device0 to /dev/iio:device5 representing the raw iio devices.
> Not all of them are actively used, but they are simply there and can be scanned for.

Agreed, in the ideal case we wouldn't have had that either, but we are
stuck with it.  The long term plan is to allow use of IIO backends without the
front end being there at all. Lots of SoC ADC users would prefer this. We are
stuck with the legacy intertwining fo the front end and back end of IIO so
this isn't as easy to do as I would like.

> 
> So I do not see a resource problem if every accelerometer /dev/iio:device* gets
> some companion /dev/input/event* for being used on demand - but only if this bridge
> is configured at all.

That argument does not apply. If we add a config option, distros will enable it.
So the vast majority of systems will ship with this turned on.  You cannot
use a config variable to control policy and expect it to be change by anyone
but a very very small subset of users.  So please drop the 'you can just not
build it argument'.

Userspace configuration changing is a lot easier if people actually care.
Sure, many distros will ship the same script to everyone.

> 
> > I think we need some deliberate userspace interaction to instantiate
> > one of these rather than 'always doing it'.  
> 
> My gut feeling is that this additional user-space interaction needs more resources and
> adds a lot of complexity, independently of how it is done.

Trivial resources and actually fairly trivial complexity.  Key thing is
it puts the burden on the users of this functionality to configure what they
want.

> 
> And I think is even less flexible than "always doing it". Let me explain this claim.
> 
> For me, the kernel should present everything the hardware supports to user-space
> in better digestable device files or APIs (without making any assumptions about the
> user-space code).

Agreed, we just have a different view on how this should be done. I want
it to be dynamic and extremely flexible, you want the easy way of just
putting a fixed set out all the time.

> 
> Then, every user-space that will be installed can find out what the hardware supports
> by looking at standard places.
> 
> E.g. it can scan for all mice and keyboards. And for all input accelerometers.

Or, you an have the correct 'fairly trivial' userspace setup to scan for all
registered accelerometers and 'on demand' create the bindings to bring them up as
Input accelerometers if that is what makes sense for your platform.

> 
> If the kernel is hiding some chips and needs some initial user-space action before
> presenting them all, this requires that the user-space has some a-priori knowledge
> about which specific devices it should ask for.

No more that it needs to know which accelerometer to use?

> So it does not really need to scan
> for them. Because it must already know. Obviously in some mapping table stored at
> a well known location inside the rootfs image.

No. Let me give some more details of how this would work.  It's really just
a more flexible version of what you have.

A distro, or individual user decides to put the relevant script in place for the
following:

1. Userspace detects a new accelerometer driver, via the standard methods (uevent)
2. Userspace looks to see if it has the required properties. Now this includes things
like detecting that it is the accelerometer in the lid of a laptop - if so do not
register it as an input device.  If it's in the keyboard then do register it.
3. Userspace script then creates the files in configfs
/sys/kernel/config/iio/maps/
(this interface needs appropriate definition)
Maybe...
/sys/kernel/config/iio/maps/iio_input/iio_device:X/accel_x, accel_y, etc
When done it writes to the bind file
/sys/kernel/config/iio/maps/iio_input/iio_device:X/bind
which instantiates the input driver.

This moves all of the policy decision into userspace, where it belongs.  If
we want to enable a particular accelerometer on a particular board because it
actually works better than the one the default policy says to use, then we can
do so.

The resulting infrastructure is much more general, because it lets us do the
same for any IIO consumer.  This input bridge is not a special case. It works
equally well for the existing hwmon bridge any would even let us do things
like provide the information from userspace that we have an analog accelerometer
wired up to an ADC on some hacker board.


> 
> This seems to make it impossible to develop a generic distro rootfs image - without
> asking the user for manual configuration. And that where the kernel already knows
> this (which iio accelerometers do exist for a specific piece of hardware).
> 
> This is why I believe a mechanism to instantiate only on demand isn't adding but
> removing flexibility because it prevents copying a rootfs from one device to another.

I disagree, see above.

> 
> > 
> > As I mentioned in V1, look at the possibility of a configfs based method
> > to build the map.  It's easy for userspace to work out what makes sense to
> > map in principle.  There may be some missing info that we also need to
> > look to expose.  
> 
> With a "may be missing" it is impossible to write code for it...
> Can you please name which information is missing on the input accelerometer
> API?

See above. It's not the input accelerometer ABI, it's the missing ability
to instantiate IIO maps from user space.

> 
> > 
> > In general, userspace created channel maps would be very useful for
> > other things such as maker type boards where they can plug all sorts
> > of odd things into ADC channels for example.  
> 
> Ok, I understand, but this is a different problem where this iio-input-bridge is not
> intended to be a solution. Generic ADCs are not input devices. Like SD cards are not
> keyboards.
> 
> So we should not try to mix the idea of general mapping with this input-bridge for
> input accelerometers.
Yes we should. You are proposing a solution that is a subset of the larger
problem set.  Why introduce a stop gap like this when we can do it correctly
and provide something useful for all those other use cases.

The only difference here is the uevent triggered script that creates those maps
for your particular usecase.


> 
> BTW, there is a way to define additional mapping using udev rules which symlink the
> /dev/input/event* paths to stable names like /dev/input/accelerometer.
> 
> This comes without additional code and is already provided by udev and the input system.
> 
> So in summary, I have not yet seen a convincing scenario where being able to dynamically
> map iio channels to input devices seems beneficial.

That is true for the narrow case you are talking about. I don't want to see that
narrow case solved in a fashion that effectively breaks solving it properly.
If we add this, we have to export all accelerometers for ever under all circumstances
to userspace, because to remove it will break existing userspace.

If we stand back and work out if we can do the general solution now, we avoid
this problem.

> 
> >   
> >> 
> >> This driver simply collects the first 3 accelerometer channels as X, Y and Z.
> >> If only 1 or 2 channels are available, they are used for X and Y only. Additional
> >> channels are ignored.
> >> 
> >> Scaling is done automatically so that 1g is represented by value 256 and
> >> range is assumed to be -511 .. +511 which gives a reasonable precision as an
> >> input device.  
> > 
> > Why do we do this, rather than letting input deal with it?  Input is used
> > to widely differing scales IIRC  
> 
> Well, it can't be done differently... And what I call scale here is nothing more than
> defining ABSMIN_ACC_VAL and ABSMAX_ACC_VAL.
> 
> We need to apply some scale since iio reports in (fractional) units of 1g, i.e. values
> of magnitude 1.

m/s^2 not g, but doesn't matter for the point of view of this discussion.

> These are not adaequate for input events which use integers. So we must
> define some factor for iio_convert_raw_to_processed() to scale from raw value range
> to int value range. We could report raw values but this would be an improper abstraction
> from chip specific differences.

Hmm. I can see we perhaps need some mapping, but is there a concept of standard scale
for existing input accelerometers?  How is this done to give for other input devices
such as touch screens?  I'd expect to see a separation between scale, and range.


> 
> BTW: the range (and therefore the factor) is reported through the evdev driver to user-space
> (evtest reports Min and Max as you can see in the example).
> 
> The most important thing is that this is a hardware independent definition. Every accelerometer
> chip will report this range. So you can easily upgrade hardware or switch accelerometers
> without touching user-space calibration. Like you can replace ethernet controller chips but
> networking works the same with all of them.

Agreed, it needs to be hardware independent by the time it hits userspace, but I would
have thought that scaling would be done in input, rather than IIO. It's hardly
a problem unique to our usecase!

Perhaps Dmitry can give some advice on this.

> 
> >   
> >> 
> >> If a mount-matrix is provided by the iio driver, it is also taken into account
> >> so that the input event automatically gets the correct orientation with respect
> >> to the device.
> >> 
> >> If this extension is not configured into the kernel it takes no resources (except
> >> source code).
> >> 
> >> If it is configured, but there is no accelerometer, there is only a tiny penalty
> >> for scanning for accelerometer channels once during probe of each iio device.
> >> 
> >> If it runs, the driver polls the device(s) once every 100 ms. A mode where the
> >> iio device defines the update rate is not implemented and for further study.
> >> 
> >> If there is no user-space client, polling is not running.
> >> 
> >> The driver is capable to handle multiple iio accelerometers and they are
> >> presented by unique /dev/input/event* files. The iio chip name is used to define
> >> the input device name so that it can be identified (e.g. by udev rules or evtest).
> >> 
> >> Here is some example what you can expect from the driver (device:
> >> arch/arm/boot/dts/omap3-gta04a5.dts):
> >> 
> >> root@letux:~# dmesg|fgrep iio
> >> [    6.324584] input: iio-bridge: bmc150_accel as /devices/platform/68000000.ocp/48072000.i2c/i2c-1/1-0010/iio:device1/input/input5
> >> [    6.516632] input: iio-bridge: bno055 as /devices/platform/68000000.ocp/48072000.i2c/i2c-1/1-0029/iio:device3/input/input7
> >> root@letux:~# evtest /dev/input/event5 | head -19
> >> Input driver version is 1.0.1
> >> Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
> >> Input device name: "iio-bridge: bmc150_accel"
> >> Supported events:
> >>   Event type 0 (EV_SYN)
> >>   Event type 3 (EV_ABS)
> >>     Event code 0 (ABS_X)
> >>       Value      8
> >>       Min     -511
> >>       Max      511
> >>     Event code 1 (ABS_Y)
> >>       Value    -44
> >>       Min     -511
> >>       Max      511
> >>     Event code 2 (ABS_Z)
> >>       Value   -265
> >>       Min     -511
> >>       Max      511
> >> Properties:
> >> root@letux:~# evtest /dev/input/event7 | head -19
> >> Input driver version is 1.0.1
> >> Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
> >> Input device name: "iio-bridge: bno055"
> >> Supported events:
> >>   Event type 0 (EV_SYN)
> >>   Event type 3 (EV_ABS)
> >>     Event code 0 (ABS_X)
> >>       Value     -6
> >>       Min     -511
> >>       Max      511
> >>     Event code 1 (ABS_Y)
> >>       Value     17
> >>       Min     -511
> >>       Max      511
> >>     Event code 2 (ABS_Z)
> >>       Value   -250
> >>       Min     -511
> >>       Max      511
> >> Properties:
> >> root@letux:~# 
> >> 
> >> Although the sensor chips are mounted with different axis orientation,
> >> the application of the mount matrix provides equivalent (despite noise
> >> and precision) information on device orientation.
> >> 
> >> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
> >> ---
> >> drivers/iio/Kconfig                    |   7 +
> >> drivers/iio/Makefile                   |   1 +
> >> drivers/iio/industrialio-core.c        |  12 +
> >> drivers/iio/industrialio-inputbridge.c | 295 +++++++++++++++++++++++++
> >> drivers/iio/industrialio-inputbridge.h |  28 +++
> >> 5 files changed, 343 insertions(+)
> >> create mode 100644 drivers/iio/industrialio-inputbridge.c
> >> create mode 100644 drivers/iio/industrialio-inputbridge.h
> >> 
> >> diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
> >> index d08aeb41cd07..d85afe002613 100644
> >> --- a/drivers/iio/Kconfig
> >> +++ b/drivers/iio/Kconfig
> >> @@ -68,6 +68,13 @@ config IIO_TRIGGERED_EVENT
> >> 	help
> >> 	  Provides helper functions for setting up triggered events.
> >> 
> >> +config IIO_INPUT_BRIDGE
> >> +	bool "Enable accelerometer bridge to input driver"  
> > 
> > Dependency on input?  
> 
> Yes, should be added.
> 
> >   
> >> +	help
> >> +	  Provides a /dev/input/event* device for accelerometers
> >> +	  to use as a 3D input device, e.g. for gaming or auto-rotation
> >> +	  of screen contents.
> >> +
> >> source "drivers/iio/accel/Kconfig"
> >> source "drivers/iio/adc/Kconfig"
> >> source "drivers/iio/afe/Kconfig"
> >> diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
> >> index cb5993251381..d695e5a27da5 100644
> >> --- a/drivers/iio/Makefile
> >> +++ b/drivers/iio/Makefile
> >> @@ -7,6 +7,7 @@ obj-$(CONFIG_IIO) += industrialio.o
> >> industrialio-y := industrialio-core.o industrialio-event.o inkern.o
> >> industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o
> >> industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
> >> +industrialio-$(CONFIG_IIO_INPUT_BRIDGE) += industrialio-inputbridge.o
> >> 
> >> obj-$(CONFIG_IIO_CONFIGFS) += industrialio-configfs.o
> >> obj-$(CONFIG_IIO_SW_DEVICE) += industrialio-sw-device.o
> >> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> >> index 4700fd5d8c90..81f412b41a78 100644
> >> --- a/drivers/iio/industrialio-core.c
> >> +++ b/drivers/iio/industrialio-core.c
> >> @@ -29,6 +29,7 @@
> >> #include <linux/iio/iio.h>
> >> #include "iio_core.h"
> >> #include "iio_core_trigger.h"
> >> +#include "industrialio-inputbridge.h"
> >> #include <linux/iio/sysfs.h>
> >> #include <linux/iio/events.h>
> >> #include <linux/iio/buffer.h>
> >> @@ -1723,6 +1724,15 @@ int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
> >> 	if (ret < 0)
> >> 		goto error_unreg_eventset;
> >> 
> >> +	ret = iio_device_register_inputbridge(indio_dev);
> >> +	if (ret) {
> >> +		dev_err(indio_dev->dev.parent,
> >> +			"Failed to register as input driver\n");
> >> +		device_del(&indio_dev->dev);
> >> +  
> > This doesn't look like balanced error handling given the goto in the previous
> > case.  If we are treating this as an error we need to unwind the whole
> > of this function properly.  
> 
> Will to check.
> 
> >   
> >> +		return ret;
> >> +	}
> >> +
> >> 	return 0;
> >> 
> >> error_unreg_eventset:
> >> @@ -1745,6 +1755,8 @@ void iio_device_unregister(struct iio_dev *indio_dev)
> >> {
> >> 	mutex_lock(&indio_dev->info_exist_lock);
> >> 
> >> +	iio_device_unregister_inputbridge(indio_dev);
> >> +
> >> 	cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
> >> 
> >> 	iio_device_unregister_debugfs(indio_dev);
> >> diff --git a/drivers/iio/industrialio-inputbridge.c b/drivers/iio/industrialio-inputbridge.c
> >> new file mode 100644
> >> index 000000000000..dd672e25bc70
> >> --- /dev/null
> >> +++ b/drivers/iio/industrialio-inputbridge.c
> >> @@ -0,0 +1,295 @@
> >> +// SPDX-License-Identifier: GPL-2.0
> >> +/*
> >> + * The Industrial I/O core, bridge to input devices
> >> + *
> >> + * Copyright (c) 2016-2019 Golden Delicious Computers GmbH&Co. KG
> >> + *
> >> + * This program is free software; you can redistribute it and/or modify it
> >> + * under the terms of the GNU General Public License version 2 as published by
> >> + * the Free Software Foundation.  
> > No need for the boiler plate if you have SPDX.  That is one of the
> > advantages!  
> 
> Ok.
> 
> >   
> >> + */
> >> +
> >> +#include <linux/iio/consumer.h>
> >> +#include <linux/iio/iio.h>
> >> +#include <linux/iio/types.h>
> >> +#include <linux/input.h>
> >> +#include <linux/input-polldev.h>
> >> +#include <linux/module.h>
> >> +#include <linux/slab.h>
> >> +
> >> +#include "industrialio-inputbridge.h"
> >> +
> >> +/* currently, only polling is implemented */
> >> +#define POLLING_MSEC	100
> >> +
> >> +struct iio_input_map {
> >> +	struct input_polled_dev *poll_dev;	/* the input device */
> >> +	struct iio_channel channels[3];		/* x, y, z channels */
> >> +	struct matrix {
> >> +		int mxx, myx, mzx;	/* fixed point mount-matrix */
> >> +		int mxy, myy, mzy;
> >> +		int mxz, myz, mzz;
> >> +	} matrix;
> >> +};
> >> +
> >> +static inline struct iio_input_map *to_iio_input_map(
> >> +		struct iio_channel *channel)
> >> +{
> >> +	return (struct iio_input_map *) channel->data;
> >> +}
> >> +
> >> +/* minimum and maximum range we want to report */
> >> +#define ABSMAX_ACC_VAL		(512 - 1)
> >> +#define ABSMIN_ACC_VAL		-(ABSMAX_ACC_VAL)
> >> +
> >> +/* scale processed iio values so that 1g maps to ABSMAX_ACC_VAL / 2 */
> >> +#define SCALE			((100 * ABSMAX_ACC_VAL) / (2 * 981))
> >> +
> >> +/*
> >> + * convert float string to scaled fixed point format, e.g.
> >> + *   1		-> 1000		(value passed as unit)
> >> + *   1.23	-> 1230
> >> + *   0.1234	->  123
> >> + *   -.01234	->  -12
> >> + */
> >> +
> >> +static int32_t atofix(const char *str, uint32_t unit)
> >> +{
> >> +	int32_t mantissa = 0;
> >> +	bool sign = false;
> >> +	bool decimal = false;
> >> +	int32_t divisor = 1;
> >> +
> >> +	if (*str == '-')
> >> +		sign = true, str++;
> >> +	while (*str && divisor < unit) {
> >> +		if (*str >= '0' && *str <= '9') {
> >> +			mantissa = 10 * mantissa + (*str - '0');
> >> +			if (decimal)
> >> +				divisor *= 10;
> >> +		} else if (*str == '.')
> >> +			decimal = true;
> >> +		else
> >> +			return 0;	/* error */
> >> +		str++;
> >> +	}
> >> +
> >> +	mantissa = (mantissa * unit) / divisor;
> >> +	if (sign)
> >> +		mantissa = -mantissa;
> >> +
> >> +	return mantissa;
> >> +}
> >> +
> >> +static void iio_apply_matrix(struct matrix *m, int *in, int *out, uint32_t unit)
> >> +{
> >> +	/* apply mount matrix */
> >> +	out[0] = (m->mxx * in[0] + m->myx * in[1] + m->mzx * in[2]) / unit;
> >> +	out[1] = (m->mxy * in[0] + m->myy * in[1] + m->mzy * in[2]) / unit;
> >> +	out[2] = (m->mxz * in[0] + m->myz * in[1] + m->mzz * in[2]) / unit;
> >> +}
> >> +
> >> +#define FIXED_POINT_UNIT	1000	/* seems reasonable for accelerometer input */
> >> +
> >> +static void iio_accel_poll(struct input_polled_dev *dev)
> >> +{
> >> +	struct iio_input_map *map = dev->private;
> >> +	struct input_dev *input = dev->input;
> >> +
> >> +	int values[3];		/* values while processing */
> >> +	int aligned_values[3];	/* mount matrix applied */
> >> +
> >> +	int cindex = 0;
> >> +
> >> +printk("%s: map=%px input=%px\n", __func__, map, input);  
> > Remember to tidy these debug statements up at some point.  
> 
> Oops... Shouldn't be there. Maybe I did squash in some commit from my debugging branch.
> 
> >   
> >> +
> >> +	while (cindex < ARRAY_SIZE(values)) {
> >> +		struct iio_channel *channel =
> >> +			&map->channels[cindex];
> >> +		int val;
> >> +		int ret;
> >> +
> >> +		if (!channel->indio_dev) {
> >> +			values[cindex] = 0;
> >> +			continue;
> >> +		}
> >> +
> >> +		ret = iio_read_channel_raw(channel, &val);
> >> +
> >> +		if (ret < 0) {
> >> +			pr_err("%s(): channel read error %d\n",
> >> +				__func__, cindex);
> >> +			return;
> >> +		}
> >> +
> >> +		ret = iio_convert_raw_to_processed(channel, val,
> >> +				 &values[cindex], SCALE);
> >> +
> >> +		if (ret < 0) {
> >> +			pr_err("%s(): channel processing error\n",
> >> +				__func__);
> >> +			return;
> >> +		}
> >> +
> >> +		cindex++;
> >> +	}
> >> +
> >> +	iio_apply_matrix(&map->matrix, values, aligned_values, FIXED_POINT_UNIT);
> >> +
> >> +	input_report_abs(input, ABS_X, aligned_values[0]);
> >> +	input_report_abs(input, ABS_Y, aligned_values[1]);
> >> +	input_report_abs(input, ABS_Z, aligned_values[2]);
> >> +	input_sync(input);
> >> +}
> >> +
> >> +static int dindex=0;	/* assign unique names to accel/input devices */  
> > Build something from the iio device IDA perhaps?  Those are unique
> > as well.  Useful to know which one this is linked to.  
> 
> We just use this unique number in the poll_dev->input->phys name.
> Mainly this number gives them unique names in /sys/class/input/input*/phys
>  
> Using the iio device IDA seems to be a very good replacement.
> 
> >   
> >> +
> >> +static int iio_input_register_accel_channel(struct iio_dev *indio_dev,
> >> +		 const struct iio_chan_spec *chan)
> >> +{ /* we found some accelerometer channel */
> >> +	int ret;
> >> +	int cindex;
> >> +	struct iio_input_map *map = iio_device_get_drvdata(indio_dev);  
> > 
> > Don't do that.   That is in the domain of the device driver and so
> > will sometimes already be in use.   
> 
> Hm. Is there an alternative to attach such private data to an struct iio_dev
> allocated by someone else? I have not found one yet.
> 
> Or can I add some void *input_mapping; to struct iio_dev? Depending on
> #if defined(CONFIG_IIO_INPUT_BRIDGE)?

Yes, add a new element.

> 
> >   
> >> +
> >> +printk("%s: map=%px\n", __func__, map);
> >> +
> >> +	if (!map) {
> >> +		struct input_polled_dev *poll_dev;
> >> +		const struct iio_chan_spec_ext_info *ext_info;
> >> +
> >> +		map = devm_kzalloc(&indio_dev->dev, sizeof(struct iio_input_map), GFP_KERNEL);
> >> +		if (!map)
> >> +			return -ENOMEM;
> >> +
> >> +		iio_device_set_drvdata(indio_dev, map);
> >> +
> >> +		poll_dev = devm_input_allocate_polled_device(&indio_dev->dev);
> >> +		if (!poll_dev)
> >> +			return -ENOMEM;
> >> +
> >> +		poll_dev->private = map;
> >> +		poll_dev->poll = iio_accel_poll;
> >> +		poll_dev->poll_interval = POLLING_MSEC;
> >> +
> >> +		poll_dev->input->name = kasprintf(GFP_KERNEL, "iio-bridge: %s",
> >> +						    indio_dev->name);
> >> +		poll_dev->input->phys = kasprintf(GFP_KERNEL, "accel/input%d",  
> 
> If we use the IDA, we can define phys as "iio:device%d", indio_dev->id)
> which makes it easy to locate the associated iio device file name (although
> there are other mechanisms in sysfs tree).
> 
> >> +						    dindex++);
> >> +
> >> +// do we need something like this?
> >> +//		poll_dev->input->id.bustype = BUS_IIO;
> >> +//		poll_dev->input->id.vendor = 0x0001;
> >> +//		poll_dev->input->id.product = 0x0001;
> >> +//		poll_dev->input->id.version = 0x0001;
> >> +
> >> +		set_bit(INPUT_PROP_ACCELEROMETER, poll_dev->input->propbit);
> >> +		poll_dev->input->evbit[0] = BIT_MASK(EV_ABS);
> >> +		input_alloc_absinfo(poll_dev->input);
> >> +		input_set_abs_params(poll_dev->input, ABS_X, ABSMIN_ACC_VAL,
> >> +					ABSMAX_ACC_VAL, 0, 0);
> >> +		input_set_abs_params(poll_dev->input, ABS_Y, ABSMIN_ACC_VAL,
> >> +					ABSMAX_ACC_VAL, 0, 0);
> >> +		input_set_abs_params(poll_dev->input, ABS_Z, ABSMIN_ACC_VAL,
> >> +					ABSMAX_ACC_VAL, 0, 0);
> >> +
> >> +		map->poll_dev = poll_dev;
> >> +
> >> +		ret = input_register_polled_device(poll_dev);
> >> +
> >> +		if (ret < 0) {
> >> +			kfree(poll_dev->input->name);
> >> +			kfree(poll_dev->input->phys);
> >> +			return ret;
> >> +		}
> >> +
> >> +		/* assume all channels of a device share the same matrix */
> >> +
> >> +		ext_info = chan->ext_info;
> >> +		for (; ext_info && ext_info->name; ext_info++) {
> >> +			if (strcmp(ext_info->name, "mount_matrix") == 0)
> >> +				break;
> >> +		}
> >> +
> >> +		if (ext_info && ext_info->name) {
> >> +			/* matrix found */
> >> +			uintptr_t priv = ext_info->private;
> >> +			const struct iio_mount_matrix *mtx;
> >> +
> >> +			mtx = ((iio_get_mount_matrix_t *) priv)(indio_dev,
> >> +								chan);
> >> +
> >> +			map->matrix.mxx = atofix(mtx->rotation[0], FIXED_POINT_UNIT);
> >> +			map->matrix.myx = atofix(mtx->rotation[1], FIXED_POINT_UNIT);
> >> +			map->matrix.mzx = atofix(mtx->rotation[2], FIXED_POINT_UNIT);
> >> +			map->matrix.mxy = atofix(mtx->rotation[3], FIXED_POINT_UNIT);
> >> +			map->matrix.myy = atofix(mtx->rotation[4], FIXED_POINT_UNIT);
> >> +			map->matrix.mzy = atofix(mtx->rotation[5], FIXED_POINT_UNIT);
> >> +			map->matrix.mxz = atofix(mtx->rotation[6], FIXED_POINT_UNIT);
> >> +			map->matrix.myz = atofix(mtx->rotation[7], FIXED_POINT_UNIT);
> >> +			map->matrix.mzz = atofix(mtx->rotation[8], FIXED_POINT_UNIT);
> >> +		} else {
> >> +			map->matrix.mxx = FIXED_POINT_UNIT;
> >> +			map->matrix.myx = 0;
> >> +			map->matrix.mzx = 0;
> >> +			map->matrix.mxy = 0;
> >> +			map->matrix.myy = FIXED_POINT_UNIT;
> >> +			map->matrix.mzy = 0;
> >> +			map->matrix.mxz = 0;
> >> +			map->matrix.myz = 0;
> >> +			map->matrix.mzz = FIXED_POINT_UNIT;
> >> +		}
> >> +	}
> >> +
> >> +// brauchen wir das noch? Oder nehmen wir einfach an dass es 3 Kanäle gibt?
> >> +
> >> +	/* find free channel within this device */
> >> +
> >> +	for (cindex = 0; cindex < ARRAY_SIZE(map->channels); cindex++) {
> >> +		if (!map->channels[cindex].indio_dev)
> >> +			break;
> >> +	}
> >> +
> >> +	/* check if we already have collected enough channels */
> >> +	if (cindex == ARRAY_SIZE(map->channels))
> >> +		return 0;	/* silently ignore */
> >> +
> >> +	map->channels[cindex].indio_dev = indio_dev;
> >> +	map->channels[cindex].channel = chan;
> >> +	map->channels[cindex].data = map;
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +int iio_device_register_inputbridge(struct iio_dev *indio_dev)
> >> +{
> >> +	int i;
> >> +
> >> +	for (i = 0; i < indio_dev->num_channels; i++) {
> >> +		const struct iio_chan_spec *chan =
> >> +				&indio_dev->channels[i];
> >> +
> >> +		if (chan->type == IIO_ACCEL) {
> >> +			int r = iio_input_register_accel_channel(indio_dev,
> >> +								 chan);  
> > It would be cleaner (and safer) to go find all the necessary channels then
> > set up the map in one go, rather that iterating and trying to build it
> > in a sequential fashion.
> > 
> > So move the search loop inside and have something like.
> > 
> > iio_input_find_accel_channel(indio_dev, chan, &numchans);
> > iio_input_register_device(indio_dev, chan, numchans);  
> 
> Well, that looks like it needs some temporary storage of dynamic size
> and loop twice over channels for no functional benefit.

Use fixed size. The worst that happens is we end up with it being
an entry larger that it needs to be.

> And handle the
> special case of numchans == 0 (the proposed code simply does not call
> iio_input_register_accel_channel and does not register anything).
> 
> So I'd prefer to follow the "KISS" principle and register single channels
> instead of a set of channels.

Well we disagree on this.  A singleton approach like used here
is to my mind not KISS.  I would rather see what is there then
act as two simple steps, rather than interleave two different
actions with a totally different path for the first channel found.
If there is only one channel you just built a load of infrastructure
that makes no sense.  If you scan first then you can know that
before building anything.


> 
> >   
> >> +
> >> +			if (r < 0)
> >> +				return r;
> >> +		}
> >> +	}
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +void iio_device_unregister_inputbridge(struct iio_dev *indio_dev)
> >> +{
> >> +	struct iio_input_map *map = iio_device_get_drvdata(indio_dev);
> >> +	struct input_dev *input = map->poll_dev->input;
> >> +
> >> +	kfree(input->name);
> >> +	kfree(input->phys);
> >> +	input_unregister_polled_device(map->poll_dev);
> >> +}
> >> +
> >> +MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
> >> +MODULE_DESCRIPTION("Bridge to present Industrial I/O accelerometers as properly oriented Input devices");
> >> +MODULE_LICENSE("GPL v2");
> >> diff --git a/drivers/iio/industrialio-inputbridge.h b/drivers/iio/industrialio-inputbridge.h
> >> new file mode 100644
> >> index 000000000000..1363b10ab3f7
> >> --- /dev/null
> >> +++ b/drivers/iio/industrialio-inputbridge.h
> >> @@ -0,0 +1,28 @@
> >> +/* SPDX-License-Identifier: GPL-2.0 */
> >> +/*
> >> + * The Industrial I/O core, bridge to input devices
> >> + *
> >> + * Copyright (c) 2016-2019 Golden Delicious Computers GmbH&Co. KG
> >> + *
> >> + * This program is free software; you can redistribute it and/or modify it
> >> + * under the terms of the GNU General Public License version 2 as published by
> >> + * the Free Software Foundation.
> >> + */
> >> +
> >> +#if defined(CONFIG_IIO_INPUT_BRIDGE)
> >> +
> >> +extern int iio_device_register_inputbridge(struct iio_dev *indio_dev);
> >> +extern void iio_device_unregister_inputbridge(struct iio_dev *indio_dev);
> >> +
> >> +#else
> >> +
> >> +static inline int iio_device_register_inputbridge(struct iio_dev *indio_dev)
> >> +{
> >> +	return 0;
> >> +}
> >> +
> >> +static inline void iio_device_unregister_inputbridge(struct iio_dev *indio_dev)
> >> +{
> >> +}
> >> +
> >> +#endif  
> >   
> 
> BR and thanks,
> Nikolaus
> 
Thanks,

Jonathan


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-04-14 11:40     ` Jonathan Cameron
@ 2019-04-14 16:26       ` Roderick Colenbrander
  2019-05-10  8:57         ` Bastien Nocera
       [not found]       ` <CD6219BE-61FF-4C38-9532-054C60A77F89@goldelico.com>
  1 sibling, 1 reply; 16+ messages in thread
From: Roderick Colenbrander @ 2019-04-14 16:26 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: H. Nikolaus Schaller, Dmitry Torokhov, Eric Piel, linux-input,
	letux-kernel, kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, lkml, linux-iio

Hi Jonathan and Nikolaus,

I would like to jump into this discussion a bit as well. Might be
slightly off topic or not... I'm not too familiar with the details of
IIO, but as Sony we do report accelerometer / gyroscope data through
/dev/input for our DualShock devices through hid-sony. This is among
reasons done for Android, which Nikolaus showed interest in as well.
We are working on exposing this data through user space through
standard Android APIs (see,
https://android-review.googlesource.com/c/platform/hardware/libhardware/+/571762/).
The work will likely be merged later this year and it is already used
in some devices. Main reason it wasn't merged yet it that in our case
we have multiple evdev devices (gamepad, touchpad, motion sensors) and
need to tie those together for applications. Android doesn't support a
good mechanism for that yet.

Since the IIO input work relates to our Android work as well, I will
jump in more from the sidelines to make sure things are done in a
consistent manner. The main concern are resolution and value ranges.

Thanks,
Roderick

On Sun, Apr 14, 2019 at 4:41 AM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Mon, 8 Apr 2019 15:15:56 +0200
> H. Nikolaus Schaller <hns@goldelico.com> wrote:
>
> > Hi Jonathan,
> >
> > > Am 07.04.2019 um 14:30 schrieb Jonathan Cameron <jic23@kernel.org>:
> > >
> > > On Sun, 31 Mar 2019 12:09:46 +0200
> > > "H. Nikolaus Schaller" <hns@goldelico.com> wrote:
> > >
> > > Hi Nikolaus,
> > >
> > > I'm probably going to repeat a few things I sent for v1 as the audience has
> > > expanded somewhat!
> > >
> > > Good to see this moving forwards though as there has been at least some demand
> > > for it going way back to the early days of IIO.
> > >
> > >> Some user spaces (e.g. some Android devices) use /dev/input/event* for handling
> > >> the 3D position of the device with respect to the center of gravity (earth).
> > >> This can be used for gaming input, auto-rotation of screens etc.
> > >>
> > >> This interface should be the standard for such use cases because it is an abstraction
> > >> of how orientation data is acquired from sensor chips. Sensor chips may be connected
> > >> through different interfaces and in different positions. They may also have different
> > >> parameters. And, if a chip is replaced by a different one, the values reported by
> > >> the device position interface should remain the same, provided the device tree reflects
> > >> the changed chip.
> > >>
> > >> This did initially lead to input accelerometer drivers like drivers/input/misc/bma150.c
> > >> or drivers/misc/lis3lv02d/
> > >>
> > >> But nowadays, new accelerometer chips mostly get iio drivers and rarely input drivers.
> > >>
> > >> Therefore we need something like a protocol stack which bridges raw data and input devices.
> > >> It can be seen as a similar layering like TCP/IP vs. bare Ethernet. Or keyboard
> > >> input events vs. raw gpio or raw USB access.
> > >>
> > >> This patch bridges the gap between raw iio data and the input device abstraction
> > >> so that accelerometer measurements can additionally be presented as X/Y/Z accelerometer
> > >> channels (INPUT_PROP_ACCELEROMETER) through /dev/input/event*.
> > >>
> > >> There are no special requirements or changes needed for an iio driver.
> > >>
> > >> There is no need to define a mapping (e.g. in device tree).
> > > This worries me, as it inherently means we end up with this interface being
> > > registered in cases where it makes no sense.  A lot of generic distros get
> > > used across widely differing use cases.
> >
> > I still do not fully understand what is worrying you here.
>
> >
> > Do you worry about functionality, flexibility or resources or something else?
>
> Two main things:
> 1) Lack of generality of the approach.
>    This is a single use trick for input devices. Why does it make sense for
>    input devices?  There are lots of other in kernel users and potential
>    ones in the future.  The ability to register additional IIO consumers like
>    this is useful, lets make it useful to everyone.
>
> 2) To much generality of the specific usecase.  I don't want to put an Input
>    interface on accelerometers where it makes no sense.  The rule of it has
>    2-3 axis so it must make sense isn't good enough to my mind.  How
>    does userspace know which accelerometer to use (more and more devices have
>    multiple?)  You could do something like looking at the location info from
>    DT / ACPI in your driver and pick the 'best' but that's policy. Should be
>    in userspace.  Sure you can just use the right input driver, but the moment
>    we do that, we need aware userspace, if that's the case why not make it
>    aware from the start.
>
> Believe me I've been round this one a good few times and thought about it
> a lot.  I'll take a lot of convincing that this isn't a problem that
> should be pushed into userspace.
>
> >
> > I think having them mapped always does not need much resources (except a handful of bytes
> > in memory and some µs during probing) unless the event device is opened and really used.
> > Only then it starts e.g. I2C traffic to read the sensors.
>
> The bytes don't really mater. The userspace ABI additions do.
>
> >
> > So it is just some unused file sitting around in /dev. Or 2 or could be even 100.
> > For devices which have no iio accelerometers configured, there will be no /dev/input
> > file. So we are discussing the rare case of devices with more than one or two accelerometers.
>
> Well they aren't exactly rare in IIO using systems ;)
>
> >
> > Now, on every system there are many interfaces and files that are not used because it makes
> > no sense to look at them. If I check on one of my systems, I find for example a lot of
> > /dev/tty and only a very small portion is used and generic distros have no issue with it.
> >
> > There is even /dev/iio:device0 to /dev/iio:device5 representing the raw iio devices.
> > Not all of them are actively used, but they are simply there and can be scanned for.
>
> Agreed, in the ideal case we wouldn't have had that either, but we are
> stuck with it.  The long term plan is to allow use of IIO backends without the
> front end being there at all. Lots of SoC ADC users would prefer this. We are
> stuck with the legacy intertwining fo the front end and back end of IIO so
> this isn't as easy to do as I would like.
>
> >
> > So I do not see a resource problem if every accelerometer /dev/iio:device* gets
> > some companion /dev/input/event* for being used on demand - but only if this bridge
> > is configured at all.
>
> That argument does not apply. If we add a config option, distros will enable it.
> So the vast majority of systems will ship with this turned on.  You cannot
> use a config variable to control policy and expect it to be change by anyone
> but a very very small subset of users.  So please drop the 'you can just not
> build it argument'.
>
> Userspace configuration changing is a lot easier if people actually care.
> Sure, many distros will ship the same script to everyone.
>
> >
> > > I think we need some deliberate userspace interaction to instantiate
> > > one of these rather than 'always doing it'.
> >
> > My gut feeling is that this additional user-space interaction needs more resources and
> > adds a lot of complexity, independently of how it is done.
>
> Trivial resources and actually fairly trivial complexity.  Key thing is
> it puts the burden on the users of this functionality to configure what they
> want.
>
> >
> > And I think is even less flexible than "always doing it". Let me explain this claim.
> >
> > For me, the kernel should present everything the hardware supports to user-space
> > in better digestable device files or APIs (without making any assumptions about the
> > user-space code).
>
> Agreed, we just have a different view on how this should be done. I want
> it to be dynamic and extremely flexible, you want the easy way of just
> putting a fixed set out all the time.
>
> >
> > Then, every user-space that will be installed can find out what the hardware supports
> > by looking at standard places.
> >
> > E.g. it can scan for all mice and keyboards. And for all input accelerometers.
>
> Or, you an have the correct 'fairly trivial' userspace setup to scan for all
> registered accelerometers and 'on demand' create the bindings to bring them up as
> Input accelerometers if that is what makes sense for your platform.
>
> >
> > If the kernel is hiding some chips and needs some initial user-space action before
> > presenting them all, this requires that the user-space has some a-priori knowledge
> > about which specific devices it should ask for.
>
> No more that it needs to know which accelerometer to use?
>
> > So it does not really need to scan
> > for them. Because it must already know. Obviously in some mapping table stored at
> > a well known location inside the rootfs image.
>
> No. Let me give some more details of how this would work.  It's really just
> a more flexible version of what you have.
>
> A distro, or individual user decides to put the relevant script in place for the
> following:
>
> 1. Userspace detects a new accelerometer driver, via the standard methods (uevent)
> 2. Userspace looks to see if it has the required properties. Now this includes things
> like detecting that it is the accelerometer in the lid of a laptop - if so do not
> register it as an input device.  If it's in the keyboard then do register it.
> 3. Userspace script then creates the files in configfs
> /sys/kernel/config/iio/maps/
> (this interface needs appropriate definition)
> Maybe...
> /sys/kernel/config/iio/maps/iio_input/iio_device:X/accel_x, accel_y, etc
> When done it writes to the bind file
> /sys/kernel/config/iio/maps/iio_input/iio_device:X/bind
> which instantiates the input driver.
>
> This moves all of the policy decision into userspace, where it belongs.  If
> we want to enable a particular accelerometer on a particular board because it
> actually works better than the one the default policy says to use, then we can
> do so.
>
> The resulting infrastructure is much more general, because it lets us do the
> same for any IIO consumer.  This input bridge is not a special case. It works
> equally well for the existing hwmon bridge any would even let us do things
> like provide the information from userspace that we have an analog accelerometer
> wired up to an ADC on some hacker board.
>
>
> >
> > This seems to make it impossible to develop a generic distro rootfs image - without
> > asking the user for manual configuration. And that where the kernel already knows
> > this (which iio accelerometers do exist for a specific piece of hardware).
> >
> > This is why I believe a mechanism to instantiate only on demand isn't adding but
> > removing flexibility because it prevents copying a rootfs from one device to another.
>
> I disagree, see above.
>
> >
> > >
> > > As I mentioned in V1, look at the possibility of a configfs based method
> > > to build the map.  It's easy for userspace to work out what makes sense to
> > > map in principle.  There may be some missing info that we also need to
> > > look to expose.
> >
> > With a "may be missing" it is impossible to write code for it...
> > Can you please name which information is missing on the input accelerometer
> > API?
>
> See above. It's not the input accelerometer ABI, it's the missing ability
> to instantiate IIO maps from user space.
>
> >
> > >
> > > In general, userspace created channel maps would be very useful for
> > > other things such as maker type boards where they can plug all sorts
> > > of odd things into ADC channels for example.
> >
> > Ok, I understand, but this is a different problem where this iio-input-bridge is not
> > intended to be a solution. Generic ADCs are not input devices. Like SD cards are not
> > keyboards.
> >
> > So we should not try to mix the idea of general mapping with this input-bridge for
> > input accelerometers.
> Yes we should. You are proposing a solution that is a subset of the larger
> problem set.  Why introduce a stop gap like this when we can do it correctly
> and provide something useful for all those other use cases.
>
> The only difference here is the uevent triggered script that creates those maps
> for your particular usecase.
>
>
> >
> > BTW, there is a way to define additional mapping using udev rules which symlink the
> > /dev/input/event* paths to stable names like /dev/input/accelerometer.
> >
> > This comes without additional code and is already provided by udev and the input system.
> >
> > So in summary, I have not yet seen a convincing scenario where being able to dynamically
> > map iio channels to input devices seems beneficial.
>
> That is true for the narrow case you are talking about. I don't want to see that
> narrow case solved in a fashion that effectively breaks solving it properly.
> If we add this, we have to export all accelerometers for ever under all circumstances
> to userspace, because to remove it will break existing userspace.
>
> If we stand back and work out if we can do the general solution now, we avoid
> this problem.
>
> >
> > >
> > >>
> > >> This driver simply collects the first 3 accelerometer channels as X, Y and Z.
> > >> If only 1 or 2 channels are available, they are used for X and Y only. Additional
> > >> channels are ignored.
> > >>
> > >> Scaling is done automatically so that 1g is represented by value 256 and
> > >> range is assumed to be -511 .. +511 which gives a reasonable precision as an
> > >> input device.
> > >
> > > Why do we do this, rather than letting input deal with it?  Input is used
> > > to widely differing scales IIRC
> >
> > Well, it can't be done differently... And what I call scale here is nothing more than
> > defining ABSMIN_ACC_VAL and ABSMAX_ACC_VAL.
> >
> > We need to apply some scale since iio reports in (fractional) units of 1g, i.e. values
> > of magnitude 1.
>
> m/s^2 not g, but doesn't matter for the point of view of this discussion.

m/s^2 is conceptually right, but others have been using 'g' for input,
so I would consider that the standard now. Also leaves handling of
location on Earth to user space instead of assuming a fixed conversion
to 9.81 m/s^2.

> > These are not adaequate for input events which use integers. So we must
> > define some factor for iio_convert_raw_to_processed() to scale from raw value range
> > to int value range. We could report raw values but this would be an improper abstraction
> > from chip specific differences.
>
> Hmm. I can see we perhaps need some mapping, but is there a concept of standard scale
> for existing input accelerometers?  How is this done to give for other input devices
> such as touch screens?  I'd expect to see a separation between scale, and range.
>

We at the time were one of the first to expose acceleration and gyro
data through /dev/input for DualShock 4 as supported by hid-sony. We
report acceleration in 'g' and angular velocity in 'degree / s'. We
set the resolution to respectively '1 g' and '1 degree / s'. The range
we set to the range of the device e.g. for DS4 -4g to +4g for
acceleration. I need to check though what coordinate system we use,
but I think it is right handed (gyro counter clockwise relative to
acceleration axes).

The two other drivers using INPUT_PROC_ACCELEROMETER are hid-wacom and
hid-udraw-ps3 Wacom. Both seem to report resolution in 'g'  as well.

Thanks,
Roderick

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
       [not found]       ` <CD6219BE-61FF-4C38-9532-054C60A77F89@goldelico.com>
@ 2019-04-22 14:20         ` Jonathan Cameron
  2019-05-09  9:09           ` H. Nikolaus Schaller
  2019-05-10  8:57           ` Bastien Nocera
  0 siblings, 2 replies; 16+ messages in thread
From: Jonathan Cameron @ 2019-04-22 14:20 UTC (permalink / raw)
  To: H. Nikolaus Schaller
  Cc: Dmitry Torokhov, Eric Piel, linux-input, letux-kernel, kernel,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-kernel, linux-iio

On Mon, 15 Apr 2019 23:04:15 +0200
H. Nikolaus Schaller <hns@goldelico.com> wrote:

> Hi Jonathan,
> we seem to have opposite goals and therefore quite different ideas what the right
> solution is.

Yup :)  Yikes this email has gotten complex.  Hopefully I haven't
lost track of the various points below.

> 
> I come from user-space and the input side, find that input can handle abstract "acceleration input",
> and some older chip drivers even provide that as their only interface. Therefore I want
> to provide "acceleration input" in these cases where only iio capable drivers exist by
> using the existing in-kernel-iio infrastructure. Nothing more.
> 
> You seem to come from the iio architecture and want to extend it to other APIs as
> general as possible and input is just one of several of them.

Yes, my target is to produce a subsystem that meets many (all would be nice)
requirements, including yours.  Whilst I'm happy to debate this for ever, I'm not
sure we are making any substantial progress.  As you mention below we
probably need to 'see the code' to drive the discussion forwards.

> 
> Different goals usually lead to different solution architectures.

Indeed, but in this case we have your proposal which is a subset of what
I am suggesting.  One architecture can fulfil both requirements.

I'll leave it for the other thread, but Bastien has raised the case
(that I'd forgotten) that there already userspace stacks that are
capable of happily taking in both IIO and Input devices.  The confusion
here is they will now discover 'both' without the existing userspace
knowing that they are the same device.  We need to be very careful not
to break those userspace programs.

So this is an illustration of why the simplistic case doesn't work
'now'.

> 
> > Am 14.04.2019 um 13:40 schrieb Jonathan Cameron <jic23@kernel.org>:
> > 
> > On Mon, 8 Apr 2019 15:15:56 +0200
> > H. Nikolaus Schaller <hns@goldelico.com> wrote:
> >   
> >> Hi Jonathan,
> >> 
> >> I still do not fully understand what is worrying you here.  
> >   
> >> 
> >> Do you worry about functionality, flexibility or resources or something else?  
> > 
> > Two main things:
> > 1) Lack of generality of the approach. 
> >   This is a single use trick for input devices. Why does it make sense for
> >   input devices?  
> 
> No, it is not a trick...
Bad choice of words. Sorry about that.

Any time we register a device as two types of device it is less than ideal.

If we had the true separation of IIO front end and back end then it
would be perfectly acceptable to 'only' have an input front end for
a given device.  That choice would still, in this sort of usecase,
have to be made from userspace. It's policy, not design.  If there are reasons
a particular device 'is input' then that mapping should be in DT or similar.
It's no different from knowing an ADC channel is wired to an analog
temperature sensor.  For example, you could build a joystick with
an accelerometer in the stick - then the usecase would be obvious!
Hence I would also argue that any dynamic interface should also support
a static path (DT or equivalent) for the cases where is really a
physical characteristic of the system!  Perhaps the Sony parts
fall into this category as well.

For a bit of historical background, there was a concerted effort
to produced a userspace stack for IIO for android.  
https://01.org/android-iio-sensors-hal

Unfortunately I think it died as a result of other moves in Intel on
one of their periodic shifts in direction.

> 
> Why does it make sense for input devices?

These are not just 'input' devices. They are accelerometers. One usecase
is to use them for input.  The exact same physical device is used for games
input uses and counting steps for example (actually a lot wider cases than
that, but this one is common in android devices).

Keep that in mind at all times. There are lots of usecases.
So we need a solution that does not result in problems for those
usecases.  We are not writing a subsytem targetting android use of
accelerometers. We are writing a subsystem addressing as many usecases
as we can of those devices.

Note that the original reason for IIO was to generalize a whole set of
proposed individual subsystems targeting particular sensor types. So
that is our focus - solutions that work for everyone.  This isn't
totally dissimilar from those discussions - at the time I wanted
to do a small focused ALS subsystem and got a resounding "no" from
Linus.  Generality matters a lot for the long term.

> 
> a) because there is no alternative for accelerometer input devices and there are some
> (older) accelerometer chips which only present themselves as input devices as soon
> as the driver is compiled and the chip is found.

Actually that is not accurate.  The vast majority of those older devices
that have had any attempt at mainlining are in IIO. AFAIK no accelerometer
driver has been merged to mainline input for many years. This is because,
amongst others, Dmitry has been of the view they didn't belong there for
a very long time.

> 
> b) because input events define ACCEL input devices. But no Temperature or Voltage
> input or generic ADC input.So there is no generalization of input devices beyond
> keyboard, mouse, joystick, accelerometer and some others.

That's not totally inaccurate, but the distinction in the other sensor cases
is that there is a clear 'additional' element that we can map in devicetree
which relates the IIO sensor channel to the input device.
Doesn't matter for the point of view of this discussion though.

> 
> >  There are lots of other in kernel users and potential
> >   ones in the future.  
> 
> This bridge addition does not exclude any (additional) in-kernel use.

No but it creates several problems:

1. Two ways to do the same thing. 
2. Two sets of code to maintain.
3. Confusion over what is the best way of doing it.
4. The known issues with multiple consumers (note my solution has
that problem as well!)

My job here is to maintain the code, which is why I push back on something
that makes that job harder.

When the next usecases comes along and someone says they want to map
all ADC channels to hwmon channels because that is the subystem that
they expect to measure voltages in, then I don't have a good argument
to stop them doing the same thing you have.

As a side note we have in the past had input drivers for gyroscopes and
magnetometers.  Why are accelerometers special?

I really don't see why we should treat accelerometers differently

As this discussion runs on, I am increasingly convinced that there *must*
be a userspace policy control over whether an input device is
instantiated for a given accelerometer.   Once that is the
case then I cannot see a reason to treat it any differently from
other channel types.

> 
> >  The ability to register additional IIO consumers like
> >   this is useful, lets make it useful to everyone.  
> 
> But not everyone and every iio device can and should be mappable to the "input" abstraction.
> This does not make any sense to me.

Absolutely.  It should not be.  I clearly didn't explain this well.

It should be mapped to a consumer.

One type of consumer is iio-input, another is iio-hmwon etc.

> 
> For example, does it make sense to map a temperature sensor to accelerometer input? Or an
> accelerometer to hwmon? This seems to be possible of your generalization unless I am missing something here.
> If it is, it ignores that iio sensors are already grouped by what physical property they do measure.

If people want to map crazy channels to crazy sensor outputs, why stop them?
(at this level of the interface).

This is a policy question for a userspace script.  Particular consumer drivers
could of course perform sanity checking and refuse to do anything if they
cannot sensibly use the channels.

Yes, the interface has this flexibility. Which is a good thing!  Take the example
of the gyroscope as input I used above.  If we want to add that support
in future to your driver (I have no idea if it actually makes sense)
then we can - without having to change the interface.

> 
> > 
> > 2) To much generality of the specific usecase.  I don't want to put an Input
> >   interface on accelerometers where it makes no sense.  
> 
> I think you can just ignore the input interfaces in that case, if it was created.

Bastien raised a case where this isn't true.

> 
> >  The rule of it has
> >   2-3 axis so it must make sense isn't good enough to my mind.  How
> >   does userspace know which accelerometer to use (more and more devices have
> >   multiple?)  
> 
> In our approach user-space can make it known by udev rules based on /dev/input/event*
> (not on iio but the input created for accelerometers). I think I mentioned that. This
> comes for free for any device registering as input. So it is no additional code.

Sorry, I'm lost.  What in there tells you to use 'this' interface rather than one
of the other N that were registered?  I'm not sure what information you
have available there.

> 
> >  You could do something like looking at the location info from
> >   DT / ACPI in your driver and pick the 'best' but that's policy. Should be
> >   in userspace.  Sure you can just use the right input driver, but the moment
> >   we do that, we need aware userspace, if that's the case why not make it
> >   aware from the start.
> > 
> > Believe me I've been round this one a good few times and thought about it
> > a lot.  
> 
> That is fine and why we should discuss all the different aspects we have collected.
> 
> >  I'll take a lot of convincing that this isn't a problem that
> > should be pushed into userspace.
> >   
> >> 
> >> I think having them mapped always does not need much resources (except a handful of bytes
> >> in memory and some µs during probing) unless the event device is opened and really used.
> >> Only then it starts e.g. I2C traffic to read the sensors.  
> > 
> > The bytes don't really mater.  
> 
> Ok, good to know.
> 
> > The userspace ABI additions do.  
> 
> There are only new /dev/input/event devices with well defined ABI. This approach does
> not invent anything new here, hence there are no ABI additions which we can break.

But it does - we aren't talking general ABI, but ABI on specific
devices.  Sure, Android doesn't care - though you'd be amazed how much
individual android device developers will because we just added another
pile of tests to their CI.

An industrial sensor platform absolutely does.  They have to validate
those interfaces.  They can't just ignore them because they feel like
it because who knows if some future user will use them?

For another case, see Bastien's reply to the later thread.

Instantiating interfaces has testing costs, even when the are standard
interfaces.

> 
> >   
> >> 
> >> So it is just some unused file sitting around in /dev. Or 2 or could be even 100.
> >> For devices which have no iio accelerometers configured, there will be no /dev/input
> >> file. So we are discussing the rare case of devices with more than one or two accelerometers.  
> > 
> > Well they aren't exactly rare in IIO using systems ;)  
> 
> This is another thing where our experiences differ. What specific devices are you thinking
> of? I am focussed on handhelds where the accelerometer (or two) is a way to do GUI input
> depending on device orientation in space.

Again, you are introducing this interface for everyone. Including lots of
'interesting' usecases.

I have worked with sensor platforms with accelerometers of different parts of humans,
We have people do bridge vibration measurement, flying UAVs and tracking the motion
of trucks.

Most are not huge numbers of accelerometers per node but don't rule out the
possibility.  It's normally limited by length of cables rather than anything
else so you used multiple nodes after a while each with their own set of sensors.

There are lots of plaforms out there that use multiple accelerometers in more
or less the same place to do very high dynamic range measurement (without losing
precision when things are nearly still).

Anyhow, it's not a particularly important point anyway!

> >   
> >> 
> >> Now, on every system there are many interfaces and files that are not used because it makes
> >> no sense to look at them. If I check on one of my systems, I find for example a lot of
> >> /dev/tty and only a very small portion is used and generic distros have no issue with it.
> >> 
> >> There is even /dev/iio:device0 to /dev/iio:device5 representing the raw iio devices.
> >> Not all of them are actively used, but they are simply there and can be scanned for.  
> > 
> > Agreed, in the ideal case we wouldn't have had that either, but we are
> > stuck with it.  The long term plan is to allow use of IIO backends without the
> > front end being there at all. Lots of SoC ADC users would prefer this. We are
> > stuck with the legacy intertwining fo the front end and back end of IIO so
> > this isn't as easy to do as I would like.  
> 
> Ah, ok. I think it is a similar discussion of hiding the serdev /dev/tty* if it is
> used for accessing an embedded GPS or Bluetooth chip, for example.
> 
> But is this needed? I think it is not a problem if there are multiple consumers for
> the same iio channel. Some in-kernel, some through /dev/iio:device* and maybe some
> through /dev/input (which boils down to in-kernel).

There are quite a few complexities around multiple consumers that we really haven't
solved.  Right now the cases that work are very much restricted.  I'd love
to tidy some of these up, but never enough time and all that.

It's not that relevant here, but in short a few of the issues are:
1) Interference over control settings.  - Two consumers need different filter
   settings/ sampling frequency / range.  How do we negotiate the choice and
   communicate it to the other consumers.  More complex questions such
   mediating choices of triggers.
2) One driver is doing polled reads, the other is doing interrupt driven.
   Most drivers prevent this combination because the polled reads can lead
   to unlimited delays on the interrupt driven path and hence break it.

The main driver for this separation was to present only the 'right' interface
to reduced people's validation costs etc.  People really do want to have the
option to strip back the userspace inteface.  Obviously these are the rare
people who would disable your config option, but the point of this was
that we actually would like to make even the IIO interface optional as
well but have a fair way to go before we can.

> 
> >   
> >> 
> >> So I do not see a resource problem if every accelerometer /dev/iio:device* gets
> >> some companion /dev/input/event* for being used on demand - but only if this bridge
> >> is configured at all.  
> > 
> > That argument does not apply. If we add a config option, distros will enable it.
> > So the vast majority of systems will ship with this turned on.  You cannot
> > use a config variable to control policy and expect it to be change by anyone
> > but a very very small subset of users.  So please drop the 'you can just not
> > build it argument'.  
> 
> This is not my point here. I mention this under the (now known to be wrong) assumption
> that resources do care. I just want to state that kernels built for platforms where every
> byte counts can be stripped down by disabling it. Others where resources are no concern
> simply can map them all, even if not used.

Agreed. A subset of users will just build without this.

> 
> > Userspace configuration changing is a lot easier if people actually care.
> > Sure, many distros will ship the same script to everyone.
> >   
> >>   
> >>> I think we need some deliberate userspace interaction to instantiate
> >>> one of these rather than 'always doing it'.    
> >> 
> >> My gut feeling is that this additional user-space interaction needs more resources and
> >> adds a lot of complexity, independently of how it is done.  
> > 
> > Trivial resources and actually fairly trivial complexity.  Key thing is
> > it puts the burden on the users of this functionality to configure what they
> > want.  
> 
> Hm. No. My proposal does not need configuration which accelerometers should go where.

Agreed. I was talking about my proposal here :)

> 
> I assumethat input accelerometer users do not want to configure anything, like neither
> a mouse or keyboard is to be configured to be useable (yes there are keymaps but that
> is impossible to automate).

The difference is a mouse is only really useful as a mouse and most of the time a keyboard
is a used only as a keyboard.  Here that's not true.

> 
> They just want to be able to read device orientation in a device-independent scale.
> Therefore my approach already takes the mount-matrix into account to hide sensor position
> differences.

And how does that work on the common case of a sensor in the lid of a laptop?
how do you know what angle the screen is at?  
One oddity to note here is that until very recently we deliberately didn't register
certain ACPI IDs because they confused userspace by reporting two accelerometers
without any info on which was in the lid.  Thankfully proper handling of that
is no being sorted.  It's still mostly a case of just deliberately ignoring one
of the sensors.

> 
> >   
> >> 
> >> And I think is even less flexible than "always doing it". Let me explain this claim.
> >> 
> >> For me, the kernel should present everything the hardware supports to user-space
> >> in better digestable device files or APIs (without making any assumptions about the
> >> user-space code).  
> > 
> > Agreed, we just have a different view on how this should be done. I want
> > it to be dynamic and extremely flexible, you want the easy way of just
> > putting a fixed set out all the time.
> >   
> >> 
> >> Then, every user-space that will be installed can find out what the hardware supports
> >> by looking at standard places.
> >> 
> >> E.g. it can scan for all mice and keyboards. And for all input accelerometers.  
> > 
> > Or, you an have the correct 'fairly trivial' userspace setup to scan for all
> > registered accelerometers and 'on demand' create the bindings to bring them up as
> > Input accelerometers if that is what makes sense for your platform.  
> 
> Why not scan for input accelerometers and leave it as an implementation detail that
> the kernel does serve the physical chips through the iio infrastructure?

If we could separate the IIO front end from the IIO backend I would agree that
would be another valid -userspace- policy.

> 
> IMHO some user-spaces may already be scanning all */input/event* and check for
> the device property INPUT_PROP_ACCELEROMETER.
> 
> This is a discussion mainly about proper encapsulation of lower level differences.
> 
> >   
> >> 
> >> If the kernel is hiding some chips and needs some initial user-space action before
> >> presenting them all, this requires that the user-space has some a-priori knowledge
> >> about which specific devices it should ask for.  
> > 
> > No more that it needs to know which accelerometer to use?  
> 
> >   
> >> So it does not really need to scan
> >> for them. Because it must already know. Obviously in some mapping table stored at
> >> a well known location inside the rootfs image.  
> > 
> > No. Let me give some more details of how this would work.  It's really just
> > a more flexible version of what you have.
> > 
> > A distro, or individual user decides to put the relevant script in place for the
> > following:
> > 
> > 1. Userspace detects a new accelerometer driver, via the standard methods (uevent)
> > 2. Userspace looks to see if it has the required properties. Now this includes things
> > like detecting that it is the accelerometer in the lid of a laptop - if so do not
> > register it as an input device.  If it's in the keyboard then do register it.
> > 3. Userspace script then creates the files in configfs
> > /sys/kernel/config/iio/maps/
> > (this interface needs appropriate definition)
> > Maybe...
> > /sys/kernel/config/iio/maps/iio_input/iio_device:X/accel_x, accel_y, etc
> > When done it writes to the bind file
> > /sys/kernel/config/iio/maps/iio_input/iio_device:X/bind
> > which instantiates the input driver.
> > 
> > This moves all of the policy decision into userspace, where it belongs.  If
> > we want to enable a particular accelerometer on a particular board because it
> > actually works better than the one the default policy says to use, then we can
> > do so.
> > 
> > The resulting infrastructure is much more general, because it lets us do the
> > same for any IIO consumer.  This input bridge is not a special case. It works
> > equally well for the existing hwmon bridge any would even let us do things
> > like provide the information from userspace that we have an analog accelerometer
> > wired up to an ADC on some hacker board.  
> 
> Ok, understood.
> 
> My approach triggers input uevents:
> 
> 1. kernel detects a new iio accelerometer (looks like an analog accelerometer should be
>    the DTS child of an iio adc and then iio should create an accelerometer and not a voltage
>    channel)

Yes ultimately it would be a child device that would be it's own IIO device. We
already have this for some gyroscopes.

> 2. iio-bridge registers as input event
> 3. this triggers an uevent
> 4  an udev-rule can detect the properties and map it to some "speaking" name like
>    /dev/input/main-accelerometer, /dev/input/lid-accelerometer etc. Or if the
>    accelerometer is to be ignored, it does not get a "speaking" name at all.
> 
> The required udev rules are stored in user space and are of course user-space and application
> specific. But this does not require to invent some new configfs stuff and special scripts
> in user-space. Just install some udev rule at a well established location in file-system.

I'm not sure there is any significant difference between you creating a mapping like
this an udev rule that creates the whole mapping.  Bit more to do perhaps but it's
nothing particularly special that I can see.  Sure there is new kernel support to be
done.

> 
> Yes, this does not cover arbitrary mappings. But what are arbitrary mappings good
> for? Your scheme seems to be able to map a light sensor to accelerometer input.
> Does this "full matrix of everything is possible" really make sense?

From a generic interface point of view - yes it absolutely does.

We define an interface that covers all usecases rather than a whole set of
separate ones that cover individual corner cases.  That way we don't have to
keep defining new interfaces.

The individual drivers can easily do validation of what they are provided with.

> 
> I can't decide because I have no need for it. Others may have.
> 
> But another thought: does it interfere with this input-bridge? Probably no. You can
> still add your configfs approach for general iio devices to e.g. hwmon mappings. Even
> as an alternate method of creating input devices (enabled only if my input-bridge is
> disabled).

Yes see above.  Both approaches meet your requirement (I think anyway).
I do not want to see two long term solutions to the same problem.

I'm interested in a long term sustainable solution so I want to see
the generic one.

> 
> > 
> >   
> >> 
> >> This seems to make it impossible to develop a generic distro rootfs image - without
> >> asking the user for manual configuration. And that where the kernel already knows
> >> this (which iio accelerometers do exist for a specific piece of hardware).
> >> 
> >> This is why I believe a mechanism to instantiate only on demand isn't adding but
> >> removing flexibility because it prevents copying a rootfs from one device to another.  
> > 
> > I disagree, see above.
> >   
> >>   
> >>> 
> >>> As I mentioned in V1, look at the possibility of a configfs based method
> >>> to build the map.  It's easy for userspace to work out what makes sense to
> >>> map in principle.  There may be some missing info that we also need to
> >>> look to expose.    
> >> 
> >> With a "may be missing" it is impossible to write code for it...
> >> Can you please name which information is missing on the input accelerometer
> >> API?  
> > 
> > See above. It's not the input accelerometer ABI, it's the missing ability
> > to instantiate IIO maps from user space.
> >   
> >>   
> >>> 
> >>> In general, userspace created channel maps would be very useful for
> >>> other things such as maker type boards where they can plug all sorts
> >>> of odd things into ADC channels for example.    
> >> 
> >> Ok, I understand, but this is a different problem where this iio-input-bridge is not
> >> intended to be a solution. Generic ADCs are not input devices. Like SD cards are not
> >> keyboards.
> >> 
> >> So we should not try to mix the idea of general mapping with this input-bridge for
> >> input accelerometers.  
> > Yes we should. You are proposing a solution that is a subset of the larger
> > problem set.  
> 
> Yes, of course. Because I did not see or know about the general problem set.
> And I still don't see a need for user-space controlled mapping for input-accelerometers.

We are clearly going to differ on this.  Bastien gave one example for why
this is required.  There will be others.

> 
> >  Why introduce a stop gap like this when we can do it correctly
> > and provide something useful for all those other use cases.
> > 
> > The only difference here is the uevent triggered script that creates those maps
> > for your particular usecase.  
> 
> Well, I am a friend of solving one problem after the other in smaller steps than
> immediately aiming at a very general solution, which has side-effects of inventing
> new stuff for things that would work without.

That works in a world where you can drop the previous approach as part of your
generalization.  When you are playing with kernel / userspace ABI then it
doesn't. Ideally you have to figure out the extensible general solution at the
start because you are stuck maintaining the 'small steps' for many years to
come.  I don't want to perpetually 'have' to export all 3D accelerometers as
input devices, because we didn't have the ability to chose which should be
exported at some point in the past.

> 
> > 
> >   
> >> 
> >> BTW, there is a way to define additional mapping using udev rules which symlink the
> >> /dev/input/event* paths to stable names like /dev/input/accelerometer.
> >> 
> >> This comes without additional code and is already provided by udev and the input system.
> >> 
> >> So in summary, I have not yet seen a convincing scenario where being able to dynamically
> >> map iio channels to input devices seems beneficial.  
> > 
> > That is true for the narrow case you are talking about. I don't want to see that
> > narrow case solved in a fashion that effectively breaks solving it properly.  
> 
> How does it break your approach if added later? The more I think about it they are
> not incompatible. It is just useless to apply both in parallel.

The reality is that if we put one in first that will used for ever because there
will be devices out there using it.  Therefore we have to maintain both for
ever.

> 
> > If we add this, we have to export all accelerometers for ever under all circumstances
> > to userspace, because to remove it will break existing userspace.
> > 
> > If we stand back and work out if we can do the general solution now, we avoid
> > this problem.  
> 
> We get a different problem that we break existing user-space that simply wants to see
> an /dev/input/accelerometer without doing more than an existing udev rule.

I would love to say such a userspace doesn't exist, but reality is there are
all sorts of hideous things out there.  There are cases that deal with this
as an option of course (such as Bastien's sensor-proxy)

The number of devices that are supported under mainline as input accelerometers
is pretty small.  It's not a perfect world unfortunately but having to add a
small udev script is at least not a major break if we do cause it.
> 
> >   
> >>   
> >>>   
> >>>> 
> >>>> This driver simply collects the first 3 accelerometer channels as X, Y and Z.
> >>>> If only 1 or 2 channels are available, they are used for X and Y only. Additional
> >>>> channels are ignored.
> >>>> 
> >>>> Scaling is done automatically so that 1g is represented by value 256 and
> >>>> range is assumed to be -511 .. +511 which gives a reasonable precision as an
> >>>> input device.    
> >>> 
> >>> Why do we do this, rather than letting input deal with it?  Input is used
> >>> to widely differing scales IIRC    
> >> 
> >> Well, it can't be done differently... And what I call scale here is nothing more than
> >> defining ABSMIN_ACC_VAL and ABSMAX_ACC_VAL.
> >> 
> >> We need to apply some scale since iio reports in (fractional) units of 1g, i.e. values
> >> of magnitude 1.  
> > 
> > m/s^2 not g, but doesn't matter for the point of view of this discussion.  
> 
> My fault. The driver takes care of this in the scaling formula so that "input" reports
> MAX/2 for 1g.
> 
> >   
> >> These are not adaequate for input events which use integers. So we must
> >> define some factor for iio_convert_raw_to_processed() to scale from raw value range
> >> to int value range. We could report raw values but this would be an improper abstraction
> >> from chip specific differences.  
> > 
> > Hmm. I can see we perhaps need some mapping, but is there a concept of standard scale
> > for existing input accelerometers?  How is this done to give for other input devices
> > such as touch screens?  I'd expect to see a separation between scale, and range.
> > 
> >   
> >> 
> >> BTW: the range (and therefore the factor) is reported through the evdev driver to user-space
> >> (evtest reports Min and Max as you can see in the example).
> >> 
> >> The most important thing is that this is a hardware independent definition. Every accelerometer
> >> chip will report this range. So you can easily upgrade hardware or switch accelerometers
> >> without touching user-space calibration. Like you can replace ethernet controller chips but
> >> networking works the same with all of them.  
> > 
> > Agreed, it needs to be hardware independent by the time it hits userspace, but I would
> > have thought that scaling would be done in input, rather than IIO. It's hardly
> > a problem unique to our usecase!
> > 
> > Perhaps Dmitry can give some advice on this.  
> 
> Yes, that would be helpful.
> 
> >   
> >> 
> >> 
> >> Hm. Is there an alternative to attach such private data to an struct iio_dev
> >> allocated by someone else? I have not found one yet.
> >> 
> >> Or can I add some void *input_mapping; to struct iio_dev? Depending on
> >> #if defined(CONFIG_IIO_INPUT_BRIDGE)?  
> > 
> > Yes, add a new element.  
> 
> Ok, works fine.
> 
> I already have found one case of iio accelerometer driver where it did make a problem
> not using a special element.
> 
> >>> 
> >>> iio_input_find_accel_channel(indio_dev, chan, &numchans);
> >>> iio_input_register_device(indio_dev, chan, numchans);    
> >> 
> >> Well, that looks like it needs some temporary storage of dynamic size
> >> and loop twice over channels for no functional benefit.  
> > 
> > Use fixed size. The worst that happens is we end up with it being
> > an entry larger that it needs to be.
> >   
> >> And handle the
> >> special case of numchans == 0 (the proposed code simply does not call
> >> iio_input_register_accel_channel and does not register anything).
> >> 
> >> So I'd prefer to follow the "KISS" principle and register single channels
> >> instead of a set of channels.  
> > 
> > Well we disagree on this.  A singleton approach like used here
> > is to my mind not KISS.  I would rather see what is there then
> > act as two simple steps, rather than interleave two different
> > actions with a totally different path for the first channel found.
> > If there is only one channel you just built a load of infrastructure
> > that makes no sense.  If you scan first then you can know that
> > before building anything.  
> 
> Ok, this is more a matter of taste and resource requirements can probably
> be neglected. I'll update the driver.
> 
> So in summary, I'll post a v3 that fixes some bugs of v2 (because we need
> them fixed for our production systems as well).
> 
> Then it is up to you if you want to take this approach or want to write
> a full version following your concept. Or if it is possible as I assume, we
> can have both.

Thanks. I think we need at some code for what I was proposing to discuss
much further. Unfortunately it may be a little while before I get time to
work on that.  Hopefully not too long though!

Jonathan

> 
> BR and thanks,
> Nikolaus
> 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-04-22 14:20         ` Jonathan Cameron
@ 2019-05-09  9:09           ` H. Nikolaus Schaller
  2019-05-09 17:02             ` [Letux-kernel] " H. Nikolaus Schaller
  2019-05-11 10:54             ` Jonathan Cameron
  2019-05-10  8:57           ` Bastien Nocera
  1 sibling, 2 replies; 16+ messages in thread
From: H. Nikolaus Schaller @ 2019-05-09  9:09 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Dmitry Torokhov, Eric Piel, linux-input, letux-kernel, kernel,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-kernel, linux-iio

Hi Jonathan,
sorry for the delayed response, but I have to focus on other topics now.

Just some small comments came to my mind to finish the discussion, mainly on the accelerometer in the lid case (see inline below).

BR and thanks,
Nikolaus


> Am 22.04.2019 um 16:20 schrieb Jonathan Cameron <jic23@kernel.org>:
> 
> On Mon, 15 Apr 2019 23:04:15 +0200
> H. Nikolaus Schaller <hns@goldelico.com> wrote:
> 
>> Hi Jonathan,
>> we seem to have opposite goals and therefore quite different ideas what the right
>> solution is.
> 
> Yup :)  Yikes this email has gotten complex.  Hopefully I haven't
> lost track of the various points below.
> 
>> 
>> I come from user-space and the input side, find that input can handle abstract "acceleration input",
>> and some older chip drivers even provide that as their only interface. Therefore I want
>> to provide "acceleration input" in these cases where only iio capable drivers exist by
>> using the existing in-kernel-iio infrastructure. Nothing more.
>> 
>> You seem to come from the iio architecture and want to extend it to other APIs as
>> general as possible and input is just one of several of them.
> 
> Yes, my target is to produce a subsystem that meets many (all would be nice)
> requirements, including yours.  Whilst I'm happy to debate this for ever, I'm not
> sure we are making any substantial progress.  As you mention below we
> probably need to 'see the code' to drive the discussion forwards.
> 
>> 
>> Different goals usually lead to different solution architectures.
> 
> Indeed, but in this case we have your proposal which is a subset of what
> I am suggesting.  One architecture can fulfil both requirements.

Not exactly. Yours always needs configuration in every case. My RFC works without
user-space config support for the most common cases. This user-space config must
be maintained and spread over all distributions. So we can never be sure that
if a user changes the distro that it still works.

> 
> I'll leave it for the other thread, but Bastien has raised the case
> (that I'd forgotten) that there already userspace stacks that are
> capable of happily taking in both IIO and Input devices.  The confusion
> here is they will now discover 'both' without the existing userspace
> knowing that they are the same device.  We need to be very careful not
> to break those userspace programs.
> 
> So this is an illustration of why the simplistic case doesn't work
> 'now'.
> 
>> 
>>> Am 14.04.2019 um 13:40 schrieb Jonathan Cameron <jic23@kernel.org>:
>>> 
>>> On Mon, 8 Apr 2019 15:15:56 +0200
>>> H. Nikolaus Schaller <hns@goldelico.com> wrote:
>>> 
>>>> Hi Jonathan,
>>>> 
>>>> I still do not fully understand what is worrying you here.  
>>> 
>>>> 
>>>> Do you worry about functionality, flexibility or resources or something else?  
>>> 
>>> Two main things:
>>> 1) Lack of generality of the approach. 
>>>  This is a single use trick for input devices. Why does it make sense for
>>>  input devices?  
>> 
>> No, it is not a trick...
> Bad choice of words. Sorry about that.
> 
> Any time we register a device as two types of device it is less than ideal.
> 
> If we had the true separation of IIO front end and back end then it
> would be perfectly acceptable to 'only' have an input front end for
> a given device.  That choice would still, in this sort of usecase,
> have to be made from userspace. It's policy, not design.  If there are reasons
> a particular device 'is input' then that mapping should be in DT or similar.
> It's no different from knowing an ADC channel is wired to an analog
> temperature sensor.  For example, you could build a joystick with
> an accelerometer in the stick - then the usecase would be obvious!
> Hence I would also argue that any dynamic interface should also support
> a static path (DT or equivalent) for the cases where is really a
> physical characteristic of the system!  Perhaps the Sony parts
> fall into this category as well.
> 
> For a bit of historical background, there was a concerted effort
> to produced a userspace stack for IIO for android.  
> https://01.org/android-iio-sensors-hal
> 
> Unfortunately I think it died as a result of other moves in Intel on
> one of their periodic shifts in direction.
> 
>> 
>> Why does it make sense for input devices?
> 
> These are not just 'input' devices. They are accelerometers. One usecase
> is to use them for input.  The exact same physical device is used for games
> input uses and counting steps for example (actually a lot wider cases than
> that, but this one is common in android devices).
> 
> Keep that in mind at all times. There are lots of usecases.
> So we need a solution that does not result in problems for those
> usecases.  We are not writing a subsytem targetting android use of
> accelerometers. We are writing a subsystem addressing as many usecases
> as we can of those devices.
> 
> Note that the original reason for IIO was to generalize a whole set of
> proposed individual subsystems targeting particular sensor types. So
> that is our focus - solutions that work for everyone.  This isn't
> totally dissimilar from those discussions - at the time I wanted
> to do a small focused ALS subsystem and got a resounding "no" from
> Linus.  Generality matters a lot for the long term.
> 
>> 
>> a) because there is no alternative for accelerometer input devices and there are some
>> (older) accelerometer chips which only present themselves as input devices as soon
>> as the driver is compiled and the chip is found.
> 
> Actually that is not accurate.  The vast majority of those older devices
> that have had any attempt at mainlining are in IIO. AFAIK no accelerometer
> driver has been merged to mainline input for many years. This is because,
> amongst others, Dmitry has been of the view they didn't belong there for
> a very long time.
> 
>> 
>> b) because input events define ACCEL input devices. But no Temperature or Voltage
>> input or generic ADC input.So there is no generalization of input devices beyond
>> keyboard, mouse, joystick, accelerometer and some others.
> 
> That's not totally inaccurate, but the distinction in the other sensor cases
> is that there is a clear 'additional' element that we can map in devicetree
> which relates the IIO sensor channel to the input device.
> Doesn't matter for the point of view of this discussion though.
> 
>> 
>>> There are lots of other in kernel users and potential
>>>  ones in the future.  
>> 
>> This bridge addition does not exclude any (additional) in-kernel use.
> 
> No but it creates several problems:
> 
> 1. Two ways to do the same thing. 
> 2. Two sets of code to maintain.
> 3. Confusion over what is the best way of doing it.
> 4. The known issues with multiple consumers (note my solution has
> that problem as well!)
> 
> My job here is to maintain the code, which is why I push back on something
> that makes that job harder.
> 
> When the next usecases comes along and someone says they want to map
> all ADC channels to hwmon channels because that is the subystem that
> they expect to measure voltages in, then I don't have a good argument
> to stop them doing the same thing you have.
> 
> As a side note we have in the past had input drivers for gyroscopes and
> magnetometers.  Why are accelerometers special?
> 
> I really don't see why we should treat accelerometers differently

Not special. I just did not want to add them yet in the RFC phase.
The principle is the same.

> 
> As this discussion runs on, I am increasingly convinced that there *must*
> be a userspace policy control over whether an input device is
> instantiated for a given accelerometer.   Once that is the
> case then I cannot see a reason to treat it any differently from
> other channel types.

Well, I believe that we should avoid any user-space policy control that
can be avoided. At least those cases that can live without policy
control should not need to get one because there are other cases that
need one.

This is something we can't solve by discussion, of course.

The decision is that you are maintainer and I am just proposing an RFC.
So you have two votes and a veto right...

But maybe this can be fixed by proper defaults? I don't know.

> 
>> 
>>> The ability to register additional IIO consumers like
>>>  this is useful, lets make it useful to everyone.  
>> 
>> But not everyone and every iio device can and should be mappable to the "input" abstraction.
>> This does not make any sense to me.
> 
> Absolutely.  It should not be.  I clearly didn't explain this well.
> 
> It should be mapped to a consumer.
> 
> One type of consumer is iio-input, another is iio-hmwon etc.
> 
>> 
>> For example, does it make sense to map a temperature sensor to accelerometer input? Or an
>> accelerometer to hwmon? This seems to be possible of your generalization unless I am missing something here.
>> If it is, it ignores that iio sensors are already grouped by what physical property they do measure.
> 
> If people want to map crazy channels to crazy sensor outputs, why stop them?
> (at this level of the interface).
> 
> This is a policy question for a userspace script.  Particular consumer drivers
> could of course perform sanity checking and refuse to do anything if they
> cannot sensibly use the channels.
> 
> Yes, the interface has this flexibility. Which is a good thing!  Take the example
> of the gyroscope as input I used above.  If we want to add that support
> in future to your driver (I have no idea if it actually makes sense)
> then we can - without having to change the interface.
> 
>> 
>>> 
>>> 2) To much generality of the specific usecase.  I don't want to put an Input
>>>  interface on accelerometers where it makes no sense.  
>> 
>> I think you can just ignore the input interfaces in that case, if it was created.
> 
> Bastien raised a case where this isn't true.
> 
>> 
>>> The rule of it has
>>>  2-3 axis so it must make sense isn't good enough to my mind.  How
>>>  does userspace know which accelerometer to use (more and more devices have
>>>  multiple?)  
>> 
>> In our approach user-space can make it known by udev rules based on /dev/input/event*
>> (not on iio but the input created for accelerometers). I think I mentioned that. This
>> comes for free for any device registering as input. So it is no additional code.
> 
> Sorry, I'm lost.  What in there tells you to use 'this' interface rather than one
> of the other N that were registered?  I'm not sure what information you
> have available there.
> 
>> 
>>> You could do something like looking at the location info from
>>>  DT / ACPI in your driver and pick the 'best' but that's policy. Should be
>>>  in userspace.  Sure you can just use the right input driver, but the moment
>>>  we do that, we need aware userspace, if that's the case why not make it
>>>  aware from the start.
>>> 
>>> Believe me I've been round this one a good few times and thought about it
>>> a lot.  
>> 
>> That is fine and why we should discuss all the different aspects we have collected.
>> 
>>> I'll take a lot of convincing that this isn't a problem that
>>> should be pushed into userspace.
>>> 
>>>> 
>>>> I think having them mapped always does not need much resources (except a handful of bytes
>>>> in memory and some µs during probing) unless the event device is opened and really used.
>>>> Only then it starts e.g. I2C traffic to read the sensors.  
>>> 
>>> The bytes don't really mater.  
>> 
>> Ok, good to know.
>> 
>>> The userspace ABI additions do.  
>> 
>> There are only new /dev/input/event devices with well defined ABI. This approach does
>> not invent anything new here, hence there are no ABI additions which we can break.
> 
> But it does - we aren't talking general ABI, but ABI on specific
> devices.  Sure, Android doesn't care - though you'd be amazed how much
> individual android device developers will because we just added another
> pile of tests to their CI.
> 
> An industrial sensor platform absolutely does.  They have to validate
> those interfaces.  They can't just ignore them because they feel like
> it because who knows if some future user will use them?
> 
> For another case, see Bastien's reply to the later thread.
> 
> Instantiating interfaces has testing costs, even when the are standard
> interfaces.
> 
>> 
>>> 
>>>> 
>>>> So it is just some unused file sitting around in /dev. Or 2 or could be even 100.
>>>> For devices which have no iio accelerometers configured, there will be no /dev/input
>>>> file. So we are discussing the rare case of devices with more than one or two accelerometers.  
>>> 
>>> Well they aren't exactly rare in IIO using systems ;)  
>> 
>> This is another thing where our experiences differ. What specific devices are you thinking
>> of? I am focussed on handhelds where the accelerometer (or two) is a way to do GUI input
>> depending on device orientation in space.
> 
> Again, you are introducing this interface for everyone. Including lots of
> 'interesting' usecases.
> 
> I have worked with sensor platforms with accelerometers of different parts of humans,
> We have people do bridge vibration measurement, flying UAVs and tracking the motion
> of trucks.
> 
> Most are not huge numbers of accelerometers per node but don't rule out the
> possibility.  It's normally limited by length of cables rather than anything
> else so you used multiple nodes after a while each with their own set of sensors.
> 
> There are lots of plaforms out there that use multiple accelerometers in more
> or less the same place to do very high dynamic range measurement (without losing
> precision when things are nearly still).
> 
> Anyhow, it's not a particularly important point anyway!
> 
>>> 
>>>> 
>>>> Now, on every system there are many interfaces and files that are not used because it makes
>>>> no sense to look at them. If I check on one of my systems, I find for example a lot of
>>>> /dev/tty and only a very small portion is used and generic distros have no issue with it.
>>>> 
>>>> There is even /dev/iio:device0 to /dev/iio:device5 representing the raw iio devices.
>>>> Not all of them are actively used, but they are simply there and can be scanned for.  
>>> 
>>> Agreed, in the ideal case we wouldn't have had that either, but we are
>>> stuck with it.  The long term plan is to allow use of IIO backends without the
>>> front end being there at all. Lots of SoC ADC users would prefer this. We are
>>> stuck with the legacy intertwining fo the front end and back end of IIO so
>>> this isn't as easy to do as I would like.  
>> 
>> Ah, ok. I think it is a similar discussion of hiding the serdev /dev/tty* if it is
>> used for accessing an embedded GPS or Bluetooth chip, for example.
>> 
>> But is this needed? I think it is not a problem if there are multiple consumers for
>> the same iio channel. Some in-kernel, some through /dev/iio:device* and maybe some
>> through /dev/input (which boils down to in-kernel).
> 
> There are quite a few complexities around multiple consumers that we really haven't
> solved.  Right now the cases that work are very much restricted.  I'd love
> to tidy some of these up, but never enough time and all that.
> 
> It's not that relevant here, but in short a few of the issues are:
> 1) Interference over control settings.  - Two consumers need different filter
>   settings/ sampling frequency / range.  How do we negotiate the choice and
>   communicate it to the other consumers.  More complex questions such
>   mediating choices of triggers.
> 2) One driver is doing polled reads, the other is doing interrupt driven.
>   Most drivers prevent this combination because the polled reads can lead
>   to unlimited delays on the interrupt driven path and hence break it.
> 
> The main driver for this separation was to present only the 'right' interface
> to reduced people's validation costs etc.  People really do want to have the
> option to strip back the userspace inteface.  Obviously these are the rare
> people who would disable your config option, but the point of this was
> that we actually would like to make even the IIO interface optional as
> well but have a fair way to go before we can.
> 
>> 
>>> 
>>>> 
>>>> So I do not see a resource problem if every accelerometer /dev/iio:device* gets
>>>> some companion /dev/input/event* for being used on demand - but only if this bridge
>>>> is configured at all.  
>>> 
>>> That argument does not apply. If we add a config option, distros will enable it.
>>> So the vast majority of systems will ship with this turned on.  You cannot
>>> use a config variable to control policy and expect it to be change by anyone
>>> but a very very small subset of users.  So please drop the 'you can just not
>>> build it argument'.  
>> 
>> This is not my point here. I mention this under the (now known to be wrong) assumption
>> that resources do care. I just want to state that kernels built for platforms where every
>> byte counts can be stripped down by disabling it. Others where resources are no concern
>> simply can map them all, even if not used.
> 
> Agreed. A subset of users will just build without this.
> 
>> 
>>> Userspace configuration changing is a lot easier if people actually care.
>>> Sure, many distros will ship the same script to everyone.
>>> 
>>>> 
>>>>> I think we need some deliberate userspace interaction to instantiate
>>>>> one of these rather than 'always doing it'.    
>>>> 
>>>> My gut feeling is that this additional user-space interaction needs more resources and
>>>> adds a lot of complexity, independently of how it is done.  
>>> 
>>> Trivial resources and actually fairly trivial complexity.  Key thing is
>>> it puts the burden on the users of this functionality to configure what they
>>> want.  
>> 
>> Hm. No. My proposal does not need configuration which accelerometers should go where.
> 
> Agreed. I was talking about my proposal here :)
> 
>> 
>> I assumethat input accelerometer users do not want to configure anything, like neither
>> a mouse or keyboard is to be configured to be useable (yes there are keymaps but that
>> is impossible to automate).
> 
> The difference is a mouse is only really useful as a mouse and most of the time a keyboard
> is a used only as a keyboard.  Here that's not true.
> 
>> 
>> They just want to be able to read device orientation in a device-independent scale.
>> Therefore my approach already takes the mount-matrix into account to hide sensor position
>> differences.
> 
> And how does that work on the common case of a sensor in the lid of a laptop?
> how do you know what angle the screen is at?  

Well, I am not aware of laptops where the sensor is in the lid because I am in the handhelds
business, but let's assume it is common.

I realized that if the sensor orientation is related to the lid position, while the reference
frame reported to user space is to be referenced to the lap or keyboard of the laptop, there does
not exist a static mount-matrix to describe it properly. So no driver can report that correctly.

Therefore, such a device needs a dynamic mount matrix, i.e. there should be a kernel driver that
reads out the lid angle sensor and modifies the mount-matrix of the accelerometer by some sin()/cos()
table.

Well, you can delegate this to the user-space. But IMHO this is wrong layering. Every layer
put on top of the lower layers should abstract away implementation details so that the highest
layer has the most general interface.

In my view we have here this "protocol" stack (casting into the ISO 7-layer model):

	L7: application - user space
	L3: input - to get device orientation information
	L2: iio - to get raw data and mount-matrix
	L1: i2c, spi, usb, hdq, ... - to get bits from/to chips
	L0: chips, ...

My RFC mainly mangles the raw data reported from the iio level and the mount matrix into
device orientation information. I.e. it is a proposal for L3 implementation.

So fixing an issue of L2 (dynamic mount matrix for lid angle) in the user-space layer would be improper
layering.

So there are two possibilites:
a) make the mount-matrix dynamical as described above in L2
b) extend my RFC to handle this special case


> One oddity to note here is that until very recently we deliberately didn't register
> certain ACPI IDs because they confused userspace by reporting two accelerometers
> without any info on which was in the lid.  Thankfully proper handling of that
> is no being sorted.  It's still mostly a case of just deliberately ignoring one
> of the sensors.
> 
>> 
>>> 
>>>> 
>>>> And I think is even less flexible than "always doing it". Let me explain this claim.
>>>> 
>>>> For me, the kernel should present everything the hardware supports to user-space
>>>> in better digestable device files or APIs (without making any assumptions about the
>>>> user-space code).  
>>> 
>>> Agreed, we just have a different view on how this should be done. I want
>>> it to be dynamic and extremely flexible, you want the easy way of just
>>> putting a fixed set out all the time.
>>> 
>>>> 
>>>> Then, every user-space that will be installed can find out what the hardware supports
>>>> by looking at standard places.
>>>> 
>>>> E.g. it can scan for all mice and keyboards. And for all input accelerometers.  
>>> 
>>> Or, you an have the correct 'fairly trivial' userspace setup to scan for all
>>> registered accelerometers and 'on demand' create the bindings to bring them up as
>>> Input accelerometers if that is what makes sense for your platform.  
>> 
>> Why not scan for input accelerometers and leave it as an implementation detail that
>> the kernel does serve the physical chips through the iio infrastructure?
> 
> If we could separate the IIO front end from the IIO backend I would agree that
> would be another valid -userspace- policy.
> 
>> 
>> IMHO some user-spaces may already be scanning all */input/event* and check for
>> the device property INPUT_PROP_ACCELEROMETER.
>> 
>> This is a discussion mainly about proper encapsulation of lower level differences.
>> 
>>> 
>>>> 
>>>> If the kernel is hiding some chips and needs some initial user-space action before
>>>> presenting them all, this requires that the user-space has some a-priori knowledge
>>>> about which specific devices it should ask for.  
>>> 
>>> No more that it needs to know which accelerometer to use?  
>> 
>>> 
>>>> So it does not really need to scan
>>>> for them. Because it must already know. Obviously in some mapping table stored at
>>>> a well known location inside the rootfs image.  
>>> 
>>> No. Let me give some more details of how this would work.  It's really just
>>> a more flexible version of what you have.
>>> 
>>> A distro, or individual user decides to put the relevant script in place for the
>>> following:
>>> 
>>> 1. Userspace detects a new accelerometer driver, via the standard methods (uevent)
>>> 2. Userspace looks to see if it has the required properties. Now this includes things
>>> like detecting that it is the accelerometer in the lid of a laptop - if so do not
>>> register it as an input device.  If it's in the keyboard then do register it.
>>> 3. Userspace script then creates the files in configfs
>>> /sys/kernel/config/iio/maps/
>>> (this interface needs appropriate definition)
>>> Maybe...
>>> /sys/kernel/config/iio/maps/iio_input/iio_device:X/accel_x, accel_y, etc
>>> When done it writes to the bind file
>>> /sys/kernel/config/iio/maps/iio_input/iio_device:X/bind
>>> which instantiates the input driver.
>>> 
>>> This moves all of the policy decision into userspace, where it belongs.  If
>>> we want to enable a particular accelerometer on a particular board because it
>>> actually works better than the one the default policy says to use, then we can
>>> do so.
>>> 
>>> The resulting infrastructure is much more general, because it lets us do the
>>> same for any IIO consumer.  This input bridge is not a special case. It works
>>> equally well for the existing hwmon bridge any would even let us do things
>>> like provide the information from userspace that we have an analog accelerometer
>>> wired up to an ADC on some hacker board.  
>> 
>> Ok, understood.
>> 
>> My approach triggers input uevents:
>> 
>> 1. kernel detects a new iio accelerometer (looks like an analog accelerometer should be
>>   the DTS child of an iio adc and then iio should create an accelerometer and not a voltage
>>   channel)
> 
> Yes ultimately it would be a child device that would be it's own IIO device. We
> already have this for some gyroscopes.
> 
>> 2. iio-bridge registers as input event
>> 3. this triggers an uevent
>> 4  an udev-rule can detect the properties and map it to some "speaking" name like
>>   /dev/input/main-accelerometer, /dev/input/lid-accelerometer etc. Or if the
>>   accelerometer is to be ignored, it does not get a "speaking" name at all.
>> 
>> The required udev rules are stored in user space and are of course user-space and application
>> specific. But this does not require to invent some new configfs stuff and special scripts
>> in user-space. Just install some udev rule at a well established location in file-system.
> 
> I'm not sure there is any significant difference between you creating a mapping like
> this an udev rule that creates the whole mapping.  Bit more to do perhaps but it's
> nothing particularly special that I can see.  Sure there is new kernel support to be
> done.
> 
>> 
>> Yes, this does not cover arbitrary mappings. But what are arbitrary mappings good
>> for? Your scheme seems to be able to map a light sensor to accelerometer input.
>> Does this "full matrix of everything is possible" really make sense?
> 
> From a generic interface point of view - yes it absolutely does.
> 
> We define an interface that covers all usecases rather than a whole set of
> separate ones that cover individual corner cases.  That way we don't have to
> keep defining new interfaces.
> 
> The individual drivers can easily do validation of what they are provided with.
> 
>> 
>> I can't decide because I have no need for it. Others may have.
>> 
>> But another thought: does it interfere with this input-bridge? Probably no. You can
>> still add your configfs approach for general iio devices to e.g. hwmon mappings. Even
>> as an alternate method of creating input devices (enabled only if my input-bridge is
>> disabled).
> 
> Yes see above.  Both approaches meet your requirement (I think anyway).
> I do not want to see two long term solutions to the same problem.
> 
> I'm interested in a long term sustainable solution so I want to see
> the generic one.
> 
>> 
>>> 
>>> 
>>>> 
>>>> This seems to make it impossible to develop a generic distro rootfs image - without
>>>> asking the user for manual configuration. And that where the kernel already knows
>>>> this (which iio accelerometers do exist for a specific piece of hardware).
>>>> 
>>>> This is why I believe a mechanism to instantiate only on demand isn't adding but
>>>> removing flexibility because it prevents copying a rootfs from one device to another.  
>>> 
>>> I disagree, see above.
>>> 
>>>> 
>>>>> 
>>>>> As I mentioned in V1, look at the possibility of a configfs based method
>>>>> to build the map.  It's easy for userspace to work out what makes sense to
>>>>> map in principle.  There may be some missing info that we also need to
>>>>> look to expose.    
>>>> 
>>>> With a "may be missing" it is impossible to write code for it...
>>>> Can you please name which information is missing on the input accelerometer
>>>> API?  
>>> 
>>> See above. It's not the input accelerometer ABI, it's the missing ability
>>> to instantiate IIO maps from user space.
>>> 
>>>> 
>>>>> 
>>>>> In general, userspace created channel maps would be very useful for
>>>>> other things such as maker type boards where they can plug all sorts
>>>>> of odd things into ADC channels for example.    
>>>> 
>>>> Ok, I understand, but this is a different problem where this iio-input-bridge is not
>>>> intended to be a solution. Generic ADCs are not input devices. Like SD cards are not
>>>> keyboards.
>>>> 
>>>> So we should not try to mix the idea of general mapping with this input-bridge for
>>>> input accelerometers.  
>>> Yes we should. You are proposing a solution that is a subset of the larger
>>> problem set.  
>> 
>> Yes, of course. Because I did not see or know about the general problem set.
>> And I still don't see a need for user-space controlled mapping for input-accelerometers.
> 
> We are clearly going to differ on this.  Bastien gave one example for why
> this is required.  There will be others.
> 
>> 
>>> Why introduce a stop gap like this when we can do it correctly
>>> and provide something useful for all those other use cases.
>>> 
>>> The only difference here is the uevent triggered script that creates those maps
>>> for your particular usecase.  
>> 
>> Well, I am a friend of solving one problem after the other in smaller steps than
>> immediately aiming at a very general solution, which has side-effects of inventing
>> new stuff for things that would work without.
> 
> That works in a world where you can drop the previous approach as part of your
> generalization.  When you are playing with kernel / userspace ABI then it
> doesn't. Ideally you have to figure out the extensible general solution at the
> start because you are stuck maintaining the 'small steps' for many years to
> come.  I don't want to perpetually 'have' to export all 3D accelerometers as
> input devices, because we didn't have the ability to chose which should be
> exported at some point in the past.
> 
>> 
>>> 
>>> 
>>>> 
>>>> BTW, there is a way to define additional mapping using udev rules which symlink the
>>>> /dev/input/event* paths to stable names like /dev/input/accelerometer.
>>>> 
>>>> This comes without additional code and is already provided by udev and the input system.
>>>> 
>>>> So in summary, I have not yet seen a convincing scenario where being able to dynamically
>>>> map iio channels to input devices seems beneficial.  
>>> 
>>> That is true for the narrow case you are talking about. I don't want to see that
>>> narrow case solved in a fashion that effectively breaks solving it properly.  
>> 
>> How does it break your approach if added later? The more I think about it they are
>> not incompatible. It is just useless to apply both in parallel.
> 
> The reality is that if we put one in first that will used for ever because there
> will be devices out there using it.  Therefore we have to maintain both for
> ever.
> 
>> 
>>> If we add this, we have to export all accelerometers for ever under all circumstances
>>> to userspace, because to remove it will break existing userspace.
>>> 
>>> If we stand back and work out if we can do the general solution now, we avoid
>>> this problem.  
>> 
>> We get a different problem that we break existing user-space that simply wants to see
>> an /dev/input/accelerometer without doing more than an existing udev rule.
> 
> I would love to say such a userspace doesn't exist, but reality is there are
> all sorts of hideous things out there.  There are cases that deal with this
> as an option of course (such as Bastien's sensor-proxy)
> 
> The number of devices that are supported under mainline as input accelerometers
> is pretty small.  It's not a perfect world unfortunately but having to add a
> small udev script is at least not a major break if we do cause it.
>> 
>>> 
>>>> 
>>>>> 
>>>>>> 
>>>>>> This driver simply collects the first 3 accelerometer channels as X, Y and Z.
>>>>>> If only 1 or 2 channels are available, they are used for X and Y only. Additional
>>>>>> channels are ignored.
>>>>>> 
>>>>>> Scaling is done automatically so that 1g is represented by value 256 and
>>>>>> range is assumed to be -511 .. +511 which gives a reasonable precision as an
>>>>>> input device.    
>>>>> 
>>>>> Why do we do this, rather than letting input deal with it?  Input is used
>>>>> to widely differing scales IIRC    
>>>> 
>>>> Well, it can't be done differently... And what I call scale here is nothing more than
>>>> defining ABSMIN_ACC_VAL and ABSMAX_ACC_VAL.
>>>> 
>>>> We need to apply some scale since iio reports in (fractional) units of 1g, i.e. values
>>>> of magnitude 1.  
>>> 
>>> m/s^2 not g, but doesn't matter for the point of view of this discussion.  
>> 
>> My fault. The driver takes care of this in the scaling formula so that "input" reports
>> MAX/2 for 1g.
>> 
>>> 
>>>> These are not adaequate for input events which use integers. So we must
>>>> define some factor for iio_convert_raw_to_processed() to scale from raw value range
>>>> to int value range. We could report raw values but this would be an improper abstraction
>>>> from chip specific differences.  
>>> 
>>> Hmm. I can see we perhaps need some mapping, but is there a concept of standard scale
>>> for existing input accelerometers?  How is this done to give for other input devices
>>> such as touch screens?  I'd expect to see a separation between scale, and range.
>>> 
>>> 
>>>> 
>>>> BTW: the range (and therefore the factor) is reported through the evdev driver to user-space
>>>> (evtest reports Min and Max as you can see in the example).
>>>> 
>>>> The most important thing is that this is a hardware independent definition. Every accelerometer
>>>> chip will report this range. So you can easily upgrade hardware or switch accelerometers
>>>> without touching user-space calibration. Like you can replace ethernet controller chips but
>>>> networking works the same with all of them.  
>>> 
>>> Agreed, it needs to be hardware independent by the time it hits userspace, but I would
>>> have thought that scaling would be done in input, rather than IIO. It's hardly
>>> a problem unique to our usecase!
>>> 
>>> Perhaps Dmitry can give some advice on this.  
>> 
>> Yes, that would be helpful.
>> 
>>> 
>>>> 
>>>> 
>>>> Hm. Is there an alternative to attach such private data to an struct iio_dev
>>>> allocated by someone else? I have not found one yet.
>>>> 
>>>> Or can I add some void *input_mapping; to struct iio_dev? Depending on
>>>> #if defined(CONFIG_IIO_INPUT_BRIDGE)?  
>>> 
>>> Yes, add a new element.  
>> 
>> Ok, works fine.
>> 
>> I already have found one case of iio accelerometer driver where it did make a problem
>> not using a special element.
>> 
>>>>> 
>>>>> iio_input_find_accel_channel(indio_dev, chan, &numchans);
>>>>> iio_input_register_device(indio_dev, chan, numchans);    
>>>> 
>>>> Well, that looks like it needs some temporary storage of dynamic size
>>>> and loop twice over channels for no functional benefit.  
>>> 
>>> Use fixed size. The worst that happens is we end up with it being
>>> an entry larger that it needs to be.
>>> 
>>>> And handle the
>>>> special case of numchans == 0 (the proposed code simply does not call
>>>> iio_input_register_accel_channel and does not register anything).
>>>> 
>>>> So I'd prefer to follow the "KISS" principle and register single channels
>>>> instead of a set of channels.  
>>> 
>>> Well we disagree on this.  A singleton approach like used here
>>> is to my mind not KISS.  I would rather see what is there then
>>> act as two simple steps, rather than interleave two different
>>> actions with a totally different path for the first channel found.
>>> If there is only one channel you just built a load of infrastructure
>>> that makes no sense.  If you scan first then you can know that
>>> before building anything.  
>> 
>> Ok, this is more a matter of taste and resource requirements can probably
>> be neglected. I'll update the driver.
>> 
>> So in summary, I'll post a v3 that fixes some bugs of v2 (because we need
>> them fixed for our production systems as well).
>> 
>> Then it is up to you if you want to take this approach or want to write
>> a full version following your concept. Or if it is possible as I assume, we
>> can have both.
> 
> Thanks. I think we need at some code for what I was proposing to discuss
> much further. Unfortunately it may be a little while before I get time to
> work on that.  Hopefully not too long though!
> 
> Jonathan
> 
>> 
>> BR and thanks,
>> Nikolaus
>> 
> 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Letux-kernel] [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-05-09  9:09           ` H. Nikolaus Schaller
@ 2019-05-09 17:02             ` " H. Nikolaus Schaller
  2019-05-11 11:05               ` Jonathan Cameron
  2019-05-11 10:54             ` Jonathan Cameron
  1 sibling, 1 reply; 16+ messages in thread
From: H. Nikolaus Schaller @ 2019-05-09 17:02 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-input, Dmitry Torokhov, Eric Piel, linux-iio, kernel, lkml,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Hartmut Knaack,
	Discussions about the Letux Kernel


> Am 09.05.2019 um 11:09 schrieb H. Nikolaus Schaller <hns@goldelico.com>:
> 
> Hi Jonathan,
>> 
>> 
>> And how does that work on the common case of a sensor in the lid of a laptop?
>> how do you know what angle the screen is at?  
> 
> Well, I am not aware of laptops where the sensor is in the lid because I am in the handhelds
> business, but let's assume it is common.
> 
> I realized that if the sensor orientation is related to the lid position, while the reference
> frame reported to user space is to be referenced to the lap or keyboard of the laptop, there does
> not exist a static mount-matrix to describe it properly. So no driver can report that correctly.
> 
> Therefore, such a device needs a dynamic mount matrix, i.e. there should be a kernel driver that
> reads out the lid angle sensor and modifies the mount-matrix of the accelerometer by some sin()/cos()
> table.

One more thought on this topic.

My answer to the question "how do you know what angle the screen is at?" by requiring an ADC to
measure some potentiometer in the hinge to make the mount matrix dynamic is probably completely
wrong...

If we take the definition for the mount matrix, it defines a specific g-vector pointing to
center of earth if the user is holding the device in a specific position and looking on the display
or the keyboard.

So far the description assumes that there is a single accelerometer and display and keys of a phone
are in a single plane, i.e. there is no angle and everything is fine.

Now if we simply take the two accelerometers separately, one z-axis is going through the keyboard
and the other through the display. Which means if the mount matrices are well defined, the accelerometers
should report almost the same values if the display is fully opened by 180 degrees, i.e. the display
is sitting flat on the table. This is what my RFC does by autoscaling. The values differ only
by noise.

Now what about measuring the lid angle? Well, it is already measured by both accelerometers! If they
do not agree, the angle can be calculated by some arctan() based on y and z axis reports...

If you close the lid, the display is turned upside down and y and z axes reverse sign.

So there remains only the issue that user-space must know which sensor device file is which sensor
and can do the calculation of the lid angle. This is possible because the iio accelerometer name
is available through the input event ioctls.

In summary this case also does not need policy or configuration. Just user space using the information
that is already presented.

BR,
Nikolaus




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-04-14 16:26       ` Roderick Colenbrander
@ 2019-05-10  8:57         ` Bastien Nocera
  2019-05-11 18:47           ` Roderick Colenbrander
  0 siblings, 1 reply; 16+ messages in thread
From: Bastien Nocera @ 2019-05-10  8:57 UTC (permalink / raw)
  To: Roderick Colenbrander, Jonathan Cameron
  Cc: H. Nikolaus Schaller, Dmitry Torokhov, Eric Piel, linux-input,
	letux-kernel, kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, lkml, linux-iio

On Sun, 2019-04-14 at 09:26 -0700, Roderick Colenbrander wrote:
> 
<snip>
> We at the time were one of the first to expose acceleration and gyro
> data through /dev/input for DualShock 4 as supported by hid-sony. We
> report acceleration in 'g' and angular velocity in 'degree / s'. We
> set the resolution to respectively '1 g' and '1 degree / s'. The range
> we set to the range of the device e.g. for DS4 -4g to +4g for
> acceleration. I need to check though what coordinate system we use,
> but I think it is right handed (gyro counter clockwise relative to
> acceleration axes).

How do you export the gyro information through the input device?

FWIW, we needed to do extra work in iio-sensor-proxy so that the
accelerometer in the Sixaxis/DS4 joypads (and uDraw tablet) didn't
appear as though they were accelerometer for the system:
https://github.com/hadess/iio-sensor-proxy/commit/401d59e54b3123860180d4401e09df8a1e1bc6c3

> The two other drivers using INPUT_PROC_ACCELEROMETER are hid-wacom and
> hid-udraw-ps3 Wacom. Both seem to report resolution in 'g'  as well.

I wrote hid-udraw-ps3, and it's reporting accelerometer data through
input because the rest of the driver is input, and it didn't make much
sense to use another subsystem for just that small portion of the
events the device sends out.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-04-22 14:20         ` Jonathan Cameron
  2019-05-09  9:09           ` H. Nikolaus Schaller
@ 2019-05-10  8:57           ` Bastien Nocera
  2019-05-10  9:33             ` H. Nikolaus Schaller
  1 sibling, 1 reply; 16+ messages in thread
From: Bastien Nocera @ 2019-05-10  8:57 UTC (permalink / raw)
  To: Jonathan Cameron, H. Nikolaus Schaller
  Cc: Dmitry Torokhov, Eric Piel, linux-input, letux-kernel, kernel,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-kernel, linux-iio

On Mon, 2019-04-22 at 15:20 +0100, Jonathan Cameron wrote:
> > Different goals usually lead to different solution architectures.
> 
> Indeed, but in this case we have your proposal which is a subset of
> what
> I am suggesting.  One architecture can fulfil both requirements.
> 
> I'll leave it for the other thread, but Bastien has raised the case
> (that I'd forgotten) that there already userspace stacks that are
> capable of happily taking in both IIO and Input devices.  The
> confusion
> here is they will now discover 'both' without the existing userspace
> knowing that they are the same device.  We need to be very careful
> not
> to break those userspace programs.
> 
> So this is an illustration of why the simplistic case doesn't work
> 'now'.

I don't know what state the whole patch is, but, at the very least, I'd
expect that patch to export the fact that it's exporting synthesised
data from another driver, so that it can be easily ignored in user-
space that already supports IIO devices.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-05-10  8:57           ` Bastien Nocera
@ 2019-05-10  9:33             ` H. Nikolaus Schaller
  2019-05-10  9:35               ` Bastien Nocera
  0 siblings, 1 reply; 16+ messages in thread
From: H. Nikolaus Schaller @ 2019-05-10  9:33 UTC (permalink / raw)
  To: Bastien Nocera
  Cc: Jonathan Cameron, Dmitry Torokhov, Eric Piel, linux-input,
	letux-kernel, kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-kernel, linux-iio


> Am 10.05.2019 um 10:57 schrieb Bastien Nocera <hadess@hadess.net>:
> 
> On Mon, 2019-04-22 at 15:20 +0100, Jonathan Cameron wrote:
>>> Different goals usually lead to different solution architectures.
>> 
>> Indeed, but in this case we have your proposal which is a subset of
>> what
>> I am suggesting.  One architecture can fulfil both requirements.
>> 
>> I'll leave it for the other thread, but Bastien has raised the case
>> (that I'd forgotten) that there already userspace stacks that are
>> capable of happily taking in both IIO and Input devices.  The
>> confusion
>> here is they will now discover 'both' without the existing userspace
>> knowing that they are the same device.  We need to be very careful
>> not
>> to break those userspace programs.
>> 
>> So this is an illustration of why the simplistic case doesn't work
>> 'now'.
> 
> I don't know what state the whole patch is, but, at the very least, I'd
> expect that patch to export the fact that it's exporting synthesised
> data from another driver, so that it can be easily ignored in user-
> space that already supports IIO devices.
> 

It does through "Input device name:" starting with "iio-bridge:" as you can see in the commit message of [RFC v3]:

root@letux:~# evtest /dev/input/event5 | head -19
Input driver version is 1.0.1
Input device ID: bus 0x0 vendor 0x0 product 0x0 version 0x0
Input device name: "iio-bridge: bmc150_accel"
Supported events:
  Event type 0 (EV_SYN)
  Event type 3 (EV_ABS)
    Event code 0 (ABS_X)
      Value      8
      Min     -511
      Max      511
    Event code 1 (ABS_Y)
      Value    -44
      Min     -511
      Max      511
    Event code 2 (ABS_Z)
      Value   -265
      Min     -511
      Max      511




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-05-10  9:33             ` H. Nikolaus Schaller
@ 2019-05-10  9:35               ` Bastien Nocera
  2019-05-10 10:06                 ` H. Nikolaus Schaller
  0 siblings, 1 reply; 16+ messages in thread
From: Bastien Nocera @ 2019-05-10  9:35 UTC (permalink / raw)
  To: H. Nikolaus Schaller
  Cc: Jonathan Cameron, Dmitry Torokhov, Eric Piel, linux-input,
	letux-kernel, kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-kernel, linux-iio

On Fri, 2019-05-10 at 11:33 +0200, H. Nikolaus Schaller wrote:
> > 
<snip>
> It does through "Input device name:" starting with "iio-bridge:" as
> you can see in the commit message of [RFC v3]:

This makes it ABI, right?

Big fat warnings around the code that declares it would be appreciated.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-05-10  9:35               ` Bastien Nocera
@ 2019-05-10 10:06                 ` H. Nikolaus Schaller
  0 siblings, 0 replies; 16+ messages in thread
From: H. Nikolaus Schaller @ 2019-05-10 10:06 UTC (permalink / raw)
  To: Bastien Nocera
  Cc: Jonathan Cameron, Dmitry Torokhov, Eric Piel, linux-input,
	letux-kernel, kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-kernel, linux-iio


> Am 10.05.2019 um 11:35 schrieb Bastien Nocera <hadess@hadess.net>:
> 
> On Fri, 2019-05-10 at 11:33 +0200, H. Nikolaus Schaller wrote:
>>> 
> <snip>
>> It does through "Input device name:" starting with "iio-bridge:" as
>> you can see in the commit message of [RFC v3]:
> 
> This makes it ABI, right?

The "Input device name:" is already ABI of the input system (although I guess the string is
built into the evtest tool). I think there are also /sys nodes which carry the same information.

But yes, if someone changes the "iio-bridge:" prefix in kernel code it breaks a user space lib
making use of it:

+	poll_dev->input->name = kasprintf(GFP_KERNEL, "iio-bridge: %s",
+						    indio_dev->name);
+	poll_dev->input->phys = kasprintf(GFP_KERNEL, "iio:device%d",
+						    indio_dev->id);

This type of exporting names seems to be quite common. E.g. "mmcblk0p1" which may end up
in some /etc/fstab.

> 
> Big fat warnings around the code that declares it would be appreciated.

Ok.




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-05-09  9:09           ` H. Nikolaus Schaller
  2019-05-09 17:02             ` [Letux-kernel] " H. Nikolaus Schaller
@ 2019-05-11 10:54             ` Jonathan Cameron
  1 sibling, 0 replies; 16+ messages in thread
From: Jonathan Cameron @ 2019-05-11 10:54 UTC (permalink / raw)
  To: H. Nikolaus Schaller
  Cc: Dmitry Torokhov, Eric Piel, linux-input, letux-kernel, kernel,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-kernel, linux-iio

On Thu, 9 May 2019 11:09:26 +0200
"H. Nikolaus Schaller" <hns@goldelico.com> wrote:

> Hi Jonathan,
> sorry for the delayed response, but I have to focus on other topics now.
No worries. I have the same 'problem'!  Hence nothing yet from my side
on an alternative.

> 
> Just some small comments came to my mind to finish the discussion, mainly on the accelerometer in the lid case (see inline below).
> 
> BR and thanks,
> Nikolaus
> 
> 
> > Am 22.04.2019 um 16:20 schrieb Jonathan Cameron <jic23@kernel.org>:
> > 
> > On Mon, 15 Apr 2019 23:04:15 +0200
> > H. Nikolaus Schaller <hns@goldelico.com> wrote:
> >   
> >> Hi Jonathan,
> >> we seem to have opposite goals and therefore quite different ideas what the right
> >> solution is.  
> > 
> > Yup :)  Yikes this email has gotten complex.  Hopefully I haven't
> > lost track of the various points below.
> >   
> >> 
> >> I come from user-space and the input side, find that input can handle abstract "acceleration input",
> >> and some older chip drivers even provide that as their only interface. Therefore I want
> >> to provide "acceleration input" in these cases where only iio capable drivers exist by
> >> using the existing in-kernel-iio infrastructure. Nothing more.
> >> 
> >> You seem to come from the iio architecture and want to extend it to other APIs as
> >> general as possible and input is just one of several of them.  
> > 
> > Yes, my target is to produce a subsystem that meets many (all would be nice)
> > requirements, including yours.  Whilst I'm happy to debate this for ever, I'm not
> > sure we are making any substantial progress.  As you mention below we
> > probably need to 'see the code' to drive the discussion forwards.
> >   
> >> 
> >> Different goals usually lead to different solution architectures.  
> > 
> > Indeed, but in this case we have your proposal which is a subset of what
> > I am suggesting.  One architecture can fulfil both requirements.  
> 
> Not exactly. Yours always needs configuration in every case. My RFC works without
> user-space config support for the most common cases. This user-space config must
> be maintained and spread over all distributions. So we can never be sure that
> if a user changes the distro that it still works.

Indeed, we disagree on this and will continue to do so.  My view
is that you will always need userspace policy anyway so might as
well apply the 'right' policy :)

> 
> > 
> > I'll leave it for the other thread, but Bastien has raised the case
> > (that I'd forgotten) that there already userspace stacks that are
> > capable of happily taking in both IIO and Input devices.  The confusion
> > here is they will now discover 'both' without the existing userspace
> > knowing that they are the same device.  We need to be very careful not
> > to break those userspace programs.
> > 
> > So this is an illustration of why the simplistic case doesn't work
> > 'now'.
> >   
> >>   
> >>> Am 14.04.2019 um 13:40 schrieb Jonathan Cameron <jic23@kernel.org>:
> >>> 
> >>> On Mon, 8 Apr 2019 15:15:56 +0200
> >>> H. Nikolaus Schaller <hns@goldelico.com> wrote:
> >>>   
> >>>> Hi Jonathan,
> >>>> 
> >>>> I still do not fully understand what is worrying you here.    
> >>>   
> >>>> 
> >>>> Do you worry about functionality, flexibility or resources or something else?    
> >>> 
> >>> Two main things:
> >>> 1) Lack of generality of the approach. 
> >>>  This is a single use trick for input devices. Why does it make sense for
> >>>  input devices?    
> >> 
> >> No, it is not a trick...  
> > Bad choice of words. Sorry about that.
> > 
> > Any time we register a device as two types of device it is less than ideal.
> > 
> > If we had the true separation of IIO front end and back end then it
> > would be perfectly acceptable to 'only' have an input front end for
> > a given device.  That choice would still, in this sort of usecase,
> > have to be made from userspace. It's policy, not design.  If there are reasons
> > a particular device 'is input' then that mapping should be in DT or similar.
> > It's no different from knowing an ADC channel is wired to an analog
> > temperature sensor.  For example, you could build a joystick with
> > an accelerometer in the stick - then the usecase would be obvious!
> > Hence I would also argue that any dynamic interface should also support
> > a static path (DT or equivalent) for the cases where is really a
> > physical characteristic of the system!  Perhaps the Sony parts
> > fall into this category as well.
> > 
> > For a bit of historical background, there was a concerted effort
> > to produced a userspace stack for IIO for android.  
> > https://01.org/android-iio-sensors-hal
> > 
> > Unfortunately I think it died as a result of other moves in Intel on
> > one of their periodic shifts in direction.
> >   
> >> 
> >> Why does it make sense for input devices?  
> > 
> > These are not just 'input' devices. They are accelerometers. One usecase
> > is to use them for input.  The exact same physical device is used for games
> > input uses and counting steps for example (actually a lot wider cases than
> > that, but this one is common in android devices).
> > 
> > Keep that in mind at all times. There are lots of usecases.
> > So we need a solution that does not result in problems for those
> > usecases.  We are not writing a subsytem targetting android use of
> > accelerometers. We are writing a subsystem addressing as many usecases
> > as we can of those devices.
> > 
> > Note that the original reason for IIO was to generalize a whole set of
> > proposed individual subsystems targeting particular sensor types. So
> > that is our focus - solutions that work for everyone.  This isn't
> > totally dissimilar from those discussions - at the time I wanted
> > to do a small focused ALS subsystem and got a resounding "no" from
> > Linus.  Generality matters a lot for the long term.
> >   
> >> 
> >> a) because there is no alternative for accelerometer input devices and there are some
> >> (older) accelerometer chips which only present themselves as input devices as soon
> >> as the driver is compiled and the chip is found.  
> > 
> > Actually that is not accurate.  The vast majority of those older devices
> > that have had any attempt at mainlining are in IIO. AFAIK no accelerometer
> > driver has been merged to mainline input for many years. This is because,
> > amongst others, Dmitry has been of the view they didn't belong there for
> > a very long time.
> >   
> >> 
> >> b) because input events define ACCEL input devices. But no Temperature or Voltage
> >> input or generic ADC input.So there is no generalization of input devices beyond
> >> keyboard, mouse, joystick, accelerometer and some others.  
> > 
> > That's not totally inaccurate, but the distinction in the other sensor cases
> > is that there is a clear 'additional' element that we can map in devicetree
> > which relates the IIO sensor channel to the input device.
> > Doesn't matter for the point of view of this discussion though.
> >   
> >>   
> >>> There are lots of other in kernel users and potential
> >>>  ones in the future.    
> >> 
> >> This bridge addition does not exclude any (additional) in-kernel use.  
> > 
> > No but it creates several problems:
> > 
> > 1. Two ways to do the same thing. 
> > 2. Two sets of code to maintain.
> > 3. Confusion over what is the best way of doing it.
> > 4. The known issues with multiple consumers (note my solution has
> > that problem as well!)
> > 
> > My job here is to maintain the code, which is why I push back on something
> > that makes that job harder.
> > 
> > When the next usecases comes along and someone says they want to map
> > all ADC channels to hwmon channels because that is the subystem that
> > they expect to measure voltages in, then I don't have a good argument
> > to stop them doing the same thing you have.
> > 
> > As a side note we have in the past had input drivers for gyroscopes and
> > magnetometers.  Why are accelerometers special?
> > 
> > I really don't see why we should treat accelerometers differently  
> 
> Not special. I just did not want to add them yet in the RFC phase.
> The principle is the same.

hmm. I would argue that makes things even worse but there we are.
Now we have input interfaces for a wider set of drivers, many
of which aren't there for input at all.

> 
> > 
> > As this discussion runs on, I am increasingly convinced that there *must*
> > be a userspace policy control over whether an input device is
> > instantiated for a given accelerometer.   Once that is the
> > case then I cannot see a reason to treat it any differently from
> > other channel types.  
> 
> Well, I believe that we should avoid any user-space policy control that
> can be avoided. At least those cases that can live without policy
> control should not need to get one because there are other cases that
> need one.
> 
> This is something we can't solve by discussion, of course.
> 
> The decision is that you are maintainer and I am just proposing an RFC.
> So you have two votes and a veto right...
> 
> But maybe this can be fixed by proper defaults? I don't know.

Agreed, we aren't going to resolve this.  I need to at least find some
time to put an alternative on the table rather than just abstract
discussion.

> 
> >   
> >>   
> >>> The ability to register additional IIO consumers like
> >>>  this is useful, lets make it useful to everyone.    
> >> 
> >> But not everyone and every iio device can and should be mappable to the "input" abstraction.
> >> This does not make any sense to me.  
> > 
> > Absolutely.  It should not be.  I clearly didn't explain this well.
> > 
> > It should be mapped to a consumer.
> > 
> > One type of consumer is iio-input, another is iio-hmwon etc.
> >   
> >> 
> >> For example, does it make sense to map a temperature sensor to accelerometer input? Or an
> >> accelerometer to hwmon? This seems to be possible of your generalization unless I am missing something here.
> >> If it is, it ignores that iio sensors are already grouped by what physical property they do measure.  
> > 
> > If people want to map crazy channels to crazy sensor outputs, why stop them?
> > (at this level of the interface).
> > 
> > This is a policy question for a userspace script.  Particular consumer drivers
> > could of course perform sanity checking and refuse to do anything if they
> > cannot sensibly use the channels.
> > 
> > Yes, the interface has this flexibility. Which is a good thing!  Take the example
> > of the gyroscope as input I used above.  If we want to add that support
> > in future to your driver (I have no idea if it actually makes sense)
> > then we can - without having to change the interface.
> >   
> >>   
> >>> 
> >>> 2) To much generality of the specific usecase.  I don't want to put an Input
> >>>  interface on accelerometers where it makes no sense.    
> >> 
> >> I think you can just ignore the input interfaces in that case, if it was created.  
> > 
> > Bastien raised a case where this isn't true.
> >   
> >>   
> >>> The rule of it has
> >>>  2-3 axis so it must make sense isn't good enough to my mind.  How
> >>>  does userspace know which accelerometer to use (more and more devices have
> >>>  multiple?)    
> >> 
> >> In our approach user-space can make it known by udev rules based on /dev/input/event*
> >> (not on iio but the input created for accelerometers). I think I mentioned that. This
> >> comes for free for any device registering as input. So it is no additional code.  
> > 
> > Sorry, I'm lost.  What in there tells you to use 'this' interface rather than one
> > of the other N that were registered?  I'm not sure what information you
> > have available there.
> >   
> >>   
> >>> You could do something like looking at the location info from
> >>>  DT / ACPI in your driver and pick the 'best' but that's policy. Should be
> >>>  in userspace.  Sure you can just use the right input driver, but the moment
> >>>  we do that, we need aware userspace, if that's the case why not make it
> >>>  aware from the start.
> >>> 
> >>> Believe me I've been round this one a good few times and thought about it
> >>> a lot.    
> >> 
> >> That is fine and why we should discuss all the different aspects we have collected.
> >>   
> >>> I'll take a lot of convincing that this isn't a problem that
> >>> should be pushed into userspace.
> >>>   
> >>>> 
> >>>> I think having them mapped always does not need much resources (except a handful of bytes
> >>>> in memory and some µs during probing) unless the event device is opened and really used.
> >>>> Only then it starts e.g. I2C traffic to read the sensors.    
> >>> 
> >>> The bytes don't really mater.    
> >> 
> >> Ok, good to know.
> >>   
> >>> The userspace ABI additions do.    
> >> 
> >> There are only new /dev/input/event devices with well defined ABI. This approach does
> >> not invent anything new here, hence there are no ABI additions which we can break.  
> > 
> > But it does - we aren't talking general ABI, but ABI on specific
> > devices.  Sure, Android doesn't care - though you'd be amazed how much
> > individual android device developers will because we just added another
> > pile of tests to their CI.
> > 
> > An industrial sensor platform absolutely does.  They have to validate
> > those interfaces.  They can't just ignore them because they feel like
> > it because who knows if some future user will use them?
> > 
> > For another case, see Bastien's reply to the later thread.
> > 
> > Instantiating interfaces has testing costs, even when the are standard
> > interfaces.
> >   
> >>   
> >>>   
> >>>> 
> >>>> So it is just some unused file sitting around in /dev. Or 2 or could be even 100.
> >>>> For devices which have no iio accelerometers configured, there will be no /dev/input
> >>>> file. So we are discussing the rare case of devices with more than one or two accelerometers.    
> >>> 
> >>> Well they aren't exactly rare in IIO using systems ;)    
> >> 
> >> This is another thing where our experiences differ. What specific devices are you thinking
> >> of? I am focussed on handhelds where the accelerometer (or two) is a way to do GUI input
> >> depending on device orientation in space.  
> > 
> > Again, you are introducing this interface for everyone. Including lots of
> > 'interesting' usecases.
> > 
> > I have worked with sensor platforms with accelerometers of different parts of humans,
> > We have people do bridge vibration measurement, flying UAVs and tracking the motion
> > of trucks.
> > 
> > Most are not huge numbers of accelerometers per node but don't rule out the
> > possibility.  It's normally limited by length of cables rather than anything
> > else so you used multiple nodes after a while each with their own set of sensors.
> > 
> > There are lots of plaforms out there that use multiple accelerometers in more
> > or less the same place to do very high dynamic range measurement (without losing
> > precision when things are nearly still).
> > 
> > Anyhow, it's not a particularly important point anyway!
> >   
> >>>   
> >>>> 
> >>>> Now, on every system there are many interfaces and files that are not used because it makes
> >>>> no sense to look at them. If I check on one of my systems, I find for example a lot of
> >>>> /dev/tty and only a very small portion is used and generic distros have no issue with it.
> >>>> 
> >>>> There is even /dev/iio:device0 to /dev/iio:device5 representing the raw iio devices.
> >>>> Not all of them are actively used, but they are simply there and can be scanned for.    
> >>> 
> >>> Agreed, in the ideal case we wouldn't have had that either, but we are
> >>> stuck with it.  The long term plan is to allow use of IIO backends without the
> >>> front end being there at all. Lots of SoC ADC users would prefer this. We are
> >>> stuck with the legacy intertwining fo the front end and back end of IIO so
> >>> this isn't as easy to do as I would like.    
> >> 
> >> Ah, ok. I think it is a similar discussion of hiding the serdev /dev/tty* if it is
> >> used for accessing an embedded GPS or Bluetooth chip, for example.
> >> 
> >> But is this needed? I think it is not a problem if there are multiple consumers for
> >> the same iio channel. Some in-kernel, some through /dev/iio:device* and maybe some
> >> through /dev/input (which boils down to in-kernel).  
> > 
> > There are quite a few complexities around multiple consumers that we really haven't
> > solved.  Right now the cases that work are very much restricted.  I'd love
> > to tidy some of these up, but never enough time and all that.
> > 
> > It's not that relevant here, but in short a few of the issues are:
> > 1) Interference over control settings.  - Two consumers need different filter
> >   settings/ sampling frequency / range.  How do we negotiate the choice and
> >   communicate it to the other consumers.  More complex questions such
> >   mediating choices of triggers.
> > 2) One driver is doing polled reads, the other is doing interrupt driven.
> >   Most drivers prevent this combination because the polled reads can lead
> >   to unlimited delays on the interrupt driven path and hence break it.
> > 
> > The main driver for this separation was to present only the 'right' interface
> > to reduced people's validation costs etc.  People really do want to have the
> > option to strip back the userspace inteface.  Obviously these are the rare
> > people who would disable your config option, but the point of this was
> > that we actually would like to make even the IIO interface optional as
> > well but have a fair way to go before we can.
> >   
> >>   
> >>>   
> >>>> 
> >>>> So I do not see a resource problem if every accelerometer /dev/iio:device* gets
> >>>> some companion /dev/input/event* for being used on demand - but only if this bridge
> >>>> is configured at all.    
> >>> 
> >>> That argument does not apply. If we add a config option, distros will enable it.
> >>> So the vast majority of systems will ship with this turned on.  You cannot
> >>> use a config variable to control policy and expect it to be change by anyone
> >>> but a very very small subset of users.  So please drop the 'you can just not
> >>> build it argument'.    
> >> 
> >> This is not my point here. I mention this under the (now known to be wrong) assumption
> >> that resources do care. I just want to state that kernels built for platforms where every
> >> byte counts can be stripped down by disabling it. Others where resources are no concern
> >> simply can map them all, even if not used.  
> > 
> > Agreed. A subset of users will just build without this.
> >   
> >>   
> >>> Userspace configuration changing is a lot easier if people actually care.
> >>> Sure, many distros will ship the same script to everyone.
> >>>   
> >>>>   
> >>>>> I think we need some deliberate userspace interaction to instantiate
> >>>>> one of these rather than 'always doing it'.      
> >>>> 
> >>>> My gut feeling is that this additional user-space interaction needs more resources and
> >>>> adds a lot of complexity, independently of how it is done.    
> >>> 
> >>> Trivial resources and actually fairly trivial complexity.  Key thing is
> >>> it puts the burden on the users of this functionality to configure what they
> >>> want.    
> >> 
> >> Hm. No. My proposal does not need configuration which accelerometers should go where.  
> > 
> > Agreed. I was talking about my proposal here :)
> >   
> >> 
> >> I assumethat input accelerometer users do not want to configure anything, like neither
> >> a mouse or keyboard is to be configured to be useable (yes there are keymaps but that
> >> is impossible to automate).  
> > 
> > The difference is a mouse is only really useful as a mouse and most of the time a keyboard
> > is a used only as a keyboard.  Here that's not true.
> >   
> >> 
> >> They just want to be able to read device orientation in a device-independent scale.
> >> Therefore my approach already takes the mount-matrix into account to hide sensor position
> >> differences.  
> > 
> > And how does that work on the common case of a sensor in the lid of a laptop?
> > how do you know what angle the screen is at?    
> 
> Well, I am not aware of laptops where the sensor is in the lid because I am in the handhelds
> business, but let's assume it is common.
> 
> I realized that if the sensor orientation is related to the lid position, while the reference
> frame reported to user space is to be referenced to the lap or keyboard of the laptop, there does
> not exist a static mount-matrix to describe it properly. So no driver can report that correctly.
> 
> Therefore, such a device needs a dynamic mount matrix, i.e. there should be a kernel driver that
> reads out the lid angle sensor and modifies the mount-matrix of the accelerometer by some sin()/cos()
> table.

This is where it is tricky.  There is no such lid angle sensor, that's what the accelerometer
is for. Typically you have a pair of accelerometers and need to know which is which. which one makes
sense for input use is non obvious.

The reason I raised this at all was to make the point that there is often a need for user
policy anyway. It's not a glorious world of things just magically working.

> 
> Well, you can delegate this to the user-space. But IMHO this is wrong layering. Every layer
> put on top of the lower layers should abstract away implementation details so that the highest
> layer has the most general interface.
> 
> In my view we have here this "protocol" stack (casting into the ISO 7-layer model):
> 
> 	L7: application - user space
> 	L3: input - to get device orientation information
> 	L2: iio - to get raw data and mount-matrix
> 	L1: i2c, spi, usb, hdq, ... - to get bits from/to chips
> 	L0: chips, ...
> 
> My RFC mainly mangles the raw data reported from the iio level and the mount matrix into
> device orientation information. I.e. it is a proposal for L3 implementation.
> 
> So fixing an issue of L2 (dynamic mount matrix for lid angle) in the user-space layer would be improper
> layering.
> 
> So there are two possibilites:
> a) make the mount-matrix dynamical as described above in L2
> b) extend my RFC to handle this special case
I would argue that it becomes just another part of the userspace policy that
needs to be applied.  Sure the actual transform would be applied in kernel
but the decision on which accelerometer to use and if additional transforms
are needed, is one for userspace not the kernel.  Probably not that hard
to add as simple controls alongside a binding interface.

Thanks,

Jonathan

> 
> 
> > One oddity to note here is that until very recently we deliberately didn't register
> > certain ACPI IDs because they confused userspace by reporting two accelerometers
> > without any info on which was in the lid.  Thankfully proper handling of that
> > is no being sorted.  It's still mostly a case of just deliberately ignoring one
> > of the sensors.
> >   
> >>   
> >>>   
> >>>> 
> >>>> And I think is even less flexible than "always doing it". Let me explain this claim.
> >>>> 
> >>>> For me, the kernel should present everything the hardware supports to user-space
> >>>> in better digestable device files or APIs (without making any assumptions about the
> >>>> user-space code).    
> >>> 
> >>> Agreed, we just have a different view on how this should be done. I want
> >>> it to be dynamic and extremely flexible, you want the easy way of just
> >>> putting a fixed set out all the time.
> >>>   
> >>>> 
> >>>> Then, every user-space that will be installed can find out what the hardware supports
> >>>> by looking at standard places.
> >>>> 
> >>>> E.g. it can scan for all mice and keyboards. And for all input accelerometers.    
> >>> 
> >>> Or, you an have the correct 'fairly trivial' userspace setup to scan for all
> >>> registered accelerometers and 'on demand' create the bindings to bring them up as
> >>> Input accelerometers if that is what makes sense for your platform.    
> >> 
> >> Why not scan for input accelerometers and leave it as an implementation detail that
> >> the kernel does serve the physical chips through the iio infrastructure?  
> > 
> > If we could separate the IIO front end from the IIO backend I would agree that
> > would be another valid -userspace- policy.
> >   
> >> 
> >> IMHO some user-spaces may already be scanning all */input/event* and check for
> >> the device property INPUT_PROP_ACCELEROMETER.
> >> 
> >> This is a discussion mainly about proper encapsulation of lower level differences.
> >>   
> >>>   
> >>>> 
> >>>> If the kernel is hiding some chips and needs some initial user-space action before
> >>>> presenting them all, this requires that the user-space has some a-priori knowledge
> >>>> about which specific devices it should ask for.    
> >>> 
> >>> No more that it needs to know which accelerometer to use?    
> >>   
> >>>   
> >>>> So it does not really need to scan
> >>>> for them. Because it must already know. Obviously in some mapping table stored at
> >>>> a well known location inside the rootfs image.    
> >>> 
> >>> No. Let me give some more details of how this would work.  It's really just
> >>> a more flexible version of what you have.
> >>> 
> >>> A distro, or individual user decides to put the relevant script in place for the
> >>> following:
> >>> 
> >>> 1. Userspace detects a new accelerometer driver, via the standard methods (uevent)
> >>> 2. Userspace looks to see if it has the required properties. Now this includes things
> >>> like detecting that it is the accelerometer in the lid of a laptop - if so do not
> >>> register it as an input device.  If it's in the keyboard then do register it.
> >>> 3. Userspace script then creates the files in configfs
> >>> /sys/kernel/config/iio/maps/
> >>> (this interface needs appropriate definition)
> >>> Maybe...
> >>> /sys/kernel/config/iio/maps/iio_input/iio_device:X/accel_x, accel_y, etc
> >>> When done it writes to the bind file
> >>> /sys/kernel/config/iio/maps/iio_input/iio_device:X/bind
> >>> which instantiates the input driver.
> >>> 
> >>> This moves all of the policy decision into userspace, where it belongs.  If
> >>> we want to enable a particular accelerometer on a particular board because it
> >>> actually works better than the one the default policy says to use, then we can
> >>> do so.
> >>> 
> >>> The resulting infrastructure is much more general, because it lets us do the
> >>> same for any IIO consumer.  This input bridge is not a special case. It works
> >>> equally well for the existing hwmon bridge any would even let us do things
> >>> like provide the information from userspace that we have an analog accelerometer
> >>> wired up to an ADC on some hacker board.    
> >> 
> >> Ok, understood.
> >> 
> >> My approach triggers input uevents:
> >> 
> >> 1. kernel detects a new iio accelerometer (looks like an analog accelerometer should be
> >>   the DTS child of an iio adc and then iio should create an accelerometer and not a voltage
> >>   channel)  
> > 
> > Yes ultimately it would be a child device that would be it's own IIO device. We
> > already have this for some gyroscopes.
> >   
> >> 2. iio-bridge registers as input event
> >> 3. this triggers an uevent
> >> 4  an udev-rule can detect the properties and map it to some "speaking" name like
> >>   /dev/input/main-accelerometer, /dev/input/lid-accelerometer etc. Or if the
> >>   accelerometer is to be ignored, it does not get a "speaking" name at all.
> >> 
> >> The required udev rules are stored in user space and are of course user-space and application
> >> specific. But this does not require to invent some new configfs stuff and special scripts
> >> in user-space. Just install some udev rule at a well established location in file-system.  
> > 
> > I'm not sure there is any significant difference between you creating a mapping like
> > this an udev rule that creates the whole mapping.  Bit more to do perhaps but it's
> > nothing particularly special that I can see.  Sure there is new kernel support to be
> > done.
> >   
> >> 
> >> Yes, this does not cover arbitrary mappings. But what are arbitrary mappings good
> >> for? Your scheme seems to be able to map a light sensor to accelerometer input.
> >> Does this "full matrix of everything is possible" really make sense?  
> > 
> > From a generic interface point of view - yes it absolutely does.
> > 
> > We define an interface that covers all usecases rather than a whole set of
> > separate ones that cover individual corner cases.  That way we don't have to
> > keep defining new interfaces.
> > 
> > The individual drivers can easily do validation of what they are provided with.
> >   
> >> 
> >> I can't decide because I have no need for it. Others may have.
> >> 
> >> But another thought: does it interfere with this input-bridge? Probably no. You can
> >> still add your configfs approach for general iio devices to e.g. hwmon mappings. Even
> >> as an alternate method of creating input devices (enabled only if my input-bridge is
> >> disabled).  
> > 
> > Yes see above.  Both approaches meet your requirement (I think anyway).
> > I do not want to see two long term solutions to the same problem.
> > 
> > I'm interested in a long term sustainable solution so I want to see
> > the generic one.
> >   
> >>   
> >>> 
> >>>   
> >>>> 
> >>>> This seems to make it impossible to develop a generic distro rootfs image - without
> >>>> asking the user for manual configuration. And that where the kernel already knows
> >>>> this (which iio accelerometers do exist for a specific piece of hardware).
> >>>> 
> >>>> This is why I believe a mechanism to instantiate only on demand isn't adding but
> >>>> removing flexibility because it prevents copying a rootfs from one device to another.    
> >>> 
> >>> I disagree, see above.
> >>>   
> >>>>   
> >>>>> 
> >>>>> As I mentioned in V1, look at the possibility of a configfs based method
> >>>>> to build the map.  It's easy for userspace to work out what makes sense to
> >>>>> map in principle.  There may be some missing info that we also need to
> >>>>> look to expose.      
> >>>> 
> >>>> With a "may be missing" it is impossible to write code for it...
> >>>> Can you please name which information is missing on the input accelerometer
> >>>> API?    
> >>> 
> >>> See above. It's not the input accelerometer ABI, it's the missing ability
> >>> to instantiate IIO maps from user space.
> >>>   
> >>>>   
> >>>>> 
> >>>>> In general, userspace created channel maps would be very useful for
> >>>>> other things such as maker type boards where they can plug all sorts
> >>>>> of odd things into ADC channels for example.      
> >>>> 
> >>>> Ok, I understand, but this is a different problem where this iio-input-bridge is not
> >>>> intended to be a solution. Generic ADCs are not input devices. Like SD cards are not
> >>>> keyboards.
> >>>> 
> >>>> So we should not try to mix the idea of general mapping with this input-bridge for
> >>>> input accelerometers.    
> >>> Yes we should. You are proposing a solution that is a subset of the larger
> >>> problem set.    
> >> 
> >> Yes, of course. Because I did not see or know about the general problem set.
> >> And I still don't see a need for user-space controlled mapping for input-accelerometers.  
> > 
> > We are clearly going to differ on this.  Bastien gave one example for why
> > this is required.  There will be others.
> >   
> >>   
> >>> Why introduce a stop gap like this when we can do it correctly
> >>> and provide something useful for all those other use cases.
> >>> 
> >>> The only difference here is the uevent triggered script that creates those maps
> >>> for your particular usecase.    
> >> 
> >> Well, I am a friend of solving one problem after the other in smaller steps than
> >> immediately aiming at a very general solution, which has side-effects of inventing
> >> new stuff for things that would work without.  
> > 
> > That works in a world where you can drop the previous approach as part of your
> > generalization.  When you are playing with kernel / userspace ABI then it
> > doesn't. Ideally you have to figure out the extensible general solution at the
> > start because you are stuck maintaining the 'small steps' for many years to
> > come.  I don't want to perpetually 'have' to export all 3D accelerometers as
> > input devices, because we didn't have the ability to chose which should be
> > exported at some point in the past.
> >   
> >>   
> >>> 
> >>>   
> >>>> 
> >>>> BTW, there is a way to define additional mapping using udev rules which symlink the
> >>>> /dev/input/event* paths to stable names like /dev/input/accelerometer.
> >>>> 
> >>>> This comes without additional code and is already provided by udev and the input system.
> >>>> 
> >>>> So in summary, I have not yet seen a convincing scenario where being able to dynamically
> >>>> map iio channels to input devices seems beneficial.    
> >>> 
> >>> That is true for the narrow case you are talking about. I don't want to see that
> >>> narrow case solved in a fashion that effectively breaks solving it properly.    
> >> 
> >> How does it break your approach if added later? The more I think about it they are
> >> not incompatible. It is just useless to apply both in parallel.  
> > 
> > The reality is that if we put one in first that will used for ever because there
> > will be devices out there using it.  Therefore we have to maintain both for
> > ever.
> >   
> >>   
> >>> If we add this, we have to export all accelerometers for ever under all circumstances
> >>> to userspace, because to remove it will break existing userspace.
> >>> 
> >>> If we stand back and work out if we can do the general solution now, we avoid
> >>> this problem.    
> >> 
> >> We get a different problem that we break existing user-space that simply wants to see
> >> an /dev/input/accelerometer without doing more than an existing udev rule.  
> > 
> > I would love to say such a userspace doesn't exist, but reality is there are
> > all sorts of hideous things out there.  There are cases that deal with this
> > as an option of course (such as Bastien's sensor-proxy)
> > 
> > The number of devices that are supported under mainline as input accelerometers
> > is pretty small.  It's not a perfect world unfortunately but having to add a
> > small udev script is at least not a major break if we do cause it.  
> >>   
> >>>   
> >>>>   
> >>>>>   
> >>>>>> 
> >>>>>> This driver simply collects the first 3 accelerometer channels as X, Y and Z.
> >>>>>> If only 1 or 2 channels are available, they are used for X and Y only. Additional
> >>>>>> channels are ignored.
> >>>>>> 
> >>>>>> Scaling is done automatically so that 1g is represented by value 256 and
> >>>>>> range is assumed to be -511 .. +511 which gives a reasonable precision as an
> >>>>>> input device.      
> >>>>> 
> >>>>> Why do we do this, rather than letting input deal with it?  Input is used
> >>>>> to widely differing scales IIRC      
> >>>> 
> >>>> Well, it can't be done differently... And what I call scale here is nothing more than
> >>>> defining ABSMIN_ACC_VAL and ABSMAX_ACC_VAL.
> >>>> 
> >>>> We need to apply some scale since iio reports in (fractional) units of 1g, i.e. values
> >>>> of magnitude 1.    
> >>> 
> >>> m/s^2 not g, but doesn't matter for the point of view of this discussion.    
> >> 
> >> My fault. The driver takes care of this in the scaling formula so that "input" reports
> >> MAX/2 for 1g.
> >>   
> >>>   
> >>>> These are not adaequate for input events which use integers. So we must
> >>>> define some factor for iio_convert_raw_to_processed() to scale from raw value range
> >>>> to int value range. We could report raw values but this would be an improper abstraction
> >>>> from chip specific differences.    
> >>> 
> >>> Hmm. I can see we perhaps need some mapping, but is there a concept of standard scale
> >>> for existing input accelerometers?  How is this done to give for other input devices
> >>> such as touch screens?  I'd expect to see a separation between scale, and range.
> >>> 
> >>>   
> >>>> 
> >>>> BTW: the range (and therefore the factor) is reported through the evdev driver to user-space
> >>>> (evtest reports Min and Max as you can see in the example).
> >>>> 
> >>>> The most important thing is that this is a hardware independent definition. Every accelerometer
> >>>> chip will report this range. So you can easily upgrade hardware or switch accelerometers
> >>>> without touching user-space calibration. Like you can replace ethernet controller chips but
> >>>> networking works the same with all of them.    
> >>> 
> >>> Agreed, it needs to be hardware independent by the time it hits userspace, but I would
> >>> have thought that scaling would be done in input, rather than IIO. It's hardly
> >>> a problem unique to our usecase!
> >>> 
> >>> Perhaps Dmitry can give some advice on this.    
> >> 
> >> Yes, that would be helpful.
> >>   
> >>>   
> >>>> 
> >>>> 
> >>>> Hm. Is there an alternative to attach such private data to an struct iio_dev
> >>>> allocated by someone else? I have not found one yet.
> >>>> 
> >>>> Or can I add some void *input_mapping; to struct iio_dev? Depending on
> >>>> #if defined(CONFIG_IIO_INPUT_BRIDGE)?    
> >>> 
> >>> Yes, add a new element.    
> >> 
> >> Ok, works fine.
> >> 
> >> I already have found one case of iio accelerometer driver where it did make a problem
> >> not using a special element.
> >>   
> >>>>> 
> >>>>> iio_input_find_accel_channel(indio_dev, chan, &numchans);
> >>>>> iio_input_register_device(indio_dev, chan, numchans);      
> >>>> 
> >>>> Well, that looks like it needs some temporary storage of dynamic size
> >>>> and loop twice over channels for no functional benefit.    
> >>> 
> >>> Use fixed size. The worst that happens is we end up with it being
> >>> an entry larger that it needs to be.
> >>>   
> >>>> And handle the
> >>>> special case of numchans == 0 (the proposed code simply does not call
> >>>> iio_input_register_accel_channel and does not register anything).
> >>>> 
> >>>> So I'd prefer to follow the "KISS" principle and register single channels
> >>>> instead of a set of channels.    
> >>> 
> >>> Well we disagree on this.  A singleton approach like used here
> >>> is to my mind not KISS.  I would rather see what is there then
> >>> act as two simple steps, rather than interleave two different
> >>> actions with a totally different path for the first channel found.
> >>> If there is only one channel you just built a load of infrastructure
> >>> that makes no sense.  If you scan first then you can know that
> >>> before building anything.    
> >> 
> >> Ok, this is more a matter of taste and resource requirements can probably
> >> be neglected. I'll update the driver.
> >> 
> >> So in summary, I'll post a v3 that fixes some bugs of v2 (because we need
> >> them fixed for our production systems as well).
> >> 
> >> Then it is up to you if you want to take this approach or want to write
> >> a full version following your concept. Or if it is possible as I assume, we
> >> can have both.  
> > 
> > Thanks. I think we need at some code for what I was proposing to discuss
> > much further. Unfortunately it may be a little while before I get time to
> > work on that.  Hopefully not too long though!
> > 
> > Jonathan
> >   
> >> 
> >> BR and thanks,
> >> Nikolaus
> >>   
> >   
> 
> 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Letux-kernel] [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-05-09 17:02             ` [Letux-kernel] " H. Nikolaus Schaller
@ 2019-05-11 11:05               ` Jonathan Cameron
       [not found]                 ` <CCD87A8D-FF65-4681-964B-22870716D655@goldelico.com>
  0 siblings, 1 reply; 16+ messages in thread
From: Jonathan Cameron @ 2019-05-11 11:05 UTC (permalink / raw)
  To: H. Nikolaus Schaller
  Cc: linux-input, Dmitry Torokhov, Eric Piel, linux-iio, kernel, lkml,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Hartmut Knaack,
	Discussions about the Letux Kernel

On Thu, 9 May 2019 19:02:49 +0200
"H. Nikolaus Schaller" <hns@goldelico.com> wrote:

> > Am 09.05.2019 um 11:09 schrieb H. Nikolaus Schaller <hns@goldelico.com>:
> > 
> > Hi Jonathan,  
> >> 
> >> 
> >> And how does that work on the common case of a sensor in the lid of a laptop?
> >> how do you know what angle the screen is at?    
> > 
> > Well, I am not aware of laptops where the sensor is in the lid because I am in the handhelds
> > business, but let's assume it is common.
> > 
> > I realized that if the sensor orientation is related to the lid position, while the reference
> > frame reported to user space is to be referenced to the lap or keyboard of the laptop, there does
> > not exist a static mount-matrix to describe it properly. So no driver can report that correctly.
> > 
> > Therefore, such a device needs a dynamic mount matrix, i.e. there should be a kernel driver that
> > reads out the lid angle sensor and modifies the mount-matrix of the accelerometer by some sin()/cos()
> > table.  
> 
> One more thought on this topic.
> 
> My answer to the question "how do you know what angle the screen is at?" by requiring an ADC to
> measure some potentiometer in the hinge to make the mount matrix dynamic is probably completely
> wrong...

There are lid angle sensors out independent of this discussion that might work
as you describe but so far they are rare.  There is one under review for
cros_ec for example - how it is implemented, no idea!

> 
> If we take the definition for the mount matrix, it defines a specific g-vector pointing to
> center of earth if the user is holding the device in a specific position and looking on the display
> or the keyboard.
> 
> So far the description assumes that there is a single accelerometer and display and keys of a phone
> are in a single plane, i.e. there is no angle and everything is fine.
> 
> Now if we simply take the two accelerometers separately, one z-axis is going through the keyboard
> and the other through the display. Which means if the mount matrices are well defined, the accelerometers
> should report almost the same values if the display is fully opened by 180 degrees, i.e. the display
> is sitting flat on the table. This is what my RFC does by autoscaling. The values differ only
> by noise.
> 
> Now what about measuring the lid angle? Well, it is already measured by both accelerometers! If they
> do not agree, the angle can be calculated by some arctan() based on y and z axis reports...
Agreed. This is how it is done.
> 
> If you close the lid, the display is turned upside down and y and z axes reverse sign.
> 
> So there remains only the issue that user-space must know which sensor device file is which sensor
> and can do the calculation of the lid angle. This is possible because the iio accelerometer name
> is available through the input event ioctls.
> 
> In summary this case also does not need policy or configuration. Just user space using the information
> that is already presented.

I disagree with that last statement.  If there is a lid angle sensor, policy is
needed to know which of your associated orientation is the base one and which
device indicates the lid angle.

Actually most of the time what you will do is pick one 'correct' sensor under
some configuration of the device and use that.  That is policy.  Yes, you could
bake the policy in to device tree, but then you can also bake in the association
between the underlying IIO sensor and any virtual input sensor.

Anyhow, we still disagree on whether any such virtual input interface
should be a userspace policy decision.  So far I haven't seen any compelling
argument why it shouldn't be and the flexibility such a policy based interface
provides is its major advantage.

Thanks,

Jonathan

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
  2019-05-10  8:57         ` Bastien Nocera
@ 2019-05-11 18:47           ` Roderick Colenbrander
  0 siblings, 0 replies; 16+ messages in thread
From: Roderick Colenbrander @ 2019-05-11 18:47 UTC (permalink / raw)
  To: Bastien Nocera
  Cc: Jonathan Cameron, H. Nikolaus Schaller, Dmitry Torokhov,
	Eric Piel, linux-input, letux-kernel, kernel, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, lkml, linux-iio

On Fri, May 10, 2019 at 1:57 AM Bastien Nocera <hadess@hadess.net> wrote:
>
> On Sun, 2019-04-14 at 09:26 -0700, Roderick Colenbrander wrote:
> >
> <snip>
> > We at the time were one of the first to expose acceleration and gyro
> > data through /dev/input for DualShock 4 as supported by hid-sony. We
> > report acceleration in 'g' and angular velocity in 'degree / s'. We
> > set the resolution to respectively '1 g' and '1 degree / s'. The range
> > we set to the range of the device e.g. for DS4 -4g to +4g for
> > acceleration. I need to check though what coordinate system we use,
> > but I think it is right handed (gyro counter clockwise relative to
> > acceleration axes).
>
> How do you export the gyro information through the input device?

For each DS4, there are multiple evdev devices for a DS4 for
respectively "gamepad", touchpad and motion sensors. The motion
sensors one, has INPUT_PROP_ACCELEROMETER set. ABS_X/_Y_Z provide
acceleration and ABS_RX/_RY/_RZ provide gyro. When we added this we
also updated the input documentation (event-codes.rst) to allow gyro
to use the rotational axes.

> FWIW, we needed to do extra work in iio-sensor-proxy so that the
> accelerometer in the Sixaxis/DS4 joypads (and uDraw tablet) didn't
> appear as though they were accelerometer for the system:
> https://github.com/hadess/iio-sensor-proxy/commit/401d59e54b3123860180d4401e09df8a1e1bc6c3
>
> > The two other drivers using INPUT_PROC_ACCELEROMETER are hid-wacom and
> > hid-udraw-ps3 Wacom. Both seem to report resolution in 'g'  as well.
>
> I wrote hid-udraw-ps3, and it's reporting accelerometer data through
> input because the rest of the driver is input, and it didn't make much
> sense to use another subsystem for just that small portion of the
> events the device sends out.
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Letux-kernel] [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface
       [not found]                 ` <CCD87A8D-FF65-4681-964B-22870716D655@goldelico.com>
@ 2019-06-08 10:52                   ` Jonathan Cameron
  0 siblings, 0 replies; 16+ messages in thread
From: Jonathan Cameron @ 2019-06-08 10:52 UTC (permalink / raw)
  To: H. Nikolaus Schaller
  Cc: linux-input, Dmitry Torokhov, Eric Piel, linux-iio, kernel, lkml,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Hartmut Knaack,
	Discussions about the Letux Kernel, Bastien Nocera

On Mon, 3 Jun 2019 09:30:40 +0200
H. Nikolaus Schaller <hns@goldelico.com> wrote:

> Hi Jonathan,
> sorry again for the long delay. I just now found a little time to summarize and try to
> get the discussion boiled down to the key difference.
> 
> > Am 11.05.2019 um 13:05 schrieb Jonathan Cameron <jic23@kernel.org>:
> > 
> > On Thu, 9 May 2019 19:02:49 +0200
> > "H. Nikolaus Schaller" <hns@goldelico.com> wrote:
> >   
> >> 
> >> If you close the lid, the display is turned upside down and y and z axes reverse sign.
> >> 
> >> So there remains only the issue that user-space must know which sensor device file is which sensor
> >> and can do the calculation of the lid angle. This is possible because the iio accelerometer name
> >> is available through the input event ioctls.
> >> 
> >> In summary this case also does not need policy or configuration. Just user space using the information
> >> that is already presented.  
> > 
> > I disagree with that last statement.  If there is a lid angle sensor, policy is
> > needed to know which of your associated orientation is the base one and which
> > device indicates the lid angle.  
> 
> > 
> > Actually most of the time what you will do is pick one 'correct' sensor under
> > some configuration of the device and use that.  That is policy.  Yes, you could
> > bake the policy in to device tree, but then you can also bake in the association
> > between the underlying IIO sensor and any virtual input sensor.  
> 
> Ah, maybe I did not understand what you mean by policy here.
> 
> Indeed, choosing the right sensor is always something which is application specific
> and something user-space must obviously dictate. And we agree this should *not* be
> in device tree (or user-space scanning device tree) because that describes hardware
> and not user-space interaction.
> 
> But I still do not think that this requires a new mechanism where user-space
> *tells* the kernel which sensor to use and present as which device.
> 
> Equally well, the kernel can present all sensors it knows about and a set of properties
> that allow the user-space to simply choose the right one ("apply policy"). Properties
> could be file name (e.g. provided by udev), device name, label (provided by DT) or similar.
> 
> If it were absolutely necessary to tell the kernel to map iio devices to something before
> use, I think Bastien would not have been able to implement his library. He also has to
> choose the right sensors. This seems to work and not need a new mechanism.
> 
> > 
> > Anyhow, we still disagree on whether any such virtual input interface
> > should be a userspace policy decision.  So far I haven't seen any compelling
> > argument why it shouldn't be and the flexibility such a policy based interface
> > provides is its major advantage.  
> 
> I still think it is not needed because kernel already provides necessary information
> to user-space to make policy decisions (by ignore unwanted interfaces) without needing
> a new interface where the user-space tells the kernel to activate some interfaces.
> 
> So the key difference is about the question if user-space needs to tell the kernel first
> that it wants to see a specific interface or just makes use of it if present.

Absolutely. Good summary, but I don't think either of us is going
to persuade the other.

I've started work on my proposal but things have been 'interesting' in the
last few weeks so it may be a little while yet before I have anything
to share.

Jonathan

> 
> BR and thanks,
> Nikolaus
> 


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, back to index

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-31 10:09 [RFC v2] iio: input-bridge: optionally bridge iio acceleometers to create a /dev/input interface H. Nikolaus Schaller
2019-04-07 12:30 ` Jonathan Cameron
     [not found]   ` <CD44AFA0-6676-4842-9C80-61BB363DD556@goldelico.com>
2019-04-14 11:40     ` Jonathan Cameron
2019-04-14 16:26       ` Roderick Colenbrander
2019-05-10  8:57         ` Bastien Nocera
2019-05-11 18:47           ` Roderick Colenbrander
     [not found]       ` <CD6219BE-61FF-4C38-9532-054C60A77F89@goldelico.com>
2019-04-22 14:20         ` Jonathan Cameron
2019-05-09  9:09           ` H. Nikolaus Schaller
2019-05-09 17:02             ` [Letux-kernel] " H. Nikolaus Schaller
2019-05-11 11:05               ` Jonathan Cameron
     [not found]                 ` <CCD87A8D-FF65-4681-964B-22870716D655@goldelico.com>
2019-06-08 10:52                   ` Jonathan Cameron
2019-05-11 10:54             ` Jonathan Cameron
2019-05-10  8:57           ` Bastien Nocera
2019-05-10  9:33             ` H. Nikolaus Schaller
2019-05-10  9:35               ` Bastien Nocera
2019-05-10 10:06                 ` H. Nikolaus Schaller

Linux-IIO Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-iio/0 linux-iio/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-iio linux-iio/ https://lore.kernel.org/linux-iio \
		linux-iio@vger.kernel.org linux-iio@archiver.kernel.org
	public-inbox-index linux-iio


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-iio


AGPL code for this site: git clone https://public-inbox.org/ public-inbox