Code improvement for T2 support in magicmouse

This commit is contained in:
Aditya Garg
2025-03-17 22:45:56 +05:30
parent dd2e78d234
commit 4289a443f6
@@ -1,4 +1,4 @@
From c2e1c104044b02b21e8db083fb4401fd30e9f5a8 Mon Sep 17 00:00:00 2001
From 333c1742167b64aea0802996601b9bdc5aaa0a6e Mon Sep 17 00:00:00 2001
From: Aditya Garg <gargaditya08@live.com>
Date: Tue, 11 Mar 2025 18:44:06 +0530
Subject: [PATCH 3/3] HID: magicmouse: Add support for trackpads found on T2
@@ -10,11 +10,11 @@ It is the same format as type 4 in bcm5974.c
Signed-off-by: Aditya Garg <gargaditya08@live.com>
---
drivers/hid/hid-magicmouse.c | 408 ++++++++++++++++++++++++++++++-----
1 file changed, 360 insertions(+), 48 deletions(-)
drivers/hid/hid-magicmouse.c | 371 +++++++++++++++++++++++++++++------
1 file changed, 313 insertions(+), 58 deletions(-)
diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
index d5ab547b1..70934a221 100644
index d5ab547b1..eb62752d7 100644
--- a/drivers/hid/hid-magicmouse.c
+++ b/drivers/hid/hid-magicmouse.c
@@ -117,6 +117,94 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
@@ -216,10 +216,12 @@ index d5ab547b1..70934a221 100644
static int magicmouse_event(struct hid_device *hdev, struct hid_field *field,
struct hid_usage *usage, __s32 value)
{
@@ -1063,6 +1190,124 @@ static int magicmouse_setup_input_usb(struct input_dev *input,
@@ -1063,8 +1190,31 @@ static int magicmouse_setup_input_usb(struct input_dev *input,
return 0;
}
-static int magicmouse_setup_input_mtp(struct input_dev *input,
- struct hid_device *hdev)
+struct magicmouse_t2_properties {
+ u32 id;
+ int min_x;
@@ -241,12 +243,80 @@ index d5ab547b1..70934a221 100644
+ T2_TOUCHPAD_ENTRY(J152F),
+};
+
+static int magicmouse_setup_input_int_tpd(struct input_dev *input,
+ struct hid_device *hdev, int min_x, int min_y,
+ int max_x, int max_y, int res_x, int res_y,
+ bool query_dimensions)
{
int error;
int mt_flags = 0;
@@ -1105,19 +1255,17 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
input_abs_set_res(input, ABS_MT_PRESSURE, 1);
/* finger orientation */
- input_set_abs_params(input, ABS_MT_ORIENTATION, -J314_TP_MAX_FINGER_ORIENTATION,
- J314_TP_MAX_FINGER_ORIENTATION, 0, 0);
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -INTERNAL_TP_MAX_FINGER_ORIENTATION,
+ INTERNAL_TP_MAX_FINGER_ORIENTATION, 0, 0);
/* finger position */
- input_set_abs_params(input, ABS_MT_POSITION_X, J314_TP_MIN_X, J314_TP_MAX_X,
- 0, 0);
+ input_set_abs_params(input, ABS_MT_POSITION_X, min_x, max_x, 0, 0);
/* Y axis is inverted */
- input_set_abs_params(input, ABS_MT_POSITION_Y, -J314_TP_MAX_Y, -J314_TP_MIN_Y,
- 0, 0);
+ input_set_abs_params(input, ABS_MT_POSITION_Y, -max_y, -min_y, 0, 0);
/* X/Y resolution */
- input_abs_set_res(input, ABS_MT_POSITION_X, J314_TP_RES_X);
- input_abs_set_res(input, ABS_MT_POSITION_Y, J314_TP_RES_Y);
+ input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
+ input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
input_set_events_per_packet(input, 60);
@@ -1144,7 +1292,20 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
*/
input->open = magicmouse_open;
input->close = magicmouse_close;
- msc->query_dimensions = true;
+ msc->query_dimensions = query_dimensions;
+
+ return 0;
+}
+
+static int magicmouse_setup_input_mtp(struct input_dev *input,
+ struct hid_device *hdev)
+{
+ int ret = magicmouse_setup_input_int_tpd(input, hdev, J314_TP_MIN_X,
+ J314_TP_MIN_Y, J314_TP_MAX_X,
+ J314_TP_MAX_Y, J314_TP_RES_X,
+ J314_TP_RES_Y, true);
+ if (ret)
+ return ret;
return 0;
}
@@ -1152,7 +1313,34 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
static int magicmouse_setup_input_spi(struct input_dev *input,
struct hid_device *hdev)
{
- int ret = magicmouse_setup_input_mtp(input, hdev);
+ int ret = magicmouse_setup_input_int_tpd(input, hdev, J314_TP_MIN_X,
+ J314_TP_MIN_Y, J314_TP_MAX_X,
+ J314_TP_MAX_Y, J314_TP_RES_X,
+ J314_TP_RES_Y, true);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+static int magicmouse_setup_input_t2(struct input_dev *input,
+ struct hid_device *hdev)
+{
+ int min_x, min_y, max_x, max_y, res_x, res_y, error;
+ int mt_flags = 0;
+ struct magicmouse_sc *msc = hid_get_drvdata(hdev);
+ int min_x, min_y, max_x, max_y, res_x, res_y;
+
+ for (size_t i = 0; i < ARRAY_SIZE(magicmouse_t2_configs); i++) {
+ if (magicmouse_t2_configs[i].id == hdev->product) {
@@ -259,100 +329,12 @@ index d5ab547b1..70934a221 100644
+ }
+ }
+
+ __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
+ __clear_bit(BTN_0, input->keybit);
+ __clear_bit(BTN_RIGHT, input->keybit);
+ __clear_bit(BTN_MIDDLE, input->keybit);
+ __clear_bit(EV_REL, input->evbit);
+ __clear_bit(REL_X, input->relbit);
+ __clear_bit(REL_Y, input->relbit);
+
+ mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK;
+
+ /* finger touch area */
+ input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 5000, 0, 0);
+ input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 5000, 0, 0);
+
+ /* finger approach area */
+ input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 5000, 0, 0);
+ input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 5000, 0, 0);
+
+ /* Note: Touch Y position from the device is inverted relative
+ * to how pointer motion is reported (and relative to how USB
+ * HID recommends the coordinates work). This driver keeps
+ * the origin at the same position, and just uses the additive
+ * inverse of the reported Y.
+ */
+
+ input_set_abs_params(input, ABS_MT_PRESSURE, 0, 6000, 0, 0);
+
+ /*
+ * This makes libinput recognize this as a PressurePad and
+ * stop trying to use pressure for touch size. Pressure unit
+ * seems to be ~grams on these touchpads.
+ */
+ input_abs_set_res(input, ABS_MT_PRESSURE, 1);
+
+ /* finger orientation */
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -INTERNAL_TP_MAX_FINGER_ORIENTATION,
+ INTERNAL_TP_MAX_FINGER_ORIENTATION, 0, 0);
+
+ /* finger position */
+ input_set_abs_params(input, ABS_MT_POSITION_X, min_x, max_x,
+ 0, 0);
+ /* Y axis is inverted */
+ input_set_abs_params(input, ABS_MT_POSITION_Y, -max_y, -min_y,
+ 0, 0);
+
+ /* X/Y resolution */
+ input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
+ input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
+
+ input_set_events_per_packet(input, 60);
+
+ /* touchpad button */
+ input_set_capability(input, EV_KEY, BTN_MOUSE);
+
+ /*
+ * hid-input may mark device as using autorepeat, but the trackpad does
+ * not actually want it.
+ */
+ __clear_bit(EV_REP, input->evbit);
+
+ error = input_mt_init_slots(input, MAX_CONTACTS, mt_flags);
+ if (error)
+ return error;
+
+ /*
+ * Override the default input->open function to send the MT
+ * enable every time the device is opened. This ensures it works
+ * even if we missed a reset event due to the device being closed.
+ * input->close is overridden for symmetry.
+ *
+ * This also takes care of the dimensions query.
+ */
+ input->open = magicmouse_open;
+ input->close = magicmouse_close;
+ msc->query_dimensions = false;
+
+ return 0;
+}
+
static int magicmouse_setup_input_mtp(struct input_dev *input,
struct hid_device *hdev)
{
@@ -1105,8 +1350,8 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
input_abs_set_res(input, ABS_MT_PRESSURE, 1);
+ int ret = magicmouse_setup_input_int_tpd(input, hdev, min_x, min_y,
+ max_x, max_y, res_x, res_y, false);
if (ret)
return ret;
/* finger orientation */
- input_set_abs_params(input, ABS_MT_ORIENTATION, -J314_TP_MAX_FINGER_ORIENTATION,
- J314_TP_MAX_FINGER_ORIENTATION, 0, 0);
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -INTERNAL_TP_MAX_FINGER_ORIENTATION,
+ INTERNAL_TP_MAX_FINGER_ORIENTATION, 0, 0);
/* finger position */
input_set_abs_params(input, ABS_MT_POSITION_X, J314_TP_MIN_X, J314_TP_MAX_X,
@@ -1244,8 +1489,22 @@ static int magicmouse_probe(struct hid_device *hdev,
@@ -1244,8 +1432,22 @@ static int magicmouse_probe(struct hid_device *hdev,
int ret;
if ((id->bus == BUS_SPI || id->bus == BUS_HOST) && id->vendor == SPI_VENDOR_ID_APPLE &&
@@ -377,7 +359,7 @@ index d5ab547b1..70934a221 100644
msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
if (msc == NULL) {
@@ -1255,16 +1514,32 @@ static int magicmouse_probe(struct hid_device *hdev,
@@ -1255,16 +1457,32 @@ static int magicmouse_probe(struct hid_device *hdev,
// internal trackpad use a data format use input ops to avoid
// conflicts with the report ID.
@@ -416,7 +398,7 @@ index d5ab547b1..70934a221 100644
}
msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
@@ -1304,29 +1579,50 @@ static int magicmouse_probe(struct hid_device *hdev,
@@ -1304,29 +1522,50 @@ static int magicmouse_probe(struct hid_device *hdev,
goto err_stop_hw;
}
@@ -488,7 +470,7 @@ index d5ab547b1..70934a221 100644
}
if (!report) {
@@ -1414,6 +1710,22 @@ static const struct hid_device_id magic_mice[] = {
@@ -1414,6 +1653,22 @@ static const struct hid_device_id magic_mice[] = {
USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC), .driver_data = 0 },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC), .driver_data = 0 },