1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2022 Benjamin Tissoires
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
8 #include "hid_bpf_helpers.h"
10 #define HID_UP_BUTTON 0x0009
11 #define HID_GD_WHEEL 0x0038
13 SEC("fmod_ret/hid_bpf_device_event")
14 int BPF_PROG(hid_event, struct hid_bpf_ctx *hctx)
16 __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 9 /* size */);
19 return 0; /* EPERM check */
35 /* 72 == 360 / 5 -> 1 report every 5 degrees */
39 struct haptic_syscall_args {
44 static __u8 haptic_data[8];
47 int set_haptic(struct haptic_syscall_args *args)
49 struct hid_bpf_ctx *ctx;
50 const size_t size = sizeof(haptic_data);
54 if (size > sizeof(haptic_data))
55 return -7; /* -E2BIG */
57 ctx = hid_bpf_allocate_context(args->hid);
59 return -1; /* EPERM check */
61 haptic_data[0] = 1; /* report ID */
63 ret = hid_bpf_hw_request(ctx, haptic_data, size, HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
65 bpf_printk("probed/remove event ret value: %d", ret);
66 bpf_printk("buf: %02x %02x %02x",
70 bpf_printk(" %02x %02x %02x",
74 bpf_printk(" %02x %02x",
78 /* whenever resolution multiplier is not 3600, we have the fixed report descriptor */
79 res = (u16 *)&haptic_data[1];
81 // haptic_data[1] = 72; /* resolution multiplier */
82 // haptic_data[2] = 0; /* resolution multiplier */
83 // haptic_data[3] = 0; /* Repeat Count */
84 haptic_data[4] = 3; /* haptic Auto Trigger */
85 // haptic_data[5] = 5; /* Waveform Cutoff Time */
86 // haptic_data[6] = 80; /* Retrigger Period */
87 // haptic_data[7] = 0; /* Retrigger Period */
92 ret = hid_bpf_hw_request(ctx, haptic_data, size, HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
94 bpf_printk("set haptic ret value: %d -> %d", ret, haptic_data[4]);
98 hid_bpf_release_context(ctx);
103 /* Convert REL_DIAL into REL_WHEEL */
104 SEC("fmod_ret/hid_bpf_rdesc_fixup")
105 int BPF_PROG(hid_rdesc_fixup, struct hid_bpf_ctx *hctx)
107 __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 4096 /* size */);
111 return 0; /* EPERM check */
113 /* Convert TOUCH into a button */
114 data[31] = HID_UP_BUTTON;
117 /* Convert REL_DIAL into REL_WHEEL */
118 data[45] = HID_GD_WHEEL;
120 /* Change Resolution Multiplier */
121 phys = (__u16 *)&data[61];
123 res = (__u16 *)&data[66];
126 /* Convert X,Y from Abs to Rel */
133 char _license[] SEC("license") = "GPL";
134 u32 _version SEC("version") = 1;