From patchwork Sat Aug 26 15:21:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Staudt X-Patchwork-Id: 717630 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CC1BC83F13 for ; Sat, 26 Aug 2023 15:33:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229563AbjHZPdX (ORCPT ); Sat, 26 Aug 2023 11:33:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229473AbjHZPcv (ORCPT ); Sat, 26 Aug 2023 11:32:51 -0400 Received: from mail.enpas.org (zhong.enpas.org [46.38.239.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0666106 for ; Sat, 26 Aug 2023 08:32:49 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) by mail.enpas.org (Postfix) with ESMTPSA id 3B5C51014E9; Sat, 26 Aug 2023 15:25:16 +0000 (UTC) From: Max Staudt To: Dmitry Torokhov , Jiri Kosina , Benjamin Tissoires Cc: Vicki Pfau , Pavel Rojtberg , Roderick Colenbrander , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Max Staudt Subject: [PATCH 1/2] xpad: XTYPE_XBOX: Report analog buttons Date: Sun, 27 Aug 2023 00:21:10 +0900 Message-Id: <20230826152111.13525-2-max@enpas.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230826152111.13525-1-max@enpas.org> References: <20230826152111.13525-1-max@enpas.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org The original Xbox controllers (XTYPE_XBOX) report 8 buttons in an analog fashion, in addition to the digital on/off state: - Action buttons A/B/X/Y/black/white - Triggers L/R Up until now, only the triggers L/R are reported as values 0-255. The other pressure sensitive buttons are reported as digital buttons, as found on other controllers. This change exposes these buttons as axes in a way that is as backwards compatible as possible. The new axes are merely added, and numbered after any existing axes. This way, libraries like SDL which renumber axes in enumeration order, can keep their button/axis mapping as-is. Userspace can keep working as before, and can optionally use the new values when handling this type of gamepad. - BTN_A..BTN_Z mapped to ABS_MISC+0..ABS_MISC+5, 0 to 255 Signed-off-by: Max Staudt --- drivers/input/joystick/xpad.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c index cdb193317c3b..609c06f795de 100644 --- a/drivers/input/joystick/xpad.c +++ b/drivers/input/joystick/xpad.c @@ -420,6 +420,14 @@ static const signed short xpad_abs_triggers[] = { -1 }; +/* used for analog face buttons mapped to axes */ +static const signed short xpad_abs_analog_face_buttons[] = { + ABS_MISC + 0, ABS_MISC + 1, /* A, B */ + ABS_MISC + 3, ABS_MISC + 4, /* X, Y */ + ABS_MISC + 2, ABS_MISC + 5, /* C, Z */ + -1 +}; + /* used when the controller has extra paddle buttons */ static const signed short xpad_btn_paddles[] = { BTN_TRIGGER_HAPPY5, BTN_TRIGGER_HAPPY6, /* paddle upper right, lower right */ @@ -784,6 +792,15 @@ static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *d input_report_key(dev, BTN_C, data[8]); input_report_key(dev, BTN_Z, data[9]); + /* analog buttons A, B, X, Y as axes */ + input_report_abs(dev, xpad_abs_analog_face_buttons[0], data[4]); /* A */ + input_report_abs(dev, xpad_abs_analog_face_buttons[1], data[5]); /* B */ + input_report_abs(dev, xpad_abs_analog_face_buttons[2], data[6]); /* X */ + input_report_abs(dev, xpad_abs_analog_face_buttons[3], data[7]); /* Y */ + + /* analog buttons black, white (C, Z) as axes */ + input_report_abs(dev, xpad_abs_analog_face_buttons[4], data[8]); /* C */ + input_report_abs(dev, xpad_abs_analog_face_buttons[5], data[9]); /* Z */ input_sync(dev); } @@ -1827,6 +1844,14 @@ static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs) case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */ input_set_abs_params(input_dev, abs, -1, 1, 0, 0); break; + case ABS_MISC + 0: + case ABS_MISC + 1: + case ABS_MISC + 2: + case ABS_MISC + 3: + case ABS_MISC + 4: + case ABS_MISC + 5: + input_set_abs_params(input_dev, abs, 0, 255, 0, 0); + break; case ABS_PROFILE: /* 4 value profile button (such as on XAC) */ input_set_abs_params(input_dev, abs, 0, 4, 0, 0); break; @@ -1928,6 +1953,10 @@ static int xpad_init_input(struct usb_xpad *xpad) xpad_set_up_abs(input_dev, xpad_abs_triggers[i]); } + if (xpad->xtype == XTYPE_XBOX) + for (i = 0; xpad_abs_analog_face_buttons[i] >= 0; i++) + xpad_set_up_abs(input_dev, xpad_abs_analog_face_buttons[i]); + /* setup profile button as an axis with 4 possible values */ if (xpad->mapping & MAP_PROFILE_BUTTON) xpad_set_up_abs(input_dev, ABS_PROFILE); From patchwork Sat Aug 26 15:21:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Staudt X-Patchwork-Id: 717330 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DB78C83F01 for ; Sat, 26 Aug 2023 15:33:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229573AbjHZPdX (ORCPT ); Sat, 26 Aug 2023 11:33:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229563AbjHZPcv (ORCPT ); Sat, 26 Aug 2023 11:32:51 -0400 X-Greylist: delayed 450 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Sat, 26 Aug 2023 08:32:49 PDT Received: from mail.enpas.org (zhong.enpas.org [46.38.239.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9552DF5 for ; Sat, 26 Aug 2023 08:32:49 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) by mail.enpas.org (Postfix) with ESMTPSA id B3E3D101532; Sat, 26 Aug 2023 15:25:20 +0000 (UTC) From: Max Staudt To: Dmitry Torokhov , Jiri Kosina , Benjamin Tissoires Cc: Vicki Pfau , Pavel Rojtberg , Roderick Colenbrander , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Max Staudt Subject: [PATCH 2/2] hid-sony: DS3: Report analog buttons for Sixaxis Date: Sun, 27 Aug 2023 00:21:11 +0900 Message-Id: <20230826152111.13525-3-max@enpas.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230826152111.13525-1-max@enpas.org> References: <20230826152111.13525-1-max@enpas.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org The Sixaxis and DualShock 3 controllers report 12 buttons in an analog fashion, in addition to the digital on/off state: - D-Pad up/down/left/right - Action buttons Triangle/Circle/Cross/Square - L1/R1 - Triggers L2/R2 Up until now, only the triggers L2/R2 are reported as values 0-255. The other pressure sensitive buttons are reported as digital buttons, as found on other controllers. This change exposes these buttons as axes in a way that is as backwards compatible and as close to the Linux gamepad spec as possible. The new axes are merely added, and numbered after any existing axes. This way, libraries like SDL which renumber axes in enumeration order, can keep their button/axis mapping as-is. Userspace can keep working as before, and can optionally use the new values when handling this type of gamepad. - The D-Pad as ABS_HAT0X/ABS_HAT0Y, -255 to 255 - R1 as ABS_HAT1X, 0 to 255 - L1 as ABS_HAT1Y, 0 to 255 - BTN_A..BTN_Z mapped to ABS_MISC+0..ABS_MISC+5, 0 to 255 Most buttons are straight HID remappings in sixaxis_mapping(). For the D-Pad, two pairs of buttons need to be merged to a single axis each, so this is handled manually in sixaxis_parse_report(). Signed-off-by: Max Staudt --- drivers/hid/hid-sony.c | 66 ++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c index dd942061fd77..642fd715ba39 100644 --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c @@ -484,6 +484,7 @@ struct sony_sc { spinlock_t lock; struct list_head list_node; struct hid_device *hdev; + struct input_dev *gamepad; struct input_dev *touchpad; struct input_dev *sensor_dev; struct led_classdev *leds[MAX_LEDS]; @@ -711,22 +712,37 @@ static int sixaxis_mapping(struct hid_device *hdev, struct hid_input *hi, } else if (usage->hid == HID_GD_POINTER) { /* The DS3 provides analog values for most buttons and even * for HAT axes through GD Pointer. L2 and R2 are reported - * among these as well instead of as GD Z / RZ. Remap L2 - * and R2 and ignore other analog 'button axes' as there is - * no good way for reporting them. + * among these as well instead of as GD Z / RZ. */ + __u16 c; + switch (usage->usage_index) { + case 10: /* L1 */ + c = ABS_HAT1Y; + break; + case 11: /* R1 */ + c = ABS_HAT1X; + break; + case 12: /* NORTH */ + case 13: /* EAST */ + case 14: /* SOUTH */ + case 15: /* WEST */ + c = sixaxis_keymap[usage->usage_index + 1] + - BTN_GAMEPAD + ABS_MISC; + break; case 8: /* L2 */ usage->hid = HID_GD_Z; + c = ABS_Z; break; case 9: /* R2 */ usage->hid = HID_GD_RZ; + c = ABS_RZ; break; default: return -1; } - hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf); + hid_map_usage_clear(hi, usage, bit, max, EV_ABS, c); return 1; } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) { unsigned int abs = usage->hid & HID_USAGE; @@ -837,6 +853,17 @@ static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size) val = 511 - ((rd[offset+3] << 8) | rd[offset+2]); input_report_abs(sc->sensor_dev, ABS_Z, val); + /* Report analog D-pad */ + if (rd[17] > rd[15]) /* left */ + input_report_abs(sc->gamepad, ABS_HAT0X, 0 - rd[17]); + else /* right */ + input_report_abs(sc->gamepad, ABS_HAT0X, rd[15]); + + if (rd[14] > rd[16]) /* up */ + input_report_abs(sc->gamepad, ABS_HAT0Y, 0 - rd[14]); + else /* down */ + input_report_abs(sc->gamepad, ABS_HAT0Y, rd[16]); + input_sync(sc->sensor_dev); } } @@ -1597,18 +1624,8 @@ static int sony_play_effect(struct input_dev *dev, void *data, static int sony_init_ff(struct sony_sc *sc) { - struct hid_input *hidinput; - struct input_dev *input_dev; - - if (list_empty(&sc->hdev->inputs)) { - hid_err(sc->hdev, "no inputs found\n"); - return -ENODEV; - } - hidinput = list_entry(sc->hdev->inputs.next, struct hid_input, list); - input_dev = hidinput->input; - - input_set_capability(input_dev, EV_FF, FF_RUMBLE); - return input_ff_create_memless(input_dev, NULL, sony_play_effect); + input_set_capability(sc->gamepad, EV_FF, FF_RUMBLE); + return input_ff_create_memless(sc->gamepad, NULL, sony_play_effect); } #else @@ -2039,6 +2056,23 @@ static int sony_input_configured(struct hid_device *hdev, } } + if (sc->quirks & (SONY_FF_SUPPORT | SIXAXIS_CONTROLLER)) { + struct hid_input *hidinput; + + if (list_empty(&sc->hdev->inputs)) { + hid_err(sc->hdev, "no inputs found\n"); + return -ENODEV; + } + hidinput = list_entry(sc->hdev->inputs.next, struct hid_input, list); + sc->gamepad = hidinput->input; + } + + if (sc->quirks & SIXAXIS_CONTROLLER) { + /* Register axes for analog buttons */ + input_set_abs_params(sc->gamepad, ABS_HAT0X, -255, 255, 0, 0); + input_set_abs_params(sc->gamepad, ABS_HAT0Y, -255, 255, 0, 0); + } + if (sc->quirks & SONY_FF_SUPPORT) { ret = sony_init_ff(sc); if (ret < 0)