Skip to contentSkip to conversation

HID: nintendo: Add preliminary Switch 2 controller driver

PATCH Superseded Vicki Pfau 2 participants 7 messages Sep 29

Subsystems (2)

v14 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Vicki Pfau

author maintainer
#0 lore raw

v14 cover 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.

Since v13 two changes were made:
- The nintendo-switch2-usb supplementary driver was renamed to hid-nintendo-switch2-usb and moved into the drivers/hid directory.
- An additional patch was added, allowing the NYXI Hyperion 3 third-party controller to work properly, as it had a broken report descriptor.

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

 MAINTAINERS                            |    1 +
 drivers/hid/Kconfig                    |   30 +-
 drivers/hid/Makefile                   |    1 +
 drivers/hid/hid-ids.h                  |    4 +
 drivers/hid/hid-nintendo-switch2-usb.c |  475 +++++++
 drivers/hid/hid-nintendo.c             | 1701 +++++++++++++++++++++++-
 drivers/hid/hid-nintendo.h             |   72 +
 7 files changed, 2241 insertions(+), 43 deletions(-)
 create mode 100644 drivers/hid/hid-nintendo-switch2-usb.c
 create mode 100644 drivers/hid/hid-nintendo.h
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v14 1/4 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                    |   22 +-
 drivers/hid/Makefile                   |    1 +
 drivers/hid/hid-ids.h                  |    4 +
 drivers/hid/hid-nintendo-switch2-usb.c |  475 +++++++++
 drivers/hid/hid-nintendo.c             | 1277 +++++++++++++++++++++++-
 drivers/hid/hid-nintendo.h             |   72 ++
 7 files changed, 1842 insertions(+), 10 deletions(-)
 create mode 100644 drivers/hid/hid-nintendo-switch2-usb.c
 create mode 100644 drivers/hid/hid-nintendo.h
MAINTAINERS +1 -0
index a8ffd8336dab..824e63675e8f 100644
@@ -19374,6 +19374,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 +18 -4
index 0e3a0ccd6901..0826ac69874f 100644
@@ -860,10 +860,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.
@@ -878,6 +881,17 @@ config NINTENDO_FF
 	controller, and the NSO N64 controller. For the pro controller, both
 	rumble motors can be controlled individually.
 
+config HID_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 hid-nintendo-switch2-usb.
+
 config HID_NTI
 	tristate "NTI keyboard adapters"
 	help
drivers/hid/Makefile +1 -0
index 79384d905e00..d3f9f54ad328 100644
@@ -95,6 +95,7 @@ obj-$(CONFIG_HID_MONTEREY)	+= hid-monterey.o
 obj-$(CONFIG_HID_MSI)		+= hid-msi.o
 obj-$(CONFIG_HID_MULTITOUCH)	+= hid-multitouch.o
 obj-$(CONFIG_HID_NINTENDO)	+= hid-nintendo.o
+obj-$(CONFIG_HID_NINTENDO_SWITCH2_USB)	+= hid-nintendo-switch2-usb.o
 obj-$(CONFIG_HID_NTI)			+= hid-nti.o
 obj-$(CONFIG_HID_NTRIG)		+= hid-ntrig.o
 obj-$(CONFIG_HID_NVIDIA_SHIELD)	+= hid-nvidia-shield.o
drivers/hid/hid-ids.h +4 -0
index a9537b7bb03d..aacb8fff0757 100644
@@ -1124,6 +1124,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-switch2-usb.c new file +475 -0

475 lines not on this page · raw message

drivers/hid/hid-nintendo.c +1271 -6

1,356 lines not on this page · raw message

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

72 lines not on this page · raw message

Signature · 1 line
2.54.0
author maintainer
#3 lore raw

PATCH v14 2/4 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 | 213 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 215 insertions(+), 6 deletions(-)
drivers/hid/Kconfig +4 -4
index 0826ac69874f..62eb9a96e1c5 100644
@@ -876,10 +876,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_NINTENDO_SWITCH2_USB
 	tristate "Wired Nintendo Switch 2 controller support"
drivers/hid/hid-nintendo.c +211 -2

275 lines not on this page · raw message

Signature · 1 line
2.54.0
author maintainer
#5 lore raw

PATCH v14 3/4 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 e965076b1034..c7e450cfb8b0 100644
@@ -2915,6 +2915,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
@@ -3163,6 +3193,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,		},
@@ -3180,6 +3226,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,		},
@@ -3201,6 +3264,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,		},
@@ -3218,6 +3302,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
@@ -3850,11 +3951,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
author maintainer
#6 lore raw

PATCH v14 4/4 HID: nintendo: Patch NYXI Hyperion 3 descriptor

In reply to Vicki Pfau

The HID report descriptor for the third-party NYXI Hyperion 3's right Joy-Con erroneously reports it as using the report ID for the left Joy-Con. Since it does actually send the report ID for the right Joy-Con, the HID subsystem rejects it as unknown. Here we patch it to properly report as the right Joy-Con report ID to ensure the HID subsystem passes it through properly.

---
 drivers/hid/hid-nintendo.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/hid/hid-nintendo.c +16 -0
index c7e450cfb8b0..16f1860723fc 100644
@@ -4500,6 +4500,21 @@ static int nintendo_hid_event(struct hid_device *hdev,
 		return joycon_event(hdev, report, raw_data, size);
 }
 
+static const __u8 *nintendo_hid_report_fixup(struct hid_device *hdev,
+			    __u8 *rdesc, unsigned int *rsize)
+{
+	if (hdev->vendor == USB_VENDOR_ID_NINTENDO &&
+		hdev->product == USB_DEVICE_ID_NINTENDO_NS2_JOYCONR &&
+		*rsize == 100 && rdesc[24] == NS2_REPORT_JCL)
+		/*
+		 * The NYXI Hyperion 3 right Joy-Con accidentally reports as the
+		 * left one. Since the HID subsystem rejects report IDs that
+		 * aren't listed in the descriptor, we need to patch it.
+		 */
+		rdesc[24] = NS2_REPORT_JCR;
+	return rdesc;
+}
+
 static int nintendo_hid_probe(struct hid_device *hdev,
 			    const struct hid_device_id *id)
 {
@@ -4528,6 +4543,7 @@ static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message)
 static struct hid_driver nintendo_hid_driver = {
 	.name		= "nintendo",
 	.id_table	= nintendo_hid_devices,
+	.report_fixup	= nintendo_hid_report_fixup,
 	.probe		= nintendo_hid_probe,
 	.remove		= nintendo_hid_remove,
 	.raw_event	= nintendo_hid_event,
Signature · 1 line
2.54.0
#2 lore raw
In reply to Vicki Pfau
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] input: memory leak and UAF due to devm actions on unmanaged input_dev - [High] usb: TOCTOU race…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] input: memory leak and UAF due to devm actions on unmanaged input_dev
- [High] usb: TOCTOU race condition causing UAF in message_in_work
- [High] usb: infinite interrupt storm on -EPIPE URB completion
--

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

This commit introduces preliminary support for the Nintendo Switch 2 controllers. It uses a split-driver design with a main HID interface driver and a secondary configuration interface driver to handle input and non-standard communications over USB and Bluetooth.

Quoted 5 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +0 -0
new file mode 100644
index 0000000000000..fabe6b3f4163c

[ ... ]

Quoted 8 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +8 -0
+static void switch2_bulk_in(struct urb *urb)
+{
+	struct switch2_usb *ns2_usb = urb->context;
+	int i;
+	bool schedule = false;
+	unsigned long flags;
+
+	switch (urb->status) {

drivers/hid/hid-nintendo-switch2-usb.c line 68

Quoted 10 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +10 -0
+	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;
+	case -EPIPE:
+		break;

[Severity: High]
Does this error path handle halted endpoints correctly? The EPIPE status indicates a stalled endpoint. By breaking out here without setting the schedule flag, the code avoids scheduling work, but it proceeds to the loop below where it immediately re-anchors and resubmits the URB via usb_submit_urb() without clearing the halt condition. This can cause the hardware to instantly complete the new URB with EPIPE again, leading to an infinite interrupt storm in softirq context and a CPU lockup.

Quoted 40 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +40 -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);
+	if (ns2_usb->shutdown)
+		schedule = false;
+
+	for (i = 0; i < NS2_IN_URBS; i++) {
+		int err;
+		struct switch2_urb *ns2_urb;
+
+		if (ns2_usb->bulk_in[i].urb == urb) {
+			if (schedule) {
+				ns2_usb->bulk_in[i].state = NS2_URB_IN;
+				continue;
+			} else {
+				ns2_usb->bulk_in[i].state = NS2_URB_FREE;
+			}
+		}
+
+		if (ns2_usb->bulk_in[i].state != NS2_URB_FREE)
+			continue;
+
+		/*
+		 * We want exactly one bulk in URB scheduled at a time, so only
+		 * reschedule this immediately if nothing else is scheduled
+		 * currently.
+		 */
+		if (!usb_anchor_empty(&ns2_usb->bulk_in_anchor) || ns2_usb->shutdown)
+			continue;
+
+		ns2_urb = &ns2_usb->bulk_in[i];
+		if (!ns2_urb)
+			continue;
+
+		usb_anchor_urb(ns2_urb->urb, &ns2_usb->bulk_in_anchor);
+		err = usb_submit_urb(ns2_urb->urb, GFP_ATOMIC);
+		if (err) {

drivers/hid/hid-nintendo-switch2-usb.c line 118

Quoted 10 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +10 -0
+			usb_unanchor_urb(ns2_urb->urb);
+			dev_dbg(&ns2_usb->udev->dev, "failed to queue input urb: %d\n", err);
+		} else {
+			ns2_urb->state = NS2_URB_OUT;
+		}
+	}
+	spin_unlock_irqrestore(&ns2_usb->bulk_in_lock, flags);
+
+	if (schedule)
+		schedule_work(&ns2_usb->message_in_work);

[Severity: High]
Can calling schedule_work() outside the spinlock create a TOCTOU race against switch2_usb_disconnect()?

If switch2_bulk_in() checks ns2_usb->shutdown, releases the lock, and is then preempted before calling schedule_work(), switch2_usb_disconnect() could run. It would acquire the lock, set shutdown to true, release the lock, and call cancel_work_sync(&ns2_usb->message_in_work). Since the work is not yet queued, cancel_work_sync() returns immediately, and devres frees ns2_usb. When switch2_bulk_in() resumes, it will call schedule_work() on freed memory, causing a use-after-free.

Quoted 1 line

+}

[ ... ]

Quoted 4 lines
drivers/hid/hid-nintendo.c +0 -0
index b1fcf3e5767ba..3a7c35375be60 100644

[ ... ]

Quoted 13 lines
drivers/hid/hid-nintendo.c +13 -0
+static int switch2_player_led_brightness_set(struct led_classdev *led,
+					    enum led_brightness brightness)
+{
+	struct device *dev = led->dev->parent;
+	struct input_dev *input = to_input_dev(dev);
+	struct switch2_controller *ns2 = input_get_drvdata(input);
+
+	if (!ns2)
+		return -ENODEV;
+
+	guard(mutex)(&ns2->lock);
+	return switch2_set_leds(ns2);
+}

[ ... ]

Quoted 3 lines
drivers/hid/hid-nintendo.c +3 -0
+static int switch2_init_input(struct switch2_controller *ns2)
+{
+	struct input_dev *input;

[ ... ]

Quoted 3 lines
drivers/hid/hid-nintendo.c +3 -0
+	input = input_allocate_device();
+	if (!input)
+		return -ENOMEM;

[ ... ]

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

Quoted 1 line
drivers/hid/hid-nintendo.c +1 -0
+	ns2->leds = devm_kcalloc(&input->dev, JC_NUM_LEDS, sizeof(*ns2->leds), GFP_KERNEL);

[Severity: High]
Does allocating devm resources tied to an unmanaged input device create a memory leak and use-after-free risk?

The input device here is allocated manually with input_allocate_device(). Using devm_kcalloc() and later devm_led_classdev_register() with &input->dev as the parent means the LED devices take a reference to their parent input device. When the device is removed, input_unregister_device() is called, but the child LED devices hold the reference so the input device is never freed.

This leaves the devres actions unexecuted, leaking the input device and leaving the LED devices active in sysfs. Meanwhile, the driver frees the ns2 structure. If userspace then writes to the LED brightness file, switch2_player_led_brightness_set() will execute, fetch the freed ns2 pointer, and dereference it, leading to a deterministic use-after-free.

Quoted 8 lines
drivers/hid/hid-nintendo.c +8 -0
+	if (!ns2->leds) {
+		hid_err(ns2->hdev, "Failed to allocate LEDs\n");
+		input_unregister_device(input);
+		return -ENOMEM;
+	}
+
+	for (i = 0; i < JC_NUM_LEDS; i++) {
+		struct led_classdev *led = &ns2->leds[i];

[ ... ]

Quoted 14 lines
drivers/hid/hid-nintendo.c +14 -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",
+				i + 1, ret);
+			break;
+		}
+	}
+
+	rcu_assign_pointer(ns2->input, input);
+	synchronize_rcu();
+
+	switch2_init_step_done(ns2, NS2_INIT_INPUT);
+	return switch2_init_controller(ns2);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH v14 2/4] HID: nintendo: Add rumble support for Switch 2 controllers

In reply to Vicki Pfau
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] hid: timer catch-up storm in switch2_rumble_work() floods USB bus --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] hid: timer catch-up storm in switch2_rumble_work() floods USB bus --

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

HID: nintendo: Add rumble support for Switch 2 controllers

This commit adds rumble support for Switch 2 controllers, handling both the HD Rumble linear resonant actuator type and the GameCube eccentric rotating mass type. It implements a basic simulation of these rumble effects using delayed work.

Quoted 4 lines
drivers/hid/hid-nintendo.c +0 -0
index 3a7c35375be60..e965076b10345 100644

[ ... ]

Quoted 1 line
drivers/hid/hid-nintendo.c +0 -0
@@ -3251,6 +3305,128 @@ static void switch2_kref_put(struct kref *refcount)

[ ... ]

Quoted 2 lines
drivers/hid/hid-nintendo.c +2 -0
+static void switch2_rumble_work(struct work_struct *work)
+{

[ ... ]

Quoted 6 lines
drivers/hid/hid-nintendo.c +6 -0
+	if (active) {
+		unsigned long interval = msecs_to_jiffies(4);
+		uint64_t current_jiffies = get_jiffies_64();
+
+		if (!ns2->last_rumble_work)
+			ns2->last_rumble_work = current_jiffies;

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

Quoted 10 lines
drivers/hid/hid-nintendo.c +10 -0
+		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);
+		else
+			schedule_delayed_work(&ns2->rumble_work, 0);

[Severity: Medium]
Could this logic lead to a timer catch-up storm?

If the worker execution is delayed (for example, due to CPU load, workqueue starvation, or a suspend/resume cycle), the target time is only advanced by 4ms on each execution, while the system time may be much further ahead.

When this happens, the work immediately reschedules itself with a 0 delay. Because it might take many executions to catch up to the current time 4ms at a time, this could loop rapidly. Is there a risk that this will hog the workqueue and flood the USB subsystem with back-to-back synchronous hid_hw_output_report calls, which might also distort the intended 4ms haptic cadence?

Quoted 3 lines
drivers/hid/hid-nintendo.c +3 -0
+	} else {
+		ns2->last_rumble_work = 0;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2