1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2024 Benjamin Tissoires
7 #include "hid_bpf_helpers.h"
8 #include <bpf/bpf_tracing.h>
10 #define VID_WACOM 0x056a
11 #define ART_PEN_ID 0x0804
12 #define PID_INTUOS_PRO_2_M 0x0357
15 HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_WACOM, PID_INTUOS_PRO_2_M)
19 * This filter is here for the Art Pen stylus only:
20 * - when used on some Wacom devices (see the list of attached PIDs), this pen
21 * reports pressure every other events.
22 * - to solve that, given that we know that the next event will be the same as
23 * the current one, we can emulate a smoother pressure reporting by reporting
24 * the mean of the previous value and the current one.
26 * We are effectively delaying the pressure by one event every other event, but
27 * that's less of an annoyance compared to the chunkiness of the reported data.
29 * For example, let's assume the following set of events:
30 * <Tip switch 0> <X 0> <Y 0> <Pressure 0 > <Tooltype 0x0804>
31 * <Tip switch 1> <X 1> <Y 1> <Pressure 100 > <Tooltype 0x0804>
32 * <Tip switch 1> <X 2> <Y 2> <Pressure 100 > <Tooltype 0x0804>
33 * <Tip switch 1> <X 3> <Y 3> <Pressure 200 > <Tooltype 0x0804>
34 * <Tip switch 1> <X 4> <Y 4> <Pressure 200 > <Tooltype 0x0804>
35 * <Tip switch 0> <X 5> <Y 5> <Pressure 0 > <Tooltype 0x0804>
37 * The filter will report:
38 * <Tip switch 0> <X 0> <Y 0> <Pressure 0 > <Tooltype 0x0804>
39 * <Tip switch 1> <X 1> <Y 1> <Pressure * 50*> <Tooltype 0x0804>
40 * <Tip switch 1> <X 2> <Y 2> <Pressure 100 > <Tooltype 0x0804>
41 * <Tip switch 1> <X 3> <Y 3> <Pressure *150*> <Tooltype 0x0804>
42 * <Tip switch 1> <X 4> <Y 4> <Pressure 200 > <Tooltype 0x0804>
43 * <Tip switch 0> <X 5> <Y 5> <Pressure 0 > <Tooltype 0x0804>
60 * Multiple device can support the same stylus, so
61 * we need to know which device has which offsets
63 static const struct wacom_params devices[] = {
65 .pid = PID_INTUOS_PRO_2_M,
77 static struct wacom_params params = { 0 };
79 /* HID-BPF reports a 64 bytes chunk anyway, so this ensures
80 * the verifier to know we are addressing the memory correctly
82 #define PEN_REPORT_LEN 64
84 /* only odd frames are modified */
87 static __u16 prev_pressure;
89 static inline void *get_bits(__u8 *data, unsigned int byte_offset)
91 return data + byte_offset;
94 static inline __u16 *get_u16(__u8 *data, unsigned int offset)
96 return (__u16 *)get_bits(data, offset);
99 static inline __u8 *get_u8(__u8 *data, unsigned int offset)
101 return (__u8 *)get_bits(data, offset);
104 SEC("fmod_ret/hid_bpf_device_event")
105 int BPF_PROG(artpen_pressure_interpolate, struct hid_bpf_ctx *hctx)
107 __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, PEN_REPORT_LEN /* size */);
108 __u16 *pressure, *tool_type;
112 return 0; /* EPERM check */
114 if (data[0] != params.report_id ||
115 params.offsets.tip_switch >= PEN_REPORT_LEN ||
116 params.offsets.pressure >= PEN_REPORT_LEN - 1 ||
117 params.offsets.tool_type >= PEN_REPORT_LEN - 1)
118 return 0; /* invalid report or parameters */
120 tool_type = get_u16(data, params.offsets.tool_type);
121 if (*tool_type != ART_PEN_ID)
124 tip_switch = get_u8(data, params.offsets.tip_switch);
125 if ((*tip_switch & 0x01) == 0) {
131 pressure = get_u16(data, params.offsets.pressure);
134 *pressure = (*pressure + prev_pressure) / 2;
136 prev_pressure = *pressure;
143 int probe(struct hid_bpf_probe_args *ctx)
145 struct hid_bpf_ctx *hid_ctx;
149 /* get a struct hid_device to access the actual pid of the device */
150 hid_ctx = hid_bpf_allocate_context(ctx->hid);
152 ctx->retval = -ENODEV;
153 return -1; /* EPERM check */
155 pid = hid_ctx->hid->product;
157 ctx->retval = -EINVAL;
159 /* Match the given device with the list of known devices */
160 for (i = 0; i < ARRAY_SIZE(devices); i++) {
161 const struct wacom_params *device = &devices[i];
163 if (device->pid == pid && device->rdesc_len == ctx->rdesc_size) {
169 hid_bpf_release_context(hid_ctx);
173 char _license[] SEC("license") = "GPL";