Skip to contentSkip to conversation

HID: wiimote: proper joystick device support

RFC PATCH New Giuseppe Bilotta 1 participant 3 messages Jun 4, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover HID: wiimote: proper joystick device support

The Wii Remote driver exposes three input devices for each connected Wii
Remote: a main device for buttons and rumble, the accelorometer and the IR camera.

Since the main device exposes a BTN_A, it is considered to be a gamepad from the input subsystem, and it gets handled as a joystick, accessible via the appropriate /dev/input/jsN device. However, since the input device itself does not expose any positional information, it appears as a joystick with no axes (and 7 buttons).

The following two patches try to fix this problem by allowing buttons to also be exposed via the Accelerometer input device. This way this device also becomes a joystick, this time fully functional (three axes, 7 buttons).

The patchset is marked as RFC because I'm not 100% satisfied with the solution. Although it does make the device usable as joystick in software that does not have support for it otherwise, it presents two problems:

(1) button presses are reported twice, so one has to e.g. disable input from the main device in software that supports both keyboards and joysticks at the same time;
(2) the main device is still reported as a gamepad and caught by the joydev driver.

A possible solution (not yet implemented in this patchset) for issue #1 would be to have a control (accessible via e.g. the /sys interface) to select the device(s) for which the keys are reported.

For issue #2 there are two possible solutions: an explicit blacklist of the the Wii Remote main device, or a fine-tuning of joydev_ids list.

Suggestions and critism welcome.

Giuseppe Bilotta (2):
HID: wiimote: refactor key reporting
HID: wiimote: report keys in Accelerometer too

 drivers/hid/hid-wiimote-core.c |   44 ++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 22 deletions(-)
Signature · 1 line
1.7.10.rc3.204.g95589
#1 lore raw

RFC PATCH 1/2 HID: wiimote: refactor key reporting

A Wii Remote is exposed as three distinct input devices, but only the main one (which is the last registered) reports keys. To allow button presses to also be reported by other devices, we refactor the key reporting code out of handler_keys() in its own report_keys() function, that accepts an abritrary input device and an arbitrary key map.

---
 drivers/hid/hid-wiimote-core.c |   38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
drivers/hid/hid-wiimote-core.c +16 -22
index 84e2fbe..ece9c28 100644
@@ -690,30 +690,24 @@ static void wiimote_ir_close(struct input_dev *dev)
 	hid_hw_close(wdata->hdev);
 }
 
+static void report_keys(struct input_dev *input, const __u16 keymap[], const __u8 *payload)
+{
+	input_report_key(input, keymap[WIIPROTO_KEY_LEFT], !!(payload[0] & 0x01));
+	input_report_key(input, keymap[WIIPROTO_KEY_RIGHT], !!(payload[0] & 0x02));
+	input_report_key(input, keymap[WIIPROTO_KEY_DOWN], !!(payload[0] & 0x04));
+	input_report_key(input, keymap[WIIPROTO_KEY_UP], !!(payload[0] & 0x08));
+	input_report_key(input, keymap[WIIPROTO_KEY_PLUS], !!(payload[0] & 0x10));
+	input_report_key(input, keymap[WIIPROTO_KEY_TWO], !!(payload[1] & 0x01));
+	input_report_key(input, keymap[WIIPROTO_KEY_ONE], !!(payload[1] & 0x02));
+	input_report_key(input, keymap[WIIPROTO_KEY_B], !!(payload[1] & 0x04));
+	input_report_key(input, keymap[WIIPROTO_KEY_A], !!(payload[1] & 0x08));
+	input_report_key(input, keymap[WIIPROTO_KEY_MINUS], !!(payload[1] & 0x10));
+	input_report_key(input, keymap[WIIPROTO_KEY_HOME], !!(payload[1] & 0x80));
+}
+
 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
 {
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
-							!!(payload[0] & 0x01));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
-							!!(payload[0] & 0x02));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
-							!!(payload[0] & 0x04));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
-							!!(payload[0] & 0x08));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
-							!!(payload[0] & 0x10));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
-							!!(payload[1] & 0x01));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
-							!!(payload[1] & 0x02));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
-							!!(payload[1] & 0x04));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
-							!!(payload[1] & 0x08));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
-							!!(payload[1] & 0x10));
-	input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
-							!!(payload[1] & 0x80));
+	report_keys(wdata->input, wiiproto_keymap, payload);
 	input_sync(wdata->input);
 }
 
Signature · 1 line
1.7.10.rc3.204.g95589
#2 lore raw

RFC PATCH 2/2 HID: wiimote: report keys in Accelerometer too

In reply to Giuseppe Bilotta

The main input device registered by the wiimote driver gets classified as a gamepad because it reports the presence of a BTN_A button. This results in the detection of a joystick device with 7 buttons and no axes (thus unusable) when a Wii Remote is connected.

By letting the Accelerometer input device also report buttons, we get it to be recognized as a 7-buttons, 3-axes gamepad, that can be used on games with no direct Wii Remote support through the joydev interface.

This approach is currently not perfect: button presses gets reported on two devices (Remote and Accelerometer), and the main device is still classified as a gamepad due to the mapping.

---
 drivers/hid/hid-wiimote-core.c |    6 ++++++
 1 file changed, 6 insertions(+)
drivers/hid/hid-wiimote-core.c +6 -0
index ece9c28..2177dbe 100644
@@ -741,6 +741,7 @@ static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
 	input_report_abs(wdata->accel, ABS_RX, x - 0x200);
 	input_report_abs(wdata->accel, ABS_RY, y - 0x200);
 	input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
+	report_keys(wdata->accel, wiiproto_keymap, payload);
 	input_sync(wdata->accel);
 }
 
@@ -1096,6 +1097,11 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev)
 	input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
 	input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
 
+	set_bit(EV_KEY, wdata->accel->evbit);
+	for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
+		set_bit(wiiproto_keymap[i], wdata->accel->keybit);
+
+
 	wdata->ir = input_allocate_device();
 	if (!wdata->ir)
 		goto err_ir;
Signature · 1 line
1.7.10.rc3.204.g95589