Skip to contentSkip to conversation

HID: gamecube-adapter: add nintendo gamecube adapter

PATCH Superseded François-Xavier Carton 2 participants 6 messages May 12, 2020 – Jul 21, 2020 HID

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 HID: gamecube-adapter: add nintendo gamecube adapter

The hid-gamecube-adapter driver supports Nintendo Gamecube Controller Adapters. They are USB devices on which up to four Nintendo Gamecube Controllers can be plugged. The driver creates independent input devices as controllers are connected.

---

Changelog:
v2:
- Replaced calibration parameters by auto calibration using saved min/max values.
- Only send rumble output reports if the value changed.
- Only set rumble capability for normal controllers; wavebird controllers do not support rumble.
- Send init command after suspend to get input reports again.

 MAINTAINERS                        |   6 +
 drivers/hid/Kconfig                |  10 +
 drivers/hid/Makefile               |   1 +
 drivers/hid/hid-gamecube-adapter.c | 386 +++++++++++++++++++++++++++++
 drivers/hid/hid-ids.h              |   1 +
 5 files changed, 404 insertions(+)
 create mode 100644 drivers/hid/hid-gamecube-adapter.c
MAINTAINERS +6 -0
index d5b1878f2815..585ddcf3a6dd 100644
@@ -7025,6 +7025,12 @@ F:	scripts/gcc-plugin.sh
 F:	scripts/Makefile.gcc-plugins
 F:	Documentation/kbuild/gcc-plugins.rst
 
+GAMECUBE ADAPTER HID DRIVER
+M: François-Xavier Carton <[email protected]>
+L: [email protected]
+S: Maintained
+F: drivers/hid/hid-gamecube-adapter*
+
 GASKET DRIVER FRAMEWORK
 M:	Rob Springer <[email protected]>
 M:	Todd Poynor <[email protected]>
drivers/hid/Kconfig +10 -0
index 7c89edbd6c5a..d49e261a74f6 100644
@@ -350,6 +350,16 @@ config HID_EZKEY
 	---help---
 	Support for Ezkey BTC 8193 keyboard.
 
+config HID_GAMECUBE_ADAPTER
+	tristate "Nintendo Gamecube Controller Adapter support"
+	depends on HID
+	depends on USB_HID
+	---help---
+	Support for the Nintendo Gamecube Controller Adapter.
+
+	To compile this driver as a module, choose M here: the
+	module will be called hid-gamecube-adapter.
+
 config HID_GEMBIRD
 	tristate "Gembird Joypad"
 	depends on HID
drivers/hid/Makefile +1 -0
index d8ea4b8c95af..9cddc4d48db8 100644
@@ -46,6 +46,7 @@ obj-$(CONFIG_HID_ELAN)		+= hid-elan.o
 obj-$(CONFIG_HID_ELECOM)	+= hid-elecom.o
 obj-$(CONFIG_HID_ELO)		+= hid-elo.o
 obj-$(CONFIG_HID_EZKEY)		+= hid-ezkey.o
+obj-$(CONFIG_HID_GAMECUBE_ADAPTER)	+= hid-gamecube-adapter.o
 obj-$(CONFIG_HID_GEMBIRD)	+= hid-gembird.o
 obj-$(CONFIG_HID_GFRM)		+= hid-gfrm.o
 obj-$(CONFIG_HID_GLORIOUS)  += hid-glorious.o
drivers/hid/hid-gamecube-adapter.c new file +386 -0

386 lines not on this page · raw message

drivers/hid/hid-ids.h +1 -0
index b18b13147a6f..1ebea811ea3b 100644
@@ -882,6 +882,7 @@
 #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_GAMECUBE_ADAPTER	0x0337
 
 #define USB_VENDOR_ID_NOVATEK		0x0603
 #define USB_DEVICE_ID_NOVATEK_PCT	0x0600
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 2/4 HID: gamecube-adapter: add rumble support

Add rumble support for the hid-gamecube-adapter driver. Rumble is reported with a single output report for all four controllers.

---
 drivers/hid/Kconfig                |  8 ++++
 drivers/hid/hid-gamecube-adapter.c | 75 +++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 1 deletion(-)
drivers/hid/Kconfig +8 -0
index d49e261a74f6..324981308783 100644
@@ -360,6 +360,14 @@ config HID_GAMECUBE_ADAPTER
 	To compile this driver as a module, choose M here: the
 	module will be called hid-gamecube-adapter.
 
+config HID_GAMECUBE_ADAPTER_FF
+	bool "Nintendo Gamecube Controller Adapter force feedback"
+	depends on HID_GAMECUBE_ADAPTER
+	select INPUT_FF_MEMLESS
+	---help---
+	Say Y here if you want to enable force feedback support for Nintendo
+	Gamecube Controller Adapters.
+
 config HID_GEMBIRD
 	tristate "Gembird Joypad"
 	depends on HID
drivers/hid/hid-gamecube-adapter.c +74 -1

120 lines not on this page · raw message

Signature · 1 line
2.26.2
maintainer
#2 lore raw

Re: [PATCH v2 2/4] HID: gamecube-adapter: add rumble support

Quoted 4 lines · François-Xavier Carton

Add rumble support for the hid-gamecube-adapter driver. Rumble is reported with a single output report for all four controllers.

Francois,

somehow I seem to be missing 1/4 from this series in my inbox. Could you please resend it my way?

Thanks.

Signature · 2 lines
Jiri Kosina
SUSE Labs
#3 lore raw

Re: [PATCH v2 2/4] HID: gamecube-adapter: add rumble support

Quoted 5 lines · Jiri Kosina
Quoted 3 lines · François-Xavier Carton

Add rumble support for the hid-gamecube-adapter driver. Rumble is reported with a single output report for all four controllers.

Quoted 13 lines
Quoted 1 line

Francois,

somehow I seem to be missing 1/4 from this series in my inbox. Could you please resend it my way?

Thanks.

Signature · 2 lines
Jiri Kosina
SUSE Labs

Jiri,

Attached is the first patch.

Sorry for the late reply, I missed your email.

Thanks,
François-Xavier

From 6064a903f39e9c25d0016224525e8b907620aa5d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Carton?= <[email protected]>
Date: Sun, 12 Apr 2020 15:30:19 +0200
Subject: [PATCH v2 1/4] HID: gamecube-adapter: add nintendo gamecube adapter
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The hid-gamecube-adapter driver supports Nintendo Gamecube Controller Adapters. They are USB devices on which up to four Nintendo Gamecube Controllers can be plugged. The driver creates independent input devices as controllers are connected.

---

Changelog:
v2:
- Replaced calibration parameters by auto calibration using saved min/max values.
- Only send rumble output reports if the value changed.
- Only set rumble capability for normal controllers; wavebird controllers do not support rumble.
- Send init command after suspend to get input reports again.

 MAINTAINERS                        |   6 +
 drivers/hid/Kconfig                |  10 +
 drivers/hid/Makefile               |   1 +
 drivers/hid/hid-gamecube-adapter.c | 386 +++++++++++++++++++++++++++++
 drivers/hid/hid-ids.h              |   1 +
 5 files changed, 404 insertions(+)
 create mode 100644 drivers/hid/hid-gamecube-adapter.c
MAINTAINERS +6 -0
index d5b1878f2815..585ddcf3a6dd 100644
@@ -7025,6 +7025,12 @@ F:	scripts/gcc-plugin.sh
 F:	scripts/Makefile.gcc-plugins
 F:	Documentation/kbuild/gcc-plugins.rst
 
+GAMECUBE ADAPTER HID DRIVER
+M: François-Xavier Carton <[email protected]>
+L: [email protected]
+S: Maintained
+F: drivers/hid/hid-gamecube-adapter*
+
 GASKET DRIVER FRAMEWORK
 M:	Rob Springer <[email protected]>
 M:	Todd Poynor <[email protected]>
drivers/hid/Kconfig +10 -0
index 7c89edbd6c5a..d49e261a74f6 100644
@@ -350,6 +350,16 @@ config HID_EZKEY
 	---help---
 	Support for Ezkey BTC 8193 keyboard.
 
+config HID_GAMECUBE_ADAPTER
+	tristate "Nintendo Gamecube Controller Adapter support"
+	depends on HID
+	depends on USB_HID
+	---help---
+	Support for the Nintendo Gamecube Controller Adapter.
+
+	To compile this driver as a module, choose M here: the
+	module will be called hid-gamecube-adapter.
+
 config HID_GEMBIRD
 	tristate "Gembird Joypad"
 	depends on HID
drivers/hid/Makefile +1 -0
index d8ea4b8c95af..9cddc4d48db8 100644
@@ -46,6 +46,7 @@ obj-$(CONFIG_HID_ELAN)		+= hid-elan.o
 obj-$(CONFIG_HID_ELECOM)	+= hid-elecom.o
 obj-$(CONFIG_HID_ELO)		+= hid-elo.o
 obj-$(CONFIG_HID_EZKEY)		+= hid-ezkey.o
+obj-$(CONFIG_HID_GAMECUBE_ADAPTER)	+= hid-gamecube-adapter.o
 obj-$(CONFIG_HID_GEMBIRD)	+= hid-gembird.o
 obj-$(CONFIG_HID_GFRM)		+= hid-gfrm.o
 obj-$(CONFIG_HID_GLORIOUS)  += hid-glorious.o
drivers/hid/hid-gamecube-adapter.c new file +386 -0

386 lines not on this page · raw message

drivers/hid/hid-ids.h +1 -0
index b18b13147a6f..1ebea811ea3b 100644
@@ -882,6 +882,7 @@
 #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_GAMECUBE_ADAPTER	0x0337
 
 #define USB_VENDOR_ID_NOVATEK		0x0603
 #define USB_DEVICE_ID_NOVATEK_PCT	0x0600
Signature · 1 line
2.26.2
#4 lore raw

PATCH v2 3/4 HID: gamecube-adapter: add auto calibration

In reply to François-Xavier Carton

The axes do not cover the full 0-255 range, with different limit values for each axis. The minimal and maximal values are recorded for each axis and the values are remapped from that range to 0-255.

---
 drivers/hid/hid-gamecube-adapter.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
drivers/hid/hid-gamecube-adapter.c +17 -6
index 028ce005c9e3..efcbd8f27b67 100644
@@ -55,6 +55,8 @@ enum gamecube_btn {
 struct gamecube_ctrl {
 	struct input_dev __rcu *input;
 	enum gamecube_ctrl_flags flags;
+	u8 axis_min[6];
+	u8 axis_max[6];
 	u8 rumble;
 	struct gamecube_adapter *adpt;
 	struct work_struct work_connect;
@@ -258,6 +260,7 @@ static void gamecube_ctrl_handle_report(struct gamecube_ctrl *ctrl, u8 *data)
 	u16 btns = data[1] << 8 | data[2];
 	u8 old_flags, new_flags = data[0];
 	unsigned long irq_flags;
+	unsigned int i;
 
 	spin_lock_irqsave(&ctrl->flags_lock, irq_flags);
 	old_flags = ctrl->flags;
@@ -265,6 +268,11 @@ static void gamecube_ctrl_handle_report(struct gamecube_ctrl *ctrl, u8 *data)
 	spin_unlock_irqrestore(&ctrl->flags_lock, irq_flags);
 
 	if ((new_flags & GC_TYPES) != (old_flags & GC_TYPES)) {
+		// Reset min/max values. The default values were obtained empirically
+		for (i = 0; i < 6; i++) {
+			ctrl->axis_min[i] = 45; // max across all axes of min values
+			ctrl->axis_max[i] = 215; // min across all axes of max values
+		}
 		schedule_work(&ctrl->work_connect);
 		return;
 	}
@@ -288,12 +296,15 @@ static void gamecube_ctrl_handle_report(struct gamecube_ctrl *ctrl, u8 *data)
 	input_report_key(dev, BTN_DPAD_RIGHT, btns & GC_BTN_DPAD_RIGHT);
 	input_report_key(dev, BTN_DPAD_DOWN, btns & GC_BTN_DPAD_DOWN);
 	input_report_key(dev, BTN_DPAD_UP, btns & GC_BTN_DPAD_UP);
-	input_report_abs(dev, ABS_X, data[3]);
-	input_report_abs(dev, ABS_Y, 255 - data[4]);
-	input_report_abs(dev, ABS_RX, data[5]);
-	input_report_abs(dev, ABS_RY, 255 - data[6]);
-	input_report_abs(dev, ABS_Z, data[7]);
-	input_report_abs(dev, ABS_RZ, data[8]);
+	for (i = 0; i < 6; i++) {
+		u8 a, b, v = data[3 + i];
+		a = ctrl->axis_min[i] = min(ctrl->axis_min[i], v);
+		b = ctrl->axis_max[i] = max(ctrl->axis_max[i], v);
+		v = 255U * (v - a) / (b - a);
+		if (gamecube_axes[i] == ABS_Y || gamecube_axes[i] == ABS_RY)
+			v = 255U - v;
+		input_report_abs(dev, gamecube_axes[i], v);
+	}
 	input_sync(dev);
 
 unlock:
Signature · 1 line
2.26.2
#5 lore raw

PATCH v2 4/4 HID: gamecube-adapter: restore input after suspend

In reply to François-Xavier Carton

Send the init command after suspend, so that we get input reports again.

---
 drivers/hid/hid-gamecube-adapter.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/hid/hid-gamecube-adapter.c +11 -0
index efcbd8f27b67..11d3e483f0c1 100644
@@ -441,6 +441,14 @@ static int gamecube_hid_probe(struct hid_device *hdev,
 	return ret;
 }
 
+#ifdef CONFIG_PM
+static int gamecube_resume(struct hid_device *hdev)
+{
+	gamecube_send_cmd_init(hdev);
+	return 0;
+}
+#endif
+
 static void gamecube_hid_remove(struct hid_device *hdev)
 {
 	struct gamecube_adapter *adpt = hid_get_drvdata(hdev);
@@ -462,6 +470,9 @@ static struct hid_driver gamecube_hid_driver = {
 	.probe		= gamecube_hid_probe,
 	.remove		= gamecube_hid_remove,
 	.raw_event	= gamecube_hid_event,
+#ifdef CONFIG_PM
+	.reset_resume	= gamecube_resume,
+#endif
 };
 module_hid_driver(gamecube_hid_driver);
 
Signature · 1 line
2.26.2