linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benoit Maurin <maurinbe@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Benoit Maurin <maurinbe@gmail.com>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] input/xpad: LED controllable through input events
Date: Sun, 23 Oct 2022 00:05:07 +0200	[thread overview]
Message-ID: <20221022220508.37489-1-maurinbe@gmail.com> (raw)

(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


             reply	other threads:[~2022-10-22 22:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-22 22:05 Benoit Maurin [this message]
2022-10-23  4:00 ` [PATCH] input/xpad: LED controllable through input events Roderick Colenbrander
2022-10-23 16:02   ` Benoit Maurin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221022220508.37489-1-maurinbe@gmail.com \
    --to=maurinbe@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).