aboutsummaryrefslogtreecommitdiff
path: root/fastmouse.patch
blob: 7eb8e3b83020eff0235a90d7ea256267980f95dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
diff --git a/drivers/hid/fastmouse.h b/drivers/hid/fastmouse.h
new file mode 100644
index 000000000000..2383de88932f
--- /dev/null
+++ b/drivers/hid/fastmouse.h
@@ -0,0 +1,189 @@
+#ifndef FASTMOUSE_H_
+#define FASTMOUSE_H_
+
+// Mock functionality and structs for user space unit tests.
+#ifndef __KERNEL__
+#include <climits>
+
+typedef int __s32;
+struct input_dev {};
+enum event_type {
+    EV_REL,
+};
+enum movement_type {
+    REL_X,
+    REL_Y,
+};
+struct mouse_movement {
+    int x = 0;
+    int y = 0;
+};
+static struct mouse_movement movement = {
+    .x = 0,
+    .y = 0,
+};
+static void input_event(input_dev*, event_type, movement_type code, int value) {
+	movement.x += (code == REL_X) * value;
+	movement.y += (code == REL_Y) * value;
+}
+#endif
+
+extern void input_event_fastmouse_log(
+		struct input_dev*,
+		const unsigned int,
+		const unsigned int code,
+		__s32 value);
+extern void fastmouse_input_emit(struct input_dev *input);
+
+struct fastmouse_state {
+	int division;
+	long frame_x, frame_y;
+	long accum_x, accum_y;
+	int rise, run;
+	/* TODO
+	int polling_rate;
+	long accel;
+	*/
+};
+
+/* TODO (for accel)
+static unsigned __int128 i128_sqrt(const unsigned __int128 v) {
+	unsigned __int128 left = 0;
+	unsigned __int128 right = v;
+	while (left < right) {
+		const unsigned __int128 mid = left + ((right - left) / 2);
+		if (mid >= ~(u64)0 || mid * mid > v) {
+			right = mid;
+		} else if (mid * mid == v) {
+			return mid;
+		} else {
+			left = mid + 1;
+		}
+	}
+	return left != 0 ? left - 1 : 0;
+}
+*/
+
+#ifdef FASTMOUSE_IMPL
+static struct fastmouse_state fastmouse = {
+	.division = 1,
+	.frame_x = 0,
+	.frame_y = 0,
+	.accum_x = 0,
+	.accum_y = 0,
+	.rise = 0,
+	.run = INT_MAX,
+	/* TODO
+	.polling_rate = 4000,
+	.accel = 1,
+	*/
+};
+
+void input_event_fastmouse_log(
+		struct input_dev*,
+		const unsigned int,
+		const unsigned int code,
+		__s32 value) {
+	fastmouse.frame_x += (code == REL_X) * value;
+	fastmouse.frame_y += (code == REL_Y) * value;
+}
+
+void fastmouse_input_emit(struct input_dev *input) {
+	const long rise = fastmouse.rise / fastmouse.division;
+	const long run = fastmouse.run / fastmouse.division;
+	
+	fastmouse.accum_x += fastmouse.frame_x * run - fastmouse.frame_y * rise;
+	fastmouse.accum_y += fastmouse.frame_x * rise + fastmouse.frame_y * run;
+	
+	const long emit_x = fastmouse.accum_x / INT_MAX;
+	if (emit_x != 0) {
+		input_event(input, EV_REL, REL_X, (__s32)emit_x);
+	}
+	const long emit_y = fastmouse.accum_y / INT_MAX;
+	if (emit_y != 0) {
+		input_event(input, EV_REL, REL_Y, (__s32)emit_y);
+	}
+	fastmouse.accum_x %= INT_MAX;
+	fastmouse.accum_y %= INT_MAX;
+	
+	fastmouse.frame_x = fastmouse.frame_y = 0;
+}
+
+#ifdef __KERNEL__
+#include <linux/moduleparam.h>
+
+static int set_division(const char *val, const struct kernel_param *kp) {
+	const int ret = kstrtoint(val, 0, &fastmouse.division);
+	if (ret != 0 || fastmouse.division <= 0) {
+		return -EINVAL;
+	}
+	return ret;
+}
+static const struct kernel_param_ops division_ops = {
+	.set = set_division,
+	.get = param_get_int,
+};
+module_param_cb(division, &division_ops, &fastmouse.division, 0664);
+MODULE_PARM_DESC(division, "Mouse movement division amount (default: 1)");
+
+static int set_rise(const char *val, const struct kernel_param *kp) {
+	const int ret = kstrtoint(val, 0, &fastmouse.rise);
+	if (ret != 0) {
+		return -EINVAL;
+	}
+	return ret;
+}
+static const struct kernel_param_ops rise_ops = {
+	.set = set_rise,
+	.get = param_get_int,
+};
+module_param_cb(rise, &rise_ops, &fastmouse.rise, 0664);
+MODULE_PARM_DESC(rise, "Mouse movement rise amount (default: 0)");
+
+static int set_run(const char *val, const struct kernel_param *kp) {
+	const int ret = kstrtoint(val, 0, &fastmouse.run);
+	if (ret != 0) {
+		return -EINVAL;
+	}
+	return ret;
+}
+static const struct kernel_param_ops run_ops = {
+	.set = set_run,
+	.get = param_get_int,
+};
+module_param_cb(run, &run_ops, &fastmouse.run, 0664);
+MODULE_PARM_DESC(run, "Mouse movement run amount (default: INT_MAX)");
+
+/* TODO
+static int set_polling_rate(const char *val, const struct kernel_param *kp) {
+	const int ret = kstrtoint(val, 0, &fastmouse.polling_rate);
+	if (ret != 0) {
+		return -EINVAL;
+	}
+	return ret;
+}
+static const struct kernel_param_ops poll_ops = {
+	.set = set_polling_rate,
+	.get = param_get_int,
+};
+module_param_cb(polling_rate, &poll_ops, &fastmouse.polling_rate, 0664);
+MODULE_PARM_DESC(polling_rate, "Mouse polling rate (default: 4000)");
+
+static int set_accel(const char *val, const struct kernel_param *kp) {
+	const int ret = kstrtol(val, 0, &fastmouse.accel);
+	if (ret != 0) {
+		return -EINVAL;
+	}
+	return ret;
+}
+static const struct kernel_param_ops accel_ops = {
+	.set = set_accel,
+	.get = param_get_int,
+};
+module_param_cb(acceleration, &accel_ops, &fastmouse.accel, 0664);
+MODULE_PARM_DESC(acceleration, "Mouse acceleration (default: 1)");
+*/
+#endif
+#endif
+
+#endif
\ No newline at end of file
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 4497b50799db..9dc84229abe3 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -35,6 +35,8 @@
 
 #include "hid-ids.h"
 
+#include "fastmouse.h"
+
 /*
  * Version Information
  */
@@ -1680,6 +1682,8 @@ static void hid_process_report(struct hid_device *hid,
 	unsigned int a;
 	struct hid_field_entry *entry;
 	struct hid_field *field;
+	struct input_dev *fastmouse_dev = NULL;
+
 
 	/* first retrieve all incoming values in data */
 	for (a = 0; a < report->maxfield; a++)
@@ -1692,6 +1696,10 @@ static void hid_process_report(struct hid_device *hid,
 				    list) {
 			field = entry->field;
 
+			if (hid->type == HID_TYPE_USBMOUSE && field->hidinput) {
+				fastmouse_dev = field->hidinput->input;
+			}
+
 			if (field->flags & HID_MAIN_ITEM_VARIABLE)
 				hid_process_event(hid,
 						  field,
@@ -1721,6 +1729,13 @@ static void hid_process_report(struct hid_device *hid,
 				hid_input_array_field(hid, field, interrupt);
 		}
 	}
+
+	
+	if (fastmouse_dev) {
+		fastmouse_input_emit(fastmouse_dev);
+	}
+
+	
 }
 
 /*
diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index 9d80635a91eb..552bb63b9906 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -22,6 +22,10 @@
 
 #include "hid-ids.h"
 
+#define FASTMOUSE_IMPL
+#include "fastmouse.h"
+#undef FASTMOUSE_IMPL
+
 #define unk	KEY_UNKNOWN
 
 static const unsigned char hid_keyboard[256] = {
@@ -1505,6 +1509,7 @@ static void hid_report_set_tool(struct hid_report *report, struct input_dev *inp
 	report->tool = new_tool;
 }
 
+
 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
 {
 	struct input_dev *input;
@@ -1713,7 +1718,12 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
 	    (!test_bit(usage->code, input->key)) == value)
 		input_event(input, EV_MSC, MSC_SCAN, usage->hid);
 
-	input_event(input, usage->type, usage->code, value);
+	if (hid->type == HID_TYPE_USBMOUSE && usage->type == EV_REL) {
+		input_event_fastmouse_log(input, usage->type, usage->code, value);
+	} else {
+		input_event(input, usage->type, usage->code, value);
+	}
+
 
 	if ((field->flags & HID_MAIN_ITEM_RELATIVE) &&
 	    usage->type == EV_KEY && value) {