Skip to contentSkip to conversation

HID: nintendo: Add preliminary Switch 2 controller driver

PATCH Superseded Vicki Pfau 2 participants 7 messages Jul 1

Subsystems (3)

v9 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Vicki Pfau

author maintainer
#0 lore raw

v9 cover HID: nintendo: Add preliminary Switch 2 controller driver

This series adds preliminary support for Switch 2 controllers using the same split-driver model as previous versions. This is a minor iteration on v7, fixing errors pointed out by sashiko.

At this point I'm pretty sure that what's left are imagined errors. It has a lot of ideas of what can be run concurrently that I'm no longer convinced of. If nothing obvious stands out, I'd like a review from a human before fixing more nothings from sashiko.

Vicki Pfau (3):
HID: nintendo: Add preliminary Switch 2 controller driver
HID: nintendo: Add rumble support for Switch 2 controllers
HID: nintendo: Add unified report format support

 MAINTAINERS                                   |    1 +
 drivers/hid/Kconfig                           |   19 +-
 drivers/hid/hid-ids.h                         |    4 +
 drivers/hid/hid-nintendo.c                    | 1670 ++++++++++++++++-
 drivers/hid/hid-nintendo.h                    |   72 +
 drivers/input/joystick/Kconfig                |   11 +
 drivers/input/joystick/Makefile               |    1 +
 drivers/input/joystick/nintendo-switch2-usb.c |  464 +++++
 8 files changed, 2201 insertions(+), 41 deletions(-)
 create mode 100644 drivers/hid/hid-nintendo.h
 create mode 100644 drivers/input/joystick/nintendo-switch2-usb.c
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v9 1/3 HID: nintendo: Add preliminary Switch 2 controller driver

This adds a new driver for the Switch 2 controllers. The Switch 2 uses an unusual split-interface design such that input and rumble occur on the main HID interface, but all other communication occurs over a "configuration" interface. This is the case on both USB and Bluetooth, so this new driver uses a split-driver design with the HID interface being the "main" driver and the configuration interface is a secondary driver that looks up to the HID interface, sharing resources on a common struct.

Due to using a non-standard pairing interface as well as Bluetooth communications being extremely limited in the kernel, a custom interface between userspace and the kernel will need to be designed, along with bringup in BlueZ. That is beyond the scope of this initial patch, which only contains the generic HID and USB configuration interface drivers.

This initial work supports general input for the Joy-Con 2, Pro Controller 2, and GameCube NSO controllers. IMU, rumble and battery support is not yet present.

---
 MAINTAINERS                                   |    1 +
 drivers/hid/Kconfig                           |   11 +-
 drivers/hid/hid-ids.h                         |    4 +
 drivers/hid/hid-nintendo.c                    | 1264 ++++++++++++++++-
 drivers/hid/hid-nintendo.h                    |   72 +
 drivers/input/joystick/Kconfig                |   11 +
 drivers/input/joystick/Makefile               |    1 +
 drivers/input/joystick/nintendo-switch2-usb.c |  464 ++++++
 8 files changed, 1818 insertions(+), 10 deletions(-)
 create mode 100644 drivers/hid/hid-nintendo.h
 create mode 100644 drivers/input/joystick/nintendo-switch2-usb.c
MAINTAINERS +1 -0
index 4ecd282f8f52..778982ab298e 100644
@@ -19051,6 +19051,7 @@ F:	drivers/scsi/nsp32*
 
 NINTENDO HID DRIVER
 M:	Daniel J. Ogorchock <[email protected]>
+M:	Vicki Pfau <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	drivers/hid/hid-nintendo*
drivers/hid/Kconfig +7 -4
index f9bcaeb66385..19c77c323ec9 100644
@@ -843,10 +843,13 @@ config HID_NINTENDO
 	depends on LEDS_CLASS
 	select POWER_SUPPLY
 	help
-	Adds support for the Nintendo Switch Joy-Cons, NSO, Pro Controller.
-	All controllers support bluetooth, and the Pro Controller also supports
-	its USB mode. This also includes support for the Nintendo Switch Online
-	Controllers which include the NES, Genesis, SNES, and N64 controllers.
+	Adds support for the Nintendo Switch Joy-Cons, NSO, Pro Controller, as
+	well as Nintendo Switch 2 Joy-Cons, Pro Controller, and NSO GameCube
+	controllers. All Switch controllers support bluetooth, and the Pro
+	Controller also supports its USB mode. This also includes support for
+	the Nintendo Switch Online Controllers which include the NES, Genesis,
+	SNES, and N64 controllers. Switch 2 controllers currently only support
+	USB mode.
 
 	To compile this driver as a module, choose M here: the
 	module will be called hid-nintendo.
drivers/hid/hid-ids.h +4 -0
index 1059922baaac..9ba62b8fb894 100644
@@ -1103,6 +1103,10 @@
 #define USB_DEVICE_ID_NINTENDO_SNESCON	0x2017
 #define USB_DEVICE_ID_NINTENDO_GENCON	0x201e
 #define USB_DEVICE_ID_NINTENDO_N64CON	0x2019
+#define USB_DEVICE_ID_NINTENDO_NS2_JOYCONR	0x2066
+#define USB_DEVICE_ID_NINTENDO_NS2_JOYCONL	0x2067
+#define USB_DEVICE_ID_NINTENDO_NS2_PROCON	0x2069
+#define USB_DEVICE_ID_NINTENDO_NS2_GCCON	0x2073
 
 #define USB_VENDOR_ID_NOVATEK		0x0603
 #define USB_DEVICE_ID_NOVATEK_PCT	0x0600
drivers/hid/hid-nintendo.c +1258 -6

1,343 lines not on this page · raw message

drivers/hid/hid-nintendo.h new file +72 -0

72 lines not on this page · raw message

drivers/input/joystick/Kconfig +11 -0
index 7755e5b454d2..868262c6ccd9 100644
@@ -422,4 +422,15 @@ config JOYSTICK_SEESAW
 	  To compile this driver as a module, choose M here: the module will be
 	  called adafruit-seesaw.
 
+config JOYSTICK_NINTENDO_SWITCH2_USB
+	tristate "Wired Nintendo Switch 2 controller support"
+	depends on HID_NINTENDO
+	depends on USB
+	help
+	  Say Y here if you want to enable support for wired Nintendo Switch 2
+	  controllers.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called nintendo-switch2-usb.
+
 endif
drivers/input/joystick/Makefile +1 -0
index 9976f596a920..8f92900ae885 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_JOYSTICK_SIDEWINDER)	+= sidewinder.o
 obj-$(CONFIG_JOYSTICK_SPACEBALL)	+= spaceball.o
 obj-$(CONFIG_JOYSTICK_SPACEORB)		+= spaceorb.o
 obj-$(CONFIG_JOYSTICK_STINGER)		+= stinger.o
+obj-$(CONFIG_JOYSTICK_NINTENDO_SWITCH2_USB)	+= nintendo-switch2-usb.o
 obj-$(CONFIG_JOYSTICK_TMDC)		+= tmdc.o
 obj-$(CONFIG_JOYSTICK_TURBOGRAFX)	+= turbografx.o
 obj-$(CONFIG_JOYSTICK_TWIDJOY)		+= twidjoy.o
drivers/input/joystick/nintendo-switch2-usb.c new file +464 -0

464 lines not on this page · raw message

Signature · 1 line
2.54.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] Potential NULL pointer dereference in `switch2_handle_flash_read()` when receiving spontaneous or…

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] Potential NULL pointer dereference in switch2_handle_flash_read() when receiving spontaneous or delayed USB messages.
- [High] Race condition in switch2_remove() clearing drvdata before hardware stop causes NULL pointer dereference.
- [High] Unconditional ida_free() with U32_MAX triggers a BUG_ON panic on probe failure or device removal.
- [High] Using devm_led_classdev_register with &input->dev causes a devres circular reference, leaking devices and causing a Use-After-Free.
- [High] The -EBUSY error path in switch2_probe erroneously destroys the active input device of the existing interface.
- [High] TOCTOU race condition in switch2_usb_disconnect causes UAF due to missing usb_kill_urb.
- [Medium] URB slot leak and stuck polling on transient USB errors, plus processing of invalid data.
--

commit ef162798bccd0977cafe2dea531f9e56194e0992
Author: Vicki Pfau <[email protected]>
Subject: HID: nintendo: Add preliminary Switch 2 controller driver

This patch adds a new driver for Nintendo Switch 2 controllers, using a split-driver design for HID and USB configuration interfaces. It brings support for Joy-Con 2, Pro Controller 2, and GameCube NSO controllers via USB.

I found a few regressions while reviewing this patch.

Quoted 9 lines
drivers/hid/hid-nintendo.c +3 -0
index e7302ec01ff1f..5aacb8e88b704 100644
@@ -2838,7 +2845,1194 @@ static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message)
[ ... ]
+static void switch2_handle_flash_read(struct switch2_controller *ns2, uint8_t size,
+	uint32_t address, const uint8_t *data)
+{

drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3436

Quoted 10 lines
drivers/hid/hid-nintendo.c +9 -0
+	bool ok;
+
+	switch (address) {
[ ... ]
+	case NS2_FLASH_ADDR_FACTORY_PRIMARY_CALIB:
+		if (size != NS2_FLASH_SIZE_FACTORY_AXIS_CALIB)
+			return;
+		switch2_init_step_done(ns2, NS2_INIT_READ_FACTORY_PRIMARY_CALIB);
+		ok = switch2_parse_stick_calibration(&ns2->stick_calib[0], data);
+		if (ok) {

[Severity: High]
Can this cause a NULL pointer dereference? If the USB configuration interface receives a flash read reply before the HID interface finishes probing, or if a delayed response arrives from a previous session, ns2->hdev will be NULL when hid_dbg is called.

Quoted 10 lines
drivers/hid/hid-nintendo.c +8 -0
+			hid_dbg(ns2->hdev, "Got factory primary stick calibration:\n");
+			hid_dbg(ns2->hdev, "Left max: %i, neutral: %i, right max: %i\n",
[ ... ]
+static int switch2_init_input(struct switch2_controller *ns2)
+{
[ ... ]
+	for (i = 0; i < JC_NUM_LEDS; i++) {
+		struct led_classdev *led = &ns2->leds[i];
+		char *name = devm_kasprintf(&input->dev, GFP_KERNEL, "%s:%s:%s",
+				      dev_name(&input->dev),

drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3375

Quoted 10 lines
drivers/hid/hid-nintendo.c +10 -0
+				      "green",
+				      joycon_player_led_names[i]);
+
+		if (!name) {
+			dev_err(&input->dev, "Failed to allocate name for player %d LED; ret=%d\n",
+				i + 1, ret);
+			break;
+		}
+
+		led->name = name;

[Severity: High]
Could this create a circular devres reference? The LED class devices hold references to their parent (the input device). When the controller is removed, the input device's devres cleanup won't run until its refcount hits 0, which cannot happen because the LED devices still exist.

Since the led_classdev structures are embedded in ns2 (which is freed on removal), might this cause a use-after-free when the leaked LED sysfs nodes are later accessed?

Quoted 10 lines
drivers/hid/hid-nintendo.c +8 -0
+		ret = devm_led_classdev_register(&input->dev, led);
+		if (ret < 0) {
+			dev_err(&input->dev, "Failed to register player %d LED; ret=%d\n",
[ ... ]
+static int switch2_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
[ ... ]
+	ns2->player_id = U32_MAX;
+	ret = ida_alloc(&nintendo_player_id_allocator, GFP_KERNEL);
+	if (ret < 0)

drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 4010

Quoted 10 lines
drivers/hid/hid-nintendo.c +9 -0
+		hid_warn(hdev, "Failed to allocate player ID, skipping; ret=%d\n", ret);
+	else
+		ns2->player_id = ret;
[ ... ]
+	hid_hw_close(hdev);
+err_stop:
+	hid_hw_stop(hdev);
+err_cleanup:
+	ida_free(&nintendo_player_id_allocator, ns2->player_id);
+	ns2->hdev = NULL;

[Severity: High]
Does this error path break the already active controller? If a second HID interface for the same physical controller probes and detects a conflict, it returns -EBUSY and jumps to err_put.

However, unconditionally calling switch2_controller_put(ns2) here tears down the input device for the existing, bound interface.

Quoted 5 lines
drivers/hid/hid-nintendo.c +5 -0
+err_put:
+	switch2_controller_put(ns2);
+	mutex_unlock(&ns2->lock);
+	kref_put(&ns2->refcount, switch2_kref_put);
+

drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 4025

Quoted 10 lines
drivers/hid/hid-nintendo.c +10 -0
+	return ret;
+}
+
+static void switch2_remove(struct hid_device *hdev)
+{
+	struct switch2_controller *ns2 = hid_get_drvdata(hdev);
+
+	mutex_lock(&ns2->lock);
+	switch2_controller_put(ns2);
+	ns2->hdev = NULL;

[Severity: High]
Can this cause a race condition leading to a NULL pointer dereference? The drvdata is set to NULL before hid_hw_stop() halts the hardware.

If an input interrupt triggers switch2_event() in this window, it will retrieve a NULL pointer from hid_get_drvdata() and immediately dereference it when calling rcu_dereference(ns2->input).

drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 4026

Quoted 1 line
drivers/hid/hid-nintendo.c +1 -0
+	hid_set_drvdata(hdev, NULL);

[Severity: High]
Does this trigger a panic if ida_alloc() failed during probe? If the probe fails to allocate the ID, ns2->player_id remains U32_MAX.

Unconditionally calling ida_free() during removal passes this value, which casts to -1 and will trigger the BUG_ON((int)id < 0) inside the ida_free() implementation.

Quoted 26 lines
drivers/hid/hid-nintendo.c +6 -0
+	ida_free(&nintendo_player_id_allocator, ns2->player_id);
+	mutex_unlock(&ns2->lock);
+	kref_put(&ns2->refcount, switch2_kref_put);
+	hid_hw_close(hdev);
+	hid_hw_stop(hdev);
+}
drivers/input/joystick/nintendo-switch2-usb.c new file +11 -0
new file mode 100644
index 0000000000000..0a004918c1f10
@@ -0,0 +1,464 @@
[ ... ]
+static void switch2_bulk_in(struct urb *urb)
+{
[ ... ]
+	switch (urb->status) {
+	case 0:
+		schedule = true;
+		break;
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+		dev_dbg(&ns2_usb->udev->dev, "shutting down input urb: %d\n", urb->status);
+		return;

drivers/input/joystick/nintendo-switch2-usb.c line 76

Quoted 10 lines
drivers/input/joystick/nintendo-switch2-usb.c new file +10 -0
+	default:
+		dev_dbg(&ns2_usb->udev->dev, "unknown input urb status: %d\n", urb->status);
+		break;
+	}
+
+	spin_lock_irqsave(&ns2_usb->bulk_in_lock, flags);
+	for (i = 0; i < NS2_IN_URBS; i++) {
+		int err;
+		struct switch2_urb *ns2_urb;
+

[Severity: Medium]
Does this leak the URB slot and process invalid data? If an URB completes with an error, the schedule flag remains false, but the state is set to NS2_URB_IN. If both IN URBs fail, the workqueue is never scheduled again.

If the workqueue is scheduled by the other URB, switch2_usb_message_in_work() will process this failed URB without validating urb->status, passing potentially garbage data to switch2_receive_command().

drivers/input/joystick/nintendo-switch2-usb.c line 407

Quoted 13 lines
drivers/input/joystick/nintendo-switch2-usb.c new file +11 -0
+		if (ns2_usb->bulk_in[i].urb == urb) {
+			ns2_usb->bulk_in[i].state = NS2_URB_IN;
+			continue;
+		}
[ ... ]
+static void switch2_usb_disconnect(struct usb_interface *intf)
+{
[ ... ]
+	/* Prevent any further IN URBs from being scheduled */
+	spin_lock_irqsave(&ns2_usb->bulk_in_lock, flags);
+	ns2_usb->shutdown = true;
+	spin_unlock_irqrestore(&ns2_usb->bulk_in_lock, flags);
+

[Severity: High]
Is there a TOCTOU race condition here during disconnect? The USB core unanchors URBs before invoking their completion handler.

If switch2_bulk_in() is running concurrently, usb_kill_anchored_urbs() won't wait for it. cancel_work_sync() might return immediately if the handler hasn't yet called schedule_work().

Could the handler then schedule work on the freed ns2_usb structure, leading to a use-after-free?

Quoted 4 lines
drivers/input/joystick/nintendo-switch2-usb.c new file +4 -0
+	usb_kill_anchored_urbs(&ns2_usb->bulk_in_anchor);
+	cancel_work_sync(&ns2_usb->message_in_work);
+	for (i = 0; i < NS2_IN_URBS; i++) {
+		spin_lock_irqsave(&ns2_usb->bulk_in_lock, flags);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH v9 2/3 HID: nintendo: Add rumble support for Switch 2 controllers

In reply to Vicki Pfau

This adds rumble support for both the "HD Rumble" linear resonant actuator type as used in the Joy-Cons and Pro Controller, as well as the eccentric rotating mass type used in the GameCube controller. Note that since there's currently no API for exposing full control of LRAs with evdev, it only simulates a basic rumble for now.

---
 drivers/hid/Kconfig        |   8 +-
 drivers/hid/hid-nintendo.c | 217 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 218 insertions(+), 7 deletions(-)
drivers/hid/Kconfig +4 -4
index 19c77c323ec9..851eed76c236 100644
@@ -859,10 +859,10 @@ config NINTENDO_FF
 	depends on HID_NINTENDO
 	select INPUT_FF_MEMLESS
 	help
-	Say Y here if you have a Nintendo Switch controller and want to enable
-	force feedback support for it. This works for both joy-cons, the pro
-	controller, and the NSO N64 controller. For the pro controller, both
-	rumble motors can be controlled individually.
+	Say Y here if you have a Nintendo Switch or Switch 2 controller and want
+	to enable force feedback support for it. This works for Joy-Cons, the Pro
+	Controllers, and the NSO N64 and GameCube controller. For the Pro
+	Controller, both rumble motors can be controlled individually.
 
 config HID_NTI
 	tristate "NTI keyboard adapters"
drivers/hid/hid-nintendo.c +214 -3

291 lines not on this page · raw message

Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH v9 2/3] HID: nintendo: Add rumble support for Switch 2 controllers

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Use-After-Free on `ns2->rumble_buffer` during device teardown due to devres lifecycle mismatch.…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Use-After-Free on ns2->rumble_buffer during device teardown due to devres lifecycle mismatch.
- [Medium] Rumble work scheduling math error adds 2ms instead of subtracting, causing it to run late and risk buffer underruns.
- [Medium] Pro Controller rumble motors receive identical signals, failing to control them individually as claimed. --

commit 0899ea0ef032bf60e7abf4c07b73da1a806a6406

Author: Vicki Pfau <[email protected]>
Date:   Wed Jul 1 06:56:03 2026 +0000

HID: nintendo: Add rumble support for Switch 2 controllers

This adds rumble support for both the "HD Rumble" linear resonant actuator type as used in the Joy-Cons and Pro Controller, as well as the eccentric rotating mass type used in the GameCube controller. Note that since there's currently no API for exposing full control of LRAs with evdev, it only simulates a basic rumble for now.

Quoted 5 lines
drivers/hid/hid-nintendo.c +0 -0
index 5aacb8e88b704..96463e1586bbd 100644
@@ -3205,6 +3259,125 @@ static void switch2_kref_put(struct kref *refcount)

[ ... ]

Quoted 2 lines
drivers/hid/hid-nintendo.c +2 -0
+static int switch2_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
+{

[ ... ]

Quoted 18 lines
drivers/hid/hid-nintendo.c +18 -0
+	if (ns2->ctlr_type == NS2_CTLR_TYPE_GC) {
+		ns2->rumble.sd.amplitude = max(effect->u.rumble.strong_magnitude,
+			(uint16_t) (effect->u.rumble.weak_magnitude >> 1));
+	} else {
+		ns2->rumble.hd.hi_freq = RUMBLE_HI_FREQ;
+		ns2->rumble.hd.lo_freq = RUMBLE_LO_FREQ;
+		ns2->rumble.hd.hi_amp = effect->u.rumble.weak_magnitude * RUMBLE_MAX >> 16;
+		ns2->rumble.hd.lo_amp = effect->u.rumble.strong_magnitude * RUMBLE_MAX >> 16;
+	}
+	spin_unlock_irqrestore(&ns2->rumble_lock, flags);
+
+	schedule_delayed_work(&ns2->rumble_work, 0);
+
+	return 0;
+}
+
+static void switch2_rumble_work(struct work_struct *work)
+{

[ ... ]

Quoted 4 lines
drivers/hid/hid-nintendo.c +4 -0
+	} else {
+		ns2->rumble_buffer[0] = 1;
+		switch2_encode_rumble(&ns2->rumble.hd, &ns2->rumble_buffer[0x2]);
+		active = ns2->rumble.hd.hi_amp || ns2->rumble.hd.lo_amp;

drivers/hid/hid-nintendo.c static void switch2_kref_put(struct kref *refcount) · line 3336

Quoted 10 lines
drivers/hid/hid-nintendo.c +10 -0
+		if (ns2->ctlr_type == NS2_CTLR_TYPE_PRO) {
+			/*
+			 * The Pro Controller contains separate LRAs on each
+			 * side that can be controlled individually.
+			 */
+			ns2->rumble_buffer[0] = 2;
+			ns2->rumble_buffer[0x11] = 0x50 | ns2->rumble_seq;
+			switch2_encode_rumble(&ns2->rumble.hd, &ns2->rumble_buffer[0x12]);
+		}
+	}

[Severity: Medium]
Does this code accurately provide independent control of the two rumble motors?

The commit message and the comment above mention that the Pro Controller's left and right rumble motors can be controlled individually.

However, in switch2_play_effect(), the strong and weak magnitudes are mixed into a single switch2_hd_rumble structure (ns2->rumble.hd).

Then in switch2_rumble_work(), this exact same structure is encoded into both the left (offset 0x2) and right (offset 0x12) rumble buffers.

Since both physical motors receive the exact same mixed signal (both high and low frequencies simultaneously), doesn't this result in identical rumble on both sides rather than individual control?

Quoted 7 lines
drivers/hid/hid-nintendo.c +7 -0
+	ns2->rumble_seq = (ns2->rumble_seq + 1) & 0xF;
+	spin_unlock_irqrestore(&ns2->rumble_lock, flags);
+
+	if (active) {
+		unsigned long interval = msecs_to_jiffies(4);
+		uint64_t current_jiffies = get_jiffies_64();
+

drivers/hid/hid-nintendo.c static void switch2_kref_put(struct kref *refcount) · line 3353

Quoted 10 lines
drivers/hid/hid-nintendo.c +10 -0
+		if (!ns2->last_rumble_work)
+			ns2->last_rumble_work = current_jiffies;
+		else
+			ns2->last_rumble_work += interval;
+
+		/* Reschedule a little early to make sure the buffer never underruns */
+		interval += msecs_to_jiffies(2);
+		if (ns2->last_rumble_work + interval >= current_jiffies)
+			schedule_delayed_work(&ns2->rumble_work,
+				ns2->last_rumble_work + interval - current_jiffies);

[Severity: Medium]
Does this schedule the work early or late?

The comment mentions rescheduling slightly early to prevent buffer underruns.

Since interval starts at 4ms, adding 2ms to it with interval += msecs_to_jiffies(2) makes the new interval 6ms.

When scheduling for ns2->last_rumble_work + interval, this appears to target a time 6ms after the last theoretical tick instead of the intended earlier target (which would be 2ms after, or interval - 2ms).

Could this delay cause the work to run later than intended and lead to the buffer underruns the comment warns about?

[ ... ]

Quoted 1 line
drivers/hid/hid-nintendo.c +0 -0
@@ -3346,6 +3522,20 @@ static int switch2_init_input(struct switch2_controller *ns2)

[ ... ]

drivers/hid/hid-nintendo.c static int switch2_init_input(struct switch2_controller *ns2) · line 3530

Quoted 6 lines
drivers/hid/hid-nintendo.c +6 -0
+#if IS_ENABLED(CONFIG_NINTENDO_FF)
+	ns2->rumble_buffer = devm_kzalloc(&input->dev, 64, GFP_KERNEL);
+	if (!ns2->rumble_buffer) {
+		input_free_device(input);
+		return -ENOMEM;
+	}

[Severity: High]
Can this lead to a use-after-free during device removal?

The rumble_buffer is allocated with devm_kzalloc() tied to the input device, so it gets freed when the input device is unregistered and its final reference is dropped.

However, looking at the teardown sequence, if the input device is unregistered (for example via switch2_controller_put() calling input_unregister_device()) before the delayed work is completely synced and canceled, the worker could run and access the freed rumble_buffer.

Would it be safer to allocate this buffer without devm, or ensure the delayed work is fully canceled prior to unregistering the input device?

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

PATCH v9 3/3 HID: nintendo: Add unified report format support

In reply to Vicki Pfau

This adds support for the "unified" report format that all controllers also support, which has overlapping fields for like buttons and axes between them.

---
 drivers/hid/hid-nintendo.c | 151 +++++++++++++++++++++++++++++++++++--
 1 file changed, 146 insertions(+), 5 deletions(-)
drivers/hid/hid-nintendo.c +146 -5
index 96463e1586bb..b406e6cfd221 100644
@@ -2872,6 +2872,36 @@ static int joycon_suspend(struct hid_device *hdev, pm_message_t message)
 #define NS2_BTN3_SR	BIT(6)
 #define NS2_BTN3_SL	BIT(7)
 
+#define NS2_BTN_U1_Y	BIT(0)
+#define NS2_BTN_U1_X	BIT(1)
+#define NS2_BTN_U1_B	BIT(2)
+#define NS2_BTN_U1_A	BIT(3)
+#define NS2_BTN_U1_SR	BIT(4)
+#define NS2_BTN_U1_SL	BIT(5)
+#define NS2_BTN_U1_R	BIT(6)
+#define NS2_BTN_U1_ZR	BIT(7)
+
+#define NS2_BTN_U2_MINUS	BIT(0)
+#define NS2_BTN_U2_PLUS		BIT(1)
+#define NS2_BTN_U2_RS		BIT(2)
+#define NS2_BTN_U2_LS		BIT(3)
+#define NS2_BTN_U2_HOME		BIT(4)
+#define NS2_BTN_U2_CAPTURE	BIT(5)
+#define NS2_BTN_U2_C		BIT(6)
+
+#define NS2_BTN_U3_DOWN		BIT(0)
+#define NS2_BTN_U3_UP		BIT(1)
+#define NS2_BTN_U3_RIGHT	BIT(2)
+#define NS2_BTN_U3_LEFT		BIT(3)
+#define NS2_BTN_U3_SR		BIT(4)
+#define NS2_BTN_U3_SL		BIT(5)
+#define NS2_BTN_U3_L		BIT(6)
+#define NS2_BTN_U3_ZL		BIT(7)
+
+#define NS2_BTN_U4_GR		BIT(0)
+#define NS2_BTN_U4_GL		BIT(1)
+#define NS2_BTN_U4_HEADSET	BIT(5)
+
 #define NS2_BTN_JCR_HOME	BIT(0)
 #define NS2_BTN_JCR_GR		BIT(2)
 #define NS2_BTN_JCR_C		NS2_BTN3_C
@@ -3120,6 +3150,22 @@ static const struct switch2_ctlr_button_mapping ns2_left_joycon_button_mappings[
 	{ /* sentinel */ },
 };
 
+static const struct switch2_ctlr_button_mapping ns2_left_joycon_button_unified_mappings[] = {
+	{ BTN_DPAD_LEFT,	2, NS2_BTN_U3_LEFT,	},
+	{ BTN_DPAD_UP,		2, NS2_BTN_U3_UP,	},
+	{ BTN_DPAD_DOWN,	2, NS2_BTN_U3_DOWN,	},
+	{ BTN_DPAD_RIGHT,	2, NS2_BTN_U3_RIGHT,	},
+	{ BTN_TL,		2, NS2_BTN_U3_L,	},
+	{ BTN_TL2,		2, NS2_BTN_U3_ZL,	},
+	{ BTN_SELECT,		1, NS2_BTN_U2_MINUS,	},
+	{ BTN_THUMBL,		1, NS2_BTN_U2_LS,	},
+	{ KEY_RECORD,		1, NS2_BTN_U2_CAPTURE,	},
+	{ BTN_GRIPR,		2, NS2_BTN_U3_SL,	},
+	{ BTN_GRIPR2,		2, NS2_BTN_U3_SR,	},
+	{ BTN_GRIPL,		3, NS2_BTN_U4_GL,	},
+	{ /* sentinel */ },
+};
+
 static const struct switch2_ctlr_button_mapping ns2_right_joycon_button_mappings[] = {
 	{ BTN_SOUTH,	0, NS2_BTNR_A,		},
 	{ BTN_EAST,	0, NS2_BTNR_B,		},
@@ -3137,6 +3183,23 @@ static const struct switch2_ctlr_button_mapping ns2_right_joycon_button_mappings
 	{ /* sentinel */ },
 };
 
+static const struct switch2_ctlr_button_mapping ns2_right_joycon_button_unified_mappings[] = {
+	{ BTN_SOUTH,	0, NS2_BTN_U1_A,	},
+	{ BTN_EAST,	0, NS2_BTN_U1_B,	},
+	{ BTN_NORTH,	0, NS2_BTN_U1_X,	},
+	{ BTN_WEST,	0, NS2_BTN_U1_Y,	},
+	{ BTN_TR,	0, NS2_BTN_U1_R,	},
+	{ BTN_TR2,	0, NS2_BTN_U1_ZR	},
+	{ BTN_START,	1, NS2_BTN_U2_PLUS,	},
+	{ BTN_THUMBR,	1, NS2_BTN_U2_RS,	},
+	{ BTN_C,	1, NS2_BTN_U2_C,	},
+	{ BTN_MODE,	1, NS2_BTN_U2_HOME,	},
+	{ BTN_GRIPL2,	0, NS2_BTN_U1_SL,	},
+	{ BTN_GRIPL,	0, NS2_BTN_U1_SR,	},
+	{ BTN_GRIPR,	3, NS2_BTN_U4_GR,	},
+	{ /* sentinel */ },
+};
+
 static const struct switch2_ctlr_button_mapping ns2_procon_mappings[] = {
 	{ BTN_SOUTH,	0, NS2_BTNR_A,		},
 	{ BTN_EAST,	0, NS2_BTNR_B,		},
@@ -3158,6 +3221,27 @@ static const struct switch2_ctlr_button_mapping ns2_procon_mappings[] = {
 	{ /* sentinel */ },
 };
 
+static const struct switch2_ctlr_button_mapping ns2_procon_unified_mappings[] = {
+	{ BTN_SOUTH,	0, NS2_BTN_U1_A,	},
+	{ BTN_EAST,	0, NS2_BTN_U1_B,	},
+	{ BTN_NORTH,	0, NS2_BTN_U1_X,	},
+	{ BTN_WEST,	0, NS2_BTN_U1_Y,	},
+	{ BTN_TL,	2, NS2_BTN_U3_L,	},
+	{ BTN_TR,	0, NS2_BTN_U1_R,	},
+	{ BTN_TL2,	2, NS2_BTN_U3_ZL,	},
+	{ BTN_TR2,	0, NS2_BTN_U1_ZR,	},
+	{ BTN_SELECT,	1, NS2_BTN_U2_MINUS,	},
+	{ BTN_START,	1, NS2_BTN_U2_PLUS,	},
+	{ BTN_THUMBL,	1, NS2_BTN_U2_LS,	},
+	{ BTN_THUMBR,	1, NS2_BTN_U2_RS,	},
+	{ BTN_MODE,	1, NS2_BTN_U2_HOME	},
+	{ KEY_RECORD,	1, NS2_BTN_U2_CAPTURE	},
+	{ BTN_GRIPR,	3, NS2_BTN_U4_GR	},
+	{ BTN_GRIPL,	3, NS2_BTN_U4_GL	},
+	{ BTN_C,	1, NS2_BTN_U2_C		},
+	{ /* sentinel */ },
+};
+
 static const struct switch2_ctlr_button_mapping ns2_gccon_mappings[] = {
 	{ BTN_SOUTH,	0, NS2_BTNR_A,		},
 	{ BTN_EAST,	0, NS2_BTNR_B,		},
@@ -3175,6 +3259,23 @@ static const struct switch2_ctlr_button_mapping ns2_gccon_mappings[] = {
 	{ /* sentinel */ },
 };
 
+static const struct switch2_ctlr_button_mapping ns2_gccon_unified_mappings[] = {
+	{ BTN_SOUTH,	0, NS2_BTN_U1_A,	},
+	{ BTN_EAST,	0, NS2_BTN_U1_B,	},
+	{ BTN_NORTH,	0, NS2_BTN_U1_X,	},
+	{ BTN_WEST,	0, NS2_BTN_U1_Y,	},
+	{ BTN_TL2,	2, NS2_BTN_U3_L,	},
+	{ BTN_TR2,	0, NS2_BTN_U1_R,	},
+	{ BTN_TL,	2, NS2_BTN_U3_ZL	},
+	{ BTN_TR,	0, NS2_BTN_U1_ZR	},
+	{ BTN_SELECT,	1, NS2_BTN_U2_MINUS,	},
+	{ BTN_START,	1, NS2_BTN_U2_PLUS,	},
+	{ BTN_MODE,	1, NS2_BTN_U2_HOME	},
+	{ KEY_RECORD,	1, NS2_BTN_U2_CAPTURE	},
+	{ BTN_C,	1, NS2_BTN_U2_C		},
+	{ /* sentinel */ },
+};
+
 static const uint8_t switch2_init_cmd_data[] = {
 	/*
 	 * The last 6 bytes of this packet are the MAC address of
@@ -3787,11 +3888,51 @@ static int switch2_event(struct hid_device *hdev, struct hid_report *report, uin
 
 	switch (report->id) {
 	case NS2_REPORT_UNIFIED:
-		/*
-		 * TODO
-		 * This won't be sent unless the report type gets changed via command
-		 * 03-0A, but we should support it at some point regardless.
-		 */
+		if (size < 0x3f)
+			return -EINVAL;
+
+		switch (ns2->ctlr_type) {
+		case NS2_CTLR_TYPE_JCL:
+			switch2_report_stick(input, &ns2->stick_calib[0],
+				ABS_X, false, ABS_Y, true, &raw_data[11]);
+			switch2_report_buttons(input, &raw_data[5],
+				ns2_left_joycon_button_unified_mappings);
+			break;
+		case NS2_CTLR_TYPE_JCR:
+			switch2_report_stick(input, &ns2->stick_calib[0],
+				ABS_X, false, ABS_Y, true, &raw_data[14]);
+			switch2_report_buttons(input, &raw_data[5],
+				ns2_right_joycon_button_unified_mappings);
+			break;
+		case NS2_CTLR_TYPE_GC:
+			input_report_abs(input, ABS_HAT0X,
+				!!(raw_data[7] & NS2_BTN_U3_RIGHT) -
+				!!(raw_data[7] & NS2_BTN_U3_LEFT));
+			input_report_abs(input, ABS_HAT0Y,
+				!!(raw_data[7] & NS2_BTN_U3_DOWN) -
+				!!(raw_data[7] & NS2_BTN_U3_UP));
+			switch2_report_buttons(input, &raw_data[5], ns2_gccon_unified_mappings);
+			switch2_report_stick(input, &ns2->stick_calib[0],
+				ABS_X, false, ABS_Y, true, &raw_data[11]);
+			switch2_report_stick(input, &ns2->stick_calib[1],
+				ABS_RX, false, ABS_RY, true, &raw_data[14]);
+			switch2_report_trigger(input, ns2->lt_zero, ABS_Z, raw_data[0x3d]);
+			switch2_report_trigger(input, ns2->rt_zero, ABS_RZ, raw_data[0x3e]);
+			break;
+		case NS2_CTLR_TYPE_PRO:
+			input_report_abs(input, ABS_HAT0X,
+				!!(raw_data[7] & NS2_BTN_U3_RIGHT) -
+				!!(raw_data[7] & NS2_BTN_U3_LEFT));
+			input_report_abs(input, ABS_HAT0Y,
+				!!(raw_data[7] & NS2_BTN_U3_DOWN) -
+				!!(raw_data[7] & NS2_BTN_U3_UP));
+			switch2_report_buttons(input, &raw_data[5], ns2_procon_unified_mappings);
+			switch2_report_stick(input, &ns2->stick_calib[0],
+				ABS_X, false, ABS_Y, true, &raw_data[11]);
+			switch2_report_stick(input, &ns2->stick_calib[1],
+				ABS_RX, false, ABS_RY, true, &raw_data[14]);
+			break;
+		}
 		break;
 	case NS2_REPORT_JCL:
 		switch2_report_stick(input, &ns2->stick_calib[0], ABS_X, false,
Signature · 1 line
2.54.0
#6 lore raw

Re: [PATCH v9 3/3] HID: nintendo: Add unified report format support

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

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

Pre-existing issues:
- [Critical] LED class devices registered with devm_led_classdev_register() outlive their containing switch2_controller structure, leading to a UAF if accessed via sysfs after the device is unplugged. --

commit 84beb320490de870df51e881a9a77c861e3838ca
Author: Vicki Pfau <[email protected]>

HID: nintendo: Add unified report format support

This adds support for the "unified" report format that all controllers also support, which has overlapping fields for like buttons and axes between them.

drivers/hid/hid-nintendo.c

Quoted 4 lines
drivers/hid/hid-nintendo.c +0 -0
index 96463e1586bbd..b406e6cfd221f 100644

[ ... ]

[Severity: Critical]
This is a pre-existing issue, but can this lead to a use-after-free when the device is unplugged?

Looking at switch2_init_input(), LEDs are registered with a devm action tied to input->dev:

drivers/hid/hid-nintendo.c:switch2_init_input() {
    ...
    ret = devm_led_classdev_register(&input->dev, led);
    ...
}

This means their teardown action is delayed until the input device's kobject is released.

However, the ns2 structure containing the leds array is freed manually in switch2_kref_put() upon device removal:

drivers/hid/hid-nintendo.c:switch2_kref_put() {
    ...
    kfree(ns2);
    ...
}

Because the LED sysfs device is a child of input->dev, it prevents input->dev from being released until it is unregistered. This creates a circular dependency where the devm cleanup is never triggered.

If a process keeps the LED sysfs file open, input->dev remains alive, but ns2 is freed. Does this leave the LED sysfs endpoints pointing to freed memory, potentially executing function pointers from the freed ns2 structure when writing to the LED sysfs brightness file?

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