HID: nintendo: Add preliminary Switch 2 controller
Subsystems (3)
Vicki Pfau
maintainerv12 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 v11, fixing some nitpicks. It should be fully ready now, pending review from Dmitry and a HID tree maintainer.
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 | 1682 ++++++++++++++++- drivers/hid/hid-nintendo.h | 72 + drivers/input/joystick/Kconfig | 11 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/nintendo-switch2-usb.c | 475 +++++ 8 files changed, 2224 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 v12 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 | 1277 ++++++++++++++++- drivers/hid/hid-nintendo.h | 72 + drivers/input/joystick/Kconfig | 11 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/nintendo-switch2-usb.c | 475 ++++++ 8 files changed, 1842 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 +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
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 +475 -0
475 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v12 1/3] HID: nintendo: Add preliminary Switch 2 controller driver
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Registering managed LED class devices on `&input->dev` creates a circular reference that leaks the…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Registering managed LED class devices on &input->dev creates a circular reference that leaks the input device and causes a Use-After-Free of the controller struct (ns2) via sysfs attributes.
- [High] Sequentially plugging different controllers into the same USB port causes a new controller to erroneously adopt the state structure of the old, logically disconnected controller.
- [High] In switch2_remove(), the controller structure ns2 can be freed before the HID hardware is stopped, leading to a Use-After-Free if a HID event arrives during the teardown process.
- [High] Unconditional call to ida_free() with an unallocated ID (U32_MAX) when ida_alloc() fails, triggering a kernel BUG_ON.
- [High] A scheduled USB work can access ns2 after it has been freed during a probe failure.
--
--- Patch [1]: [PATCH v12 1/3] HID: nintendo: Add preliminary Switch 2 controller driver --- commit 4bbec8b8ea15115506e7b7ed2c3578458f9f8016
Author: Vicki Pfau <[email protected]>
Subject: HID: nintendo: Add preliminary Switch 2 controller driver
This commit introduces preliminary driver support for the Nintendo Switch 2 controllers. It uses a split driver model, handling HID features in hid-nintendo and USB configuration in a new nintendo-switch2-usb driver.
Quoted 3 lines
drivers/hid/hid-nintendo.c +0
-0
[ ... ]
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3237
Quoted 11 lines
drivers/hid/hid-nintendo.c +11
-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);
[Severity: High]
Does registering the LED class devices using the input device as the devres parent create a circular reference?
Since devm_led_classdev_register takes a kobject reference to the parent device, the devres cleanup for input->dev might never run while the LED device exists.
If the device is unplugged, ns2 is freed, but the sysfs nodes remain active. Could a subsequent sysfs write to the brightness attribute trigger a use-after-free here when fetching and locking ns2?
Quoted 2 lines
drivers/hid/hid-nintendo.c +2
-0+return switch2_set_leds (ns2); +}
[ ... ]
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3169
Quoted 11 lines
drivers/hid/hid-nintendo.c +11
-0+static struct switch2_controller *switch2_get_controller (const char *phys) +{ +struct switch2_controller *ns2; + +guard (mutex)(&switch2_controllers_lock); +list_for_each_entry (ns2, &switch2_controllers, entry) { +if (strncmp (ns2->phys, phys,sizeof (ns2->phys)) ==0 ) { +if (kref_get_unless_zero (&ns2->refcount)) +return ns2; + } + }
[Severity: High]
Could matching controllers solely by their physical USB path cause issues if an old device is unplugged but kept alive by an open evdev file descriptor?
If a new controller is plugged into the same USB port, it will have the same phys string and might erroneously attach to the old controller's state structure, polluting the open userspace handles and corrupting state.
[ ... ]
Quoted 2 lines
drivers/hid/hid-nintendo.c +2
-0+static int switch2_probe (struct hid_device *hdev,const struct hid_device_id *id) +{
[ ... ]
Quoted 7 lines
drivers/hid/hid-nintendo.c +7
-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; +
[ ... ]
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 4040
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+static void switch2_remove (struct hid_device *hdev) +{ +struct switch2_controller *ns2 =hid_get_drvdata (hdev); + +switch2_controller_put (ns2); +mutex_lock (&ns2->lock); + ns2->hdev =NULL ; +ida_free (&nintendo_player_id_allocator, ns2->player_id);
[Severity: High]
If ida_alloc fails during switch2_probe, player_id remains U32_MAX.
Since switch2_remove unconditionally calls ida_free with this value, could this trigger the BUG_ON inside the IDA subsystem, since U32_MAX casts to a negative integer?
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 4031
Quoted 5 lines
drivers/hid/hid-nintendo.c +5
-0+mutex_unlock (&ns2->lock); +kref_put (&ns2->refcount, switch2_kref_put); +hid_hw_close (hdev); +hid_hw_stop (hdev); +}
[Severity: High]
Is it safe to drop the kref on ns2 before stopping the hardware?
If the last reference is dropped and ns2 is freed here, an asynchronous input report could still arrive before hid_hw_stop finishes. Would this cause a use-after-free in switch2_event when it retrieves ns2 via hid_get_drvdata?
Quoted 4 lines
drivers/input/joystick/nintendo-switch2-usb.c new file +0
-0@@ -0,0 +1,475 @@
[ ... ]
Quoted 2 lines
drivers/input/joystick/nintendo-switch2-usb.c new file +2
-0+static int switch2_usb_probe (struct usb_interface *intf,const struct usb_device_id *id) +{
[ ... ]
drivers/input/joystick/nintendo-switch2-usb.c line 354
Quoted 10 lines
drivers/input/joystick/nintendo-switch2-usb.c new file +10
-0+spin_lock_irqsave (&ns2_usb->bulk_in_lock, flags); + ns2_usb->bulk_in[0 ].state =NS2_URB_OUT ; +usb_anchor_urb (ns2_usb->bulk_in[0 ].urb, &ns2_usb->bulk_in_anchor); + ret =usb_submit_urb (ns2_usb->bulk_in[0 ].urb,GFP_ATOMIC ); +spin_unlock_irqrestore (&ns2_usb->bulk_in_lock, flags); + +if (ret <0 ) + goto err_free_out; + + ret =switch2_controller_attach_cfg (phys, &ns2_usb->cfg);
[Severity: High]
Is there a race condition here if the URB completes immediately?
The bulk IN URB is submitted before switch2_controller_attach_cfg finishes attaching the controller. If the attach sequence fails and frees ns2, could the scheduled message_in_work worker thread access a partially attached and subsequently freed ns2 structure?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jiri Kosina
maintainerRe: [PATCH v12 1/3] HID: nintendo: Add preliminary Switch 2 controller driver
In reply to Vicki PfauQuoted 19 lines · Vicki Pfau
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.
Signed-off-by Vicki Pfau <[email protected]>
Quoted 9 lines
--- MAINTAINERS | 1 + drivers/hid/Kconfig | 11 +- drivers/hid/hid-ids.h | 4 + drivers/hid/hid-nintendo.c | 1277 ++++++++++++++++- drivers/hid/hid-nintendo.h | 72 + drivers/input/joystick/Kconfig | 11 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/nintendo-switch2-usb.c | 475 ++++++
Dmitry, can you please Ack the drivers/input/ bits here?
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Vicki Pfau
maintainerPATCH v12 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
276 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v12 2/3] HID: nintendo: Add rumble support for Switch 2 controllers
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Use-After-Free during devm cleanup of delayed work because `ns2` is freed before `input->dev`.…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Use-After-Free during devm cleanup of delayed work because ns2 is freed before input->dev.
- [High] Use-After-Free in switch2_play_effect() when the FF-memless timer fires concurrently with or after device removal.
- [High] Use-After-Free in LED sysfs callbacks because devm_led_classdev_register is bound to the input->dev lifecycle.
- [Medium] Re-initialization of rumble_lock in switch2_probe() causes lockdep splats and metadata corruption upon driver rebind. --
--- Patch [2]: [PATCH v12 2/3] HID: nintendo: Add rumble support for Switch 2 controllers --- commit 2b2da5db7899af041358cec4f201f43322db88a4
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
[ ... ]
Quoted 16 lines
drivers/hid/hid-nintendo.c +12
-0@@ -3209,6 +3260,128 @@ static void switch2_kref_put(struct kref *refcount)kfree (ns2); } +#if IS_ENABLED (CONFIG_NINTENDO_FF ) +static void switch2_encode_rumble (struct switch2_hd_rumble *rumble,uint8_t buffer[5 ]) +{ + buffer[0 ] = rumble->hi_freq; + buffer[1 ] = (rumble->hi_freq >>8 ) | (rumble->hi_amp <<2 ); + buffer[2 ] = (rumble->hi_amp >>6 ) | (rumble->lo_freq <<4 ); + buffer[3 ] = (rumble->lo_freq >>4 ) | (rumble->lo_amp <<6 ); + buffer[4 ] = rumble->lo_amp >>2 ; +} + +static int switch2_play_effect (struct input_dev *dev,void *data,struct ff_effect *effect) +{
drivers/hid/hid-nintendo.c static void switch2_kref_put(struct kref *refcount) · line 3284
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+struct switch2_controller *ns2 =input_get_drvdata (dev); +unsigned long flags; + +if (effect->type !=FF_RUMBLE ) +return 0 ; + +if (!ns2) +return -ENODEV ; + +spin_lock_irqsave (&ns2->rumble_lock, flags);
[Severity: High]
Can switch2_play_effect() access ns2 after it has been freed?
The ff-memless timer is only destroyed when input->dev is freed. If an active rumble effect arms this timer and the device is unplugged while the timer is active, switch2_remove() will free ns2 if a file descriptor remains open.
The timer could then fire, calling switch2_play_effect(), which fetches the stale ns2 pointer via input_get_drvdata() and attempts to acquire ns2->rumble_lock.
[ ... ]
drivers/hid/hid-nintendo.c static int switch2_init_input(struct switch2_controller *ns2) · line 3514
Quoted 11 lines
drivers/hid/hid-nintendo.c +7
-0@@ -3332,6 +3505,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]
Can this lead to a use-after-free during device removal?
Since evdev keeps input->dev alive until the file descriptor is closed, tying the delayed work to input->dev means the cancellation action will be deferred.
However, switch2_remove() drops the final kref on ns2 and frees it immediately on unplug. When the file descriptor is eventually closed, input->dev is destroyed, and the devm action cancel_delayed_work_sync(&ns2->rumble_work) will execute on the already-freed ns2 struct.
[Severity: High]
Does a similar use-after-free exist in the LED registration?
In switch2_init_input(), the LED device is registered using:devm_led_classdev_register(&input->dev, led);
Because this is tied to the input->dev lifecycle rather than the HID device, if the device is unbound, switch2_remove() frees ns2. But if input->dev is kept alive by an open file descriptor, the LED sysfs attributes remain registered.
A subsequent read/write to the brightness attribute triggers switch2_player_led_brightness_set(), which fetches the freed ns2 and attempts to acquire ns2->lock.
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 int switch2_probe(struct hid_device *hdev, const struct hid_device_id *id · line 4207
Quoted 7 lines
drivers/hid/hid-nintendo.c +3
-0@@ -3996,6 +4202,10 @@ static int switch2_probe(struct hid_device *hdev, const struct hid_device_id *idelse ns2->player_id = ret; +#if IS_ENABLED (CONFIG_NINTENDO_FF ) +spin_lock_init (&ns2->rumble_lock); +#endif
[Severity: Medium]
Will this corrupt the lock state on driver rebind?
The ns2 structure is shared with a USB config interface (via the switch2_controllers list) and can outlive the HID device binding.
If the HID driver is unbound and then rebound, switch2_probe() retrieves the existing ns2 and calls spin_lock_init(&ns2->rumble_lock) on the already active lock, which can cause lockdep splats and metadata corruption.
Quoted 4 lines
drivers/hid/hid-nintendo.c +1
-0+ ret =hid_hw_start (hdev,HID_CONNECT_HIDRAW );if (ret) {hid_err (hdev,"hw_start failed %d\n" , ret);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Vicki Pfau
maintainerPATCH v12 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
@@ -3805,11 +3906,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
Heyhey!
Quoted 10 lines · Vicki Pfau
This series adds preliminary support for Switch 2 controllers using the same split-driver model as previous versions. This is a minor iteration on v11, fixing some nitpicks. It should be fully ready now, pending review from Dmitry and a HID tree maintainer.
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
Quoted 11 lines
MAINTAINERS | 1 + drivers/hid/Kconfig | 19 +- drivers/hid/hid-ids.h | 4 + drivers/hid/hid-nintendo.c | 1682 ++++++++++++++++- drivers/hid/hid-nintendo.h | 72 + drivers/input/joystick/Kconfig | 11 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/nintendo-switch2-usb.c | 475 +++++ 8 files changed, 2224 insertions(+), 41 deletions(-) create mode 100644 drivers/hid/hid-nintendo.h create mode 100644 drivers/input/joystick/nintendo-switch2-usb.c
I had another look at the changes compared to v11 and everything looks good to me!
The whole series is
I have also tested the Nintendo Switch 2 Pro Controller with evtest again so that part is
Cheers,
Silvan