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