xref: /linux/drivers/hid/bpf/progs/IOGEAR__Kaliber-MMOmentum.bpf.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2023 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_IOGEAR 0x258A /* VID is shared with SinoWealth and Glorious and prob others */
11 #define PID_MOMENTUM 0x0027
12 
13 HID_BPF_CONFIG(
14 	HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_IOGEAR, PID_MOMENTUM)
15 );
16 
17 /*
18  * The IOGear Kaliber Gaming MMOmentum Pro mouse has multiple buttons (12)
19  * but only 5 are accessible out of the box because the report descriptor
20  * marks the other buttons as constants.
21  * We just fix the report descriptor to enable those missing 7 buttons.
22  */
23 
24 SEC(HID_BPF_RDESC_FIXUP)
25 int BPF_PROG(hid_fix_rdesc, struct hid_bpf_ctx *hctx)
26 {
27 	const u8 offsets[] = {84, 112, 140};
28 	__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 4096 /* size */);
29 
30 	if (!data)
31 		return 0; /* EPERM check */
32 
33 	/* if not Keyboard */
34 	if (data[3] != 0x06)
35 		return 0;
36 
37 	for (int idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
38 		u8 offset = offsets[idx];
39 
40 		/* if Input (Cnst,Var,Abs) , make it Input (Data,Var,Abs) */
41 		if (data[offset] == 0x81 && data[offset + 1] == 0x03)
42 			data[offset + 1] = 0x02;
43 	}
44 
45 	return 0;
46 }
47 
48 HID_BPF_OPS(iogear_kaliber_momentum) = {
49 	.hid_rdesc_fixup = (void *)hid_fix_rdesc,
50 };
51 
52 SEC("syscall")
53 int probe(struct hid_bpf_probe_args *ctx)
54 {
55 	/* only bind to the keyboard interface */
56 	ctx->retval = ctx->rdesc_size != 213;
57 	if (ctx->retval)
58 		ctx->retval = -EINVAL;
59 
60 	return 0;
61 }
62 
63 char _license[] SEC("license") = "GPL";
64