Skip to content

Commit 2536456

Browse files
committed
HID: magicmouse: Query device dimensions via HID report
For SPI/MTP trackpads, query the dimensions via HID report instead of hardcoding values. TODO: Does this work for the USB/BT devices? Maybe we can get rid of the hardcoded sizes everywhere? Signed-off-by: Hector Martin <[email protected]>
1 parent 7be2d4c commit 2536456

1 file changed

Lines changed: 81 additions & 21 deletions

File tree

drivers/hid/hid-magicmouse.c

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
6262
#define SPI_REPORT_ID 0x02
6363
#define SPI_RESET_REPORT_ID 0x60
6464
#define MTP_REPORT_ID 0x75
65+
#define SENSOR_DIMENSIONS_REPORT_ID 0xd9
6566
#define USB_BATTERY_TIMEOUT_MS 60000
6667

6768
#define MAX_CONTACTS 16
@@ -116,6 +117,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
116117
#define TRACKPAD2_RES_Y \
117118
((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
118119

120+
/* These are fallback values, since the real values will be queried from the device. */
119121
#define J314_TP_DIMENSION_X (float)13000
120122
#define J314_TP_MIN_X -5900
121123
#define J314_TP_MAX_X 6500
@@ -139,6 +141,7 @@ struct magicmouse_input_ops {
139141
* struct magicmouse_sc - Tracks Magic Mouse-specific data.
140142
* @input: Input device through which we report events.
141143
* @quirks: Currently unused.
144+
* @query_dimensions: Whether to query and update dimensions on first open
142145
* @ntouches: Number of touches in most recent touch report.
143146
* @scroll_accel: Number of consecutive scroll motions.
144147
* @scroll_jiffies: Time of last scroll motion.
@@ -148,6 +151,7 @@ struct magicmouse_input_ops {
148151
struct magicmouse_sc {
149152
struct input_dev *input;
150153
unsigned long quirks;
154+
bool query_dimensions;
151155

152156
int ntouches;
153157
int scroll_accel;
@@ -171,6 +175,11 @@ struct magicmouse_sc {
171175
struct magicmouse_input_ops input_ops;
172176
};
173177

178+
static inline int le16_to_int(__le16 x)
179+
{
180+
return (signed short)le16_to_cpu(x);
181+
}
182+
174183
static int magicmouse_enable_multitouch(struct hid_device *hdev)
175184
{
176185
const u8 *feature;
@@ -239,19 +248,71 @@ static int magicmouse_open(struct input_dev *dev)
239248
* This results in -EIO from the _raw low-level transport callback,
240249
* but there seems to be no other way of switching the mode.
241250
* Thus the super-ugly hacky success check below.
251+
*
252+
* MTP devices do not need this.
242253
*/
243-
ret = magicmouse_enable_multitouch(hdev);
244-
if (ret == -EIO && hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
245-
schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
246-
return 0;
254+
if (hdev->bus != BUS_HOST) {
255+
ret = magicmouse_enable_multitouch(hdev);
256+
if (ret == -EIO && hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
257+
schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
258+
return 0;
259+
}
260+
if (ret < 0)
261+
hid_err(hdev, "unable to request touch data (%d)\n", ret);
247262
}
248-
if (ret < 0)
249-
hid_err(hdev, "unable to request touch data (%d)\n", ret);
250-
251263
/*
252264
* MT enable is usually not required after the first time, so don't
253265
* consider it fatal.
254266
*/
267+
268+
/*
269+
* For Apple Silicon trackpads, we want to query the dimensions on
270+
* device open. This is because doing so requires the firmware, but
271+
* we don't want to force a firmware load until the device is opened
272+
* for the first time. So do that here and update the input properties
273+
* just in time before userspace queries them.
274+
*/
275+
if (msc->query_dimensions) {
276+
struct input_dev *input = msc->input;
277+
u8 buf[32];
278+
struct {
279+
__le32 width;
280+
__le32 height;
281+
__le16 min_x;
282+
__le16 min_y;
283+
__le16 max_x;
284+
__le16 max_y;
285+
} dim;
286+
uint32_t x_span, y_span;
287+
288+
ret = hid_hw_raw_request(hdev, SENSOR_DIMENSIONS_REPORT_ID, buf, sizeof(buf), HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
289+
if (ret < (int)(1 + sizeof(dim))) {
290+
hid_err(hdev, "unable to request dimensions (%d)\n", ret);
291+
return ret;
292+
}
293+
294+
memcpy(&dim, buf + 1, sizeof(dim));
295+
296+
/* finger position */
297+
input_set_abs_params(input, ABS_MT_POSITION_X,
298+
le16_to_int(dim.min_x), le16_to_int(dim.max_x), 0, 0);
299+
/* Y axis is inverted */
300+
input_set_abs_params(input, ABS_MT_POSITION_Y,
301+
-le16_to_int(dim.max_y), -le16_to_int(dim.min_y), 0, 0);
302+
x_span = le16_to_int(dim.max_x) - le16_to_int(dim.min_x);
303+
y_span = le16_to_int(dim.max_y) - le16_to_int(dim.min_y);
304+
305+
/* X/Y resolution */
306+
input_abs_set_res(input, ABS_MT_POSITION_X, 100 * x_span / le32_to_cpu(dim.width) );
307+
input_abs_set_res(input, ABS_MT_POSITION_Y, 100 * y_span / le32_to_cpu(dim.height) );
308+
309+
/* copy info, as input_mt_init_slots() does */
310+
dev->absinfo[ABS_X] = dev->absinfo[ABS_MT_POSITION_X];
311+
dev->absinfo[ABS_Y] = dev->absinfo[ABS_MT_POSITION_Y];
312+
313+
msc->query_dimensions = false;
314+
}
315+
255316
return 0;
256317
}
257318

@@ -692,11 +753,6 @@ struct tp_mouse_report {
692753
u8 padding[4];
693754
};
694755

695-
static inline int le16_to_int(__le16 x)
696-
{
697-
return (signed short)le16_to_cpu(x);
698-
}
699-
700756
static void report_finger_data(struct input_dev *input, int slot,
701757
const struct input_mt_pos *pos,
702758
const struct tp_finger *f)
@@ -992,6 +1048,7 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
9921048
{
9931049
int error;
9941050
int mt_flags = 0;
1051+
struct magicmouse_sc *msc = hid_get_drvdata(hdev);
9951052

9961053
__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
9971054
__clear_bit(BTN_0, input->keybit);
@@ -1057,6 +1114,18 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
10571114
if (error)
10581115
return error;
10591116

1117+
/*
1118+
* Override the default input->open function to send the MT
1119+
* enable every time the device is opened. This ensures it works
1120+
* even if we missed a reset event due to the device being closed.
1121+
* input->close is overridden for symmetry.
1122+
*
1123+
* This also takes care of the dimensions query.
1124+
*/
1125+
input->open = magicmouse_open;
1126+
input->close = magicmouse_close;
1127+
msc->query_dimensions = true;
1128+
10601129
return 0;
10611130
}
10621131

@@ -1067,15 +1136,6 @@ static int magicmouse_setup_input_spi(struct input_dev *input,
10671136
if (ret)
10681137
return ret;
10691138

1070-
/*
1071-
* Override the default input->open function to send the MT
1072-
* enable every time the device is opened. This ensures it works
1073-
* even if we missed a reset event due to the device being closed.
1074-
* input->close is overridden for symmetry.
1075-
*/
1076-
input->open = magicmouse_open;
1077-
input->close = magicmouse_close;
1078-
10791139
return 0;
10801140
}
10811141

0 commit comments

Comments
 (0)