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