Commit fde4ce06 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'hid-for-linus-2025120201' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid

Pull HID updates from Jiri Kosina:

 - Proper mapping of HID_GD_Z to ABS_DISTANCE for stylus/pen types of
   devices (Ping Cheng)

 - Power management/hibernation improvements in intel-ish (Zhang Lixu)

 - Improved support for several Logitech devices, e.g. G Pro X
   Superlight 2, new iteration of Lighspeed receiver, G13, G510 (Nathan
   Rossi, Mavroudis Chatzilazaridis, Leo L Schwab, Hans de Goede)

 - Support for UcLogic XP-PEN Artist 24 Pro (Joshua Goins)

 - WinWing Orion2 throttle support improvement (Ivan Gorinov)

 - other assorted small fixes and device ID additions

* tag 'hid-for-linus-2025120201' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid: (37 commits)
  drivers: hid: renegotiate resolution multipliers with device after reset
  HID: evision: Fix Report Descriptor for Evision Wireless Receiver 320f:226f
  HID: logitech-dj: Fix probe failure when used with KVM
  HID: logitech-dj: Remove duplicate error logging
  HID: logitech-dj: Add support for G Pro X Superlight 2 receiver
  selftests/hid-tablet: add ABS_DISTANCE test for stylus/pen
  HID: input: map HID_GD_Z to ABS_DISTANCE for stylus/pen
  HID: bpf: fix typo in HID usage table
  HID: bpf: add the Huion Kamvas 27 Pro
  HID: bpf: add heuristics to the Huion Inspiroy 2S eraser button
  HID: bpf: Add support for XP-Pen Deco02
  HID: bpf: Add support for the XP-Pen Deco 01 V3
  HID: bpf: Add support for the Waltop Batteryless Tablet
  HID: bpf: Add fixup for Logitech SpaceNavigator variants
  HID: bpf: support for Huion Kamvas 16 Gen 3
  HID: bpf: add support for Huion Kamvas 13 (Gen 3) (model GS1333)
  HID: bpf: Add support for the Inspiroy 2M
  Documentation: hid-alps: Format DataByte* subsection headings
  Documentation: hid-alps: Fix packet format section headings
  HID: nintendo: add WQ_PERCPU to alloc_workqueue users
  ...
parents d1b46f53 8af1d3fe
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ To read/write to RAM, need to send a command to the device.
The command format is as below.

DataByte(SET_REPORT)
~~~~~~~~~~~~~~~~~~~~

=====	======================
Byte1	Command Byte
@@ -89,6 +90,7 @@ Value Byte is writing data when you send the write commands.
When you read RAM, there is no meaning.

DataByte(GET_REPORT)
~~~~~~~~~~~~~~~~~~~~

=====	======================
Byte1	Response Byte
@@ -104,8 +106,10 @@ Read value is stored in Value Byte.


Packet Format
-------------

Touchpad data byte
------------------
~~~~~~~~~~~~~~~~~~


======= ======= ======= ======= ======= ======= ======= ======= =====
@@ -156,7 +160,7 @@ Zsn_6-0(7bit):


StickPointer data byte
----------------------
~~~~~~~~~~~~~~~~~~~~~~

======= ======= ======= ======= ======= ======= ======= ======= =====
-	b7	b6	b5	b4	b3	b2	b1	b0
+3 −0
Original line number Diff line number Diff line
@@ -383,6 +383,7 @@ config HID_EVISION
	help
	Support for some EVision keyboards. Note that this is needed only when
	applying customization using userspace programs.
	Support for some EVision devices requiring report descriptor fixups.

config HID_EZKEY
	tristate "Ezkey BTC 8193 keyboard"
@@ -1318,6 +1319,8 @@ config HID_WINWING
	help
	  Support for WinWing Orion2 throttle base with the following grips:

	    * TGRIP-15E
	    * TGRIP-15EX
	    * TGRIP-16EX
	    * TGRIP-18

+563 −0
Original line number Diff line number Diff line
// 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 <bpf/bpf_tracing.h>

#define VID_HUION 0x256C
#define PID_INSPIROY_2_M 0x0067

HID_BPF_CONFIG(
	HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_HUION, PID_INSPIROY_2_M),
);

/* 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_T21j_221221
 */
char EXPECTED_FIRMWARE_ID[] = "HUION_T21k_";

/* 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
 * and the dial as keyboard combinations. 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 works for both modes. 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 Tablet_H641P
 * # 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 rdesc does nothing until the tablet is switched to raw mode, see
 * https://github.com/whot/huion-switcher
 *
 *
 * Second hidraw node is the Pen. This one sends events until the tablet is
 * switched to raw mode, then it's mute.
 *
 * # Report descriptor length: 93 bytes
 * # 0x05, 0x0d,          // Usage Page (Digitizers)                   0
 * # 0x09, 0x02,          // Usage (Pen)                               2
 * # 0xa1, 0x01,          // Collection (Application)                  4
 * # 0x85, 0x0a,          //   Report ID (10)                          6
 * # 0x09, 0x20,          //   Usage (Stylus)                          8
 * # 0xa1, 0x01,          //   Collection (Application)                10
 * # 0x09, 0x42,          //     Usage (Tip Switch)                    12
 * # 0x09, 0x44,          //     Usage (Barrel Switch)                 14
 * # 0x09, 0x45,          //     Usage (Eraser)                        16
 * # 0x09, 0x3c,          //     Usage (Invert)                        18 <-- has no Invert eraser
 * # 0x15, 0x00,          //     Logical Minimum (0)                   20
 * # 0x25, 0x01,          //     Logical Maximum (1)                   22
 * # 0x75, 0x01,          //     Report Size (1)                       24
 * # 0x95, 0x06,          //     Report Count (6)                      26
 * # 0x81, 0x02,          //     Input (Data,Var,Abs)                  28
 * # 0x09, 0x32,          //     Usage (In Range)                      30
 * # 0x75, 0x01,          //     Report Size (1)                       32
 * # 0x95, 0x01,          //     Report Count (1)                      34
 * # 0x81, 0x02,          //     Input (Data,Var,Abs)                  36
 * # 0x81, 0x03,          //     Input (Cnst,Var,Abs)                  38
 * # 0x05, 0x01,          //     Usage Page (Generic Desktop)          40
 * # 0x09, 0x30,          //     Usage (X)                             42
 * # 0x09, 0x31,          //     Usage (Y)                             44
 * # 0x55, 0x0d,          //     Unit Exponent (-3)                    46 <-- change to -2
 * # 0x65, 0x33,          //     Unit (EnglishLinear: in³)             48 <-- change in³ to in
 * # 0x26, 0xff, 0x7f,    //     Logical Maximum (32767)               50
 * # 0x35, 0x00,          //     Physical Minimum (0)                  53
 * # 0x46, 0x00, 0x08,    //     Physical Maximum (2048)               55 <-- invalid size
 * # 0x75, 0x10,          //     Report Size (16)                      58
 * # 0x95, 0x02,          //     Report Count (2)                      60
 * # 0x81, 0x02,          //     Input (Data,Var,Abs)                  62
 * # 0x05, 0x0d,          //     Usage Page (Digitizers)               64
 * # 0x09, 0x30,          //     Usage (Tip Pressure)                  66
 * # 0x26, 0xff, 0x1f,    //     Logical Maximum (8191)                68
 * # 0x75, 0x10,          //     Report Size (16)                      71
 * # 0x95, 0x01,          //     Report Count (1)                      73
 * # 0x81, 0x02,          //     Input (Data,Var,Abs)                  75
 * # 0x09, 0x3d,          //     Usage (X Tilt)                        77 <-- No tilt reported
 * # 0x09, 0x3e,          //     Usage (Y Tilt)                        79
 * # 0x15, 0x81,          //     Logical Minimum (-127)                81
 * # 0x25, 0x7f,          //     Logical Maximum (127)                 83
 * # 0x75, 0x08,          //     Report Size (8)                       85
 * # 0x95, 0x02,          //     Report Count (2)                      87
 * # 0x81, 0x02,          //     Input (Data,Var,Abs)                  89
 * # 0xc0,                //   End Collection                          91
 * # 0xc0,                // End Collection                            92
 * R: 93 05 0d 09 02 a1 01 85 0a 09 20 a1 01 09 42 09 44 09 45 09 3c 15 00 25 01 7501 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 ff7f 35 00 46 00 08 75 10 95 02 81 02 05 0d 09 30 26 ff 1f 75 10 95 01 81 02 09 3d09 3e 15 81 25 7f 75 08 95 02 81 02 c0 c0
 *
 * Third hidraw node is the pad which sends a combination of keyboard shortcuts until
 * the tablet is switched to raw mode, then it's mute:
 *
 * # Report descriptor length: 65 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
 * # 0x19, 0x00,          //   UsageMinimum (0)                        48
 * # 0x2a, 0x3c, 0x02,    //   UsageMaximum (572)                      50
 * # 0x15, 0x00,          //   Logical Minimum (0)                     53
 * # 0x26, 0x3c, 0x02,    //   Logical Maximum (572)                   55
 * # 0x95, 0x01,          //   Report Count (1)                        58
 * # 0x75, 0x10,          //   Report Size (16)                        60
 * # 0x81, 0x00,          //   Input (Data,Arr,Abs)                    62
 * # 0xc0,                // End Collection                            64
 * R: 65 05 01 09 06 a1 01 85 03 05 07 19 e0 29 e7 15 00 25 01 75 01 95 08 81 02 0507 19 00 29 ff 26 ff 00 75 08 95 06 81 00 c0 05 0c 09 01 a1 01 85 04 19 00 2a 3c02 15 00 26 3c 02 95 01 75 10 81 00 c0
 * N: HUION Huion Tablet_H641P
 */

#define PAD_REPORT_DESCRIPTOR_LENGTH 133
#define PEN_REPORT_DESCRIPTOR_LENGTH 93
#define VENDOR_REPORT_DESCRIPTOR_LENGTH 36
#define PAD_REPORT_ID 3
#define PEN_REPORT_ID 10
#define VENDOR_REPORT_ID 8
#define PAD_REPORT_LENGTH 8
#define PEN_REPORT_LENGTH 10
#define VENDOR_REPORT_LENGTH 12


__u16 last_button_state;

static const __u8 fixed_rdesc_pad[] = {
	UsagePage_GenericDesktop
	Usage_GD_Keypad
	CollectionApplication(
		// -- Byte 0 in report
		ReportId(PAD_REPORT_ID)
		LogicalMinimum_i8(0)
		LogicalMaximum_i8(1)
		UsagePage_Digitizers
		Usage_Dig_TabletFunctionKeys
		CollectionPhysical(
			// Byte 1 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 2/3 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 4 in report is the wheel
			Usage_GD_Wheel
			LogicalMinimum_i8(-1)
			LogicalMaximum_i8(1)
			ReportCount(1)
			ReportSize(8)
			Input(Var|Rel)
			// Byte 5 is the button state
			UsagePage_Button
			UsageMinimum_i8(0x1)
			UsageMaximum_i8(0x8)
			LogicalMinimum_i8(0x1)
			LogicalMaximum_i8(0x8)
			ReportCount(1)
			ReportSize(8)
			Input(Arr|Abs)
		)
		// Make sure we match our original report length
		FixedSizeVendorReport(PAD_REPORT_LENGTH)
	)
};

static const __u8 fixed_rdesc_pen[] = {
	UsagePage_Digitizers
	Usage_Dig_Pen
	CollectionApplication(
		// -- Byte 0 in report
		ReportId(PEN_REPORT_ID)
		Usage_Dig_Pen
		CollectionPhysical(
			// -- Byte 1 in report
			Usage_Dig_TipSwitch
			Usage_Dig_BarrelSwitch
			Usage_Dig_SecondaryBarrelSwitch // maps eraser to BTN_STYLUS2
			LogicalMinimum_i8(0)
			LogicalMaximum_i8(1)
			ReportSize(1)
			ReportCount(3)
			Input(Var|Abs)
			ReportCount(4)  // Padding
			Input(Const)
			Usage_Dig_InRange
			ReportCount(1)
			Input(Var|Abs)
			ReportSize(16)
			ReportCount(1)
			PushPop(
				UsagePage_GenericDesktop
				Unit(cm)
				UnitExponent(-1)
				PhysicalMinimum_i16(0)
				PhysicalMaximum_i16(160)
				LogicalMinimum_i16(0)
				LogicalMaximum_i16(32767)
				Usage_GD_X
				Input(Var|Abs) // Bytes 2+3
				PhysicalMinimum_i16(0)
				PhysicalMaximum_i16(100)
				LogicalMinimum_i16(0)
				LogicalMaximum_i16(32767)
				Usage_GD_Y
				Input(Var|Abs) // Bytes 4+5
			)
			UsagePage_Digitizers
			Usage_Dig_TipPressure
			LogicalMinimum_i16(0)
			LogicalMaximum_i16(8191)
			Input(Var|Abs) // Byte 6+7
			// Two bytes padding so we don't need to change the report at all
			ReportSize(8)
			ReportCount(2)
			Input(Const) // Byte 6+7
		)
	)
};

static const __u8 fixed_rdesc_vendor[] = {
	UsagePage_Digitizers
	Usage_Dig_Pen
	CollectionApplication(
		// Byte 0
		// We leave the pen on the vendor report ID
		ReportId(VENDOR_REPORT_ID)
		Usage_Dig_Pen
		CollectionPhysical(
			// Byte 1 are the buttons
			LogicalMinimum_i8(0)
			LogicalMaximum_i8(1)
			ReportSize(1)
			Usage_Dig_TipSwitch
			Usage_Dig_BarrelSwitch
			Usage_Dig_SecondaryBarrelSwitch
			ReportCount(3)
			Input(Var|Abs)
			ReportCount(4) // Padding
			Input(Const)
			Usage_Dig_InRange
			ReportCount(1)
			Input(Var|Abs)
			ReportSize(16)
			ReportCount(1)
			PushPop(
				UsagePage_GenericDesktop
				Unit(cm)
				UnitExponent(-1)
				// Note: reported logical range differs
				// from the pen report ID for x and y
				LogicalMinimum_i16(0)
				LogicalMaximum_i16(32000)
				PhysicalMinimum_i16(0)
				PhysicalMaximum_i16(160)
				// Bytes 2/3 in report
				Usage_GD_X
				Input(Var|Abs)
				LogicalMinimum_i16(0)
				LogicalMaximum_i16(20000)
				PhysicalMinimum_i16(0)
				PhysicalMaximum_i16(100)
				// Bytes 4/5 in report
				Usage_GD_Y
				Input(Var|Abs)
			)
			// Bytes 6/7 in report
			LogicalMinimum_i16(0)
			LogicalMaximum_i16(8192)
			Usage_Dig_TipPressure
			Input(Var|Abs)
		)
	)
	UsagePage_GenericDesktop
	Usage_GD_Keypad
	CollectionApplication(
		// Byte 0
		ReportId(PAD_REPORT_ID)
		LogicalMinimum_i8(0)
		LogicalMaximum_i8(1)
		UsagePage_Digitizers
		Usage_Dig_TabletFunctionKeys
		CollectionPhysical(
			// Byte 1 are the buttons
			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
			ReportCount(2)
			ReportSize(8)
			Input(Var|Abs)
			// Bytes 4 and 5 are the button state
			UsagePage_Button
			UsageMinimum_i8(0x1)
			UsageMaximum_i8(0xa)
			LogicalMinimum_i8(0x0)
			LogicalMaximum_i8(0x1)
			ReportCount(10)
			ReportSize(1)
			Input(Var|Abs)
			Usage_i8(0x31) // maps to BTN_SOUTH
			ReportCount(1)
			Input(Var|Abs)
			ReportCount(5)
			Input(Const)
			// 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)
	)
};

static const __u8 disabled_rdesc_pen[] = {
	FixedSizeVendorReport(PEN_REPORT_LENGTH)
};

static const __u8 disabled_rdesc_pad[] = {
	FixedSizeVendorReport(PAD_REPORT_LENGTH)
};

SEC(HID_BPF_RDESC_FIXUP)
int BPF_PROG(hid_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/pen 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);
		}

		__builtin_memcpy(data, fixed_rdesc_pad, sizeof(fixed_rdesc_pad));
		return sizeof(fixed_rdesc_pad);
	}
	if (rdesc_size == PEN_REPORT_DESCRIPTOR_LENGTH) {
		if (have_fw_id) {
			__builtin_memcpy(data, disabled_rdesc_pen, sizeof(disabled_rdesc_pen));
			return sizeof(disabled_rdesc_pen);
		}

		__builtin_memcpy(data, fixed_rdesc_pen, sizeof(fixed_rdesc_pen));
		return sizeof(fixed_rdesc_pen);
	}
	/* 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(inspiroy_2_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 default mode */
	if (data[0] == PAD_REPORT_ID) {
		/* Nicely enough, this device only supports one button down at a time so
		 * the reports are easy to match. Buttons numbered from the top
		 *   Button released: 03 00 00 00 00 00 00 00
		 *   Button 1: 03 00 05 00 00 00 00 00 -> b
		 *   Button 2: 03 07 11 00 00 00 00 00 -> Ctrl Shift N
		 *   Button 3: 03 00 08 00 00 00 00 00 -> e
		 *   Button 4: 03 00 0c 00 00 00 00 00 -> i
		 *   Button 5: 03 00 2c 00 00 00 00 00 -> space
		 *   Button 6: 03 01 08 00 00 00 00 00 -> Ctrl E
		 *   Button 7: 03 01 16 00 00 00 00 00 -> Ctrl S
		 *   Button 8: 03 05 1d 00 00 00 00 00 -> Ctrl Alt Z
		 *
		 *   Wheel down: 03 01 2d 00 00 00 00 00 -> Ctrl -
		 *   Wheel up:   03 01 2e 00 00 00 00 00 -> Ctrl =
		 */
		__u8 button = 0;
		__u8 wheel = 0;

		switch (data[1] << 8 | data[2]) {
		case 0x0000:
			break;
		case 0x0005:
			button = 1;
			break;
		case 0x0711:
			button = 2;
			break;
		case 0x0008:
			button = 3;
			break;
		case 0x000c:
			button = 4;
			break;
		case 0x002c:
			button = 5;
			break;
		case 0x0108:
			button = 6;
			break;
		case 0x0116:
			button = 7;
			break;
		case 0x051d:
			button = 8;
			break;
		case 0x012d:
			wheel = -1;
			break;
		case 0x012e:
			wheel = 1;
			break;
		}

		__u8 report[6] = {PAD_REPORT_ID, 0x0, 0x0, 0x0, wheel, button};

		__builtin_memcpy(data, report, sizeof(report));
		return sizeof(report);
	}

	/* Nothing to do for the PEN_REPORT_ID, it's already mapped */

	/* Only sent if tablet is in raw mode */
	if (data[0] == VENDOR_REPORT_ID) {
		/* Pad reports */
		if (data[1] & 0x20) {
			/* See fixed_rdesc_pad */
			struct pad_report {
				__u8 report_id;
				__u8 btn_stylus;
				__u8 x;
				__u8 y;
				__u16 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] and data[5] are the buttons, mapped correctly */
				last_button_state = data[4] | (data[5] << 8);
				wheel = 0; // wheel
			}

			pad_report = (struct pad_report *)data;

			pad_report->report_id = PAD_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);
		}

		/* Pen reports need nothing done */
	}

	return 0;
}

HID_BPF_OPS(inspiroy_2) = {
	.hid_device_event = (void *)inspiroy_2_fix_events,
	.hid_rdesc_fixup = (void *)hid_fix_rdesc,
};

SEC("syscall")
int probe(struct hid_bpf_probe_args *ctx)
{
	switch (ctx->rdesc_size) {
	case PAD_REPORT_DESCRIPTOR_LENGTH:
	case PEN_REPORT_DESCRIPTOR_LENGTH:
	case VENDOR_REPORT_DESCRIPTOR_LENGTH:
		ctx->retval = 0;
		break;
	default:
		ctx->retval = -EINVAL;
	}

	return 0;
}

char _license[] SEC("license") = "GPL";
+27 −2
Original line number Diff line number Diff line
@@ -163,6 +163,9 @@ char EXPECTED_FIRMWARE_ID[] = "HUION_T21j_";


__u8 last_button_state;
__u8 last_tip_state;
__u8 last_sec_barrel_state;
__u8 force_tip_down_count;

static const __u8 fixed_rdesc_pad[] = {
	UsagePage_GenericDesktop
@@ -522,9 +525,31 @@ int BPF_PROG(inspiroy_2_fix_events, struct hid_bpf_ctx *hctx)
			pad_report->wheel = wheel;

			return sizeof(struct pad_report);
		} else if (data[1] & 0x80) { /* Pen reports with InRange 1 */
			__u8 tip_state = data[1] & 0x1;
			__u8 sec_barrel_state = data[1] & 0x4;

			if (force_tip_down_count > 0) {
				data[1] |= 0x1;
				--force_tip_down_count;
				if (tip_state)
					force_tip_down_count = 0;
			}

		/* Pen reports need nothing done */
			/* Tip was down and we just pressed or released the
			 * secondary barrel switch (the physical eraser
			 * button). The device will send up to 4
			 * reports with Tip Switch 0 and sometimes
			 * this report has Tip Switch 0.
			 */
			if (last_tip_state &&
			    last_sec_barrel_state != sec_barrel_state) {
				force_tip_down_count = 4;
				data[1] |= 0x1;
			}
			last_tip_state = tip_state;
			last_sec_barrel_state = sec_barrel_state;
		}
	}

	return 0;
+5 −1
Original line number Diff line number Diff line
@@ -9,12 +9,15 @@

#define VID_HUION 0x256C
#define PID_KAMVAS_PRO_19 0x006B
#define PID_KAMVAS_PRO_27 0x006c
#define NAME_KAMVAS_PRO_19 "HUION Huion Tablet_GT1902"
#define NAME_KAMVAS_PRO_27 "HUION Huion Tablet_GT2701"

#define TEST_PREFIX "uhid test "

HID_BPF_CONFIG(
	HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8, VID_HUION, PID_KAMVAS_PRO_19),
	HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8, VID_HUION, PID_KAMVAS_PRO_27),
);

bool prev_was_out_of_range;
@@ -351,7 +354,8 @@ int probe(struct hid_bpf_probe_args *ctx)
	if (!__builtin_memcmp(name, TEST_PREFIX, sizeof(TEST_PREFIX) - 1))
		name += sizeof(TEST_PREFIX) - 1;

	if (__builtin_memcmp(name, NAME_KAMVAS_PRO_19, sizeof(NAME_KAMVAS_PRO_19)))
	if (__builtin_memcmp(name, NAME_KAMVAS_PRO_19, sizeof(NAME_KAMVAS_PRO_19)) &&
	    __builtin_memcmp(name, NAME_KAMVAS_PRO_27, sizeof(NAME_KAMVAS_PRO_27)))
		ctx->retval = -EINVAL;

	hid_bpf_release_context(hctx);
Loading