linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Allow inverted axes for adc-joystick
@ 2023-12-31 20:56 Chris Morgan
  2023-12-31 20:56 ` [PATCH 1/2] Input: add input_invert_abs() Chris Morgan
  2023-12-31 20:56 ` [PATCH 2/2] Input: adc-joystick: Handle inverted axes Chris Morgan
  0 siblings, 2 replies; 4+ messages in thread
From: Chris Morgan @ 2023-12-31 20:56 UTC (permalink / raw)
  To: linux-input
  Cc: dmitry.torokhov, hdegoede, paul, peter.hutterer, svv, biswarupp,
	contact, Chris Morgan

From: Chris Morgan <macromorgan@hotmail.com>

Handle inverted axes in the adc-joystick driver so that reported input
events downstream from the driver conform with assumptions that
abs_min < abs_max. Add a new helper function for inputs so that abs
can be inverted that drivers can use.

Chris Morgan (2):
  Input: add input_invert_abs()
  Input: adc-joystick: Handle inverted axes

 drivers/input/input.c                 | 19 +++++++++++++++++++
 drivers/input/joystick/adc-joystick.c | 13 ++++++++++++-
 include/linux/input.h                 |  1 +
 3 files changed, 32 insertions(+), 1 deletion(-)

-- 
2.34.1


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

* [PATCH 1/2] Input: add input_invert_abs()
  2023-12-31 20:56 [PATCH 0/2] Allow inverted axes for adc-joystick Chris Morgan
@ 2023-12-31 20:56 ` Chris Morgan
  2024-01-04 17:59   ` Dmitry Torokhov
  2023-12-31 20:56 ` [PATCH 2/2] Input: adc-joystick: Handle inverted axes Chris Morgan
  1 sibling, 1 reply; 4+ messages in thread
From: Chris Morgan @ 2023-12-31 20:56 UTC (permalink / raw)
  To: linux-input
  Cc: dmitry.torokhov, hdegoede, paul, peter.hutterer, svv, biswarupp,
	contact, Chris Morgan

From: Chris Morgan <macromorgan@hotmail.com>

Add a helper function to make it easier for a driver to invert abs
values when needed. It is up to the driver itself to track axes that
need to be inverted and normalize the data before it is passed on.

This function assumes that drivers will set the min and max values
so that min < max and then will simply call this function each time
the values need to be inverted.

Signed-off-by: Chris Morgan <macromorgan@hotmail.com>
---
 drivers/input/input.c | 19 +++++++++++++++++++
 include/linux/input.h |  1 +
 2 files changed, 20 insertions(+)

diff --git a/drivers/input/input.c b/drivers/input/input.c
index 8c5fdb0f858a..f135aed165a1 100644
--- a/drivers/input/input.c
+++ b/drivers/input/input.c
@@ -552,6 +552,25 @@ void input_copy_abs(struct input_dev *dst, unsigned int dst_axis,
 }
 EXPORT_SYMBOL(input_copy_abs);
 
+/**
+ * input_invert_abs - Invert the abs value for an inverted axis.
+ * @dev: Input device with absolute events
+ * @axis: ABS_* value selecting the destination axis for the event to
+ *	  invert.
+ * @val: Value to be inverted based on min and max values of the axis.
+ *
+ * Return an inverted value for a given ABS axis based on its min and
+ * max values.
+ */
+int input_invert_abs(struct input_dev *dev, unsigned int axis, int val)
+{
+	int min = dev->absinfo[axis].minimum;
+	int max = dev->absinfo[axis].maximum;
+
+	return (max + min) - val;
+}
+EXPORT_SYMBOL(input_invert_abs);
+
 /**
  * input_grab_device - grabs device for exclusive use
  * @handle: input handle that wants to own the device
diff --git a/include/linux/input.h b/include/linux/input.h
index de6503c0edb8..deb5f8bb0ec7 100644
--- a/include/linux/input.h
+++ b/include/linux/input.h
@@ -477,6 +477,7 @@ void input_set_abs_params(struct input_dev *dev, unsigned int axis,
 			  int min, int max, int fuzz, int flat);
 void input_copy_abs(struct input_dev *dst, unsigned int dst_axis,
 		    const struct input_dev *src, unsigned int src_axis);
+int input_invert_abs(struct input_dev *dev, unsigned int axis, int val);
 
 #define INPUT_GENERATE_ABS_ACCESSORS(_suffix, _item)			\
 static inline int input_abs_get_##_suffix(struct input_dev *dev,	\
-- 
2.34.1


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

* [PATCH 2/2] Input: adc-joystick: Handle inverted axes
  2023-12-31 20:56 [PATCH 0/2] Allow inverted axes for adc-joystick Chris Morgan
  2023-12-31 20:56 ` [PATCH 1/2] Input: add input_invert_abs() Chris Morgan
@ 2023-12-31 20:56 ` Chris Morgan
  1 sibling, 0 replies; 4+ messages in thread
From: Chris Morgan @ 2023-12-31 20:56 UTC (permalink / raw)
  To: linux-input
  Cc: dmitry.torokhov, hdegoede, paul, peter.hutterer, svv, biswarupp,
	contact, Chris Morgan

From: Chris Morgan <macromorgan@hotmail.com>

When one or more axes are inverted, (where min > max), normalize the
data so that min < max and call a helper function to invert the
values reported to the input stack.

This ensures we can continue defining the device correctly in the
device tree while not breaking downstream assumptions that min is
always less than max.

Signed-off-by: Chris Morgan <macromorgan@hotmail.com>
---
 drivers/input/joystick/adc-joystick.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/input/joystick/adc-joystick.c b/drivers/input/joystick/adc-joystick.c
index c0deff5d4282..4e8d446987b6 100644
--- a/drivers/input/joystick/adc-joystick.c
+++ b/drivers/input/joystick/adc-joystick.c
@@ -18,6 +18,7 @@ struct adc_joystick_axis {
 	s32 range[2];
 	s32 fuzz;
 	s32 flat;
+	bool inverted;
 };
 
 struct adc_joystick {
@@ -38,6 +39,8 @@ static void adc_joystick_poll(struct input_dev *input)
 		ret = iio_read_channel_raw(&joy->chans[i], &val);
 		if (ret < 0)
 			return;
+		if (joy->axes[i].inverted)
+			val = input_invert_abs(input, i, val);
 		input_report_abs(input, joy->axes[i].code, val);
 	}
 	input_sync(input);
@@ -86,6 +89,8 @@ static int adc_joystick_handle(const void *data, void *private)
 			val = sign_extend32(val, msb);
 		else
 			val &= GENMASK(msb, 0);
+		if (joy->axes[i].inverted)
+			val = input_invert_abs(joy->input, i, val);
 		input_report_abs(joy->input, joy->axes[i].code, val);
 	}
 
@@ -168,11 +173,17 @@ static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy)
 			goto err_fwnode_put;
 		}
 
+		if (axes[i].range[0] > axes[i].range[1]) {
+			dev_dbg(dev, "abs-axis %d inverted\n", i);
+			axes[i].inverted = 1;
+		}
+
 		fwnode_property_read_u32(child, "abs-fuzz", &axes[i].fuzz);
 		fwnode_property_read_u32(child, "abs-flat", &axes[i].flat);
 
 		input_set_abs_params(joy->input, axes[i].code,
-				     axes[i].range[0], axes[i].range[1],
+				     min_array(axes[i].range, 2),
+				     max_array(axes[i].range, 2),
 				     axes[i].fuzz, axes[i].flat);
 		input_set_capability(joy->input, EV_ABS, axes[i].code);
 	}
-- 
2.34.1


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

* Re: [PATCH 1/2] Input: add input_invert_abs()
  2023-12-31 20:56 ` [PATCH 1/2] Input: add input_invert_abs() Chris Morgan
@ 2024-01-04 17:59   ` Dmitry Torokhov
  0 siblings, 0 replies; 4+ messages in thread
From: Dmitry Torokhov @ 2024-01-04 17:59 UTC (permalink / raw)
  To: Chris Morgan
  Cc: linux-input, hdegoede, paul, peter.hutterer, svv, biswarupp,
	contact, Chris Morgan

Hi Chris,

On Sun, Dec 31, 2023 at 02:56:42PM -0600, Chris Morgan wrote:
> From: Chris Morgan <macromorgan@hotmail.com>
> 
> Add a helper function to make it easier for a driver to invert abs
> values when needed. It is up to the driver itself to track axes that
> need to be inverted and normalize the data before it is passed on.
> 
> This function assumes that drivers will set the min and max values
> so that min < max and then will simply call this function each time
> the values need to be inverted.
> 
> Signed-off-by: Chris Morgan <macromorgan@hotmail.com>
> ---
>  drivers/input/input.c | 19 +++++++++++++++++++
>  include/linux/input.h |  1 +
>  2 files changed, 20 insertions(+)
> 
> diff --git a/drivers/input/input.c b/drivers/input/input.c
> index 8c5fdb0f858a..f135aed165a1 100644
> --- a/drivers/input/input.c
> +++ b/drivers/input/input.c
> @@ -552,6 +552,25 @@ void input_copy_abs(struct input_dev *dst, unsigned int dst_axis,
>  }
>  EXPORT_SYMBOL(input_copy_abs);
>  
> +/**
> + * input_invert_abs - Invert the abs value for an inverted axis.
> + * @dev: Input device with absolute events
> + * @axis: ABS_* value selecting the destination axis for the event to
> + *	  invert.
> + * @val: Value to be inverted based on min and max values of the axis.
> + *
> + * Return an inverted value for a given ABS axis based on its min and
> + * max values.
> + */
> +int input_invert_abs(struct input_dev *dev, unsigned int axis, int val)
> +{
> +	int min = dev->absinfo[axis].minimum;
> +	int max = dev->absinfo[axis].maximum;
> +
> +	return (max + min) - val;

I do not think this is generic enough (i.e. sometimes you have a
theoretical range in which you want to invert that is bigger than the
normal min/max - see synaptics_invert_y()), so I would prefer this be
lolcal to the ADC joystick driver, at least for now.

Thanks.

-- 
Dmitry

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

end of thread, other threads:[~2024-01-04 17:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-31 20:56 [PATCH 0/2] Allow inverted axes for adc-joystick Chris Morgan
2023-12-31 20:56 ` [PATCH 1/2] Input: add input_invert_abs() Chris Morgan
2024-01-04 17:59   ` Dmitry Torokhov
2023-12-31 20:56 ` [PATCH 2/2] Input: adc-joystick: Handle inverted axes Chris Morgan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).