From patchwork Fri Feb 7 13:55:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 863134 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15CBA1F755E; Fri, 7 Feb 2025 13:56:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936587; cv=none; b=MaIoDXcXnfwRAaMZ9cB3Q6L9hz8v8sBfDnwhz3C5xn0gsba0bSxFmF5ikwM4qi5paWuBFG0M4l91hRd+F2UIEx0cu8ZhSAyV+xcTzn/RQUXEOUqIXvo1TfrxGGzmCRMg2yvdbWz2Lk4DMY7xve0keUAZogS5T2dF4QvoVx4qCb4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936587; c=relaxed/simple; bh=KX7/ev0TKauzrT6XQNOQRS7okrpHKRqzvNCBj1pLpXQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nFIi7O3CjmSe7kigk5Gg9khF6hHEkQIgsXxRpSpKXDSHKO9B0gfOqAVUC8ePnJjR5cMYCXJaf123x0EpxRDS/YExO0Hmz23yCLhmQrOW9yPr+oMOT9SPR3W/DxEogUbXrZoBqdSdirNM/ytx5E/Nk4wmrlexncqwq0jZjKz+Lvc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KGshmPxg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KGshmPxg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55869C4AF09; Fri, 7 Feb 2025 13:56:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738936586; bh=KX7/ev0TKauzrT6XQNOQRS7okrpHKRqzvNCBj1pLpXQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=KGshmPxg2qKYWad6iZcinB8TboGwrRndfBEX45ZJ7AOjp1XpxSL9zTnDVYXZ41J9Y arluBIFi2Ld4Apm9cowNOBtFr8uj1Bjn8ahKu8Z7kFMneY3T++fhUa6Dr8r4mCGeks 9FVJCG8rH+XbKjKhH2pqHH1p9HVUBgSV2tSmkw5wF6DtMnvpsRGSKawJLPWB1JnuzL TvtIcHPt0DJjnGXAkb73tjJ806bUwO+1hVMFVnGHMjK5miV63bKmLppCYTVblo2Wua YzvSlOXDrnhGe4QeJo1X5Q9LFfzpH/MAH/XYqQS+sKBKjF8QYVMOTiiMg27uAplL9p MfHPed3lgOMBg== From: Benjamin Tissoires Date: Fri, 07 Feb 2025 14:55:57 +0100 Subject: [PATCH 1/7] HID: bpf: Add support for the default firmware mode of the Huion K20 Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250207-bpf-import-2025-02-07-v1-1-6048fdd5a206@kernel.org> References: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> In-Reply-To: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> To: Jiri Kosina , Peter Hutterer Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738936583; l=24970; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=KX7/ev0TKauzrT6XQNOQRS7okrpHKRqzvNCBj1pLpXQ=; b=9ne6IxuTyAwtNn6kP53u+K/Uw5rYh6KZ94ee4zQwWQxb93OTCAkAXTlHzuvypKQqk1/sKAqAv Y6VWhT7wMVcAbzZqsDZEkDONk0ROFjldJew4HII5m7EO4J/Eb253BnN X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= This re-uses the same HID report descriptor that we also use for the tablet mode after switching the device. The keys send uniquely identifyable shortcuts so we can map those to buttons. However the dial and the button inside the dial send events on a different hidraw node and they are unreliable (e.g. the button does not get released reliably). So they're ignored in this patch, it's not worth the effort getting those to work correctly. Signed-off-by: Peter Hutterer Link: https://gitlab.freedesktop.org/libevdev/udev-hid-bpf/-/merge_requests/158 Signed-off-by: Benjamin Tissoires --- drivers/hid/bpf/progs/Huion__KeydialK20.bpf.c | 531 ++++++++++++++++++++++++++ 1 file changed, 531 insertions(+) diff --git a/drivers/hid/bpf/progs/Huion__KeydialK20.bpf.c b/drivers/hid/bpf/progs/Huion__KeydialK20.bpf.c new file mode 100644 index 0000000000000000000000000000000000000000..ec360d71130f7edee7faf5d64f0e81ddf65291c9 --- /dev/null +++ b/drivers/hid/bpf/progs/Huion__KeydialK20.bpf.c @@ -0,0 +1,531 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024 Red Hat, Inc + */ + +#include "vmlinux.h" +#include "hid_bpf.h" +#include "hid_bpf_helpers.h" +#include "hid_report_helpers.h" +#include + +#define VID_HUION 0x256C +#define PID_KEYDIAL_K20 0x0069 + +HID_BPF_CONFIG( + HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_HUION, PID_KEYDIAL_K20), +); + +/* Filled in by udev-hid-bpf */ +char UDEV_PROP_HUION_FIRMWARE_ID[64]; + +/* The prefix of the firmware ID we expect for this device. The full firmware + * string has a date suffix, e.g. HUION_T21h_230511 + */ +char EXPECTED_FIRMWARE_ID[] = "HUION_T21h_"; + +/* How this BPF program works: the tablet has two modes, firmware mode and + * tablet mode. In firmware mode (out of the box) the tablet sends button events + * as keyboard shortcuts and the dial as wheel but it's not forwarded by the kernel. + * In tablet mode it uses a vendor specific hid report to report everything instead. + * Depending on the mode some hid reports are never sent and the corresponding + * devices are mute. + * + * To switch the tablet use e.g. https://github.com/whot/huion-switcher + * or one of the tools from the digimend project + * + * This BPF currently works for both modes only. The huion-switcher tool sets the + * HUION_FIRMWARE_ID udev property - if that is set then we disable the firmware + * pad and pen reports (by making them vendor collections that are ignored). + * If that property is not set we fix all hidraw nodes so the tablet works in + * either mode though the drawback is that the device will show up twice if + * you bind it to all event nodes + * + * Default report descriptor for the first exposed hidraw node: + * + * # HUION Huion Keydial_K20 + * # Report descriptor length: 18 bytes + * # 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 0xFF00) 0 + * # 0x09, 0x01, // Usage (Vendor Usage 0x01) 3 + * # 0xa1, 0x01, // Collection (Application) 5 + * # 0x85, 0x08, // Report ID (8) 7 + * # 0x75, 0x58, // Report Size (88) 9 + * # 0x95, 0x01, // Report Count (1) 11 + * # 0x09, 0x01, // Usage (Vendor Usage 0x01) 13 + * # 0x81, 0x02, // Input (Data,Var,Abs) 15 + * # 0xc0, // End Collection 17 + * R: 18 06 00 ff 09 01 a1 01 85 08 75 58 95 01 09 01 81 02 c0 + * + * This report descriptor appears to be identical for all Huion devices. + * + * Second hidraw node is the Pad. This one sends the button events until the tablet is + * switched to raw mode, then it's mute. + * + * # HUION Huion Keydial_K20 + * # Report descriptor length: 135 bytes + * # 0x05, 0x01, // Usage Page (Generic Desktop) 0 + * # 0x09, 0x06, // Usage (Keyboard) 2 + * # 0xa1, 0x01, // Collection (Application) 4 + * # 0x85, 0x03, // Report ID (3) 6 + * # 0x05, 0x07, // Usage Page (Keyboard/Keypad) 8 + * # 0x19, 0xe0, // UsageMinimum (224) 10 + * # 0x29, 0xe7, // UsageMaximum (231) 12 + * # 0x15, 0x00, // Logical Minimum (0) 14 + * # 0x25, 0x01, // Logical Maximum (1) 16 + * # 0x75, 0x01, // Report Size (1) 18 + * # 0x95, 0x08, // Report Count (8) 20 + * # 0x81, 0x02, // Input (Data,Var,Abs) 22 + * # 0x05, 0x07, // Usage Page (Keyboard/Keypad) 24 + * # 0x19, 0x00, // UsageMinimum (0) 26 + * # 0x29, 0xff, // UsageMaximum (255) 28 + * # 0x26, 0xff, 0x00, // Logical Maximum (255) 30 + * # 0x75, 0x08, // Report Size (8) 33 + * # 0x95, 0x06, // Report Count (6) 35 + * # 0x81, 0x00, // Input (Data,Arr,Abs) 37 + * # 0xc0, // End Collection 39 + * # 0x05, 0x0c, // Usage Page (Consumer) 40 + * # 0x09, 0x01, // Usage (Consumer Control) 42 + * # 0xa1, 0x01, // Collection (Application) 44 + * # 0x85, 0x04, // Report ID (4) 46 + * # 0x05, 0x0c, // Usage Page (Consumer) 48 + * # 0x19, 0x00, // UsageMinimum (0) 50 + * # 0x2a, 0x80, 0x03, // UsageMaximum (896) 52 + * # 0x15, 0x00, // Logical Minimum (0) 55 + * # 0x26, 0x80, 0x03, // Logical Maximum (896) 57 + * # 0x75, 0x10, // Report Size (16) 60 + * # 0x95, 0x01, // Report Count (1) 62 + * # 0x81, 0x00, // Input (Data,Arr,Abs) 64 + * # 0xc0, // End Collection 66 + * # 0x05, 0x01, // Usage Page (Generic Desktop) 67 + * # 0x09, 0x02, // Usage (Mouse) 69 + * # 0xa1, 0x01, // Collection (Application) 71 + * # 0x09, 0x01, // Usage (Pointer) 73 + * # 0x85, 0x05, // Report ID (5) 75 + * # 0xa1, 0x00, // Collection (Physical) 77 + * # 0x05, 0x09, // Usage Page (Button) 79 + * # 0x19, 0x01, // UsageMinimum (1) 81 + * # 0x29, 0x05, // UsageMaximum (5) 83 + * # 0x15, 0x00, // Logical Minimum (0) 85 + * # 0x25, 0x01, // Logical Maximum (1) 87 + * # 0x95, 0x05, // Report Count (5) 89 + * # 0x75, 0x01, // Report Size (1) 91 + * # 0x81, 0x02, // Input (Data,Var,Abs) 93 + * # 0x95, 0x01, // Report Count (1) 95 + * # 0x75, 0x03, // Report Size (3) 97 + * # 0x81, 0x01, // Input (Cnst,Arr,Abs) 99 + * # 0x05, 0x01, // Usage Page (Generic Desktop) 101 + * # 0x09, 0x30, // Usage (X) 103 + * # 0x09, 0x31, // Usage (Y) 105 + * # 0x16, 0x00, 0x80, // Logical Minimum (-32768) 107 + * # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 110 + * # 0x75, 0x10, // Report Size (16) 113 + * # 0x95, 0x02, // Report Count (2) 115 + * # 0x81, 0x06, // Input (Data,Var,Rel) 117 + * # 0x95, 0x01, // Report Count (1) 119 + * # 0x75, 0x08, // Report Size (8) 121 + * # 0x05, 0x01, // Usage Page (Generic Desktop) 123 + * # 0x09, 0x38, // Usage (Wheel) 125 + * # 0x15, 0x81, // Logical Minimum (-127) 127 + * # 0x25, 0x7f, // Logical Maximum (127) 129 + * # 0x81, 0x06, // Input (Data,Var,Rel) 131 + * # 0xc0, // End Collection 133 + * # 0xc0, // End Collection 134 + * R: 135 05 01 09 06 a1 01 85 03 05 07 19 e0 29 e7 15 00 25 01 75 01 95 08 81 02 05 07 19 00 29 ff 26 ff 00 75 08 95 06 81 00 c0 05 0c 09 01 a1 01 85 04 05 0c 19 00 2a 80 03 15 00 26 80 03 75 10 95 01 81 00 c0 05 01 09 02 a1 01 09 01 85 05 a1 00 05 09 19 01 29 05 15 00 25 01 95 05 75 01 81 02 95 01 75 03 81 01 05 01 09 30 09 31 16 00 80 26 ff 7f 7510 95 02 81 06 95 01 75 08 05 01 09 38 15 81 25 7f 81 06 c0 c0 + * + * Third hidraw node is a multi-axis controller which sends the dial events + * and the button inside the dial. If the tablet is switched to raw mode it is mute. + * + * # HUION Huion Keydial_K20 + * # Report descriptor length: 108 bytes + * # 0x05, 0x01, // Usage Page (Generic Desktop) 0 + * # 0x09, 0x0e, // Usage (System Multi-Axis Controller) 2 + * # 0xa1, 0x01, // Collection (Application) 4 + * # 0x85, 0x11, // Report ID (17) 6 + * # 0x05, 0x0d, // Usage Page (Digitizers) 8 + * # 0x09, 0x21, // Usage (Puck) 10 + * # 0xa1, 0x02, // Collection (Logical) 12 + * # 0x15, 0x00, // Logical Minimum (0) 14 + * # 0x25, 0x01, // Logical Maximum (1) 16 + * # 0x75, 0x01, // Report Size (1) 18 + * # 0x95, 0x01, // Report Count (1) 20 + * # 0xa1, 0x00, // Collection (Physical) 22 + * # 0x05, 0x09, // Usage Page (Button) 24 + * # 0x09, 0x01, // Usage (Button 1) 26 + * # 0x81, 0x02, // Input (Data,Var,Abs) 28 + * # 0x05, 0x0d, // Usage Page (Digitizers) 30 + * # 0x09, 0x33, // Usage (Touch) 32 + * # 0x81, 0x02, // Input (Data,Var,Abs) 34 + * # 0x95, 0x06, // Report Count (6) 36 + * # 0x81, 0x03, // Input (Cnst,Var,Abs) 38 + * # 0xa1, 0x02, // Collection (Logical) 40 + * # 0x05, 0x01, // Usage Page (Generic Desktop) 42 + * # 0x09, 0x37, // Usage (Dial) 44 + * # 0x16, 0x00, 0x80, // Logical Minimum (-32768) 46 + * # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 49 + * # 0x75, 0x10, // Report Size (16) 52 + * # 0x95, 0x01, // Report Count (1) 54 + * # 0x81, 0x06, // Input (Data,Var,Rel) 56 + * # 0x35, 0x00, // Physical Minimum (0) 58 + * # 0x46, 0x10, 0x0e, // Physical Maximum (3600) 60 + * # 0x15, 0x00, // Logical Minimum (0) 63 + * # 0x26, 0x10, 0x0e, // Logical Maximum (3600) 65 + * # 0x09, 0x48, // Usage (Resolution Multiplier) 68 + * # 0xb1, 0x02, // Feature (Data,Var,Abs) 70 + * # 0x45, 0x00, // Physical Maximum (0) 72 + * # 0xc0, // End Collection 74 + * # 0x75, 0x08, // Report Size (8) 75 + * # 0x95, 0x01, // Report Count (1) 77 + * # 0x81, 0x01, // Input (Cnst,Arr,Abs) 79 + * # 0x75, 0x08, // Report Size (8) 81 + * # 0x95, 0x01, // Report Count (1) 83 + * # 0x81, 0x01, // Input (Cnst,Arr,Abs) 85 + * # 0x75, 0x08, // Report Size (8) 87 + * # 0x95, 0x01, // Report Count (1) 89 + * # 0x81, 0x01, // Input (Cnst,Arr,Abs) 91 + * # 0x75, 0x08, // Report Size (8) 93 + * # 0x95, 0x01, // Report Count (1) 95 + * # 0x81, 0x01, // Input (Cnst,Arr,Abs) 97 + * # 0x75, 0x08, // Report Size (8) 99 + * # 0x95, 0x01, // Report Count (1) 101 + * # 0x81, 0x01, // Input (Cnst,Arr,Abs) 103 + * # 0xc0, // End Collection 105 + * # 0xc0, // End Collection 106 + * # 0xc0, // End Collection 107 + * R: 108 05 01 09 0e a1 01 85 11 05 0d 09 21 a1 02 15 00 25 01 75 01 95 01 a1 00 05 09 09 01 81 02 05 0d 09 33 81 02 95 06 81 03 a1 02 05 01 09 37 16 00 80 26 ff 7f 75 10 95 01 81 06 35 00 46 10 0e 15 00 26 10 0e 09 48 b1 02 45 00 c0 75 08 95 01 81 01 75 08 95 01 81 01 75 08 95 01 81 01 75 08 95 01 81 01 75 08 95 01 81 01 c0 c0 c0 + * + */ + +#define PAD_REPORT_DESCRIPTOR_LENGTH 135 +#define PUCK_REPORT_DESCRIPTOR_LENGTH 108 +#define VENDOR_REPORT_DESCRIPTOR_LENGTH 18 +#define PAD_KBD_REPORT_ID 3 +#define PAD_CC_REPORT_ID 3 // never sends events +#define PAD_MOUSE_REPORT_ID 4 // never sends events +#define PUCK_REPORT_ID 17 +#define VENDOR_REPORT_ID 8 +#define PAD_KBD_REPORT_LENGTH 8 +#define PAD_CC_REPORT_LENGTH 3 +#define PAD_MOUSE_REPORT_LENGTH 7 +#define PUCK_REPORT_LENGTH 9 +#define VENDOR_REPORT_LENGTH 12 + +__u32 last_button_state; + +static const __u8 disabled_rdesc_puck[] = { + FixedSizeVendorReport(PUCK_REPORT_LENGTH) +}; + +static const __u8 disabled_rdesc_pad[] = { + FixedSizeVendorReport(PAD_KBD_REPORT_LENGTH) + FixedSizeVendorReport(PAD_CC_REPORT_LENGTH) + FixedSizeVendorReport(PAD_MOUSE_REPORT_LENGTH) +}; + +static const __u8 fixed_rdesc_vendor[] = { + UsagePage_GenericDesktop + Usage_GD_Keypad + CollectionApplication( + // Byte 0 + // We send our pad events on the vendor report id because why not + ReportId(VENDOR_REPORT_ID) + UsagePage_Digitizers + Usage_Dig_TabletFunctionKeys + CollectionPhysical( + // Byte 1 is a button so we look like a tablet + Usage_Dig_BarrelSwitch // BTN_STYLUS, needed so we get to be a tablet pad + ReportCount(1) + ReportSize(1) + Input(Var|Abs) + ReportCount(7) // Padding + Input(Const) + // Bytes 2/3 - x/y just exist so we get to be a tablet pad + UsagePage_GenericDesktop + Usage_GD_X + Usage_GD_Y + LogicalMinimum_i8(0x0) + LogicalMaximum_i8(0x1) + ReportCount(2) + ReportSize(8) + Input(Var|Abs) + // Bytes 4-7 are the button state for 19 buttons + pad out to u32 + // We send the first 10 buttons as buttons 1-10 which is BTN_0 -> BTN_9 + UsagePage_Button + UsageMinimum_i8(1) + UsageMaximum_i8(10) + LogicalMinimum_i8(0x0) + LogicalMaximum_i8(0x1) + ReportCount(10) + ReportSize(1) + Input(Var|Abs) + // We send the other 9 buttons as buttons 0x31 and above -> BTN_A - BTN_TL2 + UsageMinimum_i8(0x31) + UsageMaximum_i8(0x3a) + ReportCount(9) + ReportSize(1) + Input(Var|Abs) + ReportCount(13) + ReportSize(1) + Input(Const) // padding + // Byte 6 is the wheel + UsagePage_GenericDesktop + Usage_GD_Wheel + LogicalMinimum_i8(-1) + LogicalMaximum_i8(1) + ReportCount(1) + ReportSize(8) + Input(Var|Rel) + ) + // Make sure we match our original report length + FixedSizeVendorReport(VENDOR_REPORT_LENGTH) + ) +}; + +/* Identical to fixed_rdesc_pad but with different FixedSizeVendorReport */ +static const __u8 fixed_rdesc_pad[] = { + UsagePage_GenericDesktop + Usage_GD_Keypad + CollectionApplication( + // Byte 0 + // We send our pad events on the vendor report id because why not + ReportId(VENDOR_REPORT_ID) + UsagePage_Digitizers + Usage_Dig_TabletFunctionKeys + CollectionPhysical( + // Byte 1 is a button so we look like a tablet + Usage_Dig_BarrelSwitch // BTN_STYLUS, needed so we get to be a tablet pad + ReportCount(1) + ReportSize(1) + Input(Var|Abs) + ReportCount(7) // Padding + Input(Const) + // Bytes 2/3 - x/y just exist so we get to be a tablet pad + UsagePage_GenericDesktop + Usage_GD_X + Usage_GD_Y + LogicalMinimum_i8(0x0) + LogicalMaximum_i8(0x1) + ReportCount(2) + ReportSize(8) + Input(Var|Abs) + // Bytes 4-7 are the button state for 19 buttons + pad out to u32 + // We send the first 10 buttons as buttons 1-10 which is BTN_0 -> BTN_9 + UsagePage_Button + UsageMinimum_i8(1) + UsageMaximum_i8(10) + LogicalMinimum_i8(0x0) + LogicalMaximum_i8(0x1) + ReportCount(10) + ReportSize(1) + Input(Var|Abs) + // We send the other 9 buttons as buttons 0x31 and above -> BTN_A - BTN_TL2 + UsageMinimum_i8(0x31) + UsageMaximum_i8(0x3a) + ReportCount(9) + ReportSize(1) + Input(Var|Abs) + ReportCount(13) + ReportSize(1) + Input(Const) // padding + // Byte 6 is the wheel + UsagePage_GenericDesktop + Usage_GD_Wheel + LogicalMinimum_i8(-1) + LogicalMaximum_i8(1) + ReportCount(1) + ReportSize(8) + Input(Var|Rel) + ) + // Make sure we match our original report lengths + FixedSizeVendorReport(PAD_KBD_REPORT_LENGTH) + FixedSizeVendorReport(PAD_CC_REPORT_LENGTH) + FixedSizeVendorReport(PAD_MOUSE_REPORT_LENGTH) + ) +}; + +SEC(HID_BPF_RDESC_FIXUP) +int BPF_PROG(k20_fix_rdesc, struct hid_bpf_ctx *hctx) +{ + __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, HID_MAX_DESCRIPTOR_SIZE /* size */); + __s32 rdesc_size = hctx->size; + __u8 have_fw_id; + + if (!data) + return 0; /* EPERM check */ + + /* If we have a firmware ID and it matches our expected prefix, we + * disable the default pad/puck nodes. They won't send events + * but cause duplicate devices. + */ + have_fw_id = __builtin_memcmp(UDEV_PROP_HUION_FIRMWARE_ID, + EXPECTED_FIRMWARE_ID, + sizeof(EXPECTED_FIRMWARE_ID) - 1) == 0; + if (rdesc_size == PAD_REPORT_DESCRIPTOR_LENGTH) { + if (have_fw_id) { + __builtin_memcpy(data, disabled_rdesc_pad, sizeof(disabled_rdesc_pad)); + return sizeof(disabled_rdesc_pad); + } else { + __builtin_memcpy(data, fixed_rdesc_pad, sizeof(fixed_rdesc_pad)); + return sizeof(fixed_rdesc_pad); + + } + } + if (rdesc_size == PUCK_REPORT_DESCRIPTOR_LENGTH) { + if (have_fw_id) { + __builtin_memcpy(data, disabled_rdesc_puck, sizeof(disabled_rdesc_puck)); + return sizeof(disabled_rdesc_puck); + } + } + /* Always fix the vendor mode so the tablet will work even if nothing sets + * the udev property (e.g. huion-switcher run manually) + */ + if (rdesc_size == VENDOR_REPORT_DESCRIPTOR_LENGTH) { + __builtin_memcpy(data, fixed_rdesc_vendor, sizeof(fixed_rdesc_vendor)); + return sizeof(fixed_rdesc_vendor); + + } + return 0; +} + +SEC(HID_BPF_DEVICE_EVENT) +int BPF_PROG(k20_fix_events, struct hid_bpf_ctx *hctx) +{ + __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 10 /* size */); + + if (!data) + return 0; /* EPERM check */ + + /* Only sent if tablet is in raw mode */ + if (data[0] == VENDOR_REPORT_ID) { + /* See fixed_rdesc_pad */ + struct pad_report { + __u8 report_id; + __u8 btn_stylus:1; + __u8 pad:7; + __u8 x; + __u8 y; + __u32 buttons; + __u8 wheel; + } __attribute__((packed)) *pad_report; + + __u8 wheel = 0; + + /* Wheel report */ + if (data[1] == 0xf1) { + if (data[5] == 2) + wheel = 0xff; + else + wheel = data[5]; + } else { + /* data[4..6] are the buttons, mapped correctly */ + last_button_state = data[4] | (data[5] << 8) | (data[6] << 16); + wheel = 0; // wheel + } + + pad_report = (struct pad_report *)data; + pad_report->report_id = VENDOR_REPORT_ID; + pad_report->btn_stylus = 0; + pad_report->x = 0; + pad_report->y = 0; + pad_report->buttons = last_button_state; + pad_report->wheel = wheel; + + return sizeof(struct pad_report); + } + + if (data[0] == PAD_KBD_REPORT_ID) { + const __u8 button_mapping[] = { + 0x0e, /* Button 1: K */ + 0x0a, /* Button 2: G */ + 0x0f, /* Button 3: L */ + 0x4c, /* Button 4: Delete */ + 0x0c, /* Button 5: I */ + 0x07, /* Button 6: D */ + 0x05, /* Button 7: B */ + 0x08, /* Button 8: E */ + 0x16, /* Button 9: S */ + 0x1d, /* Button 10: Z */ + 0x06, /* Button 11: C */ + 0x19, /* Button 12: V */ + 0xff, /* Button 13: LeftControl */ + 0xff, /* Button 14: LeftAlt */ + 0xff, /* Button 15: LeftShift */ + 0x28, /* Button 16: Return Enter */ + 0x2c, /* Button 17: Spacebar */ + 0x11, /* Button 18: N */ + }; + /* See fixed_rdesc_pad */ + struct pad_report { + __u8 report_id; + __u8 btn_stylus:1; + __u8 pad:7; + __u8 x; + __u8 y; + __u32 buttons; + __u8 wheel; + } __attribute__((packed)) *pad_report; + int i, b; + __u8 modifiers = data[1]; + __u32 buttons = 0; + + if (modifiers & 0x01) { /* Control */ + buttons |= BIT(12); + } + if (modifiers & 0x02) { /* Shift */ + buttons |= BIT(14); + } + if (modifiers & 0x04) { /* Alt */ + buttons |= BIT(13); + } + + for (i = 2; i < PAD_KBD_REPORT_LENGTH; i++) { + if (!data[i]) + break; + + for (b = 0; b < ARRAY_SIZE(button_mapping); b++) { + if (data[i] == button_mapping[b]) { + buttons |= BIT(b); + break; + } + } + data[i] = 0; + } + + pad_report = (struct pad_report *)data; + pad_report->report_id = VENDOR_REPORT_ID; + pad_report->btn_stylus = 0; + pad_report->x = 0; + pad_report->y = 0; + pad_report->buttons = buttons; + // The wheel happens on a different hidraw node but its + // values are unreliable (as is the button inside the wheel). + // So the wheel is simply always zero, if you want the wheel + // to work reliably, use the tablet mode. + pad_report->wheel = 0; + + return sizeof(struct pad_report); + } + + return 0; +} + +HID_BPF_OPS(keydial_k20) = { + .hid_device_event = (void *)k20_fix_events, + .hid_rdesc_fixup = (void *)k20_fix_rdesc, +}; + +SEC("syscall") +int probe(struct hid_bpf_probe_args *ctx) +{ + switch (ctx->rdesc_size) { + case PAD_REPORT_DESCRIPTOR_LENGTH: + case PUCK_REPORT_DESCRIPTOR_LENGTH: + case VENDOR_REPORT_DESCRIPTOR_LENGTH: + ctx->retval = 0; + break; + default: + ctx->retval = -EINVAL; + } + + return 0; +} + +char _license[] SEC("license") = "GPL"; From patchwork Fri Feb 7 13:55:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 863690 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 879D41F4169; Fri, 7 Feb 2025 13:56:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936588; cv=none; b=dGoJ877Eq8sWOkY84HIwbtR/QDMEGg+1ky6/LLE0sgoTxZkmZbDjlUDb/gFkIxn6o5J6ujmg5R7vhXSlZaf6i9MiTlM5YVaSWOe2TH2/YBjtdAM3I+pxnzoCZXTarxoS7vyL37cld3V6J2n5d2hrza9jQvAnk/M1KrChDvUxJnE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936588; c=relaxed/simple; bh=hhGOUjq9gKw2uCegb4idyIFAtU1Ni0yyB2zWQs1v3Y8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NlNWDR8UJ2RqO1S7B83Ujf6VglJzmjtMcwDS334WXmD1Q51XWdOqV49lsxBiE3A/ND5tZHs1hfgT0ZfVf1WPlhftufVLF1OgDVzw7M637mOcSdq09YwwItxfHTOJJ39M78beTg++lcdbnTVI7HZlmsLegdkbpNAirbaNDrjRfOk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RZFrdarb; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RZFrdarb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6FA3C4CEE2; Fri, 7 Feb 2025 13:56:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738936588; bh=hhGOUjq9gKw2uCegb4idyIFAtU1Ni0yyB2zWQs1v3Y8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=RZFrdarbyvO0L9rMvxiwyPGS4v+n/lJS8ykZgaw/awxFrYuaovegCHROQjWJ8kJ1e 36msLfD5w3cnsUFTPylSLQJlcdIRdP2aZj9fn4N8/WP5No2CQKPSGvTG3bgym40E+x vtv49bYimXArE8YOO2vHjC08x78rD3yt85aNLPt2wRS3PmvPuKPNryo/zO0Vs8149t 0Rh6Gif1s/W9t8i3HdiFP3TWl74ZfUhluYDwSQDTGqQCwTh+vCyT4Jn3e8gTSMQ0AE o6ckD0pele5BOkyGXI8JNm/+OS6E+zfY2GkOllu+PFRoogCtqcpJoYwmqE0OKokdfL 6cBFj6zE0PPPA== From: Benjamin Tissoires Date: Fri, 07 Feb 2025 14:55:58 +0100 Subject: [PATCH 2/7] HID: bpf: Suppress bogus F13 trigger on Sirius keyboard full fan shortcut Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250207-bpf-import-2025-02-07-v1-2-6048fdd5a206@kernel.org> References: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> In-Reply-To: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> To: Jiri Kosina , Peter Hutterer Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Benjamin Tissoires , Werner Sembach X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738936583; l=2509; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=hhGOUjq9gKw2uCegb4idyIFAtU1Ni0yyB2zWQs1v3Y8=; b=OMGOyuA40LStVM6Dcy7n7hhHVPnYVbYuJ3HPSNz7vrC7XPgVG8jhhUnoY1QCqeNBK+Q8mYpxT kt8YQyb2Yt8Cj9U9p8Av3G7Rc7JEewsMG2Jj9Tn2EIIsVFR41clMh+C X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= The TUXEDO Sirius 16 Gen1 and the TUXEDO Sirius 16 Gen2 Notebooks have an additional "fan" key next to F12. Pressing it alone sends a F14 key press which can be bound by user space. Pressing it while holding the FN key triggers two things: - The EC firmware locks the fan speed of the internal fans at 100% - F13 key press is registered which by default is already bound in xkb and desktop environments (e.g. in KDE Plasma it launches system settings) To avoid this unexpected double duty of the FN shortcut, this bpf program suppresses the F13 key press. Signed-off-by: Werner Sembach Link: https://gitlab.freedesktop.org/libevdev/udev-hid-bpf/-/merge_requests/166 Signed-off-by: Benjamin Tissoires --- .../progs/TUXEDO__Sirius-16-Gen1-and-Gen2.bpf.c | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/drivers/hid/bpf/progs/TUXEDO__Sirius-16-Gen1-and-Gen2.bpf.c b/drivers/hid/bpf/progs/TUXEDO__Sirius-16-Gen1-and-Gen2.bpf.c new file mode 100644 index 0000000000000000000000000000000000000000..a123003fb5fdd0a27bc204b9b393da52b0956cfa --- /dev/null +++ b/drivers/hid/bpf/progs/TUXEDO__Sirius-16-Gen1-and-Gen2.bpf.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Copyright (c) 2025 TUXEDO Computers GmbH + */ + +#include "vmlinux.h" +#include "hid_bpf.h" +#include "hid_bpf_helpers.h" +#include + +HID_BPF_CONFIG( + HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, 0x048D, 0x8910) +); + +SEC(HID_BPF_DEVICE_EVENT) +int BPF_PROG(ignore_key_fix_event, struct hid_bpf_ctx *hid_ctx) +{ + const int expected_length = 37; + const int expected_report_id = 1; + __u8 *data; + int i; + + if (hid_ctx->size < expected_length) + return 0; + + data = hid_bpf_get_data(hid_ctx, 0, expected_length); + if (!data || data[0] != expected_report_id) + return 0; + + // Zero out F13 (HID usage ID: 0x68) key press. + // The first 6 parallel key presses (excluding modifier keys) are + // encoded in an array containing usage IDs. + for (i = 3; i < 9; ++i) + if (data[i] == 0x68) + data[i] = 0x00; + // Additional parallel key presses starting with the 7th (excluding + // modifier keys) are encoded as a bit flag with the offset being + // the usage ID. + data[22] &= 0xfe; + + return 0; +} + +HID_BPF_OPS(ignore_button) = { + .hid_device_event = (void *)ignore_key_fix_event, +}; + +char _license[] SEC("license") = "GPL"; From patchwork Fri Feb 7 13:55:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 863133 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFB661F8EF1; Fri, 7 Feb 2025 13:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936589; cv=none; b=kuPOGc8N5ORB1Qvz6Cnpw07u2RpdzInFhqjX3tY7/fusuwSumNppwnom4CkoFqGKv0CqY9M/pqXkZxkSnOpN0tsvqFJlil1JXsrV1OG0+aco9Ks0+98I8zy7/9DuL+BviH+n4E9VQdYfIzfHXjHhkVC9SUegw19Sk65nQoL8RKo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936589; c=relaxed/simple; bh=CRzZ9NbV37dT/3RZjq6vQB+PPSjf3/hWQ8H0AXaimQk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nSn9Mt0uPUf8E+u0GyhlDXIYijml0F/+HNBw4Nnw2w7H/qs9GJFvX2aNYjl8/9YFJRGOkuHKCr7iOtwg0Ko1I8MnCs6Mtv8pnbySBYTOHpKT1mocG09Dt5edF4s/G84662PxWVp5YEKWzSxKPuEBvHYmEgV4aYD+DCh0RYiPoKc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OH6hIKeT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OH6hIKeT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85509C4CED1; Fri, 7 Feb 2025 13:56:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738936589; bh=CRzZ9NbV37dT/3RZjq6vQB+PPSjf3/hWQ8H0AXaimQk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=OH6hIKeTGz+LhHCUsFvf4Il5PqDzdJ+C5OwWWzng7Z7JgodY5PC66BpAEfQ090OMP zdE54tJ79TtmD5HKOvqRBFtEUVjCvnQg52oMZK0IpJKbEpCLB2o3N8DY5k35h1tvii vmiCof6wepQhUQPZiYo0hP3yJFOyI2wNNxuMRSpD4GosuxBH74AaVC33NacdMd5WTb QNsvYi/6+0TJ82rHzZGfnHvpeeHKiNI7s43Cdo0/cBzlrFKTUkyutyiSqNS74fir0C kNzSjOrlZQr2u87CZsVkS3Vx87iBqnCvbfSuMItMta377Z3KHeXQUXcLjvMwxb1Oql 8Z7c8PY182bhg== From: Benjamin Tissoires Date: Fri, 07 Feb 2025 14:55:59 +0100 Subject: [PATCH 3/7] HID: bpf: Added updated Kamvas Pro 19 descriptor Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250207-bpf-import-2025-02-07-v1-3-6048fdd5a206@kernel.org> References: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> In-Reply-To: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> To: Jiri Kosina , Peter Hutterer Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Benjamin Tissoires , Aki Van Ness X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738936583; l=7513; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=CRzZ9NbV37dT/3RZjq6vQB+PPSjf3/hWQ8H0AXaimQk=; b=PJVHA9Fssrb3ErZ+wLWUMs89YnXg5CcB3V6MdIHVlBwzZuKh6cGsdaC6qv1xqi9cFm2qAIm+w yJ2jXRpRUIABc/m8DhUec0kVYb/xDr2g018Vhh/HuU69IZUomeUmg53 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= This adds an updated HID descriptor for the Huion Kamvas Pro 19 that is present on newer firmware revisions, while also trying to keep compat with the older versions. Link: https://gitlab.freedesktop.org/libevdev/udev-hid-bpf/-/merge_requests/164 Signed-off-by: Aki Van Ness Signed-off-by: Benjamin Tissoires --- drivers/hid/bpf/progs/Huion__Kamvas-Pro-19.bpf.c | 75 ++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/drivers/hid/bpf/progs/Huion__Kamvas-Pro-19.bpf.c b/drivers/hid/bpf/progs/Huion__Kamvas-Pro-19.bpf.c index a4a4f324aedd5658bb9b5114b2513d37ed719c0f..489cb4fcc2cd2702e4d996ad9435ba0ca454b6d9 100644 --- a/drivers/hid/bpf/progs/Huion__Kamvas-Pro-19.bpf.c +++ b/drivers/hid/bpf/progs/Huion__Kamvas-Pro-19.bpf.c @@ -41,7 +41,7 @@ static const __u8 fixed_rdesc[] = { 0x15, 0x00, // Logical Minimum (0) 22 0x25, 0x01, // Logical Maximum (1) 24 0x75, 0x01, // Report Size (1) 26 - 0x95, 0x05, // Report Count (5) 28 /* changed (was 5) */ + 0x95, 0x05, // Report Count (5) 28 /* changed (was 6) */ 0x81, 0x02, // Input (Data,Var,Abs) 30 0x05, 0x09, // Usage Page (Button) /* inserted */ 0x09, 0x4a, // Usage (0x4a) /* inserted to be translated as input usage 0x149: BTN_STYLUS3 */ @@ -189,8 +189,68 @@ static const __u8 fixed_rdesc[] = { 0x96, 0x00, 0x01, // Report Count (256) 322 0xb1, 0x02, // Feature (Data,Var,Abs) 325 0xc0, // End Collection 327 + /* New in Firmware Version: HUION_M220_240524 */ + 0x05, 0x01, // Usage Page (Generic Desktop) 328 + 0x09, 0x01, // Usage (Pointer) 330 + 0xa1, 0x01, // Collection (Application) 332 + 0x09, 0x01, // Usage (Pointer) 334 + 0xa1, 0x00, // Collection (Physical) 336 + 0x05, 0x09, // Usage Page (Button) 338 + 0x19, 0x01, // UsageMinimum (1) 340 + 0x29, 0x03, // UsageMaximum (3) 342 + 0x15, 0x00, // Logical Minimum (0) 344 + 0x25, 0x01, // Logical Maximum (1) 346 + 0x85, 0x02, // Report ID (2) 348 + 0x95, 0x03, // Report Count (3) 350 + 0x75, 0x01, // Report Size (1) 352 + 0x81, 0x02, // Input (Data,Var,Abs) 354 + 0x95, 0x01, // Report Count (1) 356 + 0x75, 0x05, // Report Size (5) 358 + 0x81, 0x01, // Input (Cnst,Arr,Abs) 360 + 0x05, 0x01, // Usage Page (Generic Desktop) 362 + 0x09, 0x30, // Usage (X) 364 + 0x09, 0x31, // Usage (Y) 366 + 0x15, 0x81, // Logical Minimum (-127) 368 + 0x25, 0x7f, // Logical Maximum (127) 370 + 0x75, 0x08, // Report Size (8) 372 + 0x95, 0x02, // Report Count (2) 374 + 0x81, 0x06, // Input (Data,Var,Rel) 376 + 0x95, 0x04, // Report Count (4) 378 + 0x75, 0x08, // Report Size (8) 380 + 0x81, 0x01, // Input (Cnst,Arr,Abs) 382 + 0xc0, // End Collection 384 + 0xc0, // End Collection 385 + 0x05, 0x0d, // Usage Page (Digitizers) 386 + 0x09, 0x05, // Usage (Touch Pad) 388 + 0xa1, 0x01, // Collection (Application) 390 + 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page FF00) 392 + 0x09, 0x0c, // Usage (Vendor Usage 0x0c) 395 + 0x15, 0x00, // Logical Minimum (0) 397 + 0x26, 0xff, 0x00, // Logical Maximum (255) 399 + 0x75, 0x08, // Report Size (8) 402 + 0x95, 0x10, // Report Count (16) 404 + 0x85, 0x3f, // Report ID (63) 406 + 0x81, 0x22, // Input (Data,Var,Abs,NoPref) 408 + 0xc0, // End Collection 410 + 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page FF00) 411 + 0x09, 0x0c, // Usage (Vendor Usage 0x0c) 414 + 0xa1, 0x01, // Collection (Application) 416 + 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page FF00) 418 + 0x09, 0x0c, // Usage (Vendor Usage 0x0c) 421 + 0x15, 0x00, // Logical Minimum (0) 423 + 0x26, 0xff, 0x00, // Logical Maximum (255) 425 + 0x85, 0x44, // Report ID (68) 428 + 0x75, 0x08, // Report Size (8) 430 + 0x96, 0x6b, 0x05, // Report Count (1387) 432 + 0x81, 0x00, // Input (Data,Arr,Abs) 435 + 0xc0, // End Collection 437 }; +#define PRE_240524_RDESC_SIZE 328 +#define PRE_240524_RDESC_FIXED_SIZE 338 /* The original bits of the descriptor */ +#define FW_240524_RDESC_SIZE 438 +#define FW_240524_RDESC_FIXED_SIZE sizeof(fixed_rdesc) + SEC(HID_BPF_RDESC_FIXUP) int BPF_PROG(hid_fix_rdesc_huion_kamvas_pro_19, struct hid_bpf_ctx *hctx) { @@ -199,9 +259,14 @@ int BPF_PROG(hid_fix_rdesc_huion_kamvas_pro_19, struct hid_bpf_ctx *hctx) if (!data) return 0; /* EPERM check */ - __builtin_memcpy(data, fixed_rdesc, sizeof(fixed_rdesc)); + if (hctx->size == FW_240524_RDESC_SIZE) { + __builtin_memcpy(data, fixed_rdesc, FW_240524_RDESC_FIXED_SIZE); + return sizeof(fixed_rdesc); + } + + __builtin_memcpy(data, fixed_rdesc, PRE_240524_RDESC_FIXED_SIZE); - return sizeof(fixed_rdesc); + return PRE_240524_RDESC_FIXED_SIZE; } /* @@ -263,7 +328,9 @@ HID_BPF_OPS(huion_Kamvas_pro_19) = { SEC("syscall") int probe(struct hid_bpf_probe_args *ctx) { - ctx->retval = ctx->rdesc_size != 328; + + ctx->retval = !((ctx->rdesc_size == PRE_240524_RDESC_SIZE) || + (ctx->rdesc_size == FW_240524_RDESC_SIZE)); if (ctx->retval) ctx->retval = -EINVAL; From patchwork Fri Feb 7 13:56:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 863689 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B691F1FA14B; Fri, 7 Feb 2025 13:56:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936591; cv=none; b=dLEbT5lZm/DBMLY4EcM+MC+4cL45IQMQcNJcHnpXrwuUJ+q1TvDcvEUSXSsdJRkqwjrSAqJDieeLI/XpxG/ive+Gdlfc1xt9GCP386PDwY7/SEcZDy3hDZqlAuvJQZ5EXu4aGKxzR71+VWcyC9TuVeD11GZKvsgTljlqmURiaNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936591; c=relaxed/simple; bh=WgOgL1KWixuEaC0aIvIRsM65ZIULA5fzTZc224CQK+g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fivfhISbY4mXR8xZ+0eQB5dvQEbrBvPLG4IyVoQ7Jds9TLJqtQLlMHtib14KW8tq2nmjUY1tCKzwaIDKMDOaNQYKP32xmtOtcuREvjvKQSpy7aUmH7yw2OzcTIdVVTvzCm5T0iCxfMTL6enQWmKGUhYi2aOsuXmqmjJ5sq93je4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m2+E5uDZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="m2+E5uDZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 23CE8C4CEDF; Fri, 7 Feb 2025 13:56:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738936591; bh=WgOgL1KWixuEaC0aIvIRsM65ZIULA5fzTZc224CQK+g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=m2+E5uDZJXzidEAGKyehHzwIFxWZviVy5zgyWLQU9PX5nAlF6FJ80CttApYsKX2JT 1GdGs5K94/0xM8cwlUzSAC0Ux6QO+JdgecF43DrGGRGtpEtF8hoevwHH4d8YWG9hTB fOj2eNyxTy5f01Pm5ADPMz1EzV+WL6QRVyTRXYHBJHjltYOr6n3TlQni+ZnkWCwa/y fxZRSUweHk8zNf0APVHaAA3nrWCwu2UhiYZDYpIHOVBJBPDLdknndi0du2EqKg+u3Q h29twSCKZ3eanodlxWGHMgt46ELOCBwpDzyOBfE0OAuLiZTU9asPZ4kp25Furdxeul 2ORI0jRaHqk5g== From: Benjamin Tissoires Date: Fri, 07 Feb 2025 14:56:00 +0100 Subject: [PATCH 4/7] HID: bpf: add support for the XP-Pen Artist Pro 19 (gen2) Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250207-bpf-import-2025-02-07-v1-4-6048fdd5a206@kernel.org> References: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> In-Reply-To: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> To: Jiri Kosina , Peter Hutterer Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738936583; l=4704; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=WgOgL1KWixuEaC0aIvIRsM65ZIULA5fzTZc224CQK+g=; b=8bp/lOeRWYNRyJDeMTGIDqY88Oz1OAE6EWPePn5Pqz6wYA2qeY+Ly18qrxwhpWhOPiY3gruOq tfIrXCTjBDnBPkHU9oJmmTAHFAlu5A0Zsanle1Wi+SZ6icwKSh45X6K X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= The device behaves the same than the 16" and 14" models, so let's just add support for it too. Link: https://gitlab.freedesktop.org/libevdev/udev-hid-bpf/-/merge_requests/145 Signed-off-by: Benjamin Tissoires --- drivers/hid/bpf/progs/XPPen__ArtistPro16Gen2.bpf.c | 44 ++++++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/drivers/hid/bpf/progs/XPPen__ArtistPro16Gen2.bpf.c b/drivers/hid/bpf/progs/XPPen__ArtistPro16Gen2.bpf.c index a669525691aae1c354259cb3276cc3bb406bf529..0c7e5cc5dc7e30bb67717e2090a002b42dee91ef 100644 --- a/drivers/hid/bpf/progs/XPPen__ArtistPro16Gen2.bpf.c +++ b/drivers/hid/bpf/progs/XPPen__ArtistPro16Gen2.bpf.c @@ -10,10 +10,12 @@ #define VID_UGEE 0x28BD /* VID is shared with SinoWealth and Glorious and prob others */ #define PID_ARTIST_PRO14_GEN2 0x095A #define PID_ARTIST_PRO16_GEN2 0x095B +#define PID_ARTIST_PRO19_GEN2 0x096A HID_BPF_CONFIG( HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_UGEE, PID_ARTIST_PRO14_GEN2), - HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_UGEE, PID_ARTIST_PRO16_GEN2) + HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_UGEE, PID_ARTIST_PRO16_GEN2), + HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_UGEE, PID_ARTIST_PRO19_GEN2) ); /* @@ -22,7 +24,7 @@ HID_BPF_CONFIG( * - when the eraser button is pressed and the stylus is touching the tablet, * the device sends Tip Switch instead of sending Eraser * - * This descriptor uses physical dimensions of the 16" device. + * This descriptor uses the physical dimensions of the 16" device. */ static const __u8 fixed_rdesc[] = { 0x05, 0x0d, // Usage Page (Digitizers) 0 @@ -100,6 +102,12 @@ int BPF_PROG(hid_fix_rdesc_xppen_artistpro16gen2, struct hid_bpf_ctx *hctx) data[62] = 0x62; data[73] = 0x1c; data[72] = 0xfd; + } else if (hctx->hid->product == PID_ARTIST_PRO19_GEN2) { + /* 19" screen reports 16.101" x 9.057" */ + data[63] = 0x3e; + data[62] = 0xe5; + data[73] = 0x23; + data[72] = 0x61; } return sizeof(fixed_rdesc); @@ -177,6 +185,27 @@ static const __u16 angle_offsets_vertical_16[128] = { 188, 186, 184, 182, 180, 178, 176, 174, 172 }; +/* 19" inch screen 16.101" x 9.057" */ +static const __u16 angle_offsets_horizontal_19[128] = { + 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 25, 27, 29, 31, 33, 35, 37, 39, 41, + 42, 44, 46, 48, 50, 51, 53, 55, 57, 58, 60, 62, 63, 65, 67, 68, 70, 71, 73, 74, 76, + 77, 79, 80, 82, 83, 84, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100, + 101, 102, 103, 104, 104, 105, 106, 106, 107, 108, 108, 109, 109, 110, 110, 111, + 111, 112, 112, 112, 112, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, + 113, 113, 112, 112, 112, 112, 111, 111, 110, 110, 109, 109, 108, 108, 107, 106, + 106, 105, 104, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 90 +}; +static const __u16 angle_offsets_vertical_19[128] = { + 0, 4, 7, 11, 14, 18, 21, 25, 28, 32, 35, 38, 42, 45, 49, 52, 56, 59, 62, 66, 69, 72, + 75, 79, 82, 85, 88, 91, 95, 98, 101, 104, 107, 110, 113, 116, 118, 121, 124, 127, + 129, 132, 135, 137, 140, 142, 145, 147, 150, 152, 154, 157, 159, 161, 163, 165, 167, + 169, 171, 173, 174, 176, 178, 179, 181, 183, 184, 185, 187, 188, 189, 190, 192, 193, + 194, 195, 195, 196, 197, 198, 198, 199, 199, 200, 200, 201, 201, 201, 201, 201, 201, + 201, 201, 201, 201, 201, 200, 200, 199, 199, 198, 198, 197, 196, 195, 195, 194, 193, + 192, 190, 189, 188, 187, 185, 184, 183, 181, 179, 178, 176, 174, 173, 171, 169, 167, + 165, 163, 161 +}; + static void compensate_coordinates_by_tilt(__u8 *data, const __u8 idx, const __s8 tilt, const __u16 (*compensation_table)[128]) { @@ -241,12 +270,19 @@ static int xppen_16_fix_angle_offset(struct hid_bpf_ctx *hctx) __s8 tilt_x = (__s8) data[8]; __s8 tilt_y = (__s8) data[9]; - if (hctx->hid->product == PID_ARTIST_PRO14_GEN2) { + switch (hctx->hid->product) { + case PID_ARTIST_PRO14_GEN2: compensate_coordinates_by_tilt(data, 2, tilt_x, &angle_offsets_horizontal_14); compensate_coordinates_by_tilt(data, 4, tilt_y, &angle_offsets_vertical_14); - } else if (hctx->hid->product == PID_ARTIST_PRO16_GEN2) { + break; + case PID_ARTIST_PRO16_GEN2: compensate_coordinates_by_tilt(data, 2, tilt_x, &angle_offsets_horizontal_16); compensate_coordinates_by_tilt(data, 4, tilt_y, &angle_offsets_vertical_16); + break; + case PID_ARTIST_PRO19_GEN2: + compensate_coordinates_by_tilt(data, 2, tilt_x, &angle_offsets_horizontal_19); + compensate_coordinates_by_tilt(data, 4, tilt_y, &angle_offsets_vertical_19); + break; } return 0; From patchwork Fri Feb 7 13:56:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 863132 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 214A01FF7B7; Fri, 7 Feb 2025 13:56:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936593; cv=none; b=Rj4d2owo+aJfGgB/OTxs06jN3kVt4/YbRGfcuX6Db6RVwwUMhbLZCmATIlQyq8FbdNJ2rvxx7Sf1qdSXGzr01XXInccYSFm3kYjw/I/Ym31mSZ5jphSrb+adA83M7VXZHhHrMZrgrETk/efW9l61lpp+hEdUAyRkRRQqCYZOq7A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936593; c=relaxed/simple; bh=rAU4Mo85vZRk/egJ4o0I2i5pn8PnEvtG5ZWu2RSw+Ao=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AgjKkb1NfuAV9hUJbDlMRvyW2iQAmJ0fkviUNmdJSNsulRaRVl4oOZ5w7xwfKpIAgsrN84VYjR3pN2SatukXVIREwz30jI+V93iS3803wciniuXMt1cbnTQSj/gvAFzrtCgLqUx9iuIj7gsoP8JEECzUtVa06Dx33JOKmRzB9pA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BJtY5uYV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BJtY5uYV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9038DC4CEE5; Fri, 7 Feb 2025 13:56:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738936592; bh=rAU4Mo85vZRk/egJ4o0I2i5pn8PnEvtG5ZWu2RSw+Ao=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BJtY5uYVieUa0MMF2kTA6ouGpTmwwWrrAg441Qw4JGMwwlDN89Y7X+aNcsQqIahJL Iz86RT1fXjbutvA56aHsa3QfDY9m+MCR3iL/WynbC0Bi4+fGj3ouGOuTQgEj2BrwJ3 p9n/ObDCBEdAa0GCTPdYok+SyFqGnceI55059EDLEWOU0e3Dc5R9ays1NapA1AKehq fa3u1Eh7eZ9wgtfVDx7X82eeyHMuYjK8Kza3Sfk+R6u1B+6hPypwSziUZcHQ5q5mp+ CPUi3md0bJnVicKO3hyb4oxbXnbDukVgi9k+QWh9T09PPfgzAZsKMWA2YF14Bsm1oI woONkoDnorOjw== From: Benjamin Tissoires Date: Fri, 07 Feb 2025 14:56:01 +0100 Subject: [PATCH 5/7] HID: bpf: import new kfunc from v6.10 & v6.11 Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250207-bpf-import-2025-02-07-v1-5-6048fdd5a206@kernel.org> References: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> In-Reply-To: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> To: Jiri Kosina , Peter Hutterer Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738936583; l=1862; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=rAU4Mo85vZRk/egJ4o0I2i5pn8PnEvtG5ZWu2RSw+Ao=; b=W8HfDg1gXZCeiE95HUx8ht2hXVF8PAuGV2JF2QhMYPuP0hPLrgeR++5jBAdvOmRXzwUXE2iPJ 1CfsM4mXwolB4kTR5Ft9nZyPQ5vVoPsMd3wsZjzWAdxSZ4UgRE7onvt X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= These kfunc are all in v6.10 except for the hid_bpf_try_input_report() which will be in v6.11. Import their definition once now so we can make use of it. Link: https://gitlab.freedesktop.org/libevdev/udev-hid-bpf/-/merge_requests/114 Signed-off-by: Benjamin Tissoires --- drivers/hid/bpf/progs/hid_bpf_helpers.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/hid/bpf/progs/hid_bpf_helpers.h b/drivers/hid/bpf/progs/hid_bpf_helpers.h index 3ba24d125a081be4b09d88be4a2afcdc6b8c5f00..bf19785a6b06734d9465547e915bf8baeec23350 100644 --- a/drivers/hid/bpf/progs/hid_bpf_helpers.h +++ b/drivers/hid/bpf/progs/hid_bpf_helpers.h @@ -19,6 +19,25 @@ extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, size_t buf__sz, enum hid_report_type type, enum hid_class_request reqtype) __ksym; +extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, + __u8 *buf, size_t buf__sz) __weak __ksym; +extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx, + enum hid_report_type type, + __u8 *data, + size_t buf__sz) __weak __ksym; +extern int hid_bpf_try_input_report(struct hid_bpf_ctx *ctx, + enum hid_report_type type, + __u8 *data, + size_t buf__sz) __weak __ksym; + +/* bpf_wq implementation */ +extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym; +extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym; +extern int bpf_wq_set_callback_impl(struct bpf_wq *wq, + int (callback_fn)(void *map, int *key, void *value), + unsigned int flags__k, void *aux__ign) __ksym; +#define bpf_wq_set_callback(wq, cb, flags) \ + bpf_wq_set_callback_impl(wq, cb, flags, NULL) #define HID_MAX_DESCRIPTOR_SIZE 4096 #define HID_IGNORE_EVENT -1 From patchwork Fri Feb 7 13:56:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 863688 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84FD7215793; Fri, 7 Feb 2025 13:56:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936594; cv=none; b=qxLAfOP/rAf9IDWVUeDeQ4RSTLs1jkWY6YKXRe66/rdETaKLaN/fL4hHTr6Pw6kysiZ1c59frq2mG/ev2W1VascVL8MG3hBb9wgy+nb+8SAbsuKkbml/DwsvU+XNiz9l00ajXa30N9LeJTKUl1GWBz4IwVY6mgHwE5ecT7SDFDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936594; c=relaxed/simple; bh=2z5Pv1tccVbX+r4QMGR+0RbXXwaAVSkEfWk/C5nSLzw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PLiPJlR1c0abRk9XzVZzbdToyKCY4IJ3etGSEbt8U1y7Yeo0DslWOmbggi6Yv3Z6XayCTyVXwPKR6Gmm4laH+9dpxSgHzj1KYIblfXu/YvLoF8Gi1x4Ucvno5EOjxFw/OBnfz4/OE3KoC7x5W0uA17wRtmVGQ22OfxtXIwx8YAo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rylEUjNp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rylEUjNp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04632C4CED1; Fri, 7 Feb 2025 13:56:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738936594; bh=2z5Pv1tccVbX+r4QMGR+0RbXXwaAVSkEfWk/C5nSLzw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rylEUjNpSzrBeD3k8J6DJYTzv6M7938tSzwe1rtPPqWNJ8Hjvok2IK8/ZcEdUnN7B brZngJ8P6gxeEh9SxuimwHM/C64yldQOWn7rqaeXKPn9wcJk1l+x8u41He+eNlK4iF suROZhcWKwKh3Z2ufL0T1Nhalp8zUtPjJR4TgW3yJ4NE5GUQjdBUZE0q1FrNT70T4H tysZTmFtUCBGwdIp67MlHQCllPXUJuflsDi0Fir4MSAEfEqvgk00i4NCOKW+qpuKyj OAErU6zaJcSD2teYgwj4/AiEahEdtngNt8FVh0gycNWk+ocVTCoItrNwyqN8ODBNPY eSLpmp3Ah1Znw== From: Benjamin Tissoires Date: Fri, 07 Feb 2025 14:56:02 +0100 Subject: [PATCH 6/7] HID: bpf: new hid_bpf_async.h common header Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250207-bpf-import-2025-02-07-v1-6-6048fdd5a206@kernel.org> References: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> In-Reply-To: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> To: Jiri Kosina , Peter Hutterer Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738936583; l=7397; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=2z5Pv1tccVbX+r4QMGR+0RbXXwaAVSkEfWk/C5nSLzw=; b=forj309clmHoyGc7NehfynB4bD3KqMKDPMTIN3yQ3tFf9Ch7GbBWMSaXBeIIKCqbkAggPY3YE aVrIx6cCCjkAj1hPDFr/QgnphlYKBHq151IzgLBXI9ktak4Si/bjiEO X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= The purpose is to simplify the use of bpf_wq to defer blocking operations in a sleepable context. Compared to a more "classic" async approach, there is no sync mechanism to wait for the async to finish. The "simple" API is the following: ``` static int HID_BPF_ASYNC(async_fun)(struct hid_bpf_ctx *hctx) { bpf_printk("%s", __fun__); return 0; } SEC("syscall") int probe(struct hid_bpf_probe_args *ctx) { ctx->retval = HID_BPF_ASYNC_INIT(async_fun); return 0; } SEC(HID_BPF_DEVICE_EVENT) int BPF_PROG(event_handler, struct hid_bpf_ctx *hctx) { /* async_fun() can be called now, it's not a sleepable * function in this example */ async_fun(hctx); /* but we can also delay the call by 10 ms */ HID_BPF_ASYNC_DELAYED_CALL(async_fun, hctx, 10); return 0; } HID_BPF_OPS(xppen_ack05_remote) = { .hid_device_event = (void *)event_handler, }; ``` Link: https://gitlab.freedesktop.org/libevdev/udev-hid-bpf/-/merge_requests/133 Signed-off-by: Benjamin Tissoires --- drivers/hid/bpf/progs/hid_bpf_async.h | 219 ++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) diff --git a/drivers/hid/bpf/progs/hid_bpf_async.h b/drivers/hid/bpf/progs/hid_bpf_async.h new file mode 100644 index 0000000000000000000000000000000000000000..9ab5854342393f38f849cb39b83ae86c8b1d73a4 --- /dev/null +++ b/drivers/hid/bpf/progs/hid_bpf_async.h @@ -0,0 +1,219 @@ +/* SPDX-License-Identifier: GPL-2.0-only + * Copyright (c) 2024 Benjamin Tissoires + */ + +#ifndef __HID_BPF_ASYNC_H__ +#define __HID_BPF_ASYNC_H__ + +#ifndef HID_BPF_ASYNC_MAX_CTX +#error "HID_BPF_ASYNC_MAX_CTX should be set to the maximum number of concurrent async functions" +#endif /* HID_BPF_ASYNC_MAX_CTX */ + +#define CLOCK_MONOTONIC 1 + +typedef int (*hid_bpf_async_callback_t)(void *map, int *key, void *value); + +enum hid_bpf_async_state { + HID_BPF_ASYNC_STATE_UNSET = 0, + HID_BPF_ASYNC_STATE_INITIALIZING, + HID_BPF_ASYNC_STATE_INITIALIZED, + HID_BPF_ASYNC_STATE_STARTING, + HID_BPF_ASYNC_STATE_RUNNING, +}; + +struct hid_bpf_async_map_elem { + struct bpf_spin_lock lock; + enum hid_bpf_async_state state; + struct bpf_timer t; + struct bpf_wq wq; + u32 hid; +}; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, HID_BPF_ASYNC_MAX_CTX); + __type(key, u32); + __type(value, struct hid_bpf_async_map_elem); +} hid_bpf_async_ctx_map SEC(".maps"); + +/** + * HID_BPF_ASYNC_CB: macro to define an async callback used in a bpf_wq + * + * The caller is responsible for allocating a key in the async map + * with hid_bpf_async_get_ctx(). + */ +#define HID_BPF_ASYNC_CB(cb) \ +cb(void *map, int *key, void *value); \ +static __always_inline int \ +____##cb(struct hid_bpf_ctx *ctx); \ +typeof(cb(0, 0, 0)) cb(void *map, int *key, void *value) \ +{ \ + struct hid_bpf_async_map_elem *e; \ + struct hid_bpf_ctx *ctx; \ + \ + e = (struct hid_bpf_async_map_elem *)value; \ + ctx = hid_bpf_allocate_context(e->hid); \ + if (!ctx) \ + return 0; /* EPERM check */ \ + \ + e->state = HID_BPF_ASYNC_STATE_RUNNING; \ + \ + ____##cb(ctx); \ + \ + e->state = HID_BPF_ASYNC_STATE_INITIALIZED; \ + hid_bpf_release_context(ctx); \ + return 0; \ +} \ +static __always_inline int \ +____##cb + +/** + * ASYNC: macro to automatically handle async callbacks contexts + * + * Needs to be used in conjunction with HID_BPF_ASYNC_INIT and HID_BPF_ASYNC_DELAYED_CALL + */ +#define HID_BPF_ASYNC_FUN(fun) \ +fun(struct hid_bpf_ctx *ctx); \ +int ____key__##fun; \ +static int ____async_init_##fun(void) \ +{ \ + ____key__##fun = hid_bpf_async_get_ctx(); \ + if (____key__##fun < 0) \ + return ____key__##fun; \ + return 0; \ +} \ +static int HID_BPF_ASYNC_CB(____##fun##_cb)(struct hid_bpf_ctx *hctx) \ +{ \ + return fun(hctx); \ +} \ +typeof(fun(0)) fun + +#define HID_BPF_ASYNC_INIT(fun) ____async_init_##fun() +#define HID_BPF_ASYNC_DELAYED_CALL(fun, ctx, delay) \ + hid_bpf_async_delayed_call(ctx, delay, ____key__##fun, ____##fun##_cb) + +/* + * internal cb for starting the delayed work callback in a workqueue. + */ +static int __start_wq_timer_cb(void *map, int *key, void *value) +{ + struct hid_bpf_async_map_elem *e = (struct hid_bpf_async_map_elem *)value; + + bpf_wq_start(&e->wq, 0); + + return 0; +} + +static int hid_bpf_async_find_empty_key(void) +{ + int i; + + bpf_for(i, 0, HID_BPF_ASYNC_MAX_CTX) { + struct hid_bpf_async_map_elem *elem; + int key = i; + + elem = bpf_map_lookup_elem(&hid_bpf_async_ctx_map, &key); + if (!elem) + return -ENOMEM; /* should never happen */ + + bpf_spin_lock(&elem->lock); + + if (elem->state == HID_BPF_ASYNC_STATE_UNSET) { + elem->state = HID_BPF_ASYNC_STATE_INITIALIZING; + bpf_spin_unlock(&elem->lock); + return i; + } + + bpf_spin_unlock(&elem->lock); + } + + return -EINVAL; +} + +static int hid_bpf_async_get_ctx(void) +{ + int key = hid_bpf_async_find_empty_key(); + struct hid_bpf_async_map_elem *elem; + int err; + + if (key < 0) + return key; + + elem = bpf_map_lookup_elem(&hid_bpf_async_ctx_map, &key); + if (!elem) + return -EINVAL; + + err = bpf_timer_init(&elem->t, &hid_bpf_async_ctx_map, CLOCK_MONOTONIC); + if (err) + return err; + + err = bpf_timer_set_callback(&elem->t, __start_wq_timer_cb); + if (err) + return err; + + err = bpf_wq_init(&elem->wq, &hid_bpf_async_ctx_map, 0); + if (err) + return err; + + elem->state = HID_BPF_ASYNC_STATE_INITIALIZED; + + return key; +} + +static inline u64 ms_to_ns(u64 milliseconds) +{ + return (u64)milliseconds * 1000UL * 1000UL; +} + +static int hid_bpf_async_delayed_call(struct hid_bpf_ctx *hctx, u64 milliseconds, int key, + hid_bpf_async_callback_t wq_cb) +{ + struct hid_bpf_async_map_elem *elem; + int err; + + elem = bpf_map_lookup_elem(&hid_bpf_async_ctx_map, &key); + if (!elem) + return -EINVAL; + + bpf_spin_lock(&elem->lock); + /* The wq must be: + * - HID_BPF_ASYNC_STATE_INITIALIZED -> it's been initialized and ready to be called + * - HID_BPF_ASYNC_STATE_RUNNING -> possible re-entry from the wq itself + */ + if (elem->state != HID_BPF_ASYNC_STATE_INITIALIZED && + elem->state != HID_BPF_ASYNC_STATE_RUNNING) { + bpf_spin_unlock(&elem->lock); + return -EINVAL; + } + elem->state = HID_BPF_ASYNC_STATE_STARTING; + bpf_spin_unlock(&elem->lock); + + elem->hid = hctx->hid->id; + + err = bpf_wq_set_callback(&elem->wq, wq_cb, 0); + if (err) + return err; + + if (milliseconds) { + /* needed for every call because a cancel might unset this */ + err = bpf_timer_set_callback(&elem->t, __start_wq_timer_cb); + if (err) + return err; + + err = bpf_timer_start(&elem->t, ms_to_ns(milliseconds), 0); + if (err) + return err; + + return 0; + } + + return bpf_wq_start(&elem->wq, 0); +} + +static inline int hid_bpf_async_call(struct hid_bpf_ctx *ctx, int key, + hid_bpf_async_callback_t wq_cb) +{ + return hid_bpf_async_delayed_call(ctx, 0, key, wq_cb); +} + +#endif /* __HID_BPF_ASYNC_H__ */ From patchwork Fri Feb 7 13:56:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 863131 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2E9E21D003; Fri, 7 Feb 2025 13:56:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936596; cv=none; b=DK2hy+FOU/m9wJqe3XXtka6Bam0lie0DQ0DMbj5RgpzDJ6L/qPk1XNzRHkZKXRIfMXcvM9Jm/8UPfe0q9i1tAa8xSnv2thQXSpztrhBfAowBRDSOwzGGQomnGn9Ifp+VWCJvq/w/xwgh0ZScolyBmDBzc1DujKHO1dEyDR43Snc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738936596; c=relaxed/simple; bh=yAVztHnNEfJNcapun2CKXaJ/53phh1wyDW0W3990WgA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XsBc8imS/97HO0Es7uuJ6C/BAQhnzeyjfdy+KuRUVvT7V7oTdlI+MpxOdoMpefMTk/Gashaubdz7Rhu3hRXo75Ix21hq+RmtN0Vh9WGvc/34FL0EHsaQ5lPOqKrkLQiXkvvyMweeQpAdpc94zvhuOSwEmHFn9VZmlmkNlHjqMtU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GpYC8HKL; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GpYC8HKL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BF05C4CEDF; Fri, 7 Feb 2025 13:56:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738936595; bh=yAVztHnNEfJNcapun2CKXaJ/53phh1wyDW0W3990WgA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GpYC8HKLyJwblQGOrCUwsSHXO3vrlf6VsnAXTVw2nUhnJCYRJYV4r7eHzw9/qlggT 6vouxehL0qqZmaoNhuZp9OKETFKOF17Vziq/eHZ3pT3mr8fzpcN8qTbOX34IBs8VUf hiC8D/Mw/6ocxbKZI+3JklEpuDSqywLhVNji1oYklDSuugRh588KMHcYMB8rQ5ILdv fs6e38hnbVo+jWzCF2DfwYKKoRI12/bnQLduN2QQUOOpFcMeWFtmiprqu7VrMxG/SS fUcxkrMrWpQ+zny01U7uhaxxuCDvmfLl2D5C85m1SxDZyEF58I/clvrSx/gsh1Opqy 07PviSBAns3Wg== From: Benjamin Tissoires Date: Fri, 07 Feb 2025 14:56:03 +0100 Subject: [PATCH 7/7] HID: bpf: add a v6.11+ compatible BPF fixup for the XPPen ACK05 remote Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250207-bpf-import-2025-02-07-v1-7-6048fdd5a206@kernel.org> References: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> In-Reply-To: <20250207-bpf-import-2025-02-07-v1-0-6048fdd5a206@kernel.org> To: Jiri Kosina , Peter Hutterer Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738936583; l=13748; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=yAVztHnNEfJNcapun2CKXaJ/53phh1wyDW0W3990WgA=; b=o9Y/k62pS2ZjMs8wBtSda1PGKSWH15MoKhwu9Fs2Oii2OBOCr80zQRb3Mvt0fTUoQ3uyI3pOo Is0ONlvVWbFDBZMYY6IinInrizW7UILJ8p0R4zi56IGhpZo1+VXm0kc X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= With v6.11+, we can actually call hid_hw_output_report() and put the device into raw mode, thus getting accurate events without being messed up. Technically we could do the same on v6.10, but given that wayland, gnome and KDE are still not capable of handling the dial, and that v6.10 is EOL, we can safely save a little bit of compilation by only allowing v6.11+. We can easily export the battery information to userspace by adding a dedicated report. However, we need to cheat on the kernel to force it not to query the battery by making the physical collection a stylus. The kernel will then only rely on the events it gets from the device. Link: https://gitlab.freedesktop.org/libevdev/udev-hid-bpf/-/merge_requests/133 Signed-off-by: Benjamin Tissoires --- drivers/hid/bpf/progs/XPPen__ACK05.bpf.c | 330 +++++++++++++++++++++++++++++++ 1 file changed, 330 insertions(+) diff --git a/drivers/hid/bpf/progs/XPPen__ACK05.bpf.c b/drivers/hid/bpf/progs/XPPen__ACK05.bpf.c new file mode 100644 index 0000000000000000000000000000000000000000..1a0aeea6a081cdcfe30032537162784c5ab80001 --- /dev/null +++ b/drivers/hid/bpf/progs/XPPen__ACK05.bpf.c @@ -0,0 +1,330 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024 Red Hat, Inc + */ + +#include "vmlinux.h" +#include "hid_bpf.h" +#include "hid_bpf_helpers.h" +#include "hid_report_helpers.h" +#include + +#define HID_BPF_ASYNC_MAX_CTX 1 +#include "hid_bpf_async.h" + +#define VID_UGEE 0x28BD +/* same PID whether connected directly or through the provided dongle: */ +#define PID_ACK05_REMOTE 0x0202 + + +HID_BPF_CONFIG( + HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_UGEE, PID_ACK05_REMOTE), +); + +/* + * By default, the pad reports the buttons through a set of key sequences. + * + * The pad reports a classic keyboard report descriptor: + * # HANVON UGEE Shortcut Remote + * Report descriptor length: 102 bytes + * 0x05, 0x01, // Usage Page (Generic Desktop) 0 + * 0x09, 0x02, // Usage (Mouse) 2 + * 0xa1, 0x01, // Collection (Application) 4 + * 0x85, 0x09, // Report ID (9) 6 + * 0x09, 0x01, // Usage (Pointer) 8 + * 0xa1, 0x00, // Collection (Physical) 10 + * 0x05, 0x09, // Usage Page (Button) 12 + * 0x19, 0x01, // UsageMinimum (1) 14 + * 0x29, 0x03, // UsageMaximum (3) 16 + * 0x15, 0x00, // Logical Minimum (0) 18 + * 0x25, 0x01, // Logical Maximum (1) 20 + * 0x95, 0x03, // Report Count (3) 22 + * 0x75, 0x01, // Report Size (1) 24 + * 0x81, 0x02, // Input (Data,Var,Abs) 26 + * 0x95, 0x05, // Report Count (5) 28 + * 0x81, 0x01, // Input (Cnst,Arr,Abs) 30 + * 0x05, 0x01, // Usage Page (Generic Desktop) 32 + * 0x09, 0x30, // Usage (X) 34 + * 0x09, 0x31, // Usage (Y) 36 + * 0x26, 0xff, 0x7f, // Logical Maximum (32767) 38 + * 0x95, 0x02, // Report Count (2) 41 + * 0x75, 0x10, // Report Size (16) 43 + * 0x81, 0x02, // Input (Data,Var,Abs) 45 + * 0x05, 0x0d, // Usage Page (Digitizers) 47 + * 0x09, 0x30, // Usage (Tip Pressure) 49 + * 0x26, 0xff, 0x07, // Logical Maximum (2047) 51 + * 0x95, 0x01, // Report Count (1) 54 + * 0x75, 0x10, // Report Size (16) 56 + * 0x81, 0x02, // Input (Data,Var,Abs) 58 + * 0xc0, // End Collection 60 + * 0xc0, // End Collection 61 + * 0x05, 0x01, // Usage Page (Generic Desktop) 62 + * 0x09, 0x06, // Usage (Keyboard) 64 + * 0xa1, 0x01, // Collection (Application) 66 + * 0x85, 0x06, // Report ID (6) 68 + * 0x05, 0x07, // Usage Page (Keyboard/Keypad) 70 + * 0x19, 0xe0, // UsageMinimum (224) 72 + * 0x29, 0xe7, // UsageMaximum (231) 74 + * 0x15, 0x00, // Logical Minimum (0) 76 + * 0x25, 0x01, // Logical Maximum (1) 78 + * 0x75, 0x01, // Report Size (1) 80 + * 0x95, 0x08, // Report Count (8) 82 + * 0x81, 0x02, // Input (Data,Var,Abs) 84 + * 0x05, 0x07, // Usage Page (Keyboard/Keypad) 86 + * 0x19, 0x00, // UsageMinimum (0) 88 + * 0x29, 0xff, // UsageMaximum (255) 90 + * 0x26, 0xff, 0x00, // Logical Maximum (255) 92 + * 0x75, 0x08, // Report Size (8) 95 + * 0x95, 0x06, // Report Count (6) 97 + * 0x81, 0x00, // Input (Data,Arr,Abs) 99 + * 0xc0, // End Collection 101 + * + * Each button gets assigned the following events: + * + * Buttons released: 06 00 00 00 00 00 00 00 + * Button 1: 06 01 12 00 00 00 00 00 -> LControl + o + * Button 2: 06 01 11 00 00 00 00 00 -> LControl + n + * Button 3: 06 00 3e 00 00 00 00 00 -> F5 + * Button 4: 06 02 00 00 00 00 00 00 -> LShift + * Button 5: 06 01 00 00 00 00 00 00 -> LControl + * Button 6: 06 04 00 00 00 00 00 00 -> LAlt + * Button 7: 06 01 16 00 00 00 00 00 -> LControl + s + * Button 8: 06 01 1d 00 00 00 00 00 -> LControl + z + * Button 9: 06 00 2c 00 00 00 00 00 -> Space + * Button 10: 06 03 1d 00 00 00 00 00 -> LControl + LShift + z + * Wheel: 06 01 57 00 00 00 00 00 -> clockwise rotation (LControl + Keypad Plus) + * Wheel: 06 01 56 00 00 00 00 00 -> counter-clockwise rotation + * (LControl + Keypad Minus) + * + * However, multiple buttons can be pressed at the same time, and when this happens, + * each button gets assigned a new slot in the Input (Data,Arr,Abs): + * + * Button 1 + 3: 06 01 12 3e 00 00 00 00 -> LControl + o + F5 + * + * When a modifier is pressed (Button 4, 5, or 6), the assigned key is set to 00: + * + * Button 5 + 7: 06 01 00 16 00 00 00 00 -> LControl + s + * + * This is mostly fine, but with Button 8 and Button 10 sharing the same + * key value ("z"), there are cases where we can not know which is which. + * + */ + +#define PAD_WIRED_DESCRIPTOR_LENGTH 102 +#define PAD_DONGLE_DESCRIPTOR_LENGTH 177 +#define STYLUS_DESCRIPTOR_LENGTH 109 +#define VENDOR_DESCRIPTOR_LENGTH 36 +#define PAD_REPORT_ID 6 +#define RAW_PAD_REPORT_ID 0xf0 +#define RAW_BATTERY_REPORT_ID 0xf2 +#define VENDOR_REPORT_ID 2 +#define PAD_REPORT_LENGTH 8 +#define VENDOR_REPORT_LENGTH 12 + +__u16 last_button_state; + +static const __u8 disabled_rdesc[] = { + // Make sure we match our original report length + FixedSizeVendorReport(VENDOR_REPORT_LENGTH) +}; + +static const __u8 fixed_rdesc_vendor[] = { + UsagePage_GenericDesktop + Usage_GD_Keypad + CollectionApplication( + // -- Byte 0 in report + ReportId(RAW_PAD_REPORT_ID) + // Byte 1 in report - same than report ID + ReportCount(1) + ReportSize(8) + Input(Const) // padding (internal report ID) + LogicalMaximum_i8(0) + LogicalMaximum_i8(1) + UsagePage_Digitizers + Usage_Dig_TabletFunctionKeys + CollectionPhysical( + // Byte 2-3 is the button state + UsagePage_Button + UsageMinimum_i8(0x01) + UsageMaximum_i8(0x0a) + LogicalMinimum_i8(0x0) + LogicalMaximum_i8(0x1) + ReportCount(10) + ReportSize(1) + Input(Var|Abs) + Usage_i8(0x31) // will be mapped as BTN_A / BTN_SOUTH + ReportCount(1) + Input(Var|Abs) + ReportCount(5) // padding + Input(Const) + // Byte 4 in report - just exists so we get to be a tablet pad + Usage_Dig_BarrelSwitch // BTN_STYLUS + ReportCount(1) + ReportSize(1) + Input(Var|Abs) + ReportCount(7) // padding + Input(Const) + // Bytes 5/6 in report - just exists so we get to be a tablet pad + UsagePage_GenericDesktop + Usage_GD_X + Usage_GD_Y + ReportCount(2) + ReportSize(8) + Input(Var|Abs) + // Byte 7 in report is the dial + Usage_GD_Wheel + LogicalMinimum_i8(-1) + LogicalMaximum_i8(1) + ReportCount(1) + ReportSize(8) + Input(Var|Rel) + ) + // -- Byte 0 in report + ReportId(RAW_BATTERY_REPORT_ID) + // Byte 1 in report - same than report ID + ReportCount(1) + ReportSize(8) + Input(Const) // padding (internal report ID) + // Byte 2 in report - always 0x01 + Input(Const) // padding (internal report ID) + UsagePage_Digitizers + /* + * We represent the device as a stylus to force the kernel to not + * directly query its battery state. Instead the kernel will rely + * only on the provided events. + */ + Usage_Dig_Stylus + CollectionPhysical( + // Byte 3 in report - battery value + UsagePage_BatterySystem + Usage_BS_AbsoluteStateOfCharge + LogicalMinimum_i8(0) + LogicalMaximum_i8(100) + ReportCount(1) + ReportSize(8) + Input(Var|Abs) + // Byte 4 in report - charging state + Usage_BS_Charging + LogicalMinimum_i8(0) + LogicalMaximum_i8(1) + ReportCount(1) + ReportSize(8) + Input(Var|Abs) + ) + ) +}; + +SEC(HID_BPF_RDESC_FIXUP) +int BPF_PROG(ack05_fix_rdesc, struct hid_bpf_ctx *hctx) +{ + __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, HID_MAX_DESCRIPTOR_SIZE /* size */); + __s32 rdesc_size = hctx->size; + + if (!data) + return 0; /* EPERM check */ + + if (rdesc_size == VENDOR_DESCRIPTOR_LENGTH) { + /* + * The vendor fixed rdesc is appended after the current one, + * to keep the output reports working. + */ + __builtin_memcpy(data + rdesc_size, fixed_rdesc_vendor, sizeof(fixed_rdesc_vendor)); + return sizeof(fixed_rdesc_vendor) + rdesc_size; + } + + hid_set_name(hctx->hid, "Disabled by HID-BPF Hanvon Ugee Shortcut Remote"); + + __builtin_memcpy(data, disabled_rdesc, sizeof(disabled_rdesc)); + return sizeof(disabled_rdesc); +} + +static int HID_BPF_ASYNC_FUN(switch_to_raw_mode)(struct hid_bpf_ctx *hid) +{ + static __u8 magic_0[32] = {0x02, 0xb0, 0x04, 0x00, 0x00}; + int err; + + /* + * The proprietary driver sends the 3 following packets after the + * above one. + * These don't seem to have any effect, so we don't send them to save + * some processing time. + * + * static __u8 magic_1[32] = {0x02, 0xb4, 0x01, 0x00, 0x01}; + * static __u8 magic_2[32] = {0x02, 0xb4, 0x01, 0x00, 0xff}; + * static __u8 magic_3[32] = {0x02, 0xb8, 0x04, 0x00, 0x00}; + */ + + err = hid_bpf_hw_output_report(hid, magic_0, sizeof(magic_0)); + if (err < 0) + return err; + + return 0; +} + +SEC(HID_BPF_DEVICE_EVENT) +int BPF_PROG(ack05_fix_events, struct hid_bpf_ctx *hctx) +{ + __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, PAD_REPORT_LENGTH); + int ret = 0; + + if (!data) + return 0; /* EPERM check */ + + if (data[0] != VENDOR_REPORT_ID) + return 0; + + /* reconnect event */ + if (data[1] == 0xf8 && data[2] == 02 && data[3] == 0x01) + HID_BPF_ASYNC_DELAYED_CALL(switch_to_raw_mode, hctx, 10); + + /* button event */ + if (data[1] == RAW_PAD_REPORT_ID) { + data[0] = data[1]; + if (data[7] == 0x02) + data[7] = 0xff; + ret = 8; + } else if (data[1] == RAW_BATTERY_REPORT_ID) { + data[0] = data[1]; + ret = 5; + } + + return ret; +} + +HID_BPF_OPS(xppen_ack05_remote) = { + .hid_device_event = (void *)ack05_fix_events, + .hid_rdesc_fixup = (void *)ack05_fix_rdesc, +}; + +SEC("syscall") +int probe(struct hid_bpf_probe_args *ctx) +{ + switch (ctx->rdesc_size) { + case PAD_WIRED_DESCRIPTOR_LENGTH: + case PAD_DONGLE_DESCRIPTOR_LENGTH: + case STYLUS_DESCRIPTOR_LENGTH: + case VENDOR_DESCRIPTOR_LENGTH: + ctx->retval = 0; + break; + default: + ctx->retval = -EINVAL; + break; + } + + if (ctx->rdesc_size == VENDOR_DESCRIPTOR_LENGTH) { + struct hid_bpf_ctx *hctx = hid_bpf_allocate_context(ctx->hid); + + if (!hctx) { + ctx->retval = -EINVAL; + return 0; + } + + ctx->retval = HID_BPF_ASYNC_INIT(switch_to_raw_mode) || + switch_to_raw_mode(hctx); + + hid_bpf_release_context(hctx); + } + + return 0; +} + +char _license[] SEC("license") = "GPL";