HID: nintendo: Add preliminary Switch 2 controller
Subsystems (3)
Vicki Pfau
maintainerv10 cover HID: nintendo: Add preliminary Switch 2 controller
This series adds preliminary support for Switch 2 controllers using the same split-driver model as previous versions. This is a minor iteration on v9, fixing errors pointed out by sashiko and moving both the led structs and the rumble work to be devm-allocated based on the input device.
I think several of the concurrency issues pointed out by sashiko are impossible (e.g. a HID device receiving events concurrently with the remove handler), otherwise there are some major issues with lots and lots of drivers.
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 | 1681 ++++++++++++++++- drivers/hid/hid-nintendo.h | 72 + drivers/input/joystick/Kconfig | 11 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/nintendo-switch2-usb.c | 468 +++++ 8 files changed, 2216 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
Vicki Pfau
maintainerPATCH v10 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 | 1276 ++++++++++++++++- drivers/hid/hid-nintendo.h | 72 + drivers/input/joystick/Kconfig | 11 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/nintendo-switch2-usb.c | 468 ++++++ 8 files changed, 1834 insertions(+), 10 deletions(-) create mode 100644 drivers/hid/hid-nintendo.h create mode 100644 drivers/input/joystick/nintendo-switch2-usb.c
MAINTAINERS +1 -0
@@ -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
@@ -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
@@ -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 +1270 -6
1,355 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
@@ -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
@@ -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 +468 -0
468 lines not on this page · raw message
Signature · 1 line
2.54.0
Vicki Pfau
maintainerPATCH v10 2/3 HID: nintendo: Add rumble support for Switch 2 controllers
In reply to Vicki PfauThis 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 | 214 ++++++++++++++++++++++++++++++++++++- 2 files changed, 216 insertions(+), 6 deletions(-)
drivers/hid/Kconfig +4 -4
@@ -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 +212 -2
282 lines not on this page · raw message
Signature · 1 line
2.54.0
Vicki Pfau
maintainerPATCH v10 3/3 HID: nintendo: Add unified report format support
In reply to Vicki PfauThis 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
@@ -2873,6 +2873,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
@@ -3121,6 +3151,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 , },
@@ -3138,6 +3184,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 , },
@@ -3159,6 +3222,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 , },
@@ -3176,6 +3260,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
@@ -3802,11 +3903,51 @@ static int switch2_event(struct hid_device *hdev, struct hid_report *report, uinswitch (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
[email protected]
AI reviewRe: [PATCH v10 1/3] HID: nintendo: Add preliminary Switch 2 controller driver
In reply to Vicki PfauThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Calling `input_free_device()` on a successfully registered `input_dev` when subsequent allocations…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Calling input_free_device() on a successfully registered input_dev when subsequent allocations fail.
- [High] Use-After-Free of ns2->leds due to devm lifetime mismatch when reusing the global controller structure.
- [High] Race condition causing NULL pointer dereference of ns2->cfg during USB initialization error.
- [High] Unconditional ida_free() with U32_MAX causes a kernel BUG_ON.
--
commit be3a7ca67ec1bc0f796b8ba1203737257398ef18
Author: Vicki Pfau <[email protected]>
HID: nintendo: Add preliminary Switch 2 controller driver
This commit adds a new driver for the Switch 2 controllers. It utilizes a split-interface design where input and rumble occur on the main HID interface, while all other communication uses a configuration interface. The current implementation supports general input for Joy-Con 2, Pro Controller 2, and GameCube NSO controllers over USB.
Quoted 9 lines
drivers/hid/hid-nintendo.c +3
-0@@ -2838,7 +2845,1206 @@ static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) +static int switch2_init_input (struct switch2_controller *ns2) +{ + ret =input_register_device (input);
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3351
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+if (ret <0 ) { +hid_err (ns2->hdev,"Failed to register input; ret=%d\n" , ret); +input_free_device (input); +return ret; + } + + player_led_pattern = ns2->player_id %JC_NUM_LED_PATTERNS ; +hid_dbg (hdev,"assigned player %d led pattern" , player_led_pattern +1 ); + + ns2->leds =devm_kcalloc (&input->dev,JC_NUM_LEDS ,sizeof (*ns2->leds),GFP_KERNEL );
[Severity: High]
Since the ns2 structure is reused per physical port via switch2_get_controller(), could there be a lifetime mismatch here?
If a device is unplugged but its evdev file descriptor is kept open by userspace, input->dev stays alive. If a new device is then plugged into the same port, ns2 is reused and ns2->leds is overwritten with an allocation tied to the new input_dev.
When the new device is unplugged, freeing its devm resources, would a write to the original device's LED sysfs node in switch2_player_led_brightness_set() iterate over freed memory?
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3354
Quoted 3 lines
drivers/hid/hid-nintendo.c +3
-0+if (!ns2->leds) { +hid_err (ns2->hdev,"Failed to allocate LEDs\n" ); +input_free_device (input);
[Severity: High]
Is it safe to use input_free_device() here since input_register_device() has already successfully registered the device above?
Once the input device is registered, won't calling input_free_device() just drop the initial reference rather than properly unregistering the device, leaving dangling sysfs nodes and leaking the device?
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3821
Quoted 12 lines
drivers/hid/hid-nintendo.c +10
-0+return -ENOMEM ; + } +int switch2_receive_command (struct switch2_controller *ns2, +const uint8_t *message,size_t length) +{ +const struct switch2_cmd_header *header; +int ret =0 ; + header = (const struct switch2_cmd_header *)message; +if (!(header->flags &NS2_FLAG_OK )) { + dev_warn(ns2->cfg->dev, "Packet error %02x replying to command %x:%x",
[Severity: High]
Could this lead to a NULL pointer dereference in the error logging path?
In switch2_usb_probe(), we submit the bulk IN URB before calling switch2_controller_attach_cfg(). If the device immediately returns an error packet, switch2_receive_command() might run concurrently.
If attach_cfg fails and clears ns2->cfg to NULL, could the workqueue attempt to log the error using dev_warn(ns2->cfg->dev, ...) and crash?
Quoted 7 lines
drivers/hid/hid-nintendo.c +6
-0+ header->flags, header->command, header->subcommand); + ret = -EIO ; + goto exit; + } +static int switch2_probe (struct hid_device *hdev,const struct hid_device_id *id) +{
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 4023
Quoted 10 lines
drivers/hid/hid-nintendo.c +8
-0+ ns2->player_id =U32_MAX ; + ret =ida_alloc (&nintendo_player_id_allocator,GFP_KERNEL ); +if (ret <0 ) +hid_warn (hdev,"Failed to allocate player ID, skipping; ret=%d\n" , ret); +else + ns2->player_id = ret; +err_cleanup: +ida_free (&nintendo_player_id_allocator, ns2->player_id);
[Severity: High]
If ida_alloc() fails, ns2->player_id is left as U32_MAX.
If the driver probe subsequently fails and enters this cleanup path, won't ida_free() with U32_MAX (which casts to a negative integer) trigger a BUG_ON in the IDA core?
Quoted 7 lines
drivers/hid/hid-nintendo.c +7
-0+ ns2->hdev =NULL ; +mutex_unlock (&ns2->lock); +switch2_controller_put (ns2); +kref_put (&ns2->refcount, switch2_kref_put); + +return ret; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v10 2/3] HID: nintendo: Add rumble support for Switch 2 controllers
In reply to Vicki PfauThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The devres-managed delayed work `ns2->rumble_work` is bound to the `input->dev` lifecycle, but…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The devres-managed delayed work ns2->rumble_work is bound to the input->dev lifecycle, but resides in ns2, leading to a Use-After-Free.
- [High] LEDs registered with devm_led_classdev_register on input->dev outlive the controller context ns2, leading to a Use-After-Free when sysfs attributes are accessed.
- [Medium] Unconditional cancellation of potentially uninitialized ns2->rumble_work in switch2_remove() causes lockdep splats and kernel warnings. --
commit 62e28913646bc7a9de5cfbf6dcd030ac10396625
Author: Vicki Pfau <[email protected]>
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 4 lines
drivers/hid/hid-nintendo.c +0
-0
[ ... ]
drivers/hid/hid-nintendo.c static int switch2_init_input(struct switch2_controller *ns2) · line 3511
Quoted 11 lines
drivers/hid/hid-nintendo.c +7
-0@@ -3332,6 +3502,26 @@ static int switch2_init_input(struct switch2_controller *ns2)return -EINVAL ; } +#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);
[Severity: High]
Could this lead to a use-after-free? ns2->rumble_work is tied to the lifecycle of input->dev, but it resides in ns2. If the physical controller is unplugged:
switch2_remove()
kref_put(&ns2->refcount, switch2_kref_put);
kfree(ns2);If a userspace process holds the input device open, input->dev remains alive. When the file descriptor is finally closed, the devres cleanup for input->dev will execute and attempt to cancel the delayed work on the already-freed ns2 structure.
[Severity: High]
Are there similar lifecycle issues with the LED registration? If the LED class device is registered using devm_led_classdev_register(&input->dev, led), it decouples the LED lifecycle from the controller context.
If the controller is unplugged and ns2 is freed, but the input device remains open, could a write to the sysfs brightness file trigger a use-after-free?
switch2_player_led_brightness_set()
struct switch2_controller *ns2 = input_get_drvdata(input); if (!ns2)
return -ENODEV;
guard(mutex)(&ns2->lock);
Would dereferencing ns2 here cause memory corruption if the context has already been freed?
Quoted 12 lines
drivers/hid/hid-nintendo.c +12
-0+if (ret <0 ) { +input_free_device (input); +return ret; + } + +input_set_capability (input,EV_FF ,FF_RUMBLE ); + ret =input_ff_create_memless (input,NULL , switch2_play_effect); +if (ret) { +input_free_device (input); +return ret; + } +#endif
[ ... ]
drivers/hid/hid-nintendo.c static void switch2_remove(struct hid_device *hdev) · line 4249
Quoted 6 lines
drivers/hid/hid-nintendo.c +2
-0@@ -4038,6 +4245,9 @@ static void switch2_remove(struct hid_device *hdev) ns2->hdev =NULL ;ida_free (&nintendo_player_id_allocator, ns2->player_id);mutex_unlock (&ns2->lock); +#if IS_ENABLED (CONFIG_NINTENDO_FF ) +cancel_delayed_work_sync (&ns2->rumble_work);
[Severity: Medium]
Will this execute safely if the delayed work was never initialized?
Since ns2 is zero-allocated and ns2->rumble_work is only initialized in switch2_init_input(), what happens if the device is unplugged or fails initialization before switch2_init_input() is called?
Would calling cancel_delayed_work_sync() on a zero-initialized work structure trigger lockdep splats or kernel warnings?
Quoted 4 lines
drivers/hid/hid-nintendo.c +1
-0+#endif kref_put (&ns2->refcount, switch2_kref_put);hid_hw_close (hdev);hid_hw_stop (hdev);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2