HID: nintendo: Add preliminary Switch 2 controller driver
Subsystems (2)
Vicki Pfau
maintainerv16 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.
This should fix the last thing sashiko has been reporting that isn't complete and utter nonsense. Unless anything seriously bad comes through the next round, or a maintainer tells me I need to fix it, I'm just done.
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 | 493 +++++++ drivers/hid/hid-nintendo.c | 1704 +++++++++++++++++++++++- drivers/hid/hid-nintendo.h | 73 + 7 files changed, 2263 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
Vicki Pfau
maintainerPATCH v16 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 | 493 +++++++++ drivers/hid/hid-nintendo.c | 1280 +++++++++++++++++++++++- drivers/hid/hid-nintendo.h | 73 ++ 7 files changed, 1864 insertions(+), 10 deletions(-) create mode 100644 drivers/hid/hid-nintendo-switch2-usb.c create mode 100644 drivers/hid/hid-nintendo.h
MAINTAINERS +1 -0
@@ -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
@@ -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 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
@@ -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_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
@@ -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 +493 -0
493 lines not on this page · raw message
drivers/hid/hid-nintendo.c +1274 -6
1,359 lines not on this page · raw message
drivers/hid/hid-nintendo.h new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] hid: cyclic dependency in devm_led_classdev_register causes memory leak and UAF - [High] usb…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] hid: cyclic dependency in devm_led_classdev_register causes memory leak and UAF
- [High] usb: teardown ordering allows concurrent schedule_work to cause Use-After-Free
- [High] hid: AB-BA deadlock between ns2->lock and cancel_work_sync during detach --
commit dd9a3840e4c8766d649b2e556df7f9b3f872ec43
Author: Vicki Pfau <[email protected]>
HID: nintendo: Add preliminary Switch 2 controller driver
This commit adds a new preliminary driver for Nintendo Switch 2 controllers. It introduces a split-driver approach handling standard HID events alongside an unusual configuration interface over USB. Support includes general inputs for various Nintendo controllers but defers IMU, rumble, and battery setups.
Quoted 5 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +0
-0
[ ... ]
Quoted 2 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +2
-0+static void switch2_bulk_in (struct urb *urb) +{
[ ... ]
Quoted 5 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +5
-0+spin_unlock_irqrestore (&ns2_usb->bulk_in_lock, flags); + +if (schedule) +schedule_work (&ns2_usb->message_in_work); +}
[ ... ]
Quoted 2 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +2
-0+static void switch2_usb_free_in_urbs (struct switch2_usb *ns2_usb) +{
[ ... ]
drivers/hid/hid-nintendo-switch2-usb.c line 222
Quoted 7 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +7
-0+spin_lock_irqsave (&ns2_usb->bulk_in_lock, flags); + ns2_usb->shutdown = true; +spin_unlock_irqrestore (&ns2_usb->bulk_in_lock, flags); + +cancel_work_sync (&ns2_usb->message_in_work); + +usb_kill_anchored_urbs (&ns2_usb->bulk_in_anchor);
[Severity: High]
Does the current teardown ordering in switch2_usb_free_in_urbs() allow a concurrent URB completion to queue work after cancellation?
In switch2_bulk_in(), if a completion handler reads ns2_usb->shutdown as false but gets preempted before calling schedule_work(), the teardown path could proceed to call cancel_work_sync(). Since the work isn't queued yet, cancel_work_sync() returns immediately.
Then usb_kill_anchored_urbs() waits for the URB completion, which finally resumes and queues the work using schedule_work().
When this work executes later, could it access freed ns2_usb memory and cause a use-after-free?
[ ... ]
Quoted 2 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +2
-0+static void switch2_usb_shutdown (struct switch2_cfg_intf *intf) +{
[ ... ]
Quoted 13 lines
drivers/hid/hid-nintendo-switch2-usb.c new file +8
-0+spin_lock_irqsave (&ns2_usb->bulk_in_lock, flags); + ns2_usb->shutdown = true; +spin_unlock_irqrestore (&ns2_usb->bulk_in_lock, flags); + +cancel_work_sync (&ns2_usb->message_in_work); + +usb_kill_anchored_urbs (&ns2_usb->bulk_in_anchor); +spin_lock_irqsave (&ns2_usb->bulk_in_lock, flags);drivers/hid/hid-nintendo.c +0
-0
[ ... ]
Quoted 3 lines
drivers/hid/hid-nintendo.c +3
-0+int switch2_receive_command (struct switch2_controller *ns2, +const uint8_t *message,size_t length) +{
[ ... ]
Quoted 3 lines
drivers/hid/hid-nintendo.c +3
-0+print_hex_dump_debug ("got cmd: " ,DUMP_PREFIX_OFFSET ,16 ,1 , message, length, false); + +mutex_lock (&ns2->lock);
[ ... ]
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3977
Quoted 9 lines
drivers/hid/hid-nintendo.c +9
-0+void switch2_controller_detach_cfg (struct switch2_controller *ns2) +{ +mutex_lock (&ns2->lock); +if (!ns2->cfg ||WARN_ON (ns2 != ns2->cfg->parent)) { +mutex_unlock (&ns2->lock); +return ; + } +if (ns2->cfg->shutdown) + ns2->cfg->shutdown (ns2->cfg);
[Severity: High]
Could this locking sequence lead to an AB-BA deadlock between the detach path and the worker thread?
When switch2_controller_detach_cfg() or the error path in switch2_controller_attach_cfg() executes, they first acquire ns2->lock. They then call the shutdown callback, which resolves to switch2_usb_shutdown(). This callback calls cancel_work_sync() to wait for the message_in_work thread to finish.
However, the asynchronous switch2_usb_message_in_work() thread calls switch2_receive_command(), which attempts to acquire ns2->lock. If the detach path holds the lock while waiting for the work to complete, and the work waits for the lock, will they block indefinitely?
[ ... ]
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);
[ ... ]
Quoted 2 lines
drivers/hid/hid-nintendo.c +2
-0+static int switch2_init_input (struct switch2_controller *ns2) +{
[ ... ]
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+ ns2->leds =devm_kcalloc (&input->dev,JC_NUM_LEDS ,sizeof (*ns2->leds),GFP_KERNEL ); +if (!ns2->leds) { +input_unregister_device (input); +return -ENOMEM ; + } + +for (i =0 ; i <JC_NUM_LEDS ; i++) { +struct led_classdev *led = &ns2->leds[i];
[ ... ]
drivers/hid/hid-nintendo.c static int nintendo_hid_suspend(struct hid_device *hdev, pm_message_t message) · line 3418
Quoted 2 lines
drivers/hid/hid-nintendo.c +2
-0+ led->name = name; + ret =devm_led_classdev_register (&input->dev, led);
[Severity: High]
Will using the unmanaged input->dev with devm_led_classdev_register() create a cyclic dependency that leads to a memory leak and use-after-free?
The input->dev here is allocated via input_allocate_device(), meaning its devres cleanup only triggers when its reference count drops to zero. But led_classdev_register() creates a child device that naturally holds a kobject reference to its parent (the input device).
Because the child keeps the parent's reference count elevated, the parent's devres cleanup never fires. When switch2_remove() runs, input_unregister_device() disconnects handlers, but the input device and LED sysfs nodes remain active indefinitely.
If userspace later interacts with the leaked LED sysfs attributes, it will trigger switch2_player_led_brightness_set(), which fetches driver data and dereferences the ns2 object. Since ns2 is freed during switch2_remove() via kref_put(), could this result in a use-after-free?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Vicki Pfau
maintainerPATCH v16 2/4 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 | 213 ++++++++++++++++++++++++++++++++++++- 2 files changed, 215 insertions(+), 6 deletions(-)
drivers/hid/Kconfig +4 -4
@@ -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 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
[email protected]
AI reviewRe: [PATCH v16 2/4] HID: nintendo: Add rumble support for Switch 2 controllers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] hid: nintendo: Use-after-free of ns2 in rumble_work due to missing sync cancel - [High] hid…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] hid: nintendo: Use-after-free of ns2 in rumble_work due to missing sync cancel
- [High] hid: nintendo: Workqueue list corruption due to rumble_work re-initialization --
commit a197b51ef6afbdd0338765f77b38c7488b6d41fe
Author: Vicki Pfau <[email protected]>
HID: nintendo: Add rumble support for Switch 2 controllers
This commit adds basic rumble support for Switch 2 controllers, simulating standard force feedback using the HD Rumble actuators. It introduces a delayed work item to manage rumble state updates.
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 3559
Quoted 11 lines
drivers/hid/hid-nintendo.c +7
-0@@ -3374,6 +3550,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 of the ns2 structure?
The devm_delayed_work_autocancel() helper binds the work cancellation to the input device's devres group. If a userspace process holds the input device open when the physical device is unplugged, the devres execution is delayed until the file descriptor is closed. However, the driver's remove function frees the ns2 controller structure synchronously via kref_put() without waiting for the devres cleanup. If the delayed work is pending, it will execute and access the freed ns2 structure in switch2_rumble_work().
[Severity: High]
Does this code corrupt the workqueue list if initialization is retried?
In switch2_init_input(), the input device is registered and exposed to userspace. If a subsequent initialization step fails, such as the LED registration, the error path unregisters the device. If userspace opens the device and triggers a rumble effect before unregistration completes, ns2->rumble_work is queued.
The state machine then retries switch2_init_controller(), which calls switch2_init_input() again. This allocates a new input device and calls devm_delayed_work_autocancel() on the exact same &ns2->rumble_work. This unconditionally calls INIT_DELAYED_WORK() on an already-queued work item, which overwrites its list pointers and corrupts the workqueue list.
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
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Vicki Pfau
maintainerPATCH v16 3/4 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
@@ -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
@@ -3849,11 +3950,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
Vicki Pfau
maintainerPATCH v16 4/4 HID: nintendo: Patch NYXI Hyperion 3 descriptor
In reply to Vicki PfauThe 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
@@ -4503,6 +4503,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) {
@@ -4531,6 +4546,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
Quoted 22 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.
This should fix the last thing sashiko has been reporting that isn't complete and utter nonsense. Unless anything seriously bad comes through the next round, or a maintainer tells me I need to fix it, I'm just done.
Quoted 5 lines
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
Applied to hid.git#for-7.4/nintendo, thanks.
Signature · 2 lines
Jiri Kosina SUSE Labs