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