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