Path: blob/master/drivers/hid/bpf/progs/Huion__Kamvas16Gen3.bpf.c
38242 views
// SPDX-License-Identifier: GPL-2.0-only1/* Copyright (c) 2025 Nicholas LaPointe2* Copyright (c) 2025 Higgins Dragon3*/45#include "vmlinux.h"6#include "hid_bpf.h"7#include "hid_bpf_helpers.h"8#include "hid_report_helpers.h"9#include <bpf/bpf_tracing.h>1011#define VID_HUION 0x256c12#define PID_KAMVAS16_GEN3 0x20091314#define VENDOR_DESCRIPTOR_LENGTH 3615#define TABLET_DESCRIPTOR_LENGTH 32816#define WHEEL_DESCRIPTOR_LENGTH 2001718#define VENDOR_REPORT_ID 819#define VENDOR_REPORT_LENGTH 142021#define VENDOR_REPORT_SUBTYPE_PEN 0x0822#define VENDOR_REPORT_SUBTYPE_PEN_OUT 0x0023#define VENDOR_REPORT_SUBTYPE_BUTTONS 0x0e24#define VENDOR_REPORT_SUBTYPE_WHEELS 0x0f2526/* For the reports that we create ourselves */27#define CUSTOM_PAD_REPORT_ID 92829HID_BPF_CONFIG(30HID_DEVICE(BUS_USB, HID_GROUP_ANY, VID_HUION, PID_KAMVAS16_GEN3),31);3233/*34* This tablet can send reports using one of two different data formats,35* depending on what "mode" the tablet is in.36*37* By default, the tablet will send reports that can be decoded using its38* included HID descriptors (descriptors 1 and 2, shown below).39* This mode will be called "firmware mode" throughout this file.40*41* The HID descriptor that describes pen events in firmware mode (descriptor 1)42* has multiple bugs:43* * "Secondary Tip Switch" instead of "Secondary Barrel Switch"44* * "Invert" instead of (or potentially shared with) third barrel button45* * Specified tablet area of 2048 in³ instead of 293.8 x 165.2mm46* * Specified tilt range of -90 to +90 instead of -60 to +6047*48* While these can be easily patched up by editing the descriptor, a larger49* problem with the firmware mode exists: it is impossible to tell which of the50* two wheels are being rotated (or having their central button pressed).51*52*53* By using a tool such as huion-switcher (https://github.com/whot/huion-switcher),54* the tablet can be made to send reports using a proprietary format that is not55* adequately described by its relevant descriptor (descriptor 0, shown below).56* This mode will be called "vendor mode" throughout this file.57*58* The reports sent while in vendor mode allow for proper decoding of the wheels.59*60* For simplicity and maximum functionality, this BPF focuses strictly on61* enabling one to make use of the vendor mode.62*/6364/*65* DESCRIPTORS66* DESCRIPTOR 067* # 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 068* # 0x09, 0x01, // Usage (Vendor Usage 1) 369* # 0xa1, 0x01, // Collection (Application) 570* # 0x85, 0x08, // Report ID (8) 771* # 0x75, 0x68, // Report Size (104) 972* # 0x95, 0x01, // Report Count (1) 1173* # 0x09, 0x01, // Usage (Vendor Usage 1) 1374* # 0x81, 0x02, // Input (Data,Var,Abs) 1575* # 0xc0, // End Collection 1776* # 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 1877* # 0x09, 0x01, // Usage (Vendor Usage 1) 2178* # 0xa1, 0x01, // Collection (Application) 2379* # 0x85, 0x16, // Report ID (22) 2580* # 0x75, 0x08, // Report Size (8) 2781* # 0x95, 0x07, // Report Count (7) 2982* # 0x09, 0x01, // Usage (Vendor Usage 1) 3183* # 0xb1, 0x02, // Feature (Data,Var,Abs) 3384* # 0xc0, // End Collection 3585* #86* R: 36 06 00 ff 09 01 a1 01 85 08 75 68 95 01 09 01 81 02 c0 06 00 ff 09 01 a1 01 85 16 75 08 95 07 09 01 b1 02 c087* N: HUION Huion Tablet_GS156388* I: 3 256c 200989*90*91* DESCRIPTOR 192* # 0x05, 0x0d, // Usage Page (Digitizers) 093* # 0x09, 0x02, // Usage (Pen) 294* # 0xa1, 0x01, // Collection (Application) 495* # 0x85, 0x0a, // Report ID (10) 696* # 0x09, 0x20, // Usage (Stylus) 897* # 0xa1, 0x01, // Collection (Application) 1098* # 0x09, 0x42, // Usage (Tip Switch) 1299* # 0x09, 0x44, // Usage (Barrel Switch) 14100* # 0x09, 0x43, // Usage (Secondary Tip Switch) 16101* # 0x09, 0x3c, // Usage (Invert) 18102* # 0x09, 0x45, // Usage (Eraser) 20103* # 0x15, 0x00, // Logical Minimum (0) 22104* # 0x25, 0x01, // Logical Maximum (1) 24105* # 0x75, 0x01, // Report Size (1) 26106* # 0x95, 0x06, // Report Count (6) 28107* # 0x81, 0x02, // Input (Data,Var,Abs) 30108* # 0x09, 0x32, // Usage (In Range) 32109* # 0x75, 0x01, // Report Size (1) 34110* # 0x95, 0x01, // Report Count (1) 36111* # 0x81, 0x02, // Input (Data,Var,Abs) 38112* # 0x81, 0x03, // Input (Cnst,Var,Abs) 40113* # 0x05, 0x01, // Usage Page (Generic Desktop) 42114* # 0x09, 0x30, // Usage (X) 44115* # 0x09, 0x31, // Usage (Y) 46116* # 0x55, 0x0d, // Unit Exponent (-3) 48117* # 0x65, 0x33, // Unit (EnglishLinear: in³) 50118* # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 52119* # 0x35, 0x00, // Physical Minimum (0) 55120* # 0x46, 0x00, 0x08, // Physical Maximum (2048) 57121* # 0x75, 0x10, // Report Size (16) 60122* # 0x95, 0x02, // Report Count (2) 62123* # 0x81, 0x02, // Input (Data,Var,Abs) 64124* # 0x05, 0x0d, // Usage Page (Digitizers) 66125* # 0x09, 0x30, // Usage (Tip Pressure) 68126* # 0x26, 0xff, 0x3f, // Logical Maximum (16383) 70127* # 0x75, 0x10, // Report Size (16) 73128* # 0x95, 0x01, // Report Count (1) 75129* # 0x81, 0x02, // Input (Data,Var,Abs) 77130* # 0x09, 0x3d, // Usage (X Tilt) 79131* # 0x09, 0x3e, // Usage (Y Tilt) 81132* # 0x15, 0xa6, // Logical Minimum (-90) 83133* # 0x25, 0x5a, // Logical Maximum (90) 85134* # 0x75, 0x08, // Report Size (8) 87135* # 0x95, 0x02, // Report Count (2) 89136* # 0x81, 0x02, // Input (Data,Var,Abs) 91137* # 0xc0, // End Collection 93138* # 0xc0, // End Collection 94139* # 0x05, 0x0d, // Usage Page (Digitizers) 95140* # 0x09, 0x04, // Usage (Touch Screen) 97141* # 0xa1, 0x01, // Collection (Application) 99142* # 0x85, 0x04, // Report ID (4) 101143* # 0x09, 0x22, // Usage (Finger) 103144* # 0xa1, 0x02, // Collection (Logical) 105145* # 0x05, 0x0d, // Usage Page (Digitizers) 107146* # 0x95, 0x01, // Report Count (1) 109147* # 0x75, 0x06, // Report Size (6) 111148* # 0x09, 0x51, // Usage (Contact Id) 113149* # 0x15, 0x00, // Logical Minimum (0) 115150* # 0x25, 0x3f, // Logical Maximum (63) 117151* # 0x81, 0x02, // Input (Data,Var,Abs) 119152* # 0x09, 0x42, // Usage (Tip Switch) 121153* # 0x25, 0x01, // Logical Maximum (1) 123154* # 0x75, 0x01, // Report Size (1) 125155* # 0x95, 0x01, // Report Count (1) 127156* # 0x81, 0x02, // Input (Data,Var,Abs) 129157* # 0x75, 0x01, // Report Size (1) 131158* # 0x95, 0x01, // Report Count (1) 133159* # 0x81, 0x03, // Input (Cnst,Var,Abs) 135160* # 0x05, 0x01, // Usage Page (Generic Desktop) 137161* # 0x75, 0x10, // Report Size (16) 139162* # 0x55, 0x0e, // Unit Exponent (-2) 141163* # 0x65, 0x11, // Unit (SILinear: cm) 143164* # 0x09, 0x30, // Usage (X) 145165* # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 147166* # 0x35, 0x00, // Physical Minimum (0) 150167* # 0x46, 0x15, 0x0c, // Physical Maximum (3093) 152168* # 0x81, 0x42, // Input (Data,Var,Abs,Null) 155169* # 0x09, 0x31, // Usage (Y) 157170* # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 159171* # 0x46, 0xcb, 0x06, // Physical Maximum (1739) 162172* # 0x81, 0x42, // Input (Data,Var,Abs,Null) 165173* # 0x05, 0x0d, // Usage Page (Digitizers) 167174* # 0x09, 0x30, // Usage (Tip Pressure) 169175* # 0x26, 0xff, 0x1f, // Logical Maximum (8191) 171176* # 0x75, 0x10, // Report Size (16) 174177* # 0x95, 0x01, // Report Count (1) 176178* # 0x81, 0x02, // Input (Data,Var,Abs) 178179* # 0xc0, // End Collection 180180* # 0x05, 0x0d, // Usage Page (Digitizers) 181181* # 0x09, 0x22, // Usage (Finger) 183182* # 0xa1, 0x02, // Collection (Logical) 185183* # 0x05, 0x0d, // Usage Page (Digitizers) 187184* # 0x95, 0x01, // Report Count (1) 189185* # 0x75, 0x06, // Report Size (6) 191186* # 0x09, 0x51, // Usage (Contact Id) 193187* # 0x15, 0x00, // Logical Minimum (0) 195188* # 0x25, 0x3f, // Logical Maximum (63) 197189* # 0x81, 0x02, // Input (Data,Var,Abs) 199190* # 0x09, 0x42, // Usage (Tip Switch) 201191* # 0x25, 0x01, // Logical Maximum (1) 203192* # 0x75, 0x01, // Report Size (1) 205193* # 0x95, 0x01, // Report Count (1) 207194* # 0x81, 0x02, // Input (Data,Var,Abs) 209195* # 0x75, 0x01, // Report Size (1) 211196* # 0x95, 0x01, // Report Count (1) 213197* # 0x81, 0x03, // Input (Cnst,Var,Abs) 215198* # 0x05, 0x01, // Usage Page (Generic Desktop) 217199* # 0x75, 0x10, // Report Size (16) 219200* # 0x55, 0x0e, // Unit Exponent (-2) 221201* # 0x65, 0x11, // Unit (SILinear: cm) 223202* # 0x09, 0x30, // Usage (X) 225203* # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 227204* # 0x35, 0x00, // Physical Minimum (0) 230205* # 0x46, 0x15, 0x0c, // Physical Maximum (3093) 232206* # 0x81, 0x42, // Input (Data,Var,Abs,Null) 235207* # 0x09, 0x31, // Usage (Y) 237208* # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 239209* # 0x46, 0xcb, 0x06, // Physical Maximum (1739) 242210* # 0x81, 0x42, // Input (Data,Var,Abs,Null) 245211* # 0x05, 0x0d, // Usage Page (Digitizers) 247212* # 0x09, 0x30, // Usage (Tip Pressure) 249213* # 0x26, 0xff, 0x1f, // Logical Maximum (8191) 251214* # 0x75, 0x10, // Report Size (16) 254215* # 0x95, 0x01, // Report Count (1) 256216* # 0x81, 0x02, // Input (Data,Var,Abs) 258217* # 0xc0, // End Collection 260218* # 0x05, 0x0d, // Usage Page (Digitizers) 261219* # 0x09, 0x56, // Usage (Scan Time) 263220* # 0x55, 0x00, // Unit Exponent (0) 265221* # 0x65, 0x00, // Unit (None) 267222* # 0x27, 0xff, 0xff, 0xff, 0x7f, // Logical Maximum (2147483647) 269223* # 0x95, 0x01, // Report Count (1) 274224* # 0x75, 0x20, // Report Size (32) 276225* # 0x81, 0x02, // Input (Data,Var,Abs) 278226* # 0x09, 0x54, // Usage (Contact Count) 280227* # 0x25, 0x7f, // Logical Maximum (127) 282228* # 0x95, 0x01, // Report Count (1) 284229* # 0x75, 0x08, // Report Size (8) 286230* # 0x81, 0x02, // Input (Data,Var,Abs) 288231* # 0x75, 0x08, // Report Size (8) 290232* # 0x95, 0x08, // Report Count (8) 292233* # 0x81, 0x03, // Input (Cnst,Var,Abs) 294234* # 0x85, 0x05, // Report ID (5) 296235* # 0x09, 0x55, // Usage (Contact Max) 298236* # 0x25, 0x0a, // Logical Maximum (10) 300237* # 0x75, 0x08, // Report Size (8) 302238* # 0x95, 0x01, // Report Count (1) 304239* # 0xb1, 0x02, // Feature (Data,Var,Abs) 306240* # 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 308241* # 0x09, 0xc5, // Usage (Vendor Usage 0xc5) 311242* # 0x85, 0x06, // Report ID (6) 313243* # 0x15, 0x00, // Logical Minimum (0) 315244* # 0x26, 0xff, 0x00, // Logical Maximum (255) 317245* # 0x75, 0x08, // Report Size (8) 320246* # 0x96, 0x00, 0x01, // Report Count (256) 322247* # 0xb1, 0x02, // Feature (Data,Var,Abs) 325248* # 0xc0, // End Collection 327249* #250* R: 328 05 0d 09 02 a1 01 85 0a 09 20 a1 01 09 42 09 44 09 43 09 3c 09 45 15 00 25 01 75 01 95 06 81 02 09 32 75 01 95 01 81 02 81 03 05 01 09 30 09 31 55 0d 65 33 26 ff 7f 35 00 46 00 08 75 10 95 02 81 02 05 0d 09 30 26 ff 3f 75 10 95 01 81 02 09 3d 09 3e 15 a6 25 5a 75 08 95 02 81 02 c0 c0 05 0d 09 04 a1 01 85 04 09 22 a1 02 05 0d 95 01 75 06 09 51 15 00 25 3f 81 02 09 42 25 01 75 01 95 01 81 02 75 01 95 01 81 03 05 01 75 10 55 0e 65 11 09 30 26 ff 7f 35 00 46 15 0c 81 42 09 31 26 ff 7f 46 cb 06 81 42 05 0d 09 30 26 ff 1f 75 10 95 01 81 02 c0 05 0d 09 22 a1 02 05 0d 95 01 75 06 09 51 15 00 25 3f 81 02 09 42 25 01 75 01 95 01 81 02 75 01 95 01 81 03 05 01 75 10 55 0e 65 11 09 30 26 ff 7f 35 00 46 15 0c 81 42 09 31 26 ff 7f 46 cb 06 81 42 05 0d 09 30 26 ff 1f 75 10 95 01 81 02 c0 05 0d 09 56 55 00 65 00 27 ff ff ff 7f 95 01 75 20 81 02 09 54 25 7f 95 01 75 08 81 02 75 08 95 08 81 03 85 05 09 55 25 0a 75 08 95 01 b1 02 06 00 ff 09 c5 85 06 15 00 26 ff 00 75 08 96 00 01 b1 02 c0251* N: HUION Huion Tablet_GS1563252* I: 3 256c 2009253*254* DESCRIPTOR 2255* # 0x05, 0x01, // Usage Page (Generic Desktop) 0256* # 0x09, 0x0e, // Usage (System Multi-Axis Controller) 2257* # 0xa1, 0x01, // Collection (Application) 4258* # 0x85, 0x11, // Report ID (17) 6259* # 0x05, 0x0d, // Usage Page (Digitizers) 8260* # 0x09, 0x21, // Usage (Puck) 10261* # 0xa1, 0x02, // Collection (Logical) 12262* # 0x15, 0x00, // Logical Minimum (0) 14263* # 0x25, 0x01, // Logical Maximum (1) 16264* # 0x75, 0x01, // Report Size (1) 18265* # 0x95, 0x01, // Report Count (1) 20266* # 0xa1, 0x00, // Collection (Physical) 22267* # 0x05, 0x09, // Usage Page (Button) 24268* # 0x09, 0x01, // Usage (Vendor Usage 0x01) 26269* # 0x81, 0x02, // Input (Data,Var,Abs) 28270* # 0x05, 0x0d, // Usage Page (Digitizers) 30271* # 0x09, 0x33, // Usage (Touch) 32272* # 0x81, 0x02, // Input (Data,Var,Abs) 34273* # 0x95, 0x06, // Report Count (6) 36274* # 0x81, 0x03, // Input (Cnst,Var,Abs) 38275* # 0xa1, 0x02, // Collection (Logical) 40276* # 0x05, 0x01, // Usage Page (Generic Desktop) 42277* # 0x09, 0x37, // Usage (Dial) 44278* # 0x16, 0x00, 0x80, // Logical Minimum (-32768) 46279* # 0x26, 0xff, 0x7f, // Logical Maximum (32767) 49280* # 0x75, 0x10, // Report Size (16) 52281* # 0x95, 0x01, // Report Count (1) 54282* # 0x81, 0x06, // Input (Data,Var,Rel) 56283* # 0x35, 0x00, // Physical Minimum (0) 58284* # 0x46, 0x10, 0x0e, // Physical Maximum (3600) 60285* # 0x15, 0x00, // Logical Minimum (0) 63286* # 0x26, 0x10, 0x0e, // Logical Maximum (3600) 65287* # 0x09, 0x48, // Usage (Resolution Multiplier) 68288* # 0xb1, 0x02, // Feature (Data,Var,Abs) 70289* # 0x45, 0x00, // Physical Maximum (0) 72290* # 0xc0, // End Collection 74291* # 0x75, 0x08, // Report Size (8) 75292* # 0x95, 0x01, // Report Count (1) 77293* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 79294* # 0x75, 0x08, // Report Size (8) 81295* # 0x95, 0x01, // Report Count (1) 83296* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 85297* # 0x75, 0x08, // Report Size (8) 87298* # 0x95, 0x01, // Report Count (1) 89299* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 91300* # 0x75, 0x08, // Report Size (8) 93301* # 0x95, 0x01, // Report Count (1) 95302* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 97303* # 0x75, 0x08, // Report Size (8) 99304* # 0x95, 0x01, // Report Count (1) 101305* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 103306* # 0xc0, // End Collection 105307* # 0xc0, // End Collection 106308* # 0xc0, // End Collection 107309* # 0x05, 0x01, // Usage Page (Generic Desktop) 108310* # 0x09, 0x06, // Usage (Keyboard) 110311* # 0xa1, 0x01, // Collection (Application) 112312* # 0x85, 0x03, // Report ID (3) 114313* # 0x05, 0x07, // Usage Page (Keyboard) 116314* # 0x19, 0xe0, // Usage Minimum (224) 118315* # 0x29, 0xe7, // Usage Maximum (231) 120316* # 0x15, 0x00, // Logical Minimum (0) 122317* # 0x25, 0x01, // Logical Maximum (1) 124318* # 0x75, 0x01, // Report Size (1) 126319* # 0x95, 0x08, // Report Count (8) 128320* # 0x81, 0x02, // Input (Data,Var,Abs) 130321* # 0x05, 0x07, // Usage Page (Keyboard) 132322* # 0x19, 0x00, // Usage Minimum (0) 134323* # 0x29, 0xff, // Usage Maximum (255) 136324* # 0x26, 0xff, 0x00, // Logical Maximum (255) 138325* # 0x75, 0x08, // Report Size (8) 141326* # 0x95, 0x06, // Report Count (6) 143327* # 0x81, 0x00, // Input (Data,Arr,Abs) 145328* # 0xc0, // End Collection 147329* # 0x05, 0x0c, // Usage Page (Consumer Devices) 148330* # 0x09, 0x01, // Usage (Consumer Control) 150331* # 0xa1, 0x01, // Collection (Application) 152332* # 0x85, 0x04, // Report ID (4) 154333* # 0x19, 0x01, // Usage Minimum (1) 156334* # 0x2a, 0x9c, 0x02, // Usage Maximum (668) 158335* # 0x15, 0x01, // Logical Minimum (1) 161336* # 0x26, 0x9c, 0x02, // Logical Maximum (668) 163337* # 0x95, 0x01, // Report Count (1) 166338* # 0x75, 0x10, // Report Size (16) 168339* # 0x81, 0x00, // Input (Data,Arr,Abs) 170340* # 0xc0, // End Collection 172341* # 0x05, 0x01, // Usage Page (Generic Desktop) 173342* # 0x09, 0x80, // Usage (System Control) 175343* # 0xa1, 0x01, // Collection (Application) 177344* # 0x85, 0x05, // Report ID (5) 179345* # 0x19, 0x81, // Usage Minimum (129) 181346* # 0x29, 0x83, // Usage Maximum (131) 183347* # 0x15, 0x00, // Logical Minimum (0) 185348* # 0x25, 0x01, // Logical Maximum (1) 187349* # 0x75, 0x01, // Report Size (1) 189350* # 0x95, 0x03, // Report Count (3) 191351* # 0x81, 0x02, // Input (Data,Var,Abs) 193352* # 0x95, 0x05, // Report Count (5) 195353* # 0x81, 0x01, // Input (Cnst,Arr,Abs) 197354* # 0xc0, // End Collection 199355* #356* R: 200 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 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 19 01 2a 9c 02 15 01 26 9c 02 95 01 75 10 81 00 c0 05 01 09 80 a1 01 85 05 19 81 29 83 15 00 25 01 75 01 95 03 81 02 95 05 81 01 c0357* N: HUION Huion Tablet_GS1563358* I: 3 256c 2009359*360*361*362* VENDOR MODE363* HUION_FIRMWARE_ID="HUION_M22d_241101"364* HUION_MAGIC_BYTES="1403201101ac9900ff3fd81305080080083c4010"365*366* MAGIC BYTES367* [LogicalMaximum, X ] [LogicalMaximum, Y ] [LogicalMaximum, Pressure] [ LPI]368* 14 03 [ 20 11 01] [ ac 99 00] [ ff 3f] [d8 13] 05 08 00 80 08 3c 40 10369*370* See Huion__Kamvas13Gen3.bpf.c for more details on detailed button/dial reports and caveats. It's very371* similar to the Kamvas 16 Gen 3.372*/373374375/* Filled in by udev-hid-bpf */376char UDEV_PROP_HUION_FIRMWARE_ID[64];377378char EXPECTED_FIRMWARE_ID[] = "HUION_M22d_";379380__u8 last_button_state;381382static const __u8 disabled_rdesc_tablet[] = {383FixedSizeVendorReport(28) /* Input report 4 */384};385386static const __u8 disabled_rdesc_wheel[] = {387FixedSizeVendorReport(9) /* Input report 17 */388};389390static const __u8 fixed_rdesc_vendor[] = {391UsagePage_Digitizers392Usage_Dig_Pen393CollectionApplication(394ReportId(VENDOR_REPORT_ID)395UsagePage_Digitizers396Usage_Dig_Pen397CollectionPhysical(398/*399* I have only examined the tablet's behavior while using400* the PW600L pen, which does not have an eraser.401* Because of this, I don't know where the Eraser and Invert402* bits will go, or if they work as one would expect.403*404* For the time being, there is no expectation that a pen405* with an eraser will work without modifications here.406*/407ReportSize(1)408LogicalMinimum_i8(0)409LogicalMaximum_i8(1)410ReportCount(3)411Usage_Dig_TipSwitch412Usage_Dig_BarrelSwitch413Usage_Dig_SecondaryBarrelSwitch414Input(Var|Abs)415PushPop(416ReportCount(1)417UsagePage_Button418Usage_i8(0x4a) /* (BTN_STYLUS3 + 1) & 0xff */419Input(Var|Abs)420)421ReportCount(3)422Input(Const)423ReportCount(1)424Usage_Dig_InRange425Input(Var|Abs)426ReportSize(16)427ReportCount(1)428PushPop(429UsagePage_GenericDesktop430Unit(cm)431UnitExponent(-2)432LogicalMinimum_i16(0)433PhysicalMinimum_i16(0)434/*435* The tablet has a logical maximum of 69920 x 39340436* and a claimed resolution of 5080 LPI (200 L/mm)437* This works out to a physical maximum of438* 349.6 x 196.7mm, which matches Huion's advertised439* (rounded) active area dimensions from440* https://www.huion.com/products/pen_display/Kamvas/kamvas-16-gen-3.html441*442* The Kamvas uses data[8] for the 3rd byte of the X-axis, and adding443* that after data[2] and data[3] makes a contiguous little-endian444* 24-bit value. (See BPF_PROG below)445*/446ReportSize(24)447LogicalMaximum_i32(69920)448PhysicalMaximum_i16(3496)449Usage_GD_X450Input(Var|Abs)451ReportSize(16)452LogicalMaximum_i16(39340)453PhysicalMaximum_i16(1967)454Usage_GD_Y455Input(Var|Abs)456)457ReportSize(16)458LogicalMinimum_i16(0)459LogicalMaximum_i16(16383)460Usage_Dig_TipPressure461Input(Var|Abs)462ReportSize(8)463ReportCount(1)464Input(Const)465ReportCount(2)466PushPop(467Unit(deg)468UnitExponent(0)469LogicalMinimum_i8(-60)470PhysicalMinimum_i8(-60)471LogicalMaximum_i8(60)472PhysicalMaximum_i8(60)473Usage_Dig_XTilt474Usage_Dig_YTilt475Input(Var|Abs)476)477)478)479UsagePage_GenericDesktop480Usage_GD_Keypad481CollectionApplication(482ReportId(CUSTOM_PAD_REPORT_ID)483LogicalMinimum_i8(0)484LogicalMaximum_i8(1)485UsagePage_Digitizers486Usage_Dig_TabletFunctionKeys487CollectionPhysical(488/*489* The first 3 bytes are somewhat vestigial and will490* always be set to zero. Their presence here is needed491* to ensure that this device will be detected as a492* tablet pad by software that otherwise wouldn't know493* any better.494*/495/* (data[1] & 0x01) barrel switch */496ReportSize(1)497ReportCount(1)498Usage_Dig_BarrelSwitch499Input(Var|Abs)500ReportCount(7)501Input(Const)502/* data[2] X */503/* data[3] Y */504ReportSize(8)505ReportCount(2)506UsagePage_GenericDesktop507Usage_GD_X508Usage_GD_Y509Input(Var|Abs)510/*511* (data[4] & 0x01) button 1512* (data[4] & 0x02) button 2513* (data[4] & 0x04) button 3514* (data[4] & 0x08) button 4515* (data[4] & 0x10) button 5516* (data[4] & 0x20) button 6517* (data[4] & 0x40) button 7 (top wheel button)518* (data[4] & 0x80) button 8 (bottom wheel button)519*/520ReportSize(1)521ReportCount(8)522UsagePage_Button523UsageMinimum_i8(1)524UsageMaximum_i8(8)525Input(Var|Abs)526/* data[5] top wheel (signed, positive clockwise) */527ReportSize(8)528ReportCount(1)529UsagePage_GenericDesktop530Usage_GD_Wheel531LogicalMinimum_i8(-1)532LogicalMaximum_i8(1)533Input(Var|Rel)534/* data[6] bottom wheel (signed, positive clockwise) */535UsagePage_Consumer536Usage_Con_ACPan537Input(Var|Rel)538)539/*540* The kernel will drop reports that are bigger than the541* largest report specified in the HID descriptor.542* Therefore, our modified descriptor needs to have at least one543* HID report that is as long as, or longer than, the largest544* report in the original descriptor.545*546* This macro expands to a no-op report that is padded to the547* provided length.548*/549FixedSizeVendorReport(VENDOR_REPORT_LENGTH)550)551};552553SEC(HID_BPF_RDESC_FIXUP)554int BPF_PROG(hid_fix_rdesc_huion_kamvas16_gen3, struct hid_bpf_ctx *hid_ctx)555{556__u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, HID_MAX_DESCRIPTOR_SIZE /* size */);557__s32 rdesc_size = hid_ctx->size;558__u8 have_fw_id;559560if (!data)561return 0; /* EPERM check */562563have_fw_id = __builtin_memcmp(UDEV_PROP_HUION_FIRMWARE_ID,564EXPECTED_FIRMWARE_ID,565sizeof(EXPECTED_FIRMWARE_ID) - 1) == 0;566567if (have_fw_id) {568/*569* Tablet should be in vendor mode.570* Disable the unused devices571*/572if (rdesc_size == TABLET_DESCRIPTOR_LENGTH) {573__builtin_memcpy(data, disabled_rdesc_tablet,574sizeof(disabled_rdesc_tablet));575return sizeof(disabled_rdesc_tablet);576}577578if (rdesc_size == WHEEL_DESCRIPTOR_LENGTH) {579__builtin_memcpy(data, disabled_rdesc_wheel,580sizeof(disabled_rdesc_wheel));581return sizeof(disabled_rdesc_wheel);582}583}584585/*586* Regardless of which mode the tablet is in, always fix the vendor587* descriptor in case the udev property just happened to not be set588*/589if (rdesc_size == VENDOR_DESCRIPTOR_LENGTH) {590__builtin_memcpy(data, fixed_rdesc_vendor, sizeof(fixed_rdesc_vendor));591return sizeof(fixed_rdesc_vendor);592}593594return 0;595}596597SEC(HID_BPF_DEVICE_EVENT)598int BPF_PROG(hid_fix_event_huion_kamvas16_gen3, struct hid_bpf_ctx *hid_ctx)599{600__u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, VENDOR_REPORT_LENGTH /* size */);601602if (!data)603return 0; /* EPERM check */604605/* Handle vendor reports only */606if (hid_ctx->size != VENDOR_REPORT_LENGTH)607return 0;608if (data[0] != VENDOR_REPORT_ID)609return 0;610611__u8 report_subtype = (data[1] >> 4) & 0x0f;612613if (report_subtype == VENDOR_REPORT_SUBTYPE_PEN ||614report_subtype == VENDOR_REPORT_SUBTYPE_PEN_OUT) {615/* Invert Y tilt */616data[11] = -data[11];617618/*619* Rearrange the bytes of the report so that620* [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]621* will be arranged as622* [0, 1, 2, 3, 8, 4, 5, 6, 7, 9, 10, 11, 12, 13]623*/624__u8 x_24 = data[8];625626data[8] = data[7];627data[7] = data[6];628data[6] = data[5];629data[5] = data[4];630631data[4] = x_24;632633} else if (report_subtype == VENDOR_REPORT_SUBTYPE_BUTTONS ||634report_subtype == VENDOR_REPORT_SUBTYPE_WHEELS) {635struct pad_report {636__u8 report_id;637__u8 btn_stylus:1;638__u8 padding:7;639__u8 x;640__u8 y;641__u8 buttons;642__s8 top_wheel;643__s8 bottom_wheel;644} __attribute__((packed)) *pad_report;645646__s8 top_wheel = 0;647__s8 bottom_wheel = 0;648649switch (report_subtype) {650case VENDOR_REPORT_SUBTYPE_WHEELS:651/*652* The wheel direction byte is 1 for clockwise rotation653* and 2 for counter-clockwise.654* Change it to 1 and -1, respectively.655*/656switch (data[3]) {657case 1:658top_wheel = (data[5] == 1) ? 1 : -1;659break;660case 2:661bottom_wheel = (data[5] == 1) ? 1 : -1;662break;663}664break;665666case VENDOR_REPORT_SUBTYPE_BUTTONS:667/*668* If a button is already being held, ignore any new669* button event unless it's a release.670*671* The tablet only cleanly handles one button being held672* at a time, and trying to hold multiple buttons673* (particularly wheel+pad buttons) can result in sequences674* of reports that look like imaginary presses and releases.675*676* This is an imperfect way to filter out some of these677* reports.678*/679if (last_button_state != 0x00 && data[4] != 0x00)680break;681682last_button_state = data[4];683break;684}685686pad_report = (struct pad_report *)data;687688pad_report->report_id = CUSTOM_PAD_REPORT_ID;689pad_report->btn_stylus = 0;690pad_report->x = 0;691pad_report->y = 0;692pad_report->buttons = last_button_state;693pad_report->top_wheel = top_wheel;694pad_report->bottom_wheel = bottom_wheel;695696return sizeof(struct pad_report);697}698699return 0;700}701702HID_BPF_OPS(huion_kamvas16_gen3) = {703.hid_device_event = (void *)hid_fix_event_huion_kamvas16_gen3,704.hid_rdesc_fixup = (void *)hid_fix_rdesc_huion_kamvas16_gen3,705};706707SEC("syscall")708int probe(struct hid_bpf_probe_args *ctx)709{710switch (ctx->rdesc_size) {711case VENDOR_DESCRIPTOR_LENGTH:712case TABLET_DESCRIPTOR_LENGTH:713case WHEEL_DESCRIPTOR_LENGTH:714ctx->retval = 0;715break;716default:717ctx->retval = -EINVAL;718}719720return 0;721}722723char _license[] SEC("license") = "GPL";724725726