From patchwork Sat May 30 15:04:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Morse X-Patchwork-Id: 11580455 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4CED5739 for ; Sat, 30 May 2020 15:04:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2F21920722 for ; Sat, 30 May 2020 15:04:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729038AbgE3PEn (ORCPT ); Sat, 30 May 2020 11:04:43 -0400 Received: from mail-ej1-f68.google.com ([209.85.218.68]:40417 "EHLO mail-ej1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728927AbgE3PEm (ORCPT ); Sat, 30 May 2020 11:04:42 -0400 Received: by mail-ej1-f68.google.com with SMTP id ds18so962664ejc.7 for ; Sat, 30 May 2020 08:04:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=X+8wScorcl5bUo0xNotp/nOx/Z/GOVIpylgWA/jhOp8=; b=tkSlLsXT5hFQNJkQM2i1dn/dWpFj0Dh1ybEY81d4yjG3LOIV28d4pJlJHiBlxVOoDZ hRF3voxtlFqJEqtXlDjQnJB1JbaSuj8/yESzyl9vPdoIK68wWV4o5TuiuAlKlWPS1YFr 4cbYmrQNdrX0zC6mMadr41Ajj9VLYZYgKzBOcHg03soYY8XQqRvH31NBa1sPWShTS6cz YrdXDYTbp5SwEog/ZoM/AjsBpo6oFzg03T0KjeC06ShBCpn42bFlrNqv+pBjWHjU3lti n18bYLvOIZ3aqSETCUtBW7im/kv3jHJlqe28BWaPxsq6LdQIVrl+QgZiExqZIhz2GT9/ Mieg== X-Gm-Message-State: AOAM531Lhy3dY8zMhP7quhHC1A1SWsNj8Fa5JL9k1U6bZp6ogguBcd8f CW9xJ2ITuCPqCdOX13vjfNLCx3mQhxk= X-Google-Smtp-Source: ABdhPJxCeN67IuUwUGGcQ60AULy0HHvANBH5UEk7vTymi5J0bIElXilVn7PCS6NTMLu3vyqcj5fynw== X-Received: by 2002:a17:906:ae93:: with SMTP id md19mr12249902ejb.4.1590851078691; Sat, 30 May 2020 08:04:38 -0700 (PDT) Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com. [209.85.128.47]) by smtp.gmail.com with ESMTPSA id o24sm10192121ejb.72.2020.05.30.08.04.37 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 30 May 2020 08:04:38 -0700 (PDT) Received: by mail-wm1-f47.google.com with SMTP id d128so7073563wmc.1 for ; Sat, 30 May 2020 08:04:37 -0700 (PDT) X-Received: by 2002:a1c:9acf:: with SMTP id c198mr2359410wme.172.1590851077591; Sat, 30 May 2020 08:04:37 -0700 (PDT) MIME-Version: 1.0 From: Daniel Morse Date: Sat, 30 May 2020 16:04:26 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: [PATCH v3] HID: Wiimote: Treat the d-pad as an analogue stick To: David Rheinsberg , Jiri Kosina , "open list:HID CORE LAYER" Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org The controllers from the Super Nintendo Classic Edition (AKA the SNES Mini) appear as a Classic Controller Pro when connected to a Wii Remote. All the buttons work as the same, with the d-pad being mapped the same as the d-pad on the Classic Controller Pro. This differs from the behaviour of most controllers with d-pads and no analogue sticks, where the d-pad maps to ABS_HAT1X for left and right, and ABS_HAT1Y for up and down. This patch adds an option to the hid-wiimote module to make the Super Nintendo Classic Controller behave this way. The patch has been tested with a Super Nintendo Classic Controller plugged into a Wii Remote in both with the option both enabled and disabled. When enabled the d-pad acts as the analogue control, and when disabled it acts as it did before the patch was applied. This patch has not been tested with e Wii Classic Controller (either the original or the pro version) as I do not have one of these controllers. Although I have not tested it with these controllers, I think it is likely this patch will also work with the NES Classic Edition Controllers. Changes from V1 to V2 * 3 if statements to control the behaviour, one to make the d-pad register as button presses when the behaviour is disabled, and 2 to make it act as an analog stick when enabled (once for each of the motion plus states) * the values for lx and ly are calculated and then passed to input_report_abs() in one place, rather then calling input_report_abs() from different places depending on how the values are determined. * using an array to map from button presses to analog value. * reduced the values used to indicate the position of the analog stick Changes from V3 to V3 * Moved the definition of dpad_as_analog in hid-wiimote.h * Renamed the dpad_as_analog vaiable to wiimote_dpad_as_analog * changed digital_to_analog from an __s8 to static const s8 * Removed the unnecessary braces when getting values from digital_to_analog Note: I have also attached a patch file to this final version in addition to the code in the body because gmail web client keeps turning the tabs into spaces. Signed-off-by: Daniel G. Morse Reviewed-by: David Rheinsberg From 8909feec10fda919d1ec46c95418d63ac52ee41f Mon Sep 17 00:00:00 2001 From: Speedfox Date: Tue, 26 May 2020 02:55:50 +0100 Subject: [PATCH] Added option to make d-pad analog Updates from code review Reviewed-by: David Herrmann --- drivers/hid/hid-wiimote-core.c | 5 +++ drivers/hid/hid-wiimote-modules.c | 67 ++++++++++++++++++++----------- drivers/hid/hid-wiimote.h | 2 + 3 files changed, 50 insertions(+), 24 deletions(-) From 8909feec10fda919d1ec46c95418d63ac52ee41f Mon Sep 17 00:00:00 2001 From: Speedfox Date: Tue, 26 May 2020 02:55:50 +0100 Subject: [PATCH] Added option to make d-pad analog Updates from code review --- drivers/hid/hid-wiimote-core.c | 5 +++ drivers/hid/hid-wiimote-modules.c | 67 ++++++++++++++++++++----------- drivers/hid/hid-wiimote.h | 2 + 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/drivers/hid/hid-wiimote-core.c b/drivers/hid/hid-wiimote-core.c index 92874dbe4d4a..679e142fc850 100644 --- a/drivers/hid/hid-wiimote-core.c +++ b/drivers/hid/hid-wiimote-core.c @@ -1870,6 +1870,11 @@ static const struct hid_device_id wiimote_hid_devices[] = { USB_DEVICE_ID_NINTENDO_WIIMOTE2) }, { } }; + +bool wiimote_dpad_as_analog = false; +module_param_named(dpad_as_analog, wiimote_dpad_as_analog, bool, 0644); +MODULE_PARM_DESC(dpad_as_analog, "Use D-Pad as main analog input"); + MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); static struct hid_driver wiimote_hid_driver = { diff --git a/drivers/hid/hid-wiimote-modules.c b/drivers/hid/hid-wiimote-modules.c index 2c3925357857..213c58bf2495 100644 --- a/drivers/hid/hid-wiimote-modules.c +++ b/drivers/hid/hid-wiimote-modules.c @@ -1088,12 +1088,28 @@ static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext) * is the same as before. */ + static const s8 digital_to_analog[3] = {0x20, 0, -0x20}; + if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { - lx = ext[0] & 0x3e; - ly = ext[1] & 0x3e; + if (wiimote_dpad_as_analog) { + lx = digital_to_analog[1 - !(ext[4] & 0x80) + + !(ext[1] & 0x01)]; + ly = digital_to_analog[1 - !(ext[4] & 0x40) + + !(ext[0] & 0x01)]; + } else { + lx = (ext[0] & 0x3e) - 0x20; + ly = (ext[1] & 0x3e) - 0x20; + } } else { - lx = ext[0] & 0x3f; - ly = ext[1] & 0x3f; + if (wiimote_dpad_as_analog) { + lx = digital_to_analog[1 - !(ext[4] & 0x80) + + !(ext[5] & 0x02)]; + ly = digital_to_analog[1 - !(ext[4] & 0x40) + + !(ext[5] & 0x01)]; + } else { + lx = (ext[0] & 0x3f) - 0x20; + ly = (ext[1] & 0x3f) - 0x20; + } } rx = (ext[0] >> 3) & 0x18; @@ -1110,19 +1126,13 @@ static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext) rt <<= 1; lt <<= 1; - input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20); - input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20); + input_report_abs(wdata->extension.input, ABS_HAT1X, lx); + input_report_abs(wdata->extension.input, ABS_HAT1Y, ly); input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20); input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20); input_report_abs(wdata->extension.input, ABS_HAT3X, rt); input_report_abs(wdata->extension.input, ABS_HAT3Y, lt); - input_report_key(wdata->extension.input, - wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT], - !(ext[4] & 0x80)); - input_report_key(wdata->extension.input, - wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN], - !(ext[4] & 0x40)); input_report_key(wdata->extension.input, wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT], !(ext[4] & 0x20)); @@ -1157,20 +1167,29 @@ static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext) wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR], !(ext[5] & 0x04)); - if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { - input_report_key(wdata->extension.input, - wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT], - !(ext[1] & 0x01)); - input_report_key(wdata->extension.input, - wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP], - !(ext[0] & 0x01)); - } else { + if (!wiimote_dpad_as_analog) { input_report_key(wdata->extension.input, - wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT], - !(ext[5] & 0x02)); + wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT], + !(ext[4] & 0x80)); input_report_key(wdata->extension.input, - wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP], - !(ext[5] & 0x01)); + wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN], + !(ext[4] & 0x40)); + + if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { + input_report_key(wdata->extension.input, + wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT], + !(ext[1] & 0x01)); + input_report_key(wdata->extension.input, + wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP], + !(ext[0] & 0x01)); + } else { + input_report_key(wdata->extension.input, + wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT], + !(ext[5] & 0x02)); + input_report_key(wdata->extension.input, + wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP], + !(ext[5] & 0x01)); + } } input_sync(wdata->extension.input); diff --git a/drivers/hid/hid-wiimote.h b/drivers/hid/hid-wiimote.h index b2a26a0a8f12..ad4ff837f43e 100644 --- a/drivers/hid/hid-wiimote.h +++ b/drivers/hid/hid-wiimote.h @@ -162,6 +162,8 @@ struct wiimote_data { struct work_struct init_worker; }; +extern bool wiimote_dpad_as_analog; + /* wiimote modules */ enum wiimod_module { -- 2.25.1