Skip to contentSkip to conversation

HID: gamecube-adapter

PATCH New François-Xavier Carton 1 participant 5 messages Oct 14, 2020 HID

lore
Share & tools
lore
#0 lore raw

v3 cover HID: gamecube-adapter

Hi all,

This series adds a driver for Nintendo gamecube controller USB adapters. I've only done minor coding style edits since v2, to better conform to the kernel guidelines.

I'd greatly appreciate a review of the code (especially since this is my first contribution to the kernel). I've been applying this patch series to several 5.4 kernels, the latest being 5.9.66.

Many thanks,
François-Xavier

---

Changelog:

v3:
- minor coding style edits

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.

---

François-Xavier Carton (4):
HID: gamecube-adapter: add nintendo gamecube adapter
HID: gamecube-adapter: add rumble support
HID: gamecube-adapter: add auto calibration
HID: gamecube-adapter: restore input after suspend

 MAINTAINERS                        |   6 +
 drivers/hid/Kconfig                |  18 ++
 drivers/hid/Makefile               |   1 +
 drivers/hid/hid-gamecube-adapter.c | 492 +++++++++++++++++++++++++++++
 drivers/hid/hid-ids.h              |   1 +
 5 files changed, 518 insertions(+)
 create mode 100644 drivers/hid/hid-gamecube-adapter.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH v3 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.

---
 MAINTAINERS                        |   6 +
 drivers/hid/Kconfig                |  10 +
 drivers/hid/Makefile               |   1 +
 drivers/hid/hid-gamecube-adapter.c | 394 +++++++++++++++++++++++++++++
 drivers/hid/hid-ids.h              |   1 +
 5 files changed, 412 insertions(+)
 create mode 100644 drivers/hid/hid-gamecube-adapter.c
MAINTAINERS +6 -0
index d5b1878f2815..a4ff6219f84a 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..c2bb95bda44d 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 +394 -0

394 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
#2 lore raw

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

In reply to 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.

---
 drivers/hid/Kconfig                |  8 ++++
 drivers/hid/hid-gamecube-adapter.c | 77 +++++++++++++++++++++++++++++-
 2 files changed, 84 insertions(+), 1 deletion(-)
drivers/hid/Kconfig +8 -0
index c2bb95bda44d..04cbaa2625db 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 +76 -1

121 lines not on this page · raw message

Signature · 1 line
2.26.2
#3 lore raw

PATCH v3 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 | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/hid/hid-gamecube-adapter.c +18 -6
index d0bf09ba2762..53d372a24277 100644
@@ -56,6 +56,8 @@ struct gamecube_ctrl {
 	struct input_dev __rcu *input;
 	struct gamecube_adpt *adpt;
 	enum gamecube_ctrl_flags flags;
+	u8 axis_min[6];
+	u8 axis_max[6];
 	u8 rumble;
 	struct work_struct work_connect;
 	spinlock_t flags_lock;
@@ -264,6 +266,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;
@@ -271,6 +274,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;
 	}
@@ -294,12 +302,16 @@ 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
#4 lore raw

PATCH v3 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 53d372a24277..0951a25b048b 100644
@@ -452,6 +452,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_adpt *adpt = hid_get_drvdata(hdev);
@@ -473,6 +481,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