Path: blob/master/drivers/hid/bpf/progs/Wacom__ArtPen.bpf.c
26285 views
// SPDX-License-Identifier: GPL-2.0-only1/* Copyright (c) 2024 Benjamin Tissoires2*/34#include "vmlinux.h"5#include "hid_bpf.h"6#include "hid_bpf_helpers.h"7#include <bpf/bpf_tracing.h>89#define VID_WACOM 0x056a10#define ART_PEN_ID 0x080411#define PID_INTUOS_PRO_2_M 0x03571213HID_BPF_CONFIG(14HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_WACOM, PID_INTUOS_PRO_2_M)15);1617/*18* This filter is here for the Art Pen stylus only:19* - when used on some Wacom devices (see the list of attached PIDs), this pen20* reports pressure every other events.21* - to solve that, given that we know that the next event will be the same as22* the current one, we can emulate a smoother pressure reporting by reporting23* the mean of the previous value and the current one.24*25* We are effectively delaying the pressure by one event every other event, but26* that's less of an annoyance compared to the chunkiness of the reported data.27*28* For example, let's assume the following set of events:29* <Tip switch 0> <X 0> <Y 0> <Pressure 0 > <Tooltype 0x0804>30* <Tip switch 1> <X 1> <Y 1> <Pressure 100 > <Tooltype 0x0804>31* <Tip switch 1> <X 2> <Y 2> <Pressure 100 > <Tooltype 0x0804>32* <Tip switch 1> <X 3> <Y 3> <Pressure 200 > <Tooltype 0x0804>33* <Tip switch 1> <X 4> <Y 4> <Pressure 200 > <Tooltype 0x0804>34* <Tip switch 0> <X 5> <Y 5> <Pressure 0 > <Tooltype 0x0804>35*36* The filter will report:37* <Tip switch 0> <X 0> <Y 0> <Pressure 0 > <Tooltype 0x0804>38* <Tip switch 1> <X 1> <Y 1> <Pressure * 50*> <Tooltype 0x0804>39* <Tip switch 1> <X 2> <Y 2> <Pressure 100 > <Tooltype 0x0804>40* <Tip switch 1> <X 3> <Y 3> <Pressure *150*> <Tooltype 0x0804>41* <Tip switch 1> <X 4> <Y 4> <Pressure 200 > <Tooltype 0x0804>42* <Tip switch 0> <X 5> <Y 5> <Pressure 0 > <Tooltype 0x0804>43*44*/4546struct wacom_params {47__u16 pid;48__u16 rdesc_len;49__u8 report_id;50__u8 report_len;51struct {52__u8 tip_switch;53__u8 pressure;54__u8 tool_type;55} offsets;56};5758/*59* Multiple device can support the same stylus, so60* we need to know which device has which offsets61*/62static const struct wacom_params devices[] = {63{64.pid = PID_INTUOS_PRO_2_M,65.rdesc_len = 949,66.report_id = 16,67.report_len = 27,68.offsets = {69.tip_switch = 1,70.pressure = 8,71.tool_type = 25,72},73},74};7576static struct wacom_params params = { 0 };7778/* HID-BPF reports a 64 bytes chunk anyway, so this ensures79* the verifier to know we are addressing the memory correctly80*/81#define PEN_REPORT_LEN 648283/* only odd frames are modified */84static bool odd;8586static __u16 prev_pressure;8788static inline void *get_bits(__u8 *data, unsigned int byte_offset)89{90return data + byte_offset;91}9293static inline __u16 *get_u16(__u8 *data, unsigned int offset)94{95return (__u16 *)get_bits(data, offset);96}9798static inline __u8 *get_u8(__u8 *data, unsigned int offset)99{100return (__u8 *)get_bits(data, offset);101}102103SEC(HID_BPF_DEVICE_EVENT)104int BPF_PROG(artpen_pressure_interpolate, struct hid_bpf_ctx *hctx)105{106__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, PEN_REPORT_LEN /* size */);107__u16 *pressure, *tool_type;108__u8 *tip_switch;109110if (!data)111return 0; /* EPERM check */112113if (data[0] != params.report_id ||114params.offsets.tip_switch >= PEN_REPORT_LEN ||115params.offsets.pressure >= PEN_REPORT_LEN - 1 ||116params.offsets.tool_type >= PEN_REPORT_LEN - 1)117return 0; /* invalid report or parameters */118119tool_type = get_u16(data, params.offsets.tool_type);120if (*tool_type != ART_PEN_ID)121return 0;122123tip_switch = get_u8(data, params.offsets.tip_switch);124if ((*tip_switch & 0x01) == 0) {125prev_pressure = 0;126odd = true;127return 0;128}129130pressure = get_u16(data, params.offsets.pressure);131132if (odd)133*pressure = (*pressure + prev_pressure) / 2;134135prev_pressure = *pressure;136odd = !odd;137138return 0;139}140141HID_BPF_OPS(wacom_artpen) = {142.hid_device_event = (void *)artpen_pressure_interpolate,143};144145SEC("syscall")146int probe(struct hid_bpf_probe_args *ctx)147{148struct hid_bpf_ctx *hid_ctx;149__u16 pid;150int i;151152/* get a struct hid_device to access the actual pid of the device */153hid_ctx = hid_bpf_allocate_context(ctx->hid);154if (!hid_ctx) {155ctx->retval = -ENODEV;156return -1; /* EPERM check */157}158pid = hid_ctx->hid->product;159160ctx->retval = -EINVAL;161162/* Match the given device with the list of known devices */163for (i = 0; i < ARRAY_SIZE(devices); i++) {164const struct wacom_params *device = &devices[i];165166if (device->pid == pid && device->rdesc_len == ctx->rdesc_size) {167params = *device;168ctx->retval = 0;169}170}171172hid_bpf_release_context(hid_ctx);173return 0;174}175176char _license[] SEC("license") = "GPL";177178179