15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/device.h>
18 #include <linux/hid.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
26 static bool emulate_3button =
true;
30 static int middle_button_start = -350;
31 static int middle_button_stop = +350;
33 static bool emulate_scroll_wheel =
true;
37 static unsigned int scroll_speed = 32;
38 static int param_set_scroll_speed(
const char *
val,
struct kernel_param *kp) {
46 MODULE_PARM_DESC(scroll_speed,
"Scroll speed, value from 0 (slow) to 63 (fast)");
48 static bool scroll_acceleration =
false;
50 MODULE_PARM_DESC(scroll_acceleration,
"Accelerate sequential scroll events");
52 static bool report_undeciphered;
54 MODULE_PARM_DESC(report_undeciphered,
"Report undeciphered multi-touch state field using a MSC_RAW event");
56 #define TRACKPAD_REPORT_ID 0x28
57 #define MOUSE_REPORT_ID 0x29
58 #define DOUBLE_REPORT_ID 0xf7
65 #define TOUCH_STATE_MASK 0xf0
66 #define TOUCH_STATE_NONE 0x00
67 #define TOUCH_STATE_START 0x30
68 #define TOUCH_STATE_DRAG 0x40
70 #define SCROLL_ACCEL_DEFAULT 7
74 #define MOUSE_DIMENSION_X (float)9056
75 #define MOUSE_MIN_X -1100
76 #define MOUSE_MAX_X 1258
77 #define MOUSE_RES_X ((MOUSE_MAX_X - MOUSE_MIN_X) / (MOUSE_DIMENSION_X / 100))
78 #define MOUSE_DIMENSION_Y (float)5152
79 #define MOUSE_MIN_Y -1589
80 #define MOUSE_MAX_Y 2047
81 #define MOUSE_RES_Y ((MOUSE_MAX_Y - MOUSE_MIN_Y) / (MOUSE_DIMENSION_Y / 100))
83 #define TRACKPAD_DIMENSION_X (float)13000
84 #define TRACKPAD_MIN_X -2909
85 #define TRACKPAD_MAX_X 3167
86 #define TRACKPAD_RES_X \
87 ((TRACKPAD_MAX_X - TRACKPAD_MIN_X) / (TRACKPAD_DIMENSION_X / 100))
88 #define TRACKPAD_DIMENSION_Y (float)11000
89 #define TRACKPAD_MIN_Y -2456
90 #define TRACKPAD_MAX_Y 2565
91 #define TRACKPAD_RES_Y \
92 ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
130 for (ii = 0; ii < msc->
ntouches; ii++) {
132 if (msc->
touches[idx].size < 8) {
134 }
else if (touch >= 0) {
151 if (emulate_3button) {
160 }
else if (last_state != 0) {
162 }
else if ((
id = magicmouse_firm_touch(msc)) >= 0) {
164 if (x < middle_button_start)
166 else if (x > middle_button_stop)
178 if (state != last_state)
182 static void magicmouse_emit_touch(
struct magicmouse_sc *msc,
int raw_id,
u8 *tdata)
188 id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
189 x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
190 y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
191 size = tdata[5] & 0x3f;
192 orientation = (tdata[6] >> 2) - 32;
193 touch_major = tdata[3];
194 touch_minor = tdata[4];
198 id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
199 x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
200 y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
201 size = tdata[6] & 0x3f;
202 orientation = (tdata[7] >> 2) - 32;
203 touch_major = tdata[4];
204 touch_minor = tdata[5];
218 if (emulate_scroll_wheel) {
252 input_report_rel(input,
REL_WHEEL, step_y);
272 if (report_undeciphered) {
281 static int magicmouse_raw_event(
struct hid_device *hdev,
285 struct input_dev *input = msc->
input;
286 int x = 0, y = 0, ii, clicks = 0, npoints;
291 if (size < 4 || ((size - 4) % 9) != 0)
293 npoints = (size - 4) / 9;
295 for (ii = 0; ii < npoints; ii++)
296 magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
308 if (size < 6 || ((size - 6) % 8) != 0)
310 npoints = (size - 6) / 8;
312 for (ii = 0; ii < npoints; ii++)
313 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
319 x = (
int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
320 y = (
int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
333 magicmouse_raw_event(hdev, report, data + 2, data[1]);
334 magicmouse_raw_event(hdev, report, data + 2 + data[1],
342 magicmouse_emit_buttons(msc, clicks & 3);
343 input_report_rel(input,
REL_X, x);
344 input_report_rel(input,
REL_Y, y);
346 input_report_key(input,
BTN_MOUSE, clicks & 1);
354 static int magicmouse_setup_input(
struct input_dev *input,
struct hid_device *hdev)
369 if (emulate_scroll_wheel) {
438 input_set_events_per_packet(input, 60);
440 if (report_undeciphered) {
448 static int magicmouse_input_mapping(
struct hid_device *hdev,
465 static int magicmouse_probe(
struct hid_device *hdev,
475 hid_err(hdev,
"can't alloc magicmouse descriptor\n");
481 msc->
quirks =
id->driver_data;
482 hid_set_drvdata(hdev, msc);
484 ret = hid_parse(hdev);
486 hid_err(hdev,
"magicmouse hid parse failed\n");
492 hid_err(hdev,
"magicmouse hw start failed\n");
500 ret = magicmouse_setup_input(msc->
input, hdev);
502 hid_err(hdev,
"magicmouse setup input failed (%d)\n", ret);
518 hid_err(hdev,
"unable to register touch report\n");
534 if (ret != -
EIO && ret !=
sizeof(feature)) {
535 hid_err(hdev,
"unable to request touch data (%d)\n", ret);
547 static void magicmouse_remove(
struct hid_device *hdev)
564 static struct hid_driver magicmouse_driver = {
565 .name =
"magicmouse",
566 .id_table = magic_mice,
567 .probe = magicmouse_probe,
568 .remove = magicmouse_remove,
569 .raw_event = magicmouse_raw_event,
570 .input_mapping = magicmouse_input_mapping,
573 static int __init magicmouse_init(
void)
579 pr_err(
"can't register magicmouse driver\n");
584 static void __exit magicmouse_exit(
void)