Skip to contentSkip to conversation

HID: nintendo

PATCH Superseded Daniel J. Ogorchock 7 participants 27 messages Mar 17, 2020 – Aug 4, 2020

Subsystems (2)

v11 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Daniel J. Ogorchock

author maintainer
#0 lore raw

v11 cover HID: nintendo

I removed the IMU patch for now, since I have some more work on it to do before it's ready. This version fixes a bug with joy-con S-trigger configuration and switches the pro controller's Dpad input to a hat.

Version 11 changes:
- Removed IMU patch for now, since it has some issues to work out.
- Fixed bug introduced in v10 which led to the joy-cons' S-triggers not being configured as an input.
- Changed the pro controller's d-pad input from buttons to a hat to be more in line with other controller drivers.

Version 10 changes:
- Removed duplicate reporting of one of the triggers that Billy noticed
- The joy-cons now only report having the buttons they actually have (they used to register the input devices with the buttons of the other joy-con as well).
- The input device is now created after the LEDs/power supply.
- The removed state handling bool has been removed, instead opting to add a new controller state (removed).
- Eliminated a 1 second delay when probing a USB controller.
- Added support for the IMU. This mostly consisted of merging in some work provided by Carl. I'm not incredibly familiar with proper gyro/accelerometer handling in linux, so this might need some tweaking. Preliminary tests in evtest show the gyro/accel values being reported.
- Added support for the joy-con USB charging grip.

Version 9 changes:
- Fixed compiler errors on gcc versions older than 8.2
- Set input device's uniq value to the controller's MAC address

Version 8 changes:
- Corrected the handshaking protocol with USB pro controllers. A handshake now occurs both prior and after the baudrate set. This doesn't appear to have a noticeable difference, but it more accurately follows documentation found online.
- Fixed potential race condition which could lead to a slightly longer delay sending subcommands in rare circumstances.
- Moved the rumble worker to its own workqueue, since it can block. This prevents it from having a negative impact on the default kernel workqueue. It also prevents dropped subcommands due to something else blocking the kernel workqueue. The benefit is most obvious when using multiple controllers at once, since the controller subcommand timings are very picky.
- Added a patch to set the most significant bit of the hid hw version. Roderick had mentioned needing to probably do this awhile ago, but I had forgotten about it until now. This is the same thing hid-sony does. It allows SDL2 to have different mappings for the hid-nintendo driver vs the default hid mappings.

Version 7 changes:
- Changed name to hid-nintendo to fit modern naming conventions
- Removed joycon_ctlr_destroy(), since it wasn't needed an could potentially invalidate a mutex while it could be in use on other threads
- Implemented minor code improvements suggested by Silvan
- The driver now waits to send subcommands until after receiving an input report. This significantly reduces dropped subcommands.
- Reduced the number of error messages when disconnecting a controller.

Version 6 changes:
- Improved subcommand sending reliabilty
- Decreased rumble period to 50ms
- Added rumble queue to avoid missing ff_effects if sent too quickly
- Code cleanup and minor refactoring
- Added default analog stick calibration

Version 5 changes:
- Removed sysfs interface to control motor frequencies.
- Improved rumble reliability by using subcommands to set it.
- Changed mapping of the SL/SR triggers on the joy-cons to map to whichever triggers they lack (e.g. a left joycon's sl/sr map to TR and TR2). This allows userspace to distinguish between the normal and S triggers.
- Minor refactors

Version 4 changes:
- Added support for the Home button LED for the pro controller and right joy-con
- Changed name from hid-switchcon to hid-joycon
- Added rumble support
- Removed ctlr->type and use hdev->product instead
- Use POWER_SUPPLY_CAPACITY_LEVEL enum instead of manually translating to capacity percentages
- Misc. minor refactors based on v3 feedback

Version 3 changes:
- Added led_classdev support for the 4 player LEDs
- Added power_supply support for the controller's battery
- Made the controller number mutex static
- Minor refactoring/style fixes based on Roderick's feedback from v2

Version 2 changes:
- Switched to using a synchronous method for configuring the controller.
- Removed any pairing/orientation logic in the driver. Every controller now corresponds to its own input device.
- Store controller button data as a single u32.
- Style corrections

Daniel J. Ogorchock (11):
HID: nintendo: add nintendo switch controller driver
HID: nintendo: add player led support
HID: nintendo: add power supply support
HID: nintendo: add home led support
HID: nintendo: add rumble support
HID: nintendo: improve subcommand reliability
HID: nintendo: send subcommands after receiving input report
HID: nintendo: reduce device removal subcommand errors
HID: nintendo: patch hw version for userspace HID mappings
HID: nintendo: set controller uniq to MAC
HID: nintendo: add support for charging grip

 MAINTAINERS                |    6 +
 drivers/hid/Kconfig        |   24 +
 drivers/hid/Makefile       |    1 +
 drivers/hid/hid-ids.h      |    4 +
 drivers/hid/hid-nintendo.c | 1665 ++++++++++++++++++++++++++++++++++++
 5 files changed, 1700 insertions(+)
 create mode 100644 drivers/hid/hid-nintendo.c
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH v11 1/11 HID: nintendo: add nintendo switch controller driver

The hid-nintendo driver supports the Nintendo Switch Pro Controllers and the Joy-Cons. The Pro Controllers can be used over USB or Bluetooth.

The Joy-Cons each create their own, independent input devices, so it is up to userspace to combine them if desired.

---
 MAINTAINERS                |   6 +
 drivers/hid/Kconfig        |  11 +
 drivers/hid/Makefile       |   1 +
 drivers/hid/hid-ids.h      |   3 +
 drivers/hid/hid-nintendo.c | 871 +++++++++++++++++++++++++++++++++++++
 5 files changed, 892 insertions(+)
 create mode 100644 drivers/hid/hid-nintendo.c
MAINTAINERS +6 -0
index 44bc9d7f04a4..b06feb461d87 100644
@@ -11683,6 +11683,12 @@ S:	Maintained
 F:	Documentation/scsi/NinjaSCSI.txt
 F:	drivers/scsi/nsp32*
 
+NINTENDO HID DRIVER
+M:	Daniel J. Ogorchock <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/hid/hid-nintendo*
+
 NIOS2 ARCHITECTURE
 M:	Ley Foon Tan <[email protected]>
 L:	[email protected] (moderated for non-subscribers)
drivers/hid/Kconfig +11 -0
index 494a39e74939..5dbee5fd69df 100644
@@ -702,6 +702,17 @@ config HID_MULTITOUCH
 	  To compile this driver as a module, choose M here: the
 	  module will be called hid-multitouch.
 
+config HID_NINTENDO
+	tristate "Nintendo Joy-Con and Pro Controller support"
+	depends on HID
+	help
+	Adds support for the Nintendo Switch Joy-Cons and Pro Controller.
+	All controllers support bluetooth, and the Pro Controller also supports
+	its USB mode.
+
+	To compile this driver as a module, choose M here: the
+	module will be called hid-nintendo.
+
 config HID_NTI
 	tristate "NTI keyboard adapters"
 	---help---
drivers/hid/Makefile +1 -0
index bfefa365b1ce..f66702d6fa03 100644
@@ -74,6 +74,7 @@ obj-$(CONFIG_HID_MAYFLASH)	+= hid-mf.o
 obj-$(CONFIG_HID_MICROSOFT)	+= hid-microsoft.o
 obj-$(CONFIG_HID_MONTEREY)	+= hid-monterey.o
 obj-$(CONFIG_HID_MULTITOUCH)	+= hid-multitouch.o
+obj-$(CONFIG_HID_NINTENDO)	+= hid-nintendo.o
 obj-$(CONFIG_HID_NTI)			+= hid-nti.o
 obj-$(CONFIG_HID_NTRIG)		+= hid-ntrig.o
 obj-$(CONFIG_HID_ORTEK)		+= hid-ortek.o
drivers/hid/hid-ids.h +3 -0
index 3a400ce603c4..452b4228a196 100644
@@ -875,6 +875,9 @@
 #define USB_VENDOR_ID_NINTENDO		0x057e
 #define USB_DEVICE_ID_NINTENDO_WIIMOTE	0x0306
 #define USB_DEVICE_ID_NINTENDO_WIIMOTE2	0x0330
+#define USB_DEVICE_ID_NINTENDO_JOYCONL	0x2006
+#define USB_DEVICE_ID_NINTENDO_JOYCONR	0x2007
+#define USB_DEVICE_ID_NINTENDO_PROCON	0x2009
 
 #define USB_VENDOR_ID_NOVATEK		0x0603
 #define USB_DEVICE_ID_NOVATEK_PCT	0x0600
drivers/hid/hid-nintendo.c new file +871 -0

871 lines not on this page · raw message

Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH v11 2/11 HID: nintendo: add player led support

In reply to Daniel J. Ogorchock

This patch adds led_classdev functionality to the switch controller driver. It adds support for the 4 player LEDs. The Home Button LED still needs to be supported on the pro controllers and right joy-con.

---
 drivers/hid/Kconfig        |  2 +
 drivers/hid/hid-nintendo.c | 95 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 95 insertions(+), 2 deletions(-)
drivers/hid/Kconfig +2 -0
index 5dbee5fd69df..275f61427eea 100644
@@ -705,6 +705,8 @@ config HID_MULTITOUCH
 config HID_NINTENDO
 	tristate "Nintendo Joy-Con and Pro Controller support"
 	depends on HID
+	depends on NEW_LEDS
+	depends on LEDS_CLASS
 	help
 	Adds support for the Nintendo Switch Joy-Cons and Pro Controller.
 	All controllers support bluetooth, and the Pro Controller also supports
drivers/hid/hid-nintendo.c +93 -2

139 lines not on this page · raw message

Signature · 1 line
2.25.1
author maintainer
#3 lore raw

PATCH v11 3/11 HID: nintendo: add power supply support

In reply to Daniel J. Ogorchock

This patch adds power_supply functionality to the switch controller driver for its battery.

---
 drivers/hid/Kconfig        |   1 +
 drivers/hid/hid-nintendo.c | 133 +++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+)
drivers/hid/Kconfig +1 -0
index 275f61427eea..bc401da4ea76 100644
@@ -707,6 +707,7 @@ config HID_NINTENDO
 	depends on HID
 	depends on NEW_LEDS
 	depends on LEDS_CLASS
+	select POWER_SUPPLY
 	help
 	Adds support for the Nintendo Switch Joy-Cons and Pro Controller.
 	All controllers support bluetooth, and the Pro Controller also supports
drivers/hid/hid-nintendo.c +133 -0

184 lines not on this page · raw message

Signature · 1 line
2.25.1
author maintainer
#4 lore raw

PATCH v11 4/11 HID: nintendo: add home led support

In reply to Daniel J. Ogorchock

This patch adds the ability to set the intensity level of the home button's LED.

---
 drivers/hid/hid-nintendo.c | 69 +++++++++++++++++++++++++++++++++++---
 1 file changed, 65 insertions(+), 4 deletions(-)
drivers/hid/hid-nintendo.c +65 -4
index bbce15056c2d..18482f2fe0bf 100644
@@ -192,7 +192,8 @@ struct joycon_input_report {
 struct joycon_ctlr {
 	struct hid_device *hdev;
 	struct input_dev *input;
-	struct led_classdev leds[JC_NUM_LEDS];
+	struct led_classdev leds[JC_NUM_LEDS]; /* player leds */
+	struct led_classdev home_led;
 	enum joycon_ctlr_state ctlr_state;
 	spinlock_t lock;
 
@@ -726,6 +727,40 @@ static int joycon_player_led_brightness_set(struct led_classdev *led,
 	return ret;
 }
 
+static int joycon_home_led_brightness_set(struct led_classdev *led,
+					  enum led_brightness brightness)
+{
+	struct device *dev = led->dev->parent;
+	struct hid_device *hdev = to_hid_device(dev);
+	struct joycon_ctlr *ctlr;
+	struct joycon_subcmd_request *req;
+	u8 buffer[sizeof(*req) + 5] = { 0 };
+	u8 *data;
+	int ret;
+
+	ctlr = hid_get_drvdata(hdev);
+	if (!ctlr) {
+		hid_err(hdev, "No controller data\n");
+		return -ENODEV;
+	}
+
+	req = (struct joycon_subcmd_request *)buffer;
+	req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT;
+	data = req->data;
+	data[0] = 0x01;
+	data[1] = brightness << 4;
+	data[2] = brightness | (brightness << 4);
+	data[3] = 0x11;
+	data[4] = 0x11;
+
+	hid_dbg(hdev, "setting home led brightness\n");
+	mutex_lock(&ctlr->output_mutex);
+	ret = joycon_send_subcmd(ctlr, req, 5);
+	mutex_unlock(&ctlr->output_mutex);
+
+	return ret;
+}
+
 static const char * const joycon_player_led_names[] = {
 	"player1",
 	"player2",
@@ -734,7 +769,7 @@ static const char * const joycon_player_led_names[] = {
 };
 
 static DEFINE_MUTEX(joycon_input_num_mutex);
-static int joycon_player_leds_create(struct joycon_ctlr *ctlr)
+static int joycon_leds_create(struct joycon_ctlr *ctlr)
 {
 	struct hid_device *hdev = ctlr->hdev;
 	struct device *dev = &hdev->dev;
@@ -771,7 +806,7 @@ static int joycon_player_leds_create(struct joycon_ctlr *ctlr)
 		ret = devm_led_classdev_register(&hdev->dev, led);
 		if (ret) {
 			hid_err(hdev, "Failed registering %s LED\n", led->name);
-			break;
+			return ret;
 		}
 	}
 
@@ -779,6 +814,32 @@ static int joycon_player_leds_create(struct joycon_ctlr *ctlr)
 		input_num = 1;
 	mutex_unlock(&joycon_input_num_mutex);
 
+	/* configure the home LED */
+	if (ctlr->hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONL) {
+		name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", d_name, "home");
+		if (!name)
+			return ret;
+
+		led = &ctlr->home_led;
+		led->name = name;
+		led->brightness = 0;
+		led->max_brightness = 0xF;
+		led->brightness_set_blocking = joycon_home_led_brightness_set;
+		led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE;
+		ret = devm_led_classdev_register(&hdev->dev, led);
+		if (ret) {
+			hid_err(hdev, "Failed registering home led\n");
+			return ret;
+		}
+		/* Set the home LED to 0 as default state */
+		ret = joycon_home_led_brightness_set(led, 0);
+		if (ret) {
+			hid_err(hdev, "Failed to set home LED dflt; ret=%d\n",
+									ret);
+			return ret;
+		}
+	}
+
 	return 0;
 }
 
@@ -1026,7 +1087,7 @@ static int nintendo_hid_probe(struct hid_device *hdev,
 	mutex_unlock(&ctlr->output_mutex);
 
 	/* Initialize the leds */
-	ret = joycon_player_leds_create(ctlr);
+	ret = joycon_leds_create(ctlr);
 	if (ret) {
 		hid_err(hdev, "Failed to create leds; ret=%d\n", ret);
 		goto err_close;
Signature · 1 line
2.25.1
author maintainer
#5 lore raw

PATCH v11 5/11 HID: nintendo: add rumble support

In reply to Daniel J. Ogorchock

This patch adds support for controller rumble.

The ff_effect weak magnitude is associated with the pro controller's right motor (or with a right joy-con). The strong magnitude is associated with the pro's left motor (or a left joy-con).

The rumble data is sent periodically (currently configured for every 50 milliseconds). If the controller receives no rumble data for too long a time period, it will stop vibrating. The data is also sent every time joycon_set_rumble is called to avoid latency of up to 50ms.

Because the rumble subcommands are sent in a deferred workqueue (they can't be sent in the play_effect function due to the hid send sleeping), the effects are queued. This ensures that no rumble effect is missed due to them arriving in too quick of succession.

---
 drivers/hid/Kconfig        |  10 ++
 drivers/hid/hid-nintendo.c | 349 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 356 insertions(+), 3 deletions(-)
drivers/hid/Kconfig +10 -0
index bc401da4ea76..8f0203861fa5 100644
@@ -716,6 +716,16 @@ config HID_NINTENDO
 	To compile this driver as a module, choose M here: the
 	module will be called hid-nintendo.
 
+config NINTENDO_FF
+	bool "Nintendo Switch controller force feedback support"
+	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 and the pro
+	controller. For the pro controller, both rumble motors can be controlled
+	individually.
+
 config HID_NTI
 	tristate "NTI keyboard adapters"
 	---help---
drivers/hid/hid-nintendo.c +346 -3

450 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v11 05/11] HID: nintendo: add rumble support

Quoted 3 lines · Daniel J. Ogorchock

This patch adds support for controller rumble.
<snip>
+config NINTENDO_FF

This should probably be CONFIG_HID_NINTENDO_FF, right?

author maintainer
#7 lore raw

PATCH v11 6/11 HID: nintendo: improve subcommand reliability

In reply to Daniel J. Ogorchock

The controller occasionally doesn't respond to subcommands. It appears that it's dropping them. To improve reliability, this patch attempts one retry in the case of a synchronous send timeout. In testing, this has resolved all timeout failures (most common for LED setting and rumble setting subcommands).

The 1 second timeout is excessively long for rumble and LED subcommands, so the timeout has been made a param for joycon_hid_send_sync. Most subcommands continue to use the 1s timeout, since they can result in long response times. Rumble and LED setting subcommands have been reduced to 250ms, since response times for them are much quicker (and this significantly reduces the observable impact in the case of a retry being required).

---
 drivers/hid/hid-nintendo.c | 66 ++++++++++++++++++++++++--------------
 1 file changed, 42 insertions(+), 24 deletions(-)
drivers/hid/hid-nintendo.c +42 -24
index 50242a0b6296..2421ce10f234 100644
@@ -370,27 +370,45 @@ static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len)
 	return ret;
 }
 
-static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len)
+static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len,
+				u32 timeout)
 {
 	int ret;
+	int tries = 2;
 
-	ret = __joycon_hid_send(ctlr->hdev, data, len);
-	if (ret < 0) {
-		memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
-		return ret;
-	}
+	/*
+	 * The controller occasionally seems to drop subcommands. In testing,
+	 * doing one retry after a timeout appears to always work.
+	 */
+	while (tries--) {
+		ret = __joycon_hid_send(ctlr->hdev, data, len);
+		if (ret < 0) {
+			memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
+			return ret;
+		}
 
-	if (!wait_event_timeout(ctlr->wait, ctlr->received_resp, HZ)) {
-		hid_dbg(ctlr->hdev, "synchronous send/receive timed out\n");
-		memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
-		return -ETIMEDOUT;
+		ret = wait_event_timeout(ctlr->wait, ctlr->received_resp,
+					 timeout);
+		if (!ret) {
+			hid_dbg(ctlr->hdev,
+				"synchronous send/receive timed out\n");
+			if (tries) {
+				hid_dbg(ctlr->hdev,
+					"retrying sync send after timeout\n");
+			}
+			memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
+			ret = -ETIMEDOUT;
+		} else {
+			ret = 0;
+			break;
+		}
 	}
 
 	ctlr->received_resp = false;
-	return 0;
+	return ret;
 }
 
-static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd)
+static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd, u32 timeout)
 {
 	int ret;
 	u8 buf[2] = {JC_OUTPUT_USB_CMD};
@@ -398,7 +416,7 @@ static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd)
 	buf[1] = cmd;
 	ctlr->usb_ack_match = cmd;
 	ctlr->msg_type = JOYCON_MSG_TYPE_USB;
-	ret = joycon_hid_send_sync(ctlr, buf, sizeof(buf));
+	ret = joycon_hid_send_sync(ctlr, buf, sizeof(buf), timeout);
 	if (ret)
 		hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret);
 	return ret;
@@ -406,7 +424,7 @@ static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd)
 
 static int joycon_send_subcmd(struct joycon_ctlr *ctlr,
 			      struct joycon_subcmd_request *subcmd,
-			      size_t data_len)
+			      size_t data_len, u32 timeout)
 {
 	int ret;
 	unsigned long flags;
@@ -424,7 +442,7 @@ static int joycon_send_subcmd(struct joycon_ctlr *ctlr,
 	ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD;
 
 	ret = joycon_hid_send_sync(ctlr, (u8 *)subcmd,
-				   sizeof(*subcmd) + data_len);
+				   sizeof(*subcmd) + data_len, timeout);
 	if (ret < 0)
 		hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret);
 	else
@@ -443,7 +461,7 @@ static int joycon_set_player_leds(struct joycon_ctlr *ctlr, u8 flash, u8 on)
 	req->data[0] = (flash << 4) | on;
 
 	hid_dbg(ctlr->hdev, "setting player leds\n");
-	return joycon_send_subcmd(ctlr, req, 1);
+	return joycon_send_subcmd(ctlr, req, 1, HZ/4);
 }
 
 static const u16 DFLT_STICK_CAL_CEN = 2000;
@@ -474,7 +492,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
 	data[4] = JC_CAL_DATA_SIZE;
 
 	hid_dbg(ctlr->hdev, "requesting cal data\n");
-	ret = joycon_send_subcmd(ctlr, req, 5);
+	ret = joycon_send_subcmd(ctlr, req, 5, HZ);
 	if (ret) {
 		hid_warn(ctlr->hdev,
 			 "Failed to read stick cal, using defaults; ret=%d\n",
@@ -564,7 +582,7 @@ static int joycon_set_report_mode(struct joycon_ctlr *ctlr)
 	req->data[0] = 0x30; /* standard, full report mode */
 
 	hid_dbg(ctlr->hdev, "setting controller report mode\n");
-	return joycon_send_subcmd(ctlr, req, 1);
+	return joycon_send_subcmd(ctlr, req, 1, HZ);
 }
 
 static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)
@@ -577,7 +595,7 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)
 	req->data[0] = enable ? 0x01 : 0x00;
 
 	hid_dbg(ctlr->hdev, "%s rumble\n", enable ? "enabling" : "disabling");
-	return joycon_send_subcmd(ctlr, req, 1);
+	return joycon_send_subcmd(ctlr, req, 1, HZ/4);
 }
 
 static s32 joycon_map_stick_val(struct joycon_stick_cal *cal, s32 val)
@@ -1081,7 +1099,7 @@ static int joycon_home_led_brightness_set(struct led_classdev *led,
 
 	hid_dbg(hdev, "setting home led brightness\n");
 	mutex_lock(&ctlr->output_mutex);
-	ret = joycon_send_subcmd(ctlr, req, 5);
+	ret = joycon_send_subcmd(ctlr, req, 5, HZ/4);
 	mutex_unlock(&ctlr->output_mutex);
 
 	return ret;
@@ -1377,16 +1395,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,
 	mutex_lock(&ctlr->output_mutex);
 	/* if handshake command fails, assume ble pro controller */
 	if (hdev->product == USB_DEVICE_ID_NINTENDO_PROCON &&
-	    !joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE)) {
+	    !joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE, HZ)) {
 		hid_dbg(hdev, "detected USB controller\n");
 		/* set baudrate for improved latency */
-		ret = joycon_send_usb(ctlr, JC_USB_CMD_BAUDRATE_3M);
+		ret = joycon_send_usb(ctlr, JC_USB_CMD_BAUDRATE_3M, HZ);
 		if (ret) {
 			hid_err(hdev, "Failed to set baudrate; ret=%d\n", ret);
 			goto err_mutex;
 		}
 		/* handshake */
-		ret = joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE);
+		ret = joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE, HZ);
 		if (ret) {
 			hid_err(hdev, "Failed handshake; ret=%d\n", ret);
 			goto err_mutex;
@@ -1395,7 +1413,7 @@ static int nintendo_hid_probe(struct hid_device *hdev,
 		 * Set no timeout (to keep controller in USB mode).
 		 * This doesn't send a response, so ignore the timeout.
 		 */
-		joycon_send_usb(ctlr, JC_USB_CMD_NO_TIMEOUT);
+		joycon_send_usb(ctlr, JC_USB_CMD_NO_TIMEOUT, HZ/10);
 	}
 
 	/* get controller calibration data, and parse it */
Signature · 1 line
2.25.1
author maintainer
#8 lore raw

PATCH v11 7/11 HID: nintendo: send subcommands after receiving input report

In reply to Daniel J. Ogorchock

Waiting to send subcommands until right after receiving an input report drastically improves subcommand reliability. If the driver has finished initial controller configuration, it now waits until receiving an input report for all subcommands.

---
 drivers/hid/hid-nintendo.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
drivers/hid/hid-nintendo.c +33 -0
index 2421ce10f234..8e26672661dc 100644
@@ -328,6 +328,7 @@ struct joycon_ctlr {
 	bool received_resp;
 	u8 usb_ack_match;
 	u8 subcmd_ack_match;
+	bool received_input_report;
 
 	/* factory calibration data */
 	struct joycon_stick_cal left_stick_cal_x;
@@ -381,6 +382,26 @@ static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len,
 	 * doing one retry after a timeout appears to always work.
 	 */
 	while (tries--) {
+		/*
+		 * If we are in the proper reporting mode, wait for an input
+		 * report prior to sending the subcommand. This improves
+		 * reliability considerably.
+		 */
+		if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) {
+			unsigned long flags;
+
+			spin_lock_irqsave(&ctlr->lock, flags);
+			ctlr->received_input_report = false;
+			spin_unlock_irqrestore(&ctlr->lock, flags);
+			ret = wait_event_timeout(ctlr->wait,
+						 ctlr->received_input_report,
+						 HZ / 4);
+			/* We will still proceed, even with a timeout here */
+			if (!ret)
+				hid_warn(ctlr->hdev,
+					 "timeout waiting for input report\n");
+		}
+
 		ret = __joycon_hid_send(ctlr->hdev, data, len);
 		if (ret < 0) {
 			memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE);
@@ -753,6 +774,18 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,
 	}
 
 	input_sync(dev);
+
+	/*
+	 * Immediately after receiving a report is the most reliable time to
+	 * send a subcommand to the controller. Wake any subcommand senders
+	 * waiting for a report.
+	 */
+	if (unlikely(mutex_is_locked(&ctlr->output_mutex))) {
+		spin_lock_irqsave(&ctlr->lock, flags);
+		ctlr->received_input_report = true;
+		spin_unlock_irqrestore(&ctlr->lock, flags);
+		wake_up(&ctlr->wait);
+	}
 }
 
 static void joycon_rumble_worker(struct work_struct *work)
Signature · 1 line
2.25.1
author maintainer
#9 lore raw

PATCH v11 8/11 HID: nintendo: reduce device removal subcommand errors

In reply to Daniel J. Ogorchock

This patch fixes meaningless error output from trying to send subcommands immediately after controller removal. It now disables subcommands as soon as possible on removal.

---
 drivers/hid/hid-nintendo.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
drivers/hid/hid-nintendo.c +22 -2
index 8e26672661dc..abac0497b1cb 100644
@@ -230,6 +230,7 @@ static const struct joycon_rumble_amp_data joycon_rumble_amplitudes[] = {
 enum joycon_ctlr_state {
 	JOYCON_CTLR_STATE_INIT,
 	JOYCON_CTLR_STATE_READ,
+	JOYCON_CTLR_STATE_REMOVED,
 };
 
 struct joycon_stick_cal {
@@ -451,6 +452,14 @@ static int joycon_send_subcmd(struct joycon_ctlr *ctlr,
 	unsigned long flags;
 
 	spin_lock_irqsave(&ctlr->lock, flags);
+	/*
+	 * If the controller has been removed, just return ENODEV so the LED
+	 * subsystem doesn't print invalid errors on removal.
+	 */
+	if (ctlr->ctlr_state == JOYCON_CTLR_STATE_REMOVED) {
+		spin_unlock_irqrestore(&ctlr->lock, flags);
+		return -ENODEV;
+	}
 	memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail],
 	       JC_RUMBLE_DATA_SIZE);
 	spin_unlock_irqrestore(&ctlr->lock, flags);
@@ -800,10 +809,13 @@ static void joycon_rumble_worker(struct work_struct *work)
 		mutex_lock(&ctlr->output_mutex);
 		ret = joycon_enable_rumble(ctlr, true);
 		mutex_unlock(&ctlr->output_mutex);
-		if (ret < 0)
-			hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret);
 
+		/* -ENODEV means the controller was just unplugged */
 		spin_lock_irqsave(&ctlr->lock, flags);
+		if (ret < 0 && ret != -ENODEV &&
+		    ctlr->ctlr_state != JOYCON_CTLR_STATE_REMOVED)
+			hid_warn(ctlr->hdev, "Failed to set rumble; e=%d", ret);
+
 		ctlr->rumble_msecs = jiffies_to_msecs(jiffies);
 		if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) {
 			if (++ctlr->rumble_queue_tail >= JC_RUMBLE_QUEUE_SIZE)
@@ -1516,9 +1528,17 @@ static int nintendo_hid_probe(struct hid_device *hdev,
 static void nintendo_hid_remove(struct hid_device *hdev)
 {
 	struct joycon_ctlr *ctlr = hid_get_drvdata(hdev);
+	unsigned long flags;
 
 	hid_dbg(hdev, "remove\n");
+
+	/* Prevent further attempts at sending subcommands. */
+	spin_lock_irqsave(&ctlr->lock, flags);
+	ctlr->ctlr_state = JOYCON_CTLR_STATE_REMOVED;
+	spin_unlock_irqrestore(&ctlr->lock, flags);
+
 	destroy_workqueue(ctlr->rumble_queue);
+
 	hid_hw_close(hdev);
 	hid_hw_stop(hdev);
 }
Signature · 1 line
2.25.1
author maintainer
#10 lore raw

PATCH v11 9/11 HID: nintendo: patch hw version for userspace HID mappings

In reply to Daniel J. Ogorchock

This patch sets the most significant bit of the hid hw version to allow userspace to distinguish between this driver's input mappings vs. the default hid mappings. This prevents breaking userspace applications that use SDL2 for gamepad input, allowing them to distinguish the mappings based on the version.

---
 drivers/hid/hid-nintendo.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/hid/hid-nintendo.c +9 -0
index abac0497b1cb..a56fa5ffee1a 100644
@@ -1422,6 +1422,15 @@ static int nintendo_hid_probe(struct hid_device *hdev,
 		goto err_wq;
 	}
 
+	/*
+	 * Patch the hw version of pro controller/joycons, so applications can
+	 * distinguish between the default HID mappings and the mappings defined
+	 * by the Linux game controller spec. This is important for the SDL2
+	 * library, which has a game controller database, which uses device ids
+	 * in combination with version as a key.
+	 */
+	hdev->version |= 0x8000;
+
 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
 	if (ret) {
 		hid_err(hdev, "HW start failed\n");
Signature · 1 line
2.25.1
author maintainer
#11 lore raw

PATCH v11 10/11 HID: nintendo: set controller uniq to MAC

In reply to Daniel J. Ogorchock

This patch sets the input device's uniq identifier to the controller's MAC address. This is useful for future association between an IMU input device with the normal input device as well as associating the controller with any serial joy-con driver.

---
 drivers/hid/hid-nintendo.c | 45 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
drivers/hid/hid-nintendo.c +45 -0
index a56fa5ffee1a..dbd5946f71e1 100644
@@ -319,6 +319,8 @@ struct joycon_ctlr {
 	struct led_classdev home_led;
 	enum joycon_ctlr_state ctlr_state;
 	spinlock_t lock;
+	u8 mac_addr[6];
+	char *mac_addr_str;
 
 	/* The following members are used for synchronous sends/receives */
 	enum joycon_msg_type msg_type;
@@ -1005,6 +1007,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)
 	ctlr->input->id.vendor = hdev->vendor;
 	ctlr->input->id.product = hdev->product;
 	ctlr->input->id.version = hdev->version;
+	ctlr->input->uniq = ctlr->mac_addr_str;
 	ctlr->input->name = name;
 	input_set_drvdata(ctlr->input, ctlr);
 
@@ -1316,6 +1319,41 @@ static int joycon_power_supply_create(struct joycon_ctlr *ctlr)
 	return 0;
 }
 
+static int joycon_read_mac(struct joycon_ctlr *ctlr)
+{
+	int ret;
+	int i;
+	int j;
+	struct joycon_subcmd_request req = { 0 };
+	struct joycon_input_report *report;
+
+	req.subcmd_id = JC_SUBCMD_REQ_DEV_INFO;
+	ret = joycon_send_subcmd(ctlr, &req, 0, HZ);
+	if (ret) {
+		hid_err(ctlr->hdev, "Failed to get joycon info; ret=%d\n", ret);
+		return ret;
+	}
+
+	report = (struct joycon_input_report *)ctlr->input_buf;
+
+	for (i = 4, j = 0; j < 6; i++, j++)
+		ctlr->mac_addr[j] = report->reply.data[i];
+
+	ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL,
+					    "%02X:%02X:%02X:%02X:%02X:%02X",
+					    ctlr->mac_addr[0],
+					    ctlr->mac_addr[1],
+					    ctlr->mac_addr[2],
+					    ctlr->mac_addr[3],
+					    ctlr->mac_addr[4],
+					    ctlr->mac_addr[5]);
+	if (!ctlr->mac_addr_str)
+		return -ENOMEM;
+	hid_info(ctlr->hdev, "controller MAC = %s\n", ctlr->mac_addr_str);
+
+	return 0;
+}
+
 /* Common handler for parsing inputs */
 static int joycon_ctlr_read_handler(struct joycon_ctlr *ctlr, u8 *data,
 							      int size)
@@ -1494,6 +1532,13 @@ static int nintendo_hid_probe(struct hid_device *hdev,
 		goto err_mutex;
 	}
 
+	ret = joycon_read_mac(ctlr);
+	if (ret) {
+		hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n",
+			ret);
+		goto err_close;
+	}
+
 	mutex_unlock(&ctlr->output_mutex);
 
 	/* Initialize the leds */
Signature · 1 line
2.25.1
author maintainer
#12 lore raw

PATCH v11 11/11 HID: nintendo: add support for charging grip

In reply to Daniel J. Ogorchock

This patch adds support for the joy-con charging grip. The peripheral essentially behaves the same as a pro controller, but with two joy-cons attached to the grip. However the grip exposes the two joy-cons as separate hid devices, so extra handling is required. The joy-con is queried to check if it is a right or left joy-con (since the product ID is identical between left/right when using the grip).

Since controller model detection is now more complicated, the various checks for hid product values have been replaced with helper macros to reduce code duplication.

---
 drivers/hid/hid-ids.h      |  1 +
 drivers/hid/hid-nintendo.c | 67 ++++++++++++++++++++++++++++++--------
 2 files changed, 55 insertions(+), 13 deletions(-)
drivers/hid/hid-ids.h +1 -0
index 452b4228a196..e50f3f32d94e 100644
@@ -878,6 +878,7 @@
 #define USB_DEVICE_ID_NINTENDO_JOYCONL	0x2006
 #define USB_DEVICE_ID_NINTENDO_JOYCONR	0x2007
 #define USB_DEVICE_ID_NINTENDO_PROCON	0x2009
+#define USB_DEVICE_ID_NINTENDO_CHRGGRIP	0x200E
 
 #define USB_VENDOR_ID_NOVATEK		0x0603
 #define USB_DEVICE_ID_NOVATEK_PCT	0x0600
drivers/hid/hid-nintendo.c +54 -13

177 lines not on this page · raw message

Signature · 1 line
2.25.1
#13 lore raw
In reply to Daniel J. Ogorchock

Hi Daniel,

Quoted 4 lines · Daniel J. Ogorchock

I removed the IMU patch for now, since I have some more work on it to do before it's ready. This version fixes a bug with joy-con S-trigger configuration and switches the pro controller's Dpad input to a hat.

Thanks a lot for the continuous effort. I just had a quick look through the series, and nothing came up problematic. I have requested a few tests from people I know, and I'll be happy to merge this for v5.8. Unless Jiri says that 5.7 is OK, but I doubt Linus will be happy getting a new driver now that hasn't spent a little time in linux-next.

Cheers,
Benjamin

Quoted 122 lines

Version 11 changes:
- Removed IMU patch for now, since it has some issues to work out.
- Fixed bug introduced in v10 which led to the joy-cons' S-triggers not being configured as an input.
- Changed the pro controller's d-pad input from buttons to a hat to be more in line with other controller drivers.

Version 10 changes:
- Removed duplicate reporting of one of the triggers that Billy noticed
- The joy-cons now only report having the buttons they actually have (they used to register the input devices with the buttons of the other joy-con as well).
- The input device is now created after the LEDs/power supply.
- The removed state handling bool has been removed, instead opting to add a new controller state (removed).
- Eliminated a 1 second delay when probing a USB controller.
- Added support for the IMU. This mostly consisted of merging in some work provided by Carl. I'm not incredibly familiar with proper gyro/accelerometer handling in linux, so this might need some tweaking. Preliminary tests in evtest show the gyro/accel values being reported.
- Added support for the joy-con USB charging grip.

Version 9 changes:
- Fixed compiler errors on gcc versions older than 8.2
- Set input device's uniq value to the controller's MAC address

Version 8 changes:
- Corrected the handshaking protocol with USB pro controllers. A handshake now occurs both prior and after the baudrate set. This doesn't appear to have a noticeable difference, but it more accurately follows documentation found online.
- Fixed potential race condition which could lead to a slightly longer delay sending subcommands in rare circumstances.
- Moved the rumble worker to its own workqueue, since it can block. This prevents it from having a negative impact on the default kernel workqueue. It also prevents dropped subcommands due to something else blocking the kernel workqueue. The benefit is most obvious when using multiple controllers at once, since the controller subcommand timings are very picky.
- Added a patch to set the most significant bit of the hid hw version. Roderick had mentioned needing to probably do this awhile ago, but I had forgotten about it until now. This is the same thing hid-sony does. It allows SDL2 to have different mappings for the hid-nintendo driver vs the default hid mappings.

Version 7 changes:
- Changed name to hid-nintendo to fit modern naming conventions
- Removed joycon_ctlr_destroy(), since it wasn't needed an could potentially invalidate a mutex while it could be in use on other threads
- Implemented minor code improvements suggested by Silvan
- The driver now waits to send subcommands until after receiving an input report. This significantly reduces dropped subcommands.
- Reduced the number of error messages when disconnecting a controller.

Version 6 changes:
- Improved subcommand sending reliabilty
- Decreased rumble period to 50ms
- Added rumble queue to avoid missing ff_effects if sent too quickly
- Code cleanup and minor refactoring
- Added default analog stick calibration

Version 5 changes:
- Removed sysfs interface to control motor frequencies.
- Improved rumble reliability by using subcommands to set it.
- Changed mapping of the SL/SR triggers on the joy-cons to map to whichever triggers they lack (e.g. a left joycon's sl/sr map to TR and TR2). This allows userspace to distinguish between the normal and S triggers.
- Minor refactors

Version 4 changes:
- Added support for the Home button LED for the pro controller and right joy-con
- Changed name from hid-switchcon to hid-joycon
- Added rumble support
- Removed ctlr->type and use hdev->product instead
- Use POWER_SUPPLY_CAPACITY_LEVEL enum instead of manually translating to capacity percentages
- Misc. minor refactors based on v3 feedback

Version 3 changes:
- Added led_classdev support for the 4 player LEDs
- Added power_supply support for the controller's battery
- Made the controller number mutex static
- Minor refactoring/style fixes based on Roderick's feedback from v2

Version 2 changes:
- Switched to using a synchronous method for configuring the controller.
- Removed any pairing/orientation logic in the driver. Every controller now corresponds to its own input device.
- Store controller button data as a single u32.
- Style corrections

Daniel J. Ogorchock (11):
HID: nintendo: add nintendo switch controller driver
HID: nintendo: add player led support
HID: nintendo: add power supply support
HID: nintendo: add home led support
HID: nintendo: add rumble support
HID: nintendo: improve subcommand reliability
HID: nintendo: send subcommands after receiving input report
HID: nintendo: reduce device removal subcommand errors
HID: nintendo: patch hw version for userspace HID mappings
HID: nintendo: set controller uniq to MAC
HID: nintendo: add support for charging grip

 MAINTAINERS                |    6 +
 drivers/hid/Kconfig        |   24 +
 drivers/hid/Makefile       |    1 +
 drivers/hid/hid-ids.h      |    4 +
 drivers/hid/hid-nintendo.c | 1665 ++++++++++++++++++++++++++++++++++++
 5 files changed, 1700 insertions(+)
 create mode 100644 drivers/hid/hid-nintendo.c

--
2.25.1

maintainer
#14 lore raw
Quoted 9 lines · Benjamin Tissoires
Quoted 3 lines

I removed the IMU patch for now, since I have some more work on it to do before it's ready. This version fixes a bug with joy-con S-trigger configuration and switches the pro controller's Dpad input to a hat.

Thanks a lot for the continuous effort. I just had a quick look through the series, and nothing came up problematic. I have requested a few tests from people I know, and I'll be happy to merge this for v5.8. Unless Jiri says that 5.7 is OK, but I doubt Linus will be happy getting a new driver now that hasn't spent a little time in linux-next.

Yeah, let's queue this (once you have positive testing results from your testers) for 5.8.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#15 lore raw
In reply to Daniel J. Ogorchock

Hey Daniel,

Quoted 4 lines · Daniel J. Ogorchock

I removed the IMU patch for now, since I have some more work on it to do
before it's ready. This version fixes a bug with joy-con S-trigger configuration and switches the pro controller's Dpad input to a hat.

I don't have a Nintendo Switch Pro controller, but I recently got an 8Bitdo "GBros" adapter along with a third-party GameCube adapter for less than 20€, and tried out the "Switch" mode against your driver.

When plugging it in via USB:

[  593.228423] input: Nintendo Co., Ltd. Pro Controller as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:057E:2009.0007/input/input34
[  593.229859] hid-generic 0003:057E:2009.0007: input,hidraw4: USB HID v1.11 Joystick [Nintendo Co., Ltd. Pro Controller] on usb-0000:00:14.0-1/input0
[  593.317011] nintendo 0003:057E:2009.0007: hidraw4: USB HID v81.11 Joystick [Nintendo Co., Ltd. Pro Controller] on usb-0000:00:14.0-1/input0
[  595.476014] nintendo 0003:057E:2009.0007: Failed to set baudrate; ret=-110
[  595.485961] nintendo 0003:057E:2009.0007: probe - fail = -110
[  595.486019] nintendo: probe of 0003:057E:2009.0007 failed with error -110

After pairing it with Bluetooth:

[ 3974.268521] nintendo 0005:057E:2009.0009: unknown main item tag 0x0
[ 3974.271295] nintendo 0005:057E:2009.0009: hidraw4: BLUETOOTH HID v80.01 Gamepad [Pro Controller] on 44:85:00:1e:ab:f8
[ 3976.494269] nintendo 0005:057E:2009.0009: controller MAC = 98:B6:E9:42:A8:DD
[ 3977.036391] nintendo 0005:057E:2009.0009: Failed to set home LED dflt; ret=-110
[ 3977.036402] nintendo 0005:057E:2009.0009: Failed to create leds; ret=-110
[ 3977.049328] nintendo 0005:057E:2009.0009: probe - fail = -110
[ 3977.564465] leds 0005:057E:2009.0009:home: Setting an LED's brightness failed (-110)
[ 3978.076440] leds 0005:057E:2009.0009:player4: Setting an LED's brightness failed (-110)
[ 3978.589367] leds 0005:057E:2009.0009:player3: Setting an LED's brightness failed (-110)
[ 3979.101347] leds 0005:057E:2009.0009:player2: Setting an LED's brightness failed (-110)
[ 3979.612308] leds 0005:057E:2009.0009:player1: Setting an LED's brightness failed (-110)
[ 3979.612977] nintendo: probe of 0005:057E:2009.0009 failed with error -110

I'm guessing the LED and baudrate setting failures should probably be non-fatal. Let me know if there's anything else you'd like me to test.

For Fedora users who want to try out this v11 version of the driver:
https://koji.fedoraproject.org/koji/taskinfo?taskID=43730136
(note that the build will be removed in a bit less than a week)

Cheers

#16 lore raw
Quoted 35 lines · Bastien Nocera

Hey Daniel,

Quoted 4 lines · Daniel J. Ogorchock

I removed the IMU patch for now, since I have some more work on it to do
before it's ready. This version fixes a bug with joy-con S-trigger configuration and switches the pro controller's Dpad input to a hat.

I don't have a Nintendo Switch Pro controller, but I recently got an 8Bitdo "GBros" adapter along with a third-party GameCube adapter for less than 20€, and tried out the "Switch" mode against your driver.

When plugging it in via USB:

[  593.228423] input: Nintendo Co., Ltd. Pro Controller as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:057E:2009.0007/input/input34
[  593.229859] hid-generic 0003:057E:2009.0007: input,hidraw4: USB HID v1.11 Joystick [Nintendo Co., Ltd. Pro Controller] on usb-0000:00:14.0-1/input0
[  593.317011] nintendo 0003:057E:2009.0007: hidraw4: USB HID v81.11 Joystick [Nintendo Co., Ltd. Pro Controller] on usb-0000:00:14.0-1/input0
[  595.476014] nintendo 0003:057E:2009.0007: Failed to set baudrate; ret=-110
[  595.485961] nintendo 0003:057E:2009.0007: probe - fail = -110
[  595.486019] nintendo: probe of 0003:057E:2009.0007 failed with error -110

After pairing it with Bluetooth:

[ 3974.268521] nintendo 0005:057E:2009.0009: unknown main item tag 0x0
[ 3974.271295] nintendo 0005:057E:2009.0009: hidraw4: BLUETOOTH HID v80.01 Gamepad [Pro Controller] on 44:85:00:1e:ab:f8
[ 3976.494269] nintendo 0005:057E:2009.0009: controller MAC = 98:B6:E9:42:A8:DD
[ 3977.036391] nintendo 0005:057E:2009.0009: Failed to set home LED dflt; ret=-110
[ 3977.036402] nintendo 0005:057E:2009.0009: Failed to create leds; ret=-110
[ 3977.049328] nintendo 0005:057E:2009.0009: probe - fail = -110
[ 3977.564465] leds 0005:057E:2009.0009:home: Setting an LED's brightness failed (-110)
[ 3978.076440] leds 0005:057E:2009.0009:player4: Setting an LED's brightness failed (-110)
[ 3978.589367] leds 0005:057E:2009.0009:player3: Setting an LED's brightness failed (-110)
[ 3979.101347] leds 0005:057E:2009.0009:player2: Setting an LED's brightness failed (-110)
[ 3979.612308] leds 0005:057E:2009.0009:player1: Setting an LED's brightness failed (-110)
[ 3979.612977] nintendo: probe of 0005:057E:2009.0009 failed with error -110
Quoted 8 lines

I'm guessing the LED and baudrate setting failures should probably be non-fatal. Let me know if there's anything else you'd like me to test.

For Fedora users who want to try out this v11 version of the driver:
https://koji.fedoraproject.org/koji/taskinfo?taskID=43730136
(note that the build will be removed in a bit less than a week)

Cheers

I really wonder how a device like this should be handled. It looks like the device can also handle a bunch of other classic Nintendo controllers.

Is there anyway of detection this adapter? It feels nasty to have to hack in quirks for this device...

Thanks,
Roderick

#17 lore raw
Quoted 1 line · Roderick Colenbrander

<snip>

Quoted 6 lines

I really wonder how a device like this should be handled. It looks like the device can also handle a bunch of other classic Nintendo controllers.

Is there anyway of detection this adapter? It feels nasty to have to hack in quirks for this device...

The end game isn't very different from how we handle XBox 360, or PS3/PS4 "clone" devices.

Those devices (the adapters) work on the actual Nintendo Switch hardware, so should probably work with the driver that handles the same type of hardware on Linux.

#18 lore raw
Quoted 11 lines · Bastien Nocera
Quoted 1 line · Roderick Colenbrander

<snip>

Quoted 6 lines

I really wonder how a device like this should be handled. It looks like the device can also handle a bunch of other classic Nintendo controllers.

Is there anyway of detection this adapter? It feels nasty to have to hack in quirks for this device...

Quoted 7 lines

The end game isn't very different from how we handle XBox 360, or PS3/PS4 "clone" devices.

Those devices (the adapters) work on the actual Nintendo Switch hardware, so should probably work with the driver that handles the same type of hardware on Linux.

(resend in plain text)

I agree it probably makes sense to handle in this driver. I'm worried about the application level implications. We have been doing a lot of work e.g. on Android to try to make gamepads consistent. It is weird to have a "Switch controller" with different features as applications make assumptions and don't expect there to be multiple versions of a particular controller. Any button mapping files would potentially be wrong for those too, certain features are not there (e.g. no sensors or no lights or rumble) or if they are the behaviour is different (e.g. HD rumble vs a classic rumble motor).

Ideally we would do some kind of "fixup" to change the device name and or replace the device ids to at least separate them.

Roderick

#19 lore raw
Quoted 34 lines · Roderick Colenbrander
Quoted 11 lines · Bastien Nocera

On Sun, 2020-04-26 at 13:42 -0700, Roderick Colenbrander wrote:
<snip>

Quoted 8 lines

I really wonder how a device like this should be handled. It looks
like the device can also handle a bunch of other classic Nintendo controllers.

Is there anyway of detection this adapter? It feels nasty to have to
hack in quirks for this device...

Quoted 8 lines

The end game isn't very different from how we handle XBox 360, or PS3/PS4 "clone" devices.

Those devices (the adapters) work on the actual Nintendo Switch hardware, so should probably work with the driver that handles the same
type of hardware on Linux.

(resend in plain text)

I agree it probably makes sense to handle in this driver. I'm worried about the application level implications. We have been doing a lot of work e.g. on Android to try to make gamepads consistent. It is weird to have a "Switch controller" with different features as applications make assumptions and don't expect there to be multiple versions of a particular controller. Any button mapping files would potentially be wrong for those too, certain features are not there (e.g. no sensors or no lights or rumble) or if they are the behaviour is different (e.g. HD rumble vs a classic rumble motor).

Quoted 3 lines

Ideally we would do some kind of "fixup" to change the device name and
or replace the device ids to at least separate them.

All those would be detectable at runtime. I'm not sure that it's ever a good idea to presume that a particular VID/PID combination will have, say, rumble and LEDs available when the driver can answer those questions.

For example, I'm not sure that those controllers have either features (though I'm not certain they identify as Switch Pro controllers, but for the sake of argument):
https://store.nintendo.com/super-nintendo-entertainment-system-controller.html
https://store.nintendo.com/nintendo-entertainment-system-controllers.html

Cheers

author maintainer
#20 lore raw

Hi Bastien,

Apologies for the late reply. This thread sneaked past me somehow. If we want to handle clone controllers with partial protocol implementations, is it preferable to present them identically to userspace, with non-existent functionality being no-ops? Or would it be better to just not create the interfaces for missing functionality (e.g. not create the led_classdevs for controllers without LEDs)? I assume the latter makes more sense, since it doesn't lie to userspace. Though it could potentially make the driver code messier.

Thanks,
Daniel

Quoted 53 lines · Bastien Nocera
Quoted 34 lines · Roderick Colenbrander
Quoted 11 lines · Bastien Nocera

On Sun, 2020-04-26 at 13:42 -0700, Roderick Colenbrander wrote:
<snip>

Quoted 8 lines

I really wonder how a device like this should be handled. It looks
like the device can also handle a bunch of other classic Nintendo controllers.

Is there anyway of detection this adapter? It feels nasty to have to
hack in quirks for this device...

Quoted 8 lines

The end game isn't very different from how we handle XBox 360, or PS3/PS4 "clone" devices.

Those devices (the adapters) work on the actual Nintendo Switch hardware, so should probably work with the driver that handles the same
type of hardware on Linux.

(resend in plain text)

I agree it probably makes sense to handle in this driver. I'm worried about the application level implications. We have been doing a lot of work e.g. on Android to try to make gamepads consistent. It is weird to have a "Switch controller" with different features as applications make assumptions and don't expect there to be multiple versions of a particular controller. Any button mapping files would potentially be wrong for those too, certain features are not there (e.g. no sensors or no lights or rumble) or if they are the behaviour is different (e.g. HD rumble vs a classic rumble motor).

Quoted 3 lines

Ideally we would do some kind of "fixup" to change the device name and
or replace the device ids to at least separate them.

All those would be detectable at runtime. I'm not sure that it's ever a good idea to presume that a particular VID/PID combination will have, say, rumble and LEDs available when the driver can answer those questions.

For example, I'm not sure that those controllers have either features (though I'm not certain they identify as Switch Pro controllers, but for the sake of argument):
https://store.nintendo.com/super-nintendo-entertainment-system-controller.html
https://store.nintendo.com/nintendo-entertainment-system-controllers.html

Cheers

Signature · 1 line
Daniel Ogorchock
#21 lore raw

Hi Daniel,

Sorry for hijacking this branch of the thread (it's the last one that survived my inbox) - it seems like merging this driver as-is would break Steam, according to user reports.

Is there any mechanism built into this hid_nintendo patch series to duck out of the way if userland directly opens the underlying hidraw device? That's what hid_steam does to coexist peacefully with userspace drivers (Steam being one of them, but not the only one).

Thanks,
- Pierre-Loup

Quoted 70 lines · Daniel Ogorchock

Hi Bastien,

Apologies for the late reply. This thread sneaked past me somehow. If we want to handle clone controllers with partial protocol implementations, is it preferable to present them identically to userspace, with non-existent functionality being no-ops? Or would it be better to just not create the interfaces for missing functionality (e.g. not create the led_classdevs for controllers without LEDs)? I assume the latter makes more sense, since it doesn't lie to userspace. Though it could potentially make the driver code messier.

Thanks,
Daniel

Quoted 53 lines · Bastien Nocera
Quoted 34 lines · Roderick Colenbrander
Quoted 11 lines · Bastien Nocera

On Sun, 2020-04-26 at 13:42 -0700, Roderick Colenbrander wrote:
<snip>

Quoted 8 lines

I really wonder how a device like this should be handled. It looks
like the device can also handle a bunch of other classic Nintendo controllers.

Is there anyway of detection this adapter? It feels nasty to have to
hack in quirks for this device...

Quoted 8 lines

The end game isn't very different from how we handle XBox 360, or PS3/PS4 "clone" devices.

Those devices (the adapters) work on the actual Nintendo Switch hardware, so should probably work with the driver that handles the same
type of hardware on Linux.

(resend in plain text)

I agree it probably makes sense to handle in this driver. I'm worried about the application level implications. We have been doing a lot of work e.g. on Android to try to make gamepads consistent. It is weird to have a "Switch controller" with different features as applications make assumptions and don't expect there to be multiple versions of a particular controller. Any button mapping files would potentially be wrong for those too, certain features are not there (e.g. no sensors or no lights or rumble) or if they are the behaviour is different (e.g. HD rumble vs a classic rumble motor).

Quoted 3 lines

Ideally we would do some kind of "fixup" to change the device name and
or replace the device ids to at least separate them.

All those would be detectable at runtime. I'm not sure that it's ever a good idea to presume that a particular VID/PID combination will have, say, rumble and LEDs available when the driver can answer those questions.

For example, I'm not sure that those controllers have either features (though I'm not certain they identify as Switch Pro controllers, but for the sake of argument):
https://store.nintendo.com/super-nintendo-entertainment-system-controller.html
https://store.nintendo.com/nintendo-entertainment-system-controllers.html

Cheers

author maintainer
#22 lore raw

Hi Pierre-Loup,

Quoted 7 lines · Pierre-Loup A. Griffais

Hi Daniel,

Sorry for hijacking this branch of the thread (it's the last one that survived my inbox) - it seems like merging this driver as-is would break Steam, according to user reports.

Quoted 5 lines

Is there any mechanism built into this hid_nintendo patch series to duck out of the way if userland directly opens the underlying hidraw device? That's what hid_steam does to coexist peacefully with userspace drivers (Steam being one of them, but not the only one).>
Thanks,

I have run into the same issue of Steam/kernel fighting over the device. I opened an issue describing it here:
https://github.com/ValveSoftware/steam-for-linux/issues/6651.

I'd been telling people to use firejail as a temporary workaround to prevent steam from seeing the hidraw device. Note that hid-nintendo sets the most significant bit of the evdev's version number to allow userspace applications to discern it from the default hid device. There's no current mechanism in the driver to yield to userspace using hidraw, but I can look at what hid-steam is currently doing to accomplish that.

I guess the downside to that method is that any other process listening to the controller's evdev events would cease to receive them (maybe a voice chat program using one of the buttons as a push-to-talk hotkey or something similar).

Does steam use hidraw for the sony dualshock controllers as well? If so, is hid-sony doing anything special for that usecase?

As an additional note to anyone following this thread: I have a newer patchset I need to submit which has a couple fixes for issues people have found while testing (sets a missing power supply flag and improves bluetooth connection stability). I will probably hold off on submitting that until we figure out the right
solution to the hidraw issue.

Thanks,
Daniel

Quoted 2 lines

- Pierre-Loup

#23 lore raw
Quoted 5 lines · Daniel Ogorchock

<snip>
I will probably hold off on submitting that until we figure out the right
solution to the hidraw issue.

Any chance you could make your work available as a stand-alone driver? Something like this:
https://github.com/hadess/retrode
would make it easy to test in-development versions of the driver until it lands (I might want to give a try to the optional features for the GBros adapter as we discussed a couple of months ago).

Cheers

author maintainer
#24 lore raw

Hi Bastien,

Quoted 8 lines · Bastien Nocera
Quoted 5 lines · Daniel Ogorchock

<snip>
I will probably hold off on submitting that until we figure out the right
solution to the hidraw issue.

Quoted 9 lines

Any chance you could make your work available as a stand-alone driver? Something like this:
https://github.com/hadess/retrode
would make it easy to test in-development versions of the driver until it lands (I might want to give a try to the optional features for the GBros adapter as we discussed a couple of months ago).

Cheers

I've been keeping my latest patches in this tree:
https://github.com/DanielOgorchock/linux/commits/ogorchock
nicman23 has been maintaining a dkms module for it here:
https://github.com/nicman23/dkms-hid-nintendo
as well as an aur package here:
https://aur.archlinux.org/packages/hid-nintendo-dkms/
if either of those are helpful.

Thanks,
Daniel

#25 lore raw
In reply to Daniel Ogorchock
Quoted 7 lines

I'd been telling people to use firejail as a temporary workaround to prevent steam from seeing the hidraw device. Note that hid-nintendo sets the most significant bit of the evdev's version number to allow userspace applications to discern it from the default hid device. There's no current mechanism in the driver to yield to userspace using hidraw, but I can look at what hid-steam is currently doing to accomplish that.

Quoted 7 lines

I guess the downside to that method is that any other process listening to the controller's evdev events would cease to receive them (maybe a voice chat program using one of the buttons as a push-to-talk hotkey or something similar).

Does steam use hidraw for the sony dualshock controllers as well? If so, is hid-sony doing anything special for that usecase?

In hid-sony we are not doing anything like hid-steam is doing (no virtual extra hidraw device).

I just don't know how I feel about mixing of evdev and hidraw. It just means userspace drivers are doing things behind the back of the real driver (unless you add a virtual hidraw driver like hid-steam). For Sony devices I'm not a big fan as our devices are very complex. Our devices tend to use a single output report (same report id) for rumble, audio and other types of data. When we will expose audio properly, a user space application doing hidraw will mess up audio streams and other behavior (e.g. power settings are also in the same hid report).

I don't know a good way yet. Has the usage of EVIOCGRAB been explored? If I recall it was intended to claim exclusive access to a device at least for input. It is a way of avoiding of some of the issues for simpler devices.

Virtual devices could work too, but I really dislike the extra complexity to hid drivers. Just in Sony case our driver for future devices will already be quite complex. If we really want virtual device support, I don't think it would scale to add it to every driver. Maybe there would be a generic way in the HID driver in which drivers can blacklist certain reports or hook into just certain requests or so.

Thanks,
Roderick

author maintainer
#26 lore raw

Thanks for the input Roderick.

Quoted 6 lines

In hid-sony we are not doing anything like hid-steam is doing (no virtual extra hidraw device).

I just don't know how I feel about mixing of evdev and hidraw. It just means userspace drivers are doing things behind the back of the real driver (unless you add a virtual hidraw driver like hid-steam). For Sony devices I'm not a big fan as our devices are very complex. Our devices tend to use a single output report (same report id) for rumble, audio and other types of data. When we will expose audio properly, a user space application doing hidraw will mess up audio streams and other behavior (e.g. power settings are also in the same hid report).

I don't know a good way yet. Has the usage of EVIOCGRAB been explored? If I recall it was intended to claim exclusive access to a device at least for input. It is a way of avoiding of some of the issues for simpler devices.

I've had some success using EVIOCGRAB in joycond:
https://github.com/DanielOgorchock/joycond/blob/3969af9dcdc2b8199716ec08220df5d9ef7cfc6a/include/phys_ctlr.h#L46
It takes sole-control of the individual joy-con evdevs so it can re-expose them as a combined uinput device without having the original devices usable by other applications.

I forgot to note that using the joy-cons (wirelessly or using the charging grip) with steam seems to work fine. I think steam is just using the evdev in that case. That leads me to suspect steam would work fine with the pro controller evdev itself if the matching rules it uses internally are altered to check the evdev's version number as well (which hid-nintendo alters for distinction).

I guess the downside to using hid-nintendo as-is with steam is lack of gyro/accel support compared to steam's hidraw driver. Maybe it's best for me to finish up the gyro patch before getting this driver merged in that case. Several people have been successfully using the in-progress patch with the dolphin emulator. I need to clean things up with it though based on some of Roderick's previous feedback.

Thanks,
Daniel