Skip to content

Commit 2b0c086

Browse files
johnchen902rohitpidRicardoEPRodrigues
authored andcommitted
HID: magicmouse: add Apple Magic Mouse 2 support
Bluetooth device Vendor 004c (Apple) Device 0269 (Magic Mouse 2) Add support for Apple Magic Mouse 2, putting the device in multi-touch mode. Co-authored-by: Rohit Pidaparthi <[email protected]> Co-authored-by: RicardoEPRodrigues <[email protected]> Signed-off-by: John Chen <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 69aea9d commit 2b0c086

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

drivers/hid/hid-ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#define BT_VENDOR_ID_APPLE 0x004c
9494
#define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
9595
#define USB_DEVICE_ID_APPLE_MAGICMOUSE 0x030d
96+
#define USB_DEVICE_ID_APPLE_MAGICMOUSE2 0x0269
9697
#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD 0x030e
9798
#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 0x0265
9899
#define USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI 0x020e

drivers/hid/hid-magicmouse.c

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
5454
#define TRACKPAD2_USB_REPORT_ID 0x02
5555
#define TRACKPAD2_BT_REPORT_ID 0x31
5656
#define MOUSE_REPORT_ID 0x29
57+
#define MOUSE2_REPORT_ID 0x12
5758
#define DOUBLE_REPORT_ID 0xf7
5859
/* These definitions are not precise, but they're close enough. (Bits
5960
* 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
@@ -195,7 +196,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
195196
int id, x, y, size, orientation, touch_major, touch_minor, state, down;
196197
int pressure = 0;
197198

198-
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
199+
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
200+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
199201
id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
200202
x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
201203
y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
@@ -296,7 +298,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
296298
input_report_abs(input, ABS_MT_PRESSURE, pressure);
297299

298300
if (report_undeciphered) {
299-
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
301+
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
302+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
300303
input_event(input, EV_MSC, MSC_RAW, tdata[7]);
301304
else if (input->id.product !=
302305
USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
@@ -380,6 +383,34 @@ static int magicmouse_raw_event(struct hid_device *hdev,
380383
* ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
381384
*/
382385
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;
383414
case DOUBLE_REPORT_ID:
384415
/* Sometimes the trackpad sends two touch reports in one
385416
* packet.
@@ -392,7 +423,8 @@ static int magicmouse_raw_event(struct hid_device *hdev,
392423
return 0;
393424
}
394425

395-
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
426+
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
427+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
396428
magicmouse_emit_buttons(msc, clicks & 3);
397429
input_report_rel(input, REL_X, x);
398430
input_report_rel(input, REL_Y, y);
@@ -415,7 +447,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
415447

416448
__set_bit(EV_KEY, input->evbit);
417449

418-
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
450+
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
451+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
419452
__set_bit(BTN_LEFT, input->keybit);
420453
__set_bit(BTN_RIGHT, input->keybit);
421454
if (emulate_3button)
@@ -480,7 +513,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
480513
* the origin at the same position, and just uses the additive
481514
* inverse of the reported Y.
482515
*/
483-
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
516+
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
517+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
484518
input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
485519
input_set_abs_params(input, ABS_MT_POSITION_X,
486520
MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
@@ -586,6 +620,7 @@ static int magicmouse_probe(struct hid_device *hdev,
586620
{
587621
const u8 *feature;
588622
const u8 feature_mt[] = { 0xD7, 0x01 };
623+
const u8 feature_mt_mouse2[] = { 0xF1, 0x02, 0x01 };
589624
const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
590625
const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
591626
u8 *buf;
@@ -631,6 +666,9 @@ static int magicmouse_probe(struct hid_device *hdev,
631666
if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
632667
report = hid_register_report(hdev, HID_INPUT_REPORT,
633668
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);
634672
else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
635673
if (id->vendor == BT_VENDOR_ID_APPLE)
636674
report = hid_register_report(hdev, HID_INPUT_REPORT,
@@ -660,6 +698,9 @@ static int magicmouse_probe(struct hid_device *hdev,
660698
feature_size = sizeof(feature_mt_trackpad2_usb);
661699
feature = feature_mt_trackpad2_usb;
662700
}
701+
} else if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
702+
feature_size = sizeof(feature_mt_mouse2);
703+
feature = feature_mt_mouse2;
663704
} else {
664705
feature_size = sizeof(feature_mt);
665706
feature = feature_mt;
@@ -696,6 +737,8 @@ static int magicmouse_probe(struct hid_device *hdev,
696737
static const struct hid_device_id magic_mice[] = {
697738
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
698739
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 },
699742
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
700743
USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
701744
{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,

0 commit comments

Comments
 (0)