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_SEC 60
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 /* Protect against zero sized recursive calls from DOUBLE_REPORT_ID */
394 if (size < 1)
395 return 0;
396
397 switch (data[0]) {
398 case TRACKPAD_REPORT_ID:
399 case TRACKPAD2_BT_REPORT_ID:
400 /* Expect four bytes of prefix, and N*9 bytes of touch data. */
401 if (size < 4 || ((size - 4) % 9) != 0)
402 return 0;
403 npoints = (size - 4) / 9;
404 if (npoints > 15) {
405 hid_warn(hdev, "invalid size value (%d) for TRACKPAD_REPORT_ID\n",
406 size);
407 return 0;
408 }
409 msc->ntouches = 0;
410 for (ii = 0; ii < npoints; ii++)
411 magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
412
413 clicks = data[1];
414
415 /* The following bits provide a device specific timestamp. They
416 * are unused here.
417 *
418 * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
419 */
420 break;
421 case TRACKPAD2_USB_REPORT_ID:
422 /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
423 if (size < 12 || ((size - 12) % 9) != 0)
424 return 0;
425 npoints = (size - 12) / 9;
426 if (npoints > 15) {
427 hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
428 size);
429 return 0;
430 }
431 msc->ntouches = 0;
432 for (ii = 0; ii < npoints; ii++)
433 magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
434
435 clicks = data[1];
436 break;
437 case MOUSE_REPORT_ID:
438 /* Expect six bytes of prefix, and N*8 bytes of touch data. */
439 if (size < 6 || ((size - 6) % 8) != 0)
440 return 0;
441 npoints = (size - 6) / 8;
442 if (npoints > 15) {
443 hid_warn(hdev, "invalid size value (%d) for MOUSE_REPORT_ID\n",
444 size);
445 return 0;
446 }
447 msc->ntouches = 0;
448 for (ii = 0; ii < npoints; ii++)
449 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
450
451 /* When emulating three-button mode, it is important
452 * to have the current touch information before
453 * generating a click event.
454 */
455 x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
456 y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
457 clicks = data[3];
458
459 /* The following bits provide a device specific timestamp. They
460 * are unused here.
461 *
462 * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
463 */
464 break;
465 case MOUSE2_REPORT_ID:
466 /* Size is either 8 or (14 + 8 * N) */
467 if (size != 8 && (size < 14 || (size - 14) % 8 != 0))
468 return 0;
469 npoints = (size - 14) / 8;
470 if (npoints > 15) {
471 hid_warn(hdev, "invalid size value (%d) for MOUSE2_REPORT_ID\n",
472 size);
473 return 0;
474 }
475 msc->ntouches = 0;
476 for (ii = 0; ii < npoints; ii++)
477 magicmouse_emit_touch(msc, ii, data + ii * 8 + 14);
478
479 /* When emulating three-button mode, it is important
480 * to have the current touch information before
481 * generating a click event.
482 */
483 x = (int)((data[3] << 24) | (data[2] << 16)) >> 16;
484 y = (int)((data[5] << 24) | (data[4] << 16)) >> 16;
485 clicks = data[1];
486
487 /* The following bits provide a device specific timestamp. They
488 * are unused here.
489 *
490 * ts = data[11] >> 6 | data[12] << 2 | data[13] << 10;
491 */
492 break;
493 case DOUBLE_REPORT_ID:
494 /* Sometimes the trackpad sends two touch reports in one
495 * packet.
496 */
497
498 /* Ensure that we have at least 2 elements (report type and size) */
499 if (size < 2)
500 return 0;
501
502 if (size < data[1] + 2) {
503 hid_warn(hdev,
504 "received report length (%d) was smaller than specified (%d)",
505 size, data[1] + 2);
506 return 0;
507 }
508
509 magicmouse_raw_event(hdev, report, data + 2, data[1]);
510 magicmouse_raw_event(hdev, report, data + 2 + data[1],
511 size - 2 - data[1]);
512 return 0;
513 default:
514 return 0;
515 }
516
517 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
518 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
519 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
520 magicmouse_emit_buttons(msc, clicks & 3);
521 input_report_rel(input, REL_X, x);
522 input_report_rel(input, REL_Y, y);
523 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
524 input->id.product ==
525 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
526 input_mt_sync_frame(input);
527 input_report_key(input, BTN_MOUSE, clicks & 1);
528 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
529 input_report_key(input, BTN_MOUSE, clicks & 1);
530 input_mt_report_pointer_emulation(input, true);
531 }
532
533 input_sync(input);
534 return 1;
535 }
536
magicmouse_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)537 static int magicmouse_event(struct hid_device *hdev, struct hid_field *field,
538 struct hid_usage *usage, __s32 value)
539 {
540 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
541 if ((msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
542 msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) &&
543 field->report->id == MOUSE2_REPORT_ID) {
544 /*
545 * magic_mouse_raw_event has done all the work. Skip hidinput.
546 *
547 * Specifically, hidinput may modify BTN_LEFT and BTN_RIGHT,
548 * breaking emulate_3button.
549 */
550 return 1;
551 }
552 return 0;
553 }
554
magicmouse_setup_input(struct input_dev * input,struct hid_device * hdev)555 static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
556 {
557 int error;
558 int mt_flags = 0;
559
560 __set_bit(EV_KEY, input->evbit);
561
562 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
563 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
564 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
565 __set_bit(BTN_LEFT, input->keybit);
566 __set_bit(BTN_RIGHT, input->keybit);
567 if (emulate_3button)
568 __set_bit(BTN_MIDDLE, input->keybit);
569
570 __set_bit(EV_REL, input->evbit);
571 __set_bit(REL_X, input->relbit);
572 __set_bit(REL_Y, input->relbit);
573 if (emulate_scroll_wheel) {
574 __set_bit(REL_WHEEL, input->relbit);
575 __set_bit(REL_HWHEEL, input->relbit);
576 __set_bit(REL_WHEEL_HI_RES, input->relbit);
577 __set_bit(REL_HWHEEL_HI_RES, input->relbit);
578 }
579 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
580 input->id.product ==
581 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
582 /* If the trackpad has been connected to a Mac, the name is
583 * automatically personalized, e.g., "José Expósito's Trackpad".
584 * When connected through Bluetooth, the personalized name is
585 * reported, however, when connected through USB the generic
586 * name is reported.
587 * Set the device name to ensure the same driver settings get
588 * loaded, whether connected through bluetooth or USB.
589 */
590 if (hdev->vendor == BT_VENDOR_ID_APPLE) {
591 if (input->id.version == TRACKPAD2_2021_BT_VERSION)
592 input->name = "Apple Inc. Magic Trackpad 2021";
593 else if (input->id.version == TRACKPAD_2024_BT_VERSION) {
594 input->name = "Apple Inc. Magic Trackpad USB-C";
595 } else {
596 input->name = "Apple Inc. Magic Trackpad";
597 }
598 } else { /* USB_VENDOR_ID_APPLE */
599 input->name = hdev->name;
600 }
601
602 __clear_bit(EV_MSC, input->evbit);
603 __clear_bit(BTN_0, input->keybit);
604 __clear_bit(BTN_RIGHT, input->keybit);
605 __clear_bit(BTN_MIDDLE, input->keybit);
606 __set_bit(BTN_MOUSE, input->keybit);
607 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
608 __set_bit(BTN_TOOL_FINGER, input->keybit);
609
610 mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
611 INPUT_MT_TRACK;
612 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
613 /* input->keybit is initialized with incorrect button info
614 * for Magic Trackpad. There really is only one physical
615 * button (BTN_LEFT == BTN_MOUSE). Make sure we don't
616 * advertise buttons that don't exist...
617 */
618 __clear_bit(BTN_RIGHT, input->keybit);
619 __clear_bit(BTN_MIDDLE, input->keybit);
620 __set_bit(BTN_MOUSE, input->keybit);
621 __set_bit(BTN_TOOL_FINGER, input->keybit);
622 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
623 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
624 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
625 __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
626 __set_bit(BTN_TOUCH, input->keybit);
627 __set_bit(INPUT_PROP_POINTER, input->propbit);
628 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
629 }
630
631
632 __set_bit(EV_ABS, input->evbit);
633
634 error = input_mt_init_slots(input, 16, mt_flags);
635 if (error)
636 return error;
637 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
638 4, 0);
639 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
640 4, 0);
641
642 /* Note: Touch Y position from the device is inverted relative
643 * to how pointer motion is reported (and relative to how USB
644 * HID recommends the coordinates work). This driver keeps
645 * the origin at the same position, and just uses the additive
646 * inverse of the reported Y.
647 */
648 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
649 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
650 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
651 input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
652 input_set_abs_params(input, ABS_MT_POSITION_X,
653 MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
654 input_set_abs_params(input, ABS_MT_POSITION_Y,
655 MOUSE_MIN_Y, MOUSE_MAX_Y, 4, 0);
656
657 input_abs_set_res(input, ABS_MT_POSITION_X,
658 MOUSE_RES_X);
659 input_abs_set_res(input, ABS_MT_POSITION_Y,
660 MOUSE_RES_Y);
661 } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
662 input->id.product ==
663 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
664 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
665 input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
666 input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
667 input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
668 TRACKPAD2_MAX_X, 0, 0);
669 input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
670 TRACKPAD2_MAX_Y, 0, 0);
671 input_set_abs_params(input, ABS_MT_POSITION_X,
672 TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
673 input_set_abs_params(input, ABS_MT_POSITION_Y,
674 TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
675
676 input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
677 input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
678 input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
679 input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
680 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
681 input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
682 input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
683 TRACKPAD_MAX_X, 4, 0);
684 input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
685 TRACKPAD_MAX_Y, 4, 0);
686 input_set_abs_params(input, ABS_MT_POSITION_X,
687 TRACKPAD_MIN_X, TRACKPAD_MAX_X, 4, 0);
688 input_set_abs_params(input, ABS_MT_POSITION_Y,
689 TRACKPAD_MIN_Y, TRACKPAD_MAX_Y, 4, 0);
690
691 input_abs_set_res(input, ABS_X, TRACKPAD_RES_X);
692 input_abs_set_res(input, ABS_Y, TRACKPAD_RES_Y);
693 input_abs_set_res(input, ABS_MT_POSITION_X,
694 TRACKPAD_RES_X);
695 input_abs_set_res(input, ABS_MT_POSITION_Y,
696 TRACKPAD_RES_Y);
697 }
698
699 input_set_events_per_packet(input, 60);
700
701 if (report_undeciphered &&
702 input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
703 input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
704 __set_bit(EV_MSC, input->evbit);
705 __set_bit(MSC_RAW, input->mscbit);
706 }
707
708 /*
709 * hid-input may mark device as using autorepeat, but neither
710 * the trackpad, nor the mouse actually want it.
711 */
712 __clear_bit(EV_REP, input->evbit);
713
714 return 0;
715 }
716
magicmouse_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)717 static int magicmouse_input_mapping(struct hid_device *hdev,
718 struct hid_input *hi, struct hid_field *field,
719 struct hid_usage *usage, unsigned long **bit, int *max)
720 {
721 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
722
723 if (!msc->input)
724 msc->input = hi->input;
725
726 /* Magic Trackpad does not give relative data after switching to MT */
727 if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
728 hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
729 hi->input->id.product ==
730 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
731 field->flags & HID_MAIN_ITEM_RELATIVE)
732 return -1;
733
734 return 0;
735 }
736
magicmouse_input_configured(struct hid_device * hdev,struct hid_input * hi)737 static int magicmouse_input_configured(struct hid_device *hdev,
738 struct hid_input *hi)
739
740 {
741 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
742 int ret;
743
744 if (!msc->input) {
745 hid_err(hdev, "magicmouse setup input failed (no input)");
746 return -EINVAL;
747 }
748
749 ret = magicmouse_setup_input(msc->input, hdev);
750 if (ret) {
751 hid_err(hdev, "magicmouse setup input failed (%d)\n", ret);
752 /* clean msc->input to notify probe() of the failure */
753 msc->input = NULL;
754 return ret;
755 }
756
757 return 0;
758 }
759
magicmouse_enable_multitouch(struct hid_device * hdev)760 static int magicmouse_enable_multitouch(struct hid_device *hdev)
761 {
762 const u8 *feature;
763 const u8 feature_mt[] = { 0xD7, 0x01 };
764 const u8 feature_mt_mouse2[] = { 0xF1, 0x02, 0x01 };
765 const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
766 const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
767 u8 *buf;
768 int ret;
769 int feature_size;
770
771 switch (hdev->product) {
772 case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2:
773 case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC:
774 switch (hdev->vendor) {
775 case BT_VENDOR_ID_APPLE:
776 feature_size = sizeof(feature_mt_trackpad2_bt);
777 feature = feature_mt_trackpad2_bt;
778 break;
779 default: /* USB_VENDOR_ID_APPLE */
780 feature_size = sizeof(feature_mt_trackpad2_usb);
781 feature = feature_mt_trackpad2_usb;
782 }
783 break;
784 case USB_DEVICE_ID_APPLE_MAGICMOUSE2:
785 case USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC:
786 feature_size = sizeof(feature_mt_mouse2);
787 feature = feature_mt_mouse2;
788 break;
789 default:
790 feature_size = sizeof(feature_mt);
791 feature = feature_mt;
792 }
793
794 buf = kmemdup(feature, feature_size, GFP_KERNEL);
795 if (!buf)
796 return -ENOMEM;
797
798 ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
799 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
800 kfree(buf);
801 return ret;
802 }
803
magicmouse_enable_mt_work(struct work_struct * work)804 static void magicmouse_enable_mt_work(struct work_struct *work)
805 {
806 struct magicmouse_sc *msc =
807 container_of(work, struct magicmouse_sc, work.work);
808 int ret;
809
810 ret = magicmouse_enable_multitouch(msc->hdev);
811 if (ret < 0)
812 hid_err(msc->hdev, "unable to request touch data (%d)\n", ret);
813 }
814
is_usb_magicmouse2(__u32 vendor,__u32 product)815 static bool is_usb_magicmouse2(__u32 vendor, __u32 product)
816 {
817 if (vendor != USB_VENDOR_ID_APPLE)
818 return false;
819 return product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
820 product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC;
821 }
822
is_usb_magictrackpad2(__u32 vendor,__u32 product)823 static bool is_usb_magictrackpad2(__u32 vendor, __u32 product)
824 {
825 if (vendor != USB_VENDOR_ID_APPLE)
826 return false;
827 return product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
828 product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC;
829 }
830
magicmouse_fetch_battery(struct hid_device * hdev)831 static int magicmouse_fetch_battery(struct hid_device *hdev)
832 {
833 #ifdef CONFIG_HID_BATTERY_STRENGTH
834 struct hid_report_enum *report_enum;
835 struct hid_report *report;
836 struct hid_battery *bat;
837
838 bat = hid_get_battery(hdev);
839 if (!bat ||
840 (!is_usb_magicmouse2(hdev->vendor, hdev->product) &&
841 !is_usb_magictrackpad2(hdev->vendor, hdev->product)))
842 return -1;
843
844 report_enum = &hdev->report_enum[bat->report_type];
845 report = report_enum->report_id_hash[bat->report_id];
846
847 if (!report || report->maxfield < 1)
848 return -1;
849
850 if (bat->capacity == bat->max)
851 return -1;
852
853 hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
854 return 0;
855 #else
856 return -1;
857 #endif
858 }
859
magicmouse_battery_timer_tick(struct timer_list * t)860 static void magicmouse_battery_timer_tick(struct timer_list *t)
861 {
862 struct magicmouse_sc *msc = timer_container_of(msc, t, battery_timer);
863 struct hid_device *hdev = msc->hdev;
864
865 if (magicmouse_fetch_battery(hdev) == 0) {
866 mod_timer(&msc->battery_timer,
867 jiffies + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC));
868 }
869 }
870
magicmouse_probe(struct hid_device * hdev,const struct hid_device_id * id)871 static int magicmouse_probe(struct hid_device *hdev,
872 const struct hid_device_id *id)
873 {
874 struct magicmouse_sc *msc;
875 struct hid_report *report;
876 int ret;
877
878 msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
879 if (msc == NULL) {
880 hid_err(hdev, "can't alloc magicmouse descriptor\n");
881 return -ENOMEM;
882 }
883
884 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
885 msc->hdev = hdev;
886 INIT_DEFERRABLE_WORK(&msc->work, magicmouse_enable_mt_work);
887
888 msc->quirks = id->driver_data;
889 hid_set_drvdata(hdev, msc);
890
891 ret = hid_parse(hdev);
892 if (ret) {
893 hid_err(hdev, "magicmouse hid parse failed\n");
894 return ret;
895 }
896
897 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
898 if (ret) {
899 hid_err(hdev, "magicmouse hw start failed\n");
900 return ret;
901 }
902
903 if (is_usb_magicmouse2(id->vendor, id->product) ||
904 is_usb_magictrackpad2(id->vendor, id->product)) {
905 timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0);
906 mod_timer(&msc->battery_timer,
907 jiffies + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC));
908 magicmouse_fetch_battery(hdev);
909 }
910
911 if (is_usb_magicmouse2(id->vendor, id->product) ||
912 (is_usb_magictrackpad2(id->vendor, id->product) &&
913 hdev->type != HID_TYPE_USBMOUSE))
914 return 0;
915
916 if (!msc->input) {
917 hid_err(hdev, "magicmouse input not registered\n");
918 ret = -ENOMEM;
919 goto err_stop_hw;
920 }
921
922 switch (id->product) {
923 case USB_DEVICE_ID_APPLE_MAGICMOUSE:
924 report = hid_register_report(hdev, HID_INPUT_REPORT, MOUSE_REPORT_ID, 0);
925 break;
926 case USB_DEVICE_ID_APPLE_MAGICMOUSE2:
927 case USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC:
928 report = hid_register_report(hdev, HID_INPUT_REPORT, MOUSE2_REPORT_ID, 0);
929 break;
930 case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2:
931 case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC:
932 switch (id->vendor) {
933 case BT_VENDOR_ID_APPLE:
934 report = hid_register_report(hdev, HID_INPUT_REPORT,
935 TRACKPAD2_BT_REPORT_ID, 0);
936 break;
937 default:
938 report = hid_register_report(hdev, HID_INPUT_REPORT,
939 TRACKPAD2_USB_REPORT_ID, 0);
940 }
941 break;
942 default: /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
943 report = hid_register_report(hdev, HID_INPUT_REPORT,
944 TRACKPAD_REPORT_ID, 0);
945 report = hid_register_report(hdev, HID_INPUT_REPORT,
946 DOUBLE_REPORT_ID, 0);
947 }
948
949 if (!report) {
950 hid_err(hdev, "unable to register touch report\n");
951 ret = -ENOMEM;
952 goto err_stop_hw;
953 }
954 report->size = 6;
955
956 /*
957 * Some devices repond with 'invalid report id' when feature
958 * report switching it into multitouch mode is sent to it.
959 *
960 * This results in -EIO from the _raw low-level transport callback,
961 * but there seems to be no other way of switching the mode.
962 * Thus the super-ugly hacky success check below.
963 */
964 ret = magicmouse_enable_multitouch(hdev);
965 if (ret != -EIO && ret < 0) {
966 hid_err(hdev, "unable to request touch data (%d)\n", ret);
967 goto err_stop_hw;
968 }
969 if (ret == -EIO && (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
970 id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC)) {
971 schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
972 }
973
974 return 0;
975 err_stop_hw:
976 if (is_usb_magicmouse2(id->vendor, id->product) ||
977 is_usb_magictrackpad2(id->vendor, id->product))
978 timer_delete_sync(&msc->battery_timer);
979
980 hid_hw_stop(hdev);
981 return ret;
982 }
983
magicmouse_remove(struct hid_device * hdev)984 static void magicmouse_remove(struct hid_device *hdev)
985 {
986 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
987
988 if (msc) {
989 cancel_delayed_work_sync(&msc->work);
990 if (is_usb_magicmouse2(hdev->vendor, hdev->product) ||
991 is_usb_magictrackpad2(hdev->vendor, hdev->product))
992 timer_delete_sync(&msc->battery_timer);
993 }
994
995 hid_hw_stop(hdev);
996 }
997
magicmouse_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)998 static const __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc,
999 unsigned int *rsize)
1000 {
1001 /*
1002 * Change the usage from:
1003 * 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 0
1004 * 0x09, 0x0b, // Usage (Vendor Usage 0x0b) 3
1005 * To:
1006 * 0x05, 0x01, // Usage Page (Generic Desktop) 0
1007 * 0x09, 0x02, // Usage (Mouse) 2
1008 */
1009 if ((is_usb_magicmouse2(hdev->vendor, hdev->product) ||
1010 is_usb_magictrackpad2(hdev->vendor, hdev->product)) &&
1011 *rsize >= 83 && rdesc[46] == 0x84 && rdesc[58] == 0x85) {
1012 hid_info(hdev,
1013 "fixing up magicmouse battery report descriptor\n");
1014 *rsize = *rsize - 1;
1015 rdesc = rdesc + 1;
1016
1017 rdesc[0] = 0x05;
1018 rdesc[1] = 0x01;
1019 rdesc[2] = 0x09;
1020 rdesc[3] = 0x02;
1021 }
1022
1023 return rdesc;
1024 }
1025
1026 static const struct hid_device_id magic_mice[] = {
1027 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
1028 USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
1029 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
1030 USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
1031 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
1032 USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
1033 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
1034 USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC), .driver_data = 0 },
1035 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
1036 USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC), .driver_data = 0 },
1037 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
1038 USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
1039 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
1040 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
1041 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
1042 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
1043 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
1044 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC), .driver_data = 0 },
1045 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
1046 USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC), .driver_data = 0 },
1047 { }
1048 };
1049 MODULE_DEVICE_TABLE(hid, magic_mice);
1050
1051 static struct hid_driver magicmouse_driver = {
1052 .name = "magicmouse",
1053 .id_table = magic_mice,
1054 .probe = magicmouse_probe,
1055 .remove = magicmouse_remove,
1056 .report_fixup = magicmouse_report_fixup,
1057 .raw_event = magicmouse_raw_event,
1058 .event = magicmouse_event,
1059 .input_mapping = magicmouse_input_mapping,
1060 .input_configured = magicmouse_input_configured,
1061 };
1062 module_hid_driver(magicmouse_driver);
1063
1064 MODULE_DESCRIPTION("Apple \"Magic\" Wireless Mouse driver");
1065 MODULE_LICENSE("GPL");
1066