Skip to contentSkip to conversation

Input: xbox_gip - Add new driver for Xbox GIP

PATCH Superseded Vicki Pfau 2 participants 23 messages Sep 8

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Vicki Pfau

author
#0 lore raw

v6 cover Input: xbox_gip - Add new driver for Xbox GIP

This is a new version of the previously submitted xbox-gip series.

This introduces a new driver for the Xbox One/Series controller protocol, officially known as the Gaming Input Protocol, or GIP for short.

Microsoft released documentation on (some of) GIP in late 2024, upon which this driver is based. Though the documentation was incomplete, it still provided enough information to warrant a clean start over the previous, incomplete implementation.

This driver is already at feature parity with the GIP support in xpad, along with several more enhancements:

- Proper support for parsing message length and fragmented messages
- Metadata parsing, allowing for auto-detection on various parameters, including the presence and location in the message of the share button, as well as detection of specific device types
- Controllable LED support
- HID passthrough for the Chatpad
- Battery information support
- Arcade stick support
- Racing wheel support
- Support for the security flow, allowing use with wireless controllers that include their own dongles. (This does not include support for the first-party dongle yet.)

The framework set out in this driver also allows future expansion for specialized device types and additional features more cleanly than xpad.

Future plans include:

- Adding support for more device types, such as flight sticks.
- Exposing a raw character device to enable sending vendor-specific commands from userspace.
- Event logging to either sysfs or dmesg.
- Support for the headphone jack.

This version cleans up more issues found by sashiko as well as some smaller refactoring to clean up reused code.

Vicki Pfau (12):
Input: xbox_gip - Add new driver for Xbox GIP
Input: xpad - Remove Xbox One support
Input: xbox_gip - Add controllable LED support
Input: xbox_gip - Add HID relaying
Input: xbox_gip - Add battery support
Input: xbox_gip - Allow sending fragmented and ACME messages
Input: xbox_gip - Add security implementation from xone
Input: xbox_gip - Add arcade stick support
Input: xbox_gip - Add support for PDP guitar controllers
Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER
HID: Map more automobile simulation inputs
Input: xbox_gip - Add wheel support

 Documentation/input/devices/xpad.rst          |   17 +-
 MAINTAINERS                                   |    6 +
 drivers/hid/hid-debug.c                       |   16 +-
 drivers/hid/hid-input.c                       |    2 +
 drivers/input/joystick/Kconfig                |    2 +
 drivers/input/joystick/Makefile               |    1 +
 drivers/input/joystick/gip/Kconfig            |   22 +
 drivers/input/joystick/gip/Makefile           |    8 +
 drivers/input/joystick/gip/gip-arcade-stick.c |  173 +
 drivers/input/joystick/gip/gip-core.c         | 3281 +++++++++++++++++
 drivers/input/joystick/gip/gip-drivers.c      |  222 ++
 drivers/input/joystick/gip/gip-pdp-jaguar.c   |  129 +
 drivers/input/joystick/gip/gip-security.c     |  807 ++++
 drivers/input/joystick/gip/gip-wheel.c        |  350 ++
 drivers/input/joystick/gip/gip.h              |  434 +++
 drivers/input/joystick/xpad.c                 |  684 +---
 include/uapi/linux/input-event-codes.h        |    3 +
 include/uapi/linux/input.h                    |    1 +
 18 files changed, 5461 insertions(+), 697 deletions(-)
 create mode 100644 drivers/input/joystick/gip/Kconfig
 create mode 100644 drivers/input/joystick/gip/Makefile
 create mode 100644 drivers/input/joystick/gip/gip-arcade-stick.c
 create mode 100644 drivers/input/joystick/gip/gip-core.c
 create mode 100644 drivers/input/joystick/gip/gip-drivers.c
 create mode 100644 drivers/input/joystick/gip/gip-pdp-jaguar.c
 create mode 100644 drivers/input/joystick/gip/gip-security.c
 create mode 100644 drivers/input/joystick/gip/gip-wheel.c
 create mode 100644 drivers/input/joystick/gip/gip.h
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH v6 1/12 Input: xbox_gip - Add new driver for Xbox GIP

This introduces a new driver for the Xbox One/Series controller protocol, officially known as the Gaming Input Protocol, or GIP for short.

Microsoft released documentation on (some of) GIP in late 2024, upon which this driver is based. Though the documentation was incomplete, it still provided enough information to warrant a clean start over the previous, incomplete implementation.

This driver is already at feature parity with the GIP support in xpad, along with several more enhancements:

- Proper support for parsing message length and fragmented messages
- Metadata parsing, allowing for auto-detection on various parameters, including the presence and location in the message of the share button, as well as detection of specific device types

The framework set out in this driver also allows future expansion for specialized device types and additional features more cleanly than xpad.

Future plans include:

- Adding support for more device types, such as arcade sticks, racing wheels and flight sticks.
- Support for the security handshake, which is required for devices that use wireless dongles.
- Exposing a raw character device to enable sending vendor-specific commands from userspace.
- Event logging to either sysfs or dmesg.
- Support for the headphone jack.

---
 MAINTAINERS                              |    6 +
 drivers/input/joystick/Kconfig           |    2 +
 drivers/input/joystick/Makefile          |    1 +
 drivers/input/joystick/gip/Kconfig       |   21 +
 drivers/input/joystick/gip/Makefile      |    4 +
 drivers/input/joystick/gip/gip-core.c    | 2591 ++++++++++++++++++++++
 drivers/input/joystick/gip/gip-drivers.c |  204 ++
 drivers/input/joystick/gip/gip.h         |  344 +++
 8 files changed, 3173 insertions(+)
 create mode 100644 drivers/input/joystick/gip/Kconfig
 create mode 100644 drivers/input/joystick/gip/Makefile
 create mode 100644 drivers/input/joystick/gip/gip-core.c
 create mode 100644 drivers/input/joystick/gip/gip-drivers.c
 create mode 100644 drivers/input/joystick/gip/gip.h
MAINTAINERS +6 -0
index 806bd2d80d15..7fdf1b989667 100644
@@ -29347,6 +29347,12 @@ S:	Maintained
 F:	drivers/media/rc/keymaps/rc-xbox-dvd.c
 F:	drivers/media/rc/xbox_remote.c
 
+XBOX GIP
+M:	Vicki Pfau <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/input/joystick/gip/
+
 XC2028/3028 TUNER DRIVER
 M:	Mauro Carvalho Chehab <[email protected]>
 L:	[email protected]
drivers/input/joystick/Kconfig +2 -0
index 7755e5b454d2..d4665c80a371 100644
@@ -291,6 +291,8 @@ config JOYSTICK_JOYDUMP
 	  To compile this driver as a module, choose M here: the
 	  module will be called joydump.
 
+source "drivers/input/joystick/gip/Kconfig"
+
 config JOYSTICK_XPAD
 	tristate "Xbox gamepad support"
 	depends on USB_ARCH_HAS_HCD
drivers/input/joystick/Makefile +1 -0
index 9976f596a920..323392921b7d 100644
@@ -39,5 +39,6 @@ obj-$(CONFIG_JOYSTICK_TURBOGRAFX)	+= turbografx.o
 obj-$(CONFIG_JOYSTICK_TWIDJOY)		+= twidjoy.o
 obj-$(CONFIG_JOYSTICK_WARRIOR)		+= warrior.o
 obj-$(CONFIG_JOYSTICK_WALKERA0701)	+= walkera0701.o
+obj-$(CONFIG_JOYSTICK_XBOX_GIP)		+= gip/
 obj-$(CONFIG_JOYSTICK_XPAD)		+= xpad.o
 obj-$(CONFIG_JOYSTICK_ZHENHUA)		+= zhenhua.o
drivers/input/joystick/gip/Kconfig new file +21 -0
new file mode 100644
index 000000000000..2ce789d23178
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Gaming Input Protocol  driver configuration
+#
+config JOYSTICK_XBOX_GIP
+	tristate "Xbox One/Series controller support"
+	depends on USB && USB_ARCH_HAS_HCD
+	help
+	  Say Y here if you want to use Xbox One and Series controllers with your
+	  computer. Make sure to say Y to "Joystick support" (CONFIG_INPUT_JOYDEV)
+	  and/or "Event interface support" (CONFIG_INPUT_EVDEV) as well.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called xbox_gip.
+
+config JOYSTICK_XBOX_GIP_FF
+	bool "Xbox One/Series controller rumble support"
+	depends on JOYSTICK_XBOX_GIP && INPUT
+	select INPUT_FF_MEMLESS
+	help
+	  Say Y here if you want to take advantage of Xbox One/Series rumble.
drivers/input/joystick/gip/Makefile new file +4 -0
new file mode 100644
index 000000000000..d39f946db1ae
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+obj-$(CONFIG_JOYSTICK_XBOX_GIP)	+= xbox-gip.o
+# Base driver
+xbox-gip-y := gip-core.o gip-drivers.o
drivers/input/joystick/gip/gip-core.c new file +2591 -0

2,591 lines not on this page · raw message

drivers/input/joystick/gip/gip-drivers.c new file +204 -0

204 lines not on this page · raw message

drivers/input/joystick/gip/gip.h new file +344 -0

154 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.54.0

Part of the diff is not shown here. View the raw message.

#2 lore raw
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider: - [High] Incorrect teardown ordering in `gip_disconnect()` leaves pending `receive_message` work, causing…

Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
- [High] Incorrect teardown ordering in gip_disconnect() leaves pending receive_message work, causing a UAF.
- [High] Missing synchronous cancellation of out_fragments and security works during driver teardown.
- [High] Input devices are forcefully destroyed and recreated across system suspend/resume.
- [High] Use-After-Free in LED and Power Supply sysfs handlers due to devres lifetime mismatch.
- [High] URB input processing does not loop over packed messages and passes incorrect payload lengths.
- [High] Missing cancel_delayed_work_sync in gip_probe error path leads to use-after-free of attachment memory.
- [High] Functions returning negative error codes are declared with a bool return type, causing silent failures.
- [Medium] Unreachable cleanup code in gip_init_output error path leaks URBs and DMA buffers.
- [Medium] Devm-managed input_dev is leaked on dynamic device reset.
- [Medium] Concurrent modification of attachment->seq_vendor without holding attachment->lock.
- [Low] Mixing guard() scope-based cleanup with goto-based error handling in gip_send_raw_message. --

commit 28d6048d8c164794e1dada5c80d56b8de14979e0
Author: Vicki Pfau <[email protected]>

Input: xbox_gip - Add new driver for Xbox GIP

This patch introduces a new driver for the Xbox One/Series controller protocol, known as the Gaming Input Protocol. It implements proper support for message length parsing, fragmented messages, and metadata auto-detection. The driver is designed to allow for future expansion to specialized device types and additional features.

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
index 0e23811a6ecc1..0000000000000 100644

[ ... ]

drivers/input/joystick/gip/gip-core.c static uint8_t gip_sequence_next(struct gip_attachment *attachment, · line 393

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -391,5 +391,5 @@ static uint8_t gip_sequence_next(struct gip_attachment *attachment,
 		}
 	} else {
 		seq = attachment->seq_vendor++;

[Severity: Medium]
Can this modification to the shared sequence state race with other callers?

Calling gip_send_vendor_message() from softirq context, like in gip_play_effect(), increments attachment->seq_vendor without holding attachment->lock. This might race with other message senders executing in process context and cause corrupted sequence numbers.

[ ... ]

Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -533,5 +533,5 @@ static void gip_free_fragment(struct gip_out_fragment *fragment)
 {
 	cancel_delayed_work(&fragment->timeout);

[ ... ]

drivers/input/joystick/gip/gip-core.c static void gip_shutdown(struct gip_device *device) · line 3144

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -3142,5 +3142,5 @@ static void gip_shutdown(struct gip_device *device)
 
 		guard(mutex)(&attachment->lock);
 		gip_reset_attachment(attachment);

[Severity: High]
Does this non-synchronous cancellation allow the fragment timeout work to survive driver teardown?

Because gip_reset_attachment() invokes gip_free_fragment() while holding attachment->lock, a concurrently executing gip_out_fragment_timeout() will block trying to acquire the same lock.

Since cancel_delayed_work() does not wait for the blocked work to finish, the work item could resume after gip_disconnect() frees the attachment structure, leading to a use-after-free.

[ ... ]

Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -677,5 +677,5 @@ static int gip_send_raw_message(struct gip_attachment *attachment,
 
 	guard(spinlock_irqsave)(&attachment->device->message_lock);

[ ... ]

drivers/input/joystick/gip/gip-core.c

Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +0 -0
 	return rc;
 
 err_free_fragment:
 	if (fragment)
 		gip_free_fragment(fragment);
 	return rc;
 }

[Severity: Low]
Does this mix guard scope-based cleanup with goto error handling?

The cleanup subsystem guidelines recommend avoiding goto-based cleanup labels in the same function that uses guard declarations, as it creates confusing ownership and resource lifecycle semantics.

[ ... ]

Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -1151,5 +1151,5 @@ static int gip_parse_message_metadata(struct device *dev,
 
 static bool gip_parse_metadata(struct device *dev,
 	struct gip_metadata *metadata, const uint8_t *bytes, int num_bytes)
 {

[ ... ]

Quoted 2 lines
drivers/input/joystick/gip/gip-core.c +0 -0
 	if (offset >= num_bytes)
 		goto parse_err;

[ ... ]

drivers/input/joystick/gip/gip-core.c

Quoted 4 lines

parse_err:

drivers/input/joystick/gip/gip-core.c +0 -0
 	gip_metadata_free(dev, metadata);
 	return rc;
 }

[Severity: High]
Can this function return a false success on error conditions?

The function is declared with a bool return type but uses and returns negative error codes like -EINVAL and -ENOMEM. If the offset bounds check is hit, rc is exactly 0, causing the function to return false (0).

Upstream callers like gip_handle_command_hello_device will implicitly cast this to success, proceeding to use the freed metadata structure and risking out-of-bounds accesses.

[ ... ]

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -1388,5 +1388,5 @@ static int gip_guide_led_probe(struct gip_attachment *attachment, struct device
 		if (!rc)
 			rc = devm_led_classdev_multicolor_register(dev,
 				mc_cdev);

[ ... ]

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -1473,5 +1473,5 @@ static int gip_battery_create(struct gip_attachment *attachment, struct device *
 
 	attachment->battery = devm_power_supply_register(dev,
 		&attachment->battery_desc, &supply_config);

[ ... ]

drivers/input/joystick/gip/gip-core.c static void gip_disconnect(struct usb_interface *intf) · line 3186

Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -3183,5 +3183,5 @@ static void gip_disconnect(struct usb_interface *intf)
 
 		devm_kfree(to_gip_device(attachment), attachment->uniq);
 		devm_kfree(to_gip_device(attachment), attachment);
 	}

[Severity: High]
Is there a lifetime mismatch between the devres-managed sysfs interfaces and the attachment structure?

The battery and LED class devices are registered using the input device dev pointer as the devres owner. However, the attachment structure, which is used as the driver data for these callbacks, is explicitly freed via devm_kfree() in gip_disconnect().

If a userspace process holds an open file descriptor on the input event node, the input device memory and devres cleanup callbacks are deferred, leaving dangling pointers that can be dereferenced by userspace sysfs reads or writes.

[ ... ]

Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -1602,5 +1602,5 @@ int gip_setup_input_device(struct gip_attachment *attachment)
 		return 0;
 
 	input = devm_input_allocate_device(to_gip_device(attachment));
 	if (!input)

[ ... ]

drivers/input/joystick/gip/gip-core.c static void gip_free_devices(struct gip_attachment *attachment) · line 1863

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -1861,5 +1861,5 @@ static void gip_free_devices(struct gip_attachment *attachment)
 
 	if (input)
 		input_unregister_device(input);

[Severity: Medium]
Will this leak the devm-managed input device during a dynamic reset?

The input device is allocated with devm_input_allocate_device(), tying its memory to the USB interface. When the device is reset dynamically, such as upon receiving a new hello message, gip_reset_attachment() calls gip_free_devices() and unregisters the device.

Because the memory is devm-managed, it is not freed until the parent USB device unbinds, resulting in a memory leak on every internal reset.

[Severity: High]
Also, does this forcefully destroy and recreate input devices across system suspend?

When the system suspends, gip_suspend() calls gip_shutdown(), which invokes gip_reset_attachment() and gip_free_devices(). This unregisters the input device, completely destroying the node.

Applications will receive an EPOLLHUP and lose the controller permanently until restarted. Standard input drivers typically persist the input device across suspend and only pause I/O.

[ ... ]

Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -1902,5 +1902,5 @@ static void gip_reset_attachment(struct gip_attachment *attachment)
 	cancel_delayed_work(&attachment->metadata_next);
 	cancel_delayed_work(&attachment->in_fragment_timeout);

[ ... ]

Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -3108,5 +3108,5 @@ static int gip_probe(struct usb_interface *intf, const struct usb_device_id *id)
 	if (rc) {
 		gip_reset_attachment(attachment);
 		gip_reset_metadata(attachment);
 
 		devm_kfree(to_gip_device(attachment), attachment->uniq);

devm_kfree(to_gip_device(attachment), attachment);

[Severity: High]
Does this non-synchronous cancellation in the probe error path allow the metadata work item to access freed memory?

If gip_interface_init() fails, gip_reset_attachment() uses cancel_delayed_work() for metadata_next, and then the attachment memory is immediately freed by devm_kfree().

If the delayed work was scheduled, such as for devices with the GIP_QUIRK_NO_HELLO quirk, it could execute concurrently and access the freed attachment structure.

[ ... ]

drivers/input/joystick/gip/gip-core.c

Quoted 8 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -2801,5 +2801,5 @@ static int gip_receive_message(struct gip_device *device, uint8_t *bytes,
 		guard(mutex)(&attachment->lock);
 		num_bytes -= offset;
 		bytes += offset;
 		fragment_offset = header.length;
 		if (header.flags & GIP_FLAG_ACME)
drivers/input/joystick/gip/gip-core.c +0 -0
 			gip_acknowledge(attachment, &header, fragment_offset, bytes_remaining);
 		rc = gip_handle_message(attachment, &header, bytes, num_bytes);

[Severity: High]
Does this code fail to handle multiple packed messages and pass incorrect payload lengths?

For non-fragmented messages, the function passes the entire remaining buffer size (num_bytes) to gip_handle_message() instead of the actual payload size defined by header.length. It also lacks a loop to process subsequent messages in the transfer.

Because Xbox controllers often pack multiple messages or include trailing padding, strict length validation checks in downstream handlers will fail, causing valid messages to be dropped.

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_init_output(struct gip_interface *intf,

Quoted 8 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -2977,6 +2977,5 @@ static int gip_init_output(struct gip_interface *intf,
 		intf->out_queue[i].data = usb_alloc_coherent(udev, intf->mtu, GFP_KERNEL,
 			&intf->out_queue[i].urb->transfer_dma);
 
 		if (!intf->out_queue[i].data) {
 			return -ENOMEM;
 			goto err_free_urbs;
 		}

[Severity: Medium]
Will this unreachable cleanup code leak URBs and DMA buffers on failure?

The return statement is placed immediately before the goto err_free_urbs label, making the cleanup path completely unreachable if the coherent allocation fails.

[ ... ]

drivers/input/joystick/gip/gip-core.c static void gip_disconnect(struct usb_interface *intf) · line 3161

Quoted 6 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -3157,5 +3157,5 @@ static void gip_disconnect(struct usb_interface *intf)
 		return;
 
 	gip_shutdown(gip);
 
 	usb_kill_urb(gip->data.urb_in);

[Severity: High]
Does the teardown ordering here leave a window for a use-after-free?

Calling gip_shutdown() invokes cancel_work_sync(&device->receive_message). However, usb_kill_urb() is not called until after gip_shutdown() returns.

In that window, the hardware can complete a pending URB, triggering gip_urb_in(), which will successfully reschedule the receive_message work item. After gip_disconnect() completes and frees the gip_device structure, the pending work will execute and dereference the freed memory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH v6 2/12 Input: xpad - Remove Xbox One support

In reply to Vicki Pfau

It has been superseded by xbox_gip. As the new driver is already at feature parity, removing the fairly rough Xbox One support from xpad is safe and will prevent any potential conflicts.

---
 Documentation/input/devices/xpad.rst |  17 +-
 drivers/input/joystick/xpad.c        | 684 +--------------------------
 2 files changed, 11 insertions(+), 690 deletions(-)
Documentation/input/devices/xpad.rst +7 -10
index a480bc781565..fd2afdd7b405 100644
@@ -2,19 +2,19 @@
 xpad - Linux USB driver for Xbox compatible controllers
 =======================================================
 
-This driver exposes all first-party and third-party Xbox compatible
-controllers. It has a long history and has enjoyed considerable usage
-as Windows' xinput library caused most PC games to focus on Xbox
-controller compatibility.
+This driver exposes all first-party and third-party Xbox and Xbox 360
+compatible controllers. It has a long history and has enjoyed considerable
+usage as Windows' xinput library caused most PC games to focus on Xbox
+controller compatibility. Xbox One/Series controller support has been
+superseded by the xbox_gip driver, which specializes in the Gaming Input
+Protocl that is introduced on the Xbox One.
 
 Due to backwards compatibility all buttons are reported as digital.
 This only affects Original Xbox controllers. All later controller models
 have only digital face buttons.
 
 Rumble is supported on some models of Xbox 360 controllers but not of
-Original Xbox controllers nor on Xbox One controllers. As of writing
-the Xbox One's rumble protocol has not been reverse-engineered but in
-the future could be supported.
+Original Xbox controllers.
 
 
 Notes
@@ -98,9 +98,6 @@ All generations of Xbox controllers speak USB over the wire.
 - Wireless Xbox 360 controllers require a 'Xbox 360 Wireless Gaming Receiver
   for Windows'
 - Wired Xbox 360 controllers use standard USB connectors.
-- Xbox One controllers can be wireless but speak Wi-Fi Direct and are not
-  yet supported.
-- Xbox One controllers can be wired and use standard Micro-USB connectors.
 
 
 
drivers/input/joystick/xpad.c +4 -680

1,024 lines not on this page · raw message

Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH v6 02/12] Input: xpad - Remove Xbox One support

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] The `flags` field in `struct xpad_device` is now unused and should be removed. - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] The flags field in struct xpad_device is now unused and should be removed.
- [Low] Documentation Typo: 'Protocl' instead of 'Protocol'.
--

commit 30879d88695dc1f5b4aa4e33ed67c80e554e4b56
Author: Vicki Pfau <[email protected]>

Input: xpad - Remove Xbox One support

This commit removes Xbox One controller support from the xpad driver. It notes that this functionality is fully superseded by the xbox_gip driver, which specializes in the Xbox One Gaming Input Protocol.

Quoted 8 lines
Documentation/input/devices/xpad.rst +0 -0
index a480bc781565e..fd2afdd7b4059 100644
@@ -2,19 +2,19 @@
 xpad - Linux USB driver for Xbox compatible controllers
 =======================================================
 

Documentation/input/devices/xpad.rst line 10

Quoted 10 lines
Documentation/input/devices/xpad.rst +6 -4
-This driver exposes all first-party and third-party Xbox compatible
-controllers. It has a long history and has enjoyed considerable usage
-as Windows' xinput library caused most PC games to focus on Xbox
-controller compatibility.
+This driver exposes all first-party and third-party Xbox and Xbox 360
+compatible controllers. It has a long history and has enjoyed considerable
+usage as Windows' xinput library caused most PC games to focus on Xbox
+controller compatibility. Xbox One/Series controller support has been
+superseded by the xbox_gip driver, which specializes in the Gaming Input
+Protocl that is introduced on the Xbox One.

[Severity: Low]
This isn't a bug, but there's a typo in the documentation: Protocl instead of Protocol.

Quoted 4 lines
drivers/input/joystick/xpad.c +0 -0
index feb8f368f834e..206007e0cafb6 100644

[ ... ]

drivers/input/joystick/xpad.c static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id · line 1466

Quoted 9 lines
drivers/input/joystick/xpad.c +0 -3
@@ -2080,18 +1462,13 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 	xpad->mapping = xpad_device[i].mapping;
 	xpad->xtype = xpad_device[i].xtype;
 	xpad->name = xpad_device[i].name;
-	if (xpad_device[i].flags & FLAG_DELAY_INIT)
-		xpad->delay_init = true;
 
-	xpad->packet_type = PKT_XB;
 	INIT_WORK(&xpad->work, xpad_presence_work);

[Severity: Low]
Since the only flag (FLAG_DELAY_INIT) and its usage here in xpad_probe() have been removed, is it intended to leave the flags field inside the definition of struct xpad_device?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#5 lore raw

PATCH v6 3/12 Input: xbox_gip - Add controllable LED support

In reply to Vicki Pfau

Xbox One controllers have two different types of controllable LED support:

- Monochrome white, which most controllers have.
- RGBW addressible, which the Elite 2 controllers have.

This exposes both types as led cdevs.

---
 drivers/input/joystick/gip/Kconfig    |   2 +-
 drivers/input/joystick/gip/gip-core.c | 115 ++++++++++++++++++++++++++
 drivers/input/joystick/gip/gip.h      |   6 +-
 3 files changed, 121 insertions(+), 2 deletions(-)
drivers/input/joystick/gip/Kconfig +1 -1
index 2ce789d23178..3422bf84cb89 100644
@@ -4,7 +4,7 @@
 #
 config JOYSTICK_XBOX_GIP
 	tristate "Xbox One/Series controller support"
-	depends on USB && USB_ARCH_HAS_HCD
+	depends on USB && USB_ARCH_HAS_HCD && LEDS_CLASS_MULTICOLOR
 	help
 	  Say Y here if you want to use Xbox One and Series controllers with your
 	  computer. Make sure to say Y to "Joystick support" (CONFIG_INPUT_JOYDEV)
drivers/input/joystick/gip/gip-core.c +115 -0

130 lines not on this page · raw message

drivers/input/joystick/gip/gip.h +5 -1
index 605149834269..69eef001d91f 100644
@@ -12,6 +12,7 @@
 #ifndef _GIP_H
 #define _GIP_H
 
+#include <linux/led-class-multicolor.h>
 #include <linux/rcupdate.h>
 #include <linux/usb/input.h>
 
@@ -234,6 +235,10 @@ struct gip_attachment {
 	uint8_t seq_vendor;
 
 	uint8_t device_state;
+	union {
+		struct led_classdev standard;
+		struct led_classdev_mc color;
+	} guide_led;
 
 	struct gip_extended_status status;
 
@@ -245,7 +250,6 @@ struct gip_attachment {
 	int extra_axes;
 
 	bool dpad_as_buttons;
-	struct hid_device __rcu *hdev;
 };
 
 struct gip_urb {
Signature · 1 line
2.54.0
#6 lore raw

Re: [PATCH v6 03/12] Input: xbox_gip - Add controllable LED support

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Registering a managed LED classdev against an input device creates a circular dependency, leading to memory leaks, list corruption, and kernel crashes on device reconnect.

Pre-existing issues:
- [High] Repeatedly calling devm_input_allocate_device() against the USB interface without unbinding leaks input_dev structures on every device reconnect. --

commit 170b2e9725cebf9b1d26ed7fb1bf1a2b52d452c6
Author: Vicki Pfau <[email protected]>
Input: xbox_gip - Add controllable LED support

This commit adds controllable LED support for Xbox One controllers. It exposes both monochrome white and RGBW addressable LEDs as led class devices depending on the controller capabilities.

drivers/input/joystick/gip/gip.h struct gip_attachment { · line 241

Quoted 12 lines
drivers/input/joystick/gip/gip.h +4 -0
index 605149834269f..69eef001d91ff 100644
@@ -234,6 +235,10 @@ struct gip_attachment {
 	uint8_t seq_vendor;
 
 	uint8_t device_state;
+	union {
+		struct led_classdev standard;
+		struct led_classdev_mc color;
+	} guide_led;

[Severity: High]
Does embedding the led_classdev directly inside the long-lived gip_attachment structure risk list corruption?

If the input device is reset and re-probed (for example, via a reset message), the driver recreates the device and could call devm_led_classdev_register() again on the exact same already-registered led_classdev struct. This would corrupt the global leds_list and cause a kernel crash.

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
index 0e23811a6ecc1..a5ed5f3bf3fed 100644

[ ... ]

Quoted 2 lines
drivers/input/joystick/gip/gip-core.c +2 -0
+static int gip_guide_led_probe(struct gip_attachment *attachment, struct device *dev)
+{

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_send_guide_button_led(struct gip_attachment *attachment, · line 1136

Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +3 -0
+		if (!rc)
+			rc = devm_led_classdev_multicolor_register(dev,
+				mc_cdev);

[Severity: High]
Can this create a circular dependency?

By using devm_led_classdev_multicolor_register() in gip_guide_led_probe() with the input_dev as the parent device, the LED child device takes a reference to the input_dev. Since input_dev devres actions only execute when its reference count drops to zero, the LED device is never automatically unregistered, creating a circular reference and leaking memory.

[ ... ]

Quoted 2 lines
drivers/input/joystick/gip/gip-core.c +2 -0
+	} else {
+		struct led_classdev *cdev = &attachment->guide_led.standard;

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_send_guide_button_led(struct gip_attachment *attachment, · line 1152

Quoted 1 line
drivers/input/joystick/gip/gip-core.c +1 -0
+		rc = devm_led_classdev_register(dev, cdev);

[Severity: High]
Similarly, does registering the standard LED classdev against the input device here create the same circular dependency preventing automatic unregistration?

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_setup_input_device(struct gip_attachment *attachment) · line 1275

Quoted 9 lines
drivers/input/joystick/gip/gip-core.c +4 -0
@@ -1157,6 +1268,10 @@ static int gip_setup_input_device(struct gip_attachment *attachment)
 	if (rc)
 		goto err_free_device;
 
+	rc = gip_guide_led_probe(attachment, &input->dev);
+	if (rc)
+		gip_err(attachment, "Failed to register LEDs: %d\n", rc);
+
 	return 0;

[Severity: High]
This isn't a bug introduced by this patch, but does the allocation of the input device earlier in gip_setup_input_device() leak input_dev structures on every reconnect?

The code uses devm_input_allocate_device() with the long-lived USB interface as the parent. When the controller resets (by sending a reset packet), the driver correctly calls input_unregister_device(), but the devm free action remains attached to the USB interface's devres list. A new input_dev is then allocated for the re-initialization, leaking an entire input_dev structure each time until the USB dongle is physically unplugged.

[Severity: High]
Also, if a controller reset causes gip_setup_input_device() to run again, does calling gip_guide_led_probe() register the LEDs a second time without ensuring the previous LED instances were unregistered?

Combined with embedding the led_classdev inside the attachment, this seems like it would trigger list corruption.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#7 lore raw

PATCH v6 4/12 Input: xbox_gip - Add HID relaying

In reply to Vicki Pfau

GIP allows tunneling of HID packets, with the HID descriptor embedded in the GIP metadata exchanged during the initial handshake. This patch creates a hid_device for this HID descriptor if found, as well as relaying the HID packets.

---
 drivers/input/joystick/gip/gip-core.c | 115 +++++++++++++++++++++++++-
 drivers/input/joystick/gip/gip.h      |   2 +
 include/uapi/linux/input.h            |   1 +
 3 files changed, 115 insertions(+), 3 deletions(-)
drivers/input/joystick/gip/gip-core.c +112 -3
index a5ed5f3bf3fe..dc174560ee49 100644
@@ -560,6 +560,54 @@ int gip_send_vendor_message(struct gip_attachment *attachment,
 		bytes, num_bytes);
 }
 
+static int gip_hid_ll_parse(struct hid_device *hdev)
+{
+	struct gip_attachment *attachment = hdev->driver_data;
+
+	return hid_parse_report(hdev,
+		attachment->metadata.device.hid_descriptor,
+		attachment->metadata.device.hid_descriptor_size);
+}
+
+static int gip_hid_ll_start(struct hid_device *hdev)
+{
+	return 0;
+}
+
+static void gip_hid_ll_stop(struct hid_device *hdev)
+{
+}
+
+static int gip_hid_ll_open(struct hid_device *hdev)
+{
+	return 0;
+}
+
+static void gip_hid_ll_close(struct hid_device *hdev)
+{
+}
+
+static int gip_hid_ll_raw_request(struct hid_device *hdev,
+	unsigned char reportnum, uint8_t *buf, size_t count,
+	unsigned char report_type, int reqtype)
+{
+	/*
+	 * TODO: Based on the metadata, output reports appear to be possible,
+	 * but the chatpad doesn't have the LEDs it claims to support, so
+	 * it's not clear how to test we're sending them properly.
+	 */
+	return 0;
+}
+
+static const struct hid_ll_driver gip_hid_ll_driver = {
+	.parse = gip_hid_ll_parse,
+	.start = gip_hid_ll_start,
+	.stop = gip_hid_ll_stop,
+	.open = gip_hid_ll_open,
+	.close = gip_hid_ll_close,
+	.raw_request = gip_hid_ll_raw_request,
+};
+
 static void gip_metadata_free(struct device *dev, struct gip_metadata *metadata)
 {
 	devm_kfree(dev, metadata->device.audio_formats);
@@ -1310,6 +1358,50 @@ static int gip_init_input_device(struct gip_attachment *attachment)
 	return rc;
 }
 
+static int gip_create_hdev(struct gip_attachment *attachment)
+{
+	struct hid_device *hdev;
+	int rc;
+
+	if (!attachment->metadata.device.hid_descriptor)
+		return 0;
+
+	rcu_read_lock();
+	hdev = rcu_dereference(attachment->hdev);
+	rcu_read_unlock();
+	if (hdev)
+		return 0;
+
+	hdev = hid_allocate_device();
+
+	if (IS_ERR(hdev))
+		return PTR_ERR(hdev);
+
+	hdev->ll_driver = &gip_hid_ll_driver;
+	hdev->bus = BUS_GIP;
+	hdev->vendor = attachment->vendor_id;
+	hdev->product = attachment->product_id;
+	hdev->version = 0x0100;
+	hdev->country = 0;
+	hdev->dev.parent = to_gip_device(attachment);
+	hdev->driver_data = attachment;
+	if (attachment->name)
+		strscpy(hdev->name, attachment->name);
+	else
+		strscpy(hdev->name, "Xbox Chatpad");
+	strscpy(hdev->phys, attachment->phys);
+	rc = hid_add_device(hdev);
+	if (rc) {
+		gip_err(attachment, "HID device add failed: %d\n", rc);
+		hid_destroy_device(hdev);
+	} else {
+		rcu_assign_pointer(attachment->hdev, hdev);
+		synchronize_rcu();
+	}
+
+	return rc;
+}
+
 static int gip_send_init_sequence(struct gip_attachment *attachment)
 {
 	int rc = 0;
@@ -1375,7 +1467,11 @@ static int gip_send_init_sequence(struct gip_attachment *attachment)
 	if (rc)
 		return rc;
 
-	return 0;
+	rc = gip_create_hdev(attachment);
+	if (rc)
+		return rc;
+
+	return rc;
 }
 
 static void gip_fragment_timeout(struct work_struct *work)
@@ -1393,19 +1489,25 @@ static void gip_fragment_timeout(struct work_struct *work)
 static void gip_free_devices(struct gip_attachment *attachment)
 {
 	struct input_dev *input;
+	struct hid_device *hdev;
 
 	if (attachment->driver && attachment->driver->remove)
 		attachment->driver->remove(attachment);
 
 	rcu_read_lock();
 	input = rcu_dereference(attachment->input);
+	hdev = rcu_dereference(attachment->hdev);
 	rcu_read_unlock();
 
 	rcu_assign_pointer(attachment->input, NULL);
+	rcu_assign_pointer(attachment->hdev, NULL);
 	synchronize_rcu();
 
 	if (input)
 		input_unregister_device(input);
+
+	if (hdev)
+		hid_destroy_device(hdev);
 }
 
 static void gip_reset_metadata(struct gip_attachment *attachment)
@@ -1770,9 +1872,16 @@ static int gip_handle_command_firmware(struct gip_attachment *attachment,
 static int gip_handle_command_hid_report(struct gip_attachment *attachment,
 	const struct gip_header *header, uint8_t *bytes, int num_bytes)
 {
-	gip_warn(attachment, "Unimplemented HID report message\n");
+	struct hid_device *hdev;
 
-	return -EOPNOTSUPP;
+	guard(rcu)();
+	hdev = rcu_dereference(attachment->hdev);
+	if (hdev)
+		return hid_input_report(hdev, HID_INPUT_REPORT, bytes, num_bytes, true);
+
+	gip_warn(attachment, "Got HID report with no HID descriptor\n");
+
+	return -EPROTO;
 }
 
 static int gip_handle_command_extended(struct gip_attachment *attachment,
drivers/input/joystick/gip/gip.h +2 -0
index 69eef001d91f..e2571fb43aea 100644
@@ -12,6 +12,7 @@
 #ifndef _GIP_H
 #define _GIP_H
 
+#include <linux/hid.h>
 #include <linux/led-class-multicolor.h>
 #include <linux/rcupdate.h>
 #include <linux/usb/input.h>
@@ -250,6 +251,7 @@ struct gip_attachment {
 	int extra_axes;
 
 	bool dpad_as_buttons;
+	struct hid_device __rcu *hdev;
 };
 
 struct gip_urb {
include/uapi/linux/input.h +1 -0
index 6aa703fcfcfb..d9c630ff8292 100644
@@ -276,6 +276,7 @@ struct input_mask {
 #define BUS_INTEL_ISHTP		0x1F
 #define BUS_AMD_SFH		0x20
 #define BUS_SDW			0x21
+#define BUS_GIP			0x22
 
 /*
  * MT_TOOL types
Signature · 1 line
2.54.0
#8 lore raw

Re: [PATCH v6 04/12] Input: xbox_gip - Add HID relaying

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

New issues:
- [High] gip_handle_command_hid_report() uses hid_input_report() instead of hid_safe_input_report(), bypassing buffer size checks.
- [Medium] gip_create_hdev() contains a redundant synchronize_rcu() call after publishing the newly allocated hdev pointer.
- [Medium] The hid_device's uniq field is not initialized before hid_add_device() is called.
- [Low] gip_free_devices() leaks the RCU-protected hdev pointer outside the read-side critical section, violating the RCU API contract.

Pre-existing issues:
- [Critical] gip_disconnect() cancels the receive workqueue before killing the active URB, introducing a race condition that causes Use-After-Free of the gip_device.
- [Critical] The LED classdev is embedded in the attachment but registered using the devres of the ephemeral input_dev, leading to list corruption upon device reset.
- [High] Resetting the attachment leaks input_dev structures because devm_input_allocate_device() is used but the memory is never explicitly freed before reallocation. --

commit 4a45548f46c49c18ea22bd85490b3cf588327135
Author: Vicki Pfau <[email protected]>

Input: xbox_gip - Add HID relaying

GIP allows tunneling of HID packets, with the HID descriptor embedded in the GIP metadata exchanged during the initial handshake. This patch creates a hid_device for this HID descriptor if found, as well as relaying the HID packets.

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
index a5ed5f3bf3fed..dc174560ee49f 100644

[ ... ]

Quoted 9 lines
drivers/input/joystick/gip/gip-core.c +5 -0
@@ -1310,6 +1358,50 @@ static int gip_init_input_device(struct gip_attachment *attachment)
 	return rc;
 }
 
+static int gip_create_hdev(struct gip_attachment *attachment)
+{
+	struct hid_device *hdev;
+	int rc;
+

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_init_input_device(struct gip_attachment *attachment) · line 1392

Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +7 -0
+	hdev->dev.parent = to_gip_device(attachment);
+	hdev->driver_data = attachment;
+	if (attachment->name)
+		strscpy(hdev->name, attachment->name);
+	else
+		strscpy(hdev->name, "Xbox Chatpad");
+	strscpy(hdev->phys, attachment->phys);

[Severity: Medium]
Does this code forget to initialize hdev->uniq?

The HID subsystem documentation explicitly mandates that transport drivers fully initialize fields including uniq before calling hid_add_device(). While the input_dev correctly populates this, it appears to be omitted here, which leaves the device missing standard sysfs attributes.

drivers/input/joystick/gip/gip-core.c static int gip_init_input_device(struct gip_attachment *attachment) · line 1399

Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +7 -0
+	rc = hid_add_device(hdev);
+	if (rc) {
+		gip_err(attachment, "HID device add failed: %d\n", rc);
+		hid_destroy_device(hdev);
+	} else {
+		rcu_assign_pointer(attachment->hdev, hdev);
+		synchronize_rcu();

[Severity: Medium]
Is it necessary to block the thread for an RCU grace period here?

Since rcu_assign_pointer() publishes the newly allocated hdev pointer (replacing NULL or a synchronized teardown state), and no memory is being reclaimed at this point, this synchronize_rcu() call appears completely redundant and needlessly delays device initialization.

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +4 -0
+	}
+
+	return rc;
+}

[ ... ]

Quoted 13 lines
drivers/input/joystick/gip/gip-core.c +2 -0
@@ -1393,19 +1489,25 @@ static void gip_fragment_timeout(struct work_struct *work)
 static void gip_free_devices(struct gip_attachment *attachment)
 {
 	struct input_dev *input;
+	struct hid_device *hdev;
 
 	if (attachment->driver && attachment->driver->remove)
 		attachment->driver->remove(attachment);
 
 	rcu_read_lock();
 	input = rcu_dereference(attachment->input);
+	hdev = rcu_dereference(attachment->hdev);
 	rcu_read_unlock();

drivers/input/joystick/gip/gip-core.c static void gip_fragment_timeout(struct work_struct *work) · line 1510

Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +4 -0
 
 	rcu_assign_pointer(attachment->input, NULL);
+	rcu_assign_pointer(attachment->hdev, NULL);
 	synchronize_rcu();
 
 	if (input)
 		input_unregister_device(input);
+
+	if (hdev)
+		hid_destroy_device(hdev);

[Severity: Low]
Can this leak the RCU-protected hdev pointer outside the read-side critical section?

The hdev pointer is extracted using rcu_dereference() under rcu_read_lock(), but it is utilized outside the critical section to destroy it. While this might be functionally safe here because the thread is the exclusive updater and synchronizes before destruction, it violates RCU API rules. Should this use rcu_access_pointer() or rcu_dereference_protected() instead?

Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +0 -0
 }
 
 static void gip_reset_metadata(struct gip_attachment *attachment)

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_handle_command_firmware(struct gip_attachment *attachment, · line 1880

Quoted 12 lines
drivers/input/joystick/gip/gip-core.c +5 -2
@@ -1770,9 +1872,16 @@ static int gip_handle_command_firmware(struct gip_attachment *attachment,
 static int gip_handle_command_hid_report(struct gip_attachment *attachment,
 	const struct gip_header *header, uint8_t *bytes, int num_bytes)
 {
-	gip_warn(attachment, "Unimplemented HID report message\n");
+	struct hid_device *hdev;
 
-	return -EOPNOTSUPP;
+	guard(rcu)();
+	hdev = rcu_dereference(attachment->hdev);
+	if (hdev)
+		return hid_input_report(hdev, HID_INPUT_REPORT, bytes, num_bytes, true);

[Severity: High]
Could this cause an out-of-bounds read in the HID core?

The raw byte array from untrusted hardware input is passed directly to hid_input_report() without providing the allocated buffer size. The HID subsystem explicitly directs transport drivers to use hid_safe_input_report() with the allocated buffer size to ensure proper boundary checks are performed against the report descriptor.

drivers/input/joystick/gip/gip-core.c static int gip_handle_command_firmware(struct gip_attachment *attachment, · line 1885

Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +4 -0
+
+	gip_warn(attachment, "Got HID report with no HID descriptor\n");
+
+	return -EPROTO;
 }

While reviewing this file, I also noticed a few pre-existing regressions elsewhere in the codebase:

Quoted 11 lines
drivers/input/joystick/gip/gip-core.c +0 -0
 static void gip_disconnect(struct usb_interface *intf)
 {
	struct gip_device *gip = usb_get_intfdata(intf);
	int i;

	if (!gip)
		return;

	gip_shutdown(gip);

	usb_kill_urb(gip->data.urb_in);

[Severity: Critical]
This is a pre-existing issue, but does this destruction order cause a use-after-free of the gip_device?

The cleanup function gip_shutdown() cancels the receive_message workqueue before usb_kill_urb() is called. If the urb_in completes concurrently after the workqueue is canceled but before the URB is killed, it will reschedule the receive_message work. This work then executes concurrently with the rest of gip_disconnect(), which frees the gip_device.

Quoted 15 lines

static int gip_setup_input_device(struct gip_attachment *attachment)

{
	struct input_dev *input;
	int rc;

	if (!attachment->driver || !attachment->driver->setup_input)
		return -ENODEV;

	rcu_read_lock();
	input = rcu_dereference(attachment->input);
	rcu_read_unlock();
	if (input)
		return 0;

	input = devm_input_allocate_device(to_gip_device(attachment));

[Severity: High]
This isn't a bug introduced by this patch, but are input_dev structures leaked when the attachment is reset?

The input_dev is allocated using devm_input_allocate_device(). During a protocol reset, input_unregister_device() is explicitly called, but this does not free the devres-managed memory until the parent USB interface unbinds. Re-allocating the device on subsequent resets appears to accumulate leaked structures in memory.

Quoted 13 lines

static int gip_guide_led_probe(struct gip_attachment *attachment, struct device *dev)

{
	int rc = 0;

	if (!gip_supports_system_message(attachment, GIP_CMD_LED, false))
		return 0;

	if (attachment->features & GIP_FEATURE_GUIDE_COLOR) {
		struct mc_subled *mc_led_info;
		struct led_classdev_mc *mc_cdev = &attachment->guide_led.color;
		struct led_classdev *cdev = &mc_cdev->led_cdev;

		mc_led_info = devm_kcalloc(dev, 4,

[ ... ]

Quoted 3 lines
		if (!rc)
			rc = devm_led_classdev_multicolor_register(dev,
				mc_cdev);

[Severity: Critical]
This is a pre-existing issue, but does tying the embedded LED classdev registration to the ephemeral input_dev lead to list corruption?

The led_classdev is embedded in the attachment structure, but it is registered using devm_led_classdev_register() tied to the input_dev's lifecycle. On reset, the input_dev is unregistered, but its devres cleanup is deferred until the last userspace reference is closed. If the driver reallocates the input_dev and re-registers the exact same embedded led_classdev structure before cleanup completes, it corrupts the LED subsystem's linked lists.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#9 lore raw

PATCH v6 5/12 Input: xbox_gip - Add battery support

In reply to Vicki Pfau

Controllers are required to give information about the battery in the controller, if present. This patch exposes that information as a power_supply device.

---
 drivers/input/joystick/gip/gip-core.c | 116 +++++++++++++++++++++++++-
 drivers/input/joystick/gip/gip.h      |   4 +
 2 files changed, 116 insertions(+), 4 deletions(-)
drivers/input/joystick/gip/gip-core.c +112 -4
index dc174560ee49..568877a4ce4b 100644
@@ -255,6 +255,13 @@ static const struct gip_audio_format gip_audio_format_table[MAX_GIP_AUDIO_FORMAT
 };
 
 
+static enum power_supply_property gip_battery_props[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
+	POWER_SUPPLY_PROP_SCOPE,
+	POWER_SUPPLY_PROP_STATUS,
+};
+
 static const struct gip_quirks base_quirks[] = {
 	{ GIP_VID_PDP, GIP_PID_PDP_ROCK_CANDY, 0, .quirks = GIP_QUIRK_NO_HELLO },
 
@@ -1206,6 +1213,97 @@ static int gip_guide_led_probe(struct gip_attachment *attachment, struct device
 	return rc;
 }
 
+static int gip_battery_get_property(struct power_supply *psy,
+	enum power_supply_property psp, union power_supply_propval *val)
+{
+	struct gip_attachment *attachment = power_supply_get_drvdata(psy);
+
+	guard(spinlock_irqsave)(&attachment->battery_lock);
+	switch (psp) {
+	case POWER_SUPPLY_PROP_PRESENT:
+		val->intval = attachment->status.base.battery_type != GIP_BATTERY_ABSENT;
+		break;
+	case POWER_SUPPLY_PROP_SCOPE:
+		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
+		break;
+	case POWER_SUPPLY_PROP_STATUS:
+		if (attachment->status.base.battery_type == GIP_BATTERY_ABSENT) {
+			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+		} else {
+			switch (attachment->status.base.charge) {
+			case GIP_CHARGING:
+				if (attachment->status.base.battery_level == GIP_BATTERY_FULL)
+					val->intval = POWER_SUPPLY_STATUS_FULL;
+				else
+					val->intval = POWER_SUPPLY_STATUS_CHARGING;
+				break;
+			case GIP_NOT_CHARGING:
+				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+				break;
+			case GIP_CHARGE_ERROR:
+			default:
+				val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
+				break;
+			}
+		}
+		break;
+	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
+		if (attachment->status.base.battery_type == GIP_BATTERY_ABSENT) {
+			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
+		} else {
+			switch (attachment->status.base.battery_level) {
+			case GIP_BATTERY_CRITICAL:
+				val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
+				break;
+			case GIP_BATTERY_LOW:
+				val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
+				break;
+			case GIP_BATTERY_MEDIUM:
+				val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
+				break;
+			case GIP_BATTERY_FULL:
+				val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
+				break;
+			default:
+				val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
+				break;
+			}
+			break;
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int gip_battery_create(struct gip_attachment *attachment, struct device *dev)
+{
+	struct power_supply_config supply_config = { .drv_data = attachment, };
+	int rc;
+
+	attachment->battery_desc.properties = gip_battery_props;
+	attachment->battery_desc.num_properties = ARRAY_SIZE(gip_battery_props);
+	attachment->battery_desc.get_property = gip_battery_get_property;
+	attachment->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
+	attachment->battery_desc.name = devm_kasprintf(dev, GFP_KERNEL, "gip-%s", dev_name(dev));
+
+	if (!attachment->battery_desc.name)
+		return -ENOMEM;
+
+	attachment->battery = devm_power_supply_register(dev,
+		&attachment->battery_desc, &supply_config);
+	if (IS_ERR(attachment->battery)) {
+		rc = PTR_ERR(attachment->battery);
+		attachment->battery = NULL;
+		devm_kfree(dev, attachment->battery_desc.name);
+		return rc;
+	}
+
+	power_supply_powers(attachment->battery, dev);
+	return 0;
+}
+
 static bool gip_send_set_device_state(struct gip_attachment *attachment, uint8_t state)
 {
 	uint8_t buffer[] = { state };
@@ -1319,6 +1417,9 @@ static int gip_setup_input_device(struct gip_attachment *attachment)
 	rc = gip_guide_led_probe(attachment, &input->dev);
 	if (rc)
 		gip_err(attachment, "Failed to register LEDs: %d\n", rc);
+	rc = gip_battery_create(attachment, &input->dev);
+	if (rc)
+		gip_err(attachment, "Failed to register battery: %d\n", rc);
 
 	return 0;
 
@@ -1501,6 +1602,8 @@ static void gip_free_devices(struct gip_attachment *attachment)
 
 	rcu_assign_pointer(attachment->input, NULL);
 	rcu_assign_pointer(attachment->hdev, NULL);
+	/* The following are freed by devres */
+	attachment->battery = NULL;
 	synchronize_rcu();
 
 	if (input)
@@ -1633,10 +1736,14 @@ static int gip_handle_command_status_device(struct gip_attachment *attachment,
 	if (num_bytes < 1)
 		return -EINVAL;
 
-	attachment->status.base.battery_level = bytes[0] & 3;
-	attachment->status.base.battery_type = (bytes[0] >> 2) & 3;
-	attachment->status.base.charge = (bytes[0] >> 4) & 3;
-	attachment->status.base.power_level = (bytes[0] >> 6) & 3;
+	scoped_guard(spinlock_irqsave, &attachment->battery_lock) {
+		attachment->status.base.battery_level = bytes[0] & 3;
+		attachment->status.base.battery_type = (bytes[0] >> 2) & 3;
+		attachment->status.base.charge = (bytes[0] >> 4) & 3;
+		attachment->status.base.power_level = (bytes[0] >> 6) & 3;
+	}
+	if (attachment->battery)
+		power_supply_changed(attachment->battery);
 
 	if (num_bytes >= 4) {
 		attachment->status.device_active = bytes[1] & 1;
@@ -2129,6 +2236,7 @@ static struct gip_attachment *gip_ensure_attachment(struct gip_device *device,
 		device->attachments[attachment_index] = attachment;
 
 		mutex_init(&attachment->lock);
+		spin_lock_init(&attachment->battery_lock);
 		INIT_DELAYED_WORK(&attachment->in_fragment_timeout, gip_fragment_timeout);
 		INIT_DELAYED_WORK(&attachment->metadata_next, gip_retry_metadata);
 
drivers/input/joystick/gip/gip.h +4 -0
index e2571fb43aea..1c6854540f08 100644
@@ -14,6 +14,7 @@
 
 #include <linux/hid.h>
 #include <linux/led-class-multicolor.h>
+#include <linux/power_supply.h>
 #include <linux/rcupdate.h>
 #include <linux/usb/input.h>
 
@@ -241,6 +242,9 @@ struct gip_attachment {
 		struct led_classdev_mc color;
 	} guide_led;
 
+	spinlock_t battery_lock;
+	struct power_supply *battery;
+	struct power_supply_desc battery_desc;
 	struct gip_extended_status status;
 
 	enum gip_elite_button_format xbe_format;
Signature · 1 line
2.54.0
#10 lore raw

Re: [PATCH v6 05/12] Input: xbox_gip - Add battery support

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Attaching power_supply devres to the input_dev creates a Use-After-Free due to lifecycle mismatch.

Pre-existing issues:
- [High] Preexisting Use-After-Free in LED registration due to lifecycle mismatch with input_dev.
- [High] Improper cleanup ordering in gip_disconnect allows receive_message work to run after structures are freed. --

commit fc70d38900f23e7e70d19f7c2913e3ee24cac40d
Author: Vicki Pfau <[email protected]>

Input: xbox_gip - Add battery support

Controllers are required to give information about the battery in the controller, if present. This patch exposes that information as a power_supply device.

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
index dc174560ee49f..568877a4ce4b4 100644

[ ... ]

Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +6 -0
@@ -1206,6 +1213,97 @@ static int gip_guide_led_probe(struct gip_attachment *attachment, struct device
 	return rc;
 }
 
+static int gip_battery_create(struct gip_attachment *attachment, struct device *dev)
+{
+	struct power_supply_config supply_config = { .drv_data = attachment, };
+	int rc;
+
+	attachment->battery_desc.properties = gip_battery_props;

drivers/input/joystick/gip/gip-core.c static int gip_guide_led_probe(struct gip_attachment *attachment, struct device · line 1231

Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +10 -0
+	attachment->battery_desc.num_properties = ARRAY_SIZE(gip_battery_props);
+	attachment->battery_desc.get_property = gip_battery_get_property;
+	attachment->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
+	attachment->battery_desc.name = devm_kasprintf(dev, GFP_KERNEL, "gip-%s", dev_name(dev));
+
+	if (!attachment->battery_desc.name)
+		return -ENOMEM;
+
+	attachment->battery = devm_power_supply_register(dev,
+		&attachment->battery_desc, &supply_config);

[Severity: High]
Does registering the power supply against the refcounted input_dev create a use-after-free risk?

Because input_dev is refcounted (e.g. via evdev), its destruction can be delayed by a user holding an open file descriptor. However, the attachment structure (passed as driver data) is freed synchronously upon device disconnect.

If the device is unplugged, attachment is freed, but the power supply remains registered until input_dev is closed. Reading the battery sysfs files during this window will dereference the freed attachment.

Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +10 -0
+	if (IS_ERR(attachment->battery)) {
+		rc = PTR_ERR(attachment->battery);
+		attachment->battery = NULL;
+		devm_kfree(dev, attachment->battery_desc.name);
+		return rc;
+	}
+
+	power_supply_powers(attachment->battery, dev);
+	return 0;
+}

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_setup_input_device(struct gip_attachment *attachment) · line 1417

Quoted 2 lines
drivers/input/joystick/gip/gip-core.c +0 -0
@@ -1319,6 +1417,9 @@ static int gip_setup_input_device(struct gip_attachment *attachment)
 	rc = gip_guide_led_probe(attachment, &input->dev);

[Severity: High]
This is a pre-existing issue, but does registering the LED devices against input->dev create a similar use-after-free risk?

In gip_guide_led_probe() in drivers/input/joystick/gip/gip-core.c, devm_led_classdev_register() (and the multicolor variant) use input->dev as the devres parent. Like the battery, the LED class devices will remain registered as long as the input_dev exists.

Since the underlying attachment structure is freed upon unplug, this results in a use-after-free when sysfs LED attributes are read or written after disconnection.

drivers/input/joystick/gip/gip-core.c static int gip_setup_input_device(struct gip_attachment *attachment) · line 1424

Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +3 -0
 	if (rc)
 		gip_err(attachment, "Failed to register LEDs: %d\n", rc);
+	rc = gip_battery_create(attachment, &input->dev);
+	if (rc)
+		gip_err(attachment, "Failed to register battery: %d\n", rc);
 
 	return 0;

[Severity: High]
This isn't a bug introduced by this patch, but could the cleanup ordering in gip_disconnect() allow the receive_message work to run after the gip structure is freed?

gip_disconnect() in drivers/input/joystick/gip/gip-core.c calls gip_shutdown() (which does cancel_work_sync(&device->receive_message)) before it calls usb_kill_urb(gip->data.urb_in):

drivers/input/joystick/gip/gip-core.c:gip_disconnect() {
    ...
    gip_shutdown(gip);
    ...
    usb_kill_urb(gip->data.urb_in);
    ...
}

If a USB completion interrupt arrives between cancel_work_sync() and usb_kill_urb(), gip_urb_in() will execute with a success status and put the work back on the queue:

drivers/input/joystick/gip/gip-core.c:gip_urb_in() {
    ...
    schedule_work(&gip->receive_message);
    ...
}

gip_disconnect() then frees the gip structure. When the workqueue runs, it will dereference the freed gip pointer. Can we reverse the order to cancel the work after killing the URBs?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#11 lore raw

PATCH v6 6/12 Input: xbox_gip - Allow sending fragmented and ACME messages

In reply to Vicki Pfau

GIP supports sending messages that exceed the length of the MTU using a fragmented message flow. It sits on top of a reliable message transmission flow, which uses the ACME flag to signal that a message must be ACKed if it is received properly. We already supported receiving and coalescing these messages, but the ability to send them wasn't yet present due to not needing it for basic usage. This patch adds it for future use with the security flow.

---
 drivers/input/joystick/gip/gip-core.c | 385 ++++++++++++++++++++++++--
 drivers/input/joystick/gip/gip.h      |  20 ++
 2 files changed, 388 insertions(+), 17 deletions(-)
drivers/input/joystick/gip/gip-core.c +368 -17
index 568877a4ce4b..f61b5f18b819 100644
@@ -8,7 +8,6 @@
  * - Audio device support
  * - Security packet handshake
  * - Event logging
- * - Sending fragmented messages
  * - Raw character device
  * - Wheel support
  * - Flight stick support
@@ -28,6 +27,7 @@
 #define GIP_WIRED_INTF_DATA 0
 #define GIP_WIRED_INTF_AUDIO 1
 
+#define GIP_DUAL_LENGTH 6
 #define MAX_MESSAGE_LENGTH 0x4000
 
 #define MAX_AUDIO_MESSAGES 9
@@ -40,6 +40,9 @@
 #define GIP_DATA_CLASS_SHIFT 5
 #define GIP_DATA_CLASS_MASK (7u << 5)
 
+#define GIP_ACME_RETRY_TIME	(HZ / 10)
+#define GIP_ACME_MAX_RETRIES	8
+
 /* Undocumented Elite 2 vendor messages */
 #define GIP_CMD_RAW_REPORT		0x0c
 #define GIP_CMD_GUIDE_COLOR		0x0e
@@ -437,6 +440,13 @@ static uint8_t gip_sequence_next(struct gip_attachment *attachment,
 	return seq;
 }
 
+static unsigned int gip_fragment_id(const struct gip_attachment *attachment,
+	const struct gip_out_fragment *fragment)
+{
+	return BIT(attachment->attachment_index * MAX_OUT_FRAGMENTS +
+		(unsigned int) (fragment - attachment->out_fragments));
+}
+
 static void gip_handle_quirks_array(struct gip_attachment *attachment,
 	const struct gip_quirks *quirks)
 {
@@ -479,6 +489,50 @@ static void gip_handle_quirks(struct gip_attachment *attachment)
 		gip_handle_quirks_array(attachment, attachment->driver->quirks);
 }
 
+static struct gip_out_fragment *gip_find_fragment(struct gip_attachment *attachment,
+	uint8_t message_type, uint8_t flags, uint8_t seq, bool strict)
+{
+	int i;
+
+	for (i = 0; i < MAX_OUT_FRAGMENTS; i++) {
+		if (!attachment->out_fragments[i].active)
+			continue;
+		if (attachment->out_fragments[i].message != message_type)
+			continue;
+		if ((attachment->out_fragments[i].flags ^ flags) & GIP_FLAG_SYSTEM)
+			continue;
+		if (attachment->out_fragments[i].seq != seq) {
+			if (strict)
+				continue;
+
+			/*
+			 * Found one with  the wrong sequence number. For some
+			 * reason the official driver allows this.
+			 */
+			gip_warn(attachment,
+				"Found fragment with different sequence number %02x, wanted %02x\n",
+				attachment->out_fragments[i].seq, seq);
+		}
+
+		return &attachment->out_fragments[i];
+	}
+	return NULL;
+}
+
+static void gip_free_fragment(struct gip_out_fragment *fragment)
+{
+	cancel_delayed_work(&fragment->timeout);
+	kfree(fragment->data);
+	fragment->data = NULL;
+	fragment->active = false;
+	fragment->message = 0;
+	fragment->flags = 0;
+	fragment->seq = 0;
+	fragment->retries = 0;
+	fragment->total_length = 0;
+	fragment->fragment_offset = 0;
+}
+
 static int gip_send_raw_message(struct gip_attachment *attachment,
 	uint8_t message_type, uint8_t flags, uint8_t seq, const uint8_t *bytes,
 	int num_bytes)
@@ -486,20 +540,25 @@ static int gip_send_raw_message(struct gip_attachment *attachment,
 	struct gip_interface *intf;
 	int offset = 3;
 	struct gip_urb *urb = NULL;
+	struct gip_out_fragment *fragment = NULL;
 	int i;
 	int rc = 0;
+	int mtu = gip_data_class_mtu[message_type >> GIP_DATA_CLASS_SHIFT];
+	int reduced_mtu = mtu - GIP_DUAL_LENGTH;
+	/*
+	 * This buffer size shouldn't need to be more than 6 (there should never
+	 * be more than 3 length bytes), but add enough extra to fit fully
+	 * extended bignums for 32 bit (5 bytes) + 16 bit (3 bytes) values to be
+	 * safe and then WARN_ON if we go over. If this ever happens it would be
+	 * due to driver bugs.
+	 */
+	uint8_t header[11];
 
 	if (num_bytes < 0) {
 		gip_warn(attachment, "Invalid message length %d\n", num_bytes);
 		return -EINVAL;
 	}
 
-	if (num_bytes + 6 > gip_data_class_mtu[message_type >> GIP_DATA_CLASS_SHIFT]) {
-		gip_err(attachment,
-			"Attempted to send a message that requires fragmenting, which is not yet supported.\n");
-		return -EOPNOTSUPP;
-	}
-
 	if ((message_type & GIP_DATA_CLASS_MASK) == GIP_DATA_CLASS_AUDIO)
 		intf = &attachment->device->audio;
 	else
@@ -511,6 +570,89 @@ static int gip_send_raw_message(struct gip_attachment *attachment,
 		return -EOPNOTSUPP;
 	}
 
+	header[0] = message_type;
+	header[2] = seq;
+
+	if (num_bytes > reduced_mtu ||
+		(flags & (GIP_FLAG_ACME | GIP_FLAG_FRAGMENT)) == GIP_FLAG_ACME) {
+		for (i = 0; i < MAX_OUT_FRAGMENTS; i++) {
+			if (attachment->out_fragments[i].active)
+				continue;
+
+			fragment = &attachment->out_fragments[i];
+			break;
+		}
+
+		if (!fragment) {
+			gip_err(attachment, "Fragmented message queue is full; dropping message\n");
+			return -EALREADY;
+		}
+
+		fragment->data = kmalloc(num_bytes, GFP_KERNEL);
+		if (!fragment->data)
+			return -ENOMEM;
+		fragment->active = true;
+		fragment->message = message_type;
+		fragment->flags = flags & GIP_FLAG_SYSTEM;
+		fragment->seq = seq;
+		fragment->total_length = num_bytes;
+		fragment->fragment_offset = 0;
+		memcpy(fragment->data, bytes, num_bytes);
+		bytes = fragment->data;
+
+		if (num_bytes > reduced_mtu) {
+			gip_dbg(attachment, "Starting new reliable message: %02x%02x%02x, total length %u\n",
+				message_type, flags, seq, num_bytes);
+			flags |= GIP_FLAG_INIT_FRAG | GIP_FLAG_FRAGMENT | GIP_FLAG_ACME;
+			fragment->flags |= GIP_FLAG_FRAGMENT;
+			/* The spec says to extend the value to fit in 3 bytes */
+			offset += gip_encode_length(reduced_mtu, &header[offset], sizeof(header));
+			if (offset == 4 && reduced_mtu < 0x80 && num_bytes < 0x80) {
+				header[3] |= 0x80;
+				header[4] = 0;
+				offset = 5;
+			}
+			offset += gip_encode_length(num_bytes, &header[offset],
+				sizeof(header) - offset);
+			WARN_ON(offset != GIP_DUAL_LENGTH);
+			num_bytes = mtu - offset;
+		} else {
+			offset += gip_encode_length(num_bytes, &header[offset],
+				sizeof(header) - offset);
+		}
+	} else if (flags & GIP_FLAG_FRAGMENT) {
+		fragment = gip_find_fragment(attachment, message_type, flags, seq, true);
+		if (!fragment) {
+			gip_err(attachment,
+				"Attempted to send message fragment with no associated message\n");
+			return -EINVAL;
+		}
+
+		num_bytes = min(reduced_mtu, fragment->total_length - fragment->fragment_offset);
+		offset += gip_encode_length(num_bytes, &header[offset],
+			sizeof(header) - offset);
+		if (offset == 4 && num_bytes < 0x80 && fragment->fragment_offset < 0x80) {
+			header[3] |= 0x80;
+			header[4] = 0;
+			offset = 5;
+		}
+		offset += gip_encode_length(fragment->fragment_offset, &header[offset],
+			sizeof(header) - offset);
+		WARN_ON(offset != GIP_DUAL_LENGTH);
+
+		if (num_bytes && fragment->fragment_offset + num_bytes == fragment->total_length) {
+			/* The final fragment in a fragmented message must be ACKed */
+			flags |= GIP_FLAG_ACME;
+			fragment->acked = false;
+		}
+		bytes = &fragment->data[fragment->fragment_offset];
+	} else if (num_bytes >= 0) {
+		offset += gip_encode_length(num_bytes, &header[offset],
+			sizeof(header) - offset);
+	}
+
+	header[1] = flags;
+
 	guard(spinlock_irqsave)(&attachment->device->message_lock);
 	for (i = 0; i < MAX_OUT_MESSAGES && !urb; i++) {
 		if (!intf->out_queue[i].urb)
@@ -520,17 +662,13 @@ static int gip_send_raw_message(struct gip_attachment *attachment,
 	}
 	if (!urb) {
 		gip_err(attachment, "Output queue is full; dropping message\n");
-		return -ENOSPC;
+		rc = -ENOSPC;
+		goto err_free_fragment;
 	}
-	urb->data[0] = message_type;
-	urb->data[1] = flags;
-	urb->data[2] = seq;
-	offset += gip_encode_length(num_bytes, &urb->data[offset],
-		sizeof(urb->data) - offset);
 
+	memcpy(urb->data, header, offset);
 	if (num_bytes > 0)
 		memcpy(&urb->data[offset], bytes, num_bytes);
-
 	num_bytes += offset;
 	urb->urb->transfer_buffer_length = num_bytes;
 
@@ -545,11 +683,106 @@ static int gip_send_raw_message(struct gip_attachment *attachment,
 			__func__, rc);
 		usb_unanchor_urb(urb->urb);
 		rc = -EIO;
+		goto err_free_fragment;
 	}
 
+	if (fragment) {
+		/*
+		 * Sending further fragments is handled in gip_urb_out if this fragment doesn't need
+		 * ACKing or in gip_handle_command_protocol_control if it does. The flag should also
+		 * be cleared if we're done sending this packet.
+		 */
+		if (fragment->total_length > reduced_mtu && !(flags & GIP_FLAG_ACME))
+			intf->has_pending_out |= gip_fragment_id(attachment, fragment);
+		else
+			intf->has_pending_out &= ~gip_fragment_id(attachment, fragment);
+		if (flags & GIP_FLAG_ACME)
+			schedule_delayed_work(&fragment->timeout, GIP_ACME_RETRY_TIME);
+	}
+
+	return rc;
+
+err_free_fragment:
+	if (fragment)
+		gip_free_fragment(fragment);
 	return rc;
 }
 
+static int gip_send_next_fragment(struct gip_attachment *attachment,
+	struct gip_out_fragment *fragment)
+{
+	struct gip_interface *intf;
+	int next_bytes;
+	int flags = GIP_FLAG_FRAGMENT | fragment->flags | attachment->attachment_index;
+	int mtu;
+	int rc;
+
+	mtu = gip_data_class_mtu[fragment->message >> GIP_DATA_CLASS_SHIFT] - GIP_DUAL_LENGTH;
+	if (fragment->fragment_offset == fragment->total_length) {
+		if ((fragment->message & GIP_DATA_CLASS_MASK) == GIP_DATA_CLASS_AUDIO)
+			intf = &attachment->device->audio;
+		else
+			intf = &attachment->device->data;
+
+		next_bytes = 0;
+		guard(spinlock_irqsave)(&intf->device->message_lock);
+		intf->has_pending_out &= ~gip_fragment_id(attachment, fragment);
+	} else if (fragment->fragment_offset + mtu >= fragment->total_length) {
+		next_bytes = fragment->total_length - fragment->fragment_offset;
+		flags |= GIP_FLAG_ACME;
+	} else {
+		next_bytes = mtu;
+	}
+
+	rc = gip_send_raw_message(attachment, fragment->message, flags,
+		fragment->seq, &fragment->data[fragment->fragment_offset],
+		next_bytes);
+
+	if (rc < 0)
+		return rc;
+
+	if (fragment->acked)
+		fragment->fragment_offset += next_bytes;
+	if (next_bytes == 0 && !(fragment->flags & GIP_FLAG_ACME)) {
+		/* Finished sending the message */
+		gip_free_fragment(fragment);
+	} else if (flags & GIP_FLAG_ACME) {
+		fragment->acked = false;
+		schedule_delayed_work(&fragment->timeout, GIP_ACME_RETRY_TIME);
+	}
+	return next_bytes;
+}
+
+static void gip_send_fragment_work(struct work_struct *work)
+{
+	struct gip_interface *intf = container_of(work, struct gip_interface, send_fragment);
+	struct gip_out_fragment *fragment;
+	unsigned int has_pending_out;
+	unsigned long flags;
+	int i, j;
+
+	spin_lock_irqsave(&intf->device->message_lock, flags);
+	has_pending_out = intf->has_pending_out;
+	spin_unlock_irqrestore(&intf->device->message_lock, flags);
+	for (i = 0; i < MAX_ATTACHMENTS; i++) {
+		struct gip_attachment *attachment = intf->device->attachments[i];
+
+		for (j = 0; j < MAX_OUT_FRAGMENTS; j++) {
+			/* Check for pending output fragments */
+			if (!(has_pending_out & BIT(i * MAX_OUT_FRAGMENTS + j)))
+				continue;
+
+			guard(mutex)(&attachment->lock);
+			fragment = &attachment->out_fragments[j];
+			if (!fragment->active)
+				continue;
+			if (!(fragment->flags & GIP_FLAG_FRAGMENT))
+				continue;
+			gip_send_next_fragment(attachment, fragment);
+		}
+	}
+}
+
 int gip_send_system_message(struct gip_attachment *attachment,
 	uint8_t message_type, uint8_t flags, const void *bytes, int num_bytes)
 {
@@ -1587,6 +1820,25 @@ static void gip_fragment_timeout(struct work_struct *work)
 	gip_free_in_fragment(attachment);
 }
 
+static void gip_out_fragment_timeout(struct work_struct *work)
+{
+	struct gip_out_fragment *fragment = container_of(to_delayed_work(work),
+		struct gip_out_fragment, timeout);
+	struct gip_attachment *attachment = fragment->attachment;
+
+	guard(mutex)(&attachment->lock);
+	if (!fragment->active || !fragment->data || fragment->acked)
+		return;
+
+	fragment->retries++;
+	if (fragment->retries >= GIP_ACME_MAX_RETRIES) {
+		gip_free_fragment(fragment);
+		gip_warn(attachment, "Reliable message transmission failed\n");
+		return;
+	}
+	gip_send_next_fragment(attachment, fragment);
+}
+
 static void gip_free_devices(struct gip_attachment *attachment)
 {
 	struct input_dev *input;
@@ -1657,21 +1909,102 @@ static int gip_ensure_metadata(struct gip_attachment *attachment)
 
 static void gip_reset_attachment(struct gip_attachment *attachment)
 {
+	int i;
+	int pending_out_bits =
+		BIT((attachment->attachment_index + 1) * MAX_OUT_FRAGMENTS) -
+		BIT(attachment->attachment_index * MAX_OUT_FRAGMENTS);
+	unsigned long flags;
+
 	cancel_delayed_work(&attachment->metadata_next);
 	cancel_delayed_work(&attachment->in_fragment_timeout);
 
 	if (attachment->in_fragment_data)
 		gip_free_in_fragment(attachment);
 
+	spin_lock_irqsave(&attachment->device->message_lock, flags);
+	attachment->device->data.has_pending_out &= ~pending_out_bits;
+	attachment->device->audio.has_pending_out &= ~pending_out_bits;
+	spin_unlock_irqrestore(&attachment->device->message_lock, flags);
+
+	for (i = 0; i < MAX_OUT_FRAGMENTS; i++) {
+		if (attachment->out_fragments[i].active)
+			gip_free_fragment(&attachment->out_fragments[i]);
+	}
+
 	gip_free_devices(attachment);
+
 }
 
 static int gip_handle_command_protocol_control(struct gip_attachment *attachment,
 	const struct gip_header *header, const uint8_t *bytes, int num_bytes)
 {
-	/* TODO */
-	gip_warn(attachment, "Unimplemented Protocol Control message\n");
-	return -EOPNOTSUPP;
+	const struct gip_protocol_control_ack *ack;
+	struct gip_out_fragment *fragment = NULL;
+	uint32_t fragment_offset;
+	uint16_t remaining_buffer;
+	bool ok = true;
+	int mtu;
+	int rc;
+
+	if (num_bytes < 1)
+		return -EINVAL;
+
+	if (bytes[0] != GIP_CONTROL_CODE_ACK) {
+		gip_warn(attachment, "Unimplemented Protocol Control code %i message\n", bytes[0]);
+		return -EOPNOTSUPP;
+	}
+
+	if (num_bytes < sizeof(*ack))
+		return -EINVAL;
+
+	ack = (const struct gip_protocol_control_ack *)bytes;
+
+	fragment = gip_find_fragment(attachment, ack->message_type, ack->flags,
+		header->sequence_id, false);
+	if (!fragment) {
+		gip_warn(attachment, "Received ACK for unknown message\n");
+		return -EINVAL;
+	}
+
+
+	fragment_offset = le32_to_cpu(ack->fragment_offset);
+	remaining_buffer = le16_to_cpu(ack->remaining_buffer);
+
+	if (fragment_offset > fragment->total_length)
+		ok = false;
+	mtu = gip_data_class_mtu[fragment->message >> GIP_DATA_CLASS_SHIFT] - GIP_DUAL_LENGTH;
+	if (!ok) {
+		gip_warn(attachment, "Received invalid buffer offset in ACK, "
+			"got offset=%u + remaining=%u, expected offset=%u + remaining=%u\n",
+			fragment_offset, remaining_buffer,
+			fragment->fragment_offset, fragment->total_length - fragment->fragment_offset);
+		rc = -EINVAL;
+		goto resend;
+	} else if ((fragment->flags & GIP_FLAG_FRAGMENT) &&
+		(fragment->fragment_offset > fragment_offset ||
+		fragment->fragment_offset + mtu < fragment_offset)) {
+		gip_warn(attachment, "Received unexpected buffer offset in ACK, "
+			"got offset=%u, expected offset=%u\n",
+			fragment_offset, fragment->fragment_offset + mtu);
+	}
+
+	fragment->acked = true;
+	fragment->retries = 0;
+	cancel_delayed_work(&fragment->timeout);
+	if (fragment->flags & GIP_FLAG_FRAGMENT) {
+		fragment->fragment_offset = fragment_offset;
+		rc = gip_send_next_fragment(attachment, fragment);
+		if (rc < 0)
+			return rc;
+	} else {
+		gip_free_fragment(fragment);
+	}
+
+	return 0;
+
+resend:
+	gip_send_next_fragment(attachment, fragment);
+	return rc;
 }
 
 static bool gip_handle_command_hello_device(struct gip_attachment *attachment,
@@ -2218,6 +2551,7 @@ static struct gip_attachment *gip_ensure_attachment(struct gip_device *device,
 	uint8_t attachment_index)
 {
 	struct gip_attachment *attachment = device->attachments[attachment_index];
+	int i;
 
 	if (!attachment) {
 		attachment = devm_kzalloc(to_gip_device(device), sizeof(*attachment), GFP_KERNEL);
@@ -2233,6 +2567,11 @@ static struct gip_attachment *gip_ensure_attachment(struct gip_device *device,
 			attachment->product_id = device->udev->descriptor.idProduct;
 		}
 
+		for (i = 0; i < MAX_OUT_FRAGMENTS; i++) {
+			attachment->out_fragments[i].attachment = attachment;
+			INIT_DELAYED_WORK(&attachment->out_fragments[i].timeout, gip_out_fragment_timeout);
+		}
+
 		device->attachments[attachment_index] = attachment;
 
 		mutex_init(&attachment->lock);
@@ -2542,6 +2881,7 @@ static void gip_urb_out(struct urb *urb)
 	switch (status) {
 	case 0:
 		/* success */
+		schedule_work(&intf->send_fragment);
 		break;
 
 	case -ECONNRESET:
@@ -2738,9 +3078,11 @@ static int gip_probe(struct usb_interface *intf, const struct usb_device_id *id)
 	gip->data.device = gip;
 	gip->data.intf = intf;
 	gip->data.mtu = BASE_GIP_MTU;
+	INIT_WORK(&gip->data.send_fragment, gip_send_fragment_work);
 	gip->audio.device = gip;
 	gip->audio.mtu = MAX_GIP_MTU;
 	gip->audio.isoc_messages = MAX_AUDIO_MESSAGES;
+	INIT_WORK(&gip->audio.send_fragment, gip_send_fragment_work);
 
 	INIT_WORK(&gip->receive_message, gip_receive_work);
 	spin_lock_init(&gip->message_lock);
@@ -2772,8 +3114,17 @@ static int gip_probe(struct usb_interface *intf, const struct usb_device_id *id)
 static void gip_shutdown(struct gip_device *device)
 {
 	int i;
+	unsigned long flags;
+
+	spin_lock_irqsave(&device->message_lock, flags);
+	device->data.has_pending_out = 0;
+	device->audio.has_pending_out = 0;
+	spin_unlock_irqrestore(&device->message_lock, flags);
 
 	cancel_work_sync(&device->receive_message);
+	cancel_work_sync(&device->data.send_fragment);
+	if (device->audio.intf)
+		cancel_work_sync(&device->audio.send_fragment);
 
 	for (i = 0; i < MAX_ATTACHMENTS; i++) {
 		struct gip_attachment *attachment = device->attachments[i];
drivers/input/joystick/gip/gip.h +20 -0
index 1c6854540f08..007edbb628b2 100644
@@ -25,6 +25,7 @@
 
 #define MAX_IN_MESSAGES 8
 #define MAX_OUT_MESSAGES 8
+#define MAX_OUT_FRAGMENTS 2
 
 #define GIP_VID_MICROSOFT	0x045e
 #define GIP_VID_PDP		0x0e6f
@@ -194,6 +195,20 @@ struct gip_extended_status {
 	struct gip_status_event events[5];
 };
 
+struct gip_out_fragment {
+	struct gip_attachment *attachment;
+	bool active;
+	bool acked;
+	uint8_t message;
+	uint8_t flags;
+	uint8_t seq;
+	uint8_t retries;
+	uint16_t total_length;
+	uint32_t fragment_offset;
+	uint8_t *data;
+	struct delayed_work timeout;
+};
+
 struct gip_attachment;
 typedef int (*gip_command_handler)(struct gip_attachment *a, const struct gip_header *header,
 		const uint8_t *bytes, int num_bytes);
@@ -222,6 +237,8 @@ struct gip_attachment {
 	struct delayed_work in_fragment_timeout;
 	int in_fragment_retries;
 
+	struct gip_out_fragment out_fragments[MAX_OUT_FRAGMENTS];
+
 	uint16_t firmware_major_version;
 	uint16_t firmware_minor_version;
 
@@ -275,6 +292,9 @@ struct gip_interface {
 
 	struct usb_anchor out_anchor;
 	struct gip_urb out_queue[MAX_OUT_MESSAGES];
+
+	struct work_struct send_fragment;
+	unsigned int has_pending_out;
 };
 
 struct gip_device {
Signature · 1 line
2.54.0
#12 lore raw

Re: [PATCH v6 06/12] Input: xbox_gip - Allow sending fragmented and ACME messages

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] Missing synchronous cancellation of `out_fragments[i].timeout` during device shutdown leads to…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] Missing synchronous cancellation of out_fragments[i].timeout during device shutdown leads to use-after-free.
- [High] Unprotected lockless access to out_fragments state causes data races, corruption, and potential Use-After-Free.
- [High] Infinite USB transmission loop if the first fragment of a message times out.
- [High] The acked field of gip_out_fragment is not cleared on slot reuse, breaking reliable retries.
- [Medium] Stale has_pending_out bit in error path causes concurrent chunk transmissions.
- [Low] Mixing goto error handling with guard() cleanup violates subsystem guidelines.
--

commit a080933dee9387ce07bf54626ee39a087371ba35
Author: Vicki Pfau <[email protected]>
Subject: Input: xbox_gip - Allow sending fragmented and ACME messages

This patch implements support for sending fragmented messages and handling the reliable transmission flow (ACME messages). It adds dynamic message allocation slots and uses background delayed work to manage ACKs and retry timeouts.

Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0 -0
index 568877a4ce4b4..f61b5f18b819a 100644

[ ... ]

drivers/input/joystick/gip/gip-core.c static void gip_handle_quirks(struct gip_attachment *attachment) · line 534

Quoted 13 lines
drivers/input/joystick/gip/gip-core.c +13 -0
+static void gip_free_fragment(struct gip_out_fragment *fragment)
+{
+	cancel_delayed_work(&fragment->timeout);
+	kfree(fragment->data);
+	fragment->data = NULL;
+	fragment->active = false;
+	fragment->message = 0;
+	fragment->flags = 0;
+	fragment->seq = 0;
+	fragment->retries = 0;
+	fragment->total_length = 0;
+	fragment->fragment_offset = 0;
+}

[Severity: High]
Does this code leak the acked state to future messages?

When fragment slots are released, fragment->acked is not cleared here. If a slot is later reused, it could inherit a stale true value, causing the timeout retry logic to immediately skip resending lost packets.

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 584

Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +7 -0
+		for (i = 0; i < MAX_OUT_FRAGMENTS; i++) {
+			if (attachment->out_fragments[i].active)
+				continue;
+
+			fragment = &attachment->out_fragments[i];
+			break;
+		}

[Severity: High]
Is it safe to access out_fragments locklessly here?

gip_handle_command_protocol_control() also modifies these fields locklessly. Scanning and claiming slots concurrently without acquiring attachment->lock can lead to data races and duplicate slot allocations.

Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +5 -0
+
+		if (!fragment) {
+			gip_err(attachment, "Fragmented message queue is full; dropping message\n");
+			return -EALREADY;
+		}

drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 599

Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +10 -0
+
+		fragment->data = kmalloc(num_bytes, GFP_KERNEL);
+		if (!fragment->data)
+			return -ENOMEM;
+		fragment->active = true;
+		fragment->message = message_type;
+		fragment->flags = flags & GIP_FLAG_SYSTEM;
+		fragment->seq = seq;
+		fragment->total_length = num_bytes;
+		fragment->fragment_offset = 0;

[Severity: High]
Should fragment->acked be explicitly set to false here?

Since gip_free_fragment() does not clear the acked flag, explicitly setting it to false here would ensure the new message does not skip retry handling if the previous message finished successfully.

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 667

Quoted 13 lines
drivers/input/joystick/gip/gip-core.c +2 -1
 	guard(spinlock_irqsave)(&attachment->device->message_lock);
 	for (i = 0; i < MAX_OUT_MESSAGES && !urb; i++) {
 		if (!intf->out_queue[i].urb)
 			continue;
 		if (!intf->out_queue[i].urb->anchor)
 			urb = &intf->out_queue[i];
 	}
 	if (!urb) {
 		gip_err(attachment, "Output queue is full; dropping message\n");
-		return -ENOSPC;
+		rc = -ENOSPC;
+		goto err_free_fragment;
 	}

[Severity: Low]
Is it permitted to mix goto error handling with guard() in this function?

The cleanup subsystem guidelines strictly mandate that functions must not mix goto-based cleanup labels with guard() declarations, as it causes confusing ownership semantics and resource leaks.

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 709

Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +3 -0
+err_free_fragment:
+	if (fragment)
+		gip_free_fragment(fragment);
 	return rc;
 }

[Severity: Medium]
If usb_submit_urb() fails and we jump to err_free_fragment, does this leave a stale bit in intf->has_pending_out?

If the has_pending_out bit was previously set from an intermediate chunk submission, failing to clear it here leaves it active. When a new message claims this slot, gip_send_fragment_work() could see the stale bit and prematurely send the new message's second chunk.

[ ... ]

Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +3 -0
+static int gip_send_next_fragment(struct gip_attachment *attachment,
+	struct gip_out_fragment *fragment)
+{

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 735

Quoted 6 lines
drivers/input/joystick/gip/gip-core.c +6 -0
+	} else if (fragment->fragment_offset + mtu >= fragment->total_length) {
+		next_bytes = fragment->total_length - fragment->fragment_offset;
+		flags |= GIP_FLAG_ACME;
+	} else {
+		next_bytes = mtu;
+	}

[Severity: High]
How does this sequence handle retries for the first chunk?

If the first chunk of a fragmented message times out and this function retries it, GIP_FLAG_ACME is not appended because it is not the final fragment. This bypasses the reliable ACME timeout logic, which causes the worker thread to immediately and repeatedly resend offset 0 without waiting for ACKs, resulting in an infinite USB transmission loop.

[ ... ]

Quoted 17 lines

static void gip_reset_attachment(struct gip_attachment *attachment) {

drivers/input/joystick/gip/gip-core.c +9 -0
+	int i;
+	int pending_out_bits =
+		BIT((attachment->attachment_index + 1) * MAX_OUT_FRAGMENTS) -
+		BIT(attachment->attachment_index * MAX_OUT_FRAGMENTS);
+	unsigned long flags;
+
 	cancel_delayed_work(&attachment->metadata_next);
 	cancel_delayed_work(&attachment->in_fragment_timeout);
 
 	if (attachment->in_fragment_data)
 		gip_free_in_fragment(attachment);
 
+	spin_lock_irqsave(&attachment->device->message_lock, flags);
+	attachment->device->data.has_pending_out &= ~pending_out_bits;
+	attachment->device->audio.has_pending_out &= ~pending_out_bits;

drivers/input/joystick/gip/gip-core.c static int gip_ensure_metadata(struct gip_attachment *attachment) · line 1936

Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +8 -0
+	spin_unlock_irqrestore(&attachment->device->message_lock, flags);
+
+	for (i = 0; i < MAX_OUT_FRAGMENTS; i++) {
+		if (attachment->out_fragments[i].active)
+			gip_free_fragment(&attachment->out_fragments[i]);
+	}
+
 	gip_free_devices(attachment);
+
 }

[Severity: High]
Could the loop in gip_reset_attachment() introduce a use-after-free during teardown?

During gip_shutdown(), the device teardown path correctly uses cancel_delayed_work_sync() for other background workers, but relies on this loop which uses the non-synchronous cancel_delayed_work() via gip_free_fragment().

If gip_out_fragment_timeout() is executing and waiting on attachment->lock during shutdown, it could resume after devres frees the attachment structure, resulting in a use-after-free.

[ ... ]

Quoted 3 lines

static int gip_handle_command_protocol_control(struct gip_attachment *attachment,
const struct gip_header *header, const uint8_t *bytes, int num_bytes)
{

[ ... ]

drivers/input/joystick/gip/gip-core.c static int gip_ensure_metadata(struct gip_attachment *attachment) · line 2003

Quoted 13 lines
drivers/input/joystick/gip/gip-core.c +13 -0
+	fragment->acked = true;
+	fragment->retries = 0;
+	cancel_delayed_work(&fragment->timeout);
+	if (fragment->flags & GIP_FLAG_FRAGMENT) {
+		fragment->fragment_offset = fragment_offset;
+		rc = gip_send_next_fragment(attachment, fragment);
+		if (rc < 0)
+			return rc;
+	} else {
+		gip_free_fragment(fragment);
+	}
+
+	return 0;

[Severity: High]
Are modifications to the fragment state thread-safe here without attachment->lock?

This path modifies fragment->acked and fragment->fragment_offset, and calls gip_free_fragment() entirely locklessly. Doing so can race concurrently with gip_out_fragment_timeout() and gip_send_raw_message(), leading to corrupted fragment offsets or duplicated USB submissions.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author
#13 lore raw

PATCH v6 7/12 Input: xbox_gip - Add security implementation from xone

In reply to Vicki Pfau

This patch imports and adapts the security message implementation from the out-of-tree xone driver, which is licensed under the GPL 2.0. This message is necessary for most controllers that use wireless dongles. The implementation is largely unchanged, with minor changes being made to adapt it to the xbox_gip driver and renaming components as appropriate for the official terminology from the released spec.

---
 drivers/input/joystick/gip/Kconfig        |   1 +
 drivers/input/joystick/gip/Makefile       |   2 +-
 drivers/input/joystick/gip/gip-core.c     |  48 +-
 drivers/input/joystick/gip/gip-drivers.c  |   8 +
 drivers/input/joystick/gip/gip-security.c | 807 ++++++++++++++++++++++
 drivers/input/joystick/gip/gip.h          |  54 +-
 6 files changed, 895 insertions(+), 25 deletions(-)
 create mode 100644 drivers/input/joystick/gip/gip-security.c
drivers/input/joystick/gip/Kconfig +1 -0
index 3422bf84cb89..1b9b844779e6 100644
@@ -5,6 +5,7 @@
 config JOYSTICK_XBOX_GIP
 	tristate "Xbox One/Series controller support"
 	depends on USB && USB_ARCH_HAS_HCD && LEDS_CLASS_MULTICOLOR
+	depends on CRYPTO_ECDH && CRYPTO_LIB_SHA256 && CRYPTO_RSA
 	help
 	  Say Y here if you want to use Xbox One and Series controllers with your
 	  computer. Make sure to say Y to "Joystick support" (CONFIG_INPUT_JOYDEV)
drivers/input/joystick/gip/Makefile +1 -1
index d39f946db1ae..a5ad301a9063 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 obj-$(CONFIG_JOYSTICK_XBOX_GIP)	+= xbox-gip.o
 # Base driver
-xbox-gip-y := gip-core.o gip-drivers.o
+xbox-gip-y := gip-core.o gip-drivers.o gip-security.o
drivers/input/joystick/gip/gip-core.c +25 -23

96 lines not on this page · raw message

drivers/input/joystick/gip/gip-drivers.c +8 -0
index 2c2297b1fed8..74bc858e71cc 100644
@@ -81,6 +81,14 @@ const struct gip_driver gip_driver_gamepad = {
 		0xa3, 0x12, 0x7a, 0xf1, 0x97, 0xb5),
 
 	.quirks = (const struct gip_quirks[]) {
+		/* PowerA Xbox One Classic Controller */
+		{ GIP_VID_BDA, GIP_PID_BDA_XB1_CLASSIC, 0,
+			.quirks = GIP_QUIRK_SKIP_SECURITY },
+
+		/* PowerA Xbox One Fusion Pro */
+		{ GIP_VID_BDA, GIP_PID_BDA_XB1_FUSION_PRO, 0,
+			.quirks = GIP_QUIRK_SKIP_SECURITY },
+
 		/* Xbox One Controller (model 1573) */
 		{ GIP_VID_MICROSOFT, GIP_PID_XBOX_ONE_1573, 0,
 			.override_name = "Xbox One Controller" },
drivers/input/joystick/gip/gip-security.c new file +807 -0

807 lines not on this page · raw message

drivers/input/joystick/gip/gip.h +53 -1

117 lines not on this page · raw message

Signature · 1 line
2.54.0
#14 lore raw

Re: [PATCH v6 07/12] Input: xbox_gip - Add security implementation from xone

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

New issues:
- [High] Asynchronous cancellation of security workers leads to Use-After-Free on disconnect. A naive synchronous fix would cause a deadlock.
- [High] The security transcript incorrectly hashes USB transport padding for unfragmented messages, causing handshake failures.
- [Medium] Unselectable Kconfig dependency on hidden symbol CRYPTO_LIB_SHA256.
- [Medium] Out-of-bounds read when processing unexpectedly short security messages.
- [Medium] Sensitive cryptographic secrets are not cleared from stack and heap memory.

Pre-existing issues:
- [High] Preexisting Use-After-Free in out_fragment timeout handlers due to missing synchronous cancellation. --

commit e5c6e1be1aac0ee61bba5f91bea05c7f0f89796f
Author: Vicki Pfau <[email protected]>

Input: xbox_gip - Add security implementation from xone

This patch imports and adapts the security message implementation from the out-of-tree xone driver. This message is necessary for most controllers that use wireless dongles.

drivers/input/joystick/gip/Kconfig line 8

Quoted 8 lines
drivers/input/joystick/gip/Kconfig +1 -0
@@ -5,6 +5,7 @@
 config JOYSTICK_XBOX_GIP
 	tristate "Xbox One/Series controller support"
 	depends on USB && USB_ARCH_HAS_HCD && LEDS_CLASS_MULTICOLOR
+	depends on CRYPTO_ECDH && CRYPTO_LIB_SHA256 && CRYPTO_RSA

[Severity: Medium]
Is depends on CRYPTO_LIB_SHA256 intentional here?

CRYPTO_LIB_SHA256 is a hidden tristate symbol without a prompt, meaning users won't be able to select JOYSTICK_XBOX_GIP via menuconfig unless another driver accidentally selects CRYPTO_LIB_SHA256 first.

Should this use select CRYPTO_LIB_SHA256 instead?

[ ... ]

Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +0 -0

[ ... ]

drivers/input/joystick/gip/gip-core.c static void gip_reset_attachment(struct gip_attachment *attachment) · line 1925

Quoted 6 lines
drivers/input/joystick/gip/gip-core.c +0 -1
@@ -1932,7 +1922,6 @@ static void gip_reset_attachment(struct gip_attachment *attachment)
 	}
 
 	gip_free_devices(attachment);
-
 }

[Severity: High]
This is a pre-existing issue, but I noticed a potential use-after-free involving out_fragments during shutdown.

When the controller is unplugged, the teardown path calls gip_reset_attachment(), which loops through out_fragments and calls gip_free_fragment(). However, gip_free_fragment() uses the non-blocking cancel_delayed_work().

If the out_fragment timeout worker is running, it will block on guard(mutex)(&attachment->lock). After the disconnect path finishes and frees the attachment, the worker could unblock and access the freed memory.

[ ... ]

Quoted 3 lines
drivers/input/joystick/gip/gip-security.c new file +0 -0

[ ... ]

drivers/input/joystick/gip/gip-security.c line 462

Quoted 7 lines
drivers/input/joystick/gip/gip-security.c new file +7 -0
+static void gip_security2_exchange_ecdh(struct work_struct *work)
+{
+	struct gip_security *security = container_of(work, typeof(*security), work_exchange_ecdh);
+	struct gip_attachment *attachment = container_of(security, struct gip_attachment, security);
+	struct gip_security2_host_pubkey message = {};
+	u8 random[GIP_SECURITY_RANDOM_LEN * 2];
+	u8 secret[GIP_SECURITY2_SECRET_LEN];

[Severity: Medium]
Does this leave the derived hashed ECDH shared secret on the stack?

The 32-byte secret is allocated here but never explicitly cleared with memzero_explicit() before the function returns.

drivers/input/joystick/gip/gip-security.c line 465

Quoted 3 lines
drivers/input/joystick/gip/gip-security.c new file +3 -0
+	int err;
+
+	guard(mutex)(&attachment->lock);

[Severity: High]
Can this lock acquisition cause a use-after-free if the controller is disconnected during a handshake?

When gip_disconnect() triggers shutdown, it acquires attachment->lock and calls gip_security_release(), which uses the non-blocking cancel_work() on these security workers.

If this worker is running, it blocks here. After the teardown path completes and frees the attachment via devm_kfree(), this worker would unblock and access the freed memory. (Note that simply switching to cancel_work_sync() in gip_security_release() while holding the mutex might deadlock).

[ ... ]

drivers/input/joystick/gip/gip-security.c line 718

Quoted 9 lines
drivers/input/joystick/gip/gip-security.c new file +9 -0
+int gip_security_handle_message(struct gip_security *security, const void *bytes, int num_bytes)
+{
+	const struct gip_security_header_handshake *handshake = bytes;
+	const struct gip_security_header_full *hdr;
+	int err;
+
+	if (security->status != GIP_SECURITY_SENT) {
+		gip_err(security, "%s: security message received when none expected: 0x%02x\n",
+			__func__, handshake->command);

[Severity: Medium]
Could this dereference handshake->command out-of-bounds?

We haven't verified that the payload length num_bytes is at least sizeof(*handshake) before trying to read the command field for this error message.

A malformed USB message might cause an out-of-bounds read and information disclosure in the kernel log.

Quoted 6 lines
drivers/input/joystick/gip/gip-security.c new file +6 -0
+		return -EPROTO;
+	}
+
+	if (num_bytes < sizeof(*handshake))
+		return -EINVAL;
+

[ ... ]

drivers/input/joystick/gip/gip-security.c line 755

Quoted 7 lines
drivers/input/joystick/gip/gip-security.c new file +7 -0
+	err = gip_security_dispatch_message(security, hdr->data.command,
+		bytes + sizeof(*hdr), num_bytes - sizeof(*hdr));
+	if (err)
+		return err;
+
+	sha256_update(&security->transcript, bytes + sizeof(hdr->handshake),
+		num_bytes - sizeof(hdr->handshake));

[Severity: High]
Does the transcript hash include trailing padding bytes?

The core receive path in gip_receive_message() passes num_bytes (the full USB packet length minus header offset) to gip_handle_message() without truncating it to header.length.

If the USB host controller pads the interrupt transfer packet, these trailing padding bytes will be included in the sha256_update() call here, causing the handshake to fail.

[ ... ]

drivers/input/joystick/gip/gip-security.c line 769

Quoted 10 lines
drivers/input/joystick/gip/gip-security.c new file +10 -0
+void gip_security_release(struct gip_security *security)
+{
+	if (security->status < GIP_SECURITY_SENT)
+		return;
+
+	cancel_work(&security->work_exchange_rsa);
+	cancel_work(&security->work_exchange_ecdh);
+	cancel_work(&security->work_complete);
+
+	memzero_explicit(&security->transcript, sizeof(security->transcript));

[Severity: Medium]
While the transcript is explicitly cleared here, should we also zero out the long-term session keys (security->pms and security->master_secret)?

Leaving these on the heap could lead to a leak of cryptographic secrets if combined with an out-of-bounds read vulnerability.

Quoted 3 lines
drivers/input/joystick/gip/gip-security.c new file +3 -0
+
+	security->status = GIP_SECURITY_PENDING;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author
#15 lore raw

PATCH v6 8/12 Input: xbox_gip - Add arcade stick support

In reply to Vicki Pfau

This adds support for the arcade stick device type. Currently I'm only aware of one Xbox controller that uses this device type, the Razer Atrox, so testing coverage is limited.

---
 drivers/input/joystick/gip/Makefile           |   2 +
 drivers/input/joystick/gip/gip-arcade-stick.c | 173 ++++++++++++++++++
 drivers/input/joystick/gip/gip-core.c         |   6 +-
 drivers/input/joystick/gip/gip.h              |   4 +-
 4 files changed, 181 insertions(+), 4 deletions(-)
 create mode 100644 drivers/input/joystick/gip/gip-arcade-stick.c
drivers/input/joystick/gip/Makefile +2 -0
index a5ad301a9063..9ed59caec4d9 100644
@@ -2,3 +2,5 @@
 obj-$(CONFIG_JOYSTICK_XBOX_GIP)	+= xbox-gip.o
 # Base driver
 xbox-gip-y := gip-core.o gip-drivers.o gip-security.o
+# Additional device support
+xbox-gip-y += gip-arcade-stick.o
drivers/input/joystick/gip/gip-arcade-stick.c new file +173 -0

173 lines not on this page · raw message

drivers/input/joystick/gip/gip-core.c +4 -2
index 157a22c80abd..9e43d9cba453 100644
@@ -10,7 +10,8 @@
  * - Raw character device
  * - Wheel support
  * - Flight stick support
- * - Arcade stick support
+ * - More arcade stick testing
+ * - Arcade stick extra buttons
  * - Split into driver-per-attachment GIP-as-a-bus approach drivers
  *
  * This driver is based on the Microsoft GIP spec at:
@@ -331,6 +332,7 @@ struct gip_direct_motor {
 static const struct gip_driver *base_drivers[] = {
 	&gip_driver_navigation,
 	&gip_driver_gamepad,
+	&gip_driver_arcade_stick,
 	NULL /* Sentinel */
 };
 
@@ -1582,7 +1584,7 @@ static int gip_handle_command_raw_report(struct gip_attachment *attachment,
 	return 0;
 }
 
-static int gip_setup_input_device(struct gip_attachment *attachment)
+int gip_setup_input_device(struct gip_attachment *attachment)
 {
 	struct input_dev *input;
 	int rc;
drivers/input/joystick/gip/gip.h +2 -2
index d2ad10f77bc2..98add856ae21 100644
@@ -406,6 +406,8 @@ static inline struct device *gip_security_dev(struct gip_security *security)
 	return gip_attachment_dev(container_of(security, struct gip_attachment, security));
 }
 
+int gip_setup_input_device(struct gip_attachment *attachment);
+
 bool gip_supports_vendor_message(struct gip_attachment *attachment, uint8_t command, bool upstream);
 
 int gip_send_system_message(struct gip_attachment *attachment,
@@ -421,6 +423,4 @@ void gip_security_release(struct gip_security *security);
 extern const struct gip_driver gip_driver_navigation;
 extern const struct gip_driver gip_driver_gamepad;
 extern const struct gip_driver gip_driver_arcade_stick;
-extern const struct gip_driver gip_driver_wheel;
-extern const struct gip_driver gip_driver_flight_stick;
 #endif
Signature · 1 line
2.54.0
#16 lore raw

Re: [PATCH v6 08/12] Input: xbox_gip - Add arcade stick support

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `gip_handle_arcade_stick_report` function returns `-EINVAL` on short packets after…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The gip_handle_arcade_stick_report function returns -EINVAL on short packets after successfully reporting key events, bypassing input_sync(). --

commit 52729645cd0e478b1045c3e2e7b193ab5b91d1c7
Author: Vicki Pfau <[email protected]>

Input: xbox_gip - Add arcade stick support

This adds support for the arcade stick device type. Currently I'm only aware of one Xbox controller that uses this device type, the Razer Atrox, so testing coverage is limited.

Quoted 5 lines
drivers/input/joystick/gip/gip-arcade-stick.c new file +0 -0
new file mode 100644
index 0000000000000..11ededfab8555

[ ... ]

Quoted 7 lines
drivers/input/joystick/gip/gip-arcade-stick.c new file +7 -0
+static int gip_handle_arcade_stick_report(struct gip_attachment *attachment,
+	struct input_dev *input, const uint8_t *bytes, int num_bytes)
+{
+	struct gip_arcade_stick_info *info = attachment->driver_data;
+	int rc;
+
+	if (!info)

drivers/input/joystick/gip/gip-arcade-stick.c line 111

Quoted 10 lines
drivers/input/joystick/gip/gip-arcade-stick.c new file +10 -0
+		return -ENODEV;
+
+	rc = gip_driver_navigation.handle_input_report(attachment, input, bytes, num_bytes);
+	if (rc < 0)
+		return rc;
+
+	if (num_bytes < 6) {
+		gip_dbg(attachment, "Discarding too-short input report\n");
+		return -EINVAL;
+	}

[Severity: Medium]
If the input report packet is between 2 and 5 bytes long, does this sequence skip the required input_sync() call?

gip_driver_navigation.handle_input_report() will successfully process the first few bytes and queue key events. Returning -EINVAL here propagates the error to the caller, which then aborts and skips the input_sync() call for the already queued events.

Should the size check for num_bytes < 6 be performed before delegating to the navigation driver to prevent leaving unsynchronized events in the input core?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author
#17 lore raw

PATCH v6 9/12 Input: xbox_gip - Add support for PDP guitar controllers

In reply to Vicki Pfau

This patch adds support for the PDP guitar controllers, namely the Fender Jaguar and RiffMaster models. These use mostly the same protocol, but the RiffMaster appears t use a slightly newer variant.

---
 drivers/input/joystick/gip/Makefile         |   1 +
 drivers/input/joystick/gip/gip-core.c       |   1 +
 drivers/input/joystick/gip/gip-pdp-jaguar.c | 129 ++++++++++++++++++++
 drivers/input/joystick/gip/gip.h            |   2 +
 4 files changed, 133 insertions(+)
 create mode 100644 drivers/input/joystick/gip/gip-pdp-jaguar.c
drivers/input/joystick/gip/Makefile +1 -0
index 9ed59caec4d9..62c016d6c11e 100644
@@ -4,3 +4,4 @@ obj-$(CONFIG_JOYSTICK_XBOX_GIP)	+= xbox-gip.o
 xbox-gip-y := gip-core.o gip-drivers.o gip-security.o
 # Additional device support
 xbox-gip-y += gip-arcade-stick.o
+xbox-gip-y += gip-pdp-jaguar.o
drivers/input/joystick/gip/gip-core.c +1 -0
index 9e43d9cba453..5e581adb0788 100644
@@ -333,6 +333,7 @@ static const struct gip_driver *base_drivers[] = {
 	&gip_driver_navigation,
 	&gip_driver_gamepad,
 	&gip_driver_arcade_stick,
+	&gip_driver_pdp_jaguar,
 	NULL /* Sentinel */
 };
 
drivers/input/joystick/gip/gip-pdp-jaguar.c new file +129 -0

129 lines not on this page · raw message

drivers/input/joystick/gip/gip.h +2 -0
index 98add856ae21..3a35bcc43927 100644
@@ -41,6 +41,7 @@
 #define GIP_PID_XBOX_WIRELESS			0x0b12
 
 #define GIP_PID_PDP_ROCK_CANDY	0x0246
+#define GIP_PID_PDP_RIFFMASTER	0x0248
 
 #define GIP_PID_BDA_XB1_CLASSIC		0x581a
 #define GIP_PID_BDA_XB1_FUSION_PRO	0x591a
@@ -423,4 +424,5 @@ void gip_security_release(struct gip_security *security);
 extern const struct gip_driver gip_driver_navigation;
 extern const struct gip_driver gip_driver_gamepad;
 extern const struct gip_driver gip_driver_arcade_stick;
+extern const struct gip_driver gip_driver_pdp_jaguar;
 #endif
Signature · 1 line
2.54.0
author
#18 lore raw

PATCH v6 10/12 Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER

In reply to Vicki Pfau

Add new absolute axes for racing game controllers

---
 drivers/hid/hid-debug.c                | 16 +++++++++-------
 include/uapi/linux/input-event-codes.h |  3 +++
 2 files changed, 12 insertions(+), 7 deletions(-)
drivers/hid/hid-debug.c +9 -7
index f44e6e708404..7a1b56311e9b 100644
@@ -3511,13 +3511,15 @@ static const char *absolutes[ABS_CNT] = {
 	[ABS_RY] = "Ry",		[ABS_RZ] = "Rz",
 	[ABS_THROTTLE] = "Throttle",	[ABS_RUDDER] = "Rudder",
 	[ABS_WHEEL] = "Wheel",		[ABS_GAS] = "Gas",
-	[ABS_BRAKE] = "Brake",		[ABS_HAT0X] = "Hat0X",
-	[ABS_HAT0Y] = "Hat0Y",		[ABS_HAT1X] = "Hat1X",
-	[ABS_HAT1Y] = "Hat1Y",		[ABS_HAT2X] = "Hat2X",
-	[ABS_HAT2Y] = "Hat2Y",		[ABS_HAT3X] = "Hat3X",
-	[ABS_HAT3Y] = "Hat 3Y",		[ABS_PRESSURE] = "Pressure",
-	[ABS_DISTANCE] = "Distance",	[ABS_TILT_X] = "XTilt",
-	[ABS_TILT_Y] = "YTilt",		[ABS_TOOL_WIDTH] = "ToolWidth",
+	[ABS_BRAKE] = "Brake",		[ABS_CLUTCH] = "Clutch",
+	[ABS_HANDBRAKE] = "Handbrake",	[ABS_SHIFTER] = "Shifter",
+	[ABS_HAT0X] = "Hat0X",		[ABS_HAT0Y] = "Hat0Y",
+	[ABS_HAT1X] = "Hat1X",		[ABS_HAT1Y] = "Hat1Y",
+	[ABS_HAT2X] = "Hat2X",		[ABS_HAT2Y] = "Hat2Y",
+	[ABS_HAT3X] = "Hat3X",		[ABS_HAT3Y] = "Hat3Y",
+	[ABS_PRESSURE] = "Pressure",	[ABS_DISTANCE] = "Distance",
+	[ABS_TILT_X] = "XTilt",		[ABS_TILT_Y] = "YTilt",
+	[ABS_TOOL_WIDTH] = "ToolWidth",
 	[ABS_VOLUME] = "Volume",	[ABS_PROFILE] = "Profile",
 	[ABS_SND_PROFILE] = "SoundProfile",
 	[ABS_MISC] = "Misc",
include/uapi/linux/input-event-codes.h +3 -0
index 3528168f7c6d..5c881ed44177 100644
@@ -879,6 +879,9 @@
 #define ABS_WHEEL		0x08
 #define ABS_GAS			0x09
 #define ABS_BRAKE		0x0a
+#define ABS_CLUTCH		0x0b
+#define ABS_HANDBRAKE		0x0c
+#define ABS_SHIFTER		0x0d
 #define ABS_HAT0X		0x10
 #define ABS_HAT0Y		0x11
 #define ABS_HAT1X		0x12
Signature · 1 line
2.54.0
#19 lore raw

Re: [PATCH v6 10/12] Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The deadzone and fuzz reduction loop in the universal force-feedback driver hardcodes its upper…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The deadzone and fuzz reduction loop in the universal force-feedback driver hardcodes its upper bound to ABS_BRAKE, failing to include the newly added racing axes. --

commit 87216a5e9d2ce889923252443994d20643f1c840
Author: Vicki Pfau <[email protected]>

Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER

This commit adds new absolute axes for racing game controllers.

include/uapi/linux/input-event-codes.h line 884

Quoted 11 lines
include/uapi/linux/input-event-codes.h +3 -0
index 3528168f7c6d7..5c881ed441771 100644
@@ -879,6 +879,9 @@
 #define ABS_WHEEL		0x08
 #define ABS_GAS			0x09
 #define ABS_BRAKE		0x0a
+#define ABS_CLUTCH		0x0b
+#define ABS_HANDBRAKE		0x0c
+#define ABS_SHIFTER		0x0d

[Severity: Medium]
Will these newly added racing axes be excluded from the deadzone and fuzz adjustments in the generic PID force-feedback driver?

Looking at universal_pidff_input_configured() in
drivers/hid/hid-universal-pidff.c:

	/* Decrease fuzz and deadzone on available axes */
	for (axis = ABS_X; axis <= ABS_BRAKE; axis++) {
		if (!test_bit(axis, input->absbit))
			continue;

		input_set_abs_params(input, axis, input->absinfo[axis].minimum,
				     input->absinfo[axis].maximum,
				     axis == ABS_X ? 0 : 8, 0);
	}

Since this loop hardcodes its upper bound to ABS_BRAKE, it appears ABS_CLUTCH, ABS_HANDBRAKE, and ABS_SHIFTER will be skipped.

Consequently, these inputs would fall back to default HID core deadzone calculations, which might result in inconsistent behavior and unwanted deadzones compared to the gas and brake pedals which explicitly receive a deadzone of 0.

Should this loop's upper bound be updated to include the new axes?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
author
#20 lore raw

PATCH v6 11/12 HID: Map more automobile simulation inputs

In reply to Vicki Pfau

The HID usage tables section 5.3 specify clutch and shifter values that had previously been ignored. As the ABS_CLUTCH and ABS_SHIFTER bits now exist, we should use them appropriately.

---
 drivers/hid/hid-input.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/hid/hid-input.c +2 -0
index 3487600cadb4..6ade969412c6 100644
@@ -828,6 +828,8 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 		case 0xbb: map_abs(ABS_THROTTLE); break;
 		case 0xc4: map_abs(ABS_GAS);      break;
 		case 0xc5: map_abs(ABS_BRAKE);    break;
+		case 0xc6: map_abs(ABS_CLUTCH);   break;
+		case 0xc7: map_abs(ABS_SHIFTER);  break;
 		case 0xc8: map_abs(ABS_WHEEL);    break;
 		default:   goto ignore;
 		}
Signature · 1 line
2.54.0
author
#21 lore raw

PATCH v6 12/12 Input: xbox_gip - Add wheel support

In reply to Vicki Pfau

This adds preliminary support for racing wheel support in xbox_gip, exposing them mapped to the newly added axes.

---
 drivers/input/joystick/gip/Makefile      |   1 +
 drivers/input/joystick/gip/gip-core.c    |   4 +-
 drivers/input/joystick/gip/gip-drivers.c |  10 +
 drivers/input/joystick/gip/gip-wheel.c   | 350 +++++++++++++++++++++++
 drivers/input/joystick/gip/gip.h         |   6 +
 5 files changed, 370 insertions(+), 1 deletion(-)
 create mode 100644 drivers/input/joystick/gip/gip-wheel.c
drivers/input/joystick/gip/Makefile +1 -0
index 62c016d6c11e..7d4209eb32c3 100644
@@ -5,3 +5,4 @@ xbox-gip-y := gip-core.o gip-drivers.o gip-security.o
 # Additional device support
 xbox-gip-y += gip-arcade-stick.o
 xbox-gip-y += gip-pdp-jaguar.o
+xbox-gip-y += gip-wheel.o
drivers/input/joystick/gip/gip-core.c +3 -1
index 5e581adb0788..b4c0693f24ef 100644
@@ -8,7 +8,7 @@
  * - Audio device support
  * - Event logging
  * - Raw character device
- * - Wheel support
+ * - Wheel force feedback
  * - Flight stick support
  * - More arcade stick testing
  * - Arcade stick extra buttons
@@ -334,6 +334,8 @@ static const struct gip_driver *base_drivers[] = {
 	&gip_driver_gamepad,
 	&gip_driver_arcade_stick,
 	&gip_driver_pdp_jaguar,
+	&gip_driver_trueforce_wheel,
+	&gip_driver_wheel,
 	NULL /* Sentinel */
 };
 
drivers/input/joystick/gip/gip-drivers.c +10 -0
index 74bc858e71cc..7ba11b19c8ee 100644
@@ -142,6 +142,11 @@ static int gip_setup_navigation_input(struct gip_attachment *attachment, struct
 	input_set_capability(input, EV_KEY, BTN_TR);
 	input_set_capability(input, EV_KEY, BTN_TL);
 
+	if (attachment->quirks & GIP_QUIRK_FORCE_GAMEPAD_SB) {
+		input_set_capability(input, EV_KEY, BTN_THUMBR);
+		input_set_capability(input, EV_KEY, BTN_THUMBL);
+	}
+
 	attachment->dpad_as_buttons = dpad_as_buttons;
 	if (attachment->dpad_as_buttons) {
 		input_set_capability(input, EV_KEY, BTN_DPAD_UP);
@@ -193,6 +198,11 @@ static int gip_handle_navigation_report(struct gip_attachment *attachment,
 		input_report_key(input, BTN_TR, bytes[1] & BIT(5));
 	}
 
+	if (attachment->quirks & GIP_QUIRK_FORCE_GAMEPAD_SB) {
+		input_report_key(input, BTN_THUMBL, bytes[1] & BIT(6));
+		input_report_key(input, BTN_THUMBR, bytes[1] & BIT(7));
+	}
+
 	return 0;
 }
 
drivers/input/joystick/gip/gip-wheel.c new file +350 -0

350 lines not on this page · raw message

drivers/input/joystick/gip/gip.h +6 -0
index 3a35bcc43927..d488fad5463f 100644
@@ -28,10 +28,13 @@
 #define MAX_OUT_MESSAGES 8
 #define MAX_OUT_FRAGMENTS 2
 
+#define GIP_VID_THRUSTMASTER	0x044f
 #define GIP_VID_MICROSOFT	0x045e
 #define GIP_VID_PDP		0x0e6f
 #define GIP_VID_BDA		0x24c6
 
+#define GIP_PID_THRUSTMASTER_T128X	0xb69c
+
 #define GIP_PID_XBOX_ONE_1573			0x02d1
 #define GIP_PID_XBOX_ONE_1697			0x02dd
 #define GIP_PID_XBOX_ELITE			0x02e3
@@ -50,6 +53,7 @@
 #define GIP_QUIRK_NO_IMPULSE_VIBRATION	BIT(1)
 #define GIP_QUIRK_SWAP_LB_RB		BIT(2)
 #define GIP_QUIRK_SKIP_SECURITY		BIT(3)
+#define GIP_QUIRK_FORCE_GAMEPAD_SB	BIT(4)
 /*
  * Driver-specific quirks should start from 31 and go downwards to avoid
  * conflicts with newly-added core quirks
@@ -425,4 +429,6 @@ extern const struct gip_driver gip_driver_navigation;
 extern const struct gip_driver gip_driver_gamepad;
 extern const struct gip_driver gip_driver_arcade_stick;
 extern const struct gip_driver gip_driver_pdp_jaguar;
+extern const struct gip_driver gip_driver_wheel;
+extern const struct gip_driver gip_driver_trueforce_wheel;
 #endif
Signature · 1 line
2.54.0
#22 lore raw

Re: [PATCH v6 12/12] Input: xbox_gip - Add wheel support

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [Medium] Late packet length validation causes input state desynchronization.
- [Medium] Incorrect wraparound delta calculation for the REL_DIAL axis when multiple steps occur.
- [Medium] Phantom REL_DIAL scroll event upon device initialization.

Pre-existing issues:
- [High] Out-of-order teardown in gip_disconnect allows URB completion handler to trigger use-after-free of gip_device. --

commit 4a4f1a60a6109caec93af3211950c865f6e16e58
Author: Vicki Pfau <[email protected]>

Input: xbox_gip - Add wheel support

This adds preliminary support for racing wheel support in xbox_gip, exposing them mapped to the newly added axes.

Quoted 5 lines
drivers/input/joystick/gip/gip-wheel.c new file +0 -0
new file mode 100644
index 0000000000000..5de455edff3c1

[ ... ]

Quoted 5 lines
drivers/input/joystick/gip/gip-wheel.c new file +5 -0
+static int gip_handle_wheel_report(struct gip_attachment *attachment,
+	struct input_dev *input, const uint8_t *bytes, int num_bytes)
+{
+	uint8_t connections;
+	struct gip_wheel_info *info = attachment->driver_data;

drivers/input/joystick/gip/gip-wheel.c line 168

Quoted 10 lines
drivers/input/joystick/gip/gip-wheel.c new file +10 -0
+	int rc = gip_driver_navigation.handle_input_report(attachment, input, bytes, num_bytes);
+
+	if (rc < 0)
+		return rc;
+
+	if (!info)
+		return -ENODEV;
+
+	if (num_bytes < 17)
+		return -EINVAL;

[Severity: Medium]
Will returning -EINVAL here trap events that were already queued?

If a device sends a fragmented or short report (e.g. 16 bytes), gip_driver_navigation.handle_input_report() will process the first bytes and queue events.

By returning -EINVAL after this, the error propagates and bypasses the input_sync() call in the core loop, leaving the queued events unsynchronized in the input subsystem buffer and potentially causing stuck keys.

[ ... ]

drivers/input/joystick/gip/gip-wheel.c line 313

Quoted 12 lines
drivers/input/joystick/gip/gip-wheel.c new file +12 -0
+static int gip_handle_trueforce_wheel_report(struct gip_attachment *attachment,
+	struct input_dev *input, const uint8_t *bytes, int num_bytes)
+{
+	int rc = gip_driver_wheel.handle_input_report(attachment, input, bytes, num_bytes);
+	struct gip_trueforce_wheel_state *state = attachment->driver_data;
+	int dial;
+
+	if (rc < 0)
+		return rc;
+
+	if (num_bytes < 18)
+		return -EINVAL;

[Severity: Medium]
Can this similarly trap events reported by the base wheel handler?

Events are reported by gip_driver_wheel.handle_input_report() before the TrueForce-specific size validation rejects the packet. This will also bypass input_sync().

[ ... ]

drivers/input/joystick/gip/gip-wheel.c line 329

Quoted 9 lines
drivers/input/joystick/gip/gip-wheel.c new file +9 -0
+	input_report_key(input, KEY_KPENTER, bytes[17] & BIT(4));
+	/* Dial states only change one at a time, so we can easily check for wraparound */
+	if (dial == 0 && state->dial == 7)
+		input_report_rel(input, REL_DIAL, -1);
+	else if (dial == 7 && state->dial == 0)
+		input_report_rel(input, REL_DIAL, 1);
+	else
+		input_report_rel(input, REL_DIAL,
+			state->dial - dial);

[Severity: Medium]
Is it possible for this calculation to compute an incorrect wraparound delta if multiple steps occur between reports?

If a user quickly scrolls the TrueForce dial, the step might jump by 2 or more across the 0/7 boundary (e.g. from 7 to 1).

The explicit edge case checks would fail, and the fallback logic would compute state->dial - dial (7 - 1 = 6), incorrectly reporting a delta of +6 instead of the correct logical step of -2.

[Severity: Medium]
Could this logic emit a phantom REL_DIAL scroll event when the device is initialized?

When the driver first allocates gip_trueforce_wheel_state, kzalloc zeroes state->dial.

If the physical wheel dial is at a non-zero position (e.g. 4) when normal reporting begins, the handler computes 0 - 4 = -4 and immediately emits a scroll event before the user even touches the wheel.

[ ... ]

This is a pre-existing issue, but I noticed a potential problem in gip_disconnect() within drivers/input/joystick/gip/gip-core.c:

[Severity: High]
Does cancelling the work before killing the URB allow the URB completion handler to schedule new work that outlives the device?

drivers/input/joystick/gip/gip-core.c:gip_disconnect() {
	...
	gip_shutdown(gip);

	usb_kill_urb(gip->data.urb_in);
	...
}

If the URB is still active and a packet arrives just after gip_shutdown() cancels the device->receive_message work struct, the URB completion handler can call schedule_work() again.

When gip_disconnect() completes and frees the gip structure, the system workqueue could execute the corrupted workqueue item and trigger a use-after-free kernel panic.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12