Skip to contentSkip to conversation

HID: nintendo: Add preliminary Switch 2 controller driver

PATCH Superseded Vicki Pfau 2 participants 7 messages Oct 2

Subsystems (2)

v15 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Vicki Pfau

author maintainer
#0 lore raw

v15 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.

There are a few minor changes over v14: better handling of -EPIPE, switching to using kzalloc_obj where applicable, and renaming the config option for the hid-nintendo-switch2-usb driver from CONFIG_HID_NINTENDO_SWITCH2_USB to CONFIG_NINTENDO_SWITCH2_USB to match other hid driver "suboptions."

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 |  474 +++++++
 drivers/hid/hid-nintendo.c             | 1701 +++++++++++++++++++++++-
 drivers/hid/hid-nintendo.h             |   72 +
 7 files changed, 2240 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 v15 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 |  474 +++++++++
 drivers/hid/hid-nintendo.c             | 1277 +++++++++++++++++++++++-
 drivers/hid/hid-nintendo.h             |   72 ++
 7 files changed, 1841 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 +474 -0

474 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 v15 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 v15 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 c185ad4a27c9..533a2560bfb9 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 v15 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 533a2560bfb9..d114cc389aac 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 4 potential issue(s) to consider: - [High] hid: nintendo: devm_led_classdev_register causes circular reference and UAF - [High] hid…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] hid: nintendo: devm_led_classdev_register causes circular reference and UAF
- [High] hid: nintendo: switch2_usb_message_in_work accesses ns2 without kref leading to UAF
- [High] hid: nintendo: state machine restart in switch2_remove leads to UAF and leak
- [High] hid: nintendo: switch2_usb_disconnect UAF due to missing usb_kill_urb --

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

HID: nintendo: Add preliminary Switch 2 controller driver

This commit adds a new driver for Nintendo Switch 2 controllers utilizing a split-interface design. It supports USB communication for Switch 2 Joy-Cons, Pro Controllers, and GameCube NSO controllers.

Quoted 3 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +0 -0

[ ... ]

Quoted 8 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +8 -0
+static void switch2_usb_message_in_work(struct work_struct *work)
+{
+	struct switch2_usb *ns2_usb = container_of(work, struct switch2_usb, message_in_work);
+	struct switch2_urb *urb;
+	int err;
+	int i;
+	unsigned long flags;
+

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

Quoted 10 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +10 -0
+	spin_lock_irqsave(&ns2_usb->bulk_in_lock, flags);
+	for (i = 0; i < NS2_IN_URBS; i++) {
+		urb = &ns2_usb->bulk_in[i];
+		if (urb->state != NS2_URB_IN)
+			continue;
+		spin_unlock_irqrestore(&ns2_usb->bulk_in_lock, flags);
+
+		if (ns2_usb->cfg.parent) {
+			err = switch2_receive_command(ns2_usb->cfg.parent,
+				urb->urb->transfer_buffer, urb->urb->actual_length);

[Severity: High]
Does this access the parent controller without holding a reference?

If switch2_controller_attach_cfg() fails, the kref on the parent controller is dropped before cancelling this worker. At the same time, if the HID device unbinds, switch2_remove() can drop the final kref and free the structure.

If this worker thread is preempted after reading the pointer but before acquiring the lock, could it wake up and call switch2_receive_command() on the freed structure?

Quoted 3 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +3 -0
+			if (err)
+				dev_dbg(&ns2_usb->udev->dev, "receive command failed: %d\n", err);
+		} else {

[ ... ]

Quoted 5 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +5 -0
+static void switch2_usb_disconnect(struct usb_interface *intf)
+{
+	struct switch2_usb *ns2_usb = usb_get_intfdata(intf);
+	unsigned long flags;
+	struct urb *urb;

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

Quoted 10 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +10 -0
+	uint8_t *data;
+	int i;
+
+	/* 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);
+
+	usb_kill_anchored_urbs(&ns2_usb->bulk_in_anchor);
+	cancel_work_sync(&ns2_usb->message_in_work);

[Severity: High]
Could this lead to a use-after-free if an unanchored URB completes?

The USB core unanchors URBs before invoking their completion handlers. If switch2_bulk_in() is executing, usb_kill_anchored_urbs() will not find it.

This allows cancel_work_sync() to return immediately. When the preempted completion handler resumes, it will execute schedule_work() on the devres managed structure that may have already been freed.

Should this explicitly wait for unanchored URBs by tracking them and calling usb_kill_urb() on each?

Quoted 7 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +3 -0
+	for (i = 0; i < NS2_IN_URBS; i++) {
+		spin_lock_irqsave(&ns2_usb->bulk_in_lock, flags);
+		urb = ns2_usb->bulk_in[i].urb;
drivers/hid/hid-nintendo.c +0 -0

[ ... ]

Quoted 7 lines
drivers/hid/hid-nintendo.c +7 -0
+static int switch2_init_input(struct switch2_controller *ns2)
+{
+	struct input_dev *input;
+	struct hid_device *hdev = ns2->hdev;
+	int player_led_pattern;
+	int i;
+	int ret;

[ ... ]

Quoted 4 lines
drivers/hid/hid-nintendo.c +4 -0
+		led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE | LED_RETAIN_AT_SHUTDOWN;
+		char *name = devm_kasprintf(&input->dev, GFP_KERNEL, "%s:%s:%s",
+				      dev_name(&input->dev),
+				      "green",

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

Quoted 10 lines
drivers/hid/hid-nintendo.c +10 -0
+				      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;
+		ret = devm_led_classdev_register(&input->dev, led);

[Severity: High]
Does this create an unbreakable devres circular reference?

Using the input device as the parent for devm_led_classdev_register() links the devres action to the input device, but the LED classdev child inherently holds a reference to its parent.

When input_unregister_device() runs during removal, the input device's refcount never drops to zero, leaking it and preventing devres cleanup. The LED sysfs attributes remain active.

If userspace writes to the leaked LED attributes after switch2_remove() frees the controller structure, switch2_player_led_brightness_set() will access freed memory.

Quoted 3 lines
drivers/hid/hid-nintendo.c +3 -0
+		if (ret < 0) {
+			dev_err(&input->dev, "Failed to register player %d LED; ret=%d\n",
+				i + 1, ret);

[ ... ]

Quoted 7 lines
drivers/hid/hid-nintendo.c +7 -0
+static void switch2_controller_put(struct switch2_controller *ns2)
+{
+	struct input_dev *input;
+
+	mutex_lock(&ns2->lock);
+	rcu_read_lock();
+	input = rcu_dereference(ns2->input);

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

Quoted 10 lines
drivers/hid/hid-nintendo.c +10 -0
+	rcu_read_unlock();
+
+	rcu_assign_pointer(ns2->input, NULL);
+	synchronize_rcu();
+
+	ns2->init_step = 0;
+	mutex_unlock(&ns2->lock);
+
+	if (input)
+		input_unregister_device(input);

[Severity: High]
Does clearing the initialization step before the blocking unregister open a race condition that leaks the input device?

By dropping the lock before calling the long-running input_unregister_device(), concurrent USB packets can trigger switch2_receive_command(). Since the lock is re-acquired and init_step is 0, this restarts the initialization state machine.

Because switch2_remove() hasn't cleared hdev yet:

switch2_remove()
switch2_controller_put(ns2);
mutex_lock(&ns2->lock);
ns2->hdev = NULL;

The state machine successfully registers a completely new input_dev using the soon-to-be-destroyed hdev as its parent. This leaks the newly registered device and leaves it with a dangling parent pointer.

Quoted 3 lines
drivers/hid/hid-nintendo.c +3 -0
+}
+
+static void switch2_kref_put(struct kref *refcount)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH v15 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 2 potential issue(s) to consider: - [High] hid: nintendo: Infinite catch-up loop in switch2_rumble_work - [High] hid: nintendo: AB-BA…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] hid: nintendo: Infinite catch-up loop in switch2_rumble_work
- [High] hid: nintendo: AB-BA Deadlock in switch2_init_input error path
--

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

HID: nintendo: Add rumble support for Switch 2 controllers

This commit introduces rumble support for Nintendo Switch 2 controllers, handling both HD Rumble (linear resonant actuators) and eccentric rotating mass haptics. It manages rumble states using a delayed workqueue to stream reports.

Quoted 4 lines
drivers/hid/hid-nintendo.c +0 -0
index 81d0a25d9846b..c185ad4a27c97 100644

[ ... ]

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

[ ... ]

Quoted 4 lines
drivers/hid/hid-nintendo.c +4 -0
+static void switch2_rumble_work(struct work_struct *work)
+{
+	struct switch2_controller *ns2 = container_of(to_delayed_work(work),
+						      struct switch2_controller, rumble_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: High]
Could this create an unbounded catch-up loop under load or after a suspend?

If switch2_rumble_work is delayed (for example, by lock contention or a suspend/resume cycle) and falls significantly behind current_jiffies, it reschedules itself with a 0 delay while only advancing last_rumble_work by 4ms. If the execution of the work itself takes longer than 4ms, current_jiffies will grow faster than last_rumble_work can catch up.

Can this lead to a situation where the worker spins constantly, pegging a CPU core and flooding the controller with stale rumble packets?

Quoted 9 lines
drivers/hid/hid-nintendo.c +9 -0
+	} else {
+		ns2->last_rumble_work = 0;
+	}
+
+	mutex_lock(&ns2->lock);
+	if (!ns2->hdev) {
+		cancel_delayed_work(&ns2->rumble_work);
+	} else {
+		ret = hid_hw_output_report(ns2->hdev, ns2->rumble_buffer, 64);

[ ... ]

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

[ ... ]

Quoted 7 lines
drivers/hid/hid-nintendo.c +7 -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;
+	}
+	ret = devm_delayed_work_autocancel(&input->dev, &ns2->rumble_work, switch2_rumble_work);

[ ... ]

drivers/hid/hid-nintendo.c

Quoted 6 lines
drivers/hid/hid-nintendo.c +6 -0
+	ns2->leds = devm_kcalloc(&input->dev, JC_NUM_LEDS, sizeof(*ns2->leds), GFP_KERNEL);
+	if (!ns2->leds) {
+		hid_err(ns2->hdev, "Failed to allocate LEDs\n");
+		input_unregister_device(input);
+		return -ENOMEM;
+	}

[Severity: High]
Does this error path introduce an AB-BA deadlock with the rumble worker?

switch2_receive_command() acquires ns2->lock before calling switch2_init_input(). Because the input device is registered before this allocation, userspace can immediately open the evdev node and issue a rumble effect, queuing switch2_rumble_work.

If devm_kcalloc() fails, it calls input_unregister_device(input). Since userspace might have already closed the node, dropping the last reference triggers synchronous devm cleanup (because the work is registered via devm_delayed_work_autocancel). This cleanup will invoke cancel_delayed_work_sync(&ns2->rumble_work) while this thread still holds ns2->lock.

However, switch2_rumble_work() needs to acquire ns2->lock to complete, as seen in the earlier snippet. Can this result in a permanent deadlock?

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