Skip to content

Commit b8d56ef

Browse files
AdityaGarg8Jiri Kosina
authored andcommitted
HID: magicmouse: Apple Magic Mouse 2 USB-C support
This patch adds support for USB-C model of Apple Magic Mouse 2. Except for the hardware ID, it should resemble the existing configuration for the older Magic Mouse 2. Signed-off-by: Aditya Garg <gargaditya08@live.com> Signed-off-by: Jiri Kosina <jkosina@suse.com>
1 parent 5e06802 commit b8d56ef

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

drivers/hid/hid-ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
9393
#define USB_DEVICE_ID_APPLE_MAGICMOUSE 0x030d
9494
#define USB_DEVICE_ID_APPLE_MAGICMOUSE2 0x0269
95+
#define USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC 0x0323
9596
#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD 0x030e
9697
#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 0x0265
9798
#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC 0x0324

drivers/hid/hid-magicmouse.c

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
218218
int pressure = 0;
219219

220220
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
221-
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
221+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
222+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
222223
id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
223224
x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
224225
y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
@@ -370,7 +371,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
370371

371372
if (report_undeciphered) {
372373
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
373-
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
374+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
375+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC)
374376
input_event(input, EV_MSC, MSC_RAW, tdata[7]);
375377
else if (input->id.product !=
376378
USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
@@ -497,7 +499,8 @@ static int magicmouse_raw_event(struct hid_device *hdev,
497499
}
498500

499501
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
500-
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
502+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
503+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
501504
magicmouse_emit_buttons(msc, clicks & 3);
502505
input_report_rel(input, REL_X, x);
503506
input_report_rel(input, REL_Y, y);
@@ -519,7 +522,8 @@ static int magicmouse_event(struct hid_device *hdev, struct hid_field *field,
519522
struct hid_usage *usage, __s32 value)
520523
{
521524
struct magicmouse_sc *msc = hid_get_drvdata(hdev);
522-
if (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 &&
525+
if ((msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
526+
msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) &&
523527
field->report->id == MOUSE2_REPORT_ID) {
524528
/*
525529
* magic_mouse_raw_event has done all the work. Skip hidinput.
@@ -540,7 +544,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
540544
__set_bit(EV_KEY, input->evbit);
541545

542546
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
543-
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
547+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
548+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
544549
__set_bit(BTN_LEFT, input->keybit);
545550
__set_bit(BTN_RIGHT, input->keybit);
546551
if (emulate_3button)
@@ -625,7 +630,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
625630
* inverse of the reported Y.
626631
*/
627632
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
628-
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
633+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
634+
input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
629635
input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
630636
input_set_abs_params(input, ABS_MT_POSITION_X,
631637
MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
@@ -741,19 +747,25 @@ static int magicmouse_enable_multitouch(struct hid_device *hdev)
741747
int ret;
742748
int feature_size;
743749

744-
if (hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
745-
hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
746-
if (hdev->vendor == BT_VENDOR_ID_APPLE) {
750+
switch (hdev->product) {
751+
case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2:
752+
case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC:
753+
switch (hdev->vendor) {
754+
case BT_VENDOR_ID_APPLE:
747755
feature_size = sizeof(feature_mt_trackpad2_bt);
748756
feature = feature_mt_trackpad2_bt;
749-
} else { /* USB_VENDOR_ID_APPLE */
757+
break;
758+
default: /* USB_VENDOR_ID_APPLE */
750759
feature_size = sizeof(feature_mt_trackpad2_usb);
751760
feature = feature_mt_trackpad2_usb;
752761
}
753-
} else if (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
762+
break;
763+
case USB_DEVICE_ID_APPLE_MAGICMOUSE2:
764+
case USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC:
754765
feature_size = sizeof(feature_mt_mouse2);
755766
feature = feature_mt_mouse2;
756-
} else {
767+
break;
768+
default:
757769
feature_size = sizeof(feature_mt);
758770
feature = feature_mt;
759771
}
@@ -787,6 +799,7 @@ static int magicmouse_fetch_battery(struct hid_device *hdev)
787799

788800
if (!hdev->battery || hdev->vendor != USB_VENDOR_ID_APPLE ||
789801
(hdev->product != USB_DEVICE_ID_APPLE_MAGICMOUSE2 &&
802+
hdev->product != USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC &&
790803
hdev->product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
791804
hdev->product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC))
792805
return -1;
@@ -857,6 +870,7 @@ static int magicmouse_probe(struct hid_device *hdev,
857870

858871
if (id->vendor == USB_VENDOR_ID_APPLE &&
859872
(id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
873+
id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC ||
860874
((id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
861875
id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
862876
hdev->type != HID_TYPE_USBMOUSE)))
@@ -868,21 +882,27 @@ static int magicmouse_probe(struct hid_device *hdev,
868882
goto err_stop_hw;
869883
}
870884

871-
if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
872-
report = hid_register_report(hdev, HID_INPUT_REPORT,
873-
MOUSE_REPORT_ID, 0);
874-
else if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
875-
report = hid_register_report(hdev, HID_INPUT_REPORT,
876-
MOUSE2_REPORT_ID, 0);
877-
else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
878-
id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
879-
if (id->vendor == BT_VENDOR_ID_APPLE)
885+
switch (id->product) {
886+
case USB_DEVICE_ID_APPLE_MAGICMOUSE:
887+
report = hid_register_report(hdev, HID_INPUT_REPORT, MOUSE_REPORT_ID, 0);
888+
break;
889+
case USB_DEVICE_ID_APPLE_MAGICMOUSE2:
890+
case USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC:
891+
report = hid_register_report(hdev, HID_INPUT_REPORT, MOUSE2_REPORT_ID, 0);
892+
break;
893+
case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2:
894+
case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC:
895+
switch (id->vendor) {
896+
case BT_VENDOR_ID_APPLE:
880897
report = hid_register_report(hdev, HID_INPUT_REPORT,
881898
TRACKPAD2_BT_REPORT_ID, 0);
882-
else /* USB_VENDOR_ID_APPLE */
899+
break;
900+
default:
883901
report = hid_register_report(hdev, HID_INPUT_REPORT,
884902
TRACKPAD2_USB_REPORT_ID, 0);
885-
} else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
903+
}
904+
break;
905+
default: /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
886906
report = hid_register_report(hdev, HID_INPUT_REPORT,
887907
TRACKPAD_REPORT_ID, 0);
888908
report = hid_register_report(hdev, HID_INPUT_REPORT,
@@ -909,7 +929,8 @@ static int magicmouse_probe(struct hid_device *hdev,
909929
hid_err(hdev, "unable to request touch data (%d)\n", ret);
910930
goto err_stop_hw;
911931
}
912-
if (ret == -EIO && id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
932+
if (ret == -EIO && (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
933+
id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC)) {
913934
schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
914935
}
915936

@@ -945,6 +966,7 @@ static const __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc,
945966
*/
946967
if (hdev->vendor == USB_VENDOR_ID_APPLE &&
947968
(hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
969+
hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC ||
948970
hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
949971
hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
950972
*rsize == 83 && rdesc[46] == 0x84 && rdesc[58] == 0x85) {
@@ -971,6 +993,10 @@ static const struct hid_device_id magic_mice[] = {
971993
USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
972994
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
973995
USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
996+
{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
997+
USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC), .driver_data = 0 },
998+
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
999+
USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC), .driver_data = 0 },
9741000
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
9751001
USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
9761002
{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,

0 commit comments

Comments
 (0)