diff mbox series

Input: xpad - Poweroff XBOX360W on mode button long press

Message ID 20211113141155.26217b44@rechenknecht2k11 (mailing list archive)
State Superseded
Headers show
Series Input: xpad - Poweroff XBOX360W on mode button long press | expand

Commit Message

Benjamin Valentin Nov. 13, 2021, 1:11 p.m. UTC
Newer gamepads turn themselves off when the mode button is held down.
For XBOX360W gamepads we must do this in the driver.

Power off the gamepad after 2s of holding down the button.

Signed-off-by: lawl <github@dumbinter.net>
Signed-off-by: Benjamin Valentin <benpicco@googlemail.com>
---
 drivers/input/joystick/xpad.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

Comments

Cameron Gutman Nov. 14, 2021, 1:52 a.m. UTC | #1
On 11/13/21 07:11, Benjamin Valentin wrote:
> Newer gamepads turn themselves off when the mode button is held down.
> For XBOX360W gamepads we must do this in the driver.
> 
> Power off the gamepad after 2s of holding down the button.
> 

2 seconds is pretty short. It seems like there's a pretty good chance a
user might accidentally turn their gamepad off when trying to activate
something mapped to a long-press on the guide/mode button.

I timed a couple of my Xbox One gamepads and they both took 5-7 seconds
to power off when holding down the button. How about 5 seconds here?

> Signed-off-by: lawl <github@dumbinter.net>
> Signed-off-by: Benjamin Valentin <benpicco@googlemail.com>
> ---

Regards,
Cameron
diff mbox series

Patch

diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index 18caaf436ed4..e36c4b0abd4b 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -619,11 +619,13 @@  struct usb_xpad {
 	int pad_nr;			/* the order x360 pads were attached */
 	const char *name;		/* name of the device */
 	struct work_struct work;	/* init/remove device from callback */
+	time64_t mode_btn_down_ts;	/* timestamp when mode button was pressed */
 };
 
 static int xpad_init_input(struct usb_xpad *xpad);
 static void xpad_deinit_input(struct usb_xpad *xpad);
 static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num);
+static void xpad360w_poweroff_controller(struct usb_xpad *xpad);
 
 /*
  *	xpad_process_packet
@@ -775,6 +777,24 @@  static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
 	}
 
 	input_sync(dev);
+
+	/* xbox360w controllers can't be turned off without driver assistance */
+	if (xpad->xtype == XTYPE_XBOX360W) {
+		if (xpad->mode_btn_down_ts > 0
+		    && xpad->pad_present
+		    /* send power off after 2s of holding the button */
+		    && (ktime_get_seconds() - xpad->mode_btn_down_ts) >= 2) {
+			xpad360w_poweroff_controller(xpad);
+			xpad->mode_btn_down_ts = 0;
+			return;
+		}
+
+		/* mode button down/up */
+		if (data[3] & 0x04)
+			xpad->mode_btn_down_ts = ktime_get_seconds();
+		else
+			xpad->mode_btn_down_ts = 0;
+	}
 }
 
 static void xpad_presence_work(struct work_struct *work)