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