HID: nintendo: Add preliminary Switch 2 controller driver
Subsystems (3)
Vicki Pfau
maintainerv8 cover HID: nintendo: Add preliminary Switch 2 controller driver
This series adds preliminary support for Switch 2 controllers using the same split-driver model as previous versions. This is a minor iteration on v7, fixing errors pointed out by sashiko as well as trying to clean up unplug somewhat further beyond what it pointed out.
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 | 1668 ++++++++++++++++- drivers/hid/hid-nintendo.h | 72 + drivers/input/joystick/Kconfig | 11 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/nintendo-switch2-usb.c | 453 +++++ 8 files changed, 2188 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 v8 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 | 1262 ++++++++++++++++- drivers/hid/hid-nintendo.h | 72 + drivers/input/joystick/Kconfig | 11 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/nintendo-switch2-usb.c | 453 ++++++ 8 files changed, 1805 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 +1256 -6
1,341 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 +453 -0
453 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 9 potential issue(s) to consider: - [Critical] Race condition in switch2_get_controller() allows calling kref_get() on an object with a…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [Critical] Race condition in switch2_get_controller() allows calling kref_get() on an object with a 0-refcount.
- [Critical] The controller structure (ns2) is freed before its devm-managed LEDs are unregistered, causing Use-After-Free.
- [High] Reversed teardown order in switch2_usb_disconnect() causes Use-After-Free of ns2_usb.
- [High] Explicit input_unregister_device() on a devm-managed input device causes resource leaks and LED subsystem list corruption across USB reconnects.
- [High] Double spinlock acquisition in the USB probe error path causes a hard deadlock.
- [High] Probe failure for a duplicate HID device wrongly unregisters the active input device of the existing controller.
- [High] Dangling cfg->parent pointer on switch2_controller_attach_cfg failure leads to Use-After-Free.
- [High] Dereference of ns2->cfg without a NULL check in switch2_receive_command causes a crash.
- [Low] Redundant manual assignment of parent device for devm-managed input device.
--
Note: The format of this review has been altered to a free-form summary due to recitation restrictions. The original diff is not quoted, but the locations and issues are described below.
commit d9d2794b04a1b3ab24fb33bed74fba65c1f6f459
Author: Vicki Pfau <[email protected]>
Subject: HID: nintendo: Add preliminary Switch 2 controller driver
This commit adds a new split driver for the Nintendo Switch 2 controllers, handling input and rumble over a main HID interface and configuration over a secondary interface.
[Severity: High]
In switch2_usb_disconnect() in drivers/input/joystick/nintendo-switch2-usb.c, is the teardown sequence reversed?
The code calls cancel_work_sync() for message_in_work before it calls usb_kill_anchored_urbs() for the bulk_in anchor. If an active IN URB completes after the work is canceled but before the URBs are killed, its completion handler could schedule the work again.
Will this lead to a use-after-free of the ns2_usb structure when devres eventually frees it?
[Severity: Critical]
Looking at switch2_get_controller() and switch2_kref_put() in drivers/hid/hid-nintendo.c, is there a race condition that allows acquiring a zero-refcount object?
switch2_kref_put() deletes the entry from the list, but there is a window where the refcount is zero before the lock is acquired and the item is removed.
Can a concurrent switch2_get_controller() find this dying object and blindly call kref_get() on it, resulting in list corruption and a use-after-free? Should this use kref_get_unless_zero() instead?
[Severity: High]
In switch2_controller_put(), the code explicitly calls input_unregister_device() on an input device that was allocated with devm_input_allocate_device().
Does this violate the devm lifecycle? The input subsystem defers freeing a devm-managed input device and its children until the parent unbinds.
On a USB reconnect, will this leak the old input device and cause a name collision or list corruption in the LED subsystem when the driver tries to register new LEDs with the same names?
[Severity: Low]
In switch2_init_input(), the code manually assigns the parent of the input device to the hdev device.
Since devm_input_allocate_device() automatically sets the parent device, isn't this manual assignment redundant?
[Severity: Critical]
In switch2_remove(), the code calls switch2_controller_put() and then drops the final refcount via kref_put(), which frees the ns2 structure.
However, the LED class devices are embedded inside the ns2 structure and are devm-managed.
Won't devres attempt to unregister these LEDs later during the driver core's devres_release_all() phase? Will this cause a use-after-free because the memory for the LEDs has already been freed?
[Severity: High]
In the err_free_in cleanup path of switch2_usb_probe(), if the urb pointer is NULL, the code calls spin_lock_irqsave() again instead of releasing the lock with spin_unlock_irqrestore().
Will this double spinlock acquisition cause a hard deadlock under memory pressure when URB allocation fails?
[Severity: High]
In switch2_probe(), if a second HID interface attempts to bind to an already claimed physical controller, it correctly detects this and returns an error.
However, the error path jumps to err_put, which calls switch2_controller_put().
Does this unintentionally unregister the active input device of the existing, working controller? Should it just drop the newly acquired refcount instead?
[Severity: High]
In switch2_controller_attach_cfg(), if switch2_init_controller() fails, the code clears ns2->cfg but does not clear cfg->parent.
Does this leave a dangling pointer? If an IN URB completes concurrently, could switch2_usb_message_in_work() observe a non-NULL cfg.parent and pass a freed ns2 pointer to switch2_receive_command(), causing a use-after-free?
[Severity: High]
In switch2_receive_command(), the code checks for a packet error and then unconditionally accesses ns2->cfg->dev to print a warning.
Because this work can run concurrently with switch2_controller_detach_cfg() (due to the teardown order issue mentioned above), is it possible for ns2->cfg to be NULL here?
Will this cause a NULL pointer dereference?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Vicki Pfau
maintainerPATCH v8 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 | 217 ++++++++++++++++++++++++++++++++++++- 2 files changed, 218 insertions(+), 7 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 +214 -3
291 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v8 2/3] HID: nintendo: Add rumble support for Switch 2 controllers
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
New issues:
- [Critical] Explicitly calling input_free_device() on a devm-managed input device in error paths leads to a double-free.
- [High] ABBA deadlock between ns2->lock and ns2->rumble_work during device removal or probe failure.
- [High] Teardown race allows hid_hw_output_report() to execute on a stopped HID device.
- [High] The rumble delayed work loop fails to bound catch-up scheduling, causing permanent zero-delay spam.
Pre-existing issues:
- [Critical] Double spinlock acquisition causing a deadlock in the error path of switch2_usb_probe. --
commit 8dfb25ac3f59c21072ff7262dbfd49faf2095c2d
Author: Vicki Pfau <[email protected]>
Subject: HID: nintendo: Add rumble support for Switch 2 controllers
This commit adds rumble support for Switch 2 controllers. It handles both HD Rumble LRAs for Joy-Cons and Pro Controllers, and eccentric rotating mass haptics for GameCube controllers. Since evdev lacks a full LRA API, it simulates basic rumble effects.
Quoted 4 lines
drivers/hid/hid-nintendo.c +0
-0
[ ... ]
Quoted 5 lines
drivers/hid/hid-nintendo.c +1
-0@@ -3205,6 +3259,125 @@ static void switch2_kref_put(struct kref *refcount)kfree (ns2); } +#if IS_ENABLED (CONFIG_NINTENDO_FF )
[ ... ]
Quoted 3 lines
drivers/hid/hid-nintendo.c +3
-0+static int switch2_play_effect (struct input_dev *dev,void *data,struct ff_effect *effect) +{ +struct switch2_controller *ns2 =input_get_drvdata (dev);
[ ... ]
drivers/hid/hid-nintendo.c
Quoted 1 line
drivers/hid/hid-nintendo.c +1
-0+schedule_delayed_work (&ns2->rumble_work,0 );
[Severity: High]
Does this create a race condition with device teardown?
During hot-unplug, switch2_remove() calls cancel_delayed_work_sync() and hid_hw_stop(hdev). However, because the input device is not unregistered until switch2_controller_put() is called under ns2->lock, userspace could still send EV_FF events via ioctl.
If switch2_play_effect() is invoked during this window, it schedules ns2->rumble_work unconditionally.
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+ +return 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 3355
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]
Can this loop get stuck in a permanent zero-delay spam?
If the worker gets delayed by system load or if hid_hw_output_report() takes longer than 4ms, ns2->last_rumble_work falls behind current_jiffies. The code adds a fixed 4ms to last_rumble_work and calls schedule_delayed_work() with a 0 delay to catch up.
If the worker continuously takes more than 4ms to run, current_jiffies advances alongside last_rumble_work. Without a cap or reset mechanism, could this flood the hardware and monopolize a workqueue thread indefinitely?
drivers/hid/hid-nintendo.c static void switch2_kref_put(struct kref *refcount) · line 3360
Quoted 5 lines
drivers/hid/hid-nintendo.c +5
-0+ }else { + ns2->last_rumble_work =0 ; + } + +mutex_lock (&ns2->lock);
[Severity: High]
Could this lead to an ABBA deadlock?
In switch2_remove(), we take ns2->lock and then call switch2_controller_put(), which calls input_unregister_device(). This will synchronously invoke the driver close callback, switch2_input_deref().
If switch2_input_deref() calls cancel_delayed_work_sync(), it will wait for this worker to finish while holding ns2->lock. If switch2_rumble_work() is currently executing, it will attempt to acquire ns2->lock here, resulting in a deadlock.
drivers/hid/hid-nintendo.c static void switch2_kref_put(struct kref *refcount) · line 3364
Quoted 4 lines
drivers/hid/hid-nintendo.c +4
-0+if (!ns2->hdev) { +cancel_delayed_work (&ns2->rumble_work); + }else { + ret =hid_hw_output_report (ns2->hdev, ns2->rumble_buffer,64 );
[Severity: High]
Can this execute hid_hw_output_report() on a stopped device?
If switch2_play_effect() schedules rumble_work after switch2_remove() called hid_hw_stop(), this worker will run, acquire ns2->lock, and pass the !ns2->hdev check because ns2->hdev has not yet been set to NULL.
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+/* +* Don't log on ENODEV, ESHUTDOWN, or EPROTO, which can happen +* mid-hotplug. Also cancel any further work on ENODEV or +* ESHUTDOWN as they're clear indications that the endpoint +* is dead. +*/
[ ... ]
drivers/hid/hid-nintendo.c static void switch2_input_deref(struct input_dev *input) · line 3452
Quoted 6 lines
drivers/hid/hid-nintendo.c +2
-0@@ -3275,6 +3448,9 @@ static void switch2_input_deref(struct input_dev *input) {struct switch2_controller *ns2 =input_get_drvdata (input); +#if IS_ENABLED (CONFIG_NINTENDO_FF ) +cancel_delayed_work_sync (&ns2->rumble_work);
[Severity: High]
Can this cause a deadlock during device removal?
switch2_remove() calls mutex_lock(&ns2->lock) followed by switch2_controller_put(ns2). This eventually triggers the input core to synchronously call this callback (switch2_input_deref).
Calling cancel_delayed_work_sync() while the removal thread holds ns2->lock will deadlock if switch2_rumble_work() is simultaneously trying to acquire ns2->lock.
Quoted 3 lines
drivers/hid/hid-nintendo.c +1
-0+#endif kref_put (&ns2->refcount, switch2_kref_put); }
[ ... ]
Quoted 4 lines
drivers/hid/hid-nintendo.c +0
-0@@ -3346,6 +3522,20 @@ static int switch2_init_input(struct switch2_controller *ns2)return -EINVAL ; }
drivers/hid/hid-nintendo.c static int switch2_init_input(struct switch2_controller *ns2) · line 3534
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-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 ; + } +input_set_capability (input,EV_FF ,FF_RUMBLE ); + ret =input_ff_create_memless (input,NULL , switch2_play_effect); +if (ret) { +input_free_device (input);
[Severity: Critical]
Will calling input_free_device() here lead to a double-free?
The input device was allocated earlier using devm_input_allocate_device(). By explicitly calling input_free_device() in this error path, the reference count drops and the memory is freed.
When the probe entirely fails and the devres cleanup triggers (devm_input_device_release()), it will attempt to call input_put_device() on the already freed memory.
Quoted 5 lines
drivers/hid/hid-nintendo.c +4
-0+return ret; + } +#endif + hid_info(ns2->hdev, "Firmware version %u.%u.%u (type %i)\n", ns2->version.major,
[ ... ]
drivers/hid/hid-nintendo.c static void switch2_remove(struct hid_device *hdev) · line 4233
Quoted 9 lines
drivers/hid/hid-nintendo.c +3
-0@@ -4018,6 +4226,9 @@ static void switch2_remove(struct hid_device *hdev) {struct switch2_controller *ns2 =hid_get_drvdata (hdev); +#if IS_ENABLED (CONFIG_NINTENDO_FF ) +cancel_delayed_work_sync (&ns2->rumble_work); +#endif hid_hw_close (hdev);hid_hw_stop (hdev);
[Severity: High]
Does this cancellation leave a window for the worker to be rescheduled?
The input device is not unregistered until switch2_controller_put() is called later in this function. Userspace can still send EV_FF events via ioctl, which could invoke switch2_play_effect() and unconditionally reschedule ns2->rumble_work after the device has been stopped.
drivers/hid/hid-nintendo.c static void switch2_remove(struct hid_device *hdev) · line 4234
Quoted 1 line
drivers/hid/hid-nintendo.c +0
-0mutex_lock (&ns2->lock);
Additionally, there is a finding in a separate file:
[Severity: Critical]
This isn't a bug introduced by this patch, but could the error handling in switch2_usb_probe() cause a deadlock?
During probe, if usb_alloc_urb() or usb_alloc_coherent() fails in the NS2_IN_URBS loop, the code jumps to the err_free_in label:
drivers/input/joystick/nintendo-switch2-usb.c:switch2_usb_probe() {
...
if (!urb) {
spin_lock_irqsave(&ns2_usb->bulk_in_lock, flags);
continue;
}
...
}It incorrectly calls spin_lock_irqsave(&ns2_usb->bulk_in_lock, flags) again instead of spin_unlock_irqrestore(), causing an immediate double-lock deadlock when trying to free the URBs.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Vicki Pfau
maintainerPATCH v8 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
@@ -2872,6 +2872,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
@@ -3120,6 +3150,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 , },
@@ -3137,6 +3183,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 , },
@@ -3158,6 +3221,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 , },
@@ -3175,6 +3259,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
@@ -3787,11 +3888,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