1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2024 Benjamin Tissoires
3 */
4
5 #include "vmlinux.h"
6 #include "hid_bpf.h"
7 #include "hid_bpf_helpers.h"
8 #include <bpf/bpf_tracing.h>
9
10 #define VID_HUION 0x256C
11 #define PID_KAMVAS_PRO_19 0x006B
12 #define NAME_KAMVAS_PRO_19 "HUION Huion Tablet_GT1902"
13
14 #define TEST_PREFIX "uhid test "
15
16 HID_BPF_CONFIG(
17 HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8, VID_HUION, PID_KAMVAS_PRO_19),
18 );
19
20 bool prev_was_out_of_range;
21 bool in_eraser_mode;
22
23 /*
24 * We need to amend the report descriptor for the following:
25 * - the second button is reported through Secondary Tip Switch instead of Secondary Barrel Switch
26 * - the third button is reported through Invert, and we need some room to report it.
27 *
28 */
29 static const __u8 fixed_rdesc[] = {
30 0x05, 0x0d, // Usage Page (Digitizers) 0
31 0x09, 0x02, // Usage (Pen) 2
32 0xa1, 0x01, // Collection (Application) 4
33 0x85, 0x0a, // Report ID (10) 6
34 0x09, 0x20, // Usage (Stylus) 8
35 0xa1, 0x01, // Collection (Application) 10
36 0x09, 0x42, // Usage (Tip Switch) 12
37 0x09, 0x44, // Usage (Barrel Switch) 14
38 0x09, 0x5a, // Usage (Secondary Barrel Switch) 16 /* changed from Secondary Tip Switch */
39 0x09, 0x3c, // Usage (Invert) 18
40 0x09, 0x45, // Usage (Eraser) 20
41 0x15, 0x00, // Logical Minimum (0) 22
42 0x25, 0x01, // Logical Maximum (1) 24
43 0x75, 0x01, // Report Size (1) 26
44 0x95, 0x05, // Report Count (5) 28 /* changed (was 5) */
45 0x81, 0x02, // Input (Data,Var,Abs) 30
46 0x05, 0x09, // Usage Page (Button) /* inserted */
47 0x09, 0x4a, // Usage (0x4a) /* inserted to be translated as input usage 0x149: BTN_STYLUS3 */
48 0x95, 0x01, // Report Count (1) /* inserted */
49 0x81, 0x02, // Input (Data,Var,Abs) /* inserted */
50 0x05, 0x0d, // Usage Page (Digitizers) /* inserted */
51 0x09, 0x32, // Usage (In Range) 32
52 0x75, 0x01, // Report Size (1) 34
53 0x95, 0x01, // Report Count (1) 36
54 0x81, 0x02, // Input (Data,Var,Abs) 38
55 0x81, 0x03, // Input (Cnst,Var,Abs) 40
56 0x05, 0x01, // Usage Page (Generic Desktop) 42
57 0x09, 0x30, // Usage (X) 44
58 0x09, 0x31, // Usage (Y) 46
59 0x55, 0x0d, // Unit Exponent (-3) 48
60 0x65, 0x33, // Unit (EnglishLinear: in³) 50
61 0x26, 0xff, 0x7f, // Logical Maximum (32767) 52
62 0x35, 0x00, // Physical Minimum (0) 55
63 0x46, 0x00, 0x08, // Physical Maximum (2048) 57
64 0x75, 0x10, // Report Size (16) 60
65 0x95, 0x02, // Report Count (2) 62
66 0x81, 0x02, // Input (Data,Var,Abs) 64
67 0x05, 0x0d, // Usage Page (Digitizers) 66
68 0x09, 0x30, // Usage (Tip Pressure) 68
69 0x26, 0xff, 0x3f, // Logical Maximum (16383) 70
70 0x75, 0x10, // Report Size (16) 73
71 0x95, 0x01, // Report Count (1) 75
72 0x81, 0x02, // Input (Data,Var,Abs) 77
73 0x09, 0x3d, // Usage (X Tilt) 79
74 0x09, 0x3e, // Usage (Y Tilt) 81
75 0x15, 0xa6, // Logical Minimum (-90) 83
76 0x25, 0x5a, // Logical Maximum (90) 85
77 0x75, 0x08, // Report Size (8) 87
78 0x95, 0x02, // Report Count (2) 89
79 0x81, 0x02, // Input (Data,Var,Abs) 91
80 0xc0, // End Collection 93
81 0xc0, // End Collection 94
82 0x05, 0x0d, // Usage Page (Digitizers) 95
83 0x09, 0x04, // Usage (Touch Screen) 97
84 0xa1, 0x01, // Collection (Application) 99
85 0x85, 0x04, // Report ID (4) 101
86 0x09, 0x22, // Usage (Finger) 103
87 0xa1, 0x02, // Collection (Logical) 105
88 0x05, 0x0d, // Usage Page (Digitizers) 107
89 0x95, 0x01, // Report Count (1) 109
90 0x75, 0x06, // Report Size (6) 111
91 0x09, 0x51, // Usage (Contact Id) 113
92 0x15, 0x00, // Logical Minimum (0) 115
93 0x25, 0x3f, // Logical Maximum (63) 117
94 0x81, 0x02, // Input (Data,Var,Abs) 119
95 0x09, 0x42, // Usage (Tip Switch) 121
96 0x25, 0x01, // Logical Maximum (1) 123
97 0x75, 0x01, // Report Size (1) 125
98 0x95, 0x01, // Report Count (1) 127
99 0x81, 0x02, // Input (Data,Var,Abs) 129
100 0x75, 0x01, // Report Size (1) 131
101 0x95, 0x01, // Report Count (1) 133
102 0x81, 0x03, // Input (Cnst,Var,Abs) 135
103 0x05, 0x01, // Usage Page (Generic Desktop) 137
104 0x75, 0x10, // Report Size (16) 139
105 0x55, 0x0e, // Unit Exponent (-2) 141
106 0x65, 0x11, // Unit (SILinear: cm) 143
107 0x09, 0x30, // Usage (X) 145
108 0x26, 0xff, 0x7f, // Logical Maximum (32767) 147
109 0x35, 0x00, // Physical Minimum (0) 150
110 0x46, 0x15, 0x0c, // Physical Maximum (3093) 152
111 0x81, 0x42, // Input (Data,Var,Abs,Null) 155
112 0x09, 0x31, // Usage (Y) 157
113 0x26, 0xff, 0x7f, // Logical Maximum (32767) 159
114 0x46, 0xcb, 0x06, // Physical Maximum (1739) 162
115 0x81, 0x42, // Input (Data,Var,Abs,Null) 165
116 0x05, 0x0d, // Usage Page (Digitizers) 167
117 0x09, 0x30, // Usage (Tip Pressure) 169
118 0x26, 0xff, 0x1f, // Logical Maximum (8191) 171
119 0x75, 0x10, // Report Size (16) 174
120 0x95, 0x01, // Report Count (1) 176
121 0x81, 0x02, // Input (Data,Var,Abs) 178
122 0xc0, // End Collection 180
123 0x05, 0x0d, // Usage Page (Digitizers) 181
124 0x09, 0x22, // Usage (Finger) 183
125 0xa1, 0x02, // Collection (Logical) 185
126 0x05, 0x0d, // Usage Page (Digitizers) 187
127 0x95, 0x01, // Report Count (1) 189
128 0x75, 0x06, // Report Size (6) 191
129 0x09, 0x51, // Usage (Contact Id) 193
130 0x15, 0x00, // Logical Minimum (0) 195
131 0x25, 0x3f, // Logical Maximum (63) 197
132 0x81, 0x02, // Input (Data,Var,Abs) 199
133 0x09, 0x42, // Usage (Tip Switch) 201
134 0x25, 0x01, // Logical Maximum (1) 203
135 0x75, 0x01, // Report Size (1) 205
136 0x95, 0x01, // Report Count (1) 207
137 0x81, 0x02, // Input (Data,Var,Abs) 209
138 0x75, 0x01, // Report Size (1) 211
139 0x95, 0x01, // Report Count (1) 213
140 0x81, 0x03, // Input (Cnst,Var,Abs) 215
141 0x05, 0x01, // Usage Page (Generic Desktop) 217
142 0x75, 0x10, // Report Size (16) 219
143 0x55, 0x0e, // Unit Exponent (-2) 221
144 0x65, 0x11, // Unit (SILinear: cm) 223
145 0x09, 0x30, // Usage (X) 225
146 0x26, 0xff, 0x7f, // Logical Maximum (32767) 227
147 0x35, 0x00, // Physical Minimum (0) 230
148 0x46, 0x15, 0x0c, // Physical Maximum (3093) 232
149 0x81, 0x42, // Input (Data,Var,Abs,Null) 235
150 0x09, 0x31, // Usage (Y) 237
151 0x26, 0xff, 0x7f, // Logical Maximum (32767) 239
152 0x46, 0xcb, 0x06, // Physical Maximum (1739) 242
153 0x81, 0x42, // Input (Data,Var,Abs,Null) 245
154 0x05, 0x0d, // Usage Page (Digitizers) 247
155 0x09, 0x30, // Usage (Tip Pressure) 249
156 0x26, 0xff, 0x1f, // Logical Maximum (8191) 251
157 0x75, 0x10, // Report Size (16) 254
158 0x95, 0x01, // Report Count (1) 256
159 0x81, 0x02, // Input (Data,Var,Abs) 258
160 0xc0, // End Collection 260
161 0x05, 0x0d, // Usage Page (Digitizers) 261
162 0x09, 0x56, // Usage (Scan Time) 263
163 0x55, 0x00, // Unit Exponent (0) 265
164 0x65, 0x00, // Unit (None) 267
165 0x27, 0xff, 0xff, 0xff, 0x7f, // Logical Maximum (2147483647) 269
166 0x95, 0x01, // Report Count (1) 274
167 0x75, 0x20, // Report Size (32) 276
168 0x81, 0x02, // Input (Data,Var,Abs) 278
169 0x09, 0x54, // Usage (Contact Count) 280
170 0x25, 0x7f, // Logical Maximum (127) 282
171 0x95, 0x01, // Report Count (1) 284
172 0x75, 0x08, // Report Size (8) 286
173 0x81, 0x02, // Input (Data,Var,Abs) 288
174 0x75, 0x08, // Report Size (8) 290
175 0x95, 0x08, // Report Count (8) 292
176 0x81, 0x03, // Input (Cnst,Var,Abs) 294
177 0x85, 0x05, // Report ID (5) 296
178 0x09, 0x55, // Usage (Contact Max) 298
179 0x25, 0x0a, // Logical Maximum (10) 300
180 0x75, 0x08, // Report Size (8) 302
181 0x95, 0x01, // Report Count (1) 304
182 0xb1, 0x02, // Feature (Data,Var,Abs) 306
183 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 308
184 0x09, 0xc5, // Usage (Vendor Usage 0xc5) 311
185 0x85, 0x06, // Report ID (6) 313
186 0x15, 0x00, // Logical Minimum (0) 315
187 0x26, 0xff, 0x00, // Logical Maximum (255) 317
188 0x75, 0x08, // Report Size (8) 320
189 0x96, 0x00, 0x01, // Report Count (256) 322
190 0xb1, 0x02, // Feature (Data,Var,Abs) 325
191 0xc0, // End Collection 327
192 };
193
SEC(HID_BPF_RDESC_FIXUP)194 SEC(HID_BPF_RDESC_FIXUP)
195 int BPF_PROG(hid_fix_rdesc_huion_kamvas_pro_19, struct hid_bpf_ctx *hctx)
196 {
197 __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, HID_MAX_DESCRIPTOR_SIZE /* size */);
198
199 if (!data)
200 return 0; /* EPERM check */
201
202 __builtin_memcpy(data, fixed_rdesc, sizeof(fixed_rdesc));
203
204 return sizeof(fixed_rdesc);
205 }
206
207 /*
208 * This tablet reports the 3rd button through invert, but this conflict
209 * with the normal eraser mode.
210 * Fortunately, before entering eraser mode, (so Invert = 1),
211 * the tablet always sends an out-of-proximity event.
212 * So we can detect that single event and:
213 * - if there was none but the invert bit was toggled: this is the
214 * third button
215 * - if there was this out-of-proximity event, we are entering
216 * eraser mode, and we will until the next out-of-proximity.
217 */
SEC(HID_BPF_DEVICE_EVENT)218 SEC(HID_BPF_DEVICE_EVENT)
219 int BPF_PROG(kamvas_pro_19_fix_3rd_button, struct hid_bpf_ctx *hctx)
220 {
221 __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 10 /* size */);
222
223 if (!data)
224 return 0; /* EPERM check */
225
226 if (data[0] != 0x0a) /* not the pen report ID */
227 return 0;
228
229 /* stylus is out of range */
230 if (!(data[1] & 0x40)) {
231 prev_was_out_of_range = true;
232 in_eraser_mode = false;
233 return 0;
234 }
235
236 /* going into eraser mode (Invert = 1) only happens after an
237 * out of range event
238 */
239 if (prev_was_out_of_range && (data[1] & 0x18))
240 in_eraser_mode = true;
241
242 /* eraser mode works fine */
243 if (in_eraser_mode)
244 return 0;
245
246 /* copy the Invert bit reported for the 3rd button in bit 7 */
247 if (data[1] & 0x08)
248 data[1] |= 0x20;
249
250 /* clear Invert bit now that it was copied */
251 data[1] &= 0xf7;
252
253 prev_was_out_of_range = false;
254
255 return 0;
256 }
257
258 HID_BPF_OPS(huion_Kamvas_pro_19) = {
259 .hid_rdesc_fixup = (void *)hid_fix_rdesc_huion_kamvas_pro_19,
260 .hid_device_event = (void *)kamvas_pro_19_fix_3rd_button,
261 };
262
263 SEC("syscall")
probe(struct hid_bpf_probe_args * ctx)264 int probe(struct hid_bpf_probe_args *ctx)
265 {
266 ctx->retval = ctx->rdesc_size != 328;
267 if (ctx->retval)
268 ctx->retval = -EINVAL;
269
270 /* ensure the kernel isn't fixed already */
271 if (ctx->rdesc[17] != 0x43) /* Secondary Tip Switch */
272 ctx->retval = -EINVAL;
273
274 struct hid_bpf_ctx *hctx = hid_bpf_allocate_context(ctx->hid);
275
276 if (!hctx) {
277 return ctx->retval = -EINVAL;
278 return 0;
279 }
280
281 const char *name = hctx->hid->name;
282
283 /* strip out TEST_PREFIX */
284 if (!__builtin_memcmp(name, TEST_PREFIX, sizeof(TEST_PREFIX) - 1))
285 name += sizeof(TEST_PREFIX) - 1;
286
287 if (__builtin_memcmp(name, NAME_KAMVAS_PRO_19, sizeof(NAME_KAMVAS_PRO_19)))
288 ctx->retval = -EINVAL;
289
290 hid_bpf_release_context(hctx);
291
292 return 0;
293 }
294
295 char _license[] SEC("license") = "GPL";
296