Path: blob/master/drivers/hid/bpf/progs/Huion__KeydialK20.bpf.c
26285 views
// SPDX-License-Identifier: GPL-2.0-only1/* Copyright (c) 2024 Red Hat, Inc2*/34#include "vmlinux.h"5#include "hid_bpf.h"6#include "hid_bpf_helpers.h"7#include "hid_report_helpers.h"8#include <bpf/bpf_tracing.h>910#define VID_HUION 0x256C11#define PID_KEYDIAL_K20 0x00691213HID_BPF_CONFIG(14HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_HUION, PID_KEYDIAL_K20),15);1617/* Filled in by udev-hid-bpf */18char UDEV_PROP_HUION_FIRMWARE_ID[64];1920/* The prefix of the firmware ID we expect for this device. The full firmware21* string has a date suffix, e.g. HUION_T21h_23051122*/23char EXPECTED_FIRMWARE_ID[] = "HUION_T21h_";2425/* How this BPF program works: the tablet has two modes, firmware mode and26* tablet mode. In firmware mode (out of the box) the tablet sends button events27* as keyboard shortcuts and the dial as wheel but it's not forwarded by the kernel.28* In tablet mode it uses a vendor specific hid report to report everything instead.29* Depending on the mode some hid reports are never sent and the corresponding30* devices are mute.31*32* To switch the tablet use e.g. https://github.com/whot/huion-switcher33* or one of the tools from the digimend project34*35* This BPF currently works for both modes only. The huion-switcher tool sets the36* HUION_FIRMWARE_ID udev property - if that is set then we disable the firmware37* pad and pen reports (by making them vendor collections that are ignored).38* If that property is not set we fix all hidraw nodes so the tablet works in39* either mode though the drawback is that the device will show up twice if40* you bind it to all event nodes41*42* Default report descriptor for the first exposed hidraw node:43*44* # HUION Huion Keydial_K2045* # Report descriptor length: 18 bytes46* # 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 0xFF00) 047* # 0x09, 0x01, // Usage (Vendor Usage 0x01) 348* # 0xa1, 0x01, // Collection (Application) 549* # 0x85, 0x08, // Report ID (8) 750* # 0x75, 0x58, // Report Size (88) 951* # 0x95, 0x01, // Report Count (1) 1152* # 0x09, 0x01, // Usage (Vendor Usage 0x01) 1353* # 0x81, 0x02, // Input (Data,Var,Abs) 1554* # 0xc0, // End Collection 1755* R: 18 06 00 ff 09 01 a1 01 85 08 75 58 95 01 09 01 81 02 c056*57* This report descriptor appears to be identical for all Huion devices.58*59* Second hidraw node is the Pad. This one sends the button events until the tablet is60* switched to raw mode, then it's mute.61*62* # HUION Huion Keydial_K2063* # Report descriptor length: 135 bytes64* # 0x05, 0x01, // Usage Page (Generic Desktop) 065* # 0x09, 0x06, // Usage (Keyboard) 266* # 0xa1, 0x01, // Collection (Application) 467* # 0x85, 0x03, // Report ID (3) 668* # 0x05, 0x07, // Usage Page (Keyboard/Keypad) 869* # 0x19, 0xe0, // UsageMinimum (224) 1070* # 0x29, 0xe7, // UsageMaximum (231) 1271* # 0x15, 0x00, // Logical Minimum (0) 1472* # 0x25, 0x01, // Logical Maximum (1) 1673* # 0x75, 0x01, // Report Size (1) 1874* # 0x95, 0x08, // Report Count (8) 2075* # 0x81, 0x02, // Input (Data,Var,Abs) 2276* # 0x05, 0x07, // Usage Page (Keyboard/Keypad) 2477* # 0x19, 0x00, // UsageMinimum (0) 2678* # 0x29, 0xff, // UsageMaximum (255) 2879* # 0x26, 0xff, 0x00, // Logical Maximum (255) 3080* # 0x75, 0x08, // Report Size (8) 3381* # 0x95, 0x06, // Report Count (6) 3582* # 0x81, 0x00, // Input (Data,Arr,Abs) 3783* # 0xc0, // End Collection 3984* # 0x05, 0x0c, // Usage Page (Consumer) 4085* # 0x09, 0x01, // Usage (Consumer Control) 4286* # 0xa1, 0x01, // Collection (Application) 4487* # 0x85, 0x04, // Report ID (4) 4688* # 0x05, 0x0c, // Usage Page (Consumer) 4889* # 0x19, 0x00, // UsageMinimum (0) 5090* # 0x2a, 0x80, 0x03, // UsageMaximum (896) 5291* # 0x15, 0x00, // Logical Minimum (0) 5592* # 0x26, 0x80, 0x03, // Logical Maximum (896) 5793* # 0x75, 0x10, // Report Size (16) 6094* # 0x95, 0x01, // Report Count (1) 6295* # 0x81, 0x00, // Input (Data,Arr,Abs) 6496* # 0xc0, // End Collection 6697* # 0x05, 0x01, // Usage Page (Generic Desktop) 6798* # 0x09, 0x02, // Usage (Mouse) 6999* # 0xa1, 0x01, // Collection (Application) 71100* # 0x09, 0x01, // Usage (Pointer) 73101* # 0x85, 0x05, // Report ID (5) 75102* # 0xa1, 0x00, // Collection (Physical) 77103* # 0x05, 0x09, // Usage Page (Button) 79104* # 0x19, 0x01, // UsageMinimum (1) 81105* # 0x29, 0x05, // UsageMaximum (5) 83106* # 0x15, 0x00, // Logical Minimum (0) 85107* # 0x25, 0x01, // Logical Maximum (1) 87108* # 0x95, 0x05, // Report Count (5) 89109* # 0x75, 0x01, // Report Size (1) 91110* # 0x81, 0x02, // Input (Data,Var,Abs) 93111* # 0x95, 0x01, // Report Count (1) 95112* # 0x75, 0x03, // Report Size (3) 97113* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 99114* # 0x05, 0x01, // Usage Page (Generic Desktop) 101115* # 0x09, 0x30, // Usage (X) 103116* # 0x09, 0x31, // Usage (Y) 105117* # 0x16, 0x00, 0x80, // Logical Minimum (-32768) 107118* # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 110119* # 0x75, 0x10, // Report Size (16) 113120* # 0x95, 0x02, // Report Count (2) 115121* # 0x81, 0x06, // Input (Data,Var,Rel) 117122* # 0x95, 0x01, // Report Count (1) 119123* # 0x75, 0x08, // Report Size (8) 121124* # 0x05, 0x01, // Usage Page (Generic Desktop) 123125* # 0x09, 0x38, // Usage (Wheel) 125126* # 0x15, 0x81, // Logical Minimum (-127) 127127* # 0x25, 0x7f, // Logical Maximum (127) 129128* # 0x81, 0x06, // Input (Data,Var,Rel) 131129* # 0xc0, // End Collection 133130* # 0xc0, // End Collection 134131* 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 c0132*133* Third hidraw node is a multi-axis controller which sends the dial events134* and the button inside the dial. If the tablet is switched to raw mode it is mute.135*136* # HUION Huion Keydial_K20137* # Report descriptor length: 108 bytes138* # 0x05, 0x01, // Usage Page (Generic Desktop) 0139* # 0x09, 0x0e, // Usage (System Multi-Axis Controller) 2140* # 0xa1, 0x01, // Collection (Application) 4141* # 0x85, 0x11, // Report ID (17) 6142* # 0x05, 0x0d, // Usage Page (Digitizers) 8143* # 0x09, 0x21, // Usage (Puck) 10144* # 0xa1, 0x02, // Collection (Logical) 12145* # 0x15, 0x00, // Logical Minimum (0) 14146* # 0x25, 0x01, // Logical Maximum (1) 16147* # 0x75, 0x01, // Report Size (1) 18148* # 0x95, 0x01, // Report Count (1) 20149* # 0xa1, 0x00, // Collection (Physical) 22150* # 0x05, 0x09, // Usage Page (Button) 24151* # 0x09, 0x01, // Usage (Button 1) 26152* # 0x81, 0x02, // Input (Data,Var,Abs) 28153* # 0x05, 0x0d, // Usage Page (Digitizers) 30154* # 0x09, 0x33, // Usage (Touch) 32155* # 0x81, 0x02, // Input (Data,Var,Abs) 34156* # 0x95, 0x06, // Report Count (6) 36157* # 0x81, 0x03, // Input (Cnst,Var,Abs) 38158* # 0xa1, 0x02, // Collection (Logical) 40159* # 0x05, 0x01, // Usage Page (Generic Desktop) 42160* # 0x09, 0x37, // Usage (Dial) 44161* # 0x16, 0x00, 0x80, // Logical Minimum (-32768) 46162* # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 49163* # 0x75, 0x10, // Report Size (16) 52164* # 0x95, 0x01, // Report Count (1) 54165* # 0x81, 0x06, // Input (Data,Var,Rel) 56166* # 0x35, 0x00, // Physical Minimum (0) 58167* # 0x46, 0x10, 0x0e, // Physical Maximum (3600) 60168* # 0x15, 0x00, // Logical Minimum (0) 63169* # 0x26, 0x10, 0x0e, // Logical Maximum (3600) 65170* # 0x09, 0x48, // Usage (Resolution Multiplier) 68171* # 0xb1, 0x02, // Feature (Data,Var,Abs) 70172* # 0x45, 0x00, // Physical Maximum (0) 72173* # 0xc0, // End Collection 74174* # 0x75, 0x08, // Report Size (8) 75175* # 0x95, 0x01, // Report Count (1) 77176* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 79177* # 0x75, 0x08, // Report Size (8) 81178* # 0x95, 0x01, // Report Count (1) 83179* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 85180* # 0x75, 0x08, // Report Size (8) 87181* # 0x95, 0x01, // Report Count (1) 89182* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 91183* # 0x75, 0x08, // Report Size (8) 93184* # 0x95, 0x01, // Report Count (1) 95185* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 97186* # 0x75, 0x08, // Report Size (8) 99187* # 0x95, 0x01, // Report Count (1) 101188* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 103189* # 0xc0, // End Collection 105190* # 0xc0, // End Collection 106191* # 0xc0, // End Collection 107192* 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 c0193*194*/195196#define PAD_REPORT_DESCRIPTOR_LENGTH 135197#define PUCK_REPORT_DESCRIPTOR_LENGTH 108198#define VENDOR_REPORT_DESCRIPTOR_LENGTH 18199#define PAD_KBD_REPORT_ID 3200#define PAD_CC_REPORT_ID 3 // never sends events201#define PAD_MOUSE_REPORT_ID 4 // never sends events202#define PUCK_REPORT_ID 17203#define VENDOR_REPORT_ID 8204#define PAD_KBD_REPORT_LENGTH 8205#define PAD_CC_REPORT_LENGTH 3206#define PAD_MOUSE_REPORT_LENGTH 7207#define PUCK_REPORT_LENGTH 9208#define VENDOR_REPORT_LENGTH 12209210__u32 last_button_state;211212static const __u8 disabled_rdesc_puck[] = {213FixedSizeVendorReport(PUCK_REPORT_LENGTH)214};215216static const __u8 disabled_rdesc_pad[] = {217FixedSizeVendorReport(PAD_KBD_REPORT_LENGTH)218FixedSizeVendorReport(PAD_CC_REPORT_LENGTH)219FixedSizeVendorReport(PAD_MOUSE_REPORT_LENGTH)220};221222static const __u8 fixed_rdesc_vendor[] = {223UsagePage_GenericDesktop224Usage_GD_Keypad225CollectionApplication(226// Byte 0227// We send our pad events on the vendor report id because why not228ReportId(VENDOR_REPORT_ID)229UsagePage_Digitizers230Usage_Dig_TabletFunctionKeys231CollectionPhysical(232// Byte 1 is a button so we look like a tablet233Usage_Dig_BarrelSwitch // BTN_STYLUS, needed so we get to be a tablet pad234ReportCount(1)235ReportSize(1)236Input(Var|Abs)237ReportCount(7) // Padding238Input(Const)239// Bytes 2/3 - x/y just exist so we get to be a tablet pad240UsagePage_GenericDesktop241Usage_GD_X242Usage_GD_Y243LogicalMinimum_i8(0x0)244LogicalMaximum_i8(0x1)245ReportCount(2)246ReportSize(8)247Input(Var|Abs)248// Bytes 4-7 are the button state for 19 buttons + pad out to u32249// We send the first 10 buttons as buttons 1-10 which is BTN_0 -> BTN_9250UsagePage_Button251UsageMinimum_i8(1)252UsageMaximum_i8(10)253LogicalMinimum_i8(0x0)254LogicalMaximum_i8(0x1)255ReportCount(10)256ReportSize(1)257Input(Var|Abs)258// We send the other 9 buttons as buttons 0x31 and above -> BTN_A - BTN_TL2259UsageMinimum_i8(0x31)260UsageMaximum_i8(0x3a)261ReportCount(9)262ReportSize(1)263Input(Var|Abs)264ReportCount(13)265ReportSize(1)266Input(Const) // padding267// Byte 6 is the wheel268UsagePage_GenericDesktop269Usage_GD_Wheel270LogicalMinimum_i8(-1)271LogicalMaximum_i8(1)272ReportCount(1)273ReportSize(8)274Input(Var|Rel)275)276// Make sure we match our original report length277FixedSizeVendorReport(VENDOR_REPORT_LENGTH)278)279};280281/* Identical to fixed_rdesc_pad but with different FixedSizeVendorReport */282static const __u8 fixed_rdesc_pad[] = {283UsagePage_GenericDesktop284Usage_GD_Keypad285CollectionApplication(286// Byte 0287// We send our pad events on the vendor report id because why not288ReportId(VENDOR_REPORT_ID)289UsagePage_Digitizers290Usage_Dig_TabletFunctionKeys291CollectionPhysical(292// Byte 1 is a button so we look like a tablet293Usage_Dig_BarrelSwitch // BTN_STYLUS, needed so we get to be a tablet pad294ReportCount(1)295ReportSize(1)296Input(Var|Abs)297ReportCount(7) // Padding298Input(Const)299// Bytes 2/3 - x/y just exist so we get to be a tablet pad300UsagePage_GenericDesktop301Usage_GD_X302Usage_GD_Y303LogicalMinimum_i8(0x0)304LogicalMaximum_i8(0x1)305ReportCount(2)306ReportSize(8)307Input(Var|Abs)308// Bytes 4-7 are the button state for 19 buttons + pad out to u32309// We send the first 10 buttons as buttons 1-10 which is BTN_0 -> BTN_9310UsagePage_Button311UsageMinimum_i8(1)312UsageMaximum_i8(10)313LogicalMinimum_i8(0x0)314LogicalMaximum_i8(0x1)315ReportCount(10)316ReportSize(1)317Input(Var|Abs)318// We send the other 9 buttons as buttons 0x31 and above -> BTN_A - BTN_TL2319UsageMinimum_i8(0x31)320UsageMaximum_i8(0x3a)321ReportCount(9)322ReportSize(1)323Input(Var|Abs)324ReportCount(13)325ReportSize(1)326Input(Const) // padding327// Byte 6 is the wheel328UsagePage_GenericDesktop329Usage_GD_Wheel330LogicalMinimum_i8(-1)331LogicalMaximum_i8(1)332ReportCount(1)333ReportSize(8)334Input(Var|Rel)335)336// Make sure we match our original report lengths337FixedSizeVendorReport(PAD_KBD_REPORT_LENGTH)338FixedSizeVendorReport(PAD_CC_REPORT_LENGTH)339FixedSizeVendorReport(PAD_MOUSE_REPORT_LENGTH)340)341};342343SEC(HID_BPF_RDESC_FIXUP)344int BPF_PROG(k20_fix_rdesc, struct hid_bpf_ctx *hctx)345{346__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, HID_MAX_DESCRIPTOR_SIZE /* size */);347__s32 rdesc_size = hctx->size;348__u8 have_fw_id;349350if (!data)351return 0; /* EPERM check */352353/* If we have a firmware ID and it matches our expected prefix, we354* disable the default pad/puck nodes. They won't send events355* but cause duplicate devices.356*/357have_fw_id = __builtin_memcmp(UDEV_PROP_HUION_FIRMWARE_ID,358EXPECTED_FIRMWARE_ID,359sizeof(EXPECTED_FIRMWARE_ID) - 1) == 0;360if (rdesc_size == PAD_REPORT_DESCRIPTOR_LENGTH) {361if (have_fw_id) {362__builtin_memcpy(data, disabled_rdesc_pad, sizeof(disabled_rdesc_pad));363return sizeof(disabled_rdesc_pad);364} else {365__builtin_memcpy(data, fixed_rdesc_pad, sizeof(fixed_rdesc_pad));366return sizeof(fixed_rdesc_pad);367368}369}370if (rdesc_size == PUCK_REPORT_DESCRIPTOR_LENGTH) {371if (have_fw_id) {372__builtin_memcpy(data, disabled_rdesc_puck, sizeof(disabled_rdesc_puck));373return sizeof(disabled_rdesc_puck);374}375}376/* Always fix the vendor mode so the tablet will work even if nothing sets377* the udev property (e.g. huion-switcher run manually)378*/379if (rdesc_size == VENDOR_REPORT_DESCRIPTOR_LENGTH) {380__builtin_memcpy(data, fixed_rdesc_vendor, sizeof(fixed_rdesc_vendor));381return sizeof(fixed_rdesc_vendor);382383}384return 0;385}386387SEC(HID_BPF_DEVICE_EVENT)388int BPF_PROG(k20_fix_events, struct hid_bpf_ctx *hctx)389{390__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 10 /* size */);391392if (!data)393return 0; /* EPERM check */394395/* Only sent if tablet is in raw mode */396if (data[0] == VENDOR_REPORT_ID) {397/* See fixed_rdesc_pad */398struct pad_report {399__u8 report_id;400__u8 btn_stylus:1;401__u8 pad:7;402__u8 x;403__u8 y;404__u32 buttons;405__u8 wheel;406} __attribute__((packed)) *pad_report;407408__u8 wheel = 0;409410/* Wheel report */411if (data[1] == 0xf1) {412if (data[5] == 2)413wheel = 0xff;414else415wheel = data[5];416} else {417/* data[4..6] are the buttons, mapped correctly */418last_button_state = data[4] | (data[5] << 8) | (data[6] << 16);419wheel = 0; // wheel420}421422pad_report = (struct pad_report *)data;423pad_report->report_id = VENDOR_REPORT_ID;424pad_report->btn_stylus = 0;425pad_report->x = 0;426pad_report->y = 0;427pad_report->buttons = last_button_state;428pad_report->wheel = wheel;429430return sizeof(struct pad_report);431}432433if (data[0] == PAD_KBD_REPORT_ID) {434const __u8 button_mapping[] = {4350x0e, /* Button 1: K */4360x0a, /* Button 2: G */4370x0f, /* Button 3: L */4380x4c, /* Button 4: Delete */4390x0c, /* Button 5: I */4400x07, /* Button 6: D */4410x05, /* Button 7: B */4420x08, /* Button 8: E */4430x16, /* Button 9: S */4440x1d, /* Button 10: Z */4450x06, /* Button 11: C */4460x19, /* Button 12: V */4470xff, /* Button 13: LeftControl */4480xff, /* Button 14: LeftAlt */4490xff, /* Button 15: LeftShift */4500x28, /* Button 16: Return Enter */4510x2c, /* Button 17: Spacebar */4520x11, /* Button 18: N */453};454/* See fixed_rdesc_pad */455struct pad_report {456__u8 report_id;457__u8 btn_stylus:1;458__u8 pad:7;459__u8 x;460__u8 y;461__u32 buttons;462__u8 wheel;463} __attribute__((packed)) *pad_report;464int i, b;465__u8 modifiers = data[1];466__u32 buttons = 0;467468if (modifiers & 0x01) { /* Control */469buttons |= BIT(12);470}471if (modifiers & 0x02) { /* Shift */472buttons |= BIT(14);473}474if (modifiers & 0x04) { /* Alt */475buttons |= BIT(13);476}477478for (i = 2; i < PAD_KBD_REPORT_LENGTH; i++) {479if (!data[i])480break;481482for (b = 0; b < ARRAY_SIZE(button_mapping); b++) {483if (data[i] == button_mapping[b]) {484buttons |= BIT(b);485break;486}487}488data[i] = 0;489}490491pad_report = (struct pad_report *)data;492pad_report->report_id = VENDOR_REPORT_ID;493pad_report->btn_stylus = 0;494pad_report->x = 0;495pad_report->y = 0;496pad_report->buttons = buttons;497// The wheel happens on a different hidraw node but its498// values are unreliable (as is the button inside the wheel).499// So the wheel is simply always zero, if you want the wheel500// to work reliably, use the tablet mode.501pad_report->wheel = 0;502503return sizeof(struct pad_report);504}505506return 0;507}508509HID_BPF_OPS(keydial_k20) = {510.hid_device_event = (void *)k20_fix_events,511.hid_rdesc_fixup = (void *)k20_fix_rdesc,512};513514SEC("syscall")515int probe(struct hid_bpf_probe_args *ctx)516{517switch (ctx->rdesc_size) {518case PAD_REPORT_DESCRIPTOR_LENGTH:519case PUCK_REPORT_DESCRIPTOR_LENGTH:520case VENDOR_REPORT_DESCRIPTOR_LENGTH:521ctx->retval = 0;522break;523default:524ctx->retval = -EINVAL;525}526527return 0;528}529530char _license[] SEC("license") = "GPL";531532533