1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2024 José Expósito
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_RAPOO 0x24AE
11 #define PID_M50 0x2015
12 #define RDESC_SIZE 186
13
14 HID_BPF_CONFIG(
15 HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_RAPOO, PID_M50)
16 );
17
18 /*
19 * The Rapoo M50 Plus Silent mouse has 2 side buttons in addition to the left,
20 * right and middle buttons. However, its original HID descriptor has a Usage
21 * Maximum of 3, preventing the side buttons to work. This HID-BPF driver
22 * changes that usage to 5.
23 *
24 * For reference, this is the original report descriptor:
25 *
26 * 0x05, 0x01, // Usage Page (Generic Desktop) 0
27 * 0x09, 0x02, // Usage (Mouse) 2
28 * 0xa1, 0x01, // Collection (Application) 4
29 * 0x85, 0x01, // Report ID (1) 6
30 * 0x09, 0x01, // Usage (Pointer) 8
31 * 0xa1, 0x00, // Collection (Physical) 10
32 * 0x05, 0x09, // Usage Page (Button) 12
33 * 0x19, 0x01, // Usage Minimum (1) 14
34 * 0x29, 0x03, // Usage Maximum (3) 16 <- change to 0x05
35 * 0x15, 0x00, // Logical Minimum (0) 18
36 * 0x25, 0x01, // Logical Maximum (1) 20
37 * 0x75, 0x01, // Report Size (1) 22
38 * 0x95, 0x05, // Report Count (5) 24
39 * 0x81, 0x02, // Input (Data,Var,Abs) 26
40 * 0x75, 0x03, // Report Size (3) 28
41 * 0x95, 0x01, // Report Count (1) 30
42 * 0x81, 0x01, // Input (Cnst,Arr,Abs) 32
43 * 0x05, 0x01, // Usage Page (Generic Desktop) 34
44 * 0x09, 0x30, // Usage (X) 36
45 * 0x09, 0x31, // Usage (Y) 38
46 * 0x16, 0x01, 0x80, // Logical Minimum (-32767) 40
47 * 0x26, 0xff, 0x7f, // Logical Maximum (32767) 43
48 * 0x75, 0x10, // Report Size (16) 46
49 * 0x95, 0x02, // Report Count (2) 48
50 * 0x81, 0x06, // Input (Data,Var,Rel) 50
51 * 0x09, 0x38, // Usage (Wheel) 52
52 * 0x15, 0x81, // Logical Minimum (-127) 54
53 * 0x25, 0x7f, // Logical Maximum (127) 56
54 * 0x75, 0x08, // Report Size (8) 58
55 * 0x95, 0x01, // Report Count (1) 60
56 * 0x81, 0x06, // Input (Data,Var,Rel) 62
57 * 0xc0, // End Collection 64
58 * 0xc0, // End Collection 65
59 * 0x05, 0x0c, // Usage Page (Consumer Devices) 66
60 * 0x09, 0x01, // Usage (Consumer Control) 68
61 * 0xa1, 0x01, // Collection (Application) 70
62 * 0x85, 0x02, // Report ID (2) 72
63 * 0x75, 0x10, // Report Size (16) 74
64 * 0x95, 0x01, // Report Count (1) 76
65 * 0x15, 0x01, // Logical Minimum (1) 78
66 * 0x26, 0x8c, 0x02, // Logical Maximum (652) 80
67 * 0x19, 0x01, // Usage Minimum (1) 83
68 * 0x2a, 0x8c, 0x02, // Usage Maximum (652) 85
69 * 0x81, 0x00, // Input (Data,Arr,Abs) 88
70 * 0xc0, // End Collection 90
71 * 0x05, 0x01, // Usage Page (Generic Desktop) 91
72 * 0x09, 0x80, // Usage (System Control) 93
73 * 0xa1, 0x01, // Collection (Application) 95
74 * 0x85, 0x03, // Report ID (3) 97
75 * 0x09, 0x82, // Usage (System Sleep) 99
76 * 0x09, 0x81, // Usage (System Power Down) 101
77 * 0x09, 0x83, // Usage (System Wake Up) 103
78 * 0x15, 0x00, // Logical Minimum (0) 105
79 * 0x25, 0x01, // Logical Maximum (1) 107
80 * 0x19, 0x01, // Usage Minimum (1) 109
81 * 0x29, 0x03, // Usage Maximum (3) 111
82 * 0x75, 0x01, // Report Size (1) 113
83 * 0x95, 0x03, // Report Count (3) 115
84 * 0x81, 0x02, // Input (Data,Var,Abs) 117
85 * 0x95, 0x05, // Report Count (5) 119
86 * 0x81, 0x01, // Input (Cnst,Arr,Abs) 121
87 * 0xc0, // End Collection 123
88 * 0x05, 0x01, // Usage Page (Generic Desktop) 124
89 * 0x09, 0x00, // Usage (Undefined) 126
90 * 0xa1, 0x01, // Collection (Application) 128
91 * 0x85, 0x05, // Report ID (5) 130
92 * 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 132
93 * 0x09, 0x01, // Usage (Vendor Usage 1) 135
94 * 0x15, 0x81, // Logical Minimum (-127) 137
95 * 0x25, 0x7f, // Logical Maximum (127) 139
96 * 0x75, 0x08, // Report Size (8) 141
97 * 0x95, 0x07, // Report Count (7) 143
98 * 0xb1, 0x02, // Feature (Data,Var,Abs) 145
99 * 0xc0, // End Collection 147
100 * 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 148
101 * 0x09, 0x0e, // Usage (Vendor Usage 0x0e) 151
102 * 0xa1, 0x01, // Collection (Application) 153
103 * 0x85, 0xba, // Report ID (186) 155
104 * 0x95, 0x1f, // Report Count (31) 157
105 * 0x75, 0x08, // Report Size (8) 159
106 * 0x26, 0xff, 0x00, // Logical Maximum (255) 161
107 * 0x15, 0x00, // Logical Minimum (0) 164
108 * 0x09, 0x01, // Usage (Vendor Usage 1) 166
109 * 0x91, 0x02, // Output (Data,Var,Abs) 168
110 * 0x85, 0xba, // Report ID (186) 170
111 * 0x95, 0x1f, // Report Count (31) 172
112 * 0x75, 0x08, // Report Size (8) 174
113 * 0x26, 0xff, 0x00, // Logical Maximum (255) 176
114 * 0x15, 0x00, // Logical Minimum (0) 179
115 * 0x09, 0x01, // Usage (Vendor Usage 1) 181
116 * 0x81, 0x02, // Input (Data,Var,Abs) 183
117 * 0xc0, // End Collection 185
118 */
119
SEC(HID_BPF_RDESC_FIXUP)120 SEC(HID_BPF_RDESC_FIXUP)
121 int BPF_PROG(hid_rdesc_fixup_rapoo_m50, struct hid_bpf_ctx *hctx)
122 {
123 __u8 *data = hid_bpf_get_data(hctx, 0, HID_MAX_DESCRIPTOR_SIZE);
124
125 if (!data)
126 return 0; /* EPERM check */
127
128 if (data[17] == 0x03)
129 data[17] = 0x05;
130
131 return 0;
132 }
133
134 HID_BPF_OPS(rapoo_m50) = {
135 .hid_rdesc_fixup = (void *)hid_rdesc_fixup_rapoo_m50,
136 };
137
138 SEC("syscall")
probe(struct hid_bpf_probe_args * ctx)139 int probe(struct hid_bpf_probe_args *ctx)
140 {
141 ctx->retval = ctx->rdesc_size != RDESC_SIZE;
142 if (ctx->retval)
143 ctx->retval = -EINVAL;
144
145 return 0;
146 }
147
148 char _license[] SEC("license") = "GPL";
149