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