1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Apple "Magic" Wireless Mouse driver
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/device.h>
15 #include <linux/hid.h>
16 #include <linux/input/mt.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
23 static bool emulate_3button = true;
24 module_param(emulate_3button, bool, 0644);
25 MODULE_PARM_DESC(emulate_3button, "Emulate a middle button");
27 static int middle_button_start = -350;
28 static int middle_button_stop = +350;
30 static bool emulate_scroll_wheel = true;
31 module_param(emulate_scroll_wheel, bool, 0644);
32 MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
34 static unsigned int scroll_speed = 32;
35 static int param_set_scroll_speed(const char *val,
36 const struct kernel_param *kp) {
38 if (!val || kstrtoul(val, 0, &speed) || speed > 63)
43 module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644);
44 MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)");
46 static bool scroll_acceleration = false;
47 module_param(scroll_acceleration, bool, 0644);
48 MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
50 static bool report_undeciphered;
51 module_param(report_undeciphered, bool, 0644);
52 MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
54 #define TRACKPAD_REPORT_ID 0x28
55 #define TRACKPAD2_USB_REPORT_ID 0x02
56 #define TRACKPAD2_BT_REPORT_ID 0x31
57 #define MOUSE_REPORT_ID 0x29
58 #define MOUSE2_REPORT_ID 0x12
59 #define DOUBLE_REPORT_ID 0xf7
60 /* These definitions are not precise, but they're close enough. (Bits
61 * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
62 * to be some kind of bit mask -- 0x20 may be a near-field reading,
63 * and 0x40 is actual contact, and 0x10 may be a start/stop or change
66 #define TOUCH_STATE_MASK 0xf0
67 #define TOUCH_STATE_NONE 0x00
68 #define TOUCH_STATE_START 0x30
69 #define TOUCH_STATE_DRAG 0x40
71 /* Number of high-resolution events for each low-resolution detent. */
72 #define SCROLL_HR_STEPS 10
73 #define SCROLL_HR_MULT (120 / SCROLL_HR_STEPS)
74 #define SCROLL_HR_THRESHOLD 90 /* units */
75 #define SCROLL_ACCEL_DEFAULT 7
77 /* Touch surface information. Dimension is in hundredths of a mm, min and max
79 #define MOUSE_DIMENSION_X (float)9056
80 #define MOUSE_MIN_X -1100
81 #define MOUSE_MAX_X 1258
82 #define MOUSE_RES_X ((MOUSE_MAX_X - MOUSE_MIN_X) / (MOUSE_DIMENSION_X / 100))
83 #define MOUSE_DIMENSION_Y (float)5152
84 #define MOUSE_MIN_Y -1589
85 #define MOUSE_MAX_Y 2047
86 #define MOUSE_RES_Y ((MOUSE_MAX_Y - MOUSE_MIN_Y) / (MOUSE_DIMENSION_Y / 100))
88 #define TRACKPAD_DIMENSION_X (float)13000
89 #define TRACKPAD_MIN_X -2909
90 #define TRACKPAD_MAX_X 3167
91 #define TRACKPAD_RES_X \
92 ((TRACKPAD_MAX_X - TRACKPAD_MIN_X) / (TRACKPAD_DIMENSION_X / 100))
93 #define TRACKPAD_DIMENSION_Y (float)11000
94 #define TRACKPAD_MIN_Y -2456
95 #define TRACKPAD_MAX_Y 2565
96 #define TRACKPAD_RES_Y \
97 ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
99 #define TRACKPAD2_DIMENSION_X (float)16000
100 #define TRACKPAD2_MIN_X -3678
101 #define TRACKPAD2_MAX_X 3934
102 #define TRACKPAD2_RES_X \
103 ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
104 #define TRACKPAD2_DIMENSION_Y (float)11490
105 #define TRACKPAD2_MIN_Y -2478
106 #define TRACKPAD2_MAX_Y 2587
107 #define TRACKPAD2_RES_Y \
108 ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
111 * struct magicmouse_sc - Tracks Magic Mouse-specific data.
112 * @input: Input device through which we report events.
113 * @quirks: Currently unused.
114 * @ntouches: Number of touches in most recent touch report.
115 * @scroll_accel: Number of consecutive scroll motions.
116 * @scroll_jiffies: Time of last scroll motion.
117 * @touches: Most recent data for a touch, indexed by tracking ID.
118 * @tracking_ids: Mapping of current touch input data to @touches.
120 struct magicmouse_sc {
121 struct input_dev *input;
122 unsigned long quirks;
126 unsigned long scroll_jiffies;
136 bool scroll_x_active;
137 bool scroll_y_active;
139 int tracking_ids[16];
141 struct hid_device *hdev;
142 struct delayed_work work;
145 static int magicmouse_firm_touch(struct magicmouse_sc *msc)
150 /* If there is only one "firm" touch, set touch to its
153 for (ii = 0; ii < msc->ntouches; ii++) {
154 int idx = msc->tracking_ids[ii];
155 if (msc->touches[idx].size < 8) {
156 /* Ignore this touch. */
157 } else if (touch >= 0) {
168 static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
170 int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
171 test_bit(BTN_RIGHT, msc->input->key) << 1 |
172 test_bit(BTN_MIDDLE, msc->input->key) << 2;
174 if (emulate_3button) {
177 /* If some button was pressed before, keep it held
178 * down. Otherwise, if there's exactly one firm
179 * touch, use that to override the mouse's guess.
182 /* The button was released. */
183 } else if (last_state != 0) {
185 } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
186 int x = msc->touches[id].x;
187 if (x < middle_button_start)
189 else if (x > middle_button_stop)
193 } /* else: we keep the mouse's guess */
195 input_report_key(msc->input, BTN_MIDDLE, state & 4);
198 input_report_key(msc->input, BTN_LEFT, state & 1);
199 input_report_key(msc->input, BTN_RIGHT, state & 2);
201 if (state != last_state)
202 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
205 static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
207 struct input_dev *input = msc->input;
208 int id, x, y, size, orientation, touch_major, touch_minor, state, down;
211 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
212 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
213 id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
214 x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
215 y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
216 size = tdata[5] & 0x3f;
217 orientation = (tdata[6] >> 2) - 32;
218 touch_major = tdata[3];
219 touch_minor = tdata[4];
220 state = tdata[7] & TOUCH_STATE_MASK;
221 down = state != TOUCH_STATE_NONE;
222 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
224 x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
225 y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
227 orientation = (tdata[8] >> 5) - 4;
228 touch_major = tdata[4];
229 touch_minor = tdata[5];
231 state = tdata[3] & 0xC0;
232 down = state == 0x80;
233 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
234 id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
235 x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
236 y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
237 size = tdata[6] & 0x3f;
238 orientation = (tdata[7] >> 2) - 32;
239 touch_major = tdata[4];
240 touch_minor = tdata[5];
241 state = tdata[8] & TOUCH_STATE_MASK;
242 down = state != TOUCH_STATE_NONE;
245 /* Store tracking ID and other fields. */
246 msc->tracking_ids[raw_id] = id;
247 msc->touches[id].x = x;
248 msc->touches[id].y = y;
249 msc->touches[id].size = size;
251 /* If requested, emulate a scroll wheel by detecting small
252 * vertical touch motions.
254 if (emulate_scroll_wheel && (input->id.product !=
255 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) {
256 unsigned long now = jiffies;
257 int step_x = msc->touches[id].scroll_x - x;
258 int step_y = msc->touches[id].scroll_y - y;
261 ((64 - (int)scroll_speed) * msc->scroll_accel) /
264 int step_x_hr = msc->touches[id].scroll_x_hr - x;
265 int step_y_hr = msc->touches[id].scroll_y_hr - y;
267 /* Calculate and apply the scroll motion. */
269 case TOUCH_STATE_START:
270 msc->touches[id].scroll_x = x;
271 msc->touches[id].scroll_y = y;
272 msc->touches[id].scroll_x_hr = x;
273 msc->touches[id].scroll_y_hr = y;
274 msc->touches[id].scroll_x_active = false;
275 msc->touches[id].scroll_y_active = false;
277 /* Reset acceleration after half a second. */
278 if (scroll_acceleration && time_before(now,
279 msc->scroll_jiffies + HZ / 2))
280 msc->scroll_accel = max_t(int,
281 msc->scroll_accel - 1, 1);
283 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
286 case TOUCH_STATE_DRAG:
287 step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
289 msc->touches[id].scroll_x -= step_x *
290 (64 - scroll_speed) * msc->scroll_accel;
291 msc->scroll_jiffies = now;
292 input_report_rel(input, REL_HWHEEL, -step_x);
295 step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
297 msc->touches[id].scroll_y -= step_y *
298 (64 - scroll_speed) * msc->scroll_accel;
299 msc->scroll_jiffies = now;
300 input_report_rel(input, REL_WHEEL, step_y);
303 if (!msc->touches[id].scroll_x_active &&
304 abs(step_x_hr) > SCROLL_HR_THRESHOLD) {
305 msc->touches[id].scroll_x_active = true;
306 msc->touches[id].scroll_x_hr = x;
310 step_x_hr /= step_hr;
311 if (step_x_hr != 0 &&
312 msc->touches[id].scroll_x_active) {
313 msc->touches[id].scroll_x_hr -= step_x_hr *
315 input_report_rel(input,
317 -step_x_hr * SCROLL_HR_MULT);
320 if (!msc->touches[id].scroll_y_active &&
321 abs(step_y_hr) > SCROLL_HR_THRESHOLD) {
322 msc->touches[id].scroll_y_active = true;
323 msc->touches[id].scroll_y_hr = y;
327 step_y_hr /= step_hr;
328 if (step_y_hr != 0 &&
329 msc->touches[id].scroll_y_active) {
330 msc->touches[id].scroll_y_hr -= step_y_hr *
332 input_report_rel(input,
334 step_y_hr * SCROLL_HR_MULT);
343 input_mt_slot(input, id);
344 input_mt_report_slot_state(input, MT_TOOL_FINGER, down);
346 /* Generate the input events for this touch. */
348 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major << 2);
349 input_report_abs(input, ABS_MT_TOUCH_MINOR, touch_minor << 2);
350 input_report_abs(input, ABS_MT_ORIENTATION, -orientation);
351 input_report_abs(input, ABS_MT_POSITION_X, x);
352 input_report_abs(input, ABS_MT_POSITION_Y, y);
354 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
355 input_report_abs(input, ABS_MT_PRESSURE, pressure);
357 if (report_undeciphered) {
358 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
359 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
360 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
361 else if (input->id.product !=
362 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
363 input_event(input, EV_MSC, MSC_RAW, tdata[8]);
368 static int magicmouse_raw_event(struct hid_device *hdev,
369 struct hid_report *report, u8 *data, int size)
371 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
372 struct input_dev *input = msc->input;
373 int x = 0, y = 0, ii, clicks = 0, npoints;
376 case TRACKPAD_REPORT_ID:
377 case TRACKPAD2_BT_REPORT_ID:
378 /* Expect four bytes of prefix, and N*9 bytes of touch data. */
379 if (size < 4 || ((size - 4) % 9) != 0)
381 npoints = (size - 4) / 9;
383 hid_warn(hdev, "invalid size value (%d) for TRACKPAD_REPORT_ID\n",
388 for (ii = 0; ii < npoints; ii++)
389 magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
393 /* The following bits provide a device specific timestamp. They
396 * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
399 case TRACKPAD2_USB_REPORT_ID:
400 /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
401 if (size < 12 || ((size - 12) % 9) != 0)
403 npoints = (size - 12) / 9;
405 hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
410 for (ii = 0; ii < npoints; ii++)
411 magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
415 case MOUSE_REPORT_ID:
416 /* Expect six bytes of prefix, and N*8 bytes of touch data. */
417 if (size < 6 || ((size - 6) % 8) != 0)
419 npoints = (size - 6) / 8;
421 hid_warn(hdev, "invalid size value (%d) for MOUSE_REPORT_ID\n",
426 for (ii = 0; ii < npoints; ii++)
427 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
429 /* When emulating three-button mode, it is important
430 * to have the current touch information before
431 * generating a click event.
433 x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
434 y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
437 /* The following bits provide a device specific timestamp. They
440 * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
443 case MOUSE2_REPORT_ID:
444 /* Size is either 8 or (14 + 8 * N) */
445 if (size != 8 && (size < 14 || (size - 14) % 8 != 0))
447 npoints = (size - 14) / 8;
449 hid_warn(hdev, "invalid size value (%d) for MOUSE2_REPORT_ID\n",
454 for (ii = 0; ii < npoints; ii++)
455 magicmouse_emit_touch(msc, ii, data + ii * 8 + 14);
457 /* When emulating three-button mode, it is important
458 * to have the current touch information before
459 * generating a click event.
461 x = (int)((data[3] << 24) | (data[2] << 16)) >> 16;
462 y = (int)((data[5] << 24) | (data[4] << 16)) >> 16;
465 /* The following bits provide a device specific timestamp. They
468 * ts = data[11] >> 6 | data[12] << 2 | data[13] << 10;
471 case DOUBLE_REPORT_ID:
472 /* Sometimes the trackpad sends two touch reports in one
475 magicmouse_raw_event(hdev, report, data + 2, data[1]);
476 magicmouse_raw_event(hdev, report, data + 2 + data[1],
483 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
484 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
485 magicmouse_emit_buttons(msc, clicks & 3);
486 input_report_rel(input, REL_X, x);
487 input_report_rel(input, REL_Y, y);
488 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
489 input_mt_sync_frame(input);
490 input_report_key(input, BTN_MOUSE, clicks & 1);
491 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
492 input_report_key(input, BTN_MOUSE, clicks & 1);
493 input_mt_report_pointer_emulation(input, true);
500 static int magicmouse_event(struct hid_device *hdev, struct hid_field *field,
501 struct hid_usage *usage, __s32 value)
503 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
504 if (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 &&
505 field->report->id == MOUSE2_REPORT_ID) {
507 * magic_mouse_raw_event has done all the work. Skip hidinput.
509 * Specifically, hidinput may modify BTN_LEFT and BTN_RIGHT,
510 * breaking emulate_3button.
517 static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
522 __set_bit(EV_KEY, input->evbit);
524 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
525 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
526 __set_bit(BTN_LEFT, input->keybit);
527 __set_bit(BTN_RIGHT, input->keybit);
529 __set_bit(BTN_MIDDLE, input->keybit);
531 __set_bit(EV_REL, input->evbit);
532 __set_bit(REL_X, input->relbit);
533 __set_bit(REL_Y, input->relbit);
534 if (emulate_scroll_wheel) {
535 __set_bit(REL_WHEEL, input->relbit);
536 __set_bit(REL_HWHEEL, input->relbit);
537 __set_bit(REL_WHEEL_HI_RES, input->relbit);
538 __set_bit(REL_HWHEEL_HI_RES, input->relbit);
540 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
541 /* setting the device name to ensure the same driver settings
542 * get loaded, whether connected through bluetooth or USB
544 input->name = "Apple Inc. Magic Trackpad 2";
546 __clear_bit(EV_MSC, input->evbit);
547 __clear_bit(BTN_0, input->keybit);
548 __clear_bit(BTN_RIGHT, input->keybit);
549 __clear_bit(BTN_MIDDLE, input->keybit);
550 __set_bit(BTN_MOUSE, input->keybit);
551 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
552 __set_bit(BTN_TOOL_FINGER, input->keybit);
554 mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
556 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
557 /* input->keybit is initialized with incorrect button info
558 * for Magic Trackpad. There really is only one physical
559 * button (BTN_LEFT == BTN_MOUSE). Make sure we don't
560 * advertise buttons that don't exist...
562 __clear_bit(BTN_RIGHT, input->keybit);
563 __clear_bit(BTN_MIDDLE, input->keybit);
564 __set_bit(BTN_MOUSE, input->keybit);
565 __set_bit(BTN_TOOL_FINGER, input->keybit);
566 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
567 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
568 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
569 __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
570 __set_bit(BTN_TOUCH, input->keybit);
571 __set_bit(INPUT_PROP_POINTER, input->propbit);
572 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
576 __set_bit(EV_ABS, input->evbit);
578 error = input_mt_init_slots(input, 16, mt_flags);
581 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
583 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
586 /* Note: Touch Y position from the device is inverted relative
587 * to how pointer motion is reported (and relative to how USB
588 * HID recommends the coordinates work). This driver keeps
589 * the origin at the same position, and just uses the additive
590 * inverse of the reported Y.
592 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
593 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
594 input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
595 input_set_abs_params(input, ABS_MT_POSITION_X,
596 MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
597 input_set_abs_params(input, ABS_MT_POSITION_Y,
598 MOUSE_MIN_Y, MOUSE_MAX_Y, 4, 0);
600 input_abs_set_res(input, ABS_MT_POSITION_X,
602 input_abs_set_res(input, ABS_MT_POSITION_Y,
604 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
605 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
606 input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
607 input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
608 input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
609 TRACKPAD2_MAX_X, 0, 0);
610 input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
611 TRACKPAD2_MAX_Y, 0, 0);
612 input_set_abs_params(input, ABS_MT_POSITION_X,
613 TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
614 input_set_abs_params(input, ABS_MT_POSITION_Y,
615 TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
617 input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
618 input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
619 input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
620 input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
621 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
622 input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
623 input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
624 TRACKPAD_MAX_X, 4, 0);
625 input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
626 TRACKPAD_MAX_Y, 4, 0);
627 input_set_abs_params(input, ABS_MT_POSITION_X,
628 TRACKPAD_MIN_X, TRACKPAD_MAX_X, 4, 0);
629 input_set_abs_params(input, ABS_MT_POSITION_Y,
630 TRACKPAD_MIN_Y, TRACKPAD_MAX_Y, 4, 0);
632 input_abs_set_res(input, ABS_X, TRACKPAD_RES_X);
633 input_abs_set_res(input, ABS_Y, TRACKPAD_RES_Y);
634 input_abs_set_res(input, ABS_MT_POSITION_X,
636 input_abs_set_res(input, ABS_MT_POSITION_Y,
640 input_set_events_per_packet(input, 60);
642 if (report_undeciphered &&
643 input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
644 __set_bit(EV_MSC, input->evbit);
645 __set_bit(MSC_RAW, input->mscbit);
649 * hid-input may mark device as using autorepeat, but neither
650 * the trackpad, nor the mouse actually want it.
652 __clear_bit(EV_REP, input->evbit);
657 static int magicmouse_input_mapping(struct hid_device *hdev,
658 struct hid_input *hi, struct hid_field *field,
659 struct hid_usage *usage, unsigned long **bit, int *max)
661 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
664 msc->input = hi->input;
666 /* Magic Trackpad does not give relative data after switching to MT */
667 if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
668 hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
669 field->flags & HID_MAIN_ITEM_RELATIVE)
675 static int magicmouse_input_configured(struct hid_device *hdev,
676 struct hid_input *hi)
679 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
682 ret = magicmouse_setup_input(msc->input, hdev);
684 hid_err(hdev, "magicmouse setup input failed (%d)\n", ret);
685 /* clean msc->input to notify probe() of the failure */
693 static int magicmouse_enable_multitouch(struct hid_device *hdev)
696 const u8 feature_mt[] = { 0xD7, 0x01 };
697 const u8 feature_mt_mouse2[] = { 0xF1, 0x02, 0x01 };
698 const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
699 const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
704 if (hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
705 if (hdev->vendor == BT_VENDOR_ID_APPLE) {
706 feature_size = sizeof(feature_mt_trackpad2_bt);
707 feature = feature_mt_trackpad2_bt;
708 } else { /* USB_VENDOR_ID_APPLE */
709 feature_size = sizeof(feature_mt_trackpad2_usb);
710 feature = feature_mt_trackpad2_usb;
712 } else if (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
713 feature_size = sizeof(feature_mt_mouse2);
714 feature = feature_mt_mouse2;
716 feature_size = sizeof(feature_mt);
717 feature = feature_mt;
720 buf = kmemdup(feature, feature_size, GFP_KERNEL);
724 ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
725 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
730 static void magicmouse_enable_mt_work(struct work_struct *work)
732 struct magicmouse_sc *msc =
733 container_of(work, struct magicmouse_sc, work.work);
736 ret = magicmouse_enable_multitouch(msc->hdev);
738 hid_err(msc->hdev, "unable to request touch data (%d)\n", ret);
741 static int magicmouse_probe(struct hid_device *hdev,
742 const struct hid_device_id *id)
744 struct magicmouse_sc *msc;
745 struct hid_report *report;
748 if (id->vendor == USB_VENDOR_ID_APPLE &&
749 id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
750 hdev->type != HID_TYPE_USBMOUSE)
753 msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
755 hid_err(hdev, "can't alloc magicmouse descriptor\n");
759 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
761 INIT_DEFERRABLE_WORK(&msc->work, magicmouse_enable_mt_work);
763 msc->quirks = id->driver_data;
764 hid_set_drvdata(hdev, msc);
766 ret = hid_parse(hdev);
768 hid_err(hdev, "magicmouse hid parse failed\n");
772 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
774 hid_err(hdev, "magicmouse hw start failed\n");
779 hid_err(hdev, "magicmouse input not registered\n");
784 if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
785 report = hid_register_report(hdev, HID_INPUT_REPORT,
787 else if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
788 report = hid_register_report(hdev, HID_INPUT_REPORT,
789 MOUSE2_REPORT_ID, 0);
790 else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
791 if (id->vendor == BT_VENDOR_ID_APPLE)
792 report = hid_register_report(hdev, HID_INPUT_REPORT,
793 TRACKPAD2_BT_REPORT_ID, 0);
794 else /* USB_VENDOR_ID_APPLE */
795 report = hid_register_report(hdev, HID_INPUT_REPORT,
796 TRACKPAD2_USB_REPORT_ID, 0);
797 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
798 report = hid_register_report(hdev, HID_INPUT_REPORT,
799 TRACKPAD_REPORT_ID, 0);
800 report = hid_register_report(hdev, HID_INPUT_REPORT,
801 DOUBLE_REPORT_ID, 0);
805 hid_err(hdev, "unable to register touch report\n");
812 * Some devices repond with 'invalid report id' when feature
813 * report switching it into multitouch mode is sent to it.
815 * This results in -EIO from the _raw low-level transport callback,
816 * but there seems to be no other way of switching the mode.
817 * Thus the super-ugly hacky success check below.
819 ret = magicmouse_enable_multitouch(hdev);
820 if (ret != -EIO && ret < 0) {
821 hid_err(hdev, "unable to request touch data (%d)\n", ret);
824 if (ret == -EIO && id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
825 schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
834 static void magicmouse_remove(struct hid_device *hdev)
836 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
839 cancel_delayed_work_sync(&msc->work);
844 static const struct hid_device_id magic_mice[] = {
845 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
846 USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
847 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
848 USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
849 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
850 USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
851 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
852 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
853 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
854 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
857 MODULE_DEVICE_TABLE(hid, magic_mice);
859 static struct hid_driver magicmouse_driver = {
860 .name = "magicmouse",
861 .id_table = magic_mice,
862 .probe = magicmouse_probe,
863 .remove = magicmouse_remove,
864 .raw_event = magicmouse_raw_event,
865 .event = magicmouse_event,
866 .input_mapping = magicmouse_input_mapping,
867 .input_configured = magicmouse_input_configured,
869 module_hid_driver(magicmouse_driver);
871 MODULE_LICENSE("GPL");