linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] Input: adc-joystick: Handle inverted axes
@ 2024-01-15 19:27 Chris Morgan
  2024-01-19  8:29 ` Dmitry Torokhov
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Morgan @ 2024-01-15 19:27 UTC (permalink / raw)
  To: linux-input
  Cc: contact, dmitry.torokhov, hdegoede, paul, peter.hutterer, svv,
	biswarupp, 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 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>
Acked-by: Artur Rojek <contact@artur-rojek.eu>
---
Changes since V2:
 - Explicitly set bool value to "true" instead of "1".
 - Split adc_joystick_invert() function definition to 2 lines.
 - Corrected changes message location.
Changes since V1:
 - Moved proposed helper for inversion from input stack to adc-joystick
   driver.

 drivers/input/joystick/adc-joystick.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/input/joystick/adc-joystick.c b/drivers/input/joystick/adc-joystick.c
index c0deff5d4282..6b155e614b5a 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 {
@@ -29,6 +30,15 @@ struct adc_joystick {
 	bool polled;
 };
 
+static int adc_joystick_invert(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;
+}
+
 static void adc_joystick_poll(struct input_dev *input)
 {
 	struct adc_joystick *joy = input_get_drvdata(input);
@@ -38,6 +48,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 = adc_joystick_invert(input, i, val);
 		input_report_abs(input, joy->axes[i].code, val);
 	}
 	input_sync(input);
@@ -86,6 +98,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 = adc_joystick_invert(joy->input, i, val);
 		input_report_abs(joy->input, joy->axes[i].code, val);
 	}
 
@@ -168,11 +182,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 = true;
+		}
+
 		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 v3] Input: adc-joystick: Handle inverted axes
  2024-01-15 19:27 [PATCH v3] Input: adc-joystick: Handle inverted axes Chris Morgan
@ 2024-01-19  8:29 ` Dmitry Torokhov
  2024-01-20  9:10   ` Dmitry Torokhov
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Torokhov @ 2024-01-19  8:29 UTC (permalink / raw)
  To: Chris Morgan
  Cc: linux-input, contact, hdegoede, paul, peter.hutterer, svv,
	biswarupp, Chris Morgan

Hi Chris,

On Mon, Jan 15, 2024 at 01:27:52PM -0600, Chris Morgan wrote:
>  
> +static int adc_joystick_invert(struct input_dev *dev,
> +			       unsigned int axis, int val)
> +{
> +	int min = dev->absinfo[axis].minimum;
> +	int max = dev->absinfo[axis].maximum;

I changed this to input_abs_get_[min|max](dev, axis) to avoid peeking
into absinfo and applied.

Thanks.

-- 
Dmitry

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

* Re: [PATCH v3] Input: adc-joystick: Handle inverted axes
  2024-01-19  8:29 ` Dmitry Torokhov
@ 2024-01-20  9:10   ` Dmitry Torokhov
  2024-06-07 19:31     ` Dmitry Torokhov
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Torokhov @ 2024-01-20  9:10 UTC (permalink / raw)
  To: Chris Morgan
  Cc: linux-input, contact, hdegoede, paul, peter.hutterer, svv,
	biswarupp, Chris Morgan

On Fri, Jan 19, 2024 at 12:29:54AM -0800, Dmitry Torokhov wrote:
> Hi Chris,
> 
> On Mon, Jan 15, 2024 at 01:27:52PM -0600, Chris Morgan wrote:
> >  
> > +static int adc_joystick_invert(struct input_dev *dev,
> > +			       unsigned int axis, int val)
> > +{
> > +	int min = dev->absinfo[axis].minimum;
> > +	int max = dev->absinfo[axis].maximum;
> 
> I changed this to input_abs_get_[min|max](dev, axis) to avoid peeking
> into absinfo and applied.

Apparently min_array() and max_array() are a bit too new. Also I am not
sure if they are actually needed. Can we do it like this:

diff --git a/drivers/input/joystick/adc-joystick.c b/drivers/input/joystick/adc-joystick.c
index 10ee13465cfe..916e78e4dc9f 100644
--- a/drivers/input/joystick/adc-joystick.c
+++ b/drivers/input/joystick/adc-joystick.c
@@ -185,14 +185,14 @@ static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy)
 		if (axes[i].range[0] > axes[i].range[1]) {
 			dev_dbg(dev, "abs-axis %d inverted\n", i);
 			axes[i].inverted = true;
+			swap(axes[i].range[0], axes[i].range[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,
-				     min_array(axes[i].range, 2),
-				     max_array(axes[i].range, 2),
+				     axes[i].range[0], axes[i].range[1],
 				     axes[i].fuzz, axes[i].flat);
 		input_set_capability(joy->input, EV_ABS, axes[i].code);
 	}

Thanks.

-- 
Dmitry

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

* Re: [PATCH v3] Input: adc-joystick: Handle inverted axes
  2024-01-20  9:10   ` Dmitry Torokhov
@ 2024-06-07 19:31     ` Dmitry Torokhov
  0 siblings, 0 replies; 4+ messages in thread
From: Dmitry Torokhov @ 2024-06-07 19:31 UTC (permalink / raw)
  To: Chris Morgan
  Cc: linux-input, contact, hdegoede, paul, peter.hutterer, svv,
	biswarupp, Chris Morgan

On Sat, Jan 20, 2024 at 01:10:18AM -0800, Dmitry Torokhov wrote:
> On Fri, Jan 19, 2024 at 12:29:54AM -0800, Dmitry Torokhov wrote:
> > Hi Chris,
> > 
> > On Mon, Jan 15, 2024 at 01:27:52PM -0600, Chris Morgan wrote:
> > >  
> > > +static int adc_joystick_invert(struct input_dev *dev,
> > > +			       unsigned int axis, int val)
> > > +{
> > > +	int min = dev->absinfo[axis].minimum;
> > > +	int max = dev->absinfo[axis].maximum;
> > 
> > I changed this to input_abs_get_[min|max](dev, axis) to avoid peeking
> > into absinfo and applied.
> 
> Apparently min_array() and max_array() are a bit too new. Also I am not
> sure if they are actually needed. Can we do it like this:
> 
> diff --git a/drivers/input/joystick/adc-joystick.c b/drivers/input/joystick/adc-joystick.c
> index 10ee13465cfe..916e78e4dc9f 100644
> --- a/drivers/input/joystick/adc-joystick.c
> +++ b/drivers/input/joystick/adc-joystick.c
> @@ -185,14 +185,14 @@ static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy)
>  		if (axes[i].range[0] > axes[i].range[1]) {
>  			dev_dbg(dev, "abs-axis %d inverted\n", i);
>  			axes[i].inverted = true;
> +			swap(axes[i].range[0], axes[i].range[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,
> -				     min_array(axes[i].range, 2),
> -				     max_array(axes[i].range, 2),
> +				     axes[i].range[0], axes[i].range[1],
>  				     axes[i].fuzz, axes[i].flat);
>  		input_set_capability(joy->input, EV_ABS, axes[i].code);
>  	}

OK, I went ahead and folded this into the original patch and applied.

Thanks.

-- 
Dmitry

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

end of thread, other threads:[~2024-06-07 19:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-15 19:27 [PATCH v3] Input: adc-joystick: Handle inverted axes Chris Morgan
2024-01-19  8:29 ` Dmitry Torokhov
2024-01-20  9:10   ` Dmitry Torokhov
2024-06-07 19:31     ` Dmitry Torokhov

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).