hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/hid/hid-magicmouse.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Apple "Magic" Wireless Mouse driver
34 *
....@@ -6,10 +7,6 @@
67 */
78
89 /*
9
- * This program is free software; you can redistribute it and/or modify it
10
- * under the terms of the GNU General Public License as published by the Free
11
- * Software Foundation; either version 2 of the License, or (at your option)
12
- * any later version.
1310 */
1411
1512 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -54,6 +51,8 @@
5451 MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
5552
5653 #define TRACKPAD_REPORT_ID 0x28
54
+#define TRACKPAD2_USB_REPORT_ID 0x02
55
+#define TRACKPAD2_BT_REPORT_ID 0x31
5756 #define MOUSE_REPORT_ID 0x29
5857 #define DOUBLE_REPORT_ID 0xf7
5958 /* These definitions are not precise, but they're close enough. (Bits
....@@ -90,6 +89,17 @@
9089 #define TRACKPAD_MAX_Y 2565
9190 #define TRACKPAD_RES_Y \
9291 ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
92
+
93
+#define TRACKPAD2_DIMENSION_X (float)16000
94
+#define TRACKPAD2_MIN_X -3678
95
+#define TRACKPAD2_MAX_X 3934
96
+#define TRACKPAD2_RES_X \
97
+ ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
98
+#define TRACKPAD2_DIMENSION_Y (float)11490
99
+#define TRACKPAD2_MIN_Y -2478
100
+#define TRACKPAD2_MAX_Y 2587
101
+#define TRACKPAD2_RES_Y \
102
+ ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
93103
94104 /**
95105 * struct magicmouse_sc - Tracks Magic Mouse-specific data.
....@@ -183,6 +193,7 @@
183193 {
184194 struct input_dev *input = msc->input;
185195 int id, x, y, size, orientation, touch_major, touch_minor, state, down;
196
+ int pressure = 0;
186197
187198 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
188199 id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
....@@ -194,6 +205,17 @@
194205 touch_minor = tdata[4];
195206 state = tdata[7] & TOUCH_STATE_MASK;
196207 down = state != TOUCH_STATE_NONE;
208
+ } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
209
+ id = tdata[8] & 0xf;
210
+ x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
211
+ y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
212
+ size = tdata[6];
213
+ orientation = (tdata[8] >> 5) - 4;
214
+ touch_major = tdata[4];
215
+ touch_minor = tdata[5];
216
+ pressure = tdata[7];
217
+ state = tdata[3] & 0xC0;
218
+ down = state == 0x80;
197219 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
198220 id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
199221 x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
....@@ -215,7 +237,8 @@
215237 /* If requested, emulate a scroll wheel by detecting small
216238 * vertical touch motions.
217239 */
218
- if (emulate_scroll_wheel) {
240
+ if (emulate_scroll_wheel && (input->id.product !=
241
+ USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) {
219242 unsigned long now = jiffies;
220243 int step_x = msc->touches[id].scroll_x - x;
221244 int step_y = msc->touches[id].scroll_y - y;
....@@ -269,10 +292,14 @@
269292 input_report_abs(input, ABS_MT_POSITION_X, x);
270293 input_report_abs(input, ABS_MT_POSITION_Y, y);
271294
295
+ if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
296
+ input_report_abs(input, ABS_MT_PRESSURE, pressure);
297
+
272298 if (report_undeciphered) {
273299 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
274300 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
275
- else /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
301
+ else if (input->id.product !=
302
+ USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
276303 input_event(input, EV_MSC, MSC_RAW, tdata[8]);
277304 }
278305 }
....@@ -287,6 +314,7 @@
287314
288315 switch (data[0]) {
289316 case TRACKPAD_REPORT_ID:
317
+ case TRACKPAD2_BT_REPORT_ID:
290318 /* Expect four bytes of prefix, and N*9 bytes of touch data. */
291319 if (size < 4 || ((size - 4) % 9) != 0)
292320 return 0;
....@@ -307,6 +335,22 @@
307335 *
308336 * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
309337 */
338
+ break;
339
+ case TRACKPAD2_USB_REPORT_ID:
340
+ /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
341
+ if (size < 12 || ((size - 12) % 9) != 0)
342
+ return 0;
343
+ npoints = (size - 12) / 9;
344
+ if (npoints > 15) {
345
+ hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
346
+ size);
347
+ return 0;
348
+ }
349
+ msc->ntouches = 0;
350
+ for (ii = 0; ii < npoints; ii++)
351
+ magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
352
+
353
+ clicks = data[1];
310354 break;
311355 case MOUSE_REPORT_ID:
312356 /* Expect six bytes of prefix, and N*8 bytes of touch data. */
....@@ -343,7 +387,7 @@
343387 magicmouse_raw_event(hdev, report, data + 2, data[1]);
344388 magicmouse_raw_event(hdev, report, data + 2 + data[1],
345389 size - 2 - data[1]);
346
- break;
390
+ return 0;
347391 default:
348392 return 0;
349393 }
....@@ -352,6 +396,9 @@
352396 magicmouse_emit_buttons(msc, clicks & 3);
353397 input_report_rel(input, REL_X, x);
354398 input_report_rel(input, REL_Y, y);
399
+ } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
400
+ input_mt_sync_frame(input);
401
+ input_report_key(input, BTN_MOUSE, clicks & 1);
355402 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
356403 input_report_key(input, BTN_MOUSE, clicks & 1);
357404 input_mt_report_pointer_emulation(input, true);
....@@ -364,6 +411,7 @@
364411 static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
365412 {
366413 int error;
414
+ int mt_flags = 0;
367415
368416 __set_bit(EV_KEY, input->evbit);
369417
....@@ -380,6 +428,22 @@
380428 __set_bit(REL_WHEEL, input->relbit);
381429 __set_bit(REL_HWHEEL, input->relbit);
382430 }
431
+ } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
432
+ /* setting the device name to ensure the same driver settings
433
+ * get loaded, whether connected through bluetooth or USB
434
+ */
435
+ input->name = "Apple Inc. Magic Trackpad 2";
436
+
437
+ __clear_bit(EV_MSC, input->evbit);
438
+ __clear_bit(BTN_0, input->keybit);
439
+ __clear_bit(BTN_RIGHT, input->keybit);
440
+ __clear_bit(BTN_MIDDLE, input->keybit);
441
+ __set_bit(BTN_MOUSE, input->keybit);
442
+ __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
443
+ __set_bit(BTN_TOOL_FINGER, input->keybit);
444
+
445
+ mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
446
+ INPUT_MT_TRACK;
383447 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
384448 /* input->keybit is initialized with incorrect button info
385449 * for Magic Trackpad. There really is only one physical
....@@ -402,14 +466,13 @@
402466
403467 __set_bit(EV_ABS, input->evbit);
404468
405
- error = input_mt_init_slots(input, 16, 0);
469
+ error = input_mt_init_slots(input, 16, mt_flags);
406470 if (error)
407471 return error;
408472 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
409473 4, 0);
410474 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
411475 4, 0);
412
- input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
413476
414477 /* Note: Touch Y position from the device is inverted relative
415478 * to how pointer motion is reported (and relative to how USB
....@@ -418,6 +481,7 @@
418481 * inverse of the reported Y.
419482 */
420483 if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
484
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
421485 input_set_abs_params(input, ABS_MT_POSITION_X,
422486 MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
423487 input_set_abs_params(input, ABS_MT_POSITION_Y,
....@@ -427,7 +491,25 @@
427491 MOUSE_RES_X);
428492 input_abs_set_res(input, ABS_MT_POSITION_Y,
429493 MOUSE_RES_Y);
494
+ } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
495
+ input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
496
+ input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
497
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
498
+ input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
499
+ TRACKPAD2_MAX_X, 0, 0);
500
+ input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
501
+ TRACKPAD2_MAX_Y, 0, 0);
502
+ input_set_abs_params(input, ABS_MT_POSITION_X,
503
+ TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
504
+ input_set_abs_params(input, ABS_MT_POSITION_Y,
505
+ TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
506
+
507
+ input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
508
+ input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
509
+ input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
510
+ input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
430511 } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
512
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
431513 input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
432514 TRACKPAD_MAX_X, 4, 0);
433515 input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
....@@ -447,7 +529,8 @@
447529
448530 input_set_events_per_packet(input, 60);
449531
450
- if (report_undeciphered) {
532
+ if (report_undeciphered &&
533
+ input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
451534 __set_bit(EV_MSC, input->evbit);
452535 __set_bit(MSC_RAW, input->mscbit);
453536 }
....@@ -471,7 +554,8 @@
471554 msc->input = hi->input;
472555
473556 /* Magic Trackpad does not give relative data after switching to MT */
474
- if (hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD &&
557
+ if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
558
+ hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
475559 field->flags & HID_MAIN_ITEM_RELATIVE)
476560 return -1;
477561
....@@ -500,11 +584,20 @@
500584 static int magicmouse_probe(struct hid_device *hdev,
501585 const struct hid_device_id *id)
502586 {
503
- const u8 feature[] = { 0xd7, 0x01 };
587
+ const u8 *feature;
588
+ const u8 feature_mt[] = { 0xD7, 0x01 };
589
+ const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
590
+ const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
504591 u8 *buf;
505592 struct magicmouse_sc *msc;
506593 struct hid_report *report;
507594 int ret;
595
+ int feature_size;
596
+
597
+ if (id->vendor == USB_VENDOR_ID_APPLE &&
598
+ id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
599
+ hdev->type != HID_TYPE_USBMOUSE)
600
+ return -ENODEV;
508601
509602 msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
510603 if (msc == NULL) {
....@@ -538,7 +631,14 @@
538631 if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
539632 report = hid_register_report(hdev, HID_INPUT_REPORT,
540633 MOUSE_REPORT_ID, 0);
541
- else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
634
+ else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
635
+ if (id->vendor == BT_VENDOR_ID_APPLE)
636
+ report = hid_register_report(hdev, HID_INPUT_REPORT,
637
+ TRACKPAD2_BT_REPORT_ID, 0);
638
+ else /* USB_VENDOR_ID_APPLE */
639
+ report = hid_register_report(hdev, HID_INPUT_REPORT,
640
+ TRACKPAD2_USB_REPORT_ID, 0);
641
+ } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
542642 report = hid_register_report(hdev, HID_INPUT_REPORT,
543643 TRACKPAD_REPORT_ID, 0);
544644 report = hid_register_report(hdev, HID_INPUT_REPORT,
....@@ -552,7 +652,20 @@
552652 }
553653 report->size = 6;
554654
555
- buf = kmemdup(feature, sizeof(feature), GFP_KERNEL);
655
+ if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
656
+ if (id->vendor == BT_VENDOR_ID_APPLE) {
657
+ feature_size = sizeof(feature_mt_trackpad2_bt);
658
+ feature = feature_mt_trackpad2_bt;
659
+ } else { /* USB_VENDOR_ID_APPLE */
660
+ feature_size = sizeof(feature_mt_trackpad2_usb);
661
+ feature = feature_mt_trackpad2_usb;
662
+ }
663
+ } else {
664
+ feature_size = sizeof(feature_mt);
665
+ feature = feature_mt;
666
+ }
667
+
668
+ buf = kmemdup(feature, feature_size, GFP_KERNEL);
556669 if (!buf) {
557670 ret = -ENOMEM;
558671 goto err_stop_hw;
....@@ -566,10 +679,10 @@
566679 * but there seems to be no other way of switching the mode.
567680 * Thus the super-ugly hacky success check below.
568681 */
569
- ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(feature),
682
+ ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
570683 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
571684 kfree(buf);
572
- if (ret != -EIO && ret != sizeof(feature)) {
685
+ if (ret != -EIO && ret != feature_size) {
573686 hid_err(hdev, "unable to request touch data (%d)\n", ret);
574687 goto err_stop_hw;
575688 }
....@@ -585,6 +698,10 @@
585698 USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
586699 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
587700 USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
701
+ { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
702
+ USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
703
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
704
+ USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
588705 { }
589706 };
590707 MODULE_DEVICE_TABLE(hid, magic_mice);