1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Apple "Magic" Wireless Mouse driver
4 *
5 * Copyright (c) 2010 Michael Poole <mdpoole@troilus.org>
6 * Copyright (c) 2010 Chase Douglas <chase.douglas@canonical.com>
7 */
8
9 /*
10 */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
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>
20
21 #include "hid-ids.h"
22
23 static bool emulate_3button = true;
24 module_param(emulate_3button, bool, 0644);
25 MODULE_PARM_DESC(emulate_3button, "Emulate a middle button");
26
27 static int middle_button_start = -350;
28 static int middle_button_stop = +350;
29
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");
33
34 static unsigned int scroll_speed = 32;
param_set_scroll_speed(const char * val,const struct kernel_param * kp)35 static int param_set_scroll_speed(const char *val,
36 const struct kernel_param *kp) {
37 unsigned long speed;
38 if (!val || kstrtoul(val, 0, &speed) || speed > 63)
39 return -EINVAL;
40 scroll_speed = speed;
41 return 0;
42 }
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)");
45
46 static bool scroll_acceleration = false;
47 module_param(scroll_acceleration, bool, 0644);
48 MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
49
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");
53
54 #define TRACKPAD2_2021_BT_VERSION 0x110
55
56 #define TRACKPAD_REPORT_ID 0x28
57 #define TRACKPAD2_USB_REPORT_ID 0x02
58 #define TRACKPAD2_BT_REPORT_ID 0x31
59 #define MOUSE_REPORT_ID 0x29
60 #define MOUSE2_REPORT_ID 0x12
61 #define DOUBLE_REPORT_ID 0xf7
62 #define USB_BATTERY_TIMEOUT_MS 60000
63
64 /* These definitions are not precise, but they're close enough. (Bits
65 * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
66 * to be some kind of bit mask -- 0x20 may be a near-field reading,
67 * and 0x40 is actual contact, and 0x10 may be a start/stop or change
68 * indication.)
69 */
70 #define TOUCH_STATE_MASK 0xf0
71 #define TOUCH_STATE_NONE 0x00
72 #define TOUCH_STATE_START 0x30
73 #define TOUCH_STATE_DRAG 0x40
74
75 /* Number of high-resolution events for each low-resolution detent. */
76 #define SCROLL_HR_STEPS 10
77 #define SCROLL_HR_MULT (120 / SCROLL_HR_STEPS)
78 #define SCROLL_HR_THRESHOLD 90 /* units */
79 #define SCROLL_ACCEL_DEFAULT 7
80
81 /* Touch surface information. Dimension is in hundredths of a mm, min and max
82 * are in units. */
83 #define MOUSE_DIMENSION_X (float)9056
84 #define MOUSE_MIN_X -1100
85 #define MOUSE_MAX_X 1258
86 #define MOUSE_RES_X ((MOUSE_MAX_X - MOUSE_MIN_X) / (MOUSE_DIMENSION_X / 100))
87 #define MOUSE_DIMENSION_Y (float)5152
88 #define MOUSE_MIN_Y -1589
89 #define MOUSE_MAX_Y 2047
90 #define MOUSE_RES_Y ((MOUSE_MAX_Y - MOUSE_MIN_Y) / (MOUSE_DIMENSION_Y / 100))
91
92 #define TRACKPAD_DIMENSION_X (float)13000
93 #define TRACKPAD_MIN_X -2909
94 #define TRACKPAD_MAX_X 3167
95 #define TRACKPAD_RES_X \
96 ((TRACKPAD_MAX_X - TRACKPAD_MIN_X) / (TRACKPAD_DIMENSION_X / 100))
97 #define TRACKPAD_DIMENSION_Y (float)11000
98 #define TRACKPAD_MIN_Y -2456
99 #define TRACKPAD_MAX_Y 2565
100 #define TRACKPAD_RES_Y \
101 ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
102
103 #define TRACKPAD2_DIMENSION_X (float)16000
104 #define TRACKPAD2_MIN_X -3678
105 #define TRACKPAD2_MAX_X 3934
106 #define TRACKPAD2_RES_X \
107 ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
108 #define TRACKPAD2_DIMENSION_Y (float)11490
109 #define TRACKPAD2_MIN_Y -2478
110 #define TRACKPAD2_MAX_Y 2587
111 #define TRACKPAD2_RES_Y \
112 ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
113
114 /**
115 * struct magicmouse_sc - Tracks Magic Mouse-specific data.
116 * @input: Input device through which we report events.
117 * @quirks: Currently unused.
118 * @ntouches: Number of touches in most recent touch report.
119 * @scroll_accel: Number of consecutive scroll motions.
120 * @scroll_jiffies: Time of last scroll motion.
121 * @touches: Most recent data for a touch, indexed by tracking ID.
122 * @tracking_ids: Mapping of current touch input data to @touches.
123 */
124 struct magicmouse_sc {
125 struct input_dev *input;
126 unsigned long quirks;
127
128 int ntouches;
129 int scroll_accel;
130 unsigned long scroll_jiffies;
131
132 struct {
133 short x;
134 short y;
135 short scroll_x;
136 short scroll_y;
137 short scroll_x_hr;
138 short scroll_y_hr;
139 u8 size;
140 bool scroll_x_active;
141 bool scroll_y_active;
142 } touches[16];
143 int tracking_ids[16];
144
145 struct hid_device *hdev;
146 struct delayed_work work;
147 struct timer_list battery_timer;
148 };
149
magicmouse_firm_touch(struct magicmouse_sc * msc)150 static int magicmouse_firm_touch(struct magicmouse_sc *msc)
151 {
152 int touch = -1;
153 int ii;
154
155 /* If there is only one "firm" touch, set touch to its
156 * tracking ID.
157 */
158 for (ii = 0; ii < msc->ntouches; ii++) {
159 int idx = msc->tracking_ids[ii];
160 if (msc->touches[idx].size < 8) {
161 /* Ignore this touch. */
162 } else if (touch >= 0) {
163 touch = -1;
164 break;
165 } else {
166 touch = idx;
167 }
168 }
169
170 return touch;
171 }
172
magicmouse_emit_buttons(struct magicmouse_sc * msc,int state)173 static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
174 {
175 int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
176 test_bit(BTN_RIGHT, msc->input->key) << 1 |
177 test_bit(BTN_MIDDLE, msc->input->key) << 2;
178
179 if (emulate_3button) {
180 int id;
181
182 /* If some button was pressed before, keep it held
183 * down. Otherwise, if there's exactly one firm
184 * touch, use that to override the mouse's guess.
185 */
186 if (state == 0) {
187 /* The button was released. */
188 } else if (last_state != 0) {
189 state = last_state;
190 } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
191 int x = msc->touches[id].x;
192 if (x < middle_button_start)
193 state = 1;
194 else if (x > middle_button_stop)
195 state = 2;
196 else
197 state = 4;
198 } /* else: we keep the mouse's guess */
199
200 input_report_key(msc->input, BTN_MIDDLE, state & 4);
201 }
202
203 input_report_key(msc->input, BTN_LEFT, state & 1);
204 input_report_key(msc->input, BTN_RIGHT, state & 2);
205
206 if (state != last_state)
207 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
208 }
209
magicmouse_emit_touch(struct magicmouse_sc * msc,int raw_id,u8 * tdata)210 static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
211 {
212 struct input_dev *input = msc->input;
213 int id, x, y, size, orientation, touch_major, touch_minor, state, down;
214 int pressure = 0;
215
216 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
217 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
218 id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
219 x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
220 y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
221 size = tdata[5] & 0x3f;
222 orientation = (tdata[6] >> 2) - 32;
223 touch_major = tdata[3];
224 touch_minor = tdata[4];
225 state = tdata[7] & TOUCH_STATE_MASK;
226 down = state != TOUCH_STATE_NONE;
227 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
228 input->id.product ==
229 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
230 id = tdata[8] & 0xf;
231 x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
232 y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
233 size = tdata[6];
234 orientation = (tdata[8] >> 5) - 4;
235 touch_major = tdata[4];
236 touch_minor = tdata[5];
237 pressure = tdata[7];
238 state = tdata[3] & 0xC0;
239 down = state == 0x80;
240 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
241 id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
242 x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
243 y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
244 size = tdata[6] & 0x3f;
245 orientation = (tdata[7] >> 2) - 32;
246 touch_major = tdata[4];
247 touch_minor = tdata[5];
248 state = tdata[8] & TOUCH_STATE_MASK;
249 down = state != TOUCH_STATE_NONE;
250 }
251
252 /* Store tracking ID and other fields. */
253 msc->tracking_ids[raw_id] = id;
254 msc->touches[id].x = x;
255 msc->touches[id].y = y;
256 msc->touches[id].size = size;
257
258 /* If requested, emulate a scroll wheel by detecting small
259 * vertical touch motions.
260 */
261 if (emulate_scroll_wheel &&
262 input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
263 input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
264 unsigned long now = jiffies;
265 int step_x = msc->touches[id].scroll_x - x;
266 int step_y = msc->touches[id].scroll_y - y;
267 int step_hr =
268 max_t(int,
269 ((64 - (int)scroll_speed) * msc->scroll_accel) /
270 SCROLL_HR_STEPS,
271 1);
272 int step_x_hr = msc->touches[id].scroll_x_hr - x;
273 int step_y_hr = msc->touches[id].scroll_y_hr - y;
274
275 /* Calculate and apply the scroll motion. */
276 switch (state) {
277 case TOUCH_STATE_START:
278 msc->touches[id].scroll_x = x;
279 msc->touches[id].scroll_y = y;
280 msc->touches[id].scroll_x_hr = x;
281 msc->touches[id].scroll_y_hr = y;
282 msc->touches[id].scroll_x_active = false;
283 msc->touches[id].scroll_y_active = false;
284
285 /* Reset acceleration after half a second. */
286 if (scroll_acceleration && time_before(now,
287 msc->scroll_jiffies + HZ / 2))
288 msc->scroll_accel = max_t(int,
289 msc->scroll_accel - 1, 1);
290 else
291 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
292
293 break;
294 case TOUCH_STATE_DRAG:
295 step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
296 if (step_x != 0) {
297 msc->touches[id].scroll_x -= step_x *
298 (64 - scroll_speed) * msc->scroll_accel;
299 msc->scroll_jiffies = now;
300 input_report_rel(input, REL_HWHEEL, -step_x);
301 }
302
303 step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
304 if (step_y != 0) {
305 msc->touches[id].scroll_y -= step_y *
306 (64 - scroll_speed) * msc->scroll_accel;
307 msc->scroll_jiffies = now;
308 input_report_rel(input, REL_WHEEL, step_y);
309 }
310
311 if (!msc->touches[id].scroll_x_active &&
312 abs(step_x_hr) > SCROLL_HR_THRESHOLD) {
313 msc->touches[id].scroll_x_active = true;
314 msc->touches[id].scroll_x_hr = x;
315 step_x_hr = 0;
316 }
317
318 step_x_hr /= step_hr;
319 if (step_x_hr != 0 &&
320 msc->touches[id].scroll_x_active) {
321 msc->touches[id].scroll_x_hr -= step_x_hr *
322 step_hr;
323 input_report_rel(input,
324 REL_HWHEEL_HI_RES,
325 -step_x_hr * SCROLL_HR_MULT);
326 }
327
328 if (!msc->touches[id].scroll_y_active &&
329 abs(step_y_hr) > SCROLL_HR_THRESHOLD) {
330 msc->touches[id].scroll_y_active = true;
331 msc->touches[id].scroll_y_hr = y;
332 step_y_hr = 0;
333 }
334
335 step_y_hr /= step_hr;
336 if (step_y_hr != 0 &&
337 msc->touches[id].scroll_y_active) {
338 msc->touches[id].scroll_y_hr -= step_y_hr *
339 step_hr;
340 input_report_rel(input,
341 REL_WHEEL_HI_RES,
342 step_y_hr * SCROLL_HR_MULT);
343 }
344 break;
345 }
346 }
347
348 if (down)
349 msc->ntouches++;
350
351 input_mt_slot(input, id);
352 input_mt_report_slot_state(input, MT_TOOL_FINGER, down);
353
354 /* Generate the input events for this touch. */
355 if (down) {
356 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major << 2);
357 input_report_abs(input, ABS_MT_TOUCH_MINOR, touch_minor << 2);
358 input_report_abs(input, ABS_MT_ORIENTATION, -orientation);
359 input_report_abs(input, ABS_MT_POSITION_X, x);
360 input_report_abs(input, ABS_MT_POSITION_Y, y);
361
362 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
363 input->id.product ==
364 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC)
365 input_report_abs(input, ABS_MT_PRESSURE, pressure);
366
367 if (report_undeciphered) {
368 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
369 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
370 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
371 else if (input->id.product !=
372 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
373 input->id.product !=
374 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC)
375 input_event(input, EV_MSC, MSC_RAW, tdata[8]);
376 }
377 }
378 }
379
magicmouse_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)380 static int magicmouse_raw_event(struct hid_device *hdev,
381 struct hid_report *report, u8 *data, int size)
382 {
383 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
384 struct input_dev *input = msc->input;
385 int x = 0, y = 0, ii, clicks = 0, npoints;
386
387 switch (data[0]) {
388 case TRACKPAD_REPORT_ID:
389 case TRACKPAD2_BT_REPORT_ID:
390 /* Expect four bytes of prefix, and N*9 bytes of touch data. */
391 if (size < 4 || ((size - 4) % 9) != 0)
392 return 0;
393 npoints = (size - 4) / 9;
394 if (npoints > 15) {
395 hid_warn(hdev, "invalid size value (%d) for TRACKPAD_REPORT_ID\n",
396 size);
397 return 0;
398 }
399 msc->ntouches = 0;
400 for (ii = 0; ii < npoints; ii++)
401 magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
402
403 clicks = data[1];
404
405 /* The following bits provide a device specific timestamp. They
406 * are unused here.
407 *
408 * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
409 */
410 break;
411 case TRACKPAD2_USB_REPORT_ID:
412 /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
413 if (size < 12 || ((size - 12) % 9) != 0)
414 return 0;
415 npoints = (size - 12) / 9;
416 if (npoints > 15) {
417 hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
418 size);
419 return 0;
420 }
421 msc->ntouches = 0;
422 for (ii = 0; ii < npoints; ii++)
423 magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
424
425 clicks = data[1];
426 break;
427 case MOUSE_REPORT_ID:
428 /* Expect six bytes of prefix, and N*8 bytes of touch data. */
429 if (size < 6 || ((size - 6) % 8) != 0)
430 return 0;
431 npoints = (size - 6) / 8;
432 if (npoints > 15) {
433 hid_warn(hdev, "invalid size value (%d) for MOUSE_REPORT_ID\n",
434 size);
435 return 0;
436 }
437 msc->ntouches = 0;
438 for (ii = 0; ii < npoints; ii++)
439 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
440
441 /* When emulating three-button mode, it is important
442 * to have the current touch information before
443 * generating a click event.
444 */
445 x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
446 y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
447 clicks = data[3];
448
449 /* The following bits provide a device specific timestamp. They
450 * are unused here.
451 *
452 * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
453 */
454 break;
455 case MOUSE2_REPORT_ID:
456 /* Size is either 8 or (14 + 8 * N) */
457 if (size != 8 && (size < 14 || (size - 14) % 8 != 0))
458 return 0;
459 npoints = (size - 14) / 8;
460 if (npoints > 15) {
461 hid_warn(hdev, "invalid size value (%d) for MOUSE2_REPORT_ID\n",
462 size);
463 return 0;
464 }
465 msc->ntouches = 0;
466 for (ii = 0; ii < npoints; ii++)
467 magicmouse_emit_touch(msc, ii, data + ii * 8 + 14);
468
469 /* When emulating three-button mode, it is important
470 * to have the current touch information before
471 * generating a click event.
472 */
473 x = (int)((data[3] << 24) | (data[2] << 16)) >> 16;
474 y = (int)((data[5] << 24) | (data[4] << 16)) >> 16;
475 clicks = data[1];
476
477 /* The following bits provide a device specific timestamp. They
478 * are unused here.
479 *
480 * ts = data[11] >> 6 | data[12] << 2 | data[13] << 10;
481 */
482 break;
483 case DOUBLE_REPORT_ID:
484 /* Sometimes the trackpad sends two touch reports in one
485 * packet.
486 */
487 magicmouse_raw_event(hdev, report, data + 2, data[1]);
488 magicmouse_raw_event(hdev, report, data + 2 + data[1],
489 size - 2 - data[1]);
490 return 0;
491 default:
492 return 0;
493 }
494
495 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
496 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
497 magicmouse_emit_buttons(msc, clicks & 3);
498 input_report_rel(input, REL_X, x);
499 input_report_rel(input, REL_Y, y);
500 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
501 input->id.product ==
502 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
503 input_mt_sync_frame(input);
504 input_report_key(input, BTN_MOUSE, clicks & 1);
505 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
506 input_report_key(input, BTN_MOUSE, clicks & 1);
507 input_mt_report_pointer_emulation(input, true);
508 }
509
510 input_sync(input);
511 return 1;
512 }
513
magicmouse_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)514 static int magicmouse_event(struct hid_device *hdev, struct hid_field *field,
515 struct hid_usage *usage, __s32 value)
516 {
517 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
518 if (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 &&
519 field->report->id == MOUSE2_REPORT_ID) {
520 /*
521 * magic_mouse_raw_event has done all the work. Skip hidinput.
522 *
523 * Specifically, hidinput may modify BTN_LEFT and BTN_RIGHT,
524 * breaking emulate_3button.
525 */
526 return 1;
527 }
528 return 0;
529 }
530
magicmouse_setup_input(struct input_dev * input,struct hid_device * hdev)531 static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
532 {
533 int error;
534 int mt_flags = 0;
535
536 __set_bit(EV_KEY, input->evbit);
537
538 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
539 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
540 __set_bit(BTN_LEFT, input->keybit);
541 __set_bit(BTN_RIGHT, input->keybit);
542 if (emulate_3button)
543 __set_bit(BTN_MIDDLE, input->keybit);
544
545 __set_bit(EV_REL, input->evbit);
546 __set_bit(REL_X, input->relbit);
547 __set_bit(REL_Y, input->relbit);
548 if (emulate_scroll_wheel) {
549 __set_bit(REL_WHEEL, input->relbit);
550 __set_bit(REL_HWHEEL, input->relbit);
551 __set_bit(REL_WHEEL_HI_RES, input->relbit);
552 __set_bit(REL_HWHEEL_HI_RES, input->relbit);
553 }
554 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
555 input->id.product ==
556 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
557 /* If the trackpad has been connected to a Mac, the name is
558 * automatically personalized, e.g., "José Expósito's Trackpad".
559 * When connected through Bluetooth, the personalized name is
560 * reported, however, when connected through USB the generic
561 * name is reported.
562 * Set the device name to ensure the same driver settings get
563 * loaded, whether connected through bluetooth or USB.
564 */
565 if (hdev->vendor == BT_VENDOR_ID_APPLE) {
566 if (input->id.version == TRACKPAD2_2021_BT_VERSION)
567 input->name = "Apple Inc. Magic Trackpad";
568 else
569 input->name = "Apple Inc. Magic Trackpad 2";
570 } else { /* USB_VENDOR_ID_APPLE */
571 input->name = hdev->name;
572 }
573
574 __clear_bit(EV_MSC, input->evbit);
575 __clear_bit(BTN_0, input->keybit);
576 __clear_bit(BTN_RIGHT, input->keybit);
577 __clear_bit(BTN_MIDDLE, input->keybit);
578 __set_bit(BTN_MOUSE, input->keybit);
579 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
580 __set_bit(BTN_TOOL_FINGER, input->keybit);
581
582 mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
583 INPUT_MT_TRACK;
584 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
585 /* input->keybit is initialized with incorrect button info
586 * for Magic Trackpad. There really is only one physical
587 * button (BTN_LEFT == BTN_MOUSE). Make sure we don't
588 * advertise buttons that don't exist...
589 */
590 __clear_bit(BTN_RIGHT, input->keybit);
591 __clear_bit(BTN_MIDDLE, input->keybit);
592 __set_bit(BTN_MOUSE, input->keybit);
593 __set_bit(BTN_TOOL_FINGER, input->keybit);
594 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
595 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
596 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
597 __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
598 __set_bit(BTN_TOUCH, input->keybit);
599 __set_bit(INPUT_PROP_POINTER, input->propbit);
600 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
601 }
602
603
604 __set_bit(EV_ABS, input->evbit);
605
606 error = input_mt_init_slots(input, 16, mt_flags);
607 if (error)
608 return error;
609 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
610 4, 0);
611 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
612 4, 0);
613
614 /* Note: Touch Y position from the device is inverted relative
615 * to how pointer motion is reported (and relative to how USB
616 * HID recommends the coordinates work). This driver keeps
617 * the origin at the same position, and just uses the additive
618 * inverse of the reported Y.
619 */
620 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
621 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
622 input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
623 input_set_abs_params(input, ABS_MT_POSITION_X,
624 MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
625 input_set_abs_params(input, ABS_MT_POSITION_Y,
626 MOUSE_MIN_Y, MOUSE_MAX_Y, 4, 0);
627
628 input_abs_set_res(input, ABS_MT_POSITION_X,
629 MOUSE_RES_X);
630 input_abs_set_res(input, ABS_MT_POSITION_Y,
631 MOUSE_RES_Y);
632 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
633 input->id.product ==
634 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
635 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
636 input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
637 input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
638 input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
639 TRACKPAD2_MAX_X, 0, 0);
640 input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
641 TRACKPAD2_MAX_Y, 0, 0);
642 input_set_abs_params(input, ABS_MT_POSITION_X,
643 TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
644 input_set_abs_params(input, ABS_MT_POSITION_Y,
645 TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
646
647 input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
648 input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
649 input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
650 input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
651 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
652 input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
653 input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
654 TRACKPAD_MAX_X, 4, 0);
655 input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
656 TRACKPAD_MAX_Y, 4, 0);
657 input_set_abs_params(input, ABS_MT_POSITION_X,
658 TRACKPAD_MIN_X, TRACKPAD_MAX_X, 4, 0);
659 input_set_abs_params(input, ABS_MT_POSITION_Y,
660 TRACKPAD_MIN_Y, TRACKPAD_MAX_Y, 4, 0);
661
662 input_abs_set_res(input, ABS_X, TRACKPAD_RES_X);
663 input_abs_set_res(input, ABS_Y, TRACKPAD_RES_Y);
664 input_abs_set_res(input, ABS_MT_POSITION_X,
665 TRACKPAD_RES_X);
666 input_abs_set_res(input, ABS_MT_POSITION_Y,
667 TRACKPAD_RES_Y);
668 }
669
670 input_set_events_per_packet(input, 60);
671
672 if (report_undeciphered &&
673 input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
674 input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
675 __set_bit(EV_MSC, input->evbit);
676 __set_bit(MSC_RAW, input->mscbit);
677 }
678
679 /*
680 * hid-input may mark device as using autorepeat, but neither
681 * the trackpad, nor the mouse actually want it.
682 */
683 __clear_bit(EV_REP, input->evbit);
684
685 return 0;
686 }
687
magicmouse_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)688 static int magicmouse_input_mapping(struct hid_device *hdev,
689 struct hid_input *hi, struct hid_field *field,
690 struct hid_usage *usage, unsigned long **bit, int *max)
691 {
692 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
693
694 if (!msc->input)
695 msc->input = hi->input;
696
697 /* Magic Trackpad does not give relative data after switching to MT */
698 if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
699 hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
700 hi->input->id.product ==
701 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
702 field->flags & HID_MAIN_ITEM_RELATIVE)
703 return -1;
704
705 return 0;
706 }
707
magicmouse_input_configured(struct hid_device * hdev,struct hid_input * hi)708 static int magicmouse_input_configured(struct hid_device *hdev,
709 struct hid_input *hi)
710
711 {
712 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
713 int ret;
714
715 ret = magicmouse_setup_input(msc->input, hdev);
716 if (ret) {
717 hid_err(hdev, "magicmouse setup input failed (%d)\n", ret);
718 /* clean msc->input to notify probe() of the failure */
719 msc->input = NULL;
720 return ret;
721 }
722
723 return 0;
724 }
725
magicmouse_enable_multitouch(struct hid_device * hdev)726 static int magicmouse_enable_multitouch(struct hid_device *hdev)
727 {
728 const u8 *feature;
729 const u8 feature_mt[] = { 0xD7, 0x01 };
730 const u8 feature_mt_mouse2[] = { 0xF1, 0x02, 0x01 };
731 const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
732 const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
733 u8 *buf;
734 int ret;
735 int feature_size;
736
737 if (hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
738 hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
739 if (hdev->vendor == BT_VENDOR_ID_APPLE) {
740 feature_size = sizeof(feature_mt_trackpad2_bt);
741 feature = feature_mt_trackpad2_bt;
742 } else { /* USB_VENDOR_ID_APPLE */
743 feature_size = sizeof(feature_mt_trackpad2_usb);
744 feature = feature_mt_trackpad2_usb;
745 }
746 } else if (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
747 feature_size = sizeof(feature_mt_mouse2);
748 feature = feature_mt_mouse2;
749 } else {
750 feature_size = sizeof(feature_mt);
751 feature = feature_mt;
752 }
753
754 buf = kmemdup(feature, feature_size, GFP_KERNEL);
755 if (!buf)
756 return -ENOMEM;
757
758 ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
759 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
760 kfree(buf);
761 return ret;
762 }
763
magicmouse_enable_mt_work(struct work_struct * work)764 static void magicmouse_enable_mt_work(struct work_struct *work)
765 {
766 struct magicmouse_sc *msc =
767 container_of(work, struct magicmouse_sc, work.work);
768 int ret;
769
770 ret = magicmouse_enable_multitouch(msc->hdev);
771 if (ret < 0)
772 hid_err(msc->hdev, "unable to request touch data (%d)\n", ret);
773 }
774
magicmouse_fetch_battery(struct hid_device * hdev)775 static int magicmouse_fetch_battery(struct hid_device *hdev)
776 {
777 #ifdef CONFIG_HID_BATTERY_STRENGTH
778 struct hid_report_enum *report_enum;
779 struct hid_report *report;
780
781 if (!hdev->battery || hdev->vendor != USB_VENDOR_ID_APPLE ||
782 (hdev->product != USB_DEVICE_ID_APPLE_MAGICMOUSE2 &&
783 hdev->product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
784 hdev->product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC))
785 return -1;
786
787 report_enum = &hdev->report_enum[hdev->battery_report_type];
788 report = report_enum->report_id_hash[hdev->battery_report_id];
789
790 if (!report || report->maxfield < 1)
791 return -1;
792
793 if (hdev->battery_capacity == hdev->battery_max)
794 return -1;
795
796 hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
797 return 0;
798 #else
799 return -1;
800 #endif
801 }
802
magicmouse_battery_timer_tick(struct timer_list * t)803 static void magicmouse_battery_timer_tick(struct timer_list *t)
804 {
805 struct magicmouse_sc *msc = from_timer(msc, t, battery_timer);
806 struct hid_device *hdev = msc->hdev;
807
808 if (magicmouse_fetch_battery(hdev) == 0) {
809 mod_timer(&msc->battery_timer,
810 jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
811 }
812 }
813
magicmouse_probe(struct hid_device * hdev,const struct hid_device_id * id)814 static int magicmouse_probe(struct hid_device *hdev,
815 const struct hid_device_id *id)
816 {
817 struct magicmouse_sc *msc;
818 struct hid_report *report;
819 int ret;
820
821 msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
822 if (msc == NULL) {
823 hid_err(hdev, "can't alloc magicmouse descriptor\n");
824 return -ENOMEM;
825 }
826
827 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
828 msc->hdev = hdev;
829 INIT_DEFERRABLE_WORK(&msc->work, magicmouse_enable_mt_work);
830
831 msc->quirks = id->driver_data;
832 hid_set_drvdata(hdev, msc);
833
834 ret = hid_parse(hdev);
835 if (ret) {
836 hid_err(hdev, "magicmouse hid parse failed\n");
837 return ret;
838 }
839
840 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
841 if (ret) {
842 hid_err(hdev, "magicmouse hw start failed\n");
843 return ret;
844 }
845
846 timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0);
847 mod_timer(&msc->battery_timer,
848 jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
849 magicmouse_fetch_battery(hdev);
850
851 if (id->vendor == USB_VENDOR_ID_APPLE &&
852 (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
853 ((id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
854 id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
855 hdev->type != HID_TYPE_USBMOUSE)))
856 return 0;
857
858 if (!msc->input) {
859 hid_err(hdev, "magicmouse input not registered\n");
860 ret = -ENOMEM;
861 goto err_stop_hw;
862 }
863
864 if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
865 report = hid_register_report(hdev, HID_INPUT_REPORT,
866 MOUSE_REPORT_ID, 0);
867 else if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
868 report = hid_register_report(hdev, HID_INPUT_REPORT,
869 MOUSE2_REPORT_ID, 0);
870 else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
871 id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
872 if (id->vendor == BT_VENDOR_ID_APPLE)
873 report = hid_register_report(hdev, HID_INPUT_REPORT,
874 TRACKPAD2_BT_REPORT_ID, 0);
875 else /* USB_VENDOR_ID_APPLE */
876 report = hid_register_report(hdev, HID_INPUT_REPORT,
877 TRACKPAD2_USB_REPORT_ID, 0);
878 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
879 report = hid_register_report(hdev, HID_INPUT_REPORT,
880 TRACKPAD_REPORT_ID, 0);
881 report = hid_register_report(hdev, HID_INPUT_REPORT,
882 DOUBLE_REPORT_ID, 0);
883 }
884
885 if (!report) {
886 hid_err(hdev, "unable to register touch report\n");
887 ret = -ENOMEM;
888 goto err_stop_hw;
889 }
890 report->size = 6;
891
892 /*
893 * Some devices repond with 'invalid report id' when feature
894 * report switching it into multitouch mode is sent to it.
895 *
896 * This results in -EIO from the _raw low-level transport callback,
897 * but there seems to be no other way of switching the mode.
898 * Thus the super-ugly hacky success check below.
899 */
900 ret = magicmouse_enable_multitouch(hdev);
901 if (ret != -EIO && ret < 0) {
902 hid_err(hdev, "unable to request touch data (%d)\n", ret);
903 goto err_stop_hw;
904 }
905 if (ret == -EIO && id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
906 schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
907 }
908
909 return 0;
910 err_stop_hw:
911 del_timer_sync(&msc->battery_timer);
912 hid_hw_stop(hdev);
913 return ret;
914 }
915
magicmouse_remove(struct hid_device * hdev)916 static void magicmouse_remove(struct hid_device *hdev)
917 {
918 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
919
920 if (msc) {
921 cancel_delayed_work_sync(&msc->work);
922 del_timer_sync(&msc->battery_timer);
923 }
924
925 hid_hw_stop(hdev);
926 }
927
magicmouse_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)928 static __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc,
929 unsigned int *rsize)
930 {
931 /*
932 * Change the usage from:
933 * 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 0
934 * 0x09, 0x0b, // Usage (Vendor Usage 0x0b) 3
935 * To:
936 * 0x05, 0x01, // Usage Page (Generic Desktop) 0
937 * 0x09, 0x02, // Usage (Mouse) 2
938 */
939 if (hdev->vendor == USB_VENDOR_ID_APPLE &&
940 (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
941 hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
942 hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
943 *rsize == 83 && rdesc[46] == 0x84 && rdesc[58] == 0x85) {
944 hid_info(hdev,
945 "fixing up magicmouse battery report descriptor\n");
946 *rsize = *rsize - 1;
947 rdesc = kmemdup(rdesc + 1, *rsize, GFP_KERNEL);
948 if (!rdesc)
949 return NULL;
950
951 rdesc[0] = 0x05;
952 rdesc[1] = 0x01;
953 rdesc[2] = 0x09;
954 rdesc[3] = 0x02;
955 }
956
957 return rdesc;
958 }
959
960 static const struct hid_device_id magic_mice[] = {
961 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
962 USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
963 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
964 USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
965 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
966 USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
967 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
968 USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
969 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
970 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
971 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
972 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
973 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
974 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC), .driver_data = 0 },
975 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
976 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC), .driver_data = 0 },
977 { }
978 };
979 MODULE_DEVICE_TABLE(hid, magic_mice);
980
981 static struct hid_driver magicmouse_driver = {
982 .name = "magicmouse",
983 .id_table = magic_mice,
984 .probe = magicmouse_probe,
985 .remove = magicmouse_remove,
986 .report_fixup = magicmouse_report_fixup,
987 .raw_event = magicmouse_raw_event,
988 .event = magicmouse_event,
989 .input_mapping = magicmouse_input_mapping,
990 .input_configured = magicmouse_input_configured,
991 };
992 module_hid_driver(magicmouse_driver);
993
994 MODULE_LICENSE("GPL");
995