All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] input/xpad: LED controllable through input events
@ 2022-10-22 22:05 Benoit Maurin
  2022-10-23  4:00 ` Roderick Colenbrander
  0 siblings, 1 reply; 3+ messages in thread
From: Benoit Maurin @ 2022-10-22 22:05 UTC (permalink / raw)
  Cc: Benoit Maurin, Dmitry Torokhov, linux-input, linux-kernel

(EV_LED, LED_MISC, #VALUE) can now be used to control leds on the
xpad gamepad (was only possible through /sys/class/leds/xpad0/brightness)
with permissions of /dev/input/xxx

To test the code (xpad can be compiled out-of-tree with some slight
tweaks):

```
import evdev
device = evdev.InputDevice('/dev/input/event15') # not js0
device.set_led(8, 2)
device.set_led(8, 0) # this won't be delivered
device.set_led(8, 16) # must do this instead
device.set_led(8, 15)
```

Signed-off-by: Benoit Maurin <maurinbe@gmail.com>
---
 drivers/input/joystick/xpad.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index 2959d80f7..fcf4d2c8f 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -1646,6 +1646,7 @@ static int xpad_led_probe(struct usb_xpad *xpad)
 		goto err_free_mem;
 	}
 
+	input_set_capability(xpad->dev, EV_LED, LED_MISC);
 	snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
 	led->xpad = xpad;
 
@@ -1824,6 +1825,28 @@ static void xpad_deinit_input(struct usb_xpad *xpad)
 	}
 }
 
+static int xpad_event(struct input_dev *dev, unsigned int type,
+		      unsigned int code, int value)
+{
+	struct usb_xpad *xpad = input_get_drvdata(dev);
+
+	if (type != EV_LED || xpad->led == NULL)
+		return 0;
+	xpad_send_led_command(xpad, value);
+	xpad->led->led_cdev.brightness = value;
+	/* Bit clearing is necessary otherwise two events with
+	 * different non-null values will deliver only the first one.
+	 * To work around this, we clear the bit to indicate that the
+	 * current value is zero. The downside is that events with zero
+	 * value won't be delivered. It's not a big deal since a value of
+	 * 16 can be sent which is the same as 0
+	 * See xpad_send_led_command, command %= 16
+	 */
+
+	clear_bit(code, xpad->dev->led);
+	return 0;
+}
+
 static int xpad_init_input(struct usb_xpad *xpad)
 {
 	struct input_dev *input_dev;
@@ -1851,6 +1874,7 @@ static int xpad_init_input(struct usb_xpad *xpad)
 		input_dev->open = xpad_open;
 		input_dev->close = xpad_close;
 	}
+	input_dev->event = xpad_event;
 
 	if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
 		/* set up axes */
-- 
2.38.1


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

* Re: [PATCH] input/xpad: LED controllable through input events
  2022-10-22 22:05 [PATCH] input/xpad: LED controllable through input events Benoit Maurin
@ 2022-10-23  4:00 ` Roderick Colenbrander
  2022-10-23 16:02   ` Benoit Maurin
  0 siblings, 1 reply; 3+ messages in thread
From: Roderick Colenbrander @ 2022-10-23  4:00 UTC (permalink / raw)
  To: Benoit Maurin; +Cc: Dmitry Torokhov, linux-input, linux-kernel

Hi Benoit,

Thanks for your patch. I'm thinking out loud whether this is a
direction we want to go in regards to EV_LED. I vaguely recall some
discussions a while ago that EV_LED was really from a different era
for legacy reasons (keyboard LEDs). Since the introduction of the LED
framework that was really the way to go.

Thanks,
Roderick

On Sat, Oct 22, 2022 at 3:08 PM Benoit Maurin <maurinbe@gmail.com> wrote:
>
> (EV_LED, LED_MISC, #VALUE) can now be used to control leds on the
> xpad gamepad (was only possible through /sys/class/leds/xpad0/brightness)
> with permissions of /dev/input/xxx
>
> To test the code (xpad can be compiled out-of-tree with some slight
> tweaks):
>
> ```
> import evdev
> device = evdev.InputDevice('/dev/input/event15') # not js0
> device.set_led(8, 2)
> device.set_led(8, 0) # this won't be delivered
> device.set_led(8, 16) # must do this instead
> device.set_led(8, 15)
> ```
>
> Signed-off-by: Benoit Maurin <maurinbe@gmail.com>
> ---
>  drivers/input/joystick/xpad.c | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
>
> diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
> index 2959d80f7..fcf4d2c8f 100644
> --- a/drivers/input/joystick/xpad.c
> +++ b/drivers/input/joystick/xpad.c
> @@ -1646,6 +1646,7 @@ static int xpad_led_probe(struct usb_xpad *xpad)
>                 goto err_free_mem;
>         }
>
> +       input_set_capability(xpad->dev, EV_LED, LED_MISC);
>         snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
>         led->xpad = xpad;
>
> @@ -1824,6 +1825,28 @@ static void xpad_deinit_input(struct usb_xpad *xpad)
>         }
>  }
>
> +static int xpad_event(struct input_dev *dev, unsigned int type,
> +                     unsigned int code, int value)
> +{
> +       struct usb_xpad *xpad = input_get_drvdata(dev);
> +
> +       if (type != EV_LED || xpad->led == NULL)
> +               return 0;
> +       xpad_send_led_command(xpad, value);
> +       xpad->led->led_cdev.brightness = value;
> +       /* Bit clearing is necessary otherwise two events with
> +        * different non-null values will deliver only the first one.
> +        * To work around this, we clear the bit to indicate that the
> +        * current value is zero. The downside is that events with zero
> +        * value won't be delivered. It's not a big deal since a value of
> +        * 16 can be sent which is the same as 0
> +        * See xpad_send_led_command, command %= 16
> +        */
> +
> +       clear_bit(code, xpad->dev->led);
> +       return 0;
> +}
> +
>  static int xpad_init_input(struct usb_xpad *xpad)
>  {
>         struct input_dev *input_dev;
> @@ -1851,6 +1874,7 @@ static int xpad_init_input(struct usb_xpad *xpad)
>                 input_dev->open = xpad_open;
>                 input_dev->close = xpad_close;
>         }
> +       input_dev->event = xpad_event;
>
>         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
>                 /* set up axes */
> --
> 2.38.1
>

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

* Re: [PATCH] input/xpad: LED controllable through input events
  2022-10-23  4:00 ` Roderick Colenbrander
@ 2022-10-23 16:02   ` Benoit Maurin
  0 siblings, 0 replies; 3+ messages in thread
From: Benoit Maurin @ 2022-10-23 16:02 UTC (permalink / raw)
  To: Roderick Colenbrander; +Cc: Dmitry Torokhov, linux-input, linux-kernel

Hi Roderick,

Thanks for having a look. I'd understand if this is not a desirable direction,
I was not aware of this legacy-ish connotation of EV_LED (should have figured
it out with the hoops about the clear_bit thing) and it seemed convenient to
interact with the pad through a single /dev/input/xxx interface.

Let me know what you decide :)
Benoit


Le dim. 23 oct. 2022 à 06:00, Roderick Colenbrander
<thunderbird2k@gmail.com> a écrit :
>
> Hi Benoit,
>
> Thanks for your patch. I'm thinking out loud whether this is a
> direction we want to go in regards to EV_LED. I vaguely recall some
> discussions a while ago that EV_LED was really from a different era
> for legacy reasons (keyboard LEDs). Since the introduction of the LED
> framework that was really the way to go.
>
> Thanks,
> Roderick
>
> On Sat, Oct 22, 2022 at 3:08 PM Benoit Maurin <maurinbe@gmail.com> wrote:
> >
> > (EV_LED, LED_MISC, #VALUE) can now be used to control leds on the
> > xpad gamepad (was only possible through /sys/class/leds/xpad0/brightness)
> > with permissions of /dev/input/xxx
> >
> > To test the code (xpad can be compiled out-of-tree with some slight
> > tweaks):
> >
> > ```
> > import evdev
> > device = evdev.InputDevice('/dev/input/event15') # not js0
> > device.set_led(8, 2)
> > device.set_led(8, 0) # this won't be delivered
> > device.set_led(8, 16) # must do this instead
> > device.set_led(8, 15)
> > ```
> >
> > Signed-off-by: Benoit Maurin <maurinbe@gmail.com>
> > ---
> >  drivers/input/joystick/xpad.c | 24 ++++++++++++++++++++++++
> >  1 file changed, 24 insertions(+)
> >
> > diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
> > index 2959d80f7..fcf4d2c8f 100644
> > --- a/drivers/input/joystick/xpad.c
> > +++ b/drivers/input/joystick/xpad.c
> > @@ -1646,6 +1646,7 @@ static int xpad_led_probe(struct usb_xpad *xpad)
> >                 goto err_free_mem;
> >         }
> >
> > +       input_set_capability(xpad->dev, EV_LED, LED_MISC);
> >         snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
> >         led->xpad = xpad;
> >
> > @@ -1824,6 +1825,28 @@ static void xpad_deinit_input(struct usb_xpad *xpad)
> >         }
> >  }
> >
> > +static int xpad_event(struct input_dev *dev, unsigned int type,
> > +                     unsigned int code, int value)
> > +{
> > +       struct usb_xpad *xpad = input_get_drvdata(dev);
> > +
> > +       if (type != EV_LED || xpad->led == NULL)
> > +               return 0;
> > +       xpad_send_led_command(xpad, value);
> > +       xpad->led->led_cdev.brightness = value;
> > +       /* Bit clearing is necessary otherwise two events with
> > +        * different non-null values will deliver only the first one.
> > +        * To work around this, we clear the bit to indicate that the
> > +        * current value is zero. The downside is that events with zero
> > +        * value won't be delivered. It's not a big deal since a value of
> > +        * 16 can be sent which is the same as 0
> > +        * See xpad_send_led_command, command %= 16
> > +        */
> > +
> > +       clear_bit(code, xpad->dev->led);
> > +       return 0;
> > +}
> > +
> >  static int xpad_init_input(struct usb_xpad *xpad)
> >  {
> >         struct input_dev *input_dev;
> > @@ -1851,6 +1874,7 @@ static int xpad_init_input(struct usb_xpad *xpad)
> >                 input_dev->open = xpad_open;
> >                 input_dev->close = xpad_close;
> >         }
> > +       input_dev->event = xpad_event;
> >
> >         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
> >                 /* set up axes */
> > --
> > 2.38.1
> >

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

end of thread, other threads:[~2022-10-23 16:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22 22:05 [PATCH] input/xpad: LED controllable through input events Benoit Maurin
2022-10-23  4:00 ` Roderick Colenbrander
2022-10-23 16:02   ` Benoit Maurin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.